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" |
| 21 | #include "lldb/Core/Log.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 22 | #include "lldb/Core/Module.h" |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/Symbol.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 | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 26 | #include "lldb/Core/StreamFile.h" |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 27 | #include "lldb/Expression/ClangUserExpression.h" |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 28 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Host/Host.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 30 | #include "lldb/Host/Terminal.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | #include "lldb/Target/ABI.h" |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 32 | #include "lldb/Target/DynamicLoader.h" |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 33 | #include "lldb/Target/JITLoader.h" |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 34 | #include "lldb/Target/OperatingSystem.h" |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 35 | #include "lldb/Target/LanguageRuntime.h" |
| 36 | #include "lldb/Target/CPPLanguageRuntime.h" |
| 37 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 38 | #include "lldb/Target/Platform.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 40 | #include "lldb/Target/StopInfo.h" |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 41 | #include "lldb/Target/SystemRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | #include "lldb/Target/Target.h" |
| 43 | #include "lldb/Target/TargetList.h" |
| 44 | #include "lldb/Target/Thread.h" |
| 45 | #include "lldb/Target/ThreadPlan.h" |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 46 | #include "lldb/Target/ThreadPlanBase.h" |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 47 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 49 | #ifndef LLDB_DISABLE_POSIX |
| 50 | #include <spawn.h> |
| 51 | #endif |
| 52 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | using namespace lldb; |
| 54 | using namespace lldb_private; |
| 55 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 56 | |
| 57 | // Comment out line below to disable memory caching, overriding the process setting |
| 58 | // target.process.disable-memory-cache |
| 59 | #define ENABLE_MEMORY_CACHING |
| 60 | |
| 61 | #ifdef ENABLE_MEMORY_CACHING |
| 62 | #define DISABLE_MEM_CACHE_DEFAULT false |
| 63 | #else |
| 64 | #define DISABLE_MEM_CACHE_DEFAULT true |
| 65 | #endif |
| 66 | |
| 67 | class ProcessOptionValueProperties : public OptionValueProperties |
| 68 | { |
| 69 | public: |
| 70 | ProcessOptionValueProperties (const ConstString &name) : |
| 71 | OptionValueProperties (name) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | // This constructor is used when creating ProcessOptionValueProperties when it |
| 76 | // is part of a new lldb_private::Process instance. It will copy all current |
| 77 | // global property values as needed |
| 78 | ProcessOptionValueProperties (ProcessProperties *global_properties) : |
| 79 | OptionValueProperties(*global_properties->GetValueProperties()) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | virtual const Property * |
| 84 | GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const |
| 85 | { |
| 86 | // When gettings the value for a key from the process options, we will always |
| 87 | // try and grab the setting from the current process if there is one. Else we just |
| 88 | // use the one from this instance. |
| 89 | if (exe_ctx) |
| 90 | { |
| 91 | Process *process = exe_ctx->GetProcessPtr(); |
| 92 | if (process) |
| 93 | { |
| 94 | ProcessOptionValueProperties *instance_properties = static_cast<ProcessOptionValueProperties *>(process->GetValueProperties().get()); |
| 95 | if (this != instance_properties) |
| 96 | return instance_properties->ProtectedGetPropertyAtIndex (idx); |
| 97 | } |
| 98 | } |
| 99 | return ProtectedGetPropertyAtIndex (idx); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | static PropertyDefinition |
| 104 | g_properties[] = |
| 105 | { |
| 106 | { "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] | 107 | { "extra-startup-command", OptionValue::eTypeArray , false, OptionValue::eTypeString, NULL, NULL, "A list containing extra commands understood by the particular process plugin used. " |
| 108 | "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] | 109 | { "ignore-breakpoints-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, breakpoints will be ignored during expression evaluation." }, |
| 110 | { "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] | 111 | { "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] | 112 | { "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] | 113 | { "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] | 114 | { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } |
| 115 | }; |
| 116 | |
| 117 | enum { |
| 118 | ePropertyDisableMemCache, |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 119 | ePropertyExtraStartCommand, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 120 | ePropertyIgnoreBreakpointsInExpressions, |
| 121 | ePropertyUnwindOnErrorInExpressions, |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 122 | ePropertyPythonOSPluginPath, |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 123 | ePropertyStopOnSharedLibraryEvents, |
| 124 | ePropertyDetachKeepsStopped |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | ProcessProperties::ProcessProperties (bool is_global) : |
| 128 | Properties () |
| 129 | { |
| 130 | if (is_global) |
| 131 | { |
| 132 | m_collection_sp.reset (new ProcessOptionValueProperties(ConstString("process"))); |
| 133 | m_collection_sp->Initialize(g_properties); |
| 134 | m_collection_sp->AppendProperty(ConstString("thread"), |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 135 | ConstString("Settings specific to threads."), |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 136 | true, |
| 137 | Thread::GetGlobalProperties()->GetValueProperties()); |
| 138 | } |
| 139 | else |
| 140 | m_collection_sp.reset (new ProcessOptionValueProperties(Process::GetGlobalProperties().get())); |
| 141 | } |
| 142 | |
| 143 | ProcessProperties::~ProcessProperties() |
| 144 | { |
| 145 | } |
| 146 | |
| 147 | bool |
| 148 | ProcessProperties::GetDisableMemoryCache() const |
| 149 | { |
| 150 | const uint32_t idx = ePropertyDisableMemCache; |
| 151 | return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); |
| 152 | } |
| 153 | |
| 154 | Args |
| 155 | ProcessProperties::GetExtraStartupCommands () const |
| 156 | { |
| 157 | Args args; |
| 158 | const uint32_t idx = ePropertyExtraStartCommand; |
| 159 | m_collection_sp->GetPropertyAtIndexAsArgs(NULL, idx, args); |
| 160 | return args; |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | ProcessProperties::SetExtraStartupCommands (const Args &args) |
| 165 | { |
| 166 | const uint32_t idx = ePropertyExtraStartCommand; |
| 167 | m_collection_sp->SetPropertyAtIndexFromArgs(NULL, idx, args); |
| 168 | } |
| 169 | |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 170 | FileSpec |
| 171 | ProcessProperties::GetPythonOSPluginPath () const |
| 172 | { |
| 173 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 174 | return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx); |
| 175 | } |
| 176 | |
| 177 | void |
| 178 | ProcessProperties::SetPythonOSPluginPath (const FileSpec &file) |
| 179 | { |
| 180 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 181 | m_collection_sp->SetPropertyAtIndexAsFileSpec(NULL, idx, file); |
| 182 | } |
| 183 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 184 | |
| 185 | bool |
| 186 | ProcessProperties::GetIgnoreBreakpointsInExpressions () const |
| 187 | { |
| 188 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 189 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | ProcessProperties::SetIgnoreBreakpointsInExpressions (bool ignore) |
| 194 | { |
| 195 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 196 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore); |
| 197 | } |
| 198 | |
| 199 | bool |
| 200 | ProcessProperties::GetUnwindOnErrorInExpressions () const |
| 201 | { |
| 202 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 203 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | ProcessProperties::SetUnwindOnErrorInExpressions (bool ignore) |
| 208 | { |
| 209 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 210 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore); |
| 211 | } |
| 212 | |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 213 | bool |
| 214 | ProcessProperties::GetStopOnSharedLibraryEvents () const |
| 215 | { |
| 216 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 217 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 218 | } |
| 219 | |
| 220 | void |
| 221 | ProcessProperties::SetStopOnSharedLibraryEvents (bool stop) |
| 222 | { |
| 223 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 224 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop); |
| 225 | } |
| 226 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 227 | bool |
| 228 | ProcessProperties::GetDetachKeepsStopped () const |
| 229 | { |
| 230 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 231 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 232 | } |
| 233 | |
| 234 | void |
| 235 | ProcessProperties::SetDetachKeepsStopped (bool stop) |
| 236 | { |
| 237 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 238 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop); |
| 239 | } |
| 240 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 241 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 242 | ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 243 | { |
| 244 | const char *cstr; |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 245 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 246 | s.Printf (" pid = %" PRIu64 "\n", m_pid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 247 | |
| 248 | if (m_parent_pid != LLDB_INVALID_PROCESS_ID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 249 | s.Printf (" parent = %" PRIu64 "\n", m_parent_pid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 250 | |
| 251 | if (m_executable) |
| 252 | { |
| 253 | s.Printf (" name = %s\n", m_executable.GetFilename().GetCString()); |
| 254 | s.PutCString (" file = "); |
| 255 | m_executable.Dump(&s); |
| 256 | s.EOL(); |
| 257 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 258 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 259 | if (argc > 0) |
| 260 | { |
| 261 | for (uint32_t i=0; i<argc; i++) |
| 262 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 263 | const char *arg = m_arguments.GetArgumentAtIndex(i); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 264 | if (i < 10) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 265 | s.Printf (" arg[%u] = %s\n", i, arg); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 266 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 267 | s.Printf ("arg[%u] = %s\n", i, arg); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 270 | |
| 271 | const uint32_t envc = m_environment.GetArgumentCount(); |
| 272 | if (envc > 0) |
| 273 | { |
| 274 | for (uint32_t i=0; i<envc; i++) |
| 275 | { |
| 276 | const char *env = m_environment.GetArgumentAtIndex(i); |
| 277 | if (i < 10) |
| 278 | s.Printf (" env[%u] = %s\n", i, env); |
| 279 | else |
| 280 | s.Printf ("env[%u] = %s\n", i, env); |
| 281 | } |
| 282 | } |
| 283 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 284 | if (m_arch.IsValid()) |
| 285 | s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str()); |
| 286 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 287 | if (m_uid != 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->GetUserName (m_uid); |
| 290 | s.Printf (" uid = %-5u (%s)\n", m_uid, 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_gid != 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->GetGroupName (m_gid); |
| 295 | s.Printf (" gid = %-5u (%s)\n", m_gid, 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_euid != 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->GetUserName (m_euid); |
| 300 | s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 301 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 302 | if (m_egid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 303 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 304 | cstr = platform->GetGroupName (m_egid); |
| 305 | s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 310 | ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 311 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 312 | const char *label; |
| 313 | if (show_args || verbose) |
| 314 | label = "ARGUMENTS"; |
| 315 | else |
| 316 | label = "NAME"; |
| 317 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 318 | if (verbose) |
| 319 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 320 | 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] | 321 | s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n"); |
| 322 | } |
| 323 | else |
| 324 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 325 | s.Printf ("PID PARENT USER ARCH %s\n", label); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 326 | s.PutCString ("====== ====== ========== ======= ============================\n"); |
| 327 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 331 | ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 332 | { |
| 333 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
| 334 | { |
| 335 | const char *cstr; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 336 | s.Printf ("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 337 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 338 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 339 | if (verbose) |
| 340 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 341 | cstr = platform->GetUserName (m_uid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 342 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 343 | s.Printf ("%-10s ", cstr); |
| 344 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 345 | s.Printf ("%-10u ", m_uid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 346 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 347 | cstr = platform->GetGroupName (m_gid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 348 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 349 | s.Printf ("%-10s ", cstr); |
| 350 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 351 | s.Printf ("%-10u ", m_gid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 352 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 353 | cstr = platform->GetUserName (m_euid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 354 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 355 | s.Printf ("%-10s ", cstr); |
| 356 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 357 | s.Printf ("%-10u ", m_euid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 358 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 359 | cstr = platform->GetGroupName (m_egid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 360 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 361 | s.Printf ("%-10s ", cstr); |
| 362 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 363 | s.Printf ("%-10u ", m_egid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 364 | s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : ""); |
| 365 | } |
| 366 | else |
| 367 | { |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 368 | s.Printf ("%-10s %-7d %s ", |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 369 | platform->GetUserName (m_euid), |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 370 | (int)m_arch.GetTriple().getArchName().size(), |
| 371 | m_arch.GetTriple().getArchName().data()); |
| 372 | } |
| 373 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 374 | if (verbose || show_args) |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 375 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 376 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 377 | if (argc > 0) |
| 378 | { |
| 379 | for (uint32_t i=0; i<argc; i++) |
| 380 | { |
| 381 | if (i > 0) |
| 382 | s.PutChar (' '); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 383 | s.PutCString (m_arguments.GetArgumentAtIndex(i)); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | s.PutCString (GetName()); |
| 390 | } |
| 391 | |
| 392 | s.EOL(); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 396 | |
| 397 | void |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 398 | ProcessInfo::SetArguments (char const **argv, bool first_arg_is_executable) |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 399 | { |
| 400 | m_arguments.SetArguments (argv); |
| 401 | |
| 402 | // Is the first argument the executable? |
| 403 | if (first_arg_is_executable) |
| 404 | { |
| 405 | const char *first_arg = m_arguments.GetArgumentAtIndex (0); |
| 406 | if (first_arg) |
| 407 | { |
| 408 | // Yes the first argument is an executable, set it as the executable |
| 409 | // in the launch options. Don't resolve the file path as the path |
| 410 | // could be a remote platform path |
| 411 | const bool resolve = false; |
| 412 | m_executable.SetFile(first_arg, resolve); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } |
| 416 | void |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 417 | ProcessInfo::SetArguments (const Args& args, bool first_arg_is_executable) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 418 | { |
| 419 | // Copy all arguments |
| 420 | m_arguments = args; |
| 421 | |
| 422 | // Is the first argument the executable? |
| 423 | if (first_arg_is_executable) |
| 424 | { |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 425 | const char *first_arg = m_arguments.GetArgumentAtIndex (0); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 426 | if (first_arg) |
| 427 | { |
| 428 | // Yes the first argument is an executable, set it as the executable |
| 429 | // in the launch options. Don't resolve the file path as the path |
| 430 | // could be a remote platform path |
| 431 | const bool resolve = false; |
| 432 | m_executable.SetFile(first_arg, resolve); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 437 | void |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 438 | ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty) |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 439 | { |
| 440 | // If notthing was specified, then check the process for any default |
| 441 | // settings that were set with "settings set" |
| 442 | if (m_file_actions.empty()) |
| 443 | { |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 444 | if (m_flags.Test(eLaunchFlagDisableSTDIO)) |
| 445 | { |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 446 | AppendSuppressFileAction (STDIN_FILENO , true, false); |
| 447 | AppendSuppressFileAction (STDOUT_FILENO, false, true); |
| 448 | AppendSuppressFileAction (STDERR_FILENO, false, true); |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 449 | } |
| 450 | else |
| 451 | { |
| 452 | // Check for any values that might have gotten set with any of: |
| 453 | // (lldb) settings set target.input-path |
| 454 | // (lldb) settings set target.output-path |
| 455 | // (lldb) settings set target.error-path |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 456 | FileSpec in_path; |
| 457 | FileSpec out_path; |
| 458 | FileSpec err_path; |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 459 | if (target) |
| 460 | { |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 461 | in_path = target->GetStandardInputPath(); |
| 462 | out_path = target->GetStandardOutputPath(); |
| 463 | err_path = target->GetStandardErrorPath(); |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 466 | if (in_path || out_path || err_path) |
| 467 | { |
| 468 | char path[PATH_MAX]; |
| 469 | if (in_path && in_path.GetPath(path, sizeof(path))) |
| 470 | AppendOpenFileAction(STDIN_FILENO, path, true, false); |
| 471 | |
| 472 | if (out_path && out_path.GetPath(path, sizeof(path))) |
| 473 | AppendOpenFileAction(STDOUT_FILENO, path, false, true); |
| 474 | |
| 475 | if (err_path && err_path.GetPath(path, sizeof(path))) |
| 476 | AppendOpenFileAction(STDERR_FILENO, path, false, true); |
| 477 | } |
| 478 | else if (default_to_use_pty) |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 479 | { |
| 480 | if (m_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, NULL, 0)) |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 481 | { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 482 | const char *slave_path = m_pty.GetSlaveName (NULL, 0); |
| 483 | AppendOpenFileAction(STDIN_FILENO, slave_path, true, false); |
| 484 | AppendOpenFileAction(STDOUT_FILENO, slave_path, false, true); |
| 485 | AppendOpenFileAction(STDERR_FILENO, slave_path, false, true); |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 492 | |
| 493 | bool |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 494 | ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error, |
| 495 | bool localhost, |
| 496 | bool will_debug, |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 497 | bool first_arg_is_full_shell_command, |
| 498 | int32_t num_resumes) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 499 | { |
| 500 | error.Clear(); |
| 501 | |
| 502 | if (GetFlags().Test (eLaunchFlagLaunchInShell)) |
| 503 | { |
| 504 | const char *shell_executable = GetShell(); |
| 505 | if (shell_executable) |
| 506 | { |
| 507 | char shell_resolved_path[PATH_MAX]; |
| 508 | |
| 509 | if (localhost) |
| 510 | { |
| 511 | FileSpec shell_filespec (shell_executable, true); |
| 512 | |
| 513 | if (!shell_filespec.Exists()) |
| 514 | { |
| 515 | // Resolve the path in case we just got "bash", "sh" or "tcsh" |
| 516 | if (!shell_filespec.ResolveExecutableLocation ()) |
| 517 | { |
| 518 | error.SetErrorStringWithFormat("invalid shell path '%s'", shell_executable); |
| 519 | return false; |
| 520 | } |
| 521 | } |
| 522 | shell_filespec.GetPath (shell_resolved_path, sizeof(shell_resolved_path)); |
| 523 | shell_executable = shell_resolved_path; |
| 524 | } |
| 525 | |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 526 | const char **argv = GetArguments().GetConstArgumentVector (); |
| 527 | if (argv == NULL || argv[0] == NULL) |
| 528 | return false; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 529 | Args shell_arguments; |
| 530 | std::string safe_arg; |
| 531 | shell_arguments.AppendArgument (shell_executable); |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 532 | shell_arguments.AppendArgument ("-c"); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 533 | StreamString shell_command; |
| 534 | if (will_debug) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 535 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 536 | // Add a modified PATH environment variable in case argv[0] |
| 537 | // is a relative path |
| 538 | const char *argv0 = argv[0]; |
| 539 | if (argv0 && (argv0[0] != '/' && argv0[0] != '~')) |
| 540 | { |
| 541 | // We have a relative path to our executable which may not work if |
| 542 | // we just try to run "a.out" (without it being converted to "./a.out") |
| 543 | const char *working_dir = GetWorkingDirectory(); |
Greg Clayton | 8938f8d | 2013-02-14 03:54:39 +0000 | [diff] [blame] | 544 | // Be sure to put quotes around PATH's value in case any paths have spaces... |
| 545 | std::string new_path("PATH=\""); |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 546 | const size_t empty_path_len = new_path.size(); |
| 547 | |
| 548 | if (working_dir && working_dir[0]) |
| 549 | { |
| 550 | new_path += working_dir; |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | char current_working_dir[PATH_MAX]; |
| 555 | const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir)); |
| 556 | if (cwd && cwd[0]) |
| 557 | new_path += cwd; |
| 558 | } |
| 559 | const char *curr_path = getenv("PATH"); |
| 560 | if (curr_path) |
| 561 | { |
| 562 | if (new_path.size() > empty_path_len) |
| 563 | new_path += ':'; |
| 564 | new_path += curr_path; |
| 565 | } |
Greg Clayton | 8938f8d | 2013-02-14 03:54:39 +0000 | [diff] [blame] | 566 | new_path += "\" "; |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 567 | shell_command.PutCString(new_path.c_str()); |
| 568 | } |
| 569 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 570 | shell_command.PutCString ("exec"); |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 571 | |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 572 | // Only Apple supports /usr/bin/arch being able to specify the architecture |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 573 | if (GetArchitecture().IsValid()) |
| 574 | { |
| 575 | shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName()); |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 576 | // Set the resume count to 2: |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 577 | // 1 - stop in shell |
| 578 | // 2 - stop in /usr/bin/arch |
| 579 | // 3 - then we will stop in our program |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 580 | SetResumeCount(num_resumes + 1); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 581 | } |
| 582 | else |
| 583 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 584 | // Set the resume count to 1: |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 585 | // 1 - stop in shell |
| 586 | // 2 - then we will stop in our program |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 587 | SetResumeCount(num_resumes); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 588 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 589 | } |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 590 | |
| 591 | if (first_arg_is_full_shell_command) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 592 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 593 | // There should only be one argument that is the shell command itself to be used as is |
| 594 | if (argv[0] && !argv[1]) |
| 595 | shell_command.Printf("%s", argv[0]); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 596 | else |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 597 | return false; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 598 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 599 | else |
| 600 | { |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 601 | for (size_t i=0; argv[i] != NULL; ++i) |
| 602 | { |
| 603 | const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg); |
| 604 | shell_command.Printf(" %s", arg); |
| 605 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 606 | } |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 607 | shell_arguments.AppendArgument (shell_command.GetString().c_str()); |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 608 | m_executable.SetFile(shell_executable, false); |
| 609 | m_arguments = shell_arguments; |
| 610 | return true; |
| 611 | } |
| 612 | else |
| 613 | { |
| 614 | error.SetErrorString ("invalid shell path"); |
| 615 | } |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | error.SetErrorString ("not launching in shell"); |
| 620 | } |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 625 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 626 | ProcessLaunchInfo::FileAction::Open (int fd, const char *path, bool read, bool write) |
| 627 | { |
| 628 | if ((read || write) && fd >= 0 && path && path[0]) |
| 629 | { |
| 630 | m_action = eFileActionOpen; |
| 631 | m_fd = fd; |
| 632 | if (read && write) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 633 | m_arg = O_NOCTTY | O_CREAT | O_RDWR; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 634 | else if (read) |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 635 | m_arg = O_NOCTTY | O_RDONLY; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 636 | else |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 637 | m_arg = O_NOCTTY | O_CREAT | O_WRONLY; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 638 | m_path.assign (path); |
| 639 | return true; |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | Clear(); |
| 644 | } |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | bool |
| 649 | ProcessLaunchInfo::FileAction::Close (int fd) |
| 650 | { |
| 651 | Clear(); |
| 652 | if (fd >= 0) |
| 653 | { |
| 654 | m_action = eFileActionClose; |
| 655 | m_fd = fd; |
| 656 | } |
| 657 | return m_fd >= 0; |
| 658 | } |
| 659 | |
| 660 | |
| 661 | bool |
| 662 | ProcessLaunchInfo::FileAction::Duplicate (int fd, int dup_fd) |
| 663 | { |
| 664 | Clear(); |
| 665 | if (fd >= 0 && dup_fd >= 0) |
| 666 | { |
| 667 | m_action = eFileActionDuplicate; |
| 668 | m_fd = fd; |
| 669 | m_arg = dup_fd; |
| 670 | } |
| 671 | return m_fd >= 0; |
| 672 | } |
| 673 | |
| 674 | |
| 675 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 676 | #ifndef LLDB_DISABLE_POSIX |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 677 | bool |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 678 | ProcessLaunchInfo::FileAction::AddPosixSpawnFileAction (void *_file_actions, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 679 | const FileAction *info, |
| 680 | Log *log, |
| 681 | Error& error) |
| 682 | { |
| 683 | if (info == NULL) |
| 684 | return false; |
| 685 | |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 686 | posix_spawn_file_actions_t *file_actions = reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions); |
| 687 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 688 | switch (info->m_action) |
| 689 | { |
| 690 | case eFileActionNone: |
| 691 | error.Clear(); |
| 692 | break; |
| 693 | |
| 694 | case eFileActionClose: |
| 695 | if (info->m_fd == -1) |
| 696 | error.SetErrorString ("invalid fd for posix_spawn_file_actions_addclose(...)"); |
| 697 | else |
| 698 | { |
| 699 | error.SetError (::posix_spawn_file_actions_addclose (file_actions, info->m_fd), |
| 700 | eErrorTypePOSIX); |
| 701 | if (log && (error.Fail() || log)) |
| 702 | error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 703 | static_cast<void*>(file_actions), info->m_fd); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 704 | } |
| 705 | break; |
| 706 | |
| 707 | case eFileActionDuplicate: |
| 708 | if (info->m_fd == -1) |
| 709 | error.SetErrorString ("invalid fd for posix_spawn_file_actions_adddup2(...)"); |
| 710 | else if (info->m_arg == -1) |
| 711 | error.SetErrorString ("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)"); |
| 712 | else |
| 713 | { |
| 714 | error.SetError (::posix_spawn_file_actions_adddup2 (file_actions, info->m_fd, info->m_arg), |
| 715 | eErrorTypePOSIX); |
| 716 | if (log && (error.Fail() || log)) |
| 717 | error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 718 | static_cast<void*>(file_actions), info->m_fd, |
| 719 | info->m_arg); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 720 | } |
| 721 | break; |
| 722 | |
| 723 | case eFileActionOpen: |
| 724 | if (info->m_fd == -1) |
| 725 | error.SetErrorString ("invalid fd in posix_spawn_file_actions_addopen(...)"); |
| 726 | else |
| 727 | { |
| 728 | int oflag = info->m_arg; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 729 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 730 | mode_t mode = 0; |
| 731 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 732 | if (oflag & O_CREAT) |
| 733 | mode = 0640; |
| 734 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 735 | error.SetError (::posix_spawn_file_actions_addopen (file_actions, |
| 736 | info->m_fd, |
| 737 | info->m_path.c_str(), |
| 738 | oflag, |
| 739 | mode), |
| 740 | eErrorTypePOSIX); |
| 741 | if (error.Fail() || log) |
| 742 | error.PutToLog(log, |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 743 | "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)", |
| 744 | static_cast<void*>(file_actions), info->m_fd, |
| 745 | info->m_path.c_str(), oflag, mode); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 746 | } |
| 747 | break; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 748 | } |
| 749 | return error.Success(); |
| 750 | } |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 751 | #endif |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 752 | |
| 753 | Error |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 754 | ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 755 | { |
| 756 | Error error; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 757 | const int short_option = m_getopt_table[option_idx].val; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 758 | |
| 759 | switch (short_option) |
| 760 | { |
| 761 | case 's': // Stop at program entry point |
| 762 | launch_info.GetFlags().Set (eLaunchFlagStopAtEntry); |
| 763 | break; |
| 764 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 765 | case 'i': // STDIN for read only |
| 766 | { |
| 767 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 768 | if (action.Open (STDIN_FILENO, option_arg, true, false)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 769 | launch_info.AppendFileAction (action); |
| 770 | } |
| 771 | break; |
| 772 | |
| 773 | case 'o': // Open STDOUT for write only |
| 774 | { |
| 775 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 776 | if (action.Open (STDOUT_FILENO, option_arg, false, true)) |
| 777 | launch_info.AppendFileAction (action); |
| 778 | } |
| 779 | break; |
| 780 | |
| 781 | case 'e': // STDERR for write only |
| 782 | { |
| 783 | ProcessLaunchInfo::FileAction action; |
| 784 | if (action.Open (STDERR_FILENO, option_arg, false, true)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 785 | launch_info.AppendFileAction (action); |
| 786 | } |
| 787 | break; |
| 788 | |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 789 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 790 | case 'p': // Process plug-in name |
| 791 | launch_info.SetProcessPluginName (option_arg); |
| 792 | break; |
| 793 | |
| 794 | case 'n': // Disable STDIO |
| 795 | { |
| 796 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 797 | if (action.Open (STDIN_FILENO, "/dev/null", true, false)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 798 | launch_info.AppendFileAction (action); |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 799 | if (action.Open (STDOUT_FILENO, "/dev/null", false, true)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 800 | launch_info.AppendFileAction (action); |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 801 | if (action.Open (STDERR_FILENO, "/dev/null", false, true)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 802 | launch_info.AppendFileAction (action); |
| 803 | } |
| 804 | break; |
| 805 | |
| 806 | case 'w': |
| 807 | launch_info.SetWorkingDirectory (option_arg); |
| 808 | break; |
| 809 | |
| 810 | case 't': // Open process in new terminal window |
| 811 | launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY); |
| 812 | break; |
| 813 | |
| 814 | case 'a': |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 815 | if (!launch_info.GetArchitecture().SetTriple (option_arg, m_interpreter.GetPlatform(true).get())) |
| 816 | launch_info.GetArchitecture().SetTriple (option_arg); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 817 | break; |
| 818 | |
| 819 | case 'A': |
| 820 | launch_info.GetFlags().Set (eLaunchFlagDisableASLR); |
| 821 | break; |
| 822 | |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 823 | case 'c': |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 824 | if (option_arg && option_arg[0]) |
| 825 | launch_info.SetShell (option_arg); |
| 826 | else |
Ed Maste | b8ca4a2 | 2013-09-03 23:04:53 +0000 | [diff] [blame] | 827 | launch_info.SetShell (LLDB_DEFAULT_SHELL); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 828 | break; |
| 829 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 830 | case 'v': |
| 831 | launch_info.GetEnvironmentEntries().AppendArgument(option_arg); |
| 832 | break; |
| 833 | |
| 834 | default: |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 835 | error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 836 | break; |
| 837 | |
| 838 | } |
| 839 | return error; |
| 840 | } |
| 841 | |
| 842 | OptionDefinition |
| 843 | ProcessLaunchCommandOptions::g_option_table[] = |
| 844 | { |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 845 | { 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."}, |
| 846 | { LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Disable address space layout randomization when launching a process."}, |
| 847 | { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."}, |
| 848 | { LLDB_OPT_SET_ALL, false, "working-dir", 'w', OptionParser::eRequiredArgument, NULL, 0, eArgTypeDirectoryName, "Set the current working directory to <path> when running the inferior."}, |
| 849 | { LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."}, |
| 850 | { 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."}, |
| 851 | { 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] | 852 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 853 | { LLDB_OPT_SET_1 , false, "stdin", 'i', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Redirect stdin for the process to <filename>."}, |
| 854 | { LLDB_OPT_SET_1 , false, "stdout", 'o', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFilename, "Redirect stdout for the process to <filename>."}, |
| 855 | { 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] | 856 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 857 | { 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] | 858 | |
Virgile Bello | e2607b5 | 2013-09-05 16:42:23 +0000 | [diff] [blame] | 859 | { 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] | 860 | |
| 861 | { 0 , false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
| 862 | }; |
| 863 | |
| 864 | |
| 865 | |
| 866 | bool |
| 867 | ProcessInstanceInfoMatch::NameMatches (const char *process_name) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 868 | { |
| 869 | if (m_name_match_type == eNameMatchIgnore || process_name == NULL) |
| 870 | return true; |
| 871 | const char *match_name = m_match_info.GetName(); |
| 872 | if (!match_name) |
| 873 | return true; |
| 874 | |
| 875 | return lldb_private::NameMatches (process_name, m_name_match_type, match_name); |
| 876 | } |
| 877 | |
| 878 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 879 | ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 880 | { |
| 881 | if (!NameMatches (proc_info.GetName())) |
| 882 | return false; |
| 883 | |
| 884 | if (m_match_info.ProcessIDIsValid() && |
| 885 | m_match_info.GetProcessID() != proc_info.GetProcessID()) |
| 886 | return false; |
| 887 | |
| 888 | if (m_match_info.ParentProcessIDIsValid() && |
| 889 | m_match_info.GetParentProcessID() != proc_info.GetParentProcessID()) |
| 890 | return false; |
| 891 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 892 | if (m_match_info.UserIDIsValid () && |
| 893 | m_match_info.GetUserID() != proc_info.GetUserID()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 894 | return false; |
| 895 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 896 | if (m_match_info.GroupIDIsValid () && |
| 897 | m_match_info.GetGroupID() != proc_info.GetGroupID()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 898 | return false; |
| 899 | |
| 900 | if (m_match_info.EffectiveUserIDIsValid () && |
| 901 | m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID()) |
| 902 | return false; |
| 903 | |
| 904 | if (m_match_info.EffectiveGroupIDIsValid () && |
| 905 | m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID()) |
| 906 | return false; |
| 907 | |
| 908 | if (m_match_info.GetArchitecture().IsValid() && |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 909 | !m_match_info.GetArchitecture().IsCompatibleMatch(proc_info.GetArchitecture())) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 910 | return false; |
| 911 | return true; |
| 912 | } |
| 913 | |
| 914 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 915 | ProcessInstanceInfoMatch::MatchAllProcesses () const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 916 | { |
| 917 | if (m_name_match_type != eNameMatchIgnore) |
| 918 | return false; |
| 919 | |
| 920 | if (m_match_info.ProcessIDIsValid()) |
| 921 | return false; |
| 922 | |
| 923 | if (m_match_info.ParentProcessIDIsValid()) |
| 924 | return false; |
| 925 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 926 | if (m_match_info.UserIDIsValid ()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 927 | return false; |
| 928 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 929 | if (m_match_info.GroupIDIsValid ()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 930 | return false; |
| 931 | |
| 932 | if (m_match_info.EffectiveUserIDIsValid ()) |
| 933 | return false; |
| 934 | |
| 935 | if (m_match_info.EffectiveGroupIDIsValid ()) |
| 936 | return false; |
| 937 | |
| 938 | if (m_match_info.GetArchitecture().IsValid()) |
| 939 | return false; |
| 940 | |
| 941 | if (m_match_all_users) |
| 942 | return false; |
| 943 | |
| 944 | return true; |
| 945 | |
| 946 | } |
| 947 | |
| 948 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 949 | ProcessInstanceInfoMatch::Clear() |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 950 | { |
| 951 | m_match_info.Clear(); |
| 952 | m_name_match_type = eNameMatchIgnore; |
| 953 | m_match_all_users = false; |
| 954 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 955 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 956 | ProcessSP |
| 957 | 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] | 958 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 959 | static uint32_t g_process_unique_id = 0; |
| 960 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 961 | ProcessSP process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 962 | ProcessCreateInstance create_callback = NULL; |
| 963 | if (plugin_name) |
| 964 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 965 | ConstString const_plugin_name(plugin_name); |
| 966 | create_callback = PluginManager::GetProcessCreateCallbackForPluginName (const_plugin_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 967 | if (create_callback) |
| 968 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 969 | process_sp = create_callback(target, listener, crash_file_path); |
| 970 | if (process_sp) |
| 971 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 972 | if (process_sp->CanDebug(target, true)) |
| 973 | { |
| 974 | process_sp->m_process_unique_id = ++g_process_unique_id; |
| 975 | } |
| 976 | else |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 977 | process_sp.reset(); |
| 978 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | else |
| 982 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 983 | for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 984 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 985 | process_sp = create_callback(target, listener, crash_file_path); |
| 986 | if (process_sp) |
| 987 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 988 | if (process_sp->CanDebug(target, false)) |
| 989 | { |
| 990 | process_sp->m_process_unique_id = ++g_process_unique_id; |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 991 | break; |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 992 | } |
| 993 | else |
| 994 | process_sp.reset(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 995 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 996 | } |
| 997 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 998 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 1001 | ConstString & |
| 1002 | Process::GetStaticBroadcasterClass () |
| 1003 | { |
| 1004 | static ConstString class_name ("lldb.process"); |
| 1005 | return class_name; |
| 1006 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1007 | |
| 1008 | //---------------------------------------------------------------------- |
| 1009 | // Process constructor |
| 1010 | //---------------------------------------------------------------------- |
| 1011 | Process::Process(Target &target, Listener &listener) : |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1012 | ProcessProperties (false), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1013 | UserID (LLDB_INVALID_PROCESS_ID), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 1014 | Broadcaster (&(target.GetDebugger()), "lldb.process"), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1015 | m_target (target), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1016 | m_public_state (eStateUnloaded), |
| 1017 | m_private_state (eStateUnloaded), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 1018 | m_private_state_broadcaster (NULL, "lldb.process.internal_state_broadcaster"), |
| 1019 | m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1020 | m_private_state_listener ("lldb.process.internal_state_listener"), |
| 1021 | m_private_state_control_wait(), |
| 1022 | m_private_state_thread (LLDB_INVALID_HOST_THREAD), |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1023 | m_mod_id (), |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 1024 | m_process_unique_id(0), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1025 | m_thread_index_id (0), |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1026 | m_thread_id_to_index_id_map (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1027 | m_exit_status (-1), |
| 1028 | m_exit_string (), |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1029 | m_thread_mutex (Mutex::eMutexTypeRecursive), |
| 1030 | m_thread_list_real (this), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1031 | m_thread_list (this), |
Jason Molenda | 864f1cc | 2013-11-11 05:20:44 +0000 | [diff] [blame] | 1032 | m_extended_thread_list (this), |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 1033 | m_extended_thread_stop_id (0), |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1034 | m_queue_list (this), |
| 1035 | m_queue_list_stop_id (0), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1036 | m_notifications (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1037 | m_image_tokens (), |
| 1038 | m_listener (listener), |
| 1039 | m_breakpoint_site_list (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1040 | m_dynamic_checkers_ap (), |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1041 | m_unix_signals (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1042 | m_abi_sp (), |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1043 | m_process_input_reader (), |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1044 | m_stdio_communication ("process.stdio"), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1045 | m_stdio_communication_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1046 | m_stdout_data (), |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 1047 | m_stderr_data (), |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 1048 | m_profile_data_comm_mutex (Mutex::eMutexTypeRecursive), |
| 1049 | m_profile_data (), |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 1050 | m_memory_cache (*this), |
| 1051 | m_allocated_memory_cache (*this), |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1052 | m_should_detach (false), |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 1053 | m_next_event_action_ap(), |
Greg Clayton | 9624985 | 2013-04-18 16:57:27 +0000 | [diff] [blame] | 1054 | m_public_run_lock (), |
Greg Clayton | 9624985 | 2013-04-18 16:57:27 +0000 | [diff] [blame] | 1055 | m_private_run_lock (), |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 1056 | m_currently_handling_event(false), |
Jim Ingham | 4fc6cb9 | 2012-08-22 21:34:33 +0000 | [diff] [blame] | 1057 | m_finalize_called(false), |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 1058 | m_clear_thread_plans_on_stop (false), |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1059 | m_force_next_event_delivery(false), |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 1060 | m_last_broadcast_state (eStateInvalid), |
Jason Molenda | 69b6b63 | 2013-03-05 03:33:59 +0000 | [diff] [blame] | 1061 | m_destroy_in_process (false), |
| 1062 | m_can_jit(eCanJITDontKnow) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1063 | { |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 1064 | CheckInWithManager (); |
Caroline Tice | 1559a46 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 1065 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1066 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1067 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1068 | log->Printf ("%p Process::Process()", static_cast<void*>(this)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1069 | |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 1070 | SetEventName (eBroadcastBitStateChanged, "state-changed"); |
| 1071 | SetEventName (eBroadcastBitInterrupt, "interrupt"); |
| 1072 | SetEventName (eBroadcastBitSTDOUT, "stdout-available"); |
| 1073 | SetEventName (eBroadcastBitSTDERR, "stderr-available"); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 1074 | SetEventName (eBroadcastBitProfileData, "profile-data-available"); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 1075 | |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 1076 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlStop , "control-stop" ); |
| 1077 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlPause , "control-pause" ); |
| 1078 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlResume, "control-resume"); |
| 1079 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1080 | listener.StartListeningForEvents (this, |
| 1081 | eBroadcastBitStateChanged | |
| 1082 | eBroadcastBitInterrupt | |
| 1083 | eBroadcastBitSTDOUT | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 1084 | eBroadcastBitSTDERR | |
| 1085 | eBroadcastBitProfileData); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1086 | |
| 1087 | m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster, |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1088 | eBroadcastBitStateChanged | |
| 1089 | eBroadcastBitInterrupt); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1090 | |
| 1091 | m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster, |
| 1092 | eBroadcastInternalStateControlStop | |
| 1093 | eBroadcastInternalStateControlPause | |
| 1094 | eBroadcastInternalStateControlResume); |
| 1095 | } |
| 1096 | |
| 1097 | //---------------------------------------------------------------------- |
| 1098 | // Destructor |
| 1099 | //---------------------------------------------------------------------- |
| 1100 | Process::~Process() |
| 1101 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1102 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1103 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1104 | log->Printf ("%p Process::~Process()", static_cast<void*>(this)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1105 | StopPrivateStateThread(); |
| 1106 | } |
| 1107 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1108 | const ProcessPropertiesSP & |
| 1109 | Process::GetGlobalProperties() |
| 1110 | { |
| 1111 | static ProcessPropertiesSP g_settings_sp; |
| 1112 | if (!g_settings_sp) |
| 1113 | g_settings_sp.reset (new ProcessProperties (true)); |
| 1114 | return g_settings_sp; |
| 1115 | } |
| 1116 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1117 | void |
| 1118 | Process::Finalize() |
| 1119 | { |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1120 | switch (GetPrivateState()) |
| 1121 | { |
| 1122 | case eStateConnected: |
| 1123 | case eStateAttaching: |
| 1124 | case eStateLaunching: |
| 1125 | case eStateStopped: |
| 1126 | case eStateRunning: |
| 1127 | case eStateStepping: |
| 1128 | case eStateCrashed: |
| 1129 | case eStateSuspended: |
| 1130 | if (GetShouldDetach()) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1131 | { |
| 1132 | // FIXME: This will have to be a process setting: |
| 1133 | bool keep_stopped = false; |
| 1134 | Detach(keep_stopped); |
| 1135 | } |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1136 | else |
| 1137 | Destroy(); |
| 1138 | break; |
| 1139 | |
| 1140 | case eStateInvalid: |
| 1141 | case eStateUnloaded: |
| 1142 | case eStateDetached: |
| 1143 | case eStateExited: |
| 1144 | break; |
| 1145 | } |
| 1146 | |
Greg Clayton | 1ed54f5 | 2011-10-01 00:45:15 +0000 | [diff] [blame] | 1147 | // Clear our broadcaster before we proceed with destroying |
| 1148 | Broadcaster::Clear(); |
| 1149 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1150 | // Do any cleanup needed prior to being destructed... Subclasses |
| 1151 | // that override this method should call this superclass method as well. |
Jim Ingham | d0a3e12 | 2011-02-16 17:54:55 +0000 | [diff] [blame] | 1152 | |
| 1153 | // We need to destroy the loader before the derived Process class gets destroyed |
| 1154 | // 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] | 1155 | m_dynamic_checkers_ap.reset(); |
| 1156 | m_abi_sp.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1157 | m_os_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 1158 | m_system_runtime_ap.reset(); |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 1159 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 1160 | m_jit_loaders_ap.reset(); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1161 | m_thread_list_real.Destroy(); |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 1162 | m_thread_list.Destroy(); |
Jason Molenda | 864f1cc | 2013-11-11 05:20:44 +0000 | [diff] [blame] | 1163 | m_extended_thread_list.Destroy(); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1164 | m_queue_list.Clear(); |
| 1165 | m_queue_list_stop_id = 0; |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 1166 | std::vector<Notifications> empty_notifications; |
| 1167 | m_notifications.swap(empty_notifications); |
| 1168 | m_image_tokens.clear(); |
| 1169 | m_memory_cache.Clear(); |
| 1170 | m_allocated_memory_cache.Clear(); |
| 1171 | m_language_runtimes.clear(); |
| 1172 | m_next_event_action_ap.reset(); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 1173 | //#ifdef LLDB_CONFIGURATION_DEBUG |
| 1174 | // StreamFile s(stdout, false); |
| 1175 | // EventSP event_sp; |
| 1176 | // while (m_private_state_listener.GetNextEvent(event_sp)) |
| 1177 | // { |
| 1178 | // event_sp->Dump (&s); |
| 1179 | // s.EOL(); |
| 1180 | // } |
| 1181 | //#endif |
| 1182 | // We have to be very careful here as the m_private_state_listener might |
| 1183 | // contain events that have ProcessSP values in them which can keep this |
| 1184 | // process around forever. These events need to be cleared out. |
| 1185 | m_private_state_listener.Clear(); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1186 | m_public_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 1187 | m_public_run_lock.SetStopped(); |
| 1188 | m_private_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 1189 | m_private_run_lock.SetStopped(); |
Jim Ingham | 4fc6cb9 | 2012-08-22 21:34:33 +0000 | [diff] [blame] | 1190 | m_finalize_called = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | void |
| 1194 | Process::RegisterNotificationCallbacks (const Notifications& callbacks) |
| 1195 | { |
| 1196 | m_notifications.push_back(callbacks); |
| 1197 | if (callbacks.initialize != NULL) |
| 1198 | callbacks.initialize (callbacks.baton, this); |
| 1199 | } |
| 1200 | |
| 1201 | bool |
| 1202 | Process::UnregisterNotificationCallbacks(const Notifications& callbacks) |
| 1203 | { |
| 1204 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 1205 | for (pos = m_notifications.begin(); pos != end; ++pos) |
| 1206 | { |
| 1207 | if (pos->baton == callbacks.baton && |
| 1208 | pos->initialize == callbacks.initialize && |
| 1209 | pos->process_state_changed == callbacks.process_state_changed) |
| 1210 | { |
| 1211 | m_notifications.erase(pos); |
| 1212 | return true; |
| 1213 | } |
| 1214 | } |
| 1215 | return false; |
| 1216 | } |
| 1217 | |
| 1218 | void |
| 1219 | Process::SynchronouslyNotifyStateChanged (StateType state) |
| 1220 | { |
| 1221 | std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end(); |
| 1222 | for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos) |
| 1223 | { |
| 1224 | if (notification_pos->process_state_changed) |
| 1225 | notification_pos->process_state_changed (notification_pos->baton, this, state); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | // FIXME: We need to do some work on events before the general Listener sees them. |
| 1230 | // For instance if we are continuing from a breakpoint, we need to ensure that we do |
| 1231 | // the little "insert real insn, step & stop" trick. But we can't do that when the |
| 1232 | // event is delivered by the broadcaster - since that is done on the thread that is |
| 1233 | // waiting for new events, so if we needed more than one event for our handling, we would |
| 1234 | // stall. So instead we do it when we fetch the event off of the queue. |
| 1235 | // |
| 1236 | |
| 1237 | StateType |
| 1238 | Process::GetNextEvent (EventSP &event_sp) |
| 1239 | { |
| 1240 | StateType state = eStateInvalid; |
| 1241 | |
| 1242 | if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp) |
| 1243 | state = Process::ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 1244 | |
| 1245 | return state; |
| 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | StateType |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1250 | Process::WaitForProcessToStop (const TimeValue *timeout, lldb::EventSP *event_sp_ptr, bool wait_always, Listener *hijack_listener) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1251 | { |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1252 | // We can't just wait for a "stopped" event, because the stopped event may have restarted the target. |
| 1253 | // We have to actually check each event, and in the case of a stopped event check the restarted flag |
| 1254 | // on the event. |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 1255 | if (event_sp_ptr) |
| 1256 | event_sp_ptr->reset(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1257 | StateType state = GetState(); |
| 1258 | // If we are exited or detached, we won't ever get back to any |
| 1259 | // other valid state... |
| 1260 | if (state == eStateDetached || state == eStateExited) |
| 1261 | return state; |
| 1262 | |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 1263 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1264 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1265 | log->Printf ("Process::%s (timeout = %p)", __FUNCTION__, |
| 1266 | static_cast<const void*>(timeout)); |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 1267 | |
| 1268 | if (!wait_always && |
| 1269 | StateIsStoppedState(state, true) && |
| 1270 | StateIsStoppedState(GetPrivateState(), true)) { |
| 1271 | if (log) |
| 1272 | log->Printf("Process::%s returning without waiting for events; process private and public states are already 'stopped'.", |
| 1273 | __FUNCTION__); |
| 1274 | return state; |
| 1275 | } |
| 1276 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1277 | while (state != eStateInvalid) |
| 1278 | { |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 1279 | EventSP event_sp; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1280 | state = WaitForStateChangedEvents (timeout, event_sp, hijack_listener); |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 1281 | if (event_sp_ptr && event_sp) |
| 1282 | *event_sp_ptr = event_sp; |
| 1283 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1284 | switch (state) |
| 1285 | { |
| 1286 | case eStateCrashed: |
| 1287 | case eStateDetached: |
| 1288 | case eStateExited: |
| 1289 | case eStateUnloaded: |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1290 | // We need to toggle the run lock as this won't get done in |
| 1291 | // SetPublicState() if the process is hijacked. |
| 1292 | if (hijack_listener) |
| 1293 | m_public_run_lock.SetStopped(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1294 | return state; |
| 1295 | case eStateStopped: |
| 1296 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 1297 | continue; |
| 1298 | else |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1299 | { |
| 1300 | // We need to toggle the run lock as this won't get done in |
| 1301 | // SetPublicState() if the process is hijacked. |
| 1302 | if (hijack_listener) |
| 1303 | m_public_run_lock.SetStopped(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1304 | return state; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1305 | } |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1306 | default: |
| 1307 | continue; |
| 1308 | } |
| 1309 | } |
| 1310 | return state; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | StateType |
| 1315 | Process::WaitForState |
| 1316 | ( |
| 1317 | const TimeValue *timeout, |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1318 | const StateType *match_states, |
| 1319 | const uint32_t num_match_states |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1320 | ) |
| 1321 | { |
| 1322 | EventSP event_sp; |
| 1323 | uint32_t i; |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1324 | StateType state = GetState(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1325 | while (state != eStateInvalid) |
| 1326 | { |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1327 | // If we are exited or detached, we won't ever get back to any |
| 1328 | // other valid state... |
| 1329 | if (state == eStateDetached || state == eStateExited) |
| 1330 | return state; |
| 1331 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1332 | state = WaitForStateChangedEvents (timeout, event_sp, NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1333 | |
| 1334 | for (i=0; i<num_match_states; ++i) |
| 1335 | { |
| 1336 | if (match_states[i] == state) |
| 1337 | return state; |
| 1338 | } |
| 1339 | } |
| 1340 | return state; |
| 1341 | } |
| 1342 | |
Jim Ingham | 30f9b21 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 1343 | bool |
| 1344 | Process::HijackProcessEvents (Listener *listener) |
| 1345 | { |
| 1346 | if (listener != NULL) |
| 1347 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1348 | return HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
Jim Ingham | 30f9b21 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 1349 | } |
| 1350 | else |
| 1351 | return false; |
| 1352 | } |
| 1353 | |
| 1354 | void |
| 1355 | Process::RestoreProcessEvents () |
| 1356 | { |
| 1357 | RestoreBroadcaster(); |
| 1358 | } |
| 1359 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 1360 | bool |
| 1361 | Process::HijackPrivateProcessEvents (Listener *listener) |
| 1362 | { |
| 1363 | if (listener != NULL) |
| 1364 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1365 | return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 1366 | } |
| 1367 | else |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
| 1371 | void |
| 1372 | Process::RestorePrivateProcessEvents () |
| 1373 | { |
| 1374 | m_private_state_broadcaster.RestoreBroadcaster(); |
| 1375 | } |
| 1376 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1377 | StateType |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1378 | Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp, Listener *hijack_listener) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1379 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1380 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1381 | |
| 1382 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1383 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, |
| 1384 | static_cast<const void*>(timeout)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1385 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1386 | Listener *listener = hijack_listener; |
| 1387 | if (listener == NULL) |
| 1388 | listener = &m_listener; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1389 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1390 | StateType state = eStateInvalid; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1391 | if (listener->WaitForEventForBroadcasterWithType (timeout, |
| 1392 | this, |
| 1393 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, |
| 1394 | event_sp)) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1395 | { |
| 1396 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1397 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1398 | else if (log) |
| 1399 | log->Printf ("Process::%s got no event or was interrupted.", __FUNCTION__); |
| 1400 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (log) |
| 1403 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1404 | __FUNCTION__, static_cast<const void*>(timeout), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1405 | StateAsCString(state)); |
| 1406 | return state; |
| 1407 | } |
| 1408 | |
| 1409 | Event * |
| 1410 | Process::PeekAtStateChangedEvents () |
| 1411 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1412 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (log) |
| 1415 | log->Printf ("Process::%s...", __FUNCTION__); |
| 1416 | |
| 1417 | Event *event_ptr; |
Greg Clayton | 3fcbed6 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1418 | event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this, |
| 1419 | eBroadcastBitStateChanged); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1420 | if (log) |
| 1421 | { |
| 1422 | if (event_ptr) |
| 1423 | { |
| 1424 | log->Printf ("Process::%s (event_ptr) => %s", |
| 1425 | __FUNCTION__, |
| 1426 | StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr))); |
| 1427 | } |
| 1428 | else |
| 1429 | { |
| 1430 | log->Printf ("Process::%s no events found", |
| 1431 | __FUNCTION__); |
| 1432 | } |
| 1433 | } |
| 1434 | return event_ptr; |
| 1435 | } |
| 1436 | |
| 1437 | StateType |
| 1438 | Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 1439 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1440 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1441 | |
| 1442 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1443 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, |
| 1444 | static_cast<const void*>(timeout)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1445 | |
| 1446 | StateType state = eStateInvalid; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1447 | if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout, |
| 1448 | &m_private_state_broadcaster, |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1449 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1450 | event_sp)) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1451 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1452 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1453 | |
| 1454 | // This is a bit of a hack, but when we wait here we could very well return |
| 1455 | // to the command-line, and that could disable the log, which would render the |
| 1456 | // log we got above invalid. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1457 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1458 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
| 1459 | __FUNCTION__, static_cast<const void *>(timeout), |
| 1460 | state == eStateInvalid ? "TIMEOUT" : StateAsCString(state)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1461 | return state; |
| 1462 | } |
| 1463 | |
| 1464 | bool |
| 1465 | Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only) |
| 1466 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1467 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1468 | |
| 1469 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1470 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, |
| 1471 | static_cast<const void*>(timeout)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1472 | |
| 1473 | if (control_only) |
| 1474 | return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp); |
| 1475 | else |
| 1476 | return m_private_state_listener.WaitForEvent(timeout, event_sp); |
| 1477 | } |
| 1478 | |
| 1479 | bool |
| 1480 | Process::IsRunning () const |
| 1481 | { |
| 1482 | return StateIsRunningState (m_public_state.GetValue()); |
| 1483 | } |
| 1484 | |
| 1485 | int |
| 1486 | Process::GetExitStatus () |
| 1487 | { |
| 1488 | if (m_public_state.GetValue() == eStateExited) |
| 1489 | return m_exit_status; |
| 1490 | return -1; |
| 1491 | } |
| 1492 | |
Greg Clayton | 85851dd | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 1493 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1494 | const char * |
| 1495 | Process::GetExitDescription () |
| 1496 | { |
| 1497 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 1498 | return m_exit_string.c_str(); |
| 1499 | return NULL; |
| 1500 | } |
| 1501 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1502 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1503 | Process::SetExitStatus (int status, const char *cstr) |
| 1504 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1505 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1506 | if (log) |
| 1507 | log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)", |
| 1508 | status, status, |
| 1509 | cstr ? "\"" : "", |
| 1510 | cstr ? cstr : "NULL", |
| 1511 | cstr ? "\"" : ""); |
| 1512 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1513 | // We were already in the exited state |
| 1514 | if (m_private_state.GetValue() == eStateExited) |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1515 | { |
Greg Clayton | 385d603 | 2011-01-26 23:47:29 +0000 | [diff] [blame] | 1516 | if (log) |
| 1517 | 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] | 1518 | return false; |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1519 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1520 | |
| 1521 | m_exit_status = status; |
| 1522 | if (cstr) |
| 1523 | m_exit_string = cstr; |
| 1524 | else |
| 1525 | m_exit_string.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1526 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1527 | DidExit (); |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 1528 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1529 | SetPrivateState (eStateExited); |
| 1530 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | // This static callback can be used to watch for local child processes on |
| 1534 | // the current host. The the child process exits, the process will be |
| 1535 | // found in the global target list (we want to be completely sure that the |
| 1536 | // lldb_private::Process doesn't go away before we can deliver the signal. |
| 1537 | bool |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1538 | Process::SetProcessExitStatus (void *callback_baton, |
| 1539 | lldb::pid_t pid, |
| 1540 | bool exited, |
| 1541 | int signo, // Zero for no signal |
| 1542 | int exit_status // Exit value of process if signal is zero |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1543 | ) |
| 1544 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1545 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1546 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1547 | 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] | 1548 | callback_baton, |
| 1549 | pid, |
| 1550 | exited, |
| 1551 | signo, |
| 1552 | exit_status); |
| 1553 | |
| 1554 | if (exited) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1555 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1556 | TargetSP target_sp(Debugger::FindTargetWithProcessID (pid)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1557 | if (target_sp) |
| 1558 | { |
| 1559 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 1560 | if (process_sp) |
| 1561 | { |
| 1562 | const char *signal_cstr = NULL; |
| 1563 | if (signo) |
| 1564 | signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo); |
| 1565 | |
| 1566 | process_sp->SetExitStatus (exit_status, signal_cstr); |
| 1567 | } |
| 1568 | } |
| 1569 | return true; |
| 1570 | } |
| 1571 | return false; |
| 1572 | } |
| 1573 | |
| 1574 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1575 | void |
| 1576 | Process::UpdateThreadListIfNeeded () |
| 1577 | { |
| 1578 | const uint32_t stop_id = GetStopID(); |
| 1579 | if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID()) |
| 1580 | { |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1581 | const StateType state = GetPrivateState(); |
| 1582 | if (StateIsStoppedState (state, true)) |
| 1583 | { |
| 1584 | Mutex::Locker locker (m_thread_list.GetMutex ()); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1585 | // m_thread_list does have its own mutex, but we need to |
| 1586 | // hold onto the mutex between the call to UpdateThreadList(...) |
| 1587 | // and the os->UpdateThreadList(...) so it doesn't change on us |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1588 | ThreadList &old_thread_list = m_thread_list; |
| 1589 | ThreadList real_thread_list(this); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1590 | ThreadList new_thread_list(this); |
| 1591 | // Always update the thread list with the protocol specific |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1592 | // thread list, but only update if "true" is returned |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1593 | if (UpdateThreadList (m_thread_list_real, real_thread_list)) |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1594 | { |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 1595 | // Don't call into the OperatingSystem to update the thread list if we are shutting down, since |
| 1596 | // that may call back into the SBAPI's, requiring the API lock which is already held by whoever is |
| 1597 | // shutting us down, causing a deadlock. |
| 1598 | if (!m_destroy_in_process) |
| 1599 | { |
| 1600 | OperatingSystem *os = GetOperatingSystem (); |
| 1601 | if (os) |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1602 | { |
| 1603 | // Clear any old backing threads where memory threads might have been |
| 1604 | // backed by actual threads from the lldb_private::Process subclass |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1605 | size_t num_old_threads = old_thread_list.GetSize(false); |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1606 | for (size_t i=0; i<num_old_threads; ++i) |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1607 | old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread(); |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1608 | |
| 1609 | // Now let the OperatingSystem plug-in update the thread list |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1610 | os->UpdateThreadList (old_thread_list, // Old list full of threads created by OS plug-in |
| 1611 | real_thread_list, // The actual thread list full of threads created by each lldb_private::Process subclass |
| 1612 | 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] | 1613 | } |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1614 | else |
| 1615 | { |
| 1616 | // No OS plug-in, the new thread list is the same as the real thread list |
| 1617 | new_thread_list = real_thread_list; |
| 1618 | } |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 1619 | } |
Jim Ingham | 02ff8e0 | 2013-06-22 00:55:02 +0000 | [diff] [blame] | 1620 | |
| 1621 | m_thread_list_real.Update(real_thread_list); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1622 | m_thread_list.Update (new_thread_list); |
| 1623 | m_thread_list.SetStopID (stop_id); |
Jason Molenda | a6e9130 | 2013-11-19 05:44:41 +0000 | [diff] [blame] | 1624 | |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 1625 | if (GetLastNaturalStopID () != m_extended_thread_stop_id) |
| 1626 | { |
| 1627 | // Clear any extended threads that we may have accumulated previously |
| 1628 | m_extended_thread_list.Clear(); |
| 1629 | m_extended_thread_stop_id = GetLastNaturalStopID (); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1630 | |
| 1631 | m_queue_list.Clear(); |
| 1632 | m_queue_list_stop_id = GetLastNaturalStopID (); |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 1633 | } |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1634 | } |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1635 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1636 | } |
| 1637 | } |
| 1638 | |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1639 | void |
| 1640 | Process::UpdateQueueListIfNeeded () |
| 1641 | { |
| 1642 | if (m_system_runtime_ap.get()) |
| 1643 | { |
| 1644 | if (m_queue_list.GetSize() == 0 || m_queue_list_stop_id != GetLastNaturalStopID()) |
| 1645 | { |
| 1646 | const StateType state = GetPrivateState(); |
| 1647 | if (StateIsStoppedState (state, true)) |
| 1648 | { |
| 1649 | m_system_runtime_ap->PopulateQueueList (m_queue_list); |
| 1650 | m_queue_list_stop_id = GetLastNaturalStopID(); |
| 1651 | } |
| 1652 | } |
| 1653 | } |
| 1654 | } |
| 1655 | |
Greg Clayton | a4d8747 | 2013-01-18 23:41:08 +0000 | [diff] [blame] | 1656 | ThreadSP |
| 1657 | Process::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context) |
| 1658 | { |
| 1659 | OperatingSystem *os = GetOperatingSystem (); |
| 1660 | if (os) |
| 1661 | return os->CreateThread(tid, context); |
| 1662 | return ThreadSP(); |
| 1663 | } |
| 1664 | |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1665 | uint32_t |
| 1666 | Process::GetNextThreadIndexID (uint64_t thread_id) |
| 1667 | { |
| 1668 | return AssignIndexIDToThread(thread_id); |
| 1669 | } |
| 1670 | |
| 1671 | bool |
| 1672 | Process::HasAssignedIndexIDToThread(uint64_t thread_id) |
| 1673 | { |
| 1674 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id); |
| 1675 | if (iterator == m_thread_id_to_index_id_map.end()) |
| 1676 | { |
| 1677 | return false; |
| 1678 | } |
| 1679 | else |
| 1680 | { |
| 1681 | return true; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | uint32_t |
| 1686 | Process::AssignIndexIDToThread(uint64_t thread_id) |
| 1687 | { |
| 1688 | uint32_t result = 0; |
| 1689 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id); |
| 1690 | if (iterator == m_thread_id_to_index_id_map.end()) |
| 1691 | { |
| 1692 | result = ++m_thread_index_id; |
| 1693 | m_thread_id_to_index_id_map[thread_id] = result; |
| 1694 | } |
| 1695 | else |
| 1696 | { |
| 1697 | result = iterator->second; |
| 1698 | } |
| 1699 | |
| 1700 | return result; |
| 1701 | } |
| 1702 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1703 | StateType |
| 1704 | Process::GetState() |
| 1705 | { |
| 1706 | // If any other threads access this we will need a mutex for it |
| 1707 | return m_public_state.GetValue (); |
| 1708 | } |
| 1709 | |
| 1710 | void |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1711 | Process::SetPublicState (StateType new_state, bool restarted) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | if (log) |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1715 | log->Printf("Process::SetPublicState (state = %s, restarted = %i)", StateAsCString(new_state), restarted); |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1716 | const StateType old_state = m_public_state.GetValue(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1717 | m_public_state.SetValue (new_state); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1718 | |
| 1719 | // On the transition from Run to Stopped, we unlock the writer end of the |
| 1720 | // run lock. The lock gets locked in Resume, which is the public API |
| 1721 | // to tell the program to run. |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1722 | if (!IsHijackedForEvent(eBroadcastBitStateChanged)) |
| 1723 | { |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1724 | if (new_state == eStateDetached) |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1725 | { |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1726 | if (log) |
| 1727 | 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] | 1728 | m_public_run_lock.SetStopped(); |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1729 | } |
| 1730 | else |
| 1731 | { |
| 1732 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1733 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1734 | if ((old_state_is_stopped != new_state_is_stopped)) |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1735 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1736 | if (new_state_is_stopped && !restarted) |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1737 | { |
| 1738 | if (log) |
| 1739 | log->Printf("Process::SetPublicState (%s) -- unlocking run lock", StateAsCString(new_state)); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1740 | m_public_run_lock.SetStopped(); |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1741 | } |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1742 | } |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1743 | } |
| 1744 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1747 | Error |
| 1748 | Process::Resume () |
| 1749 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1750 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1751 | if (log) |
| 1752 | log->Printf("Process::Resume -- locking run lock"); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1753 | if (!m_public_run_lock.TrySetRunning()) |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1754 | { |
| 1755 | Error error("Resume request failed - process still running."); |
| 1756 | if (log) |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1757 | log->Printf ("Process::Resume: -- TrySetRunning failed, not resuming."); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1758 | return error; |
| 1759 | } |
| 1760 | return PrivateResume(); |
| 1761 | } |
| 1762 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1763 | StateType |
| 1764 | Process::GetPrivateState () |
| 1765 | { |
| 1766 | return m_private_state.GetValue(); |
| 1767 | } |
| 1768 | |
| 1769 | void |
| 1770 | Process::SetPrivateState (StateType new_state) |
| 1771 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1772 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1773 | bool state_changed = false; |
| 1774 | |
| 1775 | if (log) |
| 1776 | log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state)); |
| 1777 | |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1778 | Mutex::Locker thread_locker(m_thread_list.GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1779 | Mutex::Locker locker(m_private_state.GetMutex()); |
| 1780 | |
| 1781 | const StateType old_state = m_private_state.GetValueNoLock (); |
| 1782 | state_changed = old_state != new_state; |
Ed Maste | c29693f | 2013-07-02 16:35:47 +0000 | [diff] [blame] | 1783 | |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1784 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1785 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1786 | if (old_state_is_stopped != new_state_is_stopped) |
| 1787 | { |
| 1788 | if (new_state_is_stopped) |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1789 | m_private_run_lock.SetStopped(); |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1790 | else |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1791 | m_private_run_lock.SetRunning(); |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1792 | } |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1793 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1794 | if (state_changed) |
| 1795 | { |
| 1796 | m_private_state.SetValueNoLock (new_state); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1797 | if (StateIsStoppedState(new_state, false)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1798 | { |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1799 | // Note, this currently assumes that all threads in the list |
| 1800 | // stop when the process stops. In the future we will want to |
| 1801 | // support a debugging model where some threads continue to run |
| 1802 | // while others are stopped. When that happens we will either need |
| 1803 | // a way for the thread list to identify which threads are stopping |
| 1804 | // or create a special thread list containing only threads which |
| 1805 | // actually stopped. |
| 1806 | // |
| 1807 | // The process plugin is responsible for managing the actual |
| 1808 | // behavior of the threads and should have stopped any threads |
| 1809 | // that are going to stop before we get here. |
| 1810 | m_thread_list.DidStop(); |
| 1811 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1812 | m_mod_id.BumpStopID(); |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1813 | m_memory_cache.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1814 | if (log) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1815 | 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] | 1816 | } |
| 1817 | // Use our target to get a shared pointer to ourselves... |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 1818 | if (m_finalize_called && PrivateStateThreadIsValid() == false) |
| 1819 | BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state)); |
| 1820 | else |
| 1821 | 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] | 1822 | } |
| 1823 | else |
| 1824 | { |
| 1825 | if (log) |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1826 | 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] | 1827 | } |
| 1828 | } |
| 1829 | |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 1830 | void |
| 1831 | Process::SetRunningUserExpression (bool on) |
| 1832 | { |
| 1833 | m_mod_id.SetRunningUserExpression (on); |
| 1834 | } |
| 1835 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1836 | addr_t |
| 1837 | Process::GetImageInfoAddress() |
| 1838 | { |
| 1839 | return LLDB_INVALID_ADDRESS; |
| 1840 | } |
| 1841 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1842 | //---------------------------------------------------------------------- |
| 1843 | // LoadImage |
| 1844 | // |
| 1845 | // This function provides a default implementation that works for most |
| 1846 | // unix variants. Any Process subclasses that need to do shared library |
| 1847 | // loading differently should override LoadImage and UnloadImage and |
| 1848 | // do what is needed. |
| 1849 | //---------------------------------------------------------------------- |
| 1850 | uint32_t |
| 1851 | Process::LoadImage (const FileSpec &image_spec, Error &error) |
| 1852 | { |
Greg Clayton | ac7a3db | 2012-04-18 00:05:19 +0000 | [diff] [blame] | 1853 | char path[PATH_MAX]; |
| 1854 | image_spec.GetPath(path, sizeof(path)); |
| 1855 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1856 | DynamicLoader *loader = GetDynamicLoader(); |
| 1857 | if (loader) |
| 1858 | { |
| 1859 | error = loader->CanLoadImage(); |
| 1860 | if (error.Fail()) |
| 1861 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1862 | } |
| 1863 | |
| 1864 | if (error.Success()) |
| 1865 | { |
| 1866 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1867 | |
| 1868 | if (thread_sp) |
| 1869 | { |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 1870 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1871 | |
| 1872 | if (frame_sp) |
| 1873 | { |
| 1874 | ExecutionContext exe_ctx; |
| 1875 | frame_sp->CalculateExecutionContext (exe_ctx); |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1876 | EvaluateExpressionOptions expr_options; |
| 1877 | expr_options.SetUnwindOnError(true); |
| 1878 | expr_options.SetIgnoreBreakpoints(true); |
| 1879 | expr_options.SetExecutionPolicy(eExecutionPolicyAlways); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1880 | StreamString expr; |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1881 | expr.Printf("dlopen (\"%s\", 2)", path); |
| 1882 | 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] | 1883 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1884 | Error expr_error; |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1885 | ClangUserExpression::Evaluate (exe_ctx, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1886 | expr_options, |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1887 | expr.GetData(), |
| 1888 | prefix, |
| 1889 | result_valobj_sp, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1890 | expr_error); |
| 1891 | if (expr_error.Success()) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1892 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1893 | error = result_valobj_sp->GetError(); |
| 1894 | if (error.Success()) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1895 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1896 | Scalar scalar; |
| 1897 | if (result_valobj_sp->ResolveValue (scalar)) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1898 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1899 | addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 1900 | if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS) |
| 1901 | { |
| 1902 | uint32_t image_token = m_image_tokens.size(); |
| 1903 | m_image_tokens.push_back (image_ptr); |
| 1904 | return image_token; |
| 1905 | } |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } |
| 1908 | } |
Jim Ingham | 6c9ed91 | 2014-04-03 01:26:14 +0000 | [diff] [blame] | 1909 | else |
| 1910 | error = expr_error; |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1911 | } |
| 1912 | } |
| 1913 | } |
Greg Clayton | ac7a3db | 2012-04-18 00:05:19 +0000 | [diff] [blame] | 1914 | if (!error.AsCString()) |
| 1915 | error.SetErrorStringWithFormat("unable to load '%s'", path); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1916 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1917 | } |
| 1918 | |
| 1919 | //---------------------------------------------------------------------- |
| 1920 | // UnloadImage |
| 1921 | // |
| 1922 | // This function provides a default implementation that works for most |
| 1923 | // unix variants. Any Process subclasses that need to do shared library |
| 1924 | // loading differently should override LoadImage and UnloadImage and |
| 1925 | // do what is needed. |
| 1926 | //---------------------------------------------------------------------- |
| 1927 | Error |
| 1928 | Process::UnloadImage (uint32_t image_token) |
| 1929 | { |
| 1930 | Error error; |
| 1931 | if (image_token < m_image_tokens.size()) |
| 1932 | { |
| 1933 | const addr_t image_addr = m_image_tokens[image_token]; |
| 1934 | if (image_addr == LLDB_INVALID_ADDRESS) |
| 1935 | { |
| 1936 | error.SetErrorString("image already unloaded"); |
| 1937 | } |
| 1938 | else |
| 1939 | { |
| 1940 | DynamicLoader *loader = GetDynamicLoader(); |
| 1941 | if (loader) |
| 1942 | error = loader->CanLoadImage(); |
| 1943 | |
| 1944 | if (error.Success()) |
| 1945 | { |
| 1946 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1947 | |
| 1948 | if (thread_sp) |
| 1949 | { |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 1950 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1951 | |
| 1952 | if (frame_sp) |
| 1953 | { |
| 1954 | ExecutionContext exe_ctx; |
| 1955 | frame_sp->CalculateExecutionContext (exe_ctx); |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1956 | EvaluateExpressionOptions expr_options; |
| 1957 | expr_options.SetUnwindOnError(true); |
| 1958 | expr_options.SetIgnoreBreakpoints(true); |
| 1959 | expr_options.SetExecutionPolicy(eExecutionPolicyAlways); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1960 | StreamString expr; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1961 | expr.Printf("dlclose ((void *)0x%" PRIx64 ")", image_addr); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1962 | const char *prefix = "extern \"C\" int dlclose(void* handle);\n"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1963 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1964 | Error expr_error; |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1965 | ClangUserExpression::Evaluate (exe_ctx, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1966 | expr_options, |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1967 | expr.GetData(), |
| 1968 | prefix, |
| 1969 | result_valobj_sp, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1970 | expr_error); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1971 | if (result_valobj_sp->GetError().Success()) |
| 1972 | { |
| 1973 | Scalar scalar; |
Jim Ingham | 6035b67 | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 1974 | if (result_valobj_sp->ResolveValue (scalar)) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1975 | { |
| 1976 | if (scalar.UInt(1)) |
| 1977 | { |
| 1978 | error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData()); |
| 1979 | } |
| 1980 | else |
| 1981 | { |
| 1982 | m_image_tokens[image_token] = LLDB_INVALID_ADDRESS; |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | else |
| 1987 | { |
| 1988 | error = result_valobj_sp->GetError(); |
| 1989 | } |
| 1990 | } |
| 1991 | } |
| 1992 | } |
| 1993 | } |
| 1994 | } |
| 1995 | else |
| 1996 | { |
| 1997 | error.SetErrorString("invalid image token"); |
| 1998 | } |
| 1999 | return error; |
| 2000 | } |
| 2001 | |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 2002 | const lldb::ABISP & |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2003 | Process::GetABI() |
| 2004 | { |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 2005 | if (!m_abi_sp) |
| 2006 | m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture()); |
| 2007 | return m_abi_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2010 | LanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2011 | Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2012 | { |
| 2013 | LanguageRuntimeCollection::iterator pos; |
| 2014 | pos = m_language_runtimes.find (language); |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2015 | if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second)) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2016 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2017 | lldb::LanguageRuntimeSP runtime_sp(LanguageRuntime::FindPlugin(this, language)); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2018 | |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2019 | m_language_runtimes[language] = runtime_sp; |
| 2020 | return runtime_sp.get(); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2021 | } |
| 2022 | else |
| 2023 | return (*pos).second.get(); |
| 2024 | } |
| 2025 | |
| 2026 | CPPLanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2027 | Process::GetCPPLanguageRuntime (bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2028 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2029 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2030 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus) |
| 2031 | return static_cast<CPPLanguageRuntime *> (runtime); |
| 2032 | return NULL; |
| 2033 | } |
| 2034 | |
| 2035 | ObjCLanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2036 | Process::GetObjCLanguageRuntime (bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2037 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2038 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC, retry_if_null); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2039 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC) |
| 2040 | return static_cast<ObjCLanguageRuntime *> (runtime); |
| 2041 | return NULL; |
| 2042 | } |
| 2043 | |
Enrico Granata | fd4c84e | 2012-05-21 16:51:35 +0000 | [diff] [blame] | 2044 | bool |
| 2045 | Process::IsPossibleDynamicValue (ValueObject& in_value) |
| 2046 | { |
| 2047 | if (in_value.IsDynamic()) |
| 2048 | return false; |
| 2049 | LanguageType known_type = in_value.GetObjectRuntimeLanguage(); |
| 2050 | |
| 2051 | if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC) |
| 2052 | { |
| 2053 | LanguageRuntime *runtime = GetLanguageRuntime (known_type); |
| 2054 | return runtime ? runtime->CouldHaveDynamicValue(in_value) : false; |
| 2055 | } |
| 2056 | |
| 2057 | LanguageRuntime *cpp_runtime = GetLanguageRuntime (eLanguageTypeC_plus_plus); |
| 2058 | if (cpp_runtime && cpp_runtime->CouldHaveDynamicValue(in_value)) |
| 2059 | return true; |
| 2060 | |
| 2061 | LanguageRuntime *objc_runtime = GetLanguageRuntime (eLanguageTypeObjC); |
| 2062 | return objc_runtime ? objc_runtime->CouldHaveDynamicValue(in_value) : false; |
| 2063 | } |
| 2064 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2065 | BreakpointSiteList & |
| 2066 | Process::GetBreakpointSiteList() |
| 2067 | { |
| 2068 | return m_breakpoint_site_list; |
| 2069 | } |
| 2070 | |
| 2071 | const BreakpointSiteList & |
| 2072 | Process::GetBreakpointSiteList() const |
| 2073 | { |
| 2074 | return m_breakpoint_site_list; |
| 2075 | } |
| 2076 | |
| 2077 | |
| 2078 | void |
| 2079 | Process::DisableAllBreakpointSites () |
| 2080 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2081 | m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void { |
| 2082 | // bp_site->SetEnabled(true); |
| 2083 | DisableBreakpointSite(bp_site); |
| 2084 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | Error |
| 2088 | Process::ClearBreakpointSiteByID (lldb::user_id_t break_id) |
| 2089 | { |
| 2090 | Error error (DisableBreakpointSiteByID (break_id)); |
| 2091 | |
| 2092 | if (error.Success()) |
| 2093 | m_breakpoint_site_list.Remove(break_id); |
| 2094 | |
| 2095 | return error; |
| 2096 | } |
| 2097 | |
| 2098 | Error |
| 2099 | Process::DisableBreakpointSiteByID (lldb::user_id_t break_id) |
| 2100 | { |
| 2101 | Error error; |
| 2102 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 2103 | if (bp_site_sp) |
| 2104 | { |
| 2105 | if (bp_site_sp->IsEnabled()) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2106 | error = DisableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2107 | } |
| 2108 | else |
| 2109 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2110 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | return error; |
| 2114 | } |
| 2115 | |
| 2116 | Error |
| 2117 | Process::EnableBreakpointSiteByID (lldb::user_id_t break_id) |
| 2118 | { |
| 2119 | Error error; |
| 2120 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 2121 | if (bp_site_sp) |
| 2122 | { |
| 2123 | if (!bp_site_sp->IsEnabled()) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2124 | error = EnableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2125 | } |
| 2126 | else |
| 2127 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2128 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2129 | } |
| 2130 | return error; |
| 2131 | } |
| 2132 | |
Stephen Wilson | 50bd94f | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 2133 | lldb::break_id_t |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 2134 | Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2135 | { |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 2136 | addr_t load_addr = LLDB_INVALID_ADDRESS; |
| 2137 | |
| 2138 | bool show_error = true; |
| 2139 | switch (GetState()) |
| 2140 | { |
| 2141 | case eStateInvalid: |
| 2142 | case eStateUnloaded: |
| 2143 | case eStateConnected: |
| 2144 | case eStateAttaching: |
| 2145 | case eStateLaunching: |
| 2146 | case eStateDetached: |
| 2147 | case eStateExited: |
| 2148 | show_error = false; |
| 2149 | break; |
| 2150 | |
| 2151 | case eStateStopped: |
| 2152 | case eStateRunning: |
| 2153 | case eStateStepping: |
| 2154 | case eStateCrashed: |
| 2155 | case eStateSuspended: |
| 2156 | show_error = IsAlive(); |
| 2157 | break; |
| 2158 | } |
| 2159 | |
| 2160 | // Reset the IsIndirect flag here, in case the location changes from |
| 2161 | // pointing to a indirect symbol to a regular symbol. |
| 2162 | owner->SetIsIndirect (false); |
| 2163 | |
| 2164 | if (owner->ShouldResolveIndirectFunctions()) |
| 2165 | { |
| 2166 | Symbol *symbol = owner->GetAddress().CalculateSymbolContextSymbol(); |
| 2167 | if (symbol && symbol->IsIndirect()) |
| 2168 | { |
| 2169 | Error error; |
| 2170 | load_addr = ResolveIndirectFunction (&symbol->GetAddress(), error); |
| 2171 | if (!error.Success() && show_error) |
| 2172 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 2173 | m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to resolve indirect function at 0x%" PRIx64 " for breakpoint %i.%i: %s\n", |
| 2174 | symbol->GetAddress().GetLoadAddress(&m_target), |
| 2175 | owner->GetBreakpoint().GetID(), |
| 2176 | owner->GetID(), |
| 2177 | error.AsCString() ? error.AsCString() : "unkown error"); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 2178 | return LLDB_INVALID_BREAK_ID; |
| 2179 | } |
| 2180 | Address resolved_address(load_addr); |
| 2181 | load_addr = resolved_address.GetOpcodeLoadAddress (&m_target); |
| 2182 | owner->SetIsIndirect(true); |
| 2183 | } |
| 2184 | else |
| 2185 | load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target); |
| 2186 | } |
| 2187 | else |
| 2188 | load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target); |
| 2189 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2190 | if (load_addr != LLDB_INVALID_ADDRESS) |
| 2191 | { |
| 2192 | BreakpointSiteSP bp_site_sp; |
| 2193 | |
| 2194 | // Look up this breakpoint site. If it exists, then add this new owner, otherwise |
| 2195 | // create a new breakpoint site and add it. |
| 2196 | |
| 2197 | bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr); |
| 2198 | |
| 2199 | if (bp_site_sp) |
| 2200 | { |
| 2201 | bp_site_sp->AddOwner (owner); |
| 2202 | owner->SetBreakpointSite (bp_site_sp); |
| 2203 | return bp_site_sp->GetID(); |
| 2204 | } |
| 2205 | else |
| 2206 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2207 | 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] | 2208 | if (bp_site_sp) |
| 2209 | { |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2210 | Error error = EnableBreakpointSite (bp_site_sp.get()); |
| 2211 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2212 | { |
| 2213 | owner->SetBreakpointSite (bp_site_sp); |
| 2214 | return m_breakpoint_site_list.Add (bp_site_sp); |
| 2215 | } |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2216 | else |
| 2217 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2218 | if (show_error) |
| 2219 | { |
| 2220 | // Report error for setting breakpoint... |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 2221 | m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to set breakpoint site at 0x%" PRIx64 " for breakpoint %i.%i: %s\n", |
| 2222 | load_addr, |
| 2223 | owner->GetBreakpoint().GetID(), |
| 2224 | owner->GetID(), |
| 2225 | error.AsCString() ? error.AsCString() : "unkown error"); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2226 | } |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2227 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | } |
| 2231 | // We failed to enable the breakpoint |
| 2232 | return LLDB_INVALID_BREAK_ID; |
| 2233 | |
| 2234 | } |
| 2235 | |
| 2236 | void |
| 2237 | Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp) |
| 2238 | { |
| 2239 | uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id); |
| 2240 | if (num_owners == 0) |
| 2241 | { |
Jim Ingham | f1ff3bb | 2013-04-06 00:16:39 +0000 | [diff] [blame] | 2242 | // Don't try to disable the site if we don't have a live process anymore. |
| 2243 | if (IsAlive()) |
| 2244 | DisableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2245 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | |
| 2250 | size_t |
| 2251 | Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const |
| 2252 | { |
| 2253 | size_t bytes_removed = 0; |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2254 | BreakpointSiteList bp_sites_in_range; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2255 | |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2256 | 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] | 2257 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2258 | bp_sites_in_range.ForEach([bp_addr, size, buf, &bytes_removed](BreakpointSite *bp_site) -> void { |
| 2259 | if (bp_site->GetType() == BreakpointSite::eSoftware) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2260 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2261 | addr_t intersect_addr; |
| 2262 | size_t intersect_size; |
| 2263 | size_t opcode_offset; |
| 2264 | 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] | 2265 | { |
| 2266 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 2267 | 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] | 2268 | assert(opcode_offset + intersect_size <= bp_site->GetByteSize()); |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2269 | size_t buf_offset = intersect_addr - bp_addr; |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2270 | ::memcpy(buf + buf_offset, bp_site->GetSavedOpcodeBytes() + opcode_offset, intersect_size); |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2271 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2272 | } |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2273 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2274 | } |
| 2275 | return bytes_removed; |
| 2276 | } |
| 2277 | |
| 2278 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 2279 | |
| 2280 | size_t |
| 2281 | Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site) |
| 2282 | { |
| 2283 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 2284 | if (platform_sp) |
| 2285 | return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site); |
| 2286 | return 0; |
| 2287 | } |
| 2288 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2289 | Error |
| 2290 | Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 2291 | { |
| 2292 | Error error; |
| 2293 | assert (bp_site != NULL); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2294 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2295 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
| 2296 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2297 | 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] | 2298 | if (bp_site->IsEnabled()) |
| 2299 | { |
| 2300 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2301 | 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] | 2302 | return error; |
| 2303 | } |
| 2304 | |
| 2305 | if (bp_addr == LLDB_INVALID_ADDRESS) |
| 2306 | { |
| 2307 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 2308 | return error; |
| 2309 | } |
| 2310 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 2311 | // trap for the breakpoint site |
| 2312 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 2313 | |
| 2314 | if (bp_opcode_size == 0) |
| 2315 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2316 | 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] | 2317 | } |
| 2318 | else |
| 2319 | { |
| 2320 | const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 2321 | |
| 2322 | if (bp_opcode_bytes == NULL) |
| 2323 | { |
| 2324 | error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 2325 | return error; |
| 2326 | } |
| 2327 | |
| 2328 | // Save the original opcode by reading it |
| 2329 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size) |
| 2330 | { |
| 2331 | // Write a software breakpoint in place of the original opcode |
| 2332 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 2333 | { |
| 2334 | uint8_t verify_bp_opcode_bytes[64]; |
| 2335 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 2336 | { |
| 2337 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0) |
| 2338 | { |
| 2339 | bp_site->SetEnabled(true); |
| 2340 | bp_site->SetType (BreakpointSite::eSoftware); |
| 2341 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2342 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2343 | bp_site->GetID(), |
| 2344 | (uint64_t)bp_addr); |
| 2345 | } |
| 2346 | else |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 2347 | error.SetErrorString("failed to verify the breakpoint trap in memory."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2348 | } |
| 2349 | else |
| 2350 | error.SetErrorString("Unable to read memory to verify breakpoint trap."); |
| 2351 | } |
| 2352 | else |
| 2353 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 2354 | } |
| 2355 | else |
| 2356 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 2357 | } |
Stephen Wilson | 78a4feb | 2011-01-12 04:20:03 +0000 | [diff] [blame] | 2358 | if (log && error.Fail()) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2359 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2360 | bp_site->GetID(), |
| 2361 | (uint64_t)bp_addr, |
| 2362 | error.AsCString()); |
| 2363 | return error; |
| 2364 | } |
| 2365 | |
| 2366 | Error |
| 2367 | Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 2368 | { |
| 2369 | Error error; |
| 2370 | assert (bp_site != NULL); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2371 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2372 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 2373 | lldb::user_id_t breakID = bp_site->GetID(); |
| 2374 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2375 | 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] | 2376 | |
| 2377 | if (bp_site->IsHardware()) |
| 2378 | { |
| 2379 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 2380 | } |
| 2381 | else if (bp_site->IsEnabled()) |
| 2382 | { |
| 2383 | const size_t break_op_size = bp_site->GetByteSize(); |
| 2384 | const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes(); |
| 2385 | if (break_op_size > 0) |
| 2386 | { |
| 2387 | // Clear a software breakoint instruction |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2388 | uint8_t curr_break_op[8]; |
Stephen Wilson | 4ab4768 | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 2389 | assert (break_op_size <= sizeof(curr_break_op)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2390 | bool break_op_found = false; |
| 2391 | |
| 2392 | // Read the breakpoint opcode |
| 2393 | if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size) |
| 2394 | { |
| 2395 | bool verify = false; |
| 2396 | // Make sure we have the a breakpoint opcode exists at this address |
| 2397 | if (::memcmp (curr_break_op, break_op, break_op_size) == 0) |
| 2398 | { |
| 2399 | break_op_found = true; |
| 2400 | // We found a valid breakpoint opcode at this address, now restore |
| 2401 | // the saved opcode. |
| 2402 | if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size) |
| 2403 | { |
| 2404 | verify = true; |
| 2405 | } |
| 2406 | else |
| 2407 | error.SetErrorString("Memory write failed when restoring original opcode."); |
| 2408 | } |
| 2409 | else |
| 2410 | { |
| 2411 | error.SetErrorString("Original breakpoint trap is no longer in memory."); |
| 2412 | // Set verify to true and so we can check if the original opcode has already been restored |
| 2413 | verify = true; |
| 2414 | } |
| 2415 | |
| 2416 | if (verify) |
| 2417 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2418 | uint8_t verify_opcode[8]; |
Stephen Wilson | 4ab4768 | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 2419 | assert (break_op_size < sizeof(verify_opcode)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2420 | // Verify that our original opcode made it back to the inferior |
| 2421 | if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size) |
| 2422 | { |
| 2423 | // compare the memory we just read with the original opcode |
| 2424 | if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0) |
| 2425 | { |
| 2426 | // SUCCESS |
| 2427 | bp_site->SetEnabled(false); |
| 2428 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2429 | 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] | 2430 | return error; |
| 2431 | } |
| 2432 | else |
| 2433 | { |
| 2434 | if (break_op_found) |
| 2435 | error.SetErrorString("Failed to restore original opcode."); |
| 2436 | } |
| 2437 | } |
| 2438 | else |
| 2439 | error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored."); |
| 2440 | } |
| 2441 | } |
| 2442 | else |
| 2443 | error.SetErrorString("Unable to read memory that should contain the breakpoint trap."); |
| 2444 | } |
| 2445 | } |
| 2446 | else |
| 2447 | { |
| 2448 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2449 | 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] | 2450 | return error; |
| 2451 | } |
| 2452 | |
| 2453 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2454 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2455 | bp_site->GetID(), |
| 2456 | (uint64_t)bp_addr, |
| 2457 | error.AsCString()); |
| 2458 | return error; |
| 2459 | |
| 2460 | } |
| 2461 | |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2462 | // Uncomment to verify memory caching works after making changes to caching code |
| 2463 | //#define VERIFY_MEMORY_READS |
| 2464 | |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2465 | size_t |
| 2466 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 2467 | { |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 2468 | error.Clear(); |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2469 | if (!GetDisableMemoryCache()) |
| 2470 | { |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2471 | #if defined (VERIFY_MEMORY_READS) |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2472 | // Memory caching is enabled, with debug verification |
| 2473 | |
| 2474 | if (buf && size) |
| 2475 | { |
| 2476 | // Uncomment the line below to make sure memory caching is working. |
| 2477 | // I ran this through the test suite and got no assertions, so I am |
| 2478 | // pretty confident this is working well. If any changes are made to |
| 2479 | // memory caching, uncomment the line below and test your changes! |
| 2480 | |
| 2481 | // Verify all memory reads by using the cache first, then redundantly |
| 2482 | // reading the same memory from the inferior and comparing to make sure |
| 2483 | // everything is exactly the same. |
| 2484 | std::string verify_buf (size, '\0'); |
| 2485 | assert (verify_buf.size() == size); |
| 2486 | const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error); |
| 2487 | Error verify_error; |
| 2488 | const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error); |
| 2489 | assert (cache_bytes_read == verify_bytes_read); |
| 2490 | assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0); |
| 2491 | assert (verify_error.Success() == error.Success()); |
| 2492 | return cache_bytes_read; |
| 2493 | } |
| 2494 | return 0; |
| 2495 | #else // !defined(VERIFY_MEMORY_READS) |
| 2496 | // Memory caching is enabled, without debug verification |
| 2497 | |
| 2498 | return m_memory_cache.Read (addr, buf, size, error); |
| 2499 | #endif // defined (VERIFY_MEMORY_READS) |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2500 | } |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2501 | else |
| 2502 | { |
| 2503 | // Memory caching is disabled |
| 2504 | |
| 2505 | return ReadMemoryFromInferior (addr, buf, size, error); |
| 2506 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2507 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2508 | |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2509 | size_t |
| 2510 | Process::ReadCStringFromMemory (addr_t addr, std::string &out_str, Error &error) |
| 2511 | { |
Greg Clayton | de87c0f | 2012-05-19 00:18:00 +0000 | [diff] [blame] | 2512 | char buf[256]; |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2513 | out_str.clear(); |
| 2514 | addr_t curr_addr = addr; |
| 2515 | while (1) |
| 2516 | { |
| 2517 | size_t length = ReadCStringFromMemory (curr_addr, buf, sizeof(buf), error); |
| 2518 | if (length == 0) |
| 2519 | break; |
| 2520 | out_str.append(buf, length); |
| 2521 | // If we got "length - 1" bytes, we didn't get the whole C string, we |
| 2522 | // need to read some more characters |
| 2523 | if (length == sizeof(buf) - 1) |
| 2524 | curr_addr += length; |
| 2525 | else |
| 2526 | break; |
| 2527 | } |
| 2528 | return out_str.size(); |
| 2529 | } |
| 2530 | |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2531 | |
| 2532 | size_t |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2533 | Process::ReadStringFromMemory (addr_t addr, char *dst, size_t max_bytes, Error &error, |
| 2534 | size_t type_width) |
| 2535 | { |
| 2536 | size_t total_bytes_read = 0; |
| 2537 | if (dst && max_bytes && type_width && max_bytes >= type_width) |
| 2538 | { |
| 2539 | // Ensure a null terminator independent of the number of bytes that is read. |
| 2540 | memset (dst, 0, max_bytes); |
| 2541 | size_t bytes_left = max_bytes - type_width; |
| 2542 | |
| 2543 | const char terminator[4] = {'\0', '\0', '\0', '\0'}; |
| 2544 | assert(sizeof(terminator) >= type_width && |
| 2545 | "Attempting to validate a string with more than 4 bytes per character!"); |
| 2546 | |
| 2547 | addr_t curr_addr = addr; |
| 2548 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2549 | char *curr_dst = dst; |
| 2550 | |
| 2551 | error.Clear(); |
| 2552 | while (bytes_left > 0 && error.Success()) |
| 2553 | { |
| 2554 | addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); |
| 2555 | addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2556 | size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error); |
| 2557 | |
| 2558 | if (bytes_read == 0) |
| 2559 | break; |
| 2560 | |
| 2561 | // Search for a null terminator of correct size and alignment in bytes_read |
| 2562 | size_t aligned_start = total_bytes_read - total_bytes_read % type_width; |
| 2563 | for (size_t i = aligned_start; i + type_width <= total_bytes_read + bytes_read; i += type_width) |
| 2564 | if (::strncmp(&dst[i], terminator, type_width) == 0) |
| 2565 | { |
| 2566 | error.Clear(); |
| 2567 | return i; |
| 2568 | } |
| 2569 | |
| 2570 | total_bytes_read += bytes_read; |
| 2571 | curr_dst += bytes_read; |
| 2572 | curr_addr += bytes_read; |
| 2573 | bytes_left -= bytes_read; |
| 2574 | } |
| 2575 | } |
| 2576 | else |
| 2577 | { |
| 2578 | if (max_bytes) |
| 2579 | error.SetErrorString("invalid arguments"); |
| 2580 | } |
| 2581 | return total_bytes_read; |
| 2582 | } |
| 2583 | |
| 2584 | // Deprecated in favor of ReadStringFromMemory which has wchar support and correct code to find |
| 2585 | // null terminators. |
| 2586 | size_t |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2587 | 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] | 2588 | { |
| 2589 | size_t total_cstr_len = 0; |
| 2590 | if (dst && dst_max_len) |
| 2591 | { |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2592 | result_error.Clear(); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2593 | // NULL out everything just to be safe |
| 2594 | memset (dst, 0, dst_max_len); |
| 2595 | Error error; |
| 2596 | addr_t curr_addr = addr; |
| 2597 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2598 | size_t bytes_left = dst_max_len - 1; |
| 2599 | char *curr_dst = dst; |
| 2600 | |
| 2601 | while (bytes_left > 0) |
| 2602 | { |
| 2603 | addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); |
| 2604 | addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2605 | size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error); |
| 2606 | |
| 2607 | if (bytes_read == 0) |
| 2608 | { |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2609 | result_error = error; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2610 | dst[total_cstr_len] = '\0'; |
| 2611 | break; |
| 2612 | } |
| 2613 | const size_t len = strlen(curr_dst); |
| 2614 | |
| 2615 | total_cstr_len += len; |
| 2616 | |
| 2617 | if (len < bytes_to_read) |
| 2618 | break; |
| 2619 | |
| 2620 | curr_dst += bytes_read; |
| 2621 | curr_addr += bytes_read; |
| 2622 | bytes_left -= bytes_read; |
| 2623 | } |
| 2624 | } |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2625 | else |
| 2626 | { |
| 2627 | if (dst == NULL) |
| 2628 | result_error.SetErrorString("invalid arguments"); |
| 2629 | else |
| 2630 | result_error.Clear(); |
| 2631 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2632 | return total_cstr_len; |
| 2633 | } |
| 2634 | |
| 2635 | size_t |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2636 | Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error) |
| 2637 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2638 | if (buf == NULL || size == 0) |
| 2639 | return 0; |
| 2640 | |
| 2641 | size_t bytes_read = 0; |
| 2642 | uint8_t *bytes = (uint8_t *)buf; |
| 2643 | |
| 2644 | while (bytes_read < size) |
| 2645 | { |
| 2646 | const size_t curr_size = size - bytes_read; |
| 2647 | const size_t curr_bytes_read = DoReadMemory (addr + bytes_read, |
| 2648 | bytes + bytes_read, |
| 2649 | curr_size, |
| 2650 | error); |
| 2651 | bytes_read += curr_bytes_read; |
| 2652 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 2653 | break; |
| 2654 | } |
| 2655 | |
| 2656 | // Replace any software breakpoint opcodes that fall into this range back |
| 2657 | // into "buf" before we return |
| 2658 | if (bytes_read > 0) |
| 2659 | RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf); |
| 2660 | return bytes_read; |
| 2661 | } |
| 2662 | |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2663 | uint64_t |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2664 | 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] | 2665 | { |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2666 | Scalar scalar; |
| 2667 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error)) |
| 2668 | return scalar.ULongLong(fail_value); |
| 2669 | return fail_value; |
| 2670 | } |
| 2671 | |
| 2672 | addr_t |
| 2673 | Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error) |
| 2674 | { |
| 2675 | Scalar scalar; |
| 2676 | if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error)) |
| 2677 | return scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 2678 | return LLDB_INVALID_ADDRESS; |
| 2679 | } |
| 2680 | |
| 2681 | |
| 2682 | bool |
| 2683 | Process::WritePointerToMemory (lldb::addr_t vm_addr, |
| 2684 | lldb::addr_t ptr_value, |
| 2685 | Error &error) |
| 2686 | { |
| 2687 | Scalar scalar; |
| 2688 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2689 | if (addr_byte_size <= 4) |
| 2690 | scalar = (uint32_t)ptr_value; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2691 | else |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2692 | scalar = ptr_value; |
| 2693 | return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2696 | size_t |
| 2697 | Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error) |
| 2698 | { |
| 2699 | size_t bytes_written = 0; |
| 2700 | const uint8_t *bytes = (const uint8_t *)buf; |
| 2701 | |
| 2702 | while (bytes_written < size) |
| 2703 | { |
| 2704 | const size_t curr_size = size - bytes_written; |
| 2705 | const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written, |
| 2706 | bytes + bytes_written, |
| 2707 | curr_size, |
| 2708 | error); |
| 2709 | bytes_written += curr_bytes_written; |
| 2710 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 2711 | break; |
| 2712 | } |
| 2713 | return bytes_written; |
| 2714 | } |
| 2715 | |
| 2716 | size_t |
| 2717 | Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 2718 | { |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2719 | #if defined (ENABLE_MEMORY_CACHING) |
| 2720 | m_memory_cache.Flush (addr, size); |
| 2721 | #endif |
| 2722 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2723 | if (buf == NULL || size == 0) |
| 2724 | return 0; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2725 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2726 | m_mod_id.BumpMemoryID(); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2727 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2728 | // We need to write any data that would go where any current software traps |
| 2729 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 2730 | // may have placed in our tasks memory. |
| 2731 | |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2732 | BreakpointSiteList bp_sites_in_range; |
| 2733 | |
| 2734 | 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] | 2735 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2736 | // No breakpoint sites overlap |
| 2737 | if (bp_sites_in_range.IsEmpty()) |
| 2738 | return WriteMemoryPrivate (addr, buf, size, error); |
| 2739 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2740 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2741 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 2742 | uint64_t bytes_written = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2743 | |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2744 | bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, &error](BreakpointSite *bp) -> void { |
| 2745 | |
| 2746 | if (error.Success()) |
| 2747 | { |
| 2748 | addr_t intersect_addr; |
| 2749 | size_t intersect_size; |
| 2750 | size_t opcode_offset; |
| 2751 | const bool intersects = bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset); |
| 2752 | assert(intersects); |
| 2753 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 2754 | assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size); |
| 2755 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 2756 | |
| 2757 | // Check for bytes before this breakpoint |
| 2758 | const addr_t curr_addr = addr + bytes_written; |
| 2759 | if (intersect_addr > curr_addr) |
| 2760 | { |
| 2761 | // There are some bytes before this breakpoint that we need to |
| 2762 | // just write to memory |
| 2763 | size_t curr_size = intersect_addr - curr_addr; |
| 2764 | size_t curr_bytes_written = WriteMemoryPrivate (curr_addr, |
| 2765 | ubuf + bytes_written, |
| 2766 | curr_size, |
| 2767 | error); |
| 2768 | bytes_written += curr_bytes_written; |
| 2769 | if (curr_bytes_written != curr_size) |
| 2770 | { |
| 2771 | // We weren't able to write all of the requested bytes, we |
| 2772 | // are done looping and will return the number of bytes that |
| 2773 | // we have written so far. |
| 2774 | if (error.Success()) |
| 2775 | error.SetErrorToGenericError(); |
| 2776 | } |
| 2777 | } |
| 2778 | // Now write any bytes that would cover up any software breakpoints |
| 2779 | // directly into the breakpoint opcode buffer |
| 2780 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size); |
| 2781 | bytes_written += intersect_size; |
| 2782 | } |
| 2783 | }); |
| 2784 | |
| 2785 | if (bytes_written < size) |
| 2786 | bytes_written += WriteMemoryPrivate (addr + bytes_written, |
| 2787 | ubuf + bytes_written, |
| 2788 | size - bytes_written, |
| 2789 | error); |
| 2790 | } |
| 2791 | } |
| 2792 | else |
| 2793 | { |
| 2794 | return WriteMemoryPrivate (addr, buf, size, error); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | // 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] | 2798 | return 0; //bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2799 | } |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2800 | |
| 2801 | size_t |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2802 | 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] | 2803 | { |
| 2804 | if (byte_size == UINT32_MAX) |
| 2805 | byte_size = scalar.GetByteSize(); |
| 2806 | if (byte_size > 0) |
| 2807 | { |
| 2808 | uint8_t buf[32]; |
| 2809 | const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error); |
| 2810 | if (mem_size > 0) |
| 2811 | return WriteMemory(addr, buf, mem_size, error); |
| 2812 | else |
| 2813 | error.SetErrorString ("failed to get scalar as memory data"); |
| 2814 | } |
| 2815 | else |
| 2816 | { |
| 2817 | error.SetErrorString ("invalid scalar value"); |
| 2818 | } |
| 2819 | return 0; |
| 2820 | } |
| 2821 | |
| 2822 | size_t |
| 2823 | Process::ReadScalarIntegerFromMemory (addr_t addr, |
| 2824 | uint32_t byte_size, |
| 2825 | bool is_signed, |
| 2826 | Scalar &scalar, |
| 2827 | Error &error) |
| 2828 | { |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2829 | uint64_t uval = 0; |
| 2830 | if (byte_size == 0) |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2831 | { |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2832 | error.SetErrorString ("byte size is zero"); |
| 2833 | } |
| 2834 | else if (byte_size & (byte_size - 1)) |
| 2835 | { |
| 2836 | error.SetErrorStringWithFormat ("byte size %u is not a power of 2", byte_size); |
| 2837 | } |
| 2838 | else if (byte_size <= sizeof(uval)) |
| 2839 | { |
| 2840 | const size_t bytes_read = ReadMemory (addr, &uval, byte_size, error); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2841 | if (bytes_read == byte_size) |
| 2842 | { |
| 2843 | DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize()); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2844 | lldb::offset_t offset = 0; |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2845 | if (byte_size <= 4) |
| 2846 | scalar = data.GetMaxU32 (&offset, byte_size); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2847 | else |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2848 | scalar = data.GetMaxU64 (&offset, byte_size); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2849 | if (is_signed) |
| 2850 | scalar.SignExtend(byte_size * 8); |
| 2851 | return bytes_read; |
| 2852 | } |
| 2853 | } |
| 2854 | else |
| 2855 | { |
| 2856 | error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size); |
| 2857 | } |
| 2858 | return 0; |
| 2859 | } |
| 2860 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2861 | #define USE_ALLOCATE_MEMORY_CACHE 1 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2862 | addr_t |
| 2863 | Process::AllocateMemory(size_t size, uint32_t permissions, Error &error) |
| 2864 | { |
Jim Ingham | f72ce3a | 2011-06-20 17:32:44 +0000 | [diff] [blame] | 2865 | if (GetPrivateState() != eStateStopped) |
| 2866 | return LLDB_INVALID_ADDRESS; |
| 2867 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2868 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2869 | return m_allocated_memory_cache.AllocateMemory(size, permissions, error); |
| 2870 | #else |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2871 | addr_t allocated_addr = DoAllocateMemory (size, permissions, error); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2872 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2873 | if (log) |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 2874 | log->Printf("Process::AllocateMemory(size=%" PRIu64 ", permissions=%s) => 0x%16.16" PRIx64 " (m_stop_id = %u m_memory_id = %u)", |
Deepak Panickal | d66b50c | 2013-10-22 12:27:43 +0000 | [diff] [blame] | 2875 | (uint64_t)size, |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2876 | GetPermissionsAsCString (permissions), |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2877 | (uint64_t)allocated_addr, |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2878 | m_mod_id.GetStopID(), |
| 2879 | m_mod_id.GetMemoryID()); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2880 | return allocated_addr; |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2881 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2884 | bool |
| 2885 | Process::CanJIT () |
| 2886 | { |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2887 | if (m_can_jit == eCanJITDontKnow) |
| 2888 | { |
| 2889 | Error err; |
| 2890 | |
| 2891 | uint64_t allocated_memory = AllocateMemory(8, |
| 2892 | ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable, |
| 2893 | err); |
| 2894 | |
| 2895 | if (err.Success()) |
| 2896 | m_can_jit = eCanJITYes; |
| 2897 | else |
| 2898 | m_can_jit = eCanJITNo; |
| 2899 | |
| 2900 | DeallocateMemory (allocated_memory); |
| 2901 | } |
| 2902 | |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2903 | return m_can_jit == eCanJITYes; |
| 2904 | } |
| 2905 | |
| 2906 | void |
| 2907 | Process::SetCanJIT (bool can_jit) |
| 2908 | { |
| 2909 | m_can_jit = (can_jit ? eCanJITYes : eCanJITNo); |
| 2910 | } |
| 2911 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2912 | Error |
| 2913 | Process::DeallocateMemory (addr_t ptr) |
| 2914 | { |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2915 | Error error; |
| 2916 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2917 | if (!m_allocated_memory_cache.DeallocateMemory(ptr)) |
| 2918 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2919 | error.SetErrorStringWithFormat ("deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2920 | } |
| 2921 | #else |
| 2922 | error = DoDeallocateMemory (ptr); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2923 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2924 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2925 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2926 | 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] | 2927 | ptr, |
| 2928 | error.AsCString("SUCCESS"), |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2929 | m_mod_id.GetStopID(), |
| 2930 | m_mod_id.GetMemoryID()); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2931 | #endif |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2932 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
Han Ming Ong | c811d38 | 2012-11-17 00:33:14 +0000 | [diff] [blame] | 2935 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2936 | ModuleSP |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2937 | Process::ReadModuleFromMemory (const FileSpec& file_spec, |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2938 | lldb::addr_t header_addr, |
| 2939 | size_t size_to_read) |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2940 | { |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2941 | ModuleSP module_sp (new Module (file_spec, ArchSpec())); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2942 | if (module_sp) |
| 2943 | { |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2944 | Error error; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2945 | ObjectFile *objfile = module_sp->GetMemoryObjectFile (shared_from_this(), header_addr, error, size_to_read); |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2946 | if (objfile) |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2947 | return module_sp; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2948 | } |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2949 | return ModuleSP(); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2950 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2951 | |
| 2952 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2953 | Process::EnableWatchpoint (Watchpoint *watchpoint, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2954 | { |
| 2955 | Error error; |
| 2956 | error.SetErrorString("watchpoints are not supported"); |
| 2957 | return error; |
| 2958 | } |
| 2959 | |
| 2960 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2961 | Process::DisableWatchpoint (Watchpoint *watchpoint, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2962 | { |
| 2963 | Error error; |
| 2964 | error.SetErrorString("watchpoints are not supported"); |
| 2965 | return error; |
| 2966 | } |
| 2967 | |
| 2968 | StateType |
| 2969 | Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 2970 | { |
| 2971 | StateType state; |
| 2972 | // Now wait for the process to launch and return control to us, and then |
| 2973 | // call DidLaunch: |
| 2974 | while (1) |
| 2975 | { |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2976 | event_sp.reset(); |
| 2977 | state = WaitForStateChangedEventsPrivate (timeout, event_sp); |
| 2978 | |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 2979 | if (StateIsStoppedState(state, false)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2980 | break; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2981 | |
| 2982 | // If state is invalid, then we timed out |
| 2983 | if (state == eStateInvalid) |
| 2984 | break; |
| 2985 | |
| 2986 | if (event_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2987 | HandlePrivateEvent (event_sp); |
| 2988 | } |
| 2989 | return state; |
| 2990 | } |
| 2991 | |
| 2992 | Error |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2993 | Process::Launch (ProcessLaunchInfo &launch_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2994 | { |
| 2995 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2996 | m_abi_sp.reset(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2997 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2998 | m_jit_loaders_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2999 | m_system_runtime_ap.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3000 | m_os_ap.reset(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3001 | m_process_input_reader.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3002 | |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 3003 | Module *exe_module = m_target.GetExecutableModulePointer(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3004 | if (exe_module) |
| 3005 | { |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 3006 | char local_exec_file_path[PATH_MAX]; |
| 3007 | char platform_exec_file_path[PATH_MAX]; |
| 3008 | exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path)); |
| 3009 | 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] | 3010 | if (exe_module->GetFileSpec().Exists()) |
| 3011 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 3012 | // Install anything that might need to be installed prior to launching. |
| 3013 | // For host systems, this will do nothing, but if we are connected to a |
| 3014 | // remote platform it will install any needed binaries |
| 3015 | error = GetTarget().Install(&launch_info); |
| 3016 | if (error.Fail()) |
| 3017 | return error; |
| 3018 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3019 | if (PrivateStateThreadIsValid ()) |
| 3020 | PausePrivateStateThread (); |
| 3021 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3022 | error = WillLaunch (exe_module); |
| 3023 | if (error.Success()) |
| 3024 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3025 | const bool restarted = false; |
| 3026 | SetPublicState (eStateLaunching, restarted); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 3027 | m_should_detach = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3028 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3029 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 69fd4be | 2012-09-04 20:29:05 +0000 | [diff] [blame] | 3030 | { |
| 3031 | // Now launch using these arguments. |
| 3032 | error = DoLaunch (exe_module, launch_info); |
| 3033 | } |
| 3034 | else |
| 3035 | { |
| 3036 | // This shouldn't happen |
| 3037 | error.SetErrorString("failed to acquire process run lock"); |
| 3038 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3039 | |
| 3040 | if (error.Fail()) |
| 3041 | { |
| 3042 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3043 | { |
| 3044 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3045 | const char *error_string = error.AsCString(); |
| 3046 | if (error_string == NULL) |
| 3047 | error_string = "launch failed"; |
| 3048 | SetExitStatus (-1, error_string); |
| 3049 | } |
| 3050 | } |
| 3051 | else |
| 3052 | { |
| 3053 | EventSP event_sp; |
Greg Clayton | 1a38ea7 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 3054 | TimeValue timeout_time; |
| 3055 | timeout_time = TimeValue::Now(); |
| 3056 | timeout_time.OffsetWithSeconds(10); |
| 3057 | StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3058 | |
Greg Clayton | 1a38ea7 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 3059 | if (state == eStateInvalid || event_sp.get() == NULL) |
| 3060 | { |
| 3061 | // We were able to launch the process, but we failed to |
| 3062 | // catch the initial stop. |
| 3063 | SetExitStatus (0, "failed to catch stop after launch"); |
| 3064 | Destroy(); |
| 3065 | } |
| 3066 | else if (state == eStateStopped || state == eStateCrashed) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3067 | { |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3068 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3069 | DidLaunch (); |
| 3070 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3071 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3072 | if (dyld) |
| 3073 | dyld->DidLaunch(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3074 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 3075 | GetJITLoaders().DidLaunch(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3076 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3077 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3078 | if (system_runtime) |
| 3079 | system_runtime->DidLaunch(); |
| 3080 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3081 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3082 | // This delays passing the stopped event to listeners till DidLaunch gets |
| 3083 | // a chance to complete... |
| 3084 | HandlePrivateEvent (event_sp); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3085 | |
| 3086 | if (PrivateStateThreadIsValid ()) |
| 3087 | ResumePrivateStateThread (); |
| 3088 | else |
| 3089 | StartPrivateStateThread (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3090 | } |
| 3091 | else if (state == eStateExited) |
| 3092 | { |
| 3093 | // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 3094 | // not likely to work, and return an invalid pid. |
| 3095 | HandlePrivateEvent (event_sp); |
| 3096 | } |
| 3097 | } |
| 3098 | } |
| 3099 | } |
| 3100 | else |
| 3101 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 3102 | error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3103 | } |
| 3104 | } |
| 3105 | return error; |
| 3106 | } |
| 3107 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3108 | |
| 3109 | Error |
| 3110 | Process::LoadCore () |
| 3111 | { |
| 3112 | Error error = DoLoadCore(); |
| 3113 | if (error.Success()) |
| 3114 | { |
| 3115 | if (PrivateStateThreadIsValid ()) |
| 3116 | ResumePrivateStateThread (); |
| 3117 | else |
| 3118 | StartPrivateStateThread (); |
| 3119 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3120 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3121 | if (dyld) |
| 3122 | dyld->DidAttach(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3123 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 3124 | GetJITLoaders().DidAttach(); |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3125 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3126 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3127 | if (system_runtime) |
| 3128 | system_runtime->DidAttach(); |
| 3129 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3130 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3131 | // We successfully loaded a core file, now pretend we stopped so we can |
| 3132 | // show all of the threads in the core file and explore the crashed |
| 3133 | // state. |
| 3134 | SetPrivateState (eStateStopped); |
| 3135 | |
| 3136 | } |
| 3137 | return error; |
| 3138 | } |
| 3139 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3140 | DynamicLoader * |
| 3141 | Process::GetDynamicLoader () |
| 3142 | { |
| 3143 | if (m_dyld_ap.get() == NULL) |
| 3144 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL)); |
| 3145 | return m_dyld_ap.get(); |
| 3146 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3147 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3148 | JITLoaderList & |
| 3149 | Process::GetJITLoaders () |
| 3150 | { |
| 3151 | if (!m_jit_loaders_ap) |
| 3152 | { |
| 3153 | m_jit_loaders_ap.reset(new JITLoaderList()); |
| 3154 | JITLoader::LoadPlugins(this, *m_jit_loaders_ap); |
| 3155 | } |
| 3156 | return *m_jit_loaders_ap; |
| 3157 | } |
| 3158 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3159 | SystemRuntime * |
| 3160 | Process::GetSystemRuntime () |
| 3161 | { |
| 3162 | if (m_system_runtime_ap.get() == NULL) |
| 3163 | m_system_runtime_ap.reset (SystemRuntime::FindPlugin(this)); |
| 3164 | return m_system_runtime_ap.get(); |
| 3165 | } |
| 3166 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3167 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3168 | Process::NextEventAction::EventActionResult |
| 3169 | Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3170 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3171 | StateType state = ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 3172 | switch (state) |
Greg Clayton | 19388cf | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 3173 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3174 | case eStateRunning: |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3175 | case eStateConnected: |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3176 | return eEventActionRetry; |
| 3177 | |
| 3178 | case eStateStopped: |
| 3179 | case eStateCrashed: |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3180 | { |
| 3181 | // During attach, prior to sending the eStateStopped event, |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3182 | // lldb_private::Process subclasses must set the new process ID. |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3183 | assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3184 | // We don't want these events to be reported, so go set the ShouldReportStop here: |
| 3185 | m_process->GetThreadList().SetShouldReportStop (eVoteNo); |
| 3186 | |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3187 | if (m_exec_count > 0) |
| 3188 | { |
| 3189 | --m_exec_count; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3190 | RequestResume(); |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3191 | return eEventActionRetry; |
| 3192 | } |
| 3193 | else |
| 3194 | { |
| 3195 | m_process->CompleteAttach (); |
| 3196 | return eEventActionSuccess; |
| 3197 | } |
| 3198 | } |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3199 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3200 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3201 | default: |
| 3202 | case eStateExited: |
| 3203 | case eStateInvalid: |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3204 | break; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3205 | } |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3206 | |
| 3207 | m_exit_string.assign ("No valid Process"); |
| 3208 | return eEventActionExit; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3209 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3210 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3211 | Process::NextEventAction::EventActionResult |
| 3212 | Process::AttachCompletionHandler::HandleBeingInterrupted() |
| 3213 | { |
| 3214 | return eEventActionSuccess; |
| 3215 | } |
| 3216 | |
| 3217 | const char * |
| 3218 | Process::AttachCompletionHandler::GetExitString () |
| 3219 | { |
| 3220 | return m_exit_string.c_str(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | Error |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3224 | Process::Attach (ProcessAttachInfo &attach_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3225 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3226 | m_abi_sp.reset(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3227 | m_process_input_reader.reset(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3228 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3229 | m_jit_loaders_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3230 | m_system_runtime_ap.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3231 | m_os_ap.reset(); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 3232 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3233 | lldb::pid_t attach_pid = attach_info.GetProcessID(); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3234 | Error error; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3235 | if (attach_pid == LLDB_INVALID_PROCESS_ID) |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 3236 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3237 | char process_name[PATH_MAX]; |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3238 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3239 | if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name))) |
Jim Ingham | 2ecb742 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 3240 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3241 | const bool wait_for_launch = attach_info.GetWaitForLaunch(); |
| 3242 | |
| 3243 | if (wait_for_launch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3244 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3245 | error = WillAttachToProcessWithName(process_name, wait_for_launch); |
| 3246 | if (error.Success()) |
| 3247 | { |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3248 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3249 | { |
| 3250 | m_should_detach = true; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3251 | const bool restarted = false; |
| 3252 | SetPublicState (eStateAttaching, restarted); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3253 | // Now attach using these arguments. |
Jean-Daniel Dupas | 9c517c0 | 2013-12-23 22:32:54 +0000 | [diff] [blame] | 3254 | error = DoAttachToProcessWithName (process_name, attach_info); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3255 | } |
| 3256 | else |
| 3257 | { |
| 3258 | // This shouldn't happen |
| 3259 | error.SetErrorString("failed to acquire process run lock"); |
| 3260 | } |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 3261 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3262 | if (error.Fail()) |
| 3263 | { |
| 3264 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3265 | { |
| 3266 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3267 | if (error.AsCString() == NULL) |
| 3268 | error.SetErrorString("attach failed"); |
| 3269 | |
| 3270 | SetExitStatus(-1, error.AsCString()); |
| 3271 | } |
| 3272 | } |
| 3273 | else |
| 3274 | { |
| 3275 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 3276 | StartPrivateStateThread(); |
| 3277 | } |
| 3278 | return error; |
| 3279 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3280 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3281 | else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3282 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3283 | ProcessInstanceInfoList process_infos; |
| 3284 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 3285 | |
| 3286 | if (platform_sp) |
| 3287 | { |
| 3288 | ProcessInstanceInfoMatch match_info; |
| 3289 | match_info.GetProcessInfo() = attach_info; |
| 3290 | match_info.SetNameMatchType (eNameMatchEquals); |
| 3291 | platform_sp->FindProcesses (match_info, process_infos); |
| 3292 | const uint32_t num_matches = process_infos.GetSize(); |
| 3293 | if (num_matches == 1) |
| 3294 | { |
| 3295 | attach_pid = process_infos.GetProcessIDAtIndex(0); |
| 3296 | // Fall through and attach using the above process ID |
| 3297 | } |
| 3298 | else |
| 3299 | { |
| 3300 | match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name)); |
| 3301 | if (num_matches > 1) |
| 3302 | error.SetErrorStringWithFormat ("more than one process named %s", process_name); |
| 3303 | else |
| 3304 | error.SetErrorStringWithFormat ("could not find a process named %s", process_name); |
| 3305 | } |
| 3306 | } |
| 3307 | else |
| 3308 | { |
| 3309 | error.SetErrorString ("invalid platform, can't find processes by name"); |
| 3310 | return error; |
| 3311 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3312 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3313 | } |
| 3314 | else |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3315 | { |
| 3316 | error.SetErrorString ("invalid process name"); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3317 | } |
| 3318 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3319 | |
| 3320 | if (attach_pid != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3321 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3322 | error = WillAttachToProcessWithID(attach_pid); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3323 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3324 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3325 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3326 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3327 | { |
| 3328 | // Now attach using these arguments. |
| 3329 | m_should_detach = true; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3330 | const bool restarted = false; |
| 3331 | SetPublicState (eStateAttaching, restarted); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3332 | error = DoAttachToProcessWithID (attach_pid, attach_info); |
| 3333 | } |
| 3334 | else |
| 3335 | { |
| 3336 | // This shouldn't happen |
| 3337 | error.SetErrorString("failed to acquire process run lock"); |
| 3338 | } |
| 3339 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3340 | if (error.Success()) |
| 3341 | { |
| 3342 | |
| 3343 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 3344 | StartPrivateStateThread(); |
| 3345 | } |
| 3346 | else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3347 | { |
| 3348 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3349 | { |
| 3350 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3351 | const char *error_string = error.AsCString(); |
| 3352 | if (error_string == NULL) |
| 3353 | error_string = "attach failed"; |
| 3354 | |
| 3355 | SetExitStatus(-1, error_string); |
| 3356 | } |
| 3357 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3358 | } |
| 3359 | } |
| 3360 | return error; |
| 3361 | } |
| 3362 | |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3363 | void |
| 3364 | Process::CompleteAttach () |
| 3365 | { |
| 3366 | // Let the process subclass figure out at much as it can about the process |
| 3367 | // before we go looking for a dynamic loader plug-in. |
| 3368 | DidAttach(); |
| 3369 | |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3370 | // We just attached. If we have a platform, ask it for the process architecture, and if it isn't |
| 3371 | // the same as the one we've already set, switch architectures. |
| 3372 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 3373 | assert (platform_sp.get()); |
| 3374 | if (platform_sp) |
| 3375 | { |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3376 | const ArchSpec &target_arch = m_target.GetArchitecture(); |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 3377 | if (target_arch.IsValid() && !platform_sp->IsCompatibleArchitecture (target_arch, false, NULL)) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3378 | { |
| 3379 | ArchSpec platform_arch; |
| 3380 | platform_sp = platform_sp->GetPlatformForArchitecture (target_arch, &platform_arch); |
| 3381 | if (platform_sp) |
| 3382 | { |
| 3383 | m_target.SetPlatform (platform_sp); |
| 3384 | m_target.SetArchitecture(platform_arch); |
| 3385 | } |
| 3386 | } |
| 3387 | else |
| 3388 | { |
| 3389 | ProcessInstanceInfo process_info; |
| 3390 | platform_sp->GetProcessInfo (GetID(), process_info); |
| 3391 | const ArchSpec &process_arch = process_info.GetArchitecture(); |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 3392 | if (process_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(process_arch)) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3393 | m_target.SetArchitecture (process_arch); |
| 3394 | } |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | // 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] | 3398 | // plug-in |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3399 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3400 | if (dyld) |
| 3401 | dyld->DidAttach(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3402 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 3403 | GetJITLoaders().DidAttach(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3404 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3405 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3406 | if (system_runtime) |
| 3407 | system_runtime->DidAttach(); |
| 3408 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3409 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3410 | // 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] | 3411 | const ModuleList &target_modules = m_target.GetImages(); |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3412 | Mutex::Locker modules_locker(target_modules.GetMutex()); |
| 3413 | size_t num_modules = target_modules.GetSize(); |
| 3414 | ModuleSP new_executable_module_sp; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3415 | |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 3416 | for (size_t i = 0; i < num_modules; i++) |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3417 | { |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3418 | ModuleSP module_sp (target_modules.GetModuleAtIndexUnlocked (i)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3419 | if (module_sp && module_sp->IsExecutable()) |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3420 | { |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 3421 | if (m_target.GetExecutableModulePointer() != module_sp.get()) |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3422 | new_executable_module_sp = module_sp; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3423 | break; |
| 3424 | } |
| 3425 | } |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3426 | if (new_executable_module_sp) |
| 3427 | m_target.SetExecutableModule (new_executable_module_sp, false); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3430 | Error |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 3431 | Process::ConnectRemote (Stream *strm, const char *remote_url) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3432 | { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3433 | m_abi_sp.reset(); |
| 3434 | m_process_input_reader.reset(); |
| 3435 | |
| 3436 | // Find the process and its architecture. Make sure it matches the architecture |
| 3437 | // of the current Target, and if not adjust it. |
| 3438 | |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 3439 | Error error (DoConnectRemote (strm, remote_url)); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3440 | if (error.Success()) |
| 3441 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3442 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3443 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 3444 | EventSP event_sp; |
| 3445 | StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 3446 | |
| 3447 | if (state == eStateStopped || state == eStateCrashed) |
| 3448 | { |
| 3449 | // If we attached and actually have a process on the other end, then |
| 3450 | // this ended up being the equivalent of an attach. |
| 3451 | CompleteAttach (); |
| 3452 | |
| 3453 | // This delays passing the stopped event to listeners till |
| 3454 | // CompleteAttach gets a chance to complete... |
| 3455 | HandlePrivateEvent (event_sp); |
| 3456 | |
| 3457 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3458 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 3459 | |
| 3460 | if (PrivateStateThreadIsValid ()) |
| 3461 | ResumePrivateStateThread (); |
| 3462 | else |
| 3463 | StartPrivateStateThread (); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3464 | } |
| 3465 | return error; |
| 3466 | } |
| 3467 | |
| 3468 | |
| 3469 | Error |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 3470 | Process::PrivateResume () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3471 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3472 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_STEP)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3473 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3474 | 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] | 3475 | m_mod_id.GetStopID(), |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3476 | StateAsCString(m_public_state.GetValue()), |
| 3477 | StateAsCString(m_private_state.GetValue())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3478 | |
| 3479 | Error error (WillResume()); |
| 3480 | // Tell the process it is about to resume before the thread list |
| 3481 | if (error.Success()) |
| 3482 | { |
Johnny Chen | c4221e4 | 2010-12-02 20:53:05 +0000 | [diff] [blame] | 3483 | // 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] | 3484 | // can let all of our threads know that they are about to be |
| 3485 | // resumed. Threads will each be called with |
| 3486 | // Thread::WillResume(StateType) where StateType contains the state |
| 3487 | // that they are supposed to have when the process is resumed |
| 3488 | // (suspended/running/stepping). Threads should also check |
| 3489 | // their resume signal in lldb::Thread::GetResumeSignal() |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3490 | // 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] | 3491 | if (m_thread_list.WillResume()) |
| 3492 | { |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3493 | // Last thing, do the PreResumeActions. |
| 3494 | if (!RunPreResumeActions()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3495 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3496 | error.SetErrorStringWithFormat ("Process::PrivateResume PreResumeActions failed, not resuming."); |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3497 | } |
| 3498 | else |
| 3499 | { |
| 3500 | m_mod_id.BumpResumeID(); |
| 3501 | error = DoResume(); |
| 3502 | if (error.Success()) |
| 3503 | { |
| 3504 | DidResume(); |
| 3505 | m_thread_list.DidResume(); |
| 3506 | if (log) |
| 3507 | log->Printf ("Process thinks the process has resumed."); |
| 3508 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3509 | } |
| 3510 | } |
| 3511 | else |
| 3512 | { |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 3513 | // Somebody wanted to run without running. So generate a continue & a stopped event, |
| 3514 | // and let the world handle them. |
| 3515 | if (log) |
| 3516 | log->Printf ("Process::PrivateResume() asked to simulate a start & stop."); |
| 3517 | |
| 3518 | SetPrivateState(eStateRunning); |
| 3519 | SetPrivateState(eStateStopped); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3520 | } |
| 3521 | } |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3522 | else if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3523 | log->Printf ("Process::PrivateResume() got an error \"%s\".", error.AsCString("<unknown error>")); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3524 | return error; |
| 3525 | } |
| 3526 | |
| 3527 | Error |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 3528 | Process::Halt (bool clear_thread_plans) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3529 | { |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 3530 | // Don't clear the m_clear_thread_plans_on_stop, only set it to true if |
| 3531 | // in case it was already set and some thread plan logic calls halt on its |
| 3532 | // own. |
| 3533 | m_clear_thread_plans_on_stop |= clear_thread_plans; |
| 3534 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3535 | // First make sure we aren't in the middle of handling an event, or we might restart. This is pretty weak, since |
| 3536 | // we could just straightaway get another event. It just narrows the window... |
| 3537 | m_currently_handling_event.WaitForValueEqualTo(false); |
| 3538 | |
| 3539 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3540 | // Pause our private state thread so we can ensure no one else eats |
| 3541 | // the stop event out from under us. |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3542 | Listener halt_listener ("lldb.process.halt_listener"); |
| 3543 | HijackPrivateProcessEvents(&halt_listener); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3544 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3545 | EventSP event_sp; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3546 | Error error (WillHalt()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3547 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3548 | if (error.Success()) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3549 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3550 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3551 | bool caused_stop = false; |
| 3552 | |
| 3553 | // Ask the process subclass to actually halt our process |
| 3554 | error = DoHalt(caused_stop); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3555 | if (error.Success()) |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3556 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3557 | if (m_public_state.GetValue() == eStateAttaching) |
| 3558 | { |
| 3559 | SetExitStatus(SIGKILL, "Cancelled async attach."); |
| 3560 | Destroy (); |
| 3561 | } |
| 3562 | else |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3563 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3564 | // If "caused_stop" is true, then DoHalt stopped the process. If |
| 3565 | // "caused_stop" is false, the process was already stopped. |
| 3566 | // If the DoHalt caused the process to stop, then we want to catch |
| 3567 | // this event and set the interrupted bool to true before we pass |
| 3568 | // this along so clients know that the process was interrupted by |
| 3569 | // a halt command. |
| 3570 | if (caused_stop) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3571 | { |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3572 | // Wait for 1 second for the process to stop. |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3573 | TimeValue timeout_time; |
| 3574 | timeout_time = TimeValue::Now(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3575 | timeout_time.OffsetWithSeconds(10); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3576 | bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp); |
| 3577 | StateType state = ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3578 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3579 | if (!got_event || state == eStateInvalid) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3580 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3581 | // We timeout out and didn't get a stop event... |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3582 | error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState())); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3583 | } |
| 3584 | else |
| 3585 | { |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 3586 | if (StateIsStoppedState (state, false)) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3587 | { |
| 3588 | // We caused the process to interrupt itself, so mark this |
| 3589 | // as such in the stop event so clients can tell an interrupted |
| 3590 | // process from a natural stop |
| 3591 | ProcessEventData::SetInterruptedInEvent (event_sp.get(), true); |
| 3592 | } |
| 3593 | else |
| 3594 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3595 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3596 | if (log) |
| 3597 | log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state)); |
| 3598 | error.SetErrorString ("Did not get stopped event after halt."); |
| 3599 | } |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3600 | } |
| 3601 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3602 | DidHalt(); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3603 | } |
| 3604 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3605 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3606 | // Resume our private state thread before we post the event (if any) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3607 | RestorePrivateProcessEvents(); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3608 | |
| 3609 | // Post any event we might have consumed. If all goes well, we will have |
| 3610 | // stopped the process, intercepted the event and set the interrupted |
| 3611 | // bool in the event. Post it to the private event queue and that will end up |
| 3612 | // correctly setting the state. |
| 3613 | if (event_sp) |
| 3614 | m_private_state_broadcaster.BroadcastEvent(event_sp); |
| 3615 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3616 | return error; |
| 3617 | } |
| 3618 | |
| 3619 | Error |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3620 | Process::HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp) |
| 3621 | { |
| 3622 | Error error; |
| 3623 | if (m_public_state.GetValue() == eStateRunning) |
| 3624 | { |
| 3625 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3626 | if (log) |
| 3627 | log->Printf("Process::Destroy() About to halt."); |
| 3628 | error = Halt(); |
| 3629 | if (error.Success()) |
| 3630 | { |
| 3631 | // Consume the halt event. |
| 3632 | TimeValue timeout (TimeValue::Now()); |
| 3633 | timeout.OffsetWithSeconds(1); |
| 3634 | StateType state = WaitForProcessToStop (&timeout, &exit_event_sp); |
| 3635 | |
| 3636 | // If the process exited while we were waiting for it to stop, put the exited event into |
| 3637 | // the shared pointer passed in and return. Our caller doesn't need to do anything else, since |
| 3638 | // they don't have a process anymore... |
| 3639 | |
| 3640 | if (state == eStateExited || m_private_state.GetValue() == eStateExited) |
| 3641 | { |
| 3642 | if (log) |
| 3643 | log->Printf("Process::HaltForDestroyOrDetach() Process exited while waiting to Halt."); |
| 3644 | return error; |
| 3645 | } |
| 3646 | else |
| 3647 | exit_event_sp.reset(); // It is ok to consume any non-exit stop events |
| 3648 | |
| 3649 | if (state != eStateStopped) |
| 3650 | { |
| 3651 | if (log) |
| 3652 | log->Printf("Process::HaltForDestroyOrDetach() Halt failed to stop, state is: %s", StateAsCString(state)); |
| 3653 | // If we really couldn't stop the process then we should just error out here, but if the |
| 3654 | // lower levels just bobbled sending the event and we really are stopped, then continue on. |
| 3655 | StateType private_state = m_private_state.GetValue(); |
| 3656 | if (private_state != eStateStopped) |
| 3657 | { |
| 3658 | return error; |
| 3659 | } |
| 3660 | } |
| 3661 | } |
| 3662 | else |
| 3663 | { |
| 3664 | if (log) |
| 3665 | log->Printf("Process::HaltForDestroyOrDetach() Halt got error: %s", error.AsCString()); |
| 3666 | } |
| 3667 | } |
| 3668 | return error; |
| 3669 | } |
| 3670 | |
| 3671 | Error |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3672 | Process::Detach (bool keep_stopped) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3673 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3674 | EventSP exit_event_sp; |
| 3675 | Error error; |
| 3676 | m_destroy_in_process = true; |
| 3677 | |
| 3678 | error = WillDetach(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3679 | |
| 3680 | if (error.Success()) |
| 3681 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3682 | if (DetachRequiresHalt()) |
| 3683 | { |
| 3684 | error = HaltForDestroyOrDetach (exit_event_sp); |
| 3685 | if (!error.Success()) |
| 3686 | { |
| 3687 | m_destroy_in_process = false; |
| 3688 | return error; |
| 3689 | } |
| 3690 | else if (exit_event_sp) |
| 3691 | { |
| 3692 | // We shouldn't need to do anything else here. There's no process left to detach from... |
| 3693 | StopPrivateStateThread(); |
| 3694 | m_destroy_in_process = false; |
| 3695 | return error; |
| 3696 | } |
| 3697 | } |
| 3698 | |
Andrew MacPherson | c3826b5 | 2014-03-25 19:59:36 +0000 | [diff] [blame] | 3699 | m_thread_list.DiscardThreadPlans(); |
| 3700 | DisableAllBreakpointSites(); |
| 3701 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3702 | error = DoDetach(keep_stopped); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3703 | if (error.Success()) |
| 3704 | { |
| 3705 | DidDetach(); |
| 3706 | StopPrivateStateThread(); |
| 3707 | } |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3708 | else |
| 3709 | { |
| 3710 | return error; |
| 3711 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3712 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3713 | m_destroy_in_process = false; |
| 3714 | |
| 3715 | // If we exited when we were waiting for a process to stop, then |
| 3716 | // forward the event here so we don't lose the event |
| 3717 | if (exit_event_sp) |
| 3718 | { |
| 3719 | // Directly broadcast our exited event because we shut down our |
| 3720 | // private state thread above |
| 3721 | BroadcastEvent(exit_event_sp); |
| 3722 | } |
| 3723 | |
| 3724 | // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating |
| 3725 | // the last events through the event system, in which case we might strand the write lock. Unlock |
| 3726 | // it here so when we do to tear down the process we don't get an error destroying the lock. |
| 3727 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3728 | m_public_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3729 | return error; |
| 3730 | } |
| 3731 | |
| 3732 | Error |
| 3733 | Process::Destroy () |
| 3734 | { |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 3735 | |
| 3736 | // Tell ourselves we are in the process of destroying the process, so that we don't do any unnecessary work |
| 3737 | // that might hinder the destruction. Remember to set this back to false when we are done. That way if the attempt |
| 3738 | // failed and the process stays around for some reason it won't be in a confused state. |
| 3739 | |
| 3740 | m_destroy_in_process = true; |
| 3741 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3742 | Error error (WillDestroy()); |
| 3743 | if (error.Success()) |
| 3744 | { |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 3745 | EventSP exit_event_sp; |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3746 | if (DestroyRequiresHalt()) |
Jim Ingham | 04e0a22 | 2012-05-23 15:46:31 +0000 | [diff] [blame] | 3747 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3748 | error = HaltForDestroyOrDetach(exit_event_sp); |
Jim Ingham | 04e0a22 | 2012-05-23 15:46:31 +0000 | [diff] [blame] | 3749 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3750 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3751 | if (m_public_state.GetValue() != eStateRunning) |
| 3752 | { |
| 3753 | // Ditch all thread plans, and remove all our breakpoints: in case we have to restart the target to |
| 3754 | // kill it, we don't want it hitting a breakpoint... |
| 3755 | // Only do this if we've stopped, however, since if we didn't manage to halt it above, then |
| 3756 | // we're not going to have much luck doing this now. |
| 3757 | m_thread_list.DiscardThreadPlans(); |
| 3758 | DisableAllBreakpointSites(); |
| 3759 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3760 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3761 | error = DoDestroy(); |
| 3762 | if (error.Success()) |
| 3763 | { |
| 3764 | DidDestroy(); |
| 3765 | StopPrivateStateThread(); |
| 3766 | } |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3767 | m_stdio_communication.StopReadThread(); |
| 3768 | m_stdio_communication.Disconnect(); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3769 | |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3770 | if (m_process_input_reader) |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3771 | { |
| 3772 | m_process_input_reader->SetIsDone(true); |
| 3773 | m_process_input_reader->Cancel(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3774 | m_process_input_reader.reset(); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 3777 | // If we exited when we were waiting for a process to stop, then |
| 3778 | // forward the event here so we don't lose the event |
| 3779 | if (exit_event_sp) |
| 3780 | { |
| 3781 | // Directly broadcast our exited event because we shut down our |
| 3782 | // private state thread above |
| 3783 | BroadcastEvent(exit_event_sp); |
| 3784 | } |
| 3785 | |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3786 | // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating |
| 3787 | // the last events through the event system, in which case we might strand the write lock. Unlock |
| 3788 | // 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] | 3789 | m_public_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3790 | } |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 3791 | |
| 3792 | m_destroy_in_process = false; |
| 3793 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3794 | return error; |
| 3795 | } |
| 3796 | |
| 3797 | Error |
| 3798 | Process::Signal (int signal) |
| 3799 | { |
| 3800 | Error error (WillSignal()); |
| 3801 | if (error.Success()) |
| 3802 | { |
| 3803 | error = DoSignal(signal); |
| 3804 | if (error.Success()) |
| 3805 | DidSignal(); |
| 3806 | } |
| 3807 | return error; |
| 3808 | } |
| 3809 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3810 | lldb::ByteOrder |
| 3811 | Process::GetByteOrder () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3812 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3813 | return m_target.GetArchitecture().GetByteOrder(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | uint32_t |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3817 | Process::GetAddressByteSize () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3818 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3819 | return m_target.GetArchitecture().GetAddressByteSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3822 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3823 | bool |
| 3824 | Process::ShouldBroadcastEvent (Event *event_ptr) |
| 3825 | { |
| 3826 | const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr); |
| 3827 | bool return_value = true; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3828 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3829 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3830 | switch (state) |
| 3831 | { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3832 | case eStateConnected: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3833 | case eStateAttaching: |
| 3834 | case eStateLaunching: |
| 3835 | case eStateDetached: |
| 3836 | case eStateExited: |
| 3837 | case eStateUnloaded: |
| 3838 | // These events indicate changes in the state of the debugging session, always report them. |
| 3839 | return_value = true; |
| 3840 | break; |
| 3841 | case eStateInvalid: |
| 3842 | // We stopped for no apparent reason, don't report it. |
| 3843 | return_value = false; |
| 3844 | break; |
| 3845 | case eStateRunning: |
| 3846 | case eStateStepping: |
| 3847 | // If we've started the target running, we handle the cases where we |
| 3848 | // are already running and where there is a transition from stopped to |
| 3849 | // running differently. |
| 3850 | // running -> running: Automatically suppress extra running events |
| 3851 | // stopped -> running: Report except when there is one or more no votes |
| 3852 | // and no yes votes. |
| 3853 | SynchronouslyNotifyStateChanged (state); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3854 | if (m_force_next_event_delivery) |
| 3855 | return_value = true; |
| 3856 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3857 | { |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3858 | switch (m_last_broadcast_state) |
| 3859 | { |
| 3860 | case eStateRunning: |
| 3861 | case eStateStepping: |
| 3862 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 3863 | return_value = false; |
| 3864 | break; |
| 3865 | default: |
| 3866 | // TODO: make this work correctly. For now always report |
| 3867 | // run if we aren't running so we don't miss any runnning |
| 3868 | // events. If I run the lldb/test/thread/a.out file and |
| 3869 | // break at main.cpp:58, run and hit the breakpoints on |
| 3870 | // multiple threads, then somehow during the stepping over |
| 3871 | // of all breakpoints no run gets reported. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3872 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3873 | // This is a transition from stop to run. |
| 3874 | switch (m_thread_list.ShouldReportRun (event_ptr)) |
| 3875 | { |
| 3876 | case eVoteYes: |
| 3877 | case eVoteNoOpinion: |
| 3878 | return_value = true; |
| 3879 | break; |
| 3880 | case eVoteNo: |
| 3881 | return_value = false; |
| 3882 | break; |
| 3883 | } |
| 3884 | break; |
| 3885 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3886 | } |
| 3887 | break; |
| 3888 | case eStateStopped: |
| 3889 | case eStateCrashed: |
| 3890 | case eStateSuspended: |
| 3891 | { |
| 3892 | // We've stopped. First see if we're going to restart the target. |
| 3893 | // If we are going to stop, then we always broadcast the event. |
| 3894 | // 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] | 3895 | // If no thread has an opinion, we don't report it. |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3896 | |
Jim Ingham | cb4ca11 | 2012-05-16 01:32:14 +0000 | [diff] [blame] | 3897 | RefreshStateAfterStop (); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3898 | if (ProcessEventData::GetInterruptedFromEvent (event_ptr)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3899 | { |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3900 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3901 | log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3902 | static_cast<void*>(event_ptr), |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3903 | StateAsCString(state)); |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 3904 | // Even though we know we are going to stop, we should let the threads have a look at the stop, |
| 3905 | // so they can properly set their state. |
| 3906 | m_thread_list.ShouldStop (event_ptr); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3907 | return_value = true; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3908 | } |
| 3909 | else |
| 3910 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3911 | bool was_restarted = ProcessEventData::GetRestartedFromEvent (event_ptr); |
| 3912 | bool should_resume = false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3913 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3914 | // It makes no sense to ask "ShouldStop" if we've already been restarted... |
| 3915 | // Asking the thread list is also not likely to go well, since we are running again. |
| 3916 | // So in that case just report the event. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3917 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3918 | if (!was_restarted) |
| 3919 | should_resume = m_thread_list.ShouldStop (event_ptr) == false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3920 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3921 | if (was_restarted || should_resume || m_resume_requested) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3922 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3923 | Vote stop_vote = m_thread_list.ShouldReportStop (event_ptr); |
| 3924 | if (log) |
| 3925 | log->Printf ("Process::ShouldBroadcastEvent: should_stop: %i state: %s was_restarted: %i stop_vote: %d.", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3926 | should_resume, StateAsCString(state), |
| 3927 | was_restarted, stop_vote); |
| 3928 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3929 | switch (stop_vote) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3930 | { |
| 3931 | case eVoteYes: |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3932 | return_value = true; |
| 3933 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3934 | case eVoteNoOpinion: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3935 | case eVoteNo: |
| 3936 | return_value = false; |
| 3937 | break; |
| 3938 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3939 | |
Jim Ingham | cb95f34 | 2012-09-05 21:13:56 +0000 | [diff] [blame] | 3940 | if (!was_restarted) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3941 | { |
| 3942 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3943 | log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", |
| 3944 | static_cast<void*>(event_ptr), |
| 3945 | StateAsCString(state)); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3946 | ProcessEventData::SetRestartedInEvent(event_ptr, true); |
Jim Ingham | cb95f34 | 2012-09-05 21:13:56 +0000 | [diff] [blame] | 3947 | PrivateResume (); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3948 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3949 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3950 | } |
| 3951 | else |
| 3952 | { |
| 3953 | return_value = true; |
| 3954 | SynchronouslyNotifyStateChanged (state); |
| 3955 | } |
| 3956 | } |
| 3957 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3958 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3959 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3960 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3961 | // Forcing the next event delivery is a one shot deal. So reset it here. |
| 3962 | m_force_next_event_delivery = false; |
| 3963 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3964 | // We do some coalescing of events (for instance two consecutive running events get coalesced.) |
| 3965 | // But we only coalesce against events we actually broadcast. So we use m_last_broadcast_state |
| 3966 | // to track that. NB - you can't use "m_public_state.GetValue()" for that purpose, as was originally done, |
| 3967 | // because the PublicState reflects the last event pulled off the queue, and there may be several |
| 3968 | // events stacked up on the queue unserviced. So the PublicState may not reflect the last broadcasted event |
| 3969 | // yet. m_last_broadcast_state gets updated here. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3970 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3971 | if (return_value) |
| 3972 | m_last_broadcast_state = state; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3973 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3974 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3975 | log->Printf ("Process::ShouldBroadcastEvent (%p) => new state: %s, last broadcast state: %s - %s", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3976 | static_cast<void*>(event_ptr), StateAsCString(state), |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3977 | StateAsCString(m_last_broadcast_state), |
| 3978 | return_value ? "YES" : "NO"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3979 | return return_value; |
| 3980 | } |
| 3981 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3982 | |
| 3983 | bool |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3984 | Process::StartPrivateStateThread (bool force) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3985 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3986 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3987 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3988 | bool already_running = PrivateStateThreadIsValid (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3989 | if (log) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3990 | log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread"); |
| 3991 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3992 | if (!force && already_running) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3993 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3994 | |
| 3995 | // Create a thread that watches our internal state and controls which |
| 3996 | // events make it to clients (into the DCProcess event queue). |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 3997 | char thread_name[1024]; |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3998 | if (already_running) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3999 | 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] | 4000 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4001 | 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] | 4002 | |
| 4003 | // Create the private state thread, and start it running. |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 4004 | m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL); |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4005 | bool success = IS_VALID_LLDB_HOST_THREAD(m_private_state_thread); |
| 4006 | if (success) |
| 4007 | { |
| 4008 | ResumePrivateStateThread(); |
| 4009 | return true; |
| 4010 | } |
| 4011 | else |
| 4012 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | void |
| 4016 | Process::PausePrivateStateThread () |
| 4017 | { |
| 4018 | ControlPrivateStateThread (eBroadcastInternalStateControlPause); |
| 4019 | } |
| 4020 | |
| 4021 | void |
| 4022 | Process::ResumePrivateStateThread () |
| 4023 | { |
| 4024 | ControlPrivateStateThread (eBroadcastInternalStateControlResume); |
| 4025 | } |
| 4026 | |
| 4027 | void |
| 4028 | Process::StopPrivateStateThread () |
| 4029 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 4030 | if (PrivateStateThreadIsValid ()) |
| 4031 | ControlPrivateStateThread (eBroadcastInternalStateControlStop); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4032 | else |
| 4033 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4034 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4035 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4036 | 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] | 4037 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | void |
| 4041 | Process::ControlPrivateStateThread (uint32_t signal) |
| 4042 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4043 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4044 | |
| 4045 | assert (signal == eBroadcastInternalStateControlStop || |
| 4046 | signal == eBroadcastInternalStateControlPause || |
| 4047 | signal == eBroadcastInternalStateControlResume); |
| 4048 | |
| 4049 | if (log) |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 4050 | log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4051 | |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 4052 | // Signal the private state thread. First we should copy this is case the |
| 4053 | // thread starts exiting since the private state thread will NULL this out |
| 4054 | // when it exits |
| 4055 | const lldb::thread_t private_state_thread = m_private_state_thread; |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 4056 | if (IS_VALID_LLDB_HOST_THREAD(private_state_thread)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4057 | { |
| 4058 | TimeValue timeout_time; |
| 4059 | bool timed_out; |
| 4060 | |
| 4061 | m_private_state_control_broadcaster.BroadcastEvent (signal, NULL); |
| 4062 | |
| 4063 | timeout_time = TimeValue::Now(); |
| 4064 | timeout_time.OffsetWithSeconds(2); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4065 | if (log) |
| 4066 | log->Printf ("Sending control event of type: %d.", signal); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4067 | m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out); |
| 4068 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 4069 | |
| 4070 | if (signal == eBroadcastInternalStateControlStop) |
| 4071 | { |
| 4072 | if (timed_out) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4073 | { |
| 4074 | Error error; |
| 4075 | Host::ThreadCancel (private_state_thread, &error); |
| 4076 | if (log) |
| 4077 | log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString()); |
| 4078 | } |
| 4079 | else |
| 4080 | { |
| 4081 | if (log) |
| 4082 | log->Printf ("The control event killed the private state thread without having to cancel."); |
| 4083 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4084 | |
| 4085 | thread_result_t result = NULL; |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 4086 | Host::ThreadJoin (private_state_thread, &result, NULL); |
Greg Clayton | 49182ed | 2010-07-22 18:34:21 +0000 | [diff] [blame] | 4087 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4088 | } |
| 4089 | } |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4090 | else |
| 4091 | { |
| 4092 | if (log) |
| 4093 | log->Printf ("Private state thread already dead, no need to signal it to stop."); |
| 4094 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | void |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4098 | Process::SendAsyncInterrupt () |
| 4099 | { |
| 4100 | if (PrivateStateThreadIsValid()) |
| 4101 | m_private_state_broadcaster.BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 4102 | else |
| 4103 | BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 4104 | } |
| 4105 | |
| 4106 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4107 | Process::HandlePrivateEvent (EventSP &event_sp) |
| 4108 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4109 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4110 | m_resume_requested = false; |
| 4111 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 4112 | m_currently_handling_event.SetValue(true, eBroadcastNever); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4113 | |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4114 | const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4115 | |
| 4116 | // First check to see if anybody wants a shot at this event: |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 4117 | if (m_next_event_action_ap.get() != NULL) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4118 | { |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 4119 | NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4120 | if (log) |
| 4121 | log->Printf ("Ran next event action, result was %d.", action_result); |
| 4122 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4123 | switch (action_result) |
| 4124 | { |
| 4125 | case NextEventAction::eEventActionSuccess: |
| 4126 | SetNextEventAction(NULL); |
| 4127 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 4128 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4129 | case NextEventAction::eEventActionRetry: |
| 4130 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 4131 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4132 | case NextEventAction::eEventActionExit: |
Jim Ingham | 2a5fdd4 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 4133 | // Handle Exiting Here. If we already got an exited event, |
| 4134 | // we should just propagate it. Otherwise, swallow this event, |
| 4135 | // and set our state to exit so the next event will kill us. |
| 4136 | if (new_state != eStateExited) |
| 4137 | { |
| 4138 | // FIXME: should cons up an exited event, and discard this one. |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 4139 | SetExitStatus(0, m_next_event_action_ap->GetExitString()); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4140 | m_currently_handling_event.SetValue(false, eBroadcastAlways); |
Jim Ingham | 2a5fdd4 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 4141 | SetNextEventAction(NULL); |
| 4142 | return; |
| 4143 | } |
| 4144 | SetNextEventAction(NULL); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4145 | break; |
| 4146 | } |
| 4147 | } |
| 4148 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4149 | // See if we should broadcast this state to external clients? |
| 4150 | const bool should_broadcast = ShouldBroadcastEvent (event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4151 | |
| 4152 | if (should_broadcast) |
| 4153 | { |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4154 | const bool is_hijacked = IsHijackedForEvent(eBroadcastBitStateChanged); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4155 | if (log) |
| 4156 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4157 | 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] | 4158 | __FUNCTION__, |
| 4159 | GetID(), |
| 4160 | StateAsCString(new_state), |
| 4161 | StateAsCString (GetState ()), |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4162 | is_hijacked ? "hijacked" : "public"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4163 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4164 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4165 | if (StateIsRunningState (new_state)) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4166 | { |
| 4167 | // Only push the input handler if we aren't fowarding events, |
| 4168 | // as this means the curses GUI is in use... |
| 4169 | if (!GetTarget().GetDebugger().IsForwardingEvents()) |
| 4170 | PushProcessIOHandler (); |
| 4171 | } |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4172 | else if (StateIsStoppedState(new_state, false)) |
| 4173 | { |
| 4174 | if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 4175 | { |
| 4176 | // If the lldb_private::Debugger is handling the events, we don't |
| 4177 | // want to pop the process IOHandler here, we want to do it when |
| 4178 | // we receive the stopped event so we can carefully control when |
| 4179 | // the process IOHandler is popped because when we stop we want to |
| 4180 | // display some text stating how and why we stopped, then maybe some |
| 4181 | // process/thread/frame info, and then we want the "(lldb) " prompt |
| 4182 | // to show up. If we pop the process IOHandler here, then we will |
| 4183 | // cause the command interpreter to become the top IOHandler after |
| 4184 | // the process pops off and it will update its prompt right away... |
| 4185 | // See the Debugger.cpp file where it calls the function as |
| 4186 | // "process_sp->PopProcessIOHandler()" to see where I am talking about. |
| 4187 | // Otherwise we end up getting overlapping "(lldb) " prompts and |
| 4188 | // garbled output. |
| 4189 | // |
| 4190 | // If we aren't handling the events in the debugger (which is indicated |
| 4191 | // by "m_target.GetDebugger().IsHandlingEvents()" returning false) or we |
| 4192 | // are hijacked, then we always pop the process IO handler manually. |
| 4193 | // Hijacking happens when the internal process state thread is running |
| 4194 | // thread plans, or when commands want to run in synchronous mode |
| 4195 | // and they call "process->WaitForProcessToStop()". An example of something |
| 4196 | // that will hijack the events is a simple expression: |
| 4197 | // |
| 4198 | // (lldb) expr (int)puts("hello") |
| 4199 | // |
| 4200 | // This will cause the internal process state thread to resume and halt |
| 4201 | // the process (and _it_ will hijack the eBroadcastBitStateChanged |
| 4202 | // events) and we do need the IO handler to be pushed and popped |
| 4203 | // correctly. |
| 4204 | |
| 4205 | if (is_hijacked || m_target.GetDebugger().IsHandlingEvents() == false) |
| 4206 | PopProcessIOHandler (); |
| 4207 | } |
| 4208 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4209 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4210 | BroadcastEvent (event_sp); |
| 4211 | } |
| 4212 | else |
| 4213 | { |
| 4214 | if (log) |
| 4215 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4216 | 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] | 4217 | __FUNCTION__, |
| 4218 | GetID(), |
| 4219 | StateAsCString(new_state), |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 4220 | StateAsCString (GetState ())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4221 | } |
| 4222 | } |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 4223 | m_currently_handling_event.SetValue(false, eBroadcastAlways); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4226 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4227 | Process::PrivateStateThread (void *arg) |
| 4228 | { |
| 4229 | Process *proc = static_cast<Process*> (arg); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4230 | thread_result_t result = proc->RunPrivateStateThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4231 | return result; |
| 4232 | } |
| 4233 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4234 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4235 | Process::RunPrivateStateThread () |
| 4236 | { |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4237 | bool control_only = true; |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4238 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4239 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4240 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4241 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4242 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...", |
| 4243 | __FUNCTION__, static_cast<void*>(this), GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4244 | |
| 4245 | bool exit_now = false; |
| 4246 | while (!exit_now) |
| 4247 | { |
| 4248 | EventSP event_sp; |
| 4249 | WaitForEventsPrivate (NULL, event_sp, control_only); |
| 4250 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) |
| 4251 | { |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4252 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4253 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") got a control event: %d", |
| 4254 | __FUNCTION__, static_cast<void*>(this), GetID(), |
| 4255 | event_sp->GetType()); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4256 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4257 | switch (event_sp->GetType()) |
| 4258 | { |
| 4259 | case eBroadcastInternalStateControlStop: |
| 4260 | exit_now = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4261 | break; // doing any internal state managment below |
| 4262 | |
| 4263 | case eBroadcastInternalStateControlPause: |
| 4264 | control_only = true; |
| 4265 | break; |
| 4266 | |
| 4267 | case eBroadcastInternalStateControlResume: |
| 4268 | control_only = false; |
| 4269 | break; |
| 4270 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4271 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4272 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4273 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4274 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4275 | else if (event_sp->GetType() == eBroadcastBitInterrupt) |
| 4276 | { |
| 4277 | if (m_public_state.GetValue() == eStateAttaching) |
| 4278 | { |
| 4279 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4280 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt while attaching - forwarding interrupt.", |
| 4281 | __FUNCTION__, static_cast<void*>(this), |
| 4282 | GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4283 | BroadcastEvent (eBroadcastBitInterrupt, NULL); |
| 4284 | } |
| 4285 | else |
| 4286 | { |
| 4287 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4288 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt - Halting.", |
| 4289 | __FUNCTION__, static_cast<void*>(this), |
| 4290 | GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4291 | Halt(); |
| 4292 | } |
| 4293 | continue; |
| 4294 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4295 | |
| 4296 | const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 4297 | |
| 4298 | if (internal_state != eStateInvalid) |
| 4299 | { |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 4300 | if (m_clear_thread_plans_on_stop && |
| 4301 | StateIsStoppedState(internal_state, true)) |
| 4302 | { |
| 4303 | m_clear_thread_plans_on_stop = false; |
| 4304 | m_thread_list.DiscardThreadPlans(); |
| 4305 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4306 | HandlePrivateEvent (event_sp); |
| 4307 | } |
| 4308 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 4309 | if (internal_state == eStateInvalid || |
| 4310 | internal_state == eStateExited || |
| 4311 | internal_state == eStateDetached ) |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4312 | { |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4313 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4314 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") about to exit with internal state %s...", |
| 4315 | __FUNCTION__, static_cast<void*>(this), GetID(), |
| 4316 | StateAsCString(internal_state)); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4317 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4318 | break; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4319 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 4322 | // Verify log is still enabled before attempting to write to it... |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4323 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4324 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", |
| 4325 | __FUNCTION__, static_cast<void*>(this), GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4326 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 4327 | m_public_run_lock.SetStopped(); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 4328 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
| 4329 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4330 | return NULL; |
| 4331 | } |
| 4332 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4333 | //------------------------------------------------------------------ |
| 4334 | // Process Event Data |
| 4335 | //------------------------------------------------------------------ |
| 4336 | |
| 4337 | Process::ProcessEventData::ProcessEventData () : |
| 4338 | EventData (), |
| 4339 | m_process_sp (), |
| 4340 | m_state (eStateInvalid), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 4341 | m_restarted (false), |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4342 | m_update_state (0), |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4343 | m_interrupted (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4344 | { |
| 4345 | } |
| 4346 | |
| 4347 | Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) : |
| 4348 | EventData (), |
| 4349 | m_process_sp (process_sp), |
| 4350 | m_state (state), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 4351 | m_restarted (false), |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4352 | m_update_state (0), |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4353 | m_interrupted (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4354 | { |
| 4355 | } |
| 4356 | |
| 4357 | Process::ProcessEventData::~ProcessEventData() |
| 4358 | { |
| 4359 | } |
| 4360 | |
| 4361 | const ConstString & |
| 4362 | Process::ProcessEventData::GetFlavorString () |
| 4363 | { |
| 4364 | static ConstString g_flavor ("Process::ProcessEventData"); |
| 4365 | return g_flavor; |
| 4366 | } |
| 4367 | |
| 4368 | const ConstString & |
| 4369 | Process::ProcessEventData::GetFlavor () const |
| 4370 | { |
| 4371 | return ProcessEventData::GetFlavorString (); |
| 4372 | } |
| 4373 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4374 | void |
| 4375 | Process::ProcessEventData::DoOnRemoval (Event *event_ptr) |
| 4376 | { |
| 4377 | // 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] | 4378 | // off of the private process event queue, and then any number of times, first when it gets pulled off of |
| 4379 | // the public event queue, then other times when we're pretending that this is where we stopped at the |
| 4380 | // end of expression evaluation. m_update_state is used to distinguish these |
| 4381 | // 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] | 4382 | // 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] | 4383 | if (m_update_state != 1) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4384 | return; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4385 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4386 | m_process_sp->SetPublicState (m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr)); |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 4387 | |
| 4388 | // If this is a halt event, even if the halt stopped with some reason other than a plain interrupt (e.g. we had |
| 4389 | // already stopped for a breakpoint when the halt request came through) don't do the StopInfo actions, as they may |
| 4390 | // end up restarting the process. |
| 4391 | if (m_interrupted) |
| 4392 | return; |
| 4393 | |
| 4394 | // If we're stopped and haven't restarted, then do the StopInfo actions here: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4395 | if (m_state == eStateStopped && ! m_restarted) |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4396 | { |
| 4397 | ThreadList &curr_thread_list = m_process_sp->GetThreadList(); |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4398 | uint32_t num_threads = curr_thread_list.GetSize(); |
| 4399 | uint32_t idx; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 4400 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4401 | // The actions might change one of the thread's stop_info's opinions about whether we should |
| 4402 | // stop the process, so we need to query that as we go. |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4403 | |
| 4404 | // One other complication here, is that we try to catch any case where the target has run (except for expressions) |
| 4405 | // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and |
| 4406 | // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like |
| 4407 | // 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 |
| 4408 | // against this list & bag out if anything differs. |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4409 | std::vector<uint32_t> thread_index_array(num_threads); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4410 | for (idx = 0; idx < num_threads; ++idx) |
| 4411 | thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID(); |
| 4412 | |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4413 | // Use this to track whether we should continue from here. We will only continue the target running if |
| 4414 | // no thread says we should stop. Of course if some thread's PerformAction actually sets the target running, |
| 4415 | // then it doesn't matter what the other threads say... |
| 4416 | |
| 4417 | bool still_should_stop = false; |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4418 | |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4419 | // Sometimes - for instance if we have a bug in the stub we are talking to, we stop but no thread has a |
| 4420 | // valid stop reason. In that case we should just stop, because we have no way of telling what the right |
| 4421 | // thing to do is, and it's better to let the user decide than continue behind their backs. |
| 4422 | |
| 4423 | bool does_anybody_have_an_opinion = false; |
| 4424 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4425 | for (idx = 0; idx < num_threads; ++idx) |
| 4426 | { |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4427 | curr_thread_list = m_process_sp->GetThreadList(); |
| 4428 | if (curr_thread_list.GetSize() != num_threads) |
| 4429 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4430 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4431 | if (log) |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4432 | 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] | 4433 | break; |
| 4434 | } |
| 4435 | |
| 4436 | lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx); |
| 4437 | |
| 4438 | if (thread_sp->GetIndexID() != thread_index_array[idx]) |
| 4439 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4440 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4441 | if (log) |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4442 | 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] | 4443 | idx, |
| 4444 | thread_index_array[idx], |
| 4445 | thread_sp->GetIndexID()); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4446 | break; |
| 4447 | } |
| 4448 | |
Jim Ingham | b15bfc7 | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 4449 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo (); |
Jim Ingham | 5d88a06 | 2012-10-16 00:09:33 +0000 | [diff] [blame] | 4450 | if (stop_info_sp && stop_info_sp->IsValid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4451 | { |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4452 | does_anybody_have_an_opinion = true; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4453 | bool this_thread_wants_to_stop; |
| 4454 | if (stop_info_sp->GetOverrideShouldStop()) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4455 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4456 | this_thread_wants_to_stop = stop_info_sp->GetOverriddenShouldStopValue(); |
| 4457 | } |
| 4458 | else |
| 4459 | { |
| 4460 | stop_info_sp->PerformAction(event_ptr); |
| 4461 | // The stop action might restart the target. If it does, then we want to mark that in the |
| 4462 | // event so that whoever is receiving it will know to wait for the running event and reflect |
| 4463 | // that state appropriately. |
| 4464 | // We also need to stop processing actions, since they aren't expecting the target to be running. |
| 4465 | |
| 4466 | // FIXME: we might have run. |
| 4467 | if (stop_info_sp->HasTargetRunSinceMe()) |
| 4468 | { |
| 4469 | SetRestarted (true); |
| 4470 | break; |
| 4471 | } |
| 4472 | |
| 4473 | this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4474 | } |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4475 | |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4476 | if (still_should_stop == false) |
| 4477 | still_should_stop = this_thread_wants_to_stop; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4478 | } |
| 4479 | } |
Jim Ingham | 3ebcf7f | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 4480 | |
Ashok Thirumurthi | cf7c55e | 2013-04-18 14:38:20 +0000 | [diff] [blame] | 4481 | |
Jim Ingham | a8ca6e2 | 2013-05-03 23:04:37 +0000 | [diff] [blame] | 4482 | if (!GetRestarted()) |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4483 | { |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4484 | if (!still_should_stop && does_anybody_have_an_opinion) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4485 | { |
| 4486 | // We've been asked to continue, so do that here. |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4487 | SetRestarted(true); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 4488 | // Use the public resume method here, since this is just |
| 4489 | // extending a public resume. |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4490 | m_process_sp->PrivateResume(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4491 | } |
| 4492 | else |
| 4493 | { |
| 4494 | // If we didn't restart, run the Stop Hooks here: |
| 4495 | // They might also restart the target, so watch for that. |
| 4496 | m_process_sp->GetTarget().RunStopHooks(); |
| 4497 | if (m_process_sp->GetPrivateState() == eStateRunning) |
| 4498 | SetRestarted(true); |
| 4499 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4500 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4501 | } |
| 4502 | } |
| 4503 | |
| 4504 | void |
| 4505 | Process::ProcessEventData::Dump (Stream *s) const |
| 4506 | { |
| 4507 | if (m_process_sp) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4508 | s->Printf(" process = %p (pid = %" PRIu64 "), ", |
| 4509 | static_cast<void*>(m_process_sp.get()), m_process_sp->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4510 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 4511 | s->Printf("state = %s", StateAsCString(GetState())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | const Process::ProcessEventData * |
| 4515 | Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr) |
| 4516 | { |
| 4517 | if (event_ptr) |
| 4518 | { |
| 4519 | const EventData *event_data = event_ptr->GetData(); |
| 4520 | if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 4521 | return static_cast <const ProcessEventData *> (event_ptr->GetData()); |
| 4522 | } |
| 4523 | return NULL; |
| 4524 | } |
| 4525 | |
| 4526 | ProcessSP |
| 4527 | Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr) |
| 4528 | { |
| 4529 | ProcessSP process_sp; |
| 4530 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4531 | if (data) |
| 4532 | process_sp = data->GetProcessSP(); |
| 4533 | return process_sp; |
| 4534 | } |
| 4535 | |
| 4536 | StateType |
| 4537 | Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr) |
| 4538 | { |
| 4539 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4540 | if (data == NULL) |
| 4541 | return eStateInvalid; |
| 4542 | else |
| 4543 | return data->GetState(); |
| 4544 | } |
| 4545 | |
| 4546 | bool |
| 4547 | Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr) |
| 4548 | { |
| 4549 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4550 | if (data == NULL) |
| 4551 | return false; |
| 4552 | else |
| 4553 | return data->GetRestarted(); |
| 4554 | } |
| 4555 | |
| 4556 | void |
| 4557 | Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value) |
| 4558 | { |
| 4559 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4560 | if (data != NULL) |
| 4561 | data->SetRestarted(new_value); |
| 4562 | } |
| 4563 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4564 | size_t |
| 4565 | Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr) |
| 4566 | { |
| 4567 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4568 | if (data != NULL) |
| 4569 | return data->GetNumRestartedReasons(); |
| 4570 | else |
| 4571 | return 0; |
| 4572 | } |
| 4573 | |
| 4574 | const char * |
| 4575 | Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx) |
| 4576 | { |
| 4577 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4578 | if (data != NULL) |
| 4579 | return data->GetRestartedReasonAtIndex(idx); |
| 4580 | else |
| 4581 | return NULL; |
| 4582 | } |
| 4583 | |
| 4584 | void |
| 4585 | Process::ProcessEventData::AddRestartedReason (Event *event_ptr, const char *reason) |
| 4586 | { |
| 4587 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4588 | if (data != NULL) |
| 4589 | data->AddRestartedReason(reason); |
| 4590 | } |
| 4591 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4592 | bool |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4593 | Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr) |
| 4594 | { |
| 4595 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4596 | if (data == NULL) |
| 4597 | return false; |
| 4598 | else |
| 4599 | return data->GetInterrupted (); |
| 4600 | } |
| 4601 | |
| 4602 | void |
| 4603 | Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value) |
| 4604 | { |
| 4605 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4606 | if (data != NULL) |
| 4607 | data->SetInterrupted(new_value); |
| 4608 | } |
| 4609 | |
| 4610 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4611 | Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr) |
| 4612 | { |
| 4613 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4614 | if (data) |
| 4615 | { |
| 4616 | data->SetUpdateStateOnRemoval(); |
| 4617 | return true; |
| 4618 | } |
| 4619 | return false; |
| 4620 | } |
| 4621 | |
Greg Clayton | d9e416c | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 4622 | lldb::TargetSP |
| 4623 | Process::CalculateTarget () |
| 4624 | { |
| 4625 | return m_target.shared_from_this(); |
| 4626 | } |
| 4627 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4628 | void |
Greg Clayton | 0603aa9 | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 4629 | Process::CalculateExecutionContext (ExecutionContext &exe_ctx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4630 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4631 | exe_ctx.SetTargetPtr (&m_target); |
| 4632 | exe_ctx.SetProcessPtr (this); |
| 4633 | exe_ctx.SetThreadPtr(NULL); |
| 4634 | exe_ctx.SetFramePtr (NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4637 | //uint32_t |
| 4638 | //Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 4639 | //{ |
| 4640 | // return 0; |
| 4641 | //} |
| 4642 | // |
| 4643 | //ArchSpec |
| 4644 | //Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
| 4645 | //{ |
| 4646 | // return Host::GetArchSpecForExistingProcess (pid); |
| 4647 | //} |
| 4648 | // |
| 4649 | //ArchSpec |
| 4650 | //Process::GetArchSpecForExistingProcess (const char *process_name) |
| 4651 | //{ |
| 4652 | // return Host::GetArchSpecForExistingProcess (process_name); |
| 4653 | //} |
| 4654 | // |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4655 | void |
| 4656 | Process::AppendSTDOUT (const char * s, size_t len) |
| 4657 | { |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 4658 | Mutex::Locker locker (m_stdio_communication_mutex); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4659 | m_stdout_data.append (s, len); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 4660 | BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (shared_from_this(), GetState())); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
| 4663 | void |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4664 | Process::AppendSTDERR (const char * s, size_t len) |
| 4665 | { |
| 4666 | Mutex::Locker locker (m_stdio_communication_mutex); |
| 4667 | m_stderr_data.append (s, len); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 4668 | BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (shared_from_this(), GetState())); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4671 | void |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4672 | Process::BroadcastAsyncProfileData(const std::string &one_profile_data) |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4673 | { |
| 4674 | Mutex::Locker locker (m_profile_data_comm_mutex); |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4675 | m_profile_data.push_back(one_profile_data); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4676 | BroadcastEventIfUnique (eBroadcastBitProfileData, new ProcessEventData (shared_from_this(), GetState())); |
| 4677 | } |
| 4678 | |
| 4679 | size_t |
| 4680 | Process::GetAsyncProfileData (char *buf, size_t buf_size, Error &error) |
| 4681 | { |
| 4682 | Mutex::Locker locker(m_profile_data_comm_mutex); |
Han Ming Ong | 929a94f | 2012-11-29 22:14:45 +0000 | [diff] [blame] | 4683 | if (m_profile_data.empty()) |
| 4684 | return 0; |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4685 | |
| 4686 | std::string &one_profile_data = m_profile_data.front(); |
| 4687 | size_t bytes_available = one_profile_data.size(); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4688 | if (bytes_available > 0) |
| 4689 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4690 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4691 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4692 | log->Printf ("Process::GetProfileData (buf = %p, size = %" PRIu64 ")", |
| 4693 | static_cast<void*>(buf), |
| 4694 | static_cast<uint64_t>(buf_size)); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4695 | if (bytes_available > buf_size) |
| 4696 | { |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4697 | memcpy(buf, one_profile_data.c_str(), buf_size); |
| 4698 | one_profile_data.erase(0, buf_size); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4699 | bytes_available = buf_size; |
| 4700 | } |
| 4701 | else |
| 4702 | { |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4703 | memcpy(buf, one_profile_data.c_str(), bytes_available); |
Han Ming Ong | 929a94f | 2012-11-29 22:14:45 +0000 | [diff] [blame] | 4704 | m_profile_data.erase(m_profile_data.begin()); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4705 | } |
| 4706 | } |
| 4707 | return bytes_available; |
| 4708 | } |
| 4709 | |
| 4710 | |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4711 | //------------------------------------------------------------------ |
| 4712 | // Process STDIO |
| 4713 | //------------------------------------------------------------------ |
| 4714 | |
| 4715 | size_t |
| 4716 | Process::GetSTDOUT (char *buf, size_t buf_size, Error &error) |
| 4717 | { |
| 4718 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 4719 | size_t bytes_available = m_stdout_data.size(); |
| 4720 | if (bytes_available > 0) |
| 4721 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4722 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4723 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4724 | log->Printf ("Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")", |
| 4725 | static_cast<void*>(buf), |
| 4726 | static_cast<uint64_t>(buf_size)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4727 | if (bytes_available > buf_size) |
| 4728 | { |
| 4729 | memcpy(buf, m_stdout_data.c_str(), buf_size); |
| 4730 | m_stdout_data.erase(0, buf_size); |
| 4731 | bytes_available = buf_size; |
| 4732 | } |
| 4733 | else |
| 4734 | { |
| 4735 | memcpy(buf, m_stdout_data.c_str(), bytes_available); |
| 4736 | m_stdout_data.clear(); |
| 4737 | } |
| 4738 | } |
| 4739 | return bytes_available; |
| 4740 | } |
| 4741 | |
| 4742 | |
| 4743 | size_t |
| 4744 | Process::GetSTDERR (char *buf, size_t buf_size, Error &error) |
| 4745 | { |
| 4746 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 4747 | size_t bytes_available = m_stderr_data.size(); |
| 4748 | if (bytes_available > 0) |
| 4749 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4750 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4751 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4752 | log->Printf ("Process::GetSTDERR (buf = %p, size = %" PRIu64 ")", |
| 4753 | static_cast<void*>(buf), |
| 4754 | static_cast<uint64_t>(buf_size)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4755 | if (bytes_available > buf_size) |
| 4756 | { |
| 4757 | memcpy(buf, m_stderr_data.c_str(), buf_size); |
| 4758 | m_stderr_data.erase(0, buf_size); |
| 4759 | bytes_available = buf_size; |
| 4760 | } |
| 4761 | else |
| 4762 | { |
| 4763 | memcpy(buf, m_stderr_data.c_str(), bytes_available); |
| 4764 | m_stderr_data.clear(); |
| 4765 | } |
| 4766 | } |
| 4767 | return bytes_available; |
| 4768 | } |
| 4769 | |
| 4770 | void |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4771 | Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len) |
| 4772 | { |
| 4773 | Process *process = (Process *) baton; |
| 4774 | process->AppendSTDOUT (static_cast<const char *>(src), src_len); |
| 4775 | } |
| 4776 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4777 | class IOHandlerProcessSTDIO : |
| 4778 | public IOHandler |
| 4779 | { |
| 4780 | public: |
| 4781 | IOHandlerProcessSTDIO (Process *process, |
| 4782 | int write_fd) : |
| 4783 | IOHandler(process->GetTarget().GetDebugger()), |
| 4784 | m_process (process), |
| 4785 | m_read_file (), |
| 4786 | m_write_file (write_fd, false), |
| 4787 | m_pipe_read(), |
| 4788 | m_pipe_write() |
| 4789 | { |
| 4790 | m_read_file.SetDescriptor(GetInputFD(), false); |
| 4791 | } |
| 4792 | |
| 4793 | virtual |
| 4794 | ~IOHandlerProcessSTDIO () |
| 4795 | { |
| 4796 | |
| 4797 | } |
| 4798 | |
| 4799 | bool |
| 4800 | OpenPipes () |
| 4801 | { |
| 4802 | if (m_pipe_read.IsValid() && m_pipe_write.IsValid()) |
| 4803 | return true; |
| 4804 | |
| 4805 | int fds[2]; |
Hafiz Abid Qadeer | 6eff101 | 2014-03-12 10:45:23 +0000 | [diff] [blame] | 4806 | #ifdef _WIN32 |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4807 | // pipe is not supported on windows so default to a fail condition |
| 4808 | int err = 1; |
| 4809 | #else |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4810 | int err = pipe(fds); |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4811 | #endif |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4812 | if (err == 0) |
| 4813 | { |
| 4814 | m_pipe_read.SetDescriptor(fds[0], true); |
| 4815 | m_pipe_write.SetDescriptor(fds[1], true); |
| 4816 | return true; |
| 4817 | } |
| 4818 | return false; |
| 4819 | } |
| 4820 | |
| 4821 | void |
| 4822 | ClosePipes() |
| 4823 | { |
| 4824 | m_pipe_read.Close(); |
| 4825 | m_pipe_write.Close(); |
| 4826 | } |
| 4827 | |
| 4828 | // Each IOHandler gets to run until it is done. It should read data |
| 4829 | // from the "in" and place output into "out" and "err and return |
| 4830 | // when done. |
| 4831 | virtual void |
| 4832 | Run () |
| 4833 | { |
| 4834 | if (m_read_file.IsValid() && m_write_file.IsValid()) |
| 4835 | { |
| 4836 | SetIsDone(false); |
| 4837 | if (OpenPipes()) |
| 4838 | { |
| 4839 | const int read_fd = m_read_file.GetDescriptor(); |
| 4840 | const int pipe_read_fd = m_pipe_read.GetDescriptor(); |
| 4841 | TerminalState terminal_state; |
| 4842 | terminal_state.Save (read_fd, false); |
| 4843 | Terminal terminal(read_fd); |
| 4844 | terminal.SetCanonical(false); |
| 4845 | terminal.SetEcho(false); |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4846 | // FD_ZERO, FD_SET are not supported on windows |
Hafiz Abid Qadeer | 6eff101 | 2014-03-12 10:45:23 +0000 | [diff] [blame] | 4847 | #ifndef _WIN32 |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4848 | while (!GetIsDone()) |
| 4849 | { |
| 4850 | fd_set read_fdset; |
| 4851 | FD_ZERO (&read_fdset); |
| 4852 | FD_SET (read_fd, &read_fdset); |
| 4853 | FD_SET (pipe_read_fd, &read_fdset); |
| 4854 | const int nfds = std::max<int>(read_fd, pipe_read_fd) + 1; |
| 4855 | int num_set_fds = select (nfds, &read_fdset, NULL, NULL, NULL); |
| 4856 | if (num_set_fds < 0) |
| 4857 | { |
| 4858 | const int select_errno = errno; |
| 4859 | |
| 4860 | if (select_errno != EINTR) |
| 4861 | SetIsDone(true); |
| 4862 | } |
| 4863 | else if (num_set_fds > 0) |
| 4864 | { |
| 4865 | char ch = 0; |
| 4866 | size_t n; |
| 4867 | if (FD_ISSET (read_fd, &read_fdset)) |
| 4868 | { |
| 4869 | n = 1; |
| 4870 | if (m_read_file.Read(&ch, n).Success() && n == 1) |
| 4871 | { |
| 4872 | if (m_write_file.Write(&ch, n).Fail() || n != 1) |
| 4873 | SetIsDone(true); |
| 4874 | } |
| 4875 | else |
| 4876 | SetIsDone(true); |
| 4877 | } |
| 4878 | if (FD_ISSET (pipe_read_fd, &read_fdset)) |
| 4879 | { |
| 4880 | // Consume the interrupt byte |
| 4881 | n = 1; |
| 4882 | m_pipe_read.Read (&ch, n); |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4883 | switch (ch) |
| 4884 | { |
| 4885 | case 'q': |
| 4886 | SetIsDone(true); |
| 4887 | break; |
| 4888 | case 'i': |
| 4889 | if (StateIsRunningState(m_process->GetState())) |
| 4890 | m_process->Halt(); |
| 4891 | break; |
| 4892 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4893 | } |
| 4894 | } |
| 4895 | } |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4896 | #endif |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4897 | terminal_state.Restore(); |
| 4898 | |
| 4899 | } |
| 4900 | else |
| 4901 | SetIsDone(true); |
| 4902 | } |
| 4903 | else |
| 4904 | SetIsDone(true); |
| 4905 | } |
| 4906 | |
| 4907 | // Hide any characters that have been displayed so far so async |
| 4908 | // output can be displayed. Refresh() will be called after the |
| 4909 | // output has been displayed. |
| 4910 | virtual void |
| 4911 | Hide () |
| 4912 | { |
| 4913 | |
| 4914 | } |
| 4915 | // Called when the async output has been received in order to update |
| 4916 | // the input reader (refresh the prompt and redisplay any current |
| 4917 | // line(s) that are being edited |
| 4918 | virtual void |
| 4919 | Refresh () |
| 4920 | { |
| 4921 | |
| 4922 | } |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4923 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4924 | virtual void |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4925 | Cancel () |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4926 | { |
| 4927 | size_t n = 1; |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4928 | char ch = 'q'; // Send 'q' for quit |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4929 | m_pipe_write.Write (&ch, n); |
| 4930 | } |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4931 | |
| 4932 | virtual void |
| 4933 | Interrupt () |
| 4934 | { |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4935 | #ifdef _MSC_VER |
| 4936 | // Windows doesn't support pipes, so we will send an async interrupt |
| 4937 | // event to stop the process |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4938 | if (StateIsRunningState(m_process->GetState())) |
Ed Maste | 96e51b8 | 2014-02-25 14:20:14 +0000 | [diff] [blame] | 4939 | m_process->SendAsyncInterrupt(); |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4940 | #else |
| 4941 | // Do only things that are safe to do in an interrupt context (like in |
| 4942 | // a SIGINT handler), like write 1 byte to a file descriptor. This will |
| 4943 | // interrupt the IOHandlerProcessSTDIO::Run() and we can look at the byte |
| 4944 | // that was written to the pipe and then call m_process->Halt() from a |
| 4945 | // much safer location in code. |
| 4946 | size_t n = 1; |
| 4947 | char ch = 'i'; // Send 'i' for interrupt |
| 4948 | m_pipe_write.Write (&ch, n); |
| 4949 | #endif |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4950 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4951 | |
| 4952 | virtual void |
| 4953 | GotEOF() |
| 4954 | { |
| 4955 | |
| 4956 | } |
| 4957 | |
| 4958 | protected: |
| 4959 | Process *m_process; |
| 4960 | File m_read_file; // Read from this file (usually actual STDIN for LLDB |
| 4961 | File m_write_file; // Write to this file (usually the master pty for getting io to debuggee) |
| 4962 | File m_pipe_read; |
| 4963 | File m_pipe_write; |
| 4964 | |
| 4965 | }; |
| 4966 | |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4967 | void |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4968 | Process::SetSTDIOFileDescriptor (int fd) |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4969 | { |
| 4970 | // First set up the Read Thread for reading/handling process I/O |
| 4971 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4972 | std::unique_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (fd, true)); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4973 | |
| 4974 | if (conn_ap.get()) |
| 4975 | { |
| 4976 | m_stdio_communication.SetConnection (conn_ap.release()); |
| 4977 | if (m_stdio_communication.IsConnected()) |
| 4978 | { |
| 4979 | m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this); |
| 4980 | m_stdio_communication.StartReadThread(); |
| 4981 | |
| 4982 | // Now read thread is set up, set up input reader. |
| 4983 | |
| 4984 | if (!m_process_input_reader.get()) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4985 | m_process_input_reader.reset (new IOHandlerProcessSTDIO (this, fd)); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4986 | } |
| 4987 | } |
| 4988 | } |
| 4989 | |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4990 | bool |
Greg Clayton | 6fea17e | 2014-03-03 19:15:20 +0000 | [diff] [blame] | 4991 | Process::ProcessIOHandlerIsActive () |
| 4992 | { |
| 4993 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 4994 | if (io_handler_sp) |
| 4995 | return m_target.GetDebugger().IsTopIOHandler (io_handler_sp); |
| 4996 | return false; |
| 4997 | } |
| 4998 | bool |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4999 | Process::PushProcessIOHandler () |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5000 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5001 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 5002 | if (io_handler_sp) |
| 5003 | { |
| 5004 | io_handler_sp->SetIsDone(false); |
| 5005 | m_target.GetDebugger().PushIOHandler (io_handler_sp); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 5006 | return true; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5007 | } |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 5008 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5009 | } |
| 5010 | |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 5011 | bool |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5012 | Process::PopProcessIOHandler () |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5013 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5014 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 5015 | if (io_handler_sp) |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 5016 | return m_target.GetDebugger().PopIOHandler (io_handler_sp); |
| 5017 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 5020 | // The process needs to know about installed plug-ins |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 5021 | void |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 5022 | Process::SettingsInitialize () |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 5023 | { |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 5024 | Thread::SettingsInitialize (); |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 5025 | } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 5026 | |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 5027 | void |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 5028 | Process::SettingsTerminate () |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 5029 | { |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 5030 | Thread::SettingsTerminate (); |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 5031 | } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 5032 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 5033 | ExecutionResults |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5034 | Process::RunThreadPlan (ExecutionContext &exe_ctx, |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5035 | lldb::ThreadPlanSP &thread_plan_sp, |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5036 | const EvaluateExpressionOptions &options, |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5037 | Stream &errors) |
| 5038 | { |
| 5039 | ExecutionResults return_value = eExecutionSetupError; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5040 | |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 5041 | if (thread_plan_sp.get() == NULL) |
| 5042 | { |
| 5043 | errors.Printf("RunThreadPlan called with empty thread plan."); |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5044 | return eExecutionSetupError; |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 5045 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5046 | |
Jim Ingham | 7d7931d | 2013-03-28 00:05:34 +0000 | [diff] [blame] | 5047 | if (!thread_plan_sp->ValidatePlan(NULL)) |
| 5048 | { |
| 5049 | errors.Printf ("RunThreadPlan called with an invalid thread plan."); |
| 5050 | return eExecutionSetupError; |
| 5051 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5052 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5053 | if (exe_ctx.GetProcessPtr() != this) |
| 5054 | { |
| 5055 | errors.Printf("RunThreadPlan called on wrong process."); |
| 5056 | return eExecutionSetupError; |
| 5057 | } |
| 5058 | |
| 5059 | Thread *thread = exe_ctx.GetThreadPtr(); |
| 5060 | if (thread == NULL) |
| 5061 | { |
| 5062 | errors.Printf("RunThreadPlan called with invalid thread."); |
| 5063 | return eExecutionSetupError; |
| 5064 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5065 | |
Jim Ingham | 17e5c4e | 2011-05-17 22:24:54 +0000 | [diff] [blame] | 5066 | // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes. |
| 5067 | // For that to be true the plan can't be private - since private plans suppress themselves in the |
| 5068 | // GetCompletedPlan call. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5069 | |
Jim Ingham | 17e5c4e | 2011-05-17 22:24:54 +0000 | [diff] [blame] | 5070 | bool orig_plan_private = thread_plan_sp->GetPrivate(); |
| 5071 | thread_plan_sp->SetPrivate(false); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5072 | |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 5073 | if (m_private_state.GetValue() != eStateStopped) |
| 5074 | { |
| 5075 | errors.Printf ("RunThreadPlan called while the private state was not stopped."); |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5076 | return eExecutionSetupError; |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 5077 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5078 | |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 5079 | // 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] | 5080 | const uint32_t thread_idx_id = thread->GetIndexID(); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 5081 | StackFrameSP selected_frame_sp = thread->GetSelectedFrame(); |
Jim Ingham | 11b0e05 | 2013-02-19 23:22:45 +0000 | [diff] [blame] | 5082 | if (!selected_frame_sp) |
| 5083 | { |
| 5084 | thread->SetSelectedFrame(0); |
| 5085 | selected_frame_sp = thread->GetSelectedFrame(); |
| 5086 | if (!selected_frame_sp) |
| 5087 | { |
| 5088 | errors.Printf("RunThreadPlan called without a selected frame on thread %d", thread_idx_id); |
| 5089 | return eExecutionSetupError; |
| 5090 | } |
| 5091 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5092 | |
Jim Ingham | 11b0e05 | 2013-02-19 23:22:45 +0000 | [diff] [blame] | 5093 | StackID ctx_frame_id = selected_frame_sp->GetStackID(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5094 | |
| 5095 | // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either, |
| 5096 | // so we should arrange to reset them as well. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5097 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5098 | lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5099 | |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 5100 | uint32_t selected_tid; |
| 5101 | StackID selected_stack_id; |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 5102 | if (selected_thread_sp) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5103 | { |
| 5104 | selected_tid = selected_thread_sp->GetIndexID(); |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 5105 | selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5106 | } |
| 5107 | else |
| 5108 | { |
| 5109 | selected_tid = LLDB_INVALID_THREAD_ID; |
| 5110 | } |
| 5111 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5112 | lldb::thread_t backup_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5113 | lldb::StateType old_state; |
| 5114 | lldb::ThreadPlanSP stopper_base_plan_sp; |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5115 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 5116 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5117 | if (Host::GetCurrentThread() == m_private_state_thread) |
| 5118 | { |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5119 | // Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since |
| 5120 | // we are the thread that is generating public events. |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5121 | // 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] | 5122 | // we are fielding public events here. |
| 5123 | if (log) |
Jason Molenda | d251c9d | 2012-11-17 01:41:04 +0000 | [diff] [blame] | 5124 | 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] | 5125 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5126 | backup_private_state_thread = m_private_state_thread; |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5127 | |
| 5128 | // One other bit of business: we want to run just this thread plan and anything it pushes, and then stop, |
| 5129 | // returning control here. |
| 5130 | // But in the normal course of things, the plan above us on the stack would be given a shot at the stop |
| 5131 | // event before deciding to stop, and we don't want that. So we insert a "stopper" base plan on the stack |
| 5132 | // before the plan we want to run. Since base plans always stop and return control to the user, that will |
| 5133 | // do just what we want. |
| 5134 | stopper_base_plan_sp.reset(new ThreadPlanBase (*thread)); |
| 5135 | thread->QueueThreadPlan (stopper_base_plan_sp, false); |
| 5136 | // Have to make sure our public state is stopped, since otherwise the reporting logic below doesn't work correctly. |
| 5137 | old_state = m_public_state.GetValue(); |
| 5138 | m_public_state.SetValueNoLock(eStateStopped); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5139 | |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5140 | // Now spin up the private state thread: |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5141 | StartPrivateStateThread(true); |
| 5142 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5143 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5144 | thread->QueueThreadPlan(thread_plan_sp, false); // This used to pass "true" does that make sense? |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5145 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5146 | if (options.GetDebug()) |
| 5147 | { |
| 5148 | // In this case, we aren't actually going to run, we just want to stop right away. |
| 5149 | // Flush this thread so we will refetch the stacks and show the correct backtrace. |
| 5150 | // FIXME: To make this prettier we should invent some stop reason for this, but that |
| 5151 | // is only cosmetic, and this functionality is only of use to lldb developers who can |
| 5152 | // live with not pretty... |
| 5153 | thread->Flush(); |
| 5154 | return eExecutionStoppedForDebug; |
| 5155 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5156 | |
Jim Ingham | 1e7a9ee | 2011-01-23 21:14:08 +0000 | [diff] [blame] | 5157 | Listener listener("lldb.process.listener.run-thread-plan"); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5158 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5159 | lldb::EventSP event_to_broadcast_sp; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5160 | |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 5161 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5162 | // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get |
| 5163 | // restored on exit to the function. |
| 5164 | // |
| 5165 | // If the event needs to propagate beyond the hijacker (e.g., the process exits during execution), then the event |
| 5166 | // is put into event_to_broadcast_sp for rebroadcasting. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5167 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5168 | ProcessEventHijacker run_thread_plan_hijacker (*this, &listener); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5169 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5170 | if (log) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5171 | { |
| 5172 | StreamString s; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5173 | thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5174 | 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] | 5175 | thread->GetIndexID(), |
| 5176 | thread->GetID(), |
| 5177 | s.GetData()); |
| 5178 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5179 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5180 | bool got_event; |
| 5181 | lldb::EventSP event_sp; |
| 5182 | lldb::StateType stop_state = lldb::eStateInvalid; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5183 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5184 | TimeValue* timeout_ptr = NULL; |
| 5185 | TimeValue real_timeout; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5186 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5187 | 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] | 5188 | bool do_resume = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5189 | bool handle_running_event = true; |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5190 | const uint64_t default_one_thread_timeout_usec = 250000; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5191 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5192 | // This is just for accounting: |
| 5193 | uint32_t num_resumes = 0; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5194 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5195 | TimeValue one_thread_timeout = TimeValue::Now(); |
| 5196 | TimeValue final_timeout = one_thread_timeout; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5197 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5198 | uint32_t timeout_usec = options.GetTimeoutUsec(); |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame^] | 5199 | |
| 5200 | // If we are going to run all threads the whole time, or if we are only going to run one thread, |
| 5201 | // then we don't need the first timeout. So we set the final timeout, and pretend we are after the |
| 5202 | // first timeout already. |
| 5203 | |
| 5204 | if (!options.GetStopOthers() || !options.GetTryAllThreads()) |
Jim Ingham | 286fb1e | 2014-02-28 02:52:06 +0000 | [diff] [blame] | 5205 | { |
| 5206 | before_first_timeout = false; |
| 5207 | final_timeout.OffsetWithMicroSeconds(timeout_usec); |
| 5208 | } |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame^] | 5209 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5210 | { |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5211 | uint64_t option_one_thread_timeout = options.GetOneThreadTimeoutUsec(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5212 | |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5213 | // If the overall wait is forever, then we only need to set the one thread timeout: |
| 5214 | if (timeout_usec == 0) |
| 5215 | { |
Ed Maste | 801335c | 2014-03-31 19:28:14 +0000 | [diff] [blame] | 5216 | if (option_one_thread_timeout != 0) |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5217 | one_thread_timeout.OffsetWithMicroSeconds(option_one_thread_timeout); |
| 5218 | else |
| 5219 | one_thread_timeout.OffsetWithMicroSeconds(default_one_thread_timeout_usec); |
| 5220 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5221 | else |
| 5222 | { |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5223 | // Otherwise, if the one thread timeout is set, make sure it isn't longer than the overall timeout, |
| 5224 | // and use it, otherwise use half the total timeout, bounded by the default_one_thread_timeout_usec. |
| 5225 | uint64_t computed_one_thread_timeout; |
| 5226 | if (option_one_thread_timeout != 0) |
| 5227 | { |
| 5228 | if (timeout_usec < option_one_thread_timeout) |
| 5229 | { |
| 5230 | errors.Printf("RunThreadPlan called without one thread timeout greater than total timeout"); |
| 5231 | return eExecutionSetupError; |
| 5232 | } |
| 5233 | computed_one_thread_timeout = option_one_thread_timeout; |
| 5234 | } |
| 5235 | else |
| 5236 | { |
| 5237 | computed_one_thread_timeout = timeout_usec / 2; |
| 5238 | if (computed_one_thread_timeout > default_one_thread_timeout_usec) |
| 5239 | computed_one_thread_timeout = default_one_thread_timeout_usec; |
| 5240 | } |
| 5241 | one_thread_timeout.OffsetWithMicroSeconds(computed_one_thread_timeout); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5242 | } |
| 5243 | final_timeout.OffsetWithMicroSeconds (timeout_usec); |
| 5244 | } |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame^] | 5245 | |
| 5246 | if (log) |
| 5247 | log->Printf ("Stop others: %u, try all: %u, one thread: %" PRIu64 " - all threads: %" PRIu64 ".\n", |
| 5248 | options.GetStopOthers(), |
| 5249 | options.GetTryAllThreads(), |
| 5250 | one_thread_timeout.GetAsMicroSecondsSinceJan1_1970(), |
| 5251 | final_timeout.GetAsMicroSecondsSinceJan1_1970()); |
| 5252 | |
| 5253 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5254 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5255 | // This isn't going to work if there are unfetched events on the queue. |
| 5256 | // Are there cases where we might want to run the remaining events here, and then try to |
| 5257 | // call the function? That's probably being too tricky for our own good. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5258 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5259 | Event *other_events = listener.PeekAtNextEvent(); |
| 5260 | if (other_events != NULL) |
| 5261 | { |
| 5262 | errors.Printf("Calling RunThreadPlan with pending events on the queue."); |
| 5263 | return eExecutionSetupError; |
| 5264 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5265 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5266 | // We also need to make sure that the next event is delivered. We might be calling a function as part of |
| 5267 | // a thread plan, in which case the last delivered event could be the running event, and we don't want |
| 5268 | // event coalescing to cause us to lose OUR running event... |
| 5269 | ForceNextEventDelivery(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5270 | |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 5271 | // This while loop must exit out the bottom, there's cleanup that we need to do when we are done. |
| 5272 | // So don't call return anywhere within it. |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 5273 | |
| 5274 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
| 5275 | // It's pretty much impossible to write test cases for things like: |
| 5276 | // One thread timeout expires, I go to halt, but the process already stopped |
| 5277 | // on the function call stop breakpoint. Turning on this define will make us not |
| 5278 | // fetch the first event till after the halt. So if you run a quick function, it will have |
| 5279 | // completed, and the completion event will be waiting, when you interrupt for halt. |
| 5280 | // The expression evaluation should still succeed. |
| 5281 | bool miss_first_event = true; |
| 5282 | #endif |
| 5283 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5284 | while (1) |
| 5285 | { |
| 5286 | // We usually want to resume the process if we get to the top of the loop. |
| 5287 | // The only exception is if we get two running events with no intervening |
| 5288 | // 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] | 5289 | if (log) |
| 5290 | log->Printf ("Top of while loop: do_resume: %i handle_running_event: %i before_first_timeout: %i.", |
| 5291 | do_resume, |
| 5292 | handle_running_event, |
| 5293 | before_first_timeout); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5294 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5295 | if (do_resume || handle_running_event) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5296 | { |
| 5297 | // Do the initial resume and wait for the running event before going further. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5298 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5299 | if (do_resume) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5300 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5301 | num_resumes++; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5302 | Error resume_error = PrivateResume (); |
| 5303 | if (!resume_error.Success()) |
| 5304 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5305 | errors.Printf("Error resuming inferior the %d time: \"%s\".\n", |
| 5306 | num_resumes, |
| 5307 | resume_error.AsCString()); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5308 | return_value = eExecutionSetupError; |
| 5309 | break; |
| 5310 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5311 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5312 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5313 | TimeValue resume_timeout = TimeValue::Now(); |
| 5314 | resume_timeout.OffsetWithMicroSeconds(500000); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5315 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5316 | got_event = listener.WaitForEvent(&resume_timeout, event_sp); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5317 | if (!got_event) |
| 5318 | { |
| 5319 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5320 | log->Printf ("Process::RunThreadPlan(): didn't get any event after resume %d, exiting.", |
| 5321 | num_resumes); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5322 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5323 | 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] | 5324 | return_value = eExecutionSetupError; |
| 5325 | break; |
| 5326 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5327 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5328 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5329 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5330 | if (stop_state != eStateRunning) |
| 5331 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5332 | bool restarted = false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5333 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5334 | if (stop_state == eStateStopped) |
| 5335 | { |
| 5336 | restarted = Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()); |
| 5337 | if (log) |
| 5338 | log->Printf("Process::RunThreadPlan(): didn't get running event after " |
| 5339 | "resume %d, got %s instead (restarted: %i, do_resume: %i, handle_running_event: %i).", |
| 5340 | num_resumes, |
| 5341 | StateAsCString(stop_state), |
| 5342 | restarted, |
| 5343 | do_resume, |
| 5344 | handle_running_event); |
| 5345 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5346 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5347 | if (restarted) |
| 5348 | { |
| 5349 | // This is probably an overabundance of caution, I don't think I should ever get a stopped & restarted |
| 5350 | // event here. But if I do, the best thing is to Halt and then get out of here. |
| 5351 | Halt(); |
| 5352 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5353 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5354 | errors.Printf("Didn't get running event after initial resume, got %s instead.", |
| 5355 | StateAsCString(stop_state)); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5356 | return_value = eExecutionSetupError; |
| 5357 | break; |
| 5358 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5359 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5360 | if (log) |
| 5361 | log->PutCString ("Process::RunThreadPlan(): resuming succeeded."); |
| 5362 | // We need to call the function synchronously, so spin waiting for it to return. |
| 5363 | // If we get interrupted while executing, we're going to lose our context, and |
| 5364 | // won't be able to gather the result at this point. |
| 5365 | // We set the timeout AFTER the resume, since the resume takes some time and we |
| 5366 | // don't want to charge that to the timeout. |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5367 | } |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5368 | else |
| 5369 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5370 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5371 | log->PutCString ("Process::RunThreadPlan(): waiting for next event."); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5372 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5373 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5374 | if (before_first_timeout) |
| 5375 | { |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5376 | if (options.GetTryAllThreads()) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5377 | timeout_ptr = &one_thread_timeout; |
| 5378 | else |
| 5379 | { |
| 5380 | if (timeout_usec == 0) |
| 5381 | timeout_ptr = NULL; |
| 5382 | else |
| 5383 | timeout_ptr = &final_timeout; |
| 5384 | } |
| 5385 | } |
| 5386 | else |
| 5387 | { |
| 5388 | if (timeout_usec == 0) |
| 5389 | timeout_ptr = NULL; |
| 5390 | else |
| 5391 | timeout_ptr = &final_timeout; |
| 5392 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5393 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5394 | do_resume = true; |
| 5395 | handle_running_event = true; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5396 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5397 | // Now wait for the process to stop again: |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5398 | event_sp.reset(); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5399 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5400 | if (log) |
Jim Ingham | 20829ac | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 5401 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5402 | if (timeout_ptr) |
| 5403 | { |
Matt Kopec | 676a487 | 2013-02-21 23:55:31 +0000 | [diff] [blame] | 5404 | 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] | 5405 | TimeValue::Now().GetAsMicroSecondsSinceJan1_1970(), |
| 5406 | timeout_ptr->GetAsMicroSecondsSinceJan1_1970()); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5407 | } |
Jim Ingham | 20829ac | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 5408 | else |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5409 | { |
| 5410 | log->Printf ("Process::RunThreadPlan(): about to wait forever."); |
| 5411 | } |
| 5412 | } |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 5413 | |
| 5414 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
| 5415 | // See comment above... |
| 5416 | if (miss_first_event) |
| 5417 | { |
| 5418 | usleep(1000); |
| 5419 | miss_first_event = false; |
| 5420 | got_event = false; |
| 5421 | } |
| 5422 | else |
| 5423 | #endif |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5424 | got_event = listener.WaitForEvent (timeout_ptr, event_sp); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5425 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5426 | if (got_event) |
| 5427 | { |
| 5428 | if (event_sp.get()) |
| 5429 | { |
| 5430 | bool keep_going = false; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5431 | if (event_sp->GetType() == eBroadcastBitInterrupt) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5432 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5433 | Halt(); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5434 | return_value = eExecutionInterrupted; |
| 5435 | errors.Printf ("Execution halted by user interrupt."); |
| 5436 | if (log) |
| 5437 | log->Printf ("Process::RunThreadPlan(): Got interrupted by eBroadcastBitInterrupted, exiting."); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5438 | break; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5439 | } |
| 5440 | else |
| 5441 | { |
| 5442 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5443 | if (log) |
| 5444 | log->Printf("Process::RunThreadPlan(): in while loop, got event: %s.", StateAsCString(stop_state)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5445 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5446 | switch (stop_state) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5447 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5448 | case lldb::eStateStopped: |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5449 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5450 | // We stopped, figure out what we are going to do now. |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5451 | ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id); |
| 5452 | if (!thread_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5453 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5454 | // Ooh, our thread has vanished. Unlikely that this was successful execution... |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5455 | if (log) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5456 | log->Printf ("Process::RunThreadPlan(): execution completed but our thread (index-id=%u) has vanished.", thread_idx_id); |
| 5457 | return_value = eExecutionInterrupted; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5458 | } |
| 5459 | else |
| 5460 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5461 | // If we were restarted, we just need to go back up to fetch another event. |
| 5462 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5463 | { |
| 5464 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5465 | { |
| 5466 | log->Printf ("Process::RunThreadPlan(): Got a stop and restart, so we'll continue waiting."); |
| 5467 | } |
| 5468 | keep_going = true; |
| 5469 | do_resume = false; |
| 5470 | handle_running_event = true; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5471 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5472 | } |
| 5473 | else |
| 5474 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5475 | StopInfoSP stop_info_sp (thread_sp->GetStopInfo ()); |
| 5476 | StopReason stop_reason = eStopReasonInvalid; |
| 5477 | if (stop_info_sp) |
| 5478 | stop_reason = stop_info_sp->GetStopReason(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5479 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5480 | // FIXME: We only check if the stop reason is plan complete, should we make sure that |
| 5481 | // it is OUR plan that is complete? |
| 5482 | if (stop_reason == eStopReasonPlanComplete) |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5483 | { |
| 5484 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5485 | log->PutCString ("Process::RunThreadPlan(): execution completed successfully."); |
| 5486 | // Now mark this plan as private so it doesn't get reported as the stop reason |
| 5487 | // after this point. |
| 5488 | if (thread_plan_sp) |
| 5489 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 5490 | return_value = eExecutionCompleted; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5491 | } |
| 5492 | else |
| 5493 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5494 | // 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] | 5495 | if (stop_reason == eStopReasonBreakpoint) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5496 | { |
| 5497 | if (log) |
| 5498 | log->Printf ("Process::RunThreadPlan() stopped for breakpoint: %s.", stop_info_sp->GetDescription()); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5499 | return_value = eExecutionHitBreakpoint; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5500 | if (!options.DoesIgnoreBreakpoints()) |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 5501 | { |
| 5502 | event_to_broadcast_sp = event_sp; |
| 5503 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5504 | } |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5505 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5506 | { |
| 5507 | if (log) |
| 5508 | log->PutCString ("Process::RunThreadPlan(): thread plan didn't successfully complete."); |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5509 | if (!options.DoesUnwindOnError()) |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 5510 | event_to_broadcast_sp = event_sp; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5511 | return_value = eExecutionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5512 | } |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5513 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5514 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5515 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5516 | } |
| 5517 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5518 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5519 | case lldb::eStateRunning: |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5520 | // This shouldn't really happen, but sometimes we do get two running events without an |
| 5521 | // 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] | 5522 | do_resume = false; |
| 5523 | keep_going = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5524 | handle_running_event = false; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5525 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5526 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5527 | default: |
| 5528 | if (log) |
| 5529 | log->Printf("Process::RunThreadPlan(): execution stopped with unexpected state: %s.", StateAsCString(stop_state)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5530 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5531 | if (stop_state == eStateExited) |
| 5532 | event_to_broadcast_sp = event_sp; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5533 | |
Sean Callanan | bf154da | 2012-08-08 17:35:10 +0000 | [diff] [blame] | 5534 | errors.Printf ("Execution stopped with unexpected state.\n"); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5535 | return_value = eExecutionInterrupted; |
| 5536 | break; |
| 5537 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5538 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5539 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5540 | if (keep_going) |
| 5541 | continue; |
| 5542 | else |
| 5543 | break; |
| 5544 | } |
| 5545 | else |
| 5546 | { |
| 5547 | if (log) |
| 5548 | log->PutCString ("Process::RunThreadPlan(): got_event was true, but the event pointer was null. How odd..."); |
| 5549 | return_value = eExecutionInterrupted; |
| 5550 | break; |
| 5551 | } |
| 5552 | } |
| 5553 | else |
| 5554 | { |
| 5555 | // If we didn't get an event that means we've timed out... |
| 5556 | // We will interrupt the process here. Depending on what we were asked to do we will |
| 5557 | // either exit, or try with all threads running for the same timeout. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5558 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5559 | if (log) { |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5560 | if (options.GetTryAllThreads()) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5561 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5562 | if (before_first_timeout) |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame^] | 5563 | { |
| 5564 | if (timeout_usec != 0) |
| 5565 | { |
| 5566 | uint64_t remaining_time = final_timeout - TimeValue::Now(); |
| 5567 | log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, " |
| 5568 | "running for %" PRIu64 " usec with all threads enabled.", |
| 5569 | remaining_time); |
| 5570 | } |
| 5571 | else |
| 5572 | { |
| 5573 | log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, " |
| 5574 | "running for ever with all threads enabled."); |
| 5575 | } |
| 5576 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5577 | else |
| 5578 | log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled " |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5579 | "and timeout: %u timed out, abandoning execution.", |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5580 | timeout_usec); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5581 | } |
| 5582 | else |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5583 | log->Printf ("Process::RunThreadPlan(): Running function with timeout: %u timed out, " |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5584 | "abandoning execution.", |
| 5585 | timeout_usec); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5586 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5587 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5588 | // It is possible that between the time we issued the Halt, and we get around to calling Halt the target |
| 5589 | // could have stopped. That's fine, Halt will figure that out and send the appropriate Stopped event. |
| 5590 | // BUT it is also possible that we stopped & restarted (e.g. hit a signal with "stop" set to false.) In |
| 5591 | // that case, we'll get the stopped & restarted event, and we should go back to waiting for the Halt's |
| 5592 | // stopped event. That's what this while loop does. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5593 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5594 | bool back_to_top = true; |
| 5595 | uint32_t try_halt_again = 0; |
| 5596 | bool do_halt = true; |
| 5597 | const uint32_t num_retries = 5; |
| 5598 | while (try_halt_again < num_retries) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5599 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5600 | Error halt_error; |
| 5601 | if (do_halt) |
| 5602 | { |
| 5603 | if (log) |
| 5604 | log->Printf ("Process::RunThreadPlan(): Running Halt."); |
| 5605 | halt_error = Halt(); |
| 5606 | } |
| 5607 | if (halt_error.Success()) |
| 5608 | { |
| 5609 | if (log) |
| 5610 | log->PutCString ("Process::RunThreadPlan(): Halt succeeded."); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5611 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5612 | real_timeout = TimeValue::Now(); |
| 5613 | real_timeout.OffsetWithMicroSeconds(500000); |
| 5614 | |
| 5615 | got_event = listener.WaitForEvent(&real_timeout, event_sp); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5616 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5617 | if (got_event) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5618 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5619 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5620 | if (log) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5621 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5622 | log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state)); |
| 5623 | if (stop_state == lldb::eStateStopped |
| 5624 | && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get())) |
| 5625 | log->PutCString (" Event was the Halt interruption event."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5626 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5627 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5628 | if (stop_state == lldb::eStateStopped) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5629 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5630 | // Between the time we initiated the Halt and the time we delivered it, the process could have |
| 5631 | // already finished its job. Check that here: |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5632 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5633 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
| 5634 | { |
| 5635 | if (log) |
| 5636 | log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. " |
| 5637 | "Exiting wait loop."); |
| 5638 | return_value = eExecutionCompleted; |
| 5639 | back_to_top = false; |
| 5640 | break; |
| 5641 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5642 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5643 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 5644 | { |
| 5645 | if (log) |
| 5646 | log->PutCString ("Process::RunThreadPlan(): Went to halt but got a restarted event, there must be an un-restarted stopped event so try again... " |
| 5647 | "Exiting wait loop."); |
| 5648 | try_halt_again++; |
| 5649 | do_halt = false; |
| 5650 | continue; |
| 5651 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5652 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5653 | if (!options.GetTryAllThreads()) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5654 | { |
| 5655 | if (log) |
| 5656 | log->PutCString ("Process::RunThreadPlan(): try_all_threads was false, we stopped so now we're quitting."); |
| 5657 | return_value = eExecutionInterrupted; |
| 5658 | back_to_top = false; |
| 5659 | break; |
| 5660 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5661 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5662 | if (before_first_timeout) |
| 5663 | { |
| 5664 | // Set all the other threads to run, and return to the top of the loop, which will continue; |
| 5665 | before_first_timeout = false; |
| 5666 | thread_plan_sp->SetStopOthers (false); |
| 5667 | if (log) |
| 5668 | log->PutCString ("Process::RunThreadPlan(): about to resume."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5669 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5670 | back_to_top = true; |
| 5671 | break; |
| 5672 | } |
| 5673 | else |
| 5674 | { |
| 5675 | // Running all threads failed, so return Interrupted. |
| 5676 | if (log) |
| 5677 | log->PutCString("Process::RunThreadPlan(): running all threads timed out."); |
| 5678 | return_value = eExecutionInterrupted; |
| 5679 | back_to_top = false; |
| 5680 | break; |
| 5681 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5682 | } |
| 5683 | } |
| 5684 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5685 | { if (log) |
| 5686 | log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. " |
| 5687 | "I'm getting out of here passing Interrupted."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5688 | return_value = eExecutionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5689 | back_to_top = false; |
| 5690 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5691 | } |
| 5692 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5693 | else |
| 5694 | { |
| 5695 | try_halt_again++; |
| 5696 | continue; |
| 5697 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5698 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5699 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5700 | if (!back_to_top || try_halt_again > num_retries) |
| 5701 | break; |
| 5702 | else |
| 5703 | continue; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5704 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5705 | } // END WAIT LOOP |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5706 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5707 | // If we had to start up a temporary private state thread to run this thread plan, shut it down now. |
| 5708 | if (IS_VALID_LLDB_HOST_THREAD(backup_private_state_thread)) |
| 5709 | { |
| 5710 | StopPrivateStateThread(); |
| 5711 | Error error; |
| 5712 | m_private_state_thread = backup_private_state_thread; |
Sean Callanan | 9a02851 | 2012-08-09 00:50:26 +0000 | [diff] [blame] | 5713 | if (stopper_base_plan_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5714 | { |
| 5715 | thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp); |
| 5716 | } |
| 5717 | m_public_state.SetValueNoLock(old_state); |
| 5718 | |
| 5719 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5720 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5721 | // Restore the thread state if we are going to discard the plan execution. There are three cases where this |
| 5722 | // could happen: |
| 5723 | // 1) The execution successfully completed |
| 5724 | // 2) We hit a breakpoint, and ignore_breakpoints was true |
| 5725 | // 3) We got some other error, and discard_on_error was true |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5726 | bool should_unwind = (return_value == eExecutionInterrupted && options.DoesUnwindOnError()) |
| 5727 | || (return_value == eExecutionHitBreakpoint && options.DoesIgnoreBreakpoints()); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5728 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5729 | if (return_value == eExecutionCompleted |
| 5730 | || should_unwind) |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 5731 | { |
| 5732 | thread_plan_sp->RestoreThreadState(); |
| 5733 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5734 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5735 | // Now do some processing on the results of the run: |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5736 | if (return_value == eExecutionInterrupted || return_value == eExecutionHitBreakpoint) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5737 | { |
| 5738 | if (log) |
| 5739 | { |
| 5740 | StreamString s; |
| 5741 | if (event_sp) |
| 5742 | event_sp->Dump (&s); |
| 5743 | else |
| 5744 | { |
| 5745 | log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL."); |
| 5746 | } |
| 5747 | |
| 5748 | StreamString ts; |
| 5749 | |
| 5750 | const char *event_explanation = NULL; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5751 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5752 | do |
| 5753 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5754 | if (!event_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5755 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5756 | event_explanation = "<no event>"; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5757 | break; |
| 5758 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5759 | else if (event_sp->GetType() == eBroadcastBitInterrupt) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5760 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5761 | event_explanation = "<user interrupt>"; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5762 | break; |
| 5763 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5764 | else |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5765 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5766 | const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get()); |
| 5767 | |
| 5768 | if (!event_data) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5769 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5770 | event_explanation = "<no event data>"; |
| 5771 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5772 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5773 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5774 | Process *process = event_data->GetProcessSP().get(); |
| 5775 | |
| 5776 | if (!process) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5777 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5778 | event_explanation = "<no process>"; |
| 5779 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5780 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5781 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5782 | ThreadList &thread_list = process->GetThreadList(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5783 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5784 | uint32_t num_threads = thread_list.GetSize(); |
| 5785 | uint32_t thread_index; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5786 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5787 | ts.Printf("<%u threads> ", num_threads); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5788 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5789 | for (thread_index = 0; |
| 5790 | thread_index < num_threads; |
| 5791 | ++thread_index) |
| 5792 | { |
| 5793 | Thread *thread = thread_list.GetThreadAtIndex(thread_index).get(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5794 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5795 | if (!thread) |
| 5796 | { |
| 5797 | ts.Printf("<?> "); |
| 5798 | continue; |
| 5799 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5800 | |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5801 | ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5802 | RegisterContext *register_context = thread->GetRegisterContext().get(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5803 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5804 | if (register_context) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5805 | ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5806 | else |
| 5807 | ts.Printf("[ip unknown] "); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5808 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5809 | lldb::StopInfoSP stop_info_sp = thread->GetStopInfo(); |
| 5810 | if (stop_info_sp) |
| 5811 | { |
| 5812 | const char *stop_desc = stop_info_sp->GetDescription(); |
| 5813 | if (stop_desc) |
| 5814 | ts.PutCString (stop_desc); |
| 5815 | } |
| 5816 | ts.Printf(">"); |
| 5817 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5818 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5819 | event_explanation = ts.GetData(); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5820 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5821 | } while (0); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5822 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5823 | if (event_explanation) |
| 5824 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5825 | else |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5826 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData()); |
| 5827 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5828 | |
Jim Ingham | e4483cf | 2013-09-27 01:13:01 +0000 | [diff] [blame] | 5829 | if (should_unwind) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5830 | { |
| 5831 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5832 | log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - discarding thread plans up to %p.", |
| 5833 | static_cast<void*>(thread_plan_sp.get())); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5834 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
| 5835 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 5836 | } |
| 5837 | else |
| 5838 | { |
| 5839 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5840 | log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - for plan: %p not discarding.", |
| 5841 | static_cast<void*>(thread_plan_sp.get())); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5842 | } |
| 5843 | } |
| 5844 | else if (return_value == eExecutionSetupError) |
| 5845 | { |
| 5846 | if (log) |
| 5847 | log->PutCString("Process::RunThreadPlan(): execution set up error."); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5848 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5849 | if (options.DoesUnwindOnError()) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5850 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5851 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 5852 | thread_plan_sp->SetPrivate (orig_plan_private); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5853 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5854 | } |
| 5855 | else |
| 5856 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5857 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5858 | { |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5859 | if (log) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5860 | log->PutCString("Process::RunThreadPlan(): thread plan is done"); |
| 5861 | return_value = eExecutionCompleted; |
| 5862 | } |
| 5863 | else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get())) |
| 5864 | { |
| 5865 | if (log) |
| 5866 | log->PutCString("Process::RunThreadPlan(): thread plan was discarded"); |
| 5867 | return_value = eExecutionDiscarded; |
| 5868 | } |
| 5869 | else |
| 5870 | { |
| 5871 | if (log) |
| 5872 | log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course"); |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5873 | if (options.DoesUnwindOnError() && thread_plan_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5874 | { |
| 5875 | if (log) |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5876 | 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] | 5877 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
| 5878 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 5879 | } |
| 5880 | } |
| 5881 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5882 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5883 | // Thread we ran the function in may have gone away because we ran the target |
| 5884 | // Check that it's still there, and if it is put it back in the context. Also restore the |
| 5885 | // frame in the context if it is still present. |
| 5886 | thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get(); |
| 5887 | if (thread) |
| 5888 | { |
| 5889 | exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id)); |
| 5890 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5891 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5892 | // Also restore the current process'es selected frame & thread, since this function calling may |
| 5893 | // be done behind the user's back. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5894 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5895 | if (selected_tid != LLDB_INVALID_THREAD_ID) |
| 5896 | { |
| 5897 | if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid()) |
| 5898 | { |
| 5899 | // We were able to restore the selected thread, now restore the frame: |
Daniel Malea | a012d3a | 2013-07-31 20:21:20 +0000 | [diff] [blame] | 5900 | Mutex::Locker lock(GetThreadList().GetMutex()); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 5901 | StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5902 | if (old_frame_sp) |
| 5903 | GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get()); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5904 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5905 | } |
| 5906 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5907 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5908 | // If the process exited during the run of the thread plan, notify everyone. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5909 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5910 | if (event_to_broadcast_sp) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5911 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5912 | if (log) |
| 5913 | log->PutCString("Process::RunThreadPlan(): rebroadcasting event."); |
| 5914 | BroadcastEvent(event_to_broadcast_sp); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5915 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5916 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5917 | return return_value; |
| 5918 | } |
| 5919 | |
| 5920 | const char * |
| 5921 | Process::ExecutionResultAsCString (ExecutionResults result) |
| 5922 | { |
| 5923 | const char *result_name; |
| 5924 | |
| 5925 | switch (result) |
| 5926 | { |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5927 | case eExecutionCompleted: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5928 | result_name = "eExecutionCompleted"; |
| 5929 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5930 | case eExecutionDiscarded: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5931 | result_name = "eExecutionDiscarded"; |
| 5932 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5933 | case eExecutionInterrupted: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5934 | result_name = "eExecutionInterrupted"; |
| 5935 | break; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5936 | case eExecutionHitBreakpoint: |
| 5937 | result_name = "eExecutionHitBreakpoint"; |
| 5938 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5939 | case eExecutionSetupError: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5940 | result_name = "eExecutionSetupError"; |
| 5941 | break; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 5942 | case eExecutionTimedOut: |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5943 | result_name = "eExecutionTimedOut"; |
| 5944 | break; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5945 | case eExecutionStoppedForDebug: |
| 5946 | result_name = "eExecutionStoppedForDebug"; |
| 5947 | break; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5948 | } |
| 5949 | return result_name; |
| 5950 | } |
| 5951 | |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5952 | void |
| 5953 | Process::GetStatus (Stream &strm) |
| 5954 | { |
| 5955 | const StateType state = GetState(); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 5956 | if (StateIsStoppedState(state, false)) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5957 | { |
| 5958 | if (state == eStateExited) |
| 5959 | { |
| 5960 | int exit_status = GetExitStatus(); |
| 5961 | const char *exit_description = GetExitDescription(); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5962 | 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] | 5963 | GetID(), |
| 5964 | exit_status, |
| 5965 | exit_status, |
| 5966 | exit_description ? exit_description : ""); |
| 5967 | } |
| 5968 | else |
| 5969 | { |
| 5970 | if (state == eStateConnected) |
| 5971 | strm.Printf ("Connected to remote target.\n"); |
| 5972 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5973 | strm.Printf ("Process %" PRIu64 " %s\n", GetID(), StateAsCString (state)); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5974 | } |
| 5975 | } |
| 5976 | else |
| 5977 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5978 | strm.Printf ("Process %" PRIu64 " is running.\n", GetID()); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5979 | } |
| 5980 | } |
| 5981 | |
| 5982 | size_t |
| 5983 | Process::GetThreadStatus (Stream &strm, |
| 5984 | bool only_threads_with_stop_reason, |
| 5985 | uint32_t start_frame, |
| 5986 | uint32_t num_frames, |
| 5987 | uint32_t num_frames_with_source) |
| 5988 | { |
| 5989 | size_t num_thread_infos_dumped = 0; |
| 5990 | |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 5991 | // You can't hold the thread list lock while calling Thread::GetStatus. That very well might run code (e.g. if we need it |
| 5992 | // to get return values or arguments.) For that to work the process has to be able to acquire it. So instead copy the thread |
| 5993 | // ID's, and look them up one by one: |
| 5994 | |
| 5995 | uint32_t num_threads; |
| 5996 | std::vector<uint32_t> thread_index_array; |
| 5997 | //Scope for thread list locker; |
| 5998 | { |
| 5999 | Mutex::Locker locker (GetThreadList().GetMutex()); |
| 6000 | ThreadList &curr_thread_list = GetThreadList(); |
| 6001 | num_threads = curr_thread_list.GetSize(); |
| 6002 | uint32_t idx; |
| 6003 | thread_index_array.resize(num_threads); |
| 6004 | for (idx = 0; idx < num_threads; ++idx) |
| 6005 | thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetID(); |
| 6006 | } |
| 6007 | |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6008 | for (uint32_t i = 0; i < num_threads; i++) |
| 6009 | { |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6010 | ThreadSP thread_sp(GetThreadList().FindThreadByID(thread_index_array[i])); |
| 6011 | if (thread_sp) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6012 | { |
| 6013 | if (only_threads_with_stop_reason) |
| 6014 | { |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6015 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo(); |
Jim Ingham | 5d88a06 | 2012-10-16 00:09:33 +0000 | [diff] [blame] | 6016 | if (stop_info_sp.get() == NULL || !stop_info_sp->IsValid()) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6017 | continue; |
| 6018 | } |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6019 | thread_sp->GetStatus (strm, |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6020 | start_frame, |
| 6021 | num_frames, |
| 6022 | num_frames_with_source); |
| 6023 | ++num_thread_infos_dumped; |
| 6024 | } |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6025 | else |
| 6026 | { |
| 6027 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 6028 | if (log) |
| 6029 | log->Printf("Process::GetThreadStatus - thread 0x" PRIu64 " vanished while running Thread::GetStatus."); |
| 6030 | |
| 6031 | } |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6032 | } |
| 6033 | return num_thread_infos_dumped; |
| 6034 | } |
| 6035 | |
Greg Clayton | a9f40ad | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 6036 | void |
| 6037 | Process::AddInvalidMemoryRegion (const LoadRange ®ion) |
| 6038 | { |
| 6039 | m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 6040 | } |
| 6041 | |
| 6042 | bool |
| 6043 | Process::RemoveInvalidMemoryRange (const LoadRange ®ion) |
| 6044 | { |
| 6045 | return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 6046 | } |
| 6047 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 6048 | void |
| 6049 | Process::AddPreResumeAction (PreResumeActionCallback callback, void *baton) |
| 6050 | { |
| 6051 | m_pre_resume_actions.push_back(PreResumeCallbackAndBaton (callback, baton)); |
| 6052 | } |
| 6053 | |
| 6054 | bool |
| 6055 | Process::RunPreResumeActions () |
| 6056 | { |
| 6057 | bool result = true; |
| 6058 | while (!m_pre_resume_actions.empty()) |
| 6059 | { |
| 6060 | struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back(); |
| 6061 | m_pre_resume_actions.pop_back(); |
| 6062 | bool this_result = action.callback (action.baton); |
| 6063 | if (result == true) result = this_result; |
| 6064 | } |
| 6065 | return result; |
| 6066 | } |
| 6067 | |
| 6068 | void |
| 6069 | Process::ClearPreResumeActions () |
| 6070 | { |
| 6071 | m_pre_resume_actions.clear(); |
| 6072 | } |
Greg Clayton | a9f40ad | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 6073 | |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 6074 | void |
| 6075 | Process::Flush () |
| 6076 | { |
| 6077 | m_thread_list.Flush(); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 6078 | m_extended_thread_list.Flush(); |
| 6079 | m_extended_thread_stop_id = 0; |
| 6080 | m_queue_list.Clear(); |
| 6081 | m_queue_list_stop_id = 0; |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 6082 | } |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6083 | |
| 6084 | void |
| 6085 | Process::DidExec () |
| 6086 | { |
| 6087 | Target &target = GetTarget(); |
| 6088 | target.CleanupProcess (); |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame] | 6089 | target.ClearModules(false); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6090 | m_dynamic_checkers_ap.reset(); |
| 6091 | m_abi_sp.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 6092 | m_system_runtime_ap.reset(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6093 | m_os_ap.reset(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 6094 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 6095 | m_jit_loaders_ap.reset(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6096 | m_image_tokens.clear(); |
| 6097 | m_allocated_memory_cache.Clear(); |
| 6098 | m_language_runtimes.clear(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 6099 | m_thread_list.DiscardThreadPlans(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 6100 | m_memory_cache.Clear(true); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6101 | DoDidExec(); |
| 6102 | CompleteAttach (); |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 6103 | // Flush the process (threads and all stack frames) after running CompleteAttach() |
| 6104 | // in case the dynamic loader loaded things in new locations. |
| 6105 | Flush(); |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame] | 6106 | |
| 6107 | // After we figure out what was loaded/unloaded in CompleteAttach, |
| 6108 | // we need to let the target know so it can do any cleanup it needs to. |
| 6109 | target.DidExec(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6110 | } |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 6111 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 6112 | addr_t |
| 6113 | Process::ResolveIndirectFunction(const Address *address, Error &error) |
| 6114 | { |
| 6115 | if (address == nullptr) |
| 6116 | { |
Jean-Daniel Dupas | ef37711f | 2014-02-08 20:22:05 +0000 | [diff] [blame] | 6117 | error.SetErrorString("Invalid address argument"); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 6118 | return LLDB_INVALID_ADDRESS; |
| 6119 | } |
| 6120 | |
| 6121 | addr_t function_addr = LLDB_INVALID_ADDRESS; |
| 6122 | |
| 6123 | addr_t addr = address->GetLoadAddress(&GetTarget()); |
| 6124 | std::map<addr_t,addr_t>::const_iterator iter = m_resolved_indirect_addresses.find(addr); |
| 6125 | if (iter != m_resolved_indirect_addresses.end()) |
| 6126 | { |
| 6127 | function_addr = (*iter).second; |
| 6128 | } |
| 6129 | else |
| 6130 | { |
| 6131 | if (!InferiorCall(this, address, function_addr)) |
| 6132 | { |
| 6133 | Symbol *symbol = address->CalculateSymbolContextSymbol(); |
| 6134 | error.SetErrorStringWithFormat ("Unable to call resolver for indirect function %s", |
| 6135 | symbol ? symbol->GetName().AsCString() : "<UNKNOWN>"); |
| 6136 | function_addr = LLDB_INVALID_ADDRESS; |
| 6137 | } |
| 6138 | else |
| 6139 | { |
| 6140 | m_resolved_indirect_addresses.insert(std::pair<addr_t, addr_t>(addr, function_addr)); |
| 6141 | } |
| 6142 | } |
| 6143 | return function_addr; |
| 6144 | } |
| 6145 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 6146 | void |
| 6147 | Process::ModulesDidLoad (ModuleList &module_list) |
| 6148 | { |
| 6149 | SystemRuntime *sys_runtime = GetSystemRuntime(); |
| 6150 | if (sys_runtime) |
| 6151 | { |
| 6152 | sys_runtime->ModulesDidLoad (module_list); |
| 6153 | } |
| 6154 | |
| 6155 | GetJITLoaders().ModulesDidLoad (module_list); |
| 6156 | } |