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