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