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