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