Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Process.cpp ---------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/Target/Process.h" |
| 13 | |
| 14 | #include "lldb/lldb-private-log.h" |
| 15 | |
| 16 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
| 17 | #include "lldb/Breakpoint/BreakpointLocation.h" |
| 18 | #include "lldb/Core/Event.h" |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 19 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Debugger.h" |
| 21 | #include "lldb/Core/Log.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 22 | #include "lldb/Core/Module.h" |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/Symbol.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | #include "lldb/Core/PluginManager.h" |
| 25 | #include "lldb/Core/State.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 26 | #include "lldb/Core/StreamFile.h" |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 27 | #include "lldb/Expression/ClangUserExpression.h" |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 28 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Host/Host.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 30 | #include "lldb/Host/HostInfo.h" |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 31 | #include "lldb/Host/Pipe.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 32 | #include "lldb/Host/Terminal.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 33 | #include "lldb/Host/ThreadLauncher.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | #include "lldb/Target/ABI.h" |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 35 | #include "lldb/Target/DynamicLoader.h" |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 36 | #include "lldb/Target/JITLoader.h" |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 37 | #include "lldb/Target/MemoryHistory.h" |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 38 | #include "lldb/Target/OperatingSystem.h" |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 39 | #include "lldb/Target/LanguageRuntime.h" |
| 40 | #include "lldb/Target/CPPLanguageRuntime.h" |
| 41 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 42 | #include "lldb/Target/Platform.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 44 | #include "lldb/Target/StopInfo.h" |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 45 | #include "lldb/Target/SystemRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | #include "lldb/Target/Target.h" |
| 47 | #include "lldb/Target/TargetList.h" |
| 48 | #include "lldb/Target/Thread.h" |
| 49 | #include "lldb/Target/ThreadPlan.h" |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 50 | #include "lldb/Target/ThreadPlanBase.h" |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 51 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | |
| 53 | using namespace lldb; |
| 54 | using namespace lldb_private; |
| 55 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 56 | |
| 57 | // Comment out line below to disable memory caching, overriding the process setting |
| 58 | // target.process.disable-memory-cache |
| 59 | #define ENABLE_MEMORY_CACHING |
| 60 | |
| 61 | #ifdef ENABLE_MEMORY_CACHING |
| 62 | #define DISABLE_MEM_CACHE_DEFAULT false |
| 63 | #else |
| 64 | #define DISABLE_MEM_CACHE_DEFAULT true |
| 65 | #endif |
| 66 | |
| 67 | class ProcessOptionValueProperties : public OptionValueProperties |
| 68 | { |
| 69 | public: |
| 70 | ProcessOptionValueProperties (const ConstString &name) : |
| 71 | OptionValueProperties (name) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | // This constructor is used when creating ProcessOptionValueProperties when it |
| 76 | // is part of a new lldb_private::Process instance. It will copy all current |
| 77 | // global property values as needed |
| 78 | ProcessOptionValueProperties (ProcessProperties *global_properties) : |
| 79 | OptionValueProperties(*global_properties->GetValueProperties()) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | virtual const Property * |
| 84 | GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const |
| 85 | { |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 86 | // When getting the value for a key from the process options, we will always |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 87 | // try and grab the setting from the current process if there is one. Else we just |
| 88 | // use the one from this instance. |
| 89 | if (exe_ctx) |
| 90 | { |
| 91 | Process *process = exe_ctx->GetProcessPtr(); |
| 92 | if (process) |
| 93 | { |
| 94 | ProcessOptionValueProperties *instance_properties = static_cast<ProcessOptionValueProperties *>(process->GetValueProperties().get()); |
| 95 | if (this != instance_properties) |
| 96 | return instance_properties->ProtectedGetPropertyAtIndex (idx); |
| 97 | } |
| 98 | } |
| 99 | return ProtectedGetPropertyAtIndex (idx); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | static PropertyDefinition |
| 104 | g_properties[] = |
| 105 | { |
| 106 | { "disable-memory-cache" , OptionValue::eTypeBoolean, false, DISABLE_MEM_CACHE_DEFAULT, NULL, NULL, "Disable reading and caching of memory in fixed-size units." }, |
Jim Ingham | 8c3f276 | 2012-11-29 00:41:12 +0000 | [diff] [blame] | 107 | { "extra-startup-command", OptionValue::eTypeArray , false, OptionValue::eTypeString, NULL, NULL, "A list containing extra commands understood by the particular process plugin used. " |
| 108 | "For instance, to turn on debugserver logging set this to \"QSetLogging:bitmask=LOG_DEFAULT;\"" }, |
Jim Ingham | afc1b12 | 2013-01-31 19:48:57 +0000 | [diff] [blame] | 109 | { "ignore-breakpoints-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, breakpoints will be ignored during expression evaluation." }, |
| 110 | { "unwind-on-error-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, errors in expression evaluation will unwind the stack back to the state before the call." }, |
Greg Clayton | e1e835c | 2012-11-29 18:48:47 +0000 | [diff] [blame] | 111 | { "python-os-plugin-path", OptionValue::eTypeFileSpec, false, true, NULL, NULL, "A path to a python OS plug-in module file that contains a OperatingSystemPlugIn class." }, |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 112 | { "stop-on-sharedlibrary-events" , OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true, stop when a shared library is loaded or unloaded." }, |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 113 | { "detach-keeps-stopped" , OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true, detach will attempt to keep the process stopped." }, |
Jason Molenda | f0340c9 | 2014-09-03 22:30:54 +0000 | [diff] [blame] | 114 | { "memory-cache-line-size" , OptionValue::eTypeUInt64, false, 512, NULL, NULL, "The memory cache line size" }, |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 115 | { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } |
| 116 | }; |
| 117 | |
| 118 | enum { |
| 119 | ePropertyDisableMemCache, |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 120 | ePropertyExtraStartCommand, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 121 | ePropertyIgnoreBreakpointsInExpressions, |
| 122 | ePropertyUnwindOnErrorInExpressions, |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 123 | ePropertyPythonOSPluginPath, |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 124 | ePropertyStopOnSharedLibraryEvents, |
Jason Molenda | f0340c9 | 2014-09-03 22:30:54 +0000 | [diff] [blame] | 125 | ePropertyDetachKeepsStopped, |
| 126 | ePropertyMemCacheLineSize |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | ProcessProperties::ProcessProperties (bool is_global) : |
| 130 | Properties () |
| 131 | { |
| 132 | if (is_global) |
| 133 | { |
| 134 | m_collection_sp.reset (new ProcessOptionValueProperties(ConstString("process"))); |
| 135 | m_collection_sp->Initialize(g_properties); |
| 136 | m_collection_sp->AppendProperty(ConstString("thread"), |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 137 | ConstString("Settings specific to threads."), |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 138 | true, |
| 139 | Thread::GetGlobalProperties()->GetValueProperties()); |
| 140 | } |
| 141 | else |
| 142 | m_collection_sp.reset (new ProcessOptionValueProperties(Process::GetGlobalProperties().get())); |
| 143 | } |
| 144 | |
| 145 | ProcessProperties::~ProcessProperties() |
| 146 | { |
| 147 | } |
| 148 | |
| 149 | bool |
| 150 | ProcessProperties::GetDisableMemoryCache() const |
| 151 | { |
| 152 | const uint32_t idx = ePropertyDisableMemCache; |
| 153 | return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); |
| 154 | } |
| 155 | |
Jason Molenda | f0340c9 | 2014-09-03 22:30:54 +0000 | [diff] [blame] | 156 | uint64_t |
| 157 | ProcessProperties::GetMemoryCacheLineSize() const |
| 158 | { |
| 159 | const uint32_t idx = ePropertyMemCacheLineSize; |
| 160 | return m_collection_sp->GetPropertyAtIndexAsUInt64 (NULL, idx, g_properties[idx].default_uint_value); |
| 161 | } |
| 162 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 163 | Args |
| 164 | ProcessProperties::GetExtraStartupCommands () const |
| 165 | { |
| 166 | Args args; |
| 167 | const uint32_t idx = ePropertyExtraStartCommand; |
| 168 | m_collection_sp->GetPropertyAtIndexAsArgs(NULL, idx, args); |
| 169 | return args; |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | ProcessProperties::SetExtraStartupCommands (const Args &args) |
| 174 | { |
| 175 | const uint32_t idx = ePropertyExtraStartCommand; |
| 176 | m_collection_sp->SetPropertyAtIndexFromArgs(NULL, idx, args); |
| 177 | } |
| 178 | |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 179 | FileSpec |
| 180 | ProcessProperties::GetPythonOSPluginPath () const |
| 181 | { |
| 182 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 183 | return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx); |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | ProcessProperties::SetPythonOSPluginPath (const FileSpec &file) |
| 188 | { |
| 189 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 190 | m_collection_sp->SetPropertyAtIndexAsFileSpec(NULL, idx, file); |
| 191 | } |
| 192 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 193 | |
| 194 | bool |
| 195 | ProcessProperties::GetIgnoreBreakpointsInExpressions () const |
| 196 | { |
| 197 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 198 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | ProcessProperties::SetIgnoreBreakpointsInExpressions (bool ignore) |
| 203 | { |
| 204 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 205 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore); |
| 206 | } |
| 207 | |
| 208 | bool |
| 209 | ProcessProperties::GetUnwindOnErrorInExpressions () const |
| 210 | { |
| 211 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 212 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | ProcessProperties::SetUnwindOnErrorInExpressions (bool ignore) |
| 217 | { |
| 218 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 219 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore); |
| 220 | } |
| 221 | |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 222 | bool |
| 223 | ProcessProperties::GetStopOnSharedLibraryEvents () const |
| 224 | { |
| 225 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 226 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | ProcessProperties::SetStopOnSharedLibraryEvents (bool stop) |
| 231 | { |
| 232 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 233 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop); |
| 234 | } |
| 235 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 236 | bool |
| 237 | ProcessProperties::GetDetachKeepsStopped () const |
| 238 | { |
| 239 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 240 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 241 | } |
| 242 | |
| 243 | void |
| 244 | ProcessProperties::SetDetachKeepsStopped (bool stop) |
| 245 | { |
| 246 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 247 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop); |
| 248 | } |
| 249 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 250 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 251 | ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 252 | { |
| 253 | const char *cstr; |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 254 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 255 | s.Printf (" pid = %" PRIu64 "\n", m_pid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 256 | |
| 257 | if (m_parent_pid != LLDB_INVALID_PROCESS_ID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 258 | s.Printf (" parent = %" PRIu64 "\n", m_parent_pid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 259 | |
| 260 | if (m_executable) |
| 261 | { |
| 262 | s.Printf (" name = %s\n", m_executable.GetFilename().GetCString()); |
| 263 | s.PutCString (" file = "); |
| 264 | m_executable.Dump(&s); |
| 265 | s.EOL(); |
| 266 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 267 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 268 | if (argc > 0) |
| 269 | { |
| 270 | for (uint32_t i=0; i<argc; i++) |
| 271 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 272 | const char *arg = m_arguments.GetArgumentAtIndex(i); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 273 | if (i < 10) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 274 | s.Printf (" arg[%u] = %s\n", i, arg); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 275 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 276 | s.Printf ("arg[%u] = %s\n", i, arg); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 279 | |
| 280 | const uint32_t envc = m_environment.GetArgumentCount(); |
| 281 | if (envc > 0) |
| 282 | { |
| 283 | for (uint32_t i=0; i<envc; i++) |
| 284 | { |
| 285 | const char *env = m_environment.GetArgumentAtIndex(i); |
| 286 | if (i < 10) |
| 287 | s.Printf (" env[%u] = %s\n", i, env); |
| 288 | else |
| 289 | s.Printf ("env[%u] = %s\n", i, env); |
| 290 | } |
| 291 | } |
| 292 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 293 | if (m_arch.IsValid()) |
| 294 | s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str()); |
| 295 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 296 | if (m_uid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 297 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 298 | cstr = platform->GetUserName (m_uid); |
| 299 | s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 300 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 301 | if (m_gid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 302 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 303 | cstr = platform->GetGroupName (m_gid); |
| 304 | s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 305 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 306 | if (m_euid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 307 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 308 | cstr = platform->GetUserName (m_euid); |
| 309 | s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 310 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 311 | if (m_egid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 312 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 313 | cstr = platform->GetGroupName (m_egid); |
| 314 | s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 319 | ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 320 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 321 | const char *label; |
| 322 | if (show_args || verbose) |
| 323 | label = "ARGUMENTS"; |
| 324 | else |
| 325 | label = "NAME"; |
| 326 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 327 | if (verbose) |
| 328 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 329 | s.Printf ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE %s\n", label); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 330 | s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n"); |
| 331 | } |
| 332 | else |
| 333 | { |
Jim Ingham | 368ac22 | 2014-08-15 17:05:27 +0000 | [diff] [blame] | 334 | s.Printf ("PID PARENT USER TRIPLE %s\n", label); |
| 335 | s.PutCString ("====== ====== ========== ======================== ============================\n"); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 336 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 340 | ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 341 | { |
| 342 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
| 343 | { |
| 344 | const char *cstr; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 345 | s.Printf ("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 346 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 347 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 348 | if (verbose) |
| 349 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 350 | cstr = platform->GetUserName (m_uid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 351 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 352 | s.Printf ("%-10s ", cstr); |
| 353 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 354 | s.Printf ("%-10u ", m_uid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 355 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 356 | cstr = platform->GetGroupName (m_gid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 357 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 358 | s.Printf ("%-10s ", cstr); |
| 359 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 360 | s.Printf ("%-10u ", m_gid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 361 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 362 | cstr = platform->GetUserName (m_euid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 363 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 364 | s.Printf ("%-10s ", cstr); |
| 365 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 366 | s.Printf ("%-10u ", m_euid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 367 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 368 | cstr = platform->GetGroupName (m_egid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 369 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 370 | s.Printf ("%-10s ", cstr); |
| 371 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 372 | s.Printf ("%-10u ", m_egid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 373 | s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : ""); |
| 374 | } |
| 375 | else |
| 376 | { |
Jim Ingham | 368ac22 | 2014-08-15 17:05:27 +0000 | [diff] [blame] | 377 | s.Printf ("%-10s %-24s ", |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 378 | platform->GetUserName (m_euid), |
Jim Ingham | 368ac22 | 2014-08-15 17:05:27 +0000 | [diff] [blame] | 379 | m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : ""); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 382 | if (verbose || show_args) |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 383 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 384 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 385 | if (argc > 0) |
| 386 | { |
| 387 | for (uint32_t i=0; i<argc; i++) |
| 388 | { |
| 389 | if (i > 0) |
| 390 | s.PutChar (' '); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 391 | s.PutCString (m_arguments.GetArgumentAtIndex(i)); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | s.PutCString (GetName()); |
| 398 | } |
| 399 | |
| 400 | s.EOL(); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 404 | Error |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 405 | ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 406 | { |
| 407 | Error error; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 408 | const int short_option = m_getopt_table[option_idx].val; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 409 | |
| 410 | switch (short_option) |
| 411 | { |
| 412 | case 's': // Stop at program entry point |
| 413 | launch_info.GetFlags().Set (eLaunchFlagStopAtEntry); |
| 414 | break; |
| 415 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 416 | case 'i': // STDIN for read only |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 417 | { |
| 418 | FileAction action; |
| 419 | if (action.Open (STDIN_FILENO, option_arg, true, false)) |
| 420 | launch_info.AppendFileAction (action); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 421 | break; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 422 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 423 | |
| 424 | case 'o': // Open STDOUT for write only |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 425 | { |
| 426 | FileAction action; |
| 427 | if (action.Open (STDOUT_FILENO, option_arg, false, true)) |
| 428 | launch_info.AppendFileAction (action); |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 429 | break; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 430 | } |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 431 | |
| 432 | case 'e': // STDERR for write only |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 433 | { |
| 434 | FileAction action; |
| 435 | if (action.Open (STDERR_FILENO, option_arg, false, true)) |
| 436 | launch_info.AppendFileAction (action); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 437 | break; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 438 | } |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 439 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 440 | case 'p': // Process plug-in name |
| 441 | launch_info.SetProcessPluginName (option_arg); |
| 442 | break; |
| 443 | |
| 444 | case 'n': // Disable STDIO |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 445 | { |
| 446 | FileAction action; |
| 447 | if (action.Open (STDIN_FILENO, "/dev/null", true, false)) |
| 448 | launch_info.AppendFileAction (action); |
| 449 | if (action.Open (STDOUT_FILENO, "/dev/null", false, true)) |
| 450 | launch_info.AppendFileAction (action); |
| 451 | if (action.Open (STDERR_FILENO, "/dev/null", false, true)) |
| 452 | launch_info.AppendFileAction (action); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 453 | break; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 454 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 455 | |
| 456 | case 'w': |
| 457 | launch_info.SetWorkingDirectory (option_arg); |
| 458 | break; |
| 459 | |
| 460 | case 't': // Open process in new terminal window |
| 461 | launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY); |
| 462 | break; |
| 463 | |
| 464 | case 'a': |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 465 | if (!launch_info.GetArchitecture().SetTriple (option_arg, m_interpreter.GetPlatform(true).get())) |
| 466 | launch_info.GetArchitecture().SetTriple (option_arg); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 467 | break; |
| 468 | |
Todd Fiala | 5163792 | 2014-08-19 17:40:43 +0000 | [diff] [blame] | 469 | case 'A': // Disable ASLR. |
| 470 | { |
| 471 | bool success; |
| 472 | const bool disable_aslr_arg = Args::StringToBoolean (option_arg, true, &success); |
| 473 | if (success) |
| 474 | disable_aslr = disable_aslr_arg ? eLazyBoolYes : eLazyBoolNo; |
| 475 | else |
| 476 | error.SetErrorStringWithFormat ("Invalid boolean value for disable-aslr option: '%s'", option_arg ? option_arg : "<null>"); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 477 | break; |
Todd Fiala | 5163792 | 2014-08-19 17:40:43 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | case 'c': |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 481 | if (option_arg && option_arg[0]) |
| 482 | launch_info.SetShell (option_arg); |
| 483 | else |
Ed Maste | b8ca4a2 | 2013-09-03 23:04:53 +0000 | [diff] [blame] | 484 | launch_info.SetShell (LLDB_DEFAULT_SHELL); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 485 | break; |
| 486 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 487 | case 'v': |
| 488 | launch_info.GetEnvironmentEntries().AppendArgument(option_arg); |
| 489 | break; |
| 490 | |
| 491 | default: |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 492 | error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 493 | break; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 494 | } |
| 495 | return error; |
| 496 | } |
| 497 | |
| 498 | OptionDefinition |
| 499 | ProcessLaunchCommandOptions::g_option_table[] = |
| 500 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 501 | { LLDB_OPT_SET_ALL, false, "stop-at-entry", 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Stop at the entry point of the program when launching a process."}, |
Todd Fiala | 5163792 | 2014-08-19 17:40:43 +0000 | [diff] [blame] | 502 | { LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Set whether to disable address space layout randomization when launching a process."}, |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 503 | { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."}, |
| 504 | { LLDB_OPT_SET_ALL, false, "working-dir", 'w', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeDirectoryName, "Set the current working directory to <path> when running the inferior."}, |
| 505 | { LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."}, |
| 506 | { LLDB_OPT_SET_ALL, false, "environment", 'v', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeNone, "Specify an environment variable name/value string (--environment NAME=VALUE). Can be specified multiple times for subsequent environment entries."}, |
| 507 | { LLDB_OPT_SET_ALL, false, "shell", 'c', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeFilename, "Run the process in a shell (not supported on all platforms)."}, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 508 | |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 509 | { LLDB_OPT_SET_1 , false, "stdin", 'i', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stdin for the process to <filename>."}, |
| 510 | { LLDB_OPT_SET_1 , false, "stdout", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stdout for the process to <filename>."}, |
| 511 | { LLDB_OPT_SET_1 , false, "stderr", 'e', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stderr for the process to <filename>."}, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 512 | |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 513 | { LLDB_OPT_SET_2 , false, "tty", 't', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Start the process in a terminal (not supported on all platforms)."}, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 514 | |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 515 | { LLDB_OPT_SET_3 , false, "no-stdio", 'n', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."}, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 516 | |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 517 | { 0 , false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | |
| 521 | |
| 522 | bool |
| 523 | ProcessInstanceInfoMatch::NameMatches (const char *process_name) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 524 | { |
| 525 | if (m_name_match_type == eNameMatchIgnore || process_name == NULL) |
| 526 | return true; |
| 527 | const char *match_name = m_match_info.GetName(); |
| 528 | if (!match_name) |
| 529 | return true; |
| 530 | |
| 531 | return lldb_private::NameMatches (process_name, m_name_match_type, match_name); |
| 532 | } |
| 533 | |
| 534 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 535 | ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 536 | { |
| 537 | if (!NameMatches (proc_info.GetName())) |
| 538 | return false; |
| 539 | |
| 540 | if (m_match_info.ProcessIDIsValid() && |
| 541 | m_match_info.GetProcessID() != proc_info.GetProcessID()) |
| 542 | return false; |
| 543 | |
| 544 | if (m_match_info.ParentProcessIDIsValid() && |
| 545 | m_match_info.GetParentProcessID() != proc_info.GetParentProcessID()) |
| 546 | return false; |
| 547 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 548 | if (m_match_info.UserIDIsValid () && |
| 549 | m_match_info.GetUserID() != proc_info.GetUserID()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 550 | return false; |
| 551 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 552 | if (m_match_info.GroupIDIsValid () && |
| 553 | m_match_info.GetGroupID() != proc_info.GetGroupID()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 554 | return false; |
| 555 | |
| 556 | if (m_match_info.EffectiveUserIDIsValid () && |
| 557 | m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID()) |
| 558 | return false; |
| 559 | |
| 560 | if (m_match_info.EffectiveGroupIDIsValid () && |
| 561 | m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID()) |
| 562 | return false; |
| 563 | |
| 564 | if (m_match_info.GetArchitecture().IsValid() && |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 565 | !m_match_info.GetArchitecture().IsCompatibleMatch(proc_info.GetArchitecture())) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 566 | return false; |
| 567 | return true; |
| 568 | } |
| 569 | |
| 570 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 571 | ProcessInstanceInfoMatch::MatchAllProcesses () const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 572 | { |
| 573 | if (m_name_match_type != eNameMatchIgnore) |
| 574 | return false; |
| 575 | |
| 576 | if (m_match_info.ProcessIDIsValid()) |
| 577 | return false; |
| 578 | |
| 579 | if (m_match_info.ParentProcessIDIsValid()) |
| 580 | return false; |
| 581 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 582 | if (m_match_info.UserIDIsValid ()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 583 | return false; |
| 584 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 585 | if (m_match_info.GroupIDIsValid ()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 586 | return false; |
| 587 | |
| 588 | if (m_match_info.EffectiveUserIDIsValid ()) |
| 589 | return false; |
| 590 | |
| 591 | if (m_match_info.EffectiveGroupIDIsValid ()) |
| 592 | return false; |
| 593 | |
| 594 | if (m_match_info.GetArchitecture().IsValid()) |
| 595 | return false; |
| 596 | |
| 597 | if (m_match_all_users) |
| 598 | return false; |
| 599 | |
| 600 | return true; |
| 601 | |
| 602 | } |
| 603 | |
| 604 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 605 | ProcessInstanceInfoMatch::Clear() |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 606 | { |
| 607 | m_match_info.Clear(); |
| 608 | m_name_match_type = eNameMatchIgnore; |
| 609 | m_match_all_users = false; |
| 610 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 611 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 612 | ProcessSP |
| 613 | Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener, const FileSpec *crash_file_path) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 614 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 615 | static uint32_t g_process_unique_id = 0; |
| 616 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 617 | ProcessSP process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 618 | ProcessCreateInstance create_callback = NULL; |
| 619 | if (plugin_name) |
| 620 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 621 | ConstString const_plugin_name(plugin_name); |
| 622 | create_callback = PluginManager::GetProcessCreateCallbackForPluginName (const_plugin_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 623 | if (create_callback) |
| 624 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 625 | process_sp = create_callback(target, listener, crash_file_path); |
| 626 | if (process_sp) |
| 627 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 628 | if (process_sp->CanDebug(target, true)) |
| 629 | { |
| 630 | process_sp->m_process_unique_id = ++g_process_unique_id; |
| 631 | } |
| 632 | else |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 633 | process_sp.reset(); |
| 634 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | else |
| 638 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 639 | for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 640 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 641 | process_sp = create_callback(target, listener, crash_file_path); |
| 642 | if (process_sp) |
| 643 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 644 | if (process_sp->CanDebug(target, false)) |
| 645 | { |
| 646 | process_sp->m_process_unique_id = ++g_process_unique_id; |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 647 | break; |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 648 | } |
| 649 | else |
| 650 | process_sp.reset(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 651 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 654 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 657 | ConstString & |
| 658 | Process::GetStaticBroadcasterClass () |
| 659 | { |
| 660 | static ConstString class_name ("lldb.process"); |
| 661 | return class_name; |
| 662 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 663 | |
| 664 | //---------------------------------------------------------------------- |
| 665 | // Process constructor |
| 666 | //---------------------------------------------------------------------- |
| 667 | Process::Process(Target &target, Listener &listener) : |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 668 | Process(target, listener, Host::GetUnixSignals ()) |
| 669 | { |
| 670 | // This constructor just delegates to the full Process constructor, |
| 671 | // defaulting to using the Host's UnixSignals. |
| 672 | } |
| 673 | |
| 674 | Process::Process(Target &target, Listener &listener, const UnixSignalsSP &unix_signals_sp) : |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 675 | ProcessProperties (false), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 676 | UserID (LLDB_INVALID_PROCESS_ID), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 677 | Broadcaster (&(target.GetDebugger()), "lldb.process"), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 678 | m_target (target), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 679 | m_public_state (eStateUnloaded), |
| 680 | m_private_state (eStateUnloaded), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 681 | m_private_state_broadcaster (NULL, "lldb.process.internal_state_broadcaster"), |
| 682 | m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 683 | m_private_state_listener ("lldb.process.internal_state_listener"), |
| 684 | m_private_state_control_wait(), |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 685 | m_mod_id (), |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 686 | m_process_unique_id(0), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 687 | m_thread_index_id (0), |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 688 | m_thread_id_to_index_id_map (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 689 | m_exit_status (-1), |
| 690 | m_exit_string (), |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 691 | m_thread_mutex (Mutex::eMutexTypeRecursive), |
| 692 | m_thread_list_real (this), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 693 | m_thread_list (this), |
Jason Molenda | 864f1cc | 2013-11-11 05:20:44 +0000 | [diff] [blame] | 694 | m_extended_thread_list (this), |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 695 | m_extended_thread_stop_id (0), |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 696 | m_queue_list (this), |
| 697 | m_queue_list_stop_id (0), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 698 | m_notifications (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 699 | m_image_tokens (), |
| 700 | m_listener (listener), |
| 701 | m_breakpoint_site_list (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 702 | m_dynamic_checkers_ap (), |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 703 | m_unix_signals_sp (unix_signals_sp), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 704 | m_abi_sp (), |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 705 | m_process_input_reader (), |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 706 | m_stdio_communication ("process.stdio"), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 707 | m_stdio_communication_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 708 | m_stdout_data (), |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 709 | m_stderr_data (), |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 710 | m_profile_data_comm_mutex (Mutex::eMutexTypeRecursive), |
| 711 | m_profile_data (), |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 712 | m_iohandler_sync (false), |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 713 | m_memory_cache (*this), |
| 714 | m_allocated_memory_cache (*this), |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 715 | m_should_detach (false), |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 716 | m_next_event_action_ap(), |
Greg Clayton | 9624985 | 2013-04-18 16:57:27 +0000 | [diff] [blame] | 717 | m_public_run_lock (), |
Greg Clayton | 9624985 | 2013-04-18 16:57:27 +0000 | [diff] [blame] | 718 | m_private_run_lock (), |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 719 | m_currently_handling_event(false), |
Jim Ingham | 4fc6cb9 | 2012-08-22 21:34:33 +0000 | [diff] [blame] | 720 | m_finalize_called(false), |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 721 | m_clear_thread_plans_on_stop (false), |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 722 | m_force_next_event_delivery(false), |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 723 | m_last_broadcast_state (eStateInvalid), |
Jason Molenda | 69b6b63 | 2013-03-05 03:33:59 +0000 | [diff] [blame] | 724 | m_destroy_in_process (false), |
| 725 | m_can_jit(eCanJITDontKnow) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 726 | { |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 727 | CheckInWithManager (); |
Caroline Tice | 1559a46 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 728 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 729 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 730 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 731 | log->Printf ("%p Process::Process()", static_cast<void*>(this)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 732 | |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 733 | if (!m_unix_signals_sp) |
| 734 | m_unix_signals_sp.reset (new UnixSignals ()); |
| 735 | |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 736 | SetEventName (eBroadcastBitStateChanged, "state-changed"); |
| 737 | SetEventName (eBroadcastBitInterrupt, "interrupt"); |
| 738 | SetEventName (eBroadcastBitSTDOUT, "stdout-available"); |
| 739 | SetEventName (eBroadcastBitSTDERR, "stderr-available"); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 740 | SetEventName (eBroadcastBitProfileData, "profile-data-available"); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 741 | |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 742 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlStop , "control-stop" ); |
| 743 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlPause , "control-pause" ); |
| 744 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlResume, "control-resume"); |
| 745 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 746 | listener.StartListeningForEvents (this, |
| 747 | eBroadcastBitStateChanged | |
| 748 | eBroadcastBitInterrupt | |
| 749 | eBroadcastBitSTDOUT | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 750 | eBroadcastBitSTDERR | |
| 751 | eBroadcastBitProfileData); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 752 | |
| 753 | m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster, |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 754 | eBroadcastBitStateChanged | |
| 755 | eBroadcastBitInterrupt); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 756 | |
| 757 | m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster, |
| 758 | eBroadcastInternalStateControlStop | |
| 759 | eBroadcastInternalStateControlPause | |
| 760 | eBroadcastInternalStateControlResume); |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 761 | // We need something valid here, even if just the default UnixSignalsSP. |
| 762 | assert (m_unix_signals_sp && "null m_unix_signals_sp after initialization"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | //---------------------------------------------------------------------- |
| 766 | // Destructor |
| 767 | //---------------------------------------------------------------------- |
| 768 | Process::~Process() |
| 769 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 770 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 771 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 772 | log->Printf ("%p Process::~Process()", static_cast<void*>(this)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 773 | StopPrivateStateThread(); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 774 | |
| 775 | // ThreadList::Clear() will try to acquire this process's mutex, so |
| 776 | // explicitly clear the thread list here to ensure that the mutex |
| 777 | // is not destroyed before the thread list. |
| 778 | m_thread_list.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 781 | const ProcessPropertiesSP & |
| 782 | Process::GetGlobalProperties() |
| 783 | { |
| 784 | static ProcessPropertiesSP g_settings_sp; |
| 785 | if (!g_settings_sp) |
| 786 | g_settings_sp.reset (new ProcessProperties (true)); |
| 787 | return g_settings_sp; |
| 788 | } |
| 789 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 790 | void |
| 791 | Process::Finalize() |
| 792 | { |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 793 | switch (GetPrivateState()) |
| 794 | { |
| 795 | case eStateConnected: |
| 796 | case eStateAttaching: |
| 797 | case eStateLaunching: |
| 798 | case eStateStopped: |
| 799 | case eStateRunning: |
| 800 | case eStateStepping: |
| 801 | case eStateCrashed: |
| 802 | case eStateSuspended: |
| 803 | if (GetShouldDetach()) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 804 | { |
| 805 | // FIXME: This will have to be a process setting: |
| 806 | bool keep_stopped = false; |
| 807 | Detach(keep_stopped); |
| 808 | } |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 809 | else |
| 810 | Destroy(); |
| 811 | break; |
| 812 | |
| 813 | case eStateInvalid: |
| 814 | case eStateUnloaded: |
| 815 | case eStateDetached: |
| 816 | case eStateExited: |
| 817 | break; |
| 818 | } |
| 819 | |
Greg Clayton | 1ed54f5 | 2011-10-01 00:45:15 +0000 | [diff] [blame] | 820 | // Clear our broadcaster before we proceed with destroying |
| 821 | Broadcaster::Clear(); |
| 822 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 823 | // Do any cleanup needed prior to being destructed... Subclasses |
| 824 | // that override this method should call this superclass method as well. |
Jim Ingham | d0a3e12 | 2011-02-16 17:54:55 +0000 | [diff] [blame] | 825 | |
| 826 | // We need to destroy the loader before the derived Process class gets destroyed |
| 827 | // since it is very likely that undoing the loader will require access to the real process. |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 828 | m_dynamic_checkers_ap.reset(); |
| 829 | m_abi_sp.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 830 | m_os_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 831 | m_system_runtime_ap.reset(); |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 832 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 833 | m_jit_loaders_ap.reset(); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 834 | m_thread_list_real.Destroy(); |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 835 | m_thread_list.Destroy(); |
Jason Molenda | 864f1cc | 2013-11-11 05:20:44 +0000 | [diff] [blame] | 836 | m_extended_thread_list.Destroy(); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 837 | m_queue_list.Clear(); |
| 838 | m_queue_list_stop_id = 0; |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 839 | std::vector<Notifications> empty_notifications; |
| 840 | m_notifications.swap(empty_notifications); |
| 841 | m_image_tokens.clear(); |
| 842 | m_memory_cache.Clear(); |
| 843 | m_allocated_memory_cache.Clear(); |
| 844 | m_language_runtimes.clear(); |
| 845 | m_next_event_action_ap.reset(); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 846 | //#ifdef LLDB_CONFIGURATION_DEBUG |
| 847 | // StreamFile s(stdout, false); |
| 848 | // EventSP event_sp; |
| 849 | // while (m_private_state_listener.GetNextEvent(event_sp)) |
| 850 | // { |
| 851 | // event_sp->Dump (&s); |
| 852 | // s.EOL(); |
| 853 | // } |
| 854 | //#endif |
| 855 | // We have to be very careful here as the m_private_state_listener might |
| 856 | // contain events that have ProcessSP values in them which can keep this |
| 857 | // process around forever. These events need to be cleared out. |
| 858 | m_private_state_listener.Clear(); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 859 | m_public_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 860 | m_public_run_lock.SetStopped(); |
| 861 | m_private_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 862 | m_private_run_lock.SetStopped(); |
Jim Ingham | 4fc6cb9 | 2012-08-22 21:34:33 +0000 | [diff] [blame] | 863 | m_finalize_called = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | void |
| 867 | Process::RegisterNotificationCallbacks (const Notifications& callbacks) |
| 868 | { |
| 869 | m_notifications.push_back(callbacks); |
| 870 | if (callbacks.initialize != NULL) |
| 871 | callbacks.initialize (callbacks.baton, this); |
| 872 | } |
| 873 | |
| 874 | bool |
| 875 | Process::UnregisterNotificationCallbacks(const Notifications& callbacks) |
| 876 | { |
| 877 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 878 | for (pos = m_notifications.begin(); pos != end; ++pos) |
| 879 | { |
| 880 | if (pos->baton == callbacks.baton && |
| 881 | pos->initialize == callbacks.initialize && |
| 882 | pos->process_state_changed == callbacks.process_state_changed) |
| 883 | { |
| 884 | m_notifications.erase(pos); |
| 885 | return true; |
| 886 | } |
| 887 | } |
| 888 | return false; |
| 889 | } |
| 890 | |
| 891 | void |
| 892 | Process::SynchronouslyNotifyStateChanged (StateType state) |
| 893 | { |
| 894 | std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end(); |
| 895 | for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos) |
| 896 | { |
| 897 | if (notification_pos->process_state_changed) |
| 898 | notification_pos->process_state_changed (notification_pos->baton, this, state); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | // FIXME: We need to do some work on events before the general Listener sees them. |
| 903 | // For instance if we are continuing from a breakpoint, we need to ensure that we do |
| 904 | // the little "insert real insn, step & stop" trick. But we can't do that when the |
| 905 | // event is delivered by the broadcaster - since that is done on the thread that is |
| 906 | // waiting for new events, so if we needed more than one event for our handling, we would |
| 907 | // stall. So instead we do it when we fetch the event off of the queue. |
| 908 | // |
| 909 | |
| 910 | StateType |
| 911 | Process::GetNextEvent (EventSP &event_sp) |
| 912 | { |
| 913 | StateType state = eStateInvalid; |
| 914 | |
| 915 | if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp) |
| 916 | state = Process::ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 917 | |
| 918 | return state; |
| 919 | } |
| 920 | |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 921 | bool |
| 922 | Process::SyncIOHandler (uint64_t timeout_msec) |
| 923 | { |
| 924 | bool timed_out = false; |
| 925 | |
| 926 | // don't sync (potentially context switch) in case where there is no process IO |
| 927 | if (m_process_input_reader) |
| 928 | { |
| 929 | TimeValue timeout = TimeValue::Now(); |
| 930 | timeout.OffsetWithMicroSeconds(timeout_msec*1000); |
| 931 | |
| 932 | m_iohandler_sync.WaitForValueEqualTo(true, &timeout, &timed_out); |
| 933 | |
| 934 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 935 | if(log) |
| 936 | { |
| 937 | if(timed_out) |
| 938 | log->Printf ("Process::%s pid %" PRIu64 " (timeout=%" PRIu64 "ms): FAIL", __FUNCTION__, GetID (), timeout_msec); |
| 939 | else |
| 940 | log->Printf ("Process::%s pid %" PRIu64 ": SUCCESS", __FUNCTION__, GetID ()); |
| 941 | } |
| 942 | |
| 943 | // reset sync one-shot so it will be ready for next time |
| 944 | m_iohandler_sync.SetValue(false, eBroadcastNever); |
| 945 | } |
| 946 | |
| 947 | return !timed_out; |
| 948 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 949 | |
| 950 | StateType |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 951 | Process::WaitForProcessToStop (const TimeValue *timeout, lldb::EventSP *event_sp_ptr, bool wait_always, Listener *hijack_listener) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 952 | { |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 953 | // We can't just wait for a "stopped" event, because the stopped event may have restarted the target. |
| 954 | // We have to actually check each event, and in the case of a stopped event check the restarted flag |
| 955 | // on the event. |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 956 | if (event_sp_ptr) |
| 957 | event_sp_ptr->reset(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 958 | StateType state = GetState(); |
| 959 | // If we are exited or detached, we won't ever get back to any |
| 960 | // other valid state... |
| 961 | if (state == eStateDetached || state == eStateExited) |
| 962 | return state; |
| 963 | |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 964 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 965 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 966 | log->Printf ("Process::%s (timeout = %p)", __FUNCTION__, |
| 967 | static_cast<const void*>(timeout)); |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 968 | |
| 969 | if (!wait_always && |
| 970 | StateIsStoppedState(state, true) && |
| 971 | StateIsStoppedState(GetPrivateState(), true)) { |
| 972 | if (log) |
| 973 | log->Printf("Process::%s returning without waiting for events; process private and public states are already 'stopped'.", |
| 974 | __FUNCTION__); |
| 975 | return state; |
| 976 | } |
| 977 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 978 | while (state != eStateInvalid) |
| 979 | { |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 980 | EventSP event_sp; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 981 | state = WaitForStateChangedEvents (timeout, event_sp, hijack_listener); |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 982 | if (event_sp_ptr && event_sp) |
| 983 | *event_sp_ptr = event_sp; |
| 984 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 985 | switch (state) |
| 986 | { |
| 987 | case eStateCrashed: |
| 988 | case eStateDetached: |
| 989 | case eStateExited: |
| 990 | case eStateUnloaded: |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 991 | // We need to toggle the run lock as this won't get done in |
| 992 | // SetPublicState() if the process is hijacked. |
| 993 | if (hijack_listener) |
| 994 | m_public_run_lock.SetStopped(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 995 | return state; |
| 996 | case eStateStopped: |
| 997 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 998 | continue; |
| 999 | else |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1000 | { |
| 1001 | // We need to toggle the run lock as this won't get done in |
| 1002 | // SetPublicState() if the process is hijacked. |
| 1003 | if (hijack_listener) |
| 1004 | m_public_run_lock.SetStopped(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1005 | return state; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1006 | } |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1007 | default: |
| 1008 | continue; |
| 1009 | } |
| 1010 | } |
| 1011 | return state; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | StateType |
| 1016 | Process::WaitForState |
| 1017 | ( |
| 1018 | const TimeValue *timeout, |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1019 | const StateType *match_states, |
| 1020 | const uint32_t num_match_states |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1021 | ) |
| 1022 | { |
| 1023 | EventSP event_sp; |
| 1024 | uint32_t i; |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1025 | StateType state = GetState(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1026 | while (state != eStateInvalid) |
| 1027 | { |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1028 | // If we are exited or detached, we won't ever get back to any |
| 1029 | // other valid state... |
| 1030 | if (state == eStateDetached || state == eStateExited) |
| 1031 | return state; |
| 1032 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1033 | state = WaitForStateChangedEvents (timeout, event_sp, NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1034 | |
| 1035 | for (i=0; i<num_match_states; ++i) |
| 1036 | { |
| 1037 | if (match_states[i] == state) |
| 1038 | return state; |
| 1039 | } |
| 1040 | } |
| 1041 | return state; |
| 1042 | } |
| 1043 | |
Jim Ingham | 30f9b21 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 1044 | bool |
| 1045 | Process::HijackProcessEvents (Listener *listener) |
| 1046 | { |
| 1047 | if (listener != NULL) |
| 1048 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1049 | return HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
Jim Ingham | 30f9b21 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 1050 | } |
| 1051 | else |
| 1052 | return false; |
| 1053 | } |
| 1054 | |
| 1055 | void |
| 1056 | Process::RestoreProcessEvents () |
| 1057 | { |
| 1058 | RestoreBroadcaster(); |
| 1059 | } |
| 1060 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 1061 | bool |
| 1062 | Process::HijackPrivateProcessEvents (Listener *listener) |
| 1063 | { |
| 1064 | if (listener != NULL) |
| 1065 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1066 | return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 1067 | } |
| 1068 | else |
| 1069 | return false; |
| 1070 | } |
| 1071 | |
| 1072 | void |
| 1073 | Process::RestorePrivateProcessEvents () |
| 1074 | { |
| 1075 | m_private_state_broadcaster.RestoreBroadcaster(); |
| 1076 | } |
| 1077 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1078 | StateType |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1079 | Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp, Listener *hijack_listener) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1080 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1081 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1082 | |
| 1083 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1084 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, |
| 1085 | static_cast<const void*>(timeout)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1086 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1087 | Listener *listener = hijack_listener; |
| 1088 | if (listener == NULL) |
| 1089 | listener = &m_listener; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1091 | StateType state = eStateInvalid; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1092 | if (listener->WaitForEventForBroadcasterWithType (timeout, |
| 1093 | this, |
| 1094 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, |
| 1095 | event_sp)) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1096 | { |
| 1097 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1098 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1099 | else if (log) |
| 1100 | log->Printf ("Process::%s got no event or was interrupted.", __FUNCTION__); |
| 1101 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1102 | |
| 1103 | if (log) |
| 1104 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1105 | __FUNCTION__, static_cast<const void*>(timeout), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1106 | StateAsCString(state)); |
| 1107 | return state; |
| 1108 | } |
| 1109 | |
| 1110 | Event * |
| 1111 | Process::PeekAtStateChangedEvents () |
| 1112 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1113 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1114 | |
| 1115 | if (log) |
| 1116 | log->Printf ("Process::%s...", __FUNCTION__); |
| 1117 | |
| 1118 | Event *event_ptr; |
Greg Clayton | 3fcbed6 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1119 | event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this, |
| 1120 | eBroadcastBitStateChanged); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1121 | if (log) |
| 1122 | { |
| 1123 | if (event_ptr) |
| 1124 | { |
| 1125 | log->Printf ("Process::%s (event_ptr) => %s", |
| 1126 | __FUNCTION__, |
| 1127 | StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr))); |
| 1128 | } |
| 1129 | else |
| 1130 | { |
| 1131 | log->Printf ("Process::%s no events found", |
| 1132 | __FUNCTION__); |
| 1133 | } |
| 1134 | } |
| 1135 | return event_ptr; |
| 1136 | } |
| 1137 | |
| 1138 | StateType |
| 1139 | Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 1140 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1141 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1142 | |
| 1143 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1144 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, |
| 1145 | static_cast<const void*>(timeout)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1146 | |
| 1147 | StateType state = eStateInvalid; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1148 | if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout, |
| 1149 | &m_private_state_broadcaster, |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1150 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1151 | event_sp)) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1152 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1153 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1154 | |
| 1155 | // This is a bit of a hack, but when we wait here we could very well return |
| 1156 | // to the command-line, and that could disable the log, which would render the |
| 1157 | // log we got above invalid. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1158 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1159 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
| 1160 | __FUNCTION__, static_cast<const void *>(timeout), |
| 1161 | state == eStateInvalid ? "TIMEOUT" : StateAsCString(state)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1162 | return state; |
| 1163 | } |
| 1164 | |
| 1165 | bool |
| 1166 | Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only) |
| 1167 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1168 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1169 | |
| 1170 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1171 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, |
| 1172 | static_cast<const void*>(timeout)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1173 | |
| 1174 | if (control_only) |
| 1175 | return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp); |
| 1176 | else |
| 1177 | return m_private_state_listener.WaitForEvent(timeout, event_sp); |
| 1178 | } |
| 1179 | |
| 1180 | bool |
| 1181 | Process::IsRunning () const |
| 1182 | { |
| 1183 | return StateIsRunningState (m_public_state.GetValue()); |
| 1184 | } |
| 1185 | |
| 1186 | int |
| 1187 | Process::GetExitStatus () |
| 1188 | { |
| 1189 | if (m_public_state.GetValue() == eStateExited) |
| 1190 | return m_exit_status; |
| 1191 | return -1; |
| 1192 | } |
| 1193 | |
Greg Clayton | 85851dd | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 1194 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1195 | const char * |
| 1196 | Process::GetExitDescription () |
| 1197 | { |
| 1198 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 1199 | return m_exit_string.c_str(); |
| 1200 | return NULL; |
| 1201 | } |
| 1202 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1203 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1204 | Process::SetExitStatus (int status, const char *cstr) |
| 1205 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1206 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1207 | if (log) |
| 1208 | log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)", |
| 1209 | status, status, |
| 1210 | cstr ? "\"" : "", |
| 1211 | cstr ? cstr : "NULL", |
| 1212 | cstr ? "\"" : ""); |
| 1213 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1214 | // We were already in the exited state |
| 1215 | if (m_private_state.GetValue() == eStateExited) |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1216 | { |
Greg Clayton | 385d603 | 2011-01-26 23:47:29 +0000 | [diff] [blame] | 1217 | if (log) |
| 1218 | log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited"); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1219 | return false; |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1220 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1221 | |
| 1222 | m_exit_status = status; |
| 1223 | if (cstr) |
| 1224 | m_exit_string = cstr; |
| 1225 | else |
| 1226 | m_exit_string.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1227 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1228 | DidExit (); |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 1229 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1230 | SetPrivateState (eStateExited); |
| 1231 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | // This static callback can be used to watch for local child processes on |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 1235 | // the current host. The child process exits, the process will be |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1236 | // found in the global target list (we want to be completely sure that the |
| 1237 | // lldb_private::Process doesn't go away before we can deliver the signal. |
| 1238 | bool |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1239 | Process::SetProcessExitStatus (void *callback_baton, |
| 1240 | lldb::pid_t pid, |
| 1241 | bool exited, |
| 1242 | int signo, // Zero for no signal |
| 1243 | int exit_status // Exit value of process if signal is zero |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1244 | ) |
| 1245 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1246 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1247 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1248 | log->Printf ("Process::SetProcessExitStatus (baton=%p, pid=%" PRIu64 ", exited=%i, signal=%i, exit_status=%i)\n", |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1249 | callback_baton, |
| 1250 | pid, |
| 1251 | exited, |
| 1252 | signo, |
| 1253 | exit_status); |
| 1254 | |
| 1255 | if (exited) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1256 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1257 | TargetSP target_sp(Debugger::FindTargetWithProcessID (pid)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1258 | if (target_sp) |
| 1259 | { |
| 1260 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 1261 | if (process_sp) |
| 1262 | { |
| 1263 | const char *signal_cstr = NULL; |
| 1264 | if (signo) |
| 1265 | signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo); |
| 1266 | |
| 1267 | process_sp->SetExitStatus (exit_status, signal_cstr); |
| 1268 | } |
| 1269 | } |
| 1270 | return true; |
| 1271 | } |
| 1272 | return false; |
| 1273 | } |
| 1274 | |
| 1275 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1276 | void |
| 1277 | Process::UpdateThreadListIfNeeded () |
| 1278 | { |
| 1279 | const uint32_t stop_id = GetStopID(); |
| 1280 | if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID()) |
| 1281 | { |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1282 | const StateType state = GetPrivateState(); |
| 1283 | if (StateIsStoppedState (state, true)) |
| 1284 | { |
| 1285 | Mutex::Locker locker (m_thread_list.GetMutex ()); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1286 | // m_thread_list does have its own mutex, but we need to |
| 1287 | // hold onto the mutex between the call to UpdateThreadList(...) |
| 1288 | // and the os->UpdateThreadList(...) so it doesn't change on us |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1289 | ThreadList &old_thread_list = m_thread_list; |
| 1290 | ThreadList real_thread_list(this); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1291 | ThreadList new_thread_list(this); |
| 1292 | // Always update the thread list with the protocol specific |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1293 | // thread list, but only update if "true" is returned |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1294 | if (UpdateThreadList (m_thread_list_real, real_thread_list)) |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1295 | { |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 1296 | // Don't call into the OperatingSystem to update the thread list if we are shutting down, since |
| 1297 | // that may call back into the SBAPI's, requiring the API lock which is already held by whoever is |
| 1298 | // shutting us down, causing a deadlock. |
| 1299 | if (!m_destroy_in_process) |
| 1300 | { |
| 1301 | OperatingSystem *os = GetOperatingSystem (); |
| 1302 | if (os) |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1303 | { |
| 1304 | // Clear any old backing threads where memory threads might have been |
| 1305 | // backed by actual threads from the lldb_private::Process subclass |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1306 | size_t num_old_threads = old_thread_list.GetSize(false); |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1307 | for (size_t i=0; i<num_old_threads; ++i) |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1308 | old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread(); |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1309 | |
| 1310 | // Now let the OperatingSystem plug-in update the thread list |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1311 | os->UpdateThreadList (old_thread_list, // Old list full of threads created by OS plug-in |
| 1312 | real_thread_list, // The actual thread list full of threads created by each lldb_private::Process subclass |
| 1313 | new_thread_list); // The new thread list that we will show to the user that gets filled in |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1314 | } |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1315 | else |
| 1316 | { |
| 1317 | // No OS plug-in, the new thread list is the same as the real thread list |
| 1318 | new_thread_list = real_thread_list; |
| 1319 | } |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 1320 | } |
Jim Ingham | 02ff8e0 | 2013-06-22 00:55:02 +0000 | [diff] [blame] | 1321 | |
| 1322 | m_thread_list_real.Update(real_thread_list); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1323 | m_thread_list.Update (new_thread_list); |
| 1324 | m_thread_list.SetStopID (stop_id); |
Jason Molenda | a6e9130 | 2013-11-19 05:44:41 +0000 | [diff] [blame] | 1325 | |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 1326 | if (GetLastNaturalStopID () != m_extended_thread_stop_id) |
| 1327 | { |
| 1328 | // Clear any extended threads that we may have accumulated previously |
| 1329 | m_extended_thread_list.Clear(); |
| 1330 | m_extended_thread_stop_id = GetLastNaturalStopID (); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1331 | |
| 1332 | m_queue_list.Clear(); |
| 1333 | m_queue_list_stop_id = GetLastNaturalStopID (); |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 1334 | } |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1335 | } |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1336 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1337 | } |
| 1338 | } |
| 1339 | |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1340 | void |
| 1341 | Process::UpdateQueueListIfNeeded () |
| 1342 | { |
| 1343 | if (m_system_runtime_ap.get()) |
| 1344 | { |
| 1345 | if (m_queue_list.GetSize() == 0 || m_queue_list_stop_id != GetLastNaturalStopID()) |
| 1346 | { |
| 1347 | const StateType state = GetPrivateState(); |
| 1348 | if (StateIsStoppedState (state, true)) |
| 1349 | { |
| 1350 | m_system_runtime_ap->PopulateQueueList (m_queue_list); |
| 1351 | m_queue_list_stop_id = GetLastNaturalStopID(); |
| 1352 | } |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | |
Greg Clayton | a4d8747 | 2013-01-18 23:41:08 +0000 | [diff] [blame] | 1357 | ThreadSP |
| 1358 | Process::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context) |
| 1359 | { |
| 1360 | OperatingSystem *os = GetOperatingSystem (); |
| 1361 | if (os) |
| 1362 | return os->CreateThread(tid, context); |
| 1363 | return ThreadSP(); |
| 1364 | } |
| 1365 | |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1366 | uint32_t |
| 1367 | Process::GetNextThreadIndexID (uint64_t thread_id) |
| 1368 | { |
| 1369 | return AssignIndexIDToThread(thread_id); |
| 1370 | } |
| 1371 | |
| 1372 | bool |
| 1373 | Process::HasAssignedIndexIDToThread(uint64_t thread_id) |
| 1374 | { |
| 1375 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id); |
| 1376 | if (iterator == m_thread_id_to_index_id_map.end()) |
| 1377 | { |
| 1378 | return false; |
| 1379 | } |
| 1380 | else |
| 1381 | { |
| 1382 | return true; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | uint32_t |
| 1387 | Process::AssignIndexIDToThread(uint64_t thread_id) |
| 1388 | { |
| 1389 | uint32_t result = 0; |
| 1390 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id); |
| 1391 | if (iterator == m_thread_id_to_index_id_map.end()) |
| 1392 | { |
| 1393 | result = ++m_thread_index_id; |
| 1394 | m_thread_id_to_index_id_map[thread_id] = result; |
| 1395 | } |
| 1396 | else |
| 1397 | { |
| 1398 | result = iterator->second; |
| 1399 | } |
| 1400 | |
| 1401 | return result; |
| 1402 | } |
| 1403 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1404 | StateType |
| 1405 | Process::GetState() |
| 1406 | { |
| 1407 | // If any other threads access this we will need a mutex for it |
| 1408 | return m_public_state.GetValue (); |
| 1409 | } |
| 1410 | |
| 1411 | void |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1412 | Process::SetPublicState (StateType new_state, bool restarted) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1413 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1414 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1415 | if (log) |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1416 | log->Printf("Process::SetPublicState (state = %s, restarted = %i)", StateAsCString(new_state), restarted); |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1417 | const StateType old_state = m_public_state.GetValue(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1418 | m_public_state.SetValue (new_state); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1419 | |
| 1420 | // On the transition from Run to Stopped, we unlock the writer end of the |
| 1421 | // run lock. The lock gets locked in Resume, which is the public API |
| 1422 | // to tell the program to run. |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1423 | if (!IsHijackedForEvent(eBroadcastBitStateChanged)) |
| 1424 | { |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1425 | if (new_state == eStateDetached) |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1426 | { |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1427 | if (log) |
| 1428 | log->Printf("Process::SetPublicState (%s) -- unlocking run lock for detach", StateAsCString(new_state)); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1429 | m_public_run_lock.SetStopped(); |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1430 | } |
| 1431 | else |
| 1432 | { |
| 1433 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1434 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1435 | if ((old_state_is_stopped != new_state_is_stopped)) |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1436 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1437 | if (new_state_is_stopped && !restarted) |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1438 | { |
| 1439 | if (log) |
| 1440 | log->Printf("Process::SetPublicState (%s) -- unlocking run lock", StateAsCString(new_state)); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1441 | m_public_run_lock.SetStopped(); |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1442 | } |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1443 | } |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1444 | } |
| 1445 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1448 | Error |
| 1449 | Process::Resume () |
| 1450 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1451 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1452 | if (log) |
| 1453 | log->Printf("Process::Resume -- locking run lock"); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1454 | if (!m_public_run_lock.TrySetRunning()) |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1455 | { |
| 1456 | Error error("Resume request failed - process still running."); |
| 1457 | if (log) |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1458 | log->Printf ("Process::Resume: -- TrySetRunning failed, not resuming."); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1459 | return error; |
| 1460 | } |
| 1461 | return PrivateResume(); |
| 1462 | } |
| 1463 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1464 | StateType |
| 1465 | Process::GetPrivateState () |
| 1466 | { |
| 1467 | return m_private_state.GetValue(); |
| 1468 | } |
| 1469 | |
| 1470 | void |
| 1471 | Process::SetPrivateState (StateType new_state) |
| 1472 | { |
Greg Clayton | fb8b37a | 2014-07-14 23:09:29 +0000 | [diff] [blame] | 1473 | if (m_finalize_called) |
| 1474 | return; |
| 1475 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1476 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1477 | bool state_changed = false; |
| 1478 | |
| 1479 | if (log) |
| 1480 | log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state)); |
| 1481 | |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1482 | Mutex::Locker thread_locker(m_thread_list.GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1483 | Mutex::Locker locker(m_private_state.GetMutex()); |
| 1484 | |
| 1485 | const StateType old_state = m_private_state.GetValueNoLock (); |
| 1486 | state_changed = old_state != new_state; |
Ed Maste | c29693f | 2013-07-02 16:35:47 +0000 | [diff] [blame] | 1487 | |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1488 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1489 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1490 | if (old_state_is_stopped != new_state_is_stopped) |
| 1491 | { |
| 1492 | if (new_state_is_stopped) |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1493 | m_private_run_lock.SetStopped(); |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1494 | else |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1495 | m_private_run_lock.SetRunning(); |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1496 | } |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1497 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1498 | if (state_changed) |
| 1499 | { |
| 1500 | m_private_state.SetValueNoLock (new_state); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1501 | if (StateIsStoppedState(new_state, false)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1502 | { |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1503 | // Note, this currently assumes that all threads in the list |
| 1504 | // stop when the process stops. In the future we will want to |
| 1505 | // support a debugging model where some threads continue to run |
| 1506 | // while others are stopped. When that happens we will either need |
| 1507 | // a way for the thread list to identify which threads are stopping |
| 1508 | // or create a special thread list containing only threads which |
| 1509 | // actually stopped. |
| 1510 | // |
| 1511 | // The process plugin is responsible for managing the actual |
| 1512 | // behavior of the threads and should have stopped any threads |
| 1513 | // that are going to stop before we get here. |
| 1514 | m_thread_list.DidStop(); |
| 1515 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1516 | m_mod_id.BumpStopID(); |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1517 | m_memory_cache.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1518 | if (log) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1519 | log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_mod_id.GetStopID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1520 | } |
| 1521 | // Use our target to get a shared pointer to ourselves... |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 1522 | if (m_finalize_called && PrivateStateThreadIsValid() == false) |
| 1523 | BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state)); |
| 1524 | else |
| 1525 | m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1526 | } |
| 1527 | else |
| 1528 | { |
| 1529 | if (log) |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1530 | log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 1534 | void |
| 1535 | Process::SetRunningUserExpression (bool on) |
| 1536 | { |
| 1537 | m_mod_id.SetRunningUserExpression (on); |
| 1538 | } |
| 1539 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1540 | addr_t |
| 1541 | Process::GetImageInfoAddress() |
| 1542 | { |
| 1543 | return LLDB_INVALID_ADDRESS; |
| 1544 | } |
| 1545 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1546 | //---------------------------------------------------------------------- |
| 1547 | // LoadImage |
| 1548 | // |
| 1549 | // This function provides a default implementation that works for most |
| 1550 | // unix variants. Any Process subclasses that need to do shared library |
| 1551 | // loading differently should override LoadImage and UnloadImage and |
| 1552 | // do what is needed. |
| 1553 | //---------------------------------------------------------------------- |
| 1554 | uint32_t |
| 1555 | Process::LoadImage (const FileSpec &image_spec, Error &error) |
| 1556 | { |
Greg Clayton | ac7a3db | 2012-04-18 00:05:19 +0000 | [diff] [blame] | 1557 | char path[PATH_MAX]; |
| 1558 | image_spec.GetPath(path, sizeof(path)); |
| 1559 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1560 | DynamicLoader *loader = GetDynamicLoader(); |
| 1561 | if (loader) |
| 1562 | { |
| 1563 | error = loader->CanLoadImage(); |
| 1564 | if (error.Fail()) |
| 1565 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1566 | } |
| 1567 | |
| 1568 | if (error.Success()) |
| 1569 | { |
| 1570 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1571 | |
| 1572 | if (thread_sp) |
| 1573 | { |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 1574 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1575 | |
| 1576 | if (frame_sp) |
| 1577 | { |
| 1578 | ExecutionContext exe_ctx; |
| 1579 | frame_sp->CalculateExecutionContext (exe_ctx); |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1580 | EvaluateExpressionOptions expr_options; |
| 1581 | expr_options.SetUnwindOnError(true); |
| 1582 | expr_options.SetIgnoreBreakpoints(true); |
| 1583 | expr_options.SetExecutionPolicy(eExecutionPolicyAlways); |
Jim Ingham | 4ac0443 | 2014-07-19 01:09:16 +0000 | [diff] [blame] | 1584 | expr_options.SetResultIsInternal(true); |
| 1585 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1586 | StreamString expr; |
Jim Ingham | 6971b86 | 2014-07-19 00:37:06 +0000 | [diff] [blame] | 1587 | expr.Printf(R"( |
| 1588 | struct __lldb_dlopen_result { void *image_ptr; const char *error_str; } the_result; |
| 1589 | the_result.image_ptr = dlopen ("%s", 2); |
| 1590 | if (the_result.image_ptr == (void *) 0x0) |
| 1591 | { |
| 1592 | the_result.error_str = dlerror(); |
| 1593 | } |
| 1594 | else |
| 1595 | { |
| 1596 | the_result.error_str = (const char *) 0x0; |
| 1597 | } |
| 1598 | the_result; |
| 1599 | )", |
| 1600 | path); |
| 1601 | const char *prefix = R"( |
| 1602 | extern "C" void* dlopen (const char *path, int mode); |
| 1603 | extern "C" const char *dlerror (void); |
| 1604 | )"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1605 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1606 | Error expr_error; |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1607 | ClangUserExpression::Evaluate (exe_ctx, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1608 | expr_options, |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1609 | expr.GetData(), |
| 1610 | prefix, |
| 1611 | result_valobj_sp, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1612 | expr_error); |
| 1613 | if (expr_error.Success()) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1614 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1615 | error = result_valobj_sp->GetError(); |
| 1616 | if (error.Success()) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1617 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1618 | Scalar scalar; |
Jim Ingham | 6971b86 | 2014-07-19 00:37:06 +0000 | [diff] [blame] | 1619 | ValueObjectSP image_ptr_sp = result_valobj_sp->GetChildAtIndex(0, true); |
| 1620 | if (image_ptr_sp && image_ptr_sp->ResolveValue (scalar)) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1621 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1622 | addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 1623 | if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS) |
| 1624 | { |
| 1625 | uint32_t image_token = m_image_tokens.size(); |
| 1626 | m_image_tokens.push_back (image_ptr); |
| 1627 | return image_token; |
| 1628 | } |
Jim Ingham | 3ac7cf3 | 2014-07-17 18:55:25 +0000 | [diff] [blame] | 1629 | else if (image_ptr == 0) |
| 1630 | { |
Jim Ingham | 6971b86 | 2014-07-19 00:37:06 +0000 | [diff] [blame] | 1631 | ValueObjectSP error_str_sp = result_valobj_sp->GetChildAtIndex(1, true); |
| 1632 | if (error_str_sp) |
Jim Ingham | 3ac7cf3 | 2014-07-17 18:55:25 +0000 | [diff] [blame] | 1633 | { |
Jim Ingham | 6971b86 | 2014-07-19 00:37:06 +0000 | [diff] [blame] | 1634 | if (error_str_sp->IsCStringContainer(true)) |
Jim Ingham | 3ac7cf3 | 2014-07-17 18:55:25 +0000 | [diff] [blame] | 1635 | { |
Jim Ingham | cf97379 | 2014-07-17 21:53:48 +0000 | [diff] [blame] | 1636 | StreamString s; |
Jim Ingham | 6971b86 | 2014-07-19 00:37:06 +0000 | [diff] [blame] | 1637 | size_t num_chars = error_str_sp->ReadPointedString (s, error); |
Jim Ingham | 3ac7cf3 | 2014-07-17 18:55:25 +0000 | [diff] [blame] | 1638 | if (error.Success() && num_chars > 0) |
| 1639 | { |
| 1640 | error.Clear(); |
Jim Ingham | 6971b86 | 2014-07-19 00:37:06 +0000 | [diff] [blame] | 1641 | error.SetErrorStringWithFormat("dlopen error: %s", s.GetData()); |
Jim Ingham | 3ac7cf3 | 2014-07-17 18:55:25 +0000 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | } |
| 1645 | } |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1646 | } |
| 1647 | } |
| 1648 | } |
Jim Ingham | 6c9ed91 | 2014-04-03 01:26:14 +0000 | [diff] [blame] | 1649 | else |
| 1650 | error = expr_error; |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1651 | } |
| 1652 | } |
| 1653 | } |
Greg Clayton | ac7a3db | 2012-04-18 00:05:19 +0000 | [diff] [blame] | 1654 | if (!error.AsCString()) |
| 1655 | error.SetErrorStringWithFormat("unable to load '%s'", path); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1656 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1657 | } |
| 1658 | |
| 1659 | //---------------------------------------------------------------------- |
| 1660 | // UnloadImage |
| 1661 | // |
| 1662 | // This function provides a default implementation that works for most |
| 1663 | // unix variants. Any Process subclasses that need to do shared library |
| 1664 | // loading differently should override LoadImage and UnloadImage and |
| 1665 | // do what is needed. |
| 1666 | //---------------------------------------------------------------------- |
| 1667 | Error |
| 1668 | Process::UnloadImage (uint32_t image_token) |
| 1669 | { |
| 1670 | Error error; |
| 1671 | if (image_token < m_image_tokens.size()) |
| 1672 | { |
| 1673 | const addr_t image_addr = m_image_tokens[image_token]; |
| 1674 | if (image_addr == LLDB_INVALID_ADDRESS) |
| 1675 | { |
| 1676 | error.SetErrorString("image already unloaded"); |
| 1677 | } |
| 1678 | else |
| 1679 | { |
| 1680 | DynamicLoader *loader = GetDynamicLoader(); |
| 1681 | if (loader) |
| 1682 | error = loader->CanLoadImage(); |
| 1683 | |
| 1684 | if (error.Success()) |
| 1685 | { |
| 1686 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1687 | |
| 1688 | if (thread_sp) |
| 1689 | { |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 1690 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1691 | |
| 1692 | if (frame_sp) |
| 1693 | { |
| 1694 | ExecutionContext exe_ctx; |
| 1695 | frame_sp->CalculateExecutionContext (exe_ctx); |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1696 | EvaluateExpressionOptions expr_options; |
| 1697 | expr_options.SetUnwindOnError(true); |
| 1698 | expr_options.SetIgnoreBreakpoints(true); |
| 1699 | expr_options.SetExecutionPolicy(eExecutionPolicyAlways); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1700 | StreamString expr; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1701 | expr.Printf("dlclose ((void *)0x%" PRIx64 ")", image_addr); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1702 | const char *prefix = "extern \"C\" int dlclose(void* handle);\n"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1703 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1704 | Error expr_error; |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1705 | ClangUserExpression::Evaluate (exe_ctx, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1706 | expr_options, |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1707 | expr.GetData(), |
| 1708 | prefix, |
| 1709 | result_valobj_sp, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1710 | expr_error); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1711 | if (result_valobj_sp->GetError().Success()) |
| 1712 | { |
| 1713 | Scalar scalar; |
Jim Ingham | 6035b67 | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 1714 | if (result_valobj_sp->ResolveValue (scalar)) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1715 | { |
| 1716 | if (scalar.UInt(1)) |
| 1717 | { |
| 1718 | error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData()); |
| 1719 | } |
| 1720 | else |
| 1721 | { |
| 1722 | m_image_tokens[image_token] = LLDB_INVALID_ADDRESS; |
| 1723 | } |
| 1724 | } |
| 1725 | } |
| 1726 | else |
| 1727 | { |
| 1728 | error = result_valobj_sp->GetError(); |
| 1729 | } |
| 1730 | } |
| 1731 | } |
| 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | else |
| 1736 | { |
| 1737 | error.SetErrorString("invalid image token"); |
| 1738 | } |
| 1739 | return error; |
| 1740 | } |
| 1741 | |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 1742 | const lldb::ABISP & |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1743 | Process::GetABI() |
| 1744 | { |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 1745 | if (!m_abi_sp) |
| 1746 | m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture()); |
| 1747 | return m_abi_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1750 | LanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1751 | Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1752 | { |
| 1753 | LanguageRuntimeCollection::iterator pos; |
| 1754 | pos = m_language_runtimes.find (language); |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1755 | if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second)) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1756 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1757 | lldb::LanguageRuntimeSP runtime_sp(LanguageRuntime::FindPlugin(this, language)); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1758 | |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1759 | m_language_runtimes[language] = runtime_sp; |
| 1760 | return runtime_sp.get(); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1761 | } |
| 1762 | else |
| 1763 | return (*pos).second.get(); |
| 1764 | } |
| 1765 | |
| 1766 | CPPLanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1767 | Process::GetCPPLanguageRuntime (bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1768 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1769 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1770 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus) |
| 1771 | return static_cast<CPPLanguageRuntime *> (runtime); |
| 1772 | return NULL; |
| 1773 | } |
| 1774 | |
| 1775 | ObjCLanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1776 | Process::GetObjCLanguageRuntime (bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1777 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1778 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC, retry_if_null); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1779 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC) |
| 1780 | return static_cast<ObjCLanguageRuntime *> (runtime); |
| 1781 | return NULL; |
| 1782 | } |
| 1783 | |
Enrico Granata | fd4c84e | 2012-05-21 16:51:35 +0000 | [diff] [blame] | 1784 | bool |
| 1785 | Process::IsPossibleDynamicValue (ValueObject& in_value) |
| 1786 | { |
| 1787 | if (in_value.IsDynamic()) |
| 1788 | return false; |
| 1789 | LanguageType known_type = in_value.GetObjectRuntimeLanguage(); |
| 1790 | |
| 1791 | if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC) |
| 1792 | { |
| 1793 | LanguageRuntime *runtime = GetLanguageRuntime (known_type); |
| 1794 | return runtime ? runtime->CouldHaveDynamicValue(in_value) : false; |
| 1795 | } |
| 1796 | |
| 1797 | LanguageRuntime *cpp_runtime = GetLanguageRuntime (eLanguageTypeC_plus_plus); |
| 1798 | if (cpp_runtime && cpp_runtime->CouldHaveDynamicValue(in_value)) |
| 1799 | return true; |
| 1800 | |
| 1801 | LanguageRuntime *objc_runtime = GetLanguageRuntime (eLanguageTypeObjC); |
| 1802 | return objc_runtime ? objc_runtime->CouldHaveDynamicValue(in_value) : false; |
| 1803 | } |
| 1804 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1805 | BreakpointSiteList & |
| 1806 | Process::GetBreakpointSiteList() |
| 1807 | { |
| 1808 | return m_breakpoint_site_list; |
| 1809 | } |
| 1810 | |
| 1811 | const BreakpointSiteList & |
| 1812 | Process::GetBreakpointSiteList() const |
| 1813 | { |
| 1814 | return m_breakpoint_site_list; |
| 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | void |
| 1819 | Process::DisableAllBreakpointSites () |
| 1820 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 1821 | m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void { |
| 1822 | // bp_site->SetEnabled(true); |
| 1823 | DisableBreakpointSite(bp_site); |
| 1824 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | Error |
| 1828 | Process::ClearBreakpointSiteByID (lldb::user_id_t break_id) |
| 1829 | { |
| 1830 | Error error (DisableBreakpointSiteByID (break_id)); |
| 1831 | |
| 1832 | if (error.Success()) |
| 1833 | m_breakpoint_site_list.Remove(break_id); |
| 1834 | |
| 1835 | return error; |
| 1836 | } |
| 1837 | |
| 1838 | Error |
| 1839 | Process::DisableBreakpointSiteByID (lldb::user_id_t break_id) |
| 1840 | { |
| 1841 | Error error; |
| 1842 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 1843 | if (bp_site_sp) |
| 1844 | { |
| 1845 | if (bp_site_sp->IsEnabled()) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 1846 | error = DisableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1847 | } |
| 1848 | else |
| 1849 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1850 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | return error; |
| 1854 | } |
| 1855 | |
| 1856 | Error |
| 1857 | Process::EnableBreakpointSiteByID (lldb::user_id_t break_id) |
| 1858 | { |
| 1859 | Error error; |
| 1860 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 1861 | if (bp_site_sp) |
| 1862 | { |
| 1863 | if (!bp_site_sp->IsEnabled()) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 1864 | error = EnableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1865 | } |
| 1866 | else |
| 1867 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1868 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1869 | } |
| 1870 | return error; |
| 1871 | } |
| 1872 | |
Stephen Wilson | 50bd94f | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 1873 | lldb::break_id_t |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 1874 | Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1875 | { |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1876 | addr_t load_addr = LLDB_INVALID_ADDRESS; |
| 1877 | |
| 1878 | bool show_error = true; |
| 1879 | switch (GetState()) |
| 1880 | { |
| 1881 | case eStateInvalid: |
| 1882 | case eStateUnloaded: |
| 1883 | case eStateConnected: |
| 1884 | case eStateAttaching: |
| 1885 | case eStateLaunching: |
| 1886 | case eStateDetached: |
| 1887 | case eStateExited: |
| 1888 | show_error = false; |
| 1889 | break; |
| 1890 | |
| 1891 | case eStateStopped: |
| 1892 | case eStateRunning: |
| 1893 | case eStateStepping: |
| 1894 | case eStateCrashed: |
| 1895 | case eStateSuspended: |
| 1896 | show_error = IsAlive(); |
| 1897 | break; |
| 1898 | } |
| 1899 | |
| 1900 | // Reset the IsIndirect flag here, in case the location changes from |
| 1901 | // pointing to a indirect symbol to a regular symbol. |
| 1902 | owner->SetIsIndirect (false); |
| 1903 | |
| 1904 | if (owner->ShouldResolveIndirectFunctions()) |
| 1905 | { |
| 1906 | Symbol *symbol = owner->GetAddress().CalculateSymbolContextSymbol(); |
| 1907 | if (symbol && symbol->IsIndirect()) |
| 1908 | { |
| 1909 | Error error; |
| 1910 | load_addr = ResolveIndirectFunction (&symbol->GetAddress(), error); |
| 1911 | if (!error.Success() && show_error) |
| 1912 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1913 | m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to resolve indirect function at 0x%" PRIx64 " for breakpoint %i.%i: %s\n", |
| 1914 | symbol->GetAddress().GetLoadAddress(&m_target), |
| 1915 | owner->GetBreakpoint().GetID(), |
| 1916 | owner->GetID(), |
Sylvestre Ledru | f610289 | 2014-08-11 18:06:28 +0000 | [diff] [blame] | 1917 | error.AsCString() ? error.AsCString() : "unknown error"); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1918 | return LLDB_INVALID_BREAK_ID; |
| 1919 | } |
| 1920 | Address resolved_address(load_addr); |
| 1921 | load_addr = resolved_address.GetOpcodeLoadAddress (&m_target); |
| 1922 | owner->SetIsIndirect(true); |
| 1923 | } |
| 1924 | else |
| 1925 | load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target); |
| 1926 | } |
| 1927 | else |
| 1928 | load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target); |
| 1929 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1930 | if (load_addr != LLDB_INVALID_ADDRESS) |
| 1931 | { |
| 1932 | BreakpointSiteSP bp_site_sp; |
| 1933 | |
| 1934 | // Look up this breakpoint site. If it exists, then add this new owner, otherwise |
| 1935 | // create a new breakpoint site and add it. |
| 1936 | |
| 1937 | bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr); |
| 1938 | |
| 1939 | if (bp_site_sp) |
| 1940 | { |
| 1941 | bp_site_sp->AddOwner (owner); |
| 1942 | owner->SetBreakpointSite (bp_site_sp); |
| 1943 | return bp_site_sp->GetID(); |
| 1944 | } |
| 1945 | else |
| 1946 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1947 | bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, use_hardware)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1948 | if (bp_site_sp) |
| 1949 | { |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 1950 | Error error = EnableBreakpointSite (bp_site_sp.get()); |
| 1951 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1952 | { |
| 1953 | owner->SetBreakpointSite (bp_site_sp); |
| 1954 | return m_breakpoint_site_list.Add (bp_site_sp); |
| 1955 | } |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 1956 | else |
| 1957 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1958 | if (show_error) |
| 1959 | { |
| 1960 | // Report error for setting breakpoint... |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1961 | m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to set breakpoint site at 0x%" PRIx64 " for breakpoint %i.%i: %s\n", |
| 1962 | load_addr, |
| 1963 | owner->GetBreakpoint().GetID(), |
| 1964 | owner->GetID(), |
Sylvestre Ledru | f610289 | 2014-08-11 18:06:28 +0000 | [diff] [blame] | 1965 | error.AsCString() ? error.AsCString() : "unknown error"); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1966 | } |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 1967 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1968 | } |
| 1969 | } |
| 1970 | } |
| 1971 | // We failed to enable the breakpoint |
| 1972 | return LLDB_INVALID_BREAK_ID; |
| 1973 | |
| 1974 | } |
| 1975 | |
| 1976 | void |
| 1977 | Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp) |
| 1978 | { |
| 1979 | uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id); |
| 1980 | if (num_owners == 0) |
| 1981 | { |
Jim Ingham | f1ff3bb | 2013-04-06 00:16:39 +0000 | [diff] [blame] | 1982 | // Don't try to disable the site if we don't have a live process anymore. |
| 1983 | if (IsAlive()) |
| 1984 | DisableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1985 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | |
| 1990 | size_t |
| 1991 | Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const |
| 1992 | { |
| 1993 | size_t bytes_removed = 0; |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 1994 | BreakpointSiteList bp_sites_in_range; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1995 | |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 1996 | if (m_breakpoint_site_list.FindInRange (bp_addr, bp_addr + size, bp_sites_in_range)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1997 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 1998 | bp_sites_in_range.ForEach([bp_addr, size, buf, &bytes_removed](BreakpointSite *bp_site) -> void { |
| 1999 | if (bp_site->GetType() == BreakpointSite::eSoftware) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2000 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2001 | addr_t intersect_addr; |
| 2002 | size_t intersect_size; |
| 2003 | size_t opcode_offset; |
| 2004 | if (bp_site->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset)) |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2005 | { |
| 2006 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 2007 | assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size); |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2008 | assert(opcode_offset + intersect_size <= bp_site->GetByteSize()); |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2009 | size_t buf_offset = intersect_addr - bp_addr; |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2010 | ::memcpy(buf + buf_offset, bp_site->GetSavedOpcodeBytes() + opcode_offset, intersect_size); |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2011 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2012 | } |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2013 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2014 | } |
| 2015 | return bytes_removed; |
| 2016 | } |
| 2017 | |
| 2018 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 2019 | |
| 2020 | size_t |
| 2021 | Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site) |
| 2022 | { |
| 2023 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 2024 | if (platform_sp) |
| 2025 | return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site); |
| 2026 | return 0; |
| 2027 | } |
| 2028 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2029 | Error |
| 2030 | Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 2031 | { |
| 2032 | Error error; |
| 2033 | assert (bp_site != NULL); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2034 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2035 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
| 2036 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2037 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64, bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2038 | if (bp_site->IsEnabled()) |
| 2039 | { |
| 2040 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2041 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- already enabled", bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2042 | return error; |
| 2043 | } |
| 2044 | |
| 2045 | if (bp_addr == LLDB_INVALID_ADDRESS) |
| 2046 | { |
| 2047 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 2048 | return error; |
| 2049 | } |
| 2050 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 2051 | // trap for the breakpoint site |
| 2052 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 2053 | |
| 2054 | if (bp_opcode_size == 0) |
| 2055 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2056 | error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%" PRIx64, bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2057 | } |
| 2058 | else |
| 2059 | { |
| 2060 | const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 2061 | |
| 2062 | if (bp_opcode_bytes == NULL) |
| 2063 | { |
| 2064 | error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 2065 | return error; |
| 2066 | } |
| 2067 | |
| 2068 | // Save the original opcode by reading it |
| 2069 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size) |
| 2070 | { |
| 2071 | // Write a software breakpoint in place of the original opcode |
| 2072 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 2073 | { |
| 2074 | uint8_t verify_bp_opcode_bytes[64]; |
| 2075 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 2076 | { |
| 2077 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0) |
| 2078 | { |
| 2079 | bp_site->SetEnabled(true); |
| 2080 | bp_site->SetType (BreakpointSite::eSoftware); |
| 2081 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2082 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2083 | bp_site->GetID(), |
| 2084 | (uint64_t)bp_addr); |
| 2085 | } |
| 2086 | else |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 2087 | error.SetErrorString("failed to verify the breakpoint trap in memory."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2088 | } |
| 2089 | else |
| 2090 | error.SetErrorString("Unable to read memory to verify breakpoint trap."); |
| 2091 | } |
| 2092 | else |
| 2093 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 2094 | } |
| 2095 | else |
| 2096 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 2097 | } |
Stephen Wilson | 78a4feb | 2011-01-12 04:20:03 +0000 | [diff] [blame] | 2098 | if (log && error.Fail()) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2099 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2100 | bp_site->GetID(), |
| 2101 | (uint64_t)bp_addr, |
| 2102 | error.AsCString()); |
| 2103 | return error; |
| 2104 | } |
| 2105 | |
| 2106 | Error |
| 2107 | Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 2108 | { |
| 2109 | Error error; |
| 2110 | assert (bp_site != NULL); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2111 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2112 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 2113 | lldb::user_id_t breakID = bp_site->GetID(); |
| 2114 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2115 | log->Printf ("Process::DisableSoftwareBreakpoint (breakID = %" PRIu64 ") addr = 0x%" PRIx64, breakID, (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2116 | |
| 2117 | if (bp_site->IsHardware()) |
| 2118 | { |
| 2119 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 2120 | } |
| 2121 | else if (bp_site->IsEnabled()) |
| 2122 | { |
| 2123 | const size_t break_op_size = bp_site->GetByteSize(); |
| 2124 | const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes(); |
| 2125 | if (break_op_size > 0) |
| 2126 | { |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 2127 | // Clear a software breakpoint instruction |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2128 | uint8_t curr_break_op[8]; |
Stephen Wilson | 4ab4768 | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 2129 | assert (break_op_size <= sizeof(curr_break_op)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2130 | bool break_op_found = false; |
| 2131 | |
| 2132 | // Read the breakpoint opcode |
| 2133 | if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size) |
| 2134 | { |
| 2135 | bool verify = false; |
| 2136 | // Make sure we have the a breakpoint opcode exists at this address |
| 2137 | if (::memcmp (curr_break_op, break_op, break_op_size) == 0) |
| 2138 | { |
| 2139 | break_op_found = true; |
| 2140 | // We found a valid breakpoint opcode at this address, now restore |
| 2141 | // the saved opcode. |
| 2142 | if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size) |
| 2143 | { |
| 2144 | verify = true; |
| 2145 | } |
| 2146 | else |
| 2147 | error.SetErrorString("Memory write failed when restoring original opcode."); |
| 2148 | } |
| 2149 | else |
| 2150 | { |
| 2151 | error.SetErrorString("Original breakpoint trap is no longer in memory."); |
| 2152 | // Set verify to true and so we can check if the original opcode has already been restored |
| 2153 | verify = true; |
| 2154 | } |
| 2155 | |
| 2156 | if (verify) |
| 2157 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2158 | uint8_t verify_opcode[8]; |
Stephen Wilson | 4ab4768 | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 2159 | assert (break_op_size < sizeof(verify_opcode)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2160 | // Verify that our original opcode made it back to the inferior |
| 2161 | if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size) |
| 2162 | { |
| 2163 | // compare the memory we just read with the original opcode |
| 2164 | if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0) |
| 2165 | { |
| 2166 | // SUCCESS |
| 2167 | bp_site->SetEnabled(false); |
| 2168 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2169 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2170 | return error; |
| 2171 | } |
| 2172 | else |
| 2173 | { |
| 2174 | if (break_op_found) |
| 2175 | error.SetErrorString("Failed to restore original opcode."); |
| 2176 | } |
| 2177 | } |
| 2178 | else |
| 2179 | error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored."); |
| 2180 | } |
| 2181 | } |
| 2182 | else |
| 2183 | error.SetErrorString("Unable to read memory that should contain the breakpoint trap."); |
| 2184 | } |
| 2185 | } |
| 2186 | else |
| 2187 | { |
| 2188 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2189 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- already disabled", bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2190 | return error; |
| 2191 | } |
| 2192 | |
| 2193 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2194 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2195 | bp_site->GetID(), |
| 2196 | (uint64_t)bp_addr, |
| 2197 | error.AsCString()); |
| 2198 | return error; |
| 2199 | |
| 2200 | } |
| 2201 | |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2202 | // Uncomment to verify memory caching works after making changes to caching code |
| 2203 | //#define VERIFY_MEMORY_READS |
| 2204 | |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2205 | size_t |
| 2206 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 2207 | { |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 2208 | error.Clear(); |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2209 | if (!GetDisableMemoryCache()) |
| 2210 | { |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2211 | #if defined (VERIFY_MEMORY_READS) |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2212 | // Memory caching is enabled, with debug verification |
| 2213 | |
| 2214 | if (buf && size) |
| 2215 | { |
| 2216 | // Uncomment the line below to make sure memory caching is working. |
| 2217 | // I ran this through the test suite and got no assertions, so I am |
| 2218 | // pretty confident this is working well. If any changes are made to |
| 2219 | // memory caching, uncomment the line below and test your changes! |
| 2220 | |
| 2221 | // Verify all memory reads by using the cache first, then redundantly |
| 2222 | // reading the same memory from the inferior and comparing to make sure |
| 2223 | // everything is exactly the same. |
| 2224 | std::string verify_buf (size, '\0'); |
| 2225 | assert (verify_buf.size() == size); |
| 2226 | const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error); |
| 2227 | Error verify_error; |
| 2228 | const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error); |
| 2229 | assert (cache_bytes_read == verify_bytes_read); |
| 2230 | assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0); |
| 2231 | assert (verify_error.Success() == error.Success()); |
| 2232 | return cache_bytes_read; |
| 2233 | } |
| 2234 | return 0; |
| 2235 | #else // !defined(VERIFY_MEMORY_READS) |
| 2236 | // Memory caching is enabled, without debug verification |
| 2237 | |
| 2238 | return m_memory_cache.Read (addr, buf, size, error); |
| 2239 | #endif // defined (VERIFY_MEMORY_READS) |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2240 | } |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2241 | else |
| 2242 | { |
| 2243 | // Memory caching is disabled |
| 2244 | |
| 2245 | return ReadMemoryFromInferior (addr, buf, size, error); |
| 2246 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2247 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2248 | |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2249 | size_t |
| 2250 | Process::ReadCStringFromMemory (addr_t addr, std::string &out_str, Error &error) |
| 2251 | { |
Greg Clayton | de87c0f | 2012-05-19 00:18:00 +0000 | [diff] [blame] | 2252 | char buf[256]; |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2253 | out_str.clear(); |
| 2254 | addr_t curr_addr = addr; |
| 2255 | while (1) |
| 2256 | { |
| 2257 | size_t length = ReadCStringFromMemory (curr_addr, buf, sizeof(buf), error); |
| 2258 | if (length == 0) |
| 2259 | break; |
| 2260 | out_str.append(buf, length); |
| 2261 | // If we got "length - 1" bytes, we didn't get the whole C string, we |
| 2262 | // need to read some more characters |
| 2263 | if (length == sizeof(buf) - 1) |
| 2264 | curr_addr += length; |
| 2265 | else |
| 2266 | break; |
| 2267 | } |
| 2268 | return out_str.size(); |
| 2269 | } |
| 2270 | |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2271 | |
| 2272 | size_t |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2273 | Process::ReadStringFromMemory (addr_t addr, char *dst, size_t max_bytes, Error &error, |
| 2274 | size_t type_width) |
| 2275 | { |
| 2276 | size_t total_bytes_read = 0; |
| 2277 | if (dst && max_bytes && type_width && max_bytes >= type_width) |
| 2278 | { |
| 2279 | // Ensure a null terminator independent of the number of bytes that is read. |
| 2280 | memset (dst, 0, max_bytes); |
| 2281 | size_t bytes_left = max_bytes - type_width; |
| 2282 | |
| 2283 | const char terminator[4] = {'\0', '\0', '\0', '\0'}; |
| 2284 | assert(sizeof(terminator) >= type_width && |
| 2285 | "Attempting to validate a string with more than 4 bytes per character!"); |
| 2286 | |
| 2287 | addr_t curr_addr = addr; |
| 2288 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2289 | char *curr_dst = dst; |
| 2290 | |
| 2291 | error.Clear(); |
| 2292 | while (bytes_left > 0 && error.Success()) |
| 2293 | { |
| 2294 | addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); |
| 2295 | addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2296 | size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error); |
| 2297 | |
| 2298 | if (bytes_read == 0) |
| 2299 | break; |
| 2300 | |
| 2301 | // Search for a null terminator of correct size and alignment in bytes_read |
| 2302 | size_t aligned_start = total_bytes_read - total_bytes_read % type_width; |
| 2303 | for (size_t i = aligned_start; i + type_width <= total_bytes_read + bytes_read; i += type_width) |
| 2304 | if (::strncmp(&dst[i], terminator, type_width) == 0) |
| 2305 | { |
| 2306 | error.Clear(); |
| 2307 | return i; |
| 2308 | } |
| 2309 | |
| 2310 | total_bytes_read += bytes_read; |
| 2311 | curr_dst += bytes_read; |
| 2312 | curr_addr += bytes_read; |
| 2313 | bytes_left -= bytes_read; |
| 2314 | } |
| 2315 | } |
| 2316 | else |
| 2317 | { |
| 2318 | if (max_bytes) |
| 2319 | error.SetErrorString("invalid arguments"); |
| 2320 | } |
| 2321 | return total_bytes_read; |
| 2322 | } |
| 2323 | |
| 2324 | // Deprecated in favor of ReadStringFromMemory which has wchar support and correct code to find |
| 2325 | // null terminators. |
| 2326 | size_t |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2327 | Process::ReadCStringFromMemory (addr_t addr, char *dst, size_t dst_max_len, Error &result_error) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2328 | { |
| 2329 | size_t total_cstr_len = 0; |
| 2330 | if (dst && dst_max_len) |
| 2331 | { |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2332 | result_error.Clear(); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2333 | // NULL out everything just to be safe |
| 2334 | memset (dst, 0, dst_max_len); |
| 2335 | Error error; |
| 2336 | addr_t curr_addr = addr; |
| 2337 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2338 | size_t bytes_left = dst_max_len - 1; |
| 2339 | char *curr_dst = dst; |
| 2340 | |
| 2341 | while (bytes_left > 0) |
| 2342 | { |
| 2343 | addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); |
| 2344 | addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2345 | size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error); |
| 2346 | |
| 2347 | if (bytes_read == 0) |
| 2348 | { |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2349 | result_error = error; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2350 | dst[total_cstr_len] = '\0'; |
| 2351 | break; |
| 2352 | } |
| 2353 | const size_t len = strlen(curr_dst); |
| 2354 | |
| 2355 | total_cstr_len += len; |
| 2356 | |
| 2357 | if (len < bytes_to_read) |
| 2358 | break; |
| 2359 | |
| 2360 | curr_dst += bytes_read; |
| 2361 | curr_addr += bytes_read; |
| 2362 | bytes_left -= bytes_read; |
| 2363 | } |
| 2364 | } |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2365 | else |
| 2366 | { |
| 2367 | if (dst == NULL) |
| 2368 | result_error.SetErrorString("invalid arguments"); |
| 2369 | else |
| 2370 | result_error.Clear(); |
| 2371 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2372 | return total_cstr_len; |
| 2373 | } |
| 2374 | |
| 2375 | size_t |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2376 | Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error) |
| 2377 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2378 | if (buf == NULL || size == 0) |
| 2379 | return 0; |
| 2380 | |
| 2381 | size_t bytes_read = 0; |
| 2382 | uint8_t *bytes = (uint8_t *)buf; |
| 2383 | |
| 2384 | while (bytes_read < size) |
| 2385 | { |
| 2386 | const size_t curr_size = size - bytes_read; |
| 2387 | const size_t curr_bytes_read = DoReadMemory (addr + bytes_read, |
| 2388 | bytes + bytes_read, |
| 2389 | curr_size, |
| 2390 | error); |
| 2391 | bytes_read += curr_bytes_read; |
| 2392 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 2393 | break; |
| 2394 | } |
| 2395 | |
| 2396 | // Replace any software breakpoint opcodes that fall into this range back |
| 2397 | // into "buf" before we return |
| 2398 | if (bytes_read > 0) |
| 2399 | RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf); |
| 2400 | return bytes_read; |
| 2401 | } |
| 2402 | |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2403 | uint64_t |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2404 | Process::ReadUnsignedIntegerFromMemory (lldb::addr_t vm_addr, size_t integer_byte_size, uint64_t fail_value, Error &error) |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2405 | { |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2406 | Scalar scalar; |
| 2407 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error)) |
| 2408 | return scalar.ULongLong(fail_value); |
| 2409 | return fail_value; |
| 2410 | } |
| 2411 | |
| 2412 | addr_t |
| 2413 | Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error) |
| 2414 | { |
| 2415 | Scalar scalar; |
| 2416 | if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error)) |
| 2417 | return scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 2418 | return LLDB_INVALID_ADDRESS; |
| 2419 | } |
| 2420 | |
| 2421 | |
| 2422 | bool |
| 2423 | Process::WritePointerToMemory (lldb::addr_t vm_addr, |
| 2424 | lldb::addr_t ptr_value, |
| 2425 | Error &error) |
| 2426 | { |
| 2427 | Scalar scalar; |
| 2428 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2429 | if (addr_byte_size <= 4) |
| 2430 | scalar = (uint32_t)ptr_value; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2431 | else |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2432 | scalar = ptr_value; |
| 2433 | return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2436 | size_t |
| 2437 | Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error) |
| 2438 | { |
| 2439 | size_t bytes_written = 0; |
| 2440 | const uint8_t *bytes = (const uint8_t *)buf; |
| 2441 | |
| 2442 | while (bytes_written < size) |
| 2443 | { |
| 2444 | const size_t curr_size = size - bytes_written; |
| 2445 | const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written, |
| 2446 | bytes + bytes_written, |
| 2447 | curr_size, |
| 2448 | error); |
| 2449 | bytes_written += curr_bytes_written; |
| 2450 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 2451 | break; |
| 2452 | } |
| 2453 | return bytes_written; |
| 2454 | } |
| 2455 | |
| 2456 | size_t |
| 2457 | Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 2458 | { |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2459 | #if defined (ENABLE_MEMORY_CACHING) |
| 2460 | m_memory_cache.Flush (addr, size); |
| 2461 | #endif |
| 2462 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2463 | if (buf == NULL || size == 0) |
| 2464 | return 0; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2465 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2466 | m_mod_id.BumpMemoryID(); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2467 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2468 | // We need to write any data that would go where any current software traps |
| 2469 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 2470 | // may have placed in our tasks memory. |
| 2471 | |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2472 | BreakpointSiteList bp_sites_in_range; |
| 2473 | |
| 2474 | if (m_breakpoint_site_list.FindInRange (addr, addr + size, bp_sites_in_range)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2475 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2476 | // No breakpoint sites overlap |
| 2477 | if (bp_sites_in_range.IsEmpty()) |
| 2478 | return WriteMemoryPrivate (addr, buf, size, error); |
| 2479 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2480 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2481 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 2482 | uint64_t bytes_written = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2483 | |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2484 | bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, &error](BreakpointSite *bp) -> void { |
| 2485 | |
| 2486 | if (error.Success()) |
| 2487 | { |
| 2488 | addr_t intersect_addr; |
| 2489 | size_t intersect_size; |
| 2490 | size_t opcode_offset; |
| 2491 | const bool intersects = bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset); |
| 2492 | assert(intersects); |
| 2493 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 2494 | assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size); |
| 2495 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 2496 | |
| 2497 | // Check for bytes before this breakpoint |
| 2498 | const addr_t curr_addr = addr + bytes_written; |
| 2499 | if (intersect_addr > curr_addr) |
| 2500 | { |
| 2501 | // There are some bytes before this breakpoint that we need to |
| 2502 | // just write to memory |
| 2503 | size_t curr_size = intersect_addr - curr_addr; |
| 2504 | size_t curr_bytes_written = WriteMemoryPrivate (curr_addr, |
| 2505 | ubuf + bytes_written, |
| 2506 | curr_size, |
| 2507 | error); |
| 2508 | bytes_written += curr_bytes_written; |
| 2509 | if (curr_bytes_written != curr_size) |
| 2510 | { |
| 2511 | // We weren't able to write all of the requested bytes, we |
| 2512 | // are done looping and will return the number of bytes that |
| 2513 | // we have written so far. |
| 2514 | if (error.Success()) |
| 2515 | error.SetErrorToGenericError(); |
| 2516 | } |
| 2517 | } |
| 2518 | // Now write any bytes that would cover up any software breakpoints |
| 2519 | // directly into the breakpoint opcode buffer |
| 2520 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size); |
| 2521 | bytes_written += intersect_size; |
| 2522 | } |
| 2523 | }); |
| 2524 | |
| 2525 | if (bytes_written < size) |
| 2526 | bytes_written += WriteMemoryPrivate (addr + bytes_written, |
| 2527 | ubuf + bytes_written, |
| 2528 | size - bytes_written, |
| 2529 | error); |
| 2530 | } |
| 2531 | } |
| 2532 | else |
| 2533 | { |
| 2534 | return WriteMemoryPrivate (addr, buf, size, error); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | // Write any remaining bytes after the last breakpoint if we have any left |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2538 | return 0; //bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2539 | } |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2540 | |
| 2541 | size_t |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2542 | Process::WriteScalarToMemory (addr_t addr, const Scalar &scalar, size_t byte_size, Error &error) |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2543 | { |
| 2544 | if (byte_size == UINT32_MAX) |
| 2545 | byte_size = scalar.GetByteSize(); |
| 2546 | if (byte_size > 0) |
| 2547 | { |
| 2548 | uint8_t buf[32]; |
| 2549 | const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error); |
| 2550 | if (mem_size > 0) |
| 2551 | return WriteMemory(addr, buf, mem_size, error); |
| 2552 | else |
| 2553 | error.SetErrorString ("failed to get scalar as memory data"); |
| 2554 | } |
| 2555 | else |
| 2556 | { |
| 2557 | error.SetErrorString ("invalid scalar value"); |
| 2558 | } |
| 2559 | return 0; |
| 2560 | } |
| 2561 | |
| 2562 | size_t |
| 2563 | Process::ReadScalarIntegerFromMemory (addr_t addr, |
| 2564 | uint32_t byte_size, |
| 2565 | bool is_signed, |
| 2566 | Scalar &scalar, |
| 2567 | Error &error) |
| 2568 | { |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2569 | uint64_t uval = 0; |
| 2570 | if (byte_size == 0) |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2571 | { |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2572 | error.SetErrorString ("byte size is zero"); |
| 2573 | } |
| 2574 | else if (byte_size & (byte_size - 1)) |
| 2575 | { |
| 2576 | error.SetErrorStringWithFormat ("byte size %u is not a power of 2", byte_size); |
| 2577 | } |
| 2578 | else if (byte_size <= sizeof(uval)) |
| 2579 | { |
| 2580 | const size_t bytes_read = ReadMemory (addr, &uval, byte_size, error); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2581 | if (bytes_read == byte_size) |
| 2582 | { |
| 2583 | DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize()); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2584 | lldb::offset_t offset = 0; |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2585 | if (byte_size <= 4) |
| 2586 | scalar = data.GetMaxU32 (&offset, byte_size); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2587 | else |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2588 | scalar = data.GetMaxU64 (&offset, byte_size); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2589 | if (is_signed) |
| 2590 | scalar.SignExtend(byte_size * 8); |
| 2591 | return bytes_read; |
| 2592 | } |
| 2593 | } |
| 2594 | else |
| 2595 | { |
| 2596 | error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size); |
| 2597 | } |
| 2598 | return 0; |
| 2599 | } |
| 2600 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2601 | #define USE_ALLOCATE_MEMORY_CACHE 1 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2602 | addr_t |
| 2603 | Process::AllocateMemory(size_t size, uint32_t permissions, Error &error) |
| 2604 | { |
Jim Ingham | f72ce3a | 2011-06-20 17:32:44 +0000 | [diff] [blame] | 2605 | if (GetPrivateState() != eStateStopped) |
| 2606 | return LLDB_INVALID_ADDRESS; |
| 2607 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2608 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2609 | return m_allocated_memory_cache.AllocateMemory(size, permissions, error); |
| 2610 | #else |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2611 | addr_t allocated_addr = DoAllocateMemory (size, permissions, error); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2612 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2613 | if (log) |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 2614 | log->Printf("Process::AllocateMemory(size=%" PRIu64 ", permissions=%s) => 0x%16.16" PRIx64 " (m_stop_id = %u m_memory_id = %u)", |
Deepak Panickal | d66b50c | 2013-10-22 12:27:43 +0000 | [diff] [blame] | 2615 | (uint64_t)size, |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2616 | GetPermissionsAsCString (permissions), |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2617 | (uint64_t)allocated_addr, |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2618 | m_mod_id.GetStopID(), |
| 2619 | m_mod_id.GetMemoryID()); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2620 | return allocated_addr; |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2621 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2624 | bool |
| 2625 | Process::CanJIT () |
| 2626 | { |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2627 | if (m_can_jit == eCanJITDontKnow) |
| 2628 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2629 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2630 | Error err; |
| 2631 | |
| 2632 | uint64_t allocated_memory = AllocateMemory(8, |
| 2633 | ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable, |
| 2634 | err); |
| 2635 | |
| 2636 | if (err.Success()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2637 | { |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2638 | m_can_jit = eCanJITYes; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2639 | if (log) |
| 2640 | log->Printf ("Process::%s pid %" PRIu64 " allocation test passed, CanJIT () is true", __FUNCTION__, GetID ()); |
| 2641 | } |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2642 | else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2643 | { |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2644 | m_can_jit = eCanJITNo; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2645 | if (log) |
| 2646 | log->Printf ("Process::%s pid %" PRIu64 " allocation test failed, CanJIT () is false: %s", __FUNCTION__, GetID (), err.AsCString ()); |
| 2647 | } |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2648 | |
| 2649 | DeallocateMemory (allocated_memory); |
| 2650 | } |
| 2651 | |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2652 | return m_can_jit == eCanJITYes; |
| 2653 | } |
| 2654 | |
| 2655 | void |
| 2656 | Process::SetCanJIT (bool can_jit) |
| 2657 | { |
| 2658 | m_can_jit = (can_jit ? eCanJITYes : eCanJITNo); |
| 2659 | } |
| 2660 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2661 | Error |
| 2662 | Process::DeallocateMemory (addr_t ptr) |
| 2663 | { |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2664 | Error error; |
| 2665 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2666 | if (!m_allocated_memory_cache.DeallocateMemory(ptr)) |
| 2667 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2668 | error.SetErrorStringWithFormat ("deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2669 | } |
| 2670 | #else |
| 2671 | error = DoDeallocateMemory (ptr); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2672 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2673 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2674 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2675 | log->Printf("Process::DeallocateMemory(addr=0x%16.16" PRIx64 ") => err = %s (m_stop_id = %u, m_memory_id = %u)", |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2676 | ptr, |
| 2677 | error.AsCString("SUCCESS"), |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2678 | m_mod_id.GetStopID(), |
| 2679 | m_mod_id.GetMemoryID()); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2680 | #endif |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2681 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Han Ming Ong | c811d38 | 2012-11-17 00:33:14 +0000 | [diff] [blame] | 2684 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2685 | ModuleSP |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2686 | Process::ReadModuleFromMemory (const FileSpec& file_spec, |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2687 | lldb::addr_t header_addr, |
| 2688 | size_t size_to_read) |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2689 | { |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2690 | ModuleSP module_sp (new Module (file_spec, ArchSpec())); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2691 | if (module_sp) |
| 2692 | { |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2693 | Error error; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2694 | ObjectFile *objfile = module_sp->GetMemoryObjectFile (shared_from_this(), header_addr, error, size_to_read); |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2695 | if (objfile) |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2696 | return module_sp; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2697 | } |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2698 | return ModuleSP(); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2699 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2700 | |
| 2701 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2702 | Process::EnableWatchpoint (Watchpoint *watchpoint, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2703 | { |
| 2704 | Error error; |
| 2705 | error.SetErrorString("watchpoints are not supported"); |
| 2706 | return error; |
| 2707 | } |
| 2708 | |
| 2709 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2710 | Process::DisableWatchpoint (Watchpoint *watchpoint, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2711 | { |
| 2712 | Error error; |
| 2713 | error.SetErrorString("watchpoints are not supported"); |
| 2714 | return error; |
| 2715 | } |
| 2716 | |
| 2717 | StateType |
| 2718 | Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 2719 | { |
| 2720 | StateType state; |
| 2721 | // Now wait for the process to launch and return control to us, and then |
| 2722 | // call DidLaunch: |
| 2723 | while (1) |
| 2724 | { |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2725 | event_sp.reset(); |
| 2726 | state = WaitForStateChangedEventsPrivate (timeout, event_sp); |
| 2727 | |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 2728 | if (StateIsStoppedState(state, false)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2729 | break; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2730 | |
| 2731 | // If state is invalid, then we timed out |
| 2732 | if (state == eStateInvalid) |
| 2733 | break; |
| 2734 | |
| 2735 | if (event_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2736 | HandlePrivateEvent (event_sp); |
| 2737 | } |
| 2738 | return state; |
| 2739 | } |
| 2740 | |
| 2741 | Error |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2742 | Process::Launch (ProcessLaunchInfo &launch_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2743 | { |
| 2744 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2745 | m_abi_sp.reset(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2746 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2747 | m_jit_loaders_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2748 | m_system_runtime_ap.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 2749 | m_os_ap.reset(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2750 | m_process_input_reader.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2751 | |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 2752 | Module *exe_module = m_target.GetExecutableModulePointer(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2753 | if (exe_module) |
| 2754 | { |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 2755 | char local_exec_file_path[PATH_MAX]; |
| 2756 | char platform_exec_file_path[PATH_MAX]; |
| 2757 | exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path)); |
| 2758 | exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, sizeof(platform_exec_file_path)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2759 | if (exe_module->GetFileSpec().Exists()) |
| 2760 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2761 | // Install anything that might need to be installed prior to launching. |
| 2762 | // For host systems, this will do nothing, but if we are connected to a |
| 2763 | // remote platform it will install any needed binaries |
| 2764 | error = GetTarget().Install(&launch_info); |
| 2765 | if (error.Fail()) |
| 2766 | return error; |
| 2767 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2768 | if (PrivateStateThreadIsValid ()) |
| 2769 | PausePrivateStateThread (); |
| 2770 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2771 | error = WillLaunch (exe_module); |
| 2772 | if (error.Success()) |
| 2773 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 2774 | const bool restarted = false; |
| 2775 | SetPublicState (eStateLaunching, restarted); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 2776 | m_should_detach = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2777 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 2778 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 69fd4be | 2012-09-04 20:29:05 +0000 | [diff] [blame] | 2779 | { |
| 2780 | // Now launch using these arguments. |
| 2781 | error = DoLaunch (exe_module, launch_info); |
| 2782 | } |
| 2783 | else |
| 2784 | { |
| 2785 | // This shouldn't happen |
| 2786 | error.SetErrorString("failed to acquire process run lock"); |
| 2787 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2788 | |
| 2789 | if (error.Fail()) |
| 2790 | { |
| 2791 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 2792 | { |
| 2793 | SetID (LLDB_INVALID_PROCESS_ID); |
| 2794 | const char *error_string = error.AsCString(); |
| 2795 | if (error_string == NULL) |
| 2796 | error_string = "launch failed"; |
| 2797 | SetExitStatus (-1, error_string); |
| 2798 | } |
| 2799 | } |
| 2800 | else |
| 2801 | { |
| 2802 | EventSP event_sp; |
Greg Clayton | 1a38ea7 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 2803 | TimeValue timeout_time; |
| 2804 | timeout_time = TimeValue::Now(); |
| 2805 | timeout_time.OffsetWithSeconds(10); |
| 2806 | StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2807 | |
Greg Clayton | 1a38ea7 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 2808 | if (state == eStateInvalid || event_sp.get() == NULL) |
| 2809 | { |
| 2810 | // We were able to launch the process, but we failed to |
| 2811 | // catch the initial stop. |
| 2812 | SetExitStatus (0, "failed to catch stop after launch"); |
| 2813 | Destroy(); |
| 2814 | } |
| 2815 | else if (state == eStateStopped || state == eStateCrashed) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2816 | { |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2817 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2818 | DidLaunch (); |
| 2819 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2820 | DynamicLoader *dyld = GetDynamicLoader (); |
| 2821 | if (dyld) |
| 2822 | dyld->DidLaunch(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2823 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 2824 | GetJITLoaders().DidLaunch(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2825 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2826 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 2827 | if (system_runtime) |
| 2828 | system_runtime->DidLaunch(); |
| 2829 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 2830 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2831 | // This delays passing the stopped event to listeners till DidLaunch gets |
| 2832 | // a chance to complete... |
| 2833 | HandlePrivateEvent (event_sp); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2834 | |
| 2835 | if (PrivateStateThreadIsValid ()) |
| 2836 | ResumePrivateStateThread (); |
| 2837 | else |
| 2838 | StartPrivateStateThread (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2839 | } |
| 2840 | else if (state == eStateExited) |
| 2841 | { |
| 2842 | // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 2843 | // not likely to work, and return an invalid pid. |
| 2844 | HandlePrivateEvent (event_sp); |
| 2845 | } |
| 2846 | } |
| 2847 | } |
| 2848 | } |
| 2849 | else |
| 2850 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 2851 | error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2852 | } |
| 2853 | } |
| 2854 | return error; |
| 2855 | } |
| 2856 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2857 | |
| 2858 | Error |
| 2859 | Process::LoadCore () |
| 2860 | { |
| 2861 | Error error = DoLoadCore(); |
| 2862 | if (error.Success()) |
| 2863 | { |
| 2864 | if (PrivateStateThreadIsValid ()) |
| 2865 | ResumePrivateStateThread (); |
| 2866 | else |
| 2867 | StartPrivateStateThread (); |
| 2868 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2869 | DynamicLoader *dyld = GetDynamicLoader (); |
| 2870 | if (dyld) |
| 2871 | dyld->DidAttach(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2872 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 2873 | GetJITLoaders().DidAttach(); |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2874 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2875 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 2876 | if (system_runtime) |
| 2877 | system_runtime->DidAttach(); |
| 2878 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2879 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2880 | // We successfully loaded a core file, now pretend we stopped so we can |
| 2881 | // show all of the threads in the core file and explore the crashed |
| 2882 | // state. |
| 2883 | SetPrivateState (eStateStopped); |
| 2884 | |
| 2885 | } |
| 2886 | return error; |
| 2887 | } |
| 2888 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2889 | DynamicLoader * |
| 2890 | Process::GetDynamicLoader () |
| 2891 | { |
| 2892 | if (m_dyld_ap.get() == NULL) |
| 2893 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL)); |
| 2894 | return m_dyld_ap.get(); |
| 2895 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2896 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2897 | const lldb::DataBufferSP |
| 2898 | Process::GetAuxvData() |
| 2899 | { |
| 2900 | return DataBufferSP (); |
| 2901 | } |
| 2902 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2903 | JITLoaderList & |
| 2904 | Process::GetJITLoaders () |
| 2905 | { |
| 2906 | if (!m_jit_loaders_ap) |
| 2907 | { |
| 2908 | m_jit_loaders_ap.reset(new JITLoaderList()); |
| 2909 | JITLoader::LoadPlugins(this, *m_jit_loaders_ap); |
| 2910 | } |
| 2911 | return *m_jit_loaders_ap; |
| 2912 | } |
| 2913 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2914 | SystemRuntime * |
| 2915 | Process::GetSystemRuntime () |
| 2916 | { |
| 2917 | if (m_system_runtime_ap.get() == NULL) |
| 2918 | m_system_runtime_ap.reset (SystemRuntime::FindPlugin(this)); |
| 2919 | return m_system_runtime_ap.get(); |
| 2920 | } |
| 2921 | |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 2922 | Process::AttachCompletionHandler::AttachCompletionHandler (Process *process, uint32_t exec_count) : |
| 2923 | NextEventAction (process), |
| 2924 | m_exec_count (exec_count) |
| 2925 | { |
| 2926 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2927 | if (log) |
| 2928 | log->Printf ("Process::AttachCompletionHandler::%s process=%p, exec_count=%" PRIu32, __FUNCTION__, static_cast<void*>(process), exec_count); |
| 2929 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2930 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2931 | Process::NextEventAction::EventActionResult |
| 2932 | Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2933 | { |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 2934 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2935 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2936 | StateType state = ProcessEventData::GetStateFromEvent (event_sp.get()); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 2937 | if (log) |
| 2938 | log->Printf ("Process::AttachCompletionHandler::%s called with state %s (%d)", __FUNCTION__, StateAsCString(state), static_cast<int> (state)); |
| 2939 | |
| 2940 | switch (state) |
Greg Clayton | 19388cf | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 2941 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2942 | case eStateRunning: |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2943 | case eStateConnected: |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2944 | return eEventActionRetry; |
| 2945 | |
| 2946 | case eStateStopped: |
| 2947 | case eStateCrashed: |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 2948 | { |
| 2949 | // During attach, prior to sending the eStateStopped event, |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 2950 | // lldb_private::Process subclasses must set the new process ID. |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 2951 | assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 2952 | // We don't want these events to be reported, so go set the ShouldReportStop here: |
| 2953 | m_process->GetThreadList().SetShouldReportStop (eVoteNo); |
| 2954 | |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 2955 | if (m_exec_count > 0) |
| 2956 | { |
| 2957 | --m_exec_count; |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 2958 | |
| 2959 | if (log) |
| 2960 | log->Printf ("Process::AttachCompletionHandler::%s state %s: reduced remaining exec count to %" PRIu32 ", requesting resume", __FUNCTION__, StateAsCString(state), m_exec_count); |
| 2961 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 2962 | RequestResume(); |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 2963 | return eEventActionRetry; |
| 2964 | } |
| 2965 | else |
| 2966 | { |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 2967 | if (log) |
| 2968 | log->Printf ("Process::AttachCompletionHandler::%s state %s: no more execs expected to start, continuing with attach", __FUNCTION__, StateAsCString(state)); |
| 2969 | |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 2970 | m_process->CompleteAttach (); |
| 2971 | return eEventActionSuccess; |
| 2972 | } |
| 2973 | } |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2974 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 2975 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2976 | default: |
| 2977 | case eStateExited: |
| 2978 | case eStateInvalid: |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2979 | break; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2980 | } |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 2981 | |
| 2982 | m_exit_string.assign ("No valid Process"); |
| 2983 | return eEventActionExit; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2984 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2985 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2986 | Process::NextEventAction::EventActionResult |
| 2987 | Process::AttachCompletionHandler::HandleBeingInterrupted() |
| 2988 | { |
| 2989 | return eEventActionSuccess; |
| 2990 | } |
| 2991 | |
| 2992 | const char * |
| 2993 | Process::AttachCompletionHandler::GetExitString () |
| 2994 | { |
| 2995 | return m_exit_string.c_str(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | Error |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2999 | Process::Attach (ProcessAttachInfo &attach_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3000 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3001 | m_abi_sp.reset(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3002 | m_process_input_reader.reset(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3003 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3004 | m_jit_loaders_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3005 | m_system_runtime_ap.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3006 | m_os_ap.reset(); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 3007 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3008 | lldb::pid_t attach_pid = attach_info.GetProcessID(); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3009 | Error error; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3010 | if (attach_pid == LLDB_INVALID_PROCESS_ID) |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 3011 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3012 | char process_name[PATH_MAX]; |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3013 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3014 | if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name))) |
Jim Ingham | 2ecb742 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 3015 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3016 | const bool wait_for_launch = attach_info.GetWaitForLaunch(); |
| 3017 | |
| 3018 | if (wait_for_launch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3019 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3020 | error = WillAttachToProcessWithName(process_name, wait_for_launch); |
| 3021 | if (error.Success()) |
| 3022 | { |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3023 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3024 | { |
| 3025 | m_should_detach = true; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3026 | const bool restarted = false; |
| 3027 | SetPublicState (eStateAttaching, restarted); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3028 | // Now attach using these arguments. |
Jean-Daniel Dupas | 9c517c0 | 2013-12-23 22:32:54 +0000 | [diff] [blame] | 3029 | error = DoAttachToProcessWithName (process_name, attach_info); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3030 | } |
| 3031 | else |
| 3032 | { |
| 3033 | // This shouldn't happen |
| 3034 | error.SetErrorString("failed to acquire process run lock"); |
| 3035 | } |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 3036 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3037 | if (error.Fail()) |
| 3038 | { |
| 3039 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3040 | { |
| 3041 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3042 | if (error.AsCString() == NULL) |
| 3043 | error.SetErrorString("attach failed"); |
| 3044 | |
| 3045 | SetExitStatus(-1, error.AsCString()); |
| 3046 | } |
| 3047 | } |
| 3048 | else |
| 3049 | { |
| 3050 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 3051 | StartPrivateStateThread(); |
| 3052 | } |
| 3053 | return error; |
| 3054 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3055 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3056 | else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3057 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3058 | ProcessInstanceInfoList process_infos; |
| 3059 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 3060 | |
| 3061 | if (platform_sp) |
| 3062 | { |
| 3063 | ProcessInstanceInfoMatch match_info; |
| 3064 | match_info.GetProcessInfo() = attach_info; |
| 3065 | match_info.SetNameMatchType (eNameMatchEquals); |
| 3066 | platform_sp->FindProcesses (match_info, process_infos); |
| 3067 | const uint32_t num_matches = process_infos.GetSize(); |
| 3068 | if (num_matches == 1) |
| 3069 | { |
| 3070 | attach_pid = process_infos.GetProcessIDAtIndex(0); |
| 3071 | // Fall through and attach using the above process ID |
| 3072 | } |
| 3073 | else |
| 3074 | { |
| 3075 | match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name)); |
| 3076 | if (num_matches > 1) |
Jim Ingham | 368ac22 | 2014-08-15 17:05:27 +0000 | [diff] [blame] | 3077 | { |
| 3078 | StreamString s; |
| 3079 | ProcessInstanceInfo::DumpTableHeader (s, platform_sp.get(), true, false); |
| 3080 | for (size_t i = 0; i < num_matches; i++) |
| 3081 | { |
| 3082 | process_infos.GetProcessInfoAtIndex(i).DumpAsTableRow(s, platform_sp.get(), true, false); |
| 3083 | } |
| 3084 | error.SetErrorStringWithFormat ("more than one process named %s:\n%s", |
| 3085 | process_name, |
| 3086 | s.GetData()); |
| 3087 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3088 | else |
| 3089 | error.SetErrorStringWithFormat ("could not find a process named %s", process_name); |
| 3090 | } |
| 3091 | } |
| 3092 | else |
| 3093 | { |
| 3094 | error.SetErrorString ("invalid platform, can't find processes by name"); |
| 3095 | return error; |
| 3096 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3097 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3098 | } |
| 3099 | else |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3100 | { |
| 3101 | error.SetErrorString ("invalid process name"); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3102 | } |
| 3103 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3104 | |
| 3105 | if (attach_pid != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3106 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3107 | error = WillAttachToProcessWithID(attach_pid); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3108 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3109 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3110 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3111 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3112 | { |
| 3113 | // Now attach using these arguments. |
| 3114 | m_should_detach = true; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3115 | const bool restarted = false; |
| 3116 | SetPublicState (eStateAttaching, restarted); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3117 | error = DoAttachToProcessWithID (attach_pid, attach_info); |
| 3118 | } |
| 3119 | else |
| 3120 | { |
| 3121 | // This shouldn't happen |
| 3122 | error.SetErrorString("failed to acquire process run lock"); |
| 3123 | } |
| 3124 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3125 | if (error.Success()) |
| 3126 | { |
| 3127 | |
| 3128 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 3129 | StartPrivateStateThread(); |
| 3130 | } |
| 3131 | else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3132 | { |
| 3133 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3134 | { |
| 3135 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3136 | const char *error_string = error.AsCString(); |
| 3137 | if (error_string == NULL) |
| 3138 | error_string = "attach failed"; |
| 3139 | |
| 3140 | SetExitStatus(-1, error_string); |
| 3141 | } |
| 3142 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3143 | } |
| 3144 | } |
| 3145 | return error; |
| 3146 | } |
| 3147 | |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3148 | void |
| 3149 | Process::CompleteAttach () |
| 3150 | { |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3151 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3152 | if (log) |
| 3153 | log->Printf ("Process::%s()", __FUNCTION__); |
| 3154 | |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3155 | // Let the process subclass figure out at much as it can about the process |
| 3156 | // before we go looking for a dynamic loader plug-in. |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 3157 | ArchSpec process_arch; |
| 3158 | DidAttach(process_arch); |
| 3159 | |
| 3160 | if (process_arch.IsValid()) |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3161 | { |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 3162 | m_target.SetArchitecture(process_arch); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3163 | if (log) |
| 3164 | { |
| 3165 | const char *triple_str = process_arch.GetTriple().getTriple().c_str (); |
| 3166 | log->Printf ("Process::%s replacing process architecture with DidAttach() architecture: %s", |
| 3167 | __FUNCTION__, |
| 3168 | triple_str ? triple_str : "<null>"); |
| 3169 | } |
| 3170 | } |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3171 | |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3172 | // We just attached. If we have a platform, ask it for the process architecture, and if it isn't |
| 3173 | // the same as the one we've already set, switch architectures. |
| 3174 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 3175 | assert (platform_sp.get()); |
| 3176 | if (platform_sp) |
| 3177 | { |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3178 | const ArchSpec &target_arch = m_target.GetArchitecture(); |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 3179 | if (target_arch.IsValid() && !platform_sp->IsCompatibleArchitecture (target_arch, false, NULL)) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3180 | { |
| 3181 | ArchSpec platform_arch; |
| 3182 | platform_sp = platform_sp->GetPlatformForArchitecture (target_arch, &platform_arch); |
| 3183 | if (platform_sp) |
| 3184 | { |
| 3185 | m_target.SetPlatform (platform_sp); |
| 3186 | m_target.SetArchitecture(platform_arch); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3187 | if (log) |
| 3188 | log->Printf ("Process::%s switching platform to %s and architecture to %s based on info from attach", __FUNCTION__, platform_sp->GetName().AsCString (""), platform_arch.GetTriple().getTriple().c_str ()); |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3189 | } |
| 3190 | } |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 3191 | else if (!process_arch.IsValid()) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3192 | { |
| 3193 | ProcessInstanceInfo process_info; |
| 3194 | platform_sp->GetProcessInfo (GetID(), process_info); |
| 3195 | const ArchSpec &process_arch = process_info.GetArchitecture(); |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 3196 | if (process_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(process_arch)) |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3197 | { |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3198 | m_target.SetArchitecture (process_arch); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3199 | if (log) |
| 3200 | log->Printf ("Process::%s switching architecture to %s based on info the platform retrieved for pid %" PRIu64, __FUNCTION__, process_arch.GetTriple().getTriple().c_str (), GetID ()); |
| 3201 | } |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3202 | } |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | // We have completed the attach, now it is time to find the dynamic loader |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3206 | // plug-in |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3207 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3208 | if (dyld) |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3209 | { |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3210 | dyld->DidAttach(); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3211 | if (log) |
| 3212 | { |
| 3213 | ModuleSP exe_module_sp = m_target.GetExecutableModule (); |
| 3214 | log->Printf ("Process::%s after DynamicLoader::DidAttach(), target executable is %s (using %s plugin)", |
| 3215 | __FUNCTION__, |
| 3216 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>", |
| 3217 | dyld->GetPluginName().AsCString ("<unnamed>")); |
| 3218 | } |
| 3219 | } |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3220 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 3221 | GetJITLoaders().DidAttach(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3222 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3223 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3224 | if (system_runtime) |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3225 | { |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3226 | system_runtime->DidAttach(); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3227 | if (log) |
| 3228 | { |
| 3229 | ModuleSP exe_module_sp = m_target.GetExecutableModule (); |
| 3230 | log->Printf ("Process::%s after SystemRuntime::DidAttach(), target executable is %s (using %s plugin)", |
| 3231 | __FUNCTION__, |
| 3232 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>", |
| 3233 | system_runtime->GetPluginName().AsCString("<unnamed>")); |
| 3234 | } |
| 3235 | } |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3236 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3237 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3238 | // Figure out which one is the executable, and set that in our target: |
Enrico Granata | 1759848 | 2012-11-08 02:22:02 +0000 | [diff] [blame] | 3239 | const ModuleList &target_modules = m_target.GetImages(); |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3240 | Mutex::Locker modules_locker(target_modules.GetMutex()); |
| 3241 | size_t num_modules = target_modules.GetSize(); |
| 3242 | ModuleSP new_executable_module_sp; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3243 | |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 3244 | for (size_t i = 0; i < num_modules; i++) |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3245 | { |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3246 | ModuleSP module_sp (target_modules.GetModuleAtIndexUnlocked (i)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3247 | if (module_sp && module_sp->IsExecutable()) |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3248 | { |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 3249 | if (m_target.GetExecutableModulePointer() != module_sp.get()) |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3250 | new_executable_module_sp = module_sp; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3251 | break; |
| 3252 | } |
| 3253 | } |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3254 | if (new_executable_module_sp) |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3255 | { |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3256 | m_target.SetExecutableModule (new_executable_module_sp, false); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3257 | if (log) |
| 3258 | { |
| 3259 | ModuleSP exe_module_sp = m_target.GetExecutableModule (); |
| 3260 | log->Printf ("Process::%s after looping through modules, target executable is %s", |
| 3261 | __FUNCTION__, |
| 3262 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>"); |
| 3263 | } |
| 3264 | } |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3267 | Error |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 3268 | Process::ConnectRemote (Stream *strm, const char *remote_url) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3269 | { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3270 | m_abi_sp.reset(); |
| 3271 | m_process_input_reader.reset(); |
| 3272 | |
| 3273 | // Find the process and its architecture. Make sure it matches the architecture |
| 3274 | // of the current Target, and if not adjust it. |
| 3275 | |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 3276 | Error error (DoConnectRemote (strm, remote_url)); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3277 | if (error.Success()) |
| 3278 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3279 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3280 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 3281 | EventSP event_sp; |
| 3282 | StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 3283 | |
| 3284 | if (state == eStateStopped || state == eStateCrashed) |
| 3285 | { |
| 3286 | // If we attached and actually have a process on the other end, then |
| 3287 | // this ended up being the equivalent of an attach. |
| 3288 | CompleteAttach (); |
| 3289 | |
| 3290 | // This delays passing the stopped event to listeners till |
| 3291 | // CompleteAttach gets a chance to complete... |
| 3292 | HandlePrivateEvent (event_sp); |
| 3293 | |
| 3294 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3295 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 3296 | |
| 3297 | if (PrivateStateThreadIsValid ()) |
| 3298 | ResumePrivateStateThread (); |
| 3299 | else |
| 3300 | StartPrivateStateThread (); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3301 | } |
| 3302 | return error; |
| 3303 | } |
| 3304 | |
| 3305 | |
| 3306 | Error |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 3307 | Process::PrivateResume () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3308 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3309 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_STEP)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3310 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3311 | log->Printf("Process::PrivateResume() m_stop_id = %u, public state: %s private state: %s", |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 3312 | m_mod_id.GetStopID(), |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3313 | StateAsCString(m_public_state.GetValue()), |
| 3314 | StateAsCString(m_private_state.GetValue())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3315 | |
| 3316 | Error error (WillResume()); |
| 3317 | // Tell the process it is about to resume before the thread list |
| 3318 | if (error.Success()) |
| 3319 | { |
Johnny Chen | c4221e4 | 2010-12-02 20:53:05 +0000 | [diff] [blame] | 3320 | // Now let the thread list know we are about to resume so it |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3321 | // can let all of our threads know that they are about to be |
| 3322 | // resumed. Threads will each be called with |
| 3323 | // Thread::WillResume(StateType) where StateType contains the state |
| 3324 | // that they are supposed to have when the process is resumed |
| 3325 | // (suspended/running/stepping). Threads should also check |
| 3326 | // their resume signal in lldb::Thread::GetResumeSignal() |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3327 | // to see if they are supposed to start back up with a signal. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3328 | if (m_thread_list.WillResume()) |
| 3329 | { |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3330 | // Last thing, do the PreResumeActions. |
| 3331 | if (!RunPreResumeActions()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3332 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3333 | error.SetErrorStringWithFormat ("Process::PrivateResume PreResumeActions failed, not resuming."); |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3334 | } |
| 3335 | else |
| 3336 | { |
| 3337 | m_mod_id.BumpResumeID(); |
| 3338 | error = DoResume(); |
| 3339 | if (error.Success()) |
| 3340 | { |
| 3341 | DidResume(); |
| 3342 | m_thread_list.DidResume(); |
| 3343 | if (log) |
| 3344 | log->Printf ("Process thinks the process has resumed."); |
| 3345 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3346 | } |
| 3347 | } |
| 3348 | else |
| 3349 | { |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 3350 | // Somebody wanted to run without running. So generate a continue & a stopped event, |
| 3351 | // and let the world handle them. |
| 3352 | if (log) |
| 3353 | log->Printf ("Process::PrivateResume() asked to simulate a start & stop."); |
| 3354 | |
| 3355 | SetPrivateState(eStateRunning); |
| 3356 | SetPrivateState(eStateStopped); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3357 | } |
| 3358 | } |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3359 | else if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3360 | log->Printf ("Process::PrivateResume() got an error \"%s\".", error.AsCString("<unknown error>")); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3361 | return error; |
| 3362 | } |
| 3363 | |
| 3364 | Error |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 3365 | Process::Halt (bool clear_thread_plans) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3366 | { |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 3367 | // Don't clear the m_clear_thread_plans_on_stop, only set it to true if |
| 3368 | // in case it was already set and some thread plan logic calls halt on its |
| 3369 | // own. |
| 3370 | m_clear_thread_plans_on_stop |= clear_thread_plans; |
| 3371 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3372 | // First make sure we aren't in the middle of handling an event, or we might restart. This is pretty weak, since |
| 3373 | // we could just straightaway get another event. It just narrows the window... |
| 3374 | m_currently_handling_event.WaitForValueEqualTo(false); |
| 3375 | |
| 3376 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3377 | // Pause our private state thread so we can ensure no one else eats |
| 3378 | // the stop event out from under us. |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3379 | Listener halt_listener ("lldb.process.halt_listener"); |
| 3380 | HijackPrivateProcessEvents(&halt_listener); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3381 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3382 | EventSP event_sp; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3383 | Error error (WillHalt()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3384 | |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 3385 | bool restored_process_events = false; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3386 | if (error.Success()) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3387 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3388 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3389 | bool caused_stop = false; |
| 3390 | |
| 3391 | // Ask the process subclass to actually halt our process |
| 3392 | error = DoHalt(caused_stop); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3393 | if (error.Success()) |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3394 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3395 | if (m_public_state.GetValue() == eStateAttaching) |
| 3396 | { |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 3397 | // Don't hijack and eat the eStateExited as the code that was doing |
| 3398 | // the attach will be waiting for this event... |
| 3399 | RestorePrivateProcessEvents(); |
| 3400 | restored_process_events = true; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3401 | SetExitStatus(SIGKILL, "Cancelled async attach."); |
| 3402 | Destroy (); |
| 3403 | } |
| 3404 | else |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3405 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3406 | // If "caused_stop" is true, then DoHalt stopped the process. If |
| 3407 | // "caused_stop" is false, the process was already stopped. |
| 3408 | // If the DoHalt caused the process to stop, then we want to catch |
| 3409 | // this event and set the interrupted bool to true before we pass |
| 3410 | // this along so clients know that the process was interrupted by |
| 3411 | // a halt command. |
| 3412 | if (caused_stop) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3413 | { |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3414 | // Wait for 1 second for the process to stop. |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3415 | TimeValue timeout_time; |
| 3416 | timeout_time = TimeValue::Now(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3417 | timeout_time.OffsetWithSeconds(10); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3418 | bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp); |
| 3419 | StateType state = ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3420 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3421 | if (!got_event || state == eStateInvalid) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3422 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3423 | // We timeout out and didn't get a stop event... |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3424 | error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState())); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3425 | } |
| 3426 | else |
| 3427 | { |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 3428 | if (StateIsStoppedState (state, false)) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3429 | { |
| 3430 | // We caused the process to interrupt itself, so mark this |
| 3431 | // as such in the stop event so clients can tell an interrupted |
| 3432 | // process from a natural stop |
| 3433 | ProcessEventData::SetInterruptedInEvent (event_sp.get(), true); |
| 3434 | } |
| 3435 | else |
| 3436 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3437 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3438 | if (log) |
| 3439 | log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state)); |
| 3440 | error.SetErrorString ("Did not get stopped event after halt."); |
| 3441 | } |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3442 | } |
| 3443 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3444 | DidHalt(); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3445 | } |
| 3446 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3447 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3448 | // Resume our private state thread before we post the event (if any) |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 3449 | if (!restored_process_events) |
| 3450 | RestorePrivateProcessEvents(); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3451 | |
| 3452 | // Post any event we might have consumed. If all goes well, we will have |
| 3453 | // stopped the process, intercepted the event and set the interrupted |
| 3454 | // bool in the event. Post it to the private event queue and that will end up |
| 3455 | // correctly setting the state. |
| 3456 | if (event_sp) |
| 3457 | m_private_state_broadcaster.BroadcastEvent(event_sp); |
| 3458 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3459 | return error; |
| 3460 | } |
| 3461 | |
| 3462 | Error |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3463 | Process::HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp) |
| 3464 | { |
| 3465 | Error error; |
| 3466 | if (m_public_state.GetValue() == eStateRunning) |
| 3467 | { |
| 3468 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3469 | if (log) |
| 3470 | log->Printf("Process::Destroy() About to halt."); |
| 3471 | error = Halt(); |
| 3472 | if (error.Success()) |
| 3473 | { |
| 3474 | // Consume the halt event. |
| 3475 | TimeValue timeout (TimeValue::Now()); |
| 3476 | timeout.OffsetWithSeconds(1); |
| 3477 | StateType state = WaitForProcessToStop (&timeout, &exit_event_sp); |
| 3478 | |
| 3479 | // If the process exited while we were waiting for it to stop, put the exited event into |
| 3480 | // the shared pointer passed in and return. Our caller doesn't need to do anything else, since |
| 3481 | // they don't have a process anymore... |
| 3482 | |
| 3483 | if (state == eStateExited || m_private_state.GetValue() == eStateExited) |
| 3484 | { |
| 3485 | if (log) |
| 3486 | log->Printf("Process::HaltForDestroyOrDetach() Process exited while waiting to Halt."); |
| 3487 | return error; |
| 3488 | } |
| 3489 | else |
| 3490 | exit_event_sp.reset(); // It is ok to consume any non-exit stop events |
| 3491 | |
| 3492 | if (state != eStateStopped) |
| 3493 | { |
| 3494 | if (log) |
| 3495 | log->Printf("Process::HaltForDestroyOrDetach() Halt failed to stop, state is: %s", StateAsCString(state)); |
| 3496 | // If we really couldn't stop the process then we should just error out here, but if the |
| 3497 | // lower levels just bobbled sending the event and we really are stopped, then continue on. |
| 3498 | StateType private_state = m_private_state.GetValue(); |
| 3499 | if (private_state != eStateStopped) |
| 3500 | { |
| 3501 | return error; |
| 3502 | } |
| 3503 | } |
| 3504 | } |
| 3505 | else |
| 3506 | { |
| 3507 | if (log) |
| 3508 | log->Printf("Process::HaltForDestroyOrDetach() Halt got error: %s", error.AsCString()); |
| 3509 | } |
| 3510 | } |
| 3511 | return error; |
| 3512 | } |
| 3513 | |
| 3514 | Error |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3515 | Process::Detach (bool keep_stopped) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3516 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3517 | EventSP exit_event_sp; |
| 3518 | Error error; |
| 3519 | m_destroy_in_process = true; |
| 3520 | |
| 3521 | error = WillDetach(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3522 | |
| 3523 | if (error.Success()) |
| 3524 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3525 | if (DetachRequiresHalt()) |
| 3526 | { |
| 3527 | error = HaltForDestroyOrDetach (exit_event_sp); |
| 3528 | if (!error.Success()) |
| 3529 | { |
| 3530 | m_destroy_in_process = false; |
| 3531 | return error; |
| 3532 | } |
| 3533 | else if (exit_event_sp) |
| 3534 | { |
| 3535 | // We shouldn't need to do anything else here. There's no process left to detach from... |
| 3536 | StopPrivateStateThread(); |
| 3537 | m_destroy_in_process = false; |
| 3538 | return error; |
| 3539 | } |
| 3540 | } |
| 3541 | |
Andrew MacPherson | c3826b5 | 2014-03-25 19:59:36 +0000 | [diff] [blame] | 3542 | m_thread_list.DiscardThreadPlans(); |
| 3543 | DisableAllBreakpointSites(); |
| 3544 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3545 | error = DoDetach(keep_stopped); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3546 | if (error.Success()) |
| 3547 | { |
| 3548 | DidDetach(); |
| 3549 | StopPrivateStateThread(); |
| 3550 | } |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3551 | else |
| 3552 | { |
| 3553 | return error; |
| 3554 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3555 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3556 | m_destroy_in_process = false; |
| 3557 | |
| 3558 | // If we exited when we were waiting for a process to stop, then |
| 3559 | // forward the event here so we don't lose the event |
| 3560 | if (exit_event_sp) |
| 3561 | { |
| 3562 | // Directly broadcast our exited event because we shut down our |
| 3563 | // private state thread above |
| 3564 | BroadcastEvent(exit_event_sp); |
| 3565 | } |
| 3566 | |
| 3567 | // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating |
| 3568 | // the last events through the event system, in which case we might strand the write lock. Unlock |
| 3569 | // it here so when we do to tear down the process we don't get an error destroying the lock. |
| 3570 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3571 | m_public_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3572 | return error; |
| 3573 | } |
| 3574 | |
| 3575 | Error |
| 3576 | Process::Destroy () |
| 3577 | { |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 3578 | |
| 3579 | // Tell ourselves we are in the process of destroying the process, so that we don't do any unnecessary work |
| 3580 | // that might hinder the destruction. Remember to set this back to false when we are done. That way if the attempt |
| 3581 | // failed and the process stays around for some reason it won't be in a confused state. |
| 3582 | |
| 3583 | m_destroy_in_process = true; |
| 3584 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3585 | Error error (WillDestroy()); |
| 3586 | if (error.Success()) |
| 3587 | { |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 3588 | EventSP exit_event_sp; |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3589 | if (DestroyRequiresHalt()) |
Jim Ingham | 04e0a22 | 2012-05-23 15:46:31 +0000 | [diff] [blame] | 3590 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3591 | error = HaltForDestroyOrDetach(exit_event_sp); |
Jim Ingham | 04e0a22 | 2012-05-23 15:46:31 +0000 | [diff] [blame] | 3592 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3593 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3594 | if (m_public_state.GetValue() != eStateRunning) |
| 3595 | { |
| 3596 | // Ditch all thread plans, and remove all our breakpoints: in case we have to restart the target to |
| 3597 | // kill it, we don't want it hitting a breakpoint... |
| 3598 | // Only do this if we've stopped, however, since if we didn't manage to halt it above, then |
| 3599 | // we're not going to have much luck doing this now. |
| 3600 | m_thread_list.DiscardThreadPlans(); |
| 3601 | DisableAllBreakpointSites(); |
| 3602 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3603 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3604 | error = DoDestroy(); |
| 3605 | if (error.Success()) |
| 3606 | { |
| 3607 | DidDestroy(); |
| 3608 | StopPrivateStateThread(); |
| 3609 | } |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3610 | m_stdio_communication.StopReadThread(); |
| 3611 | m_stdio_communication.Disconnect(); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3612 | |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3613 | if (m_process_input_reader) |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3614 | { |
| 3615 | m_process_input_reader->SetIsDone(true); |
| 3616 | m_process_input_reader->Cancel(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3617 | m_process_input_reader.reset(); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 3620 | // If we exited when we were waiting for a process to stop, then |
| 3621 | // forward the event here so we don't lose the event |
| 3622 | if (exit_event_sp) |
| 3623 | { |
| 3624 | // Directly broadcast our exited event because we shut down our |
| 3625 | // private state thread above |
| 3626 | BroadcastEvent(exit_event_sp); |
| 3627 | } |
| 3628 | |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3629 | // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating |
| 3630 | // the last events through the event system, in which case we might strand the write lock. Unlock |
| 3631 | // it here so when we do to tear down the process we don't get an error destroying the lock. |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3632 | m_public_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3633 | } |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 3634 | |
| 3635 | m_destroy_in_process = false; |
| 3636 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3637 | return error; |
| 3638 | } |
| 3639 | |
| 3640 | Error |
| 3641 | Process::Signal (int signal) |
| 3642 | { |
| 3643 | Error error (WillSignal()); |
| 3644 | if (error.Success()) |
| 3645 | { |
| 3646 | error = DoSignal(signal); |
| 3647 | if (error.Success()) |
| 3648 | DidSignal(); |
| 3649 | } |
| 3650 | return error; |
| 3651 | } |
| 3652 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3653 | lldb::ByteOrder |
| 3654 | Process::GetByteOrder () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3655 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3656 | return m_target.GetArchitecture().GetByteOrder(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | uint32_t |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3660 | Process::GetAddressByteSize () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3661 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3662 | return m_target.GetArchitecture().GetAddressByteSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3665 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3666 | bool |
| 3667 | Process::ShouldBroadcastEvent (Event *event_ptr) |
| 3668 | { |
| 3669 | const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr); |
| 3670 | bool return_value = true; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3671 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3672 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3673 | switch (state) |
| 3674 | { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3675 | case eStateConnected: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3676 | case eStateAttaching: |
| 3677 | case eStateLaunching: |
| 3678 | case eStateDetached: |
| 3679 | case eStateExited: |
| 3680 | case eStateUnloaded: |
| 3681 | // These events indicate changes in the state of the debugging session, always report them. |
| 3682 | return_value = true; |
| 3683 | break; |
| 3684 | case eStateInvalid: |
| 3685 | // We stopped for no apparent reason, don't report it. |
| 3686 | return_value = false; |
| 3687 | break; |
| 3688 | case eStateRunning: |
| 3689 | case eStateStepping: |
| 3690 | // If we've started the target running, we handle the cases where we |
| 3691 | // are already running and where there is a transition from stopped to |
| 3692 | // running differently. |
| 3693 | // running -> running: Automatically suppress extra running events |
| 3694 | // stopped -> running: Report except when there is one or more no votes |
| 3695 | // and no yes votes. |
| 3696 | SynchronouslyNotifyStateChanged (state); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3697 | if (m_force_next_event_delivery) |
| 3698 | return_value = true; |
| 3699 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3700 | { |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3701 | switch (m_last_broadcast_state) |
| 3702 | { |
| 3703 | case eStateRunning: |
| 3704 | case eStateStepping: |
| 3705 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 3706 | return_value = false; |
| 3707 | break; |
| 3708 | default: |
| 3709 | // TODO: make this work correctly. For now always report |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 3710 | // run if we aren't running so we don't miss any running |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3711 | // events. If I run the lldb/test/thread/a.out file and |
| 3712 | // break at main.cpp:58, run and hit the breakpoints on |
| 3713 | // multiple threads, then somehow during the stepping over |
| 3714 | // of all breakpoints no run gets reported. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3715 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3716 | // This is a transition from stop to run. |
| 3717 | switch (m_thread_list.ShouldReportRun (event_ptr)) |
| 3718 | { |
| 3719 | case eVoteYes: |
| 3720 | case eVoteNoOpinion: |
| 3721 | return_value = true; |
| 3722 | break; |
| 3723 | case eVoteNo: |
| 3724 | return_value = false; |
| 3725 | break; |
| 3726 | } |
| 3727 | break; |
| 3728 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3729 | } |
| 3730 | break; |
| 3731 | case eStateStopped: |
| 3732 | case eStateCrashed: |
| 3733 | case eStateSuspended: |
| 3734 | { |
| 3735 | // We've stopped. First see if we're going to restart the target. |
| 3736 | // If we are going to stop, then we always broadcast the event. |
| 3737 | // If we aren't going to stop, let the thread plans decide if we're going to report this event. |
Jim Ingham | b01e742 | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 3738 | // If no thread has an opinion, we don't report it. |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3739 | |
Jim Ingham | cb4ca11 | 2012-05-16 01:32:14 +0000 | [diff] [blame] | 3740 | RefreshStateAfterStop (); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3741 | if (ProcessEventData::GetInterruptedFromEvent (event_ptr)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3742 | { |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3743 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3744 | log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3745 | static_cast<void*>(event_ptr), |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3746 | StateAsCString(state)); |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 3747 | // Even though we know we are going to stop, we should let the threads have a look at the stop, |
| 3748 | // so they can properly set their state. |
| 3749 | m_thread_list.ShouldStop (event_ptr); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3750 | return_value = true; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3751 | } |
| 3752 | else |
| 3753 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3754 | bool was_restarted = ProcessEventData::GetRestartedFromEvent (event_ptr); |
| 3755 | bool should_resume = false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3756 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3757 | // It makes no sense to ask "ShouldStop" if we've already been restarted... |
| 3758 | // Asking the thread list is also not likely to go well, since we are running again. |
| 3759 | // So in that case just report the event. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3760 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3761 | if (!was_restarted) |
| 3762 | should_resume = m_thread_list.ShouldStop (event_ptr) == false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3763 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3764 | if (was_restarted || should_resume || m_resume_requested) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3765 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3766 | Vote stop_vote = m_thread_list.ShouldReportStop (event_ptr); |
| 3767 | if (log) |
| 3768 | log->Printf ("Process::ShouldBroadcastEvent: should_stop: %i state: %s was_restarted: %i stop_vote: %d.", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3769 | should_resume, StateAsCString(state), |
| 3770 | was_restarted, stop_vote); |
| 3771 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3772 | switch (stop_vote) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3773 | { |
| 3774 | case eVoteYes: |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3775 | return_value = true; |
| 3776 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3777 | case eVoteNoOpinion: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3778 | case eVoteNo: |
| 3779 | return_value = false; |
| 3780 | break; |
| 3781 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3782 | |
Jim Ingham | cb95f34 | 2012-09-05 21:13:56 +0000 | [diff] [blame] | 3783 | if (!was_restarted) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3784 | { |
| 3785 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3786 | log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", |
| 3787 | static_cast<void*>(event_ptr), |
| 3788 | StateAsCString(state)); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3789 | ProcessEventData::SetRestartedInEvent(event_ptr, true); |
Jim Ingham | cb95f34 | 2012-09-05 21:13:56 +0000 | [diff] [blame] | 3790 | PrivateResume (); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3791 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3792 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3793 | } |
| 3794 | else |
| 3795 | { |
| 3796 | return_value = true; |
| 3797 | SynchronouslyNotifyStateChanged (state); |
| 3798 | } |
| 3799 | } |
| 3800 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3801 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3802 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3803 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 3804 | // Forcing the next event delivery is a one shot deal. So reset it here. |
| 3805 | m_force_next_event_delivery = false; |
| 3806 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3807 | // We do some coalescing of events (for instance two consecutive running events get coalesced.) |
| 3808 | // But we only coalesce against events we actually broadcast. So we use m_last_broadcast_state |
| 3809 | // to track that. NB - you can't use "m_public_state.GetValue()" for that purpose, as was originally done, |
| 3810 | // because the PublicState reflects the last event pulled off the queue, and there may be several |
| 3811 | // events stacked up on the queue unserviced. So the PublicState may not reflect the last broadcasted event |
| 3812 | // yet. m_last_broadcast_state gets updated here. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3813 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3814 | if (return_value) |
| 3815 | m_last_broadcast_state = state; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3816 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3817 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3818 | log->Printf ("Process::ShouldBroadcastEvent (%p) => new state: %s, last broadcast state: %s - %s", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 3819 | static_cast<void*>(event_ptr), StateAsCString(state), |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3820 | StateAsCString(m_last_broadcast_state), |
| 3821 | return_value ? "YES" : "NO"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3822 | return return_value; |
| 3823 | } |
| 3824 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3825 | |
| 3826 | bool |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3827 | Process::StartPrivateStateThread (bool force) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3828 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3829 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3830 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3831 | bool already_running = PrivateStateThreadIsValid (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3832 | if (log) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3833 | log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread"); |
| 3834 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3835 | if (!force && already_running) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3836 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3837 | |
| 3838 | // Create a thread that watches our internal state and controls which |
| 3839 | // events make it to clients (into the DCProcess event queue). |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 3840 | char thread_name[1024]; |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 3841 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 3842 | if (HostInfo::GetMaxThreadNameLength() <= 30) |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 3843 | { |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 3844 | // On platforms with abbreviated thread name lengths, choose thread names that fit within the limit. |
| 3845 | if (already_running) |
| 3846 | snprintf(thread_name, sizeof(thread_name), "intern-state-OV"); |
| 3847 | else |
| 3848 | snprintf(thread_name, sizeof(thread_name), "intern-state"); |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 3849 | } |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3850 | else |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 3851 | { |
| 3852 | if (already_running) |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 3853 | snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", GetID()); |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 3854 | else |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 3855 | snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID()); |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3858 | // Create the private state thread, and start it running. |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 3859 | m_private_state_thread = ThreadLauncher::LaunchThread(thread_name, Process::PrivateStateThread, this, NULL); |
| 3860 | if (m_private_state_thread.GetState() == eThreadStateRunning) |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3861 | { |
| 3862 | ResumePrivateStateThread(); |
| 3863 | return true; |
| 3864 | } |
| 3865 | else |
| 3866 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | void |
| 3870 | Process::PausePrivateStateThread () |
| 3871 | { |
| 3872 | ControlPrivateStateThread (eBroadcastInternalStateControlPause); |
| 3873 | } |
| 3874 | |
| 3875 | void |
| 3876 | Process::ResumePrivateStateThread () |
| 3877 | { |
| 3878 | ControlPrivateStateThread (eBroadcastInternalStateControlResume); |
| 3879 | } |
| 3880 | |
| 3881 | void |
| 3882 | Process::StopPrivateStateThread () |
| 3883 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3884 | if (PrivateStateThreadIsValid ()) |
| 3885 | ControlPrivateStateThread (eBroadcastInternalStateControlStop); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3886 | else |
| 3887 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3888 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3889 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3890 | log->Printf ("Went to stop the private state thread, but it was already invalid."); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3891 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | void |
| 3895 | Process::ControlPrivateStateThread (uint32_t signal) |
| 3896 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3897 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3898 | |
| 3899 | assert (signal == eBroadcastInternalStateControlStop || |
| 3900 | signal == eBroadcastInternalStateControlPause || |
| 3901 | signal == eBroadcastInternalStateControlResume); |
| 3902 | |
| 3903 | if (log) |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 3904 | log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3905 | |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 3906 | // Signal the private state thread. First we should copy this is case the |
| 3907 | // thread starts exiting since the private state thread will NULL this out |
| 3908 | // when it exits |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 3909 | HostThread private_state_thread(m_private_state_thread); |
| 3910 | if (private_state_thread.GetState() == eThreadStateRunning) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3911 | { |
| 3912 | TimeValue timeout_time; |
| 3913 | bool timed_out; |
| 3914 | |
| 3915 | m_private_state_control_broadcaster.BroadcastEvent (signal, NULL); |
| 3916 | |
| 3917 | timeout_time = TimeValue::Now(); |
| 3918 | timeout_time.OffsetWithSeconds(2); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3919 | if (log) |
| 3920 | log->Printf ("Sending control event of type: %d.", signal); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3921 | m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out); |
| 3922 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 3923 | |
| 3924 | if (signal == eBroadcastInternalStateControlStop) |
| 3925 | { |
| 3926 | if (timed_out) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3927 | { |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 3928 | Error error = private_state_thread.Cancel(); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3929 | if (log) |
| 3930 | log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString()); |
| 3931 | } |
| 3932 | else |
| 3933 | { |
| 3934 | if (log) |
| 3935 | log->Printf ("The control event killed the private state thread without having to cancel."); |
| 3936 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3937 | |
| 3938 | thread_result_t result = NULL; |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 3939 | private_state_thread.Join(&result); |
| 3940 | m_private_state_thread.Reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3941 | } |
| 3942 | } |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3943 | else |
| 3944 | { |
| 3945 | if (log) |
| 3946 | log->Printf ("Private state thread already dead, no need to signal it to stop."); |
| 3947 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
| 3950 | void |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 3951 | Process::SendAsyncInterrupt () |
| 3952 | { |
| 3953 | if (PrivateStateThreadIsValid()) |
| 3954 | m_private_state_broadcaster.BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 3955 | else |
| 3956 | BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 3957 | } |
| 3958 | |
| 3959 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3960 | Process::HandlePrivateEvent (EventSP &event_sp) |
| 3961 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3962 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3963 | m_resume_requested = false; |
| 3964 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3965 | m_currently_handling_event.SetValue(true, eBroadcastNever); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3966 | |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 3967 | const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3968 | |
| 3969 | // First check to see if anybody wants a shot at this event: |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 3970 | if (m_next_event_action_ap.get() != NULL) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3971 | { |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 3972 | NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3973 | if (log) |
| 3974 | log->Printf ("Ran next event action, result was %d.", action_result); |
| 3975 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3976 | switch (action_result) |
| 3977 | { |
| 3978 | case NextEventAction::eEventActionSuccess: |
| 3979 | SetNextEventAction(NULL); |
| 3980 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3981 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3982 | case NextEventAction::eEventActionRetry: |
| 3983 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3984 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3985 | case NextEventAction::eEventActionExit: |
Jim Ingham | 2a5fdd4 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 3986 | // Handle Exiting Here. If we already got an exited event, |
| 3987 | // we should just propagate it. Otherwise, swallow this event, |
| 3988 | // and set our state to exit so the next event will kill us. |
| 3989 | if (new_state != eStateExited) |
| 3990 | { |
| 3991 | // FIXME: should cons up an exited event, and discard this one. |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 3992 | SetExitStatus(0, m_next_event_action_ap->GetExitString()); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3993 | m_currently_handling_event.SetValue(false, eBroadcastAlways); |
Jim Ingham | 2a5fdd4 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 3994 | SetNextEventAction(NULL); |
| 3995 | return; |
| 3996 | } |
| 3997 | SetNextEventAction(NULL); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3998 | break; |
| 3999 | } |
| 4000 | } |
| 4001 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4002 | // See if we should broadcast this state to external clients? |
| 4003 | const bool should_broadcast = ShouldBroadcastEvent (event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4004 | |
| 4005 | if (should_broadcast) |
| 4006 | { |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4007 | const bool is_hijacked = IsHijackedForEvent(eBroadcastBitStateChanged); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4008 | if (log) |
| 4009 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4010 | log->Printf ("Process::%s (pid = %" PRIu64 ") broadcasting new state %s (old state %s) to %s", |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4011 | __FUNCTION__, |
| 4012 | GetID(), |
| 4013 | StateAsCString(new_state), |
| 4014 | StateAsCString (GetState ()), |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4015 | is_hijacked ? "hijacked" : "public"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4016 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4017 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4018 | if (StateIsRunningState (new_state)) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4019 | { |
| 4020 | // Only push the input handler if we aren't fowarding events, |
| 4021 | // as this means the curses GUI is in use... |
| 4022 | if (!GetTarget().GetDebugger().IsForwardingEvents()) |
| 4023 | PushProcessIOHandler (); |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 4024 | m_iohandler_sync.SetValue(true, eBroadcastAlways); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4025 | } |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4026 | else if (StateIsStoppedState(new_state, false)) |
| 4027 | { |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 4028 | m_iohandler_sync.SetValue(false, eBroadcastNever); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4029 | if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 4030 | { |
| 4031 | // If the lldb_private::Debugger is handling the events, we don't |
| 4032 | // want to pop the process IOHandler here, we want to do it when |
| 4033 | // we receive the stopped event so we can carefully control when |
| 4034 | // the process IOHandler is popped because when we stop we want to |
| 4035 | // display some text stating how and why we stopped, then maybe some |
| 4036 | // process/thread/frame info, and then we want the "(lldb) " prompt |
| 4037 | // to show up. If we pop the process IOHandler here, then we will |
| 4038 | // cause the command interpreter to become the top IOHandler after |
| 4039 | // the process pops off and it will update its prompt right away... |
| 4040 | // See the Debugger.cpp file where it calls the function as |
| 4041 | // "process_sp->PopProcessIOHandler()" to see where I am talking about. |
| 4042 | // Otherwise we end up getting overlapping "(lldb) " prompts and |
| 4043 | // garbled output. |
| 4044 | // |
| 4045 | // If we aren't handling the events in the debugger (which is indicated |
| 4046 | // by "m_target.GetDebugger().IsHandlingEvents()" returning false) or we |
| 4047 | // are hijacked, then we always pop the process IO handler manually. |
| 4048 | // Hijacking happens when the internal process state thread is running |
| 4049 | // thread plans, or when commands want to run in synchronous mode |
| 4050 | // and they call "process->WaitForProcessToStop()". An example of something |
| 4051 | // that will hijack the events is a simple expression: |
| 4052 | // |
| 4053 | // (lldb) expr (int)puts("hello") |
| 4054 | // |
| 4055 | // This will cause the internal process state thread to resume and halt |
| 4056 | // the process (and _it_ will hijack the eBroadcastBitStateChanged |
| 4057 | // events) and we do need the IO handler to be pushed and popped |
| 4058 | // correctly. |
| 4059 | |
| 4060 | if (is_hijacked || m_target.GetDebugger().IsHandlingEvents() == false) |
| 4061 | PopProcessIOHandler (); |
| 4062 | } |
| 4063 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4064 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4065 | BroadcastEvent (event_sp); |
| 4066 | } |
| 4067 | else |
| 4068 | { |
| 4069 | if (log) |
| 4070 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4071 | log->Printf ("Process::%s (pid = %" PRIu64 ") suppressing state %s (old state %s): should_broadcast == false", |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4072 | __FUNCTION__, |
| 4073 | GetID(), |
| 4074 | StateAsCString(new_state), |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 4075 | StateAsCString (GetState ())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4076 | } |
| 4077 | } |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 4078 | m_currently_handling_event.SetValue(false, eBroadcastAlways); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4081 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4082 | Process::PrivateStateThread (void *arg) |
| 4083 | { |
| 4084 | Process *proc = static_cast<Process*> (arg); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4085 | thread_result_t result = proc->RunPrivateStateThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4086 | return result; |
| 4087 | } |
| 4088 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4089 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4090 | Process::RunPrivateStateThread () |
| 4091 | { |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4092 | bool control_only = true; |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4093 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4094 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4095 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4096 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4097 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...", |
| 4098 | __FUNCTION__, static_cast<void*>(this), GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4099 | |
| 4100 | bool exit_now = false; |
| 4101 | while (!exit_now) |
| 4102 | { |
| 4103 | EventSP event_sp; |
| 4104 | WaitForEventsPrivate (NULL, event_sp, control_only); |
| 4105 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) |
| 4106 | { |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4107 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4108 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") got a control event: %d", |
| 4109 | __FUNCTION__, static_cast<void*>(this), GetID(), |
| 4110 | event_sp->GetType()); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4111 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4112 | switch (event_sp->GetType()) |
| 4113 | { |
| 4114 | case eBroadcastInternalStateControlStop: |
| 4115 | exit_now = true; |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 4116 | break; // doing any internal state management below |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4117 | |
| 4118 | case eBroadcastInternalStateControlPause: |
| 4119 | control_only = true; |
| 4120 | break; |
| 4121 | |
| 4122 | case eBroadcastInternalStateControlResume: |
| 4123 | control_only = false; |
| 4124 | break; |
| 4125 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4126 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4127 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4128 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4129 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4130 | else if (event_sp->GetType() == eBroadcastBitInterrupt) |
| 4131 | { |
| 4132 | if (m_public_state.GetValue() == eStateAttaching) |
| 4133 | { |
| 4134 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4135 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt while attaching - forwarding interrupt.", |
| 4136 | __FUNCTION__, static_cast<void*>(this), |
| 4137 | GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4138 | BroadcastEvent (eBroadcastBitInterrupt, NULL); |
| 4139 | } |
| 4140 | else |
| 4141 | { |
| 4142 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4143 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt - Halting.", |
| 4144 | __FUNCTION__, static_cast<void*>(this), |
| 4145 | GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4146 | Halt(); |
| 4147 | } |
| 4148 | continue; |
| 4149 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4150 | |
| 4151 | const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 4152 | |
| 4153 | if (internal_state != eStateInvalid) |
| 4154 | { |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 4155 | if (m_clear_thread_plans_on_stop && |
| 4156 | StateIsStoppedState(internal_state, true)) |
| 4157 | { |
| 4158 | m_clear_thread_plans_on_stop = false; |
| 4159 | m_thread_list.DiscardThreadPlans(); |
| 4160 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4161 | HandlePrivateEvent (event_sp); |
| 4162 | } |
| 4163 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 4164 | if (internal_state == eStateInvalid || |
| 4165 | internal_state == eStateExited || |
| 4166 | internal_state == eStateDetached ) |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4167 | { |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4168 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4169 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") about to exit with internal state %s...", |
| 4170 | __FUNCTION__, static_cast<void*>(this), GetID(), |
| 4171 | StateAsCString(internal_state)); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4172 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4173 | break; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4174 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 4177 | // Verify log is still enabled before attempting to write to it... |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4178 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4179 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", |
| 4180 | __FUNCTION__, static_cast<void*>(this), GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4181 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 4182 | m_public_run_lock.SetStopped(); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 4183 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 4184 | m_private_state_thread.Reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4185 | return NULL; |
| 4186 | } |
| 4187 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4188 | //------------------------------------------------------------------ |
| 4189 | // Process Event Data |
| 4190 | //------------------------------------------------------------------ |
| 4191 | |
| 4192 | Process::ProcessEventData::ProcessEventData () : |
| 4193 | EventData (), |
| 4194 | m_process_sp (), |
| 4195 | m_state (eStateInvalid), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 4196 | m_restarted (false), |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4197 | m_update_state (0), |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4198 | m_interrupted (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4199 | { |
| 4200 | } |
| 4201 | |
| 4202 | Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) : |
| 4203 | EventData (), |
| 4204 | m_process_sp (process_sp), |
| 4205 | m_state (state), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 4206 | m_restarted (false), |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4207 | m_update_state (0), |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4208 | m_interrupted (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4209 | { |
| 4210 | } |
| 4211 | |
| 4212 | Process::ProcessEventData::~ProcessEventData() |
| 4213 | { |
| 4214 | } |
| 4215 | |
| 4216 | const ConstString & |
| 4217 | Process::ProcessEventData::GetFlavorString () |
| 4218 | { |
| 4219 | static ConstString g_flavor ("Process::ProcessEventData"); |
| 4220 | return g_flavor; |
| 4221 | } |
| 4222 | |
| 4223 | const ConstString & |
| 4224 | Process::ProcessEventData::GetFlavor () const |
| 4225 | { |
| 4226 | return ProcessEventData::GetFlavorString (); |
| 4227 | } |
| 4228 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4229 | void |
| 4230 | Process::ProcessEventData::DoOnRemoval (Event *event_ptr) |
| 4231 | { |
| 4232 | // This function gets called twice for each event, once when the event gets pulled |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4233 | // off of the private process event queue, and then any number of times, first when it gets pulled off of |
| 4234 | // the public event queue, then other times when we're pretending that this is where we stopped at the |
| 4235 | // end of expression evaluation. m_update_state is used to distinguish these |
| 4236 | // three cases; it is 0 when we're just pulling it off for private handling, |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4237 | // and > 1 for expression evaluation, and we don't want to do the breakpoint command handling then. |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4238 | if (m_update_state != 1) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4239 | return; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4240 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4241 | m_process_sp->SetPublicState (m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr)); |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 4242 | |
| 4243 | // If this is a halt event, even if the halt stopped with some reason other than a plain interrupt (e.g. we had |
| 4244 | // already stopped for a breakpoint when the halt request came through) don't do the StopInfo actions, as they may |
| 4245 | // end up restarting the process. |
| 4246 | if (m_interrupted) |
| 4247 | return; |
| 4248 | |
| 4249 | // If we're stopped and haven't restarted, then do the StopInfo actions here: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4250 | if (m_state == eStateStopped && ! m_restarted) |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4251 | { |
| 4252 | ThreadList &curr_thread_list = m_process_sp->GetThreadList(); |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4253 | uint32_t num_threads = curr_thread_list.GetSize(); |
| 4254 | uint32_t idx; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 4255 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4256 | // The actions might change one of the thread's stop_info's opinions about whether we should |
| 4257 | // stop the process, so we need to query that as we go. |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4258 | |
| 4259 | // One other complication here, is that we try to catch any case where the target has run (except for expressions) |
| 4260 | // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and |
| 4261 | // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like |
| 4262 | // 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 |
| 4263 | // against this list & bag out if anything differs. |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4264 | std::vector<uint32_t> thread_index_array(num_threads); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4265 | for (idx = 0; idx < num_threads; ++idx) |
| 4266 | thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID(); |
| 4267 | |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4268 | // Use this to track whether we should continue from here. We will only continue the target running if |
| 4269 | // no thread says we should stop. Of course if some thread's PerformAction actually sets the target running, |
| 4270 | // then it doesn't matter what the other threads say... |
| 4271 | |
| 4272 | bool still_should_stop = false; |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4273 | |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4274 | // Sometimes - for instance if we have a bug in the stub we are talking to, we stop but no thread has a |
| 4275 | // valid stop reason. In that case we should just stop, because we have no way of telling what the right |
| 4276 | // thing to do is, and it's better to let the user decide than continue behind their backs. |
| 4277 | |
| 4278 | bool does_anybody_have_an_opinion = false; |
| 4279 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4280 | for (idx = 0; idx < num_threads; ++idx) |
| 4281 | { |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4282 | curr_thread_list = m_process_sp->GetThreadList(); |
| 4283 | if (curr_thread_list.GetSize() != num_threads) |
| 4284 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4285 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4286 | if (log) |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4287 | log->Printf("Number of threads changed from %u to %u while processing event.", num_threads, curr_thread_list.GetSize()); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4288 | break; |
| 4289 | } |
| 4290 | |
| 4291 | lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx); |
| 4292 | |
| 4293 | if (thread_sp->GetIndexID() != thread_index_array[idx]) |
| 4294 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4295 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4296 | if (log) |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4297 | log->Printf("The thread at position %u changed from %u to %u while processing event.", |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4298 | idx, |
| 4299 | thread_index_array[idx], |
| 4300 | thread_sp->GetIndexID()); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4301 | break; |
| 4302 | } |
| 4303 | |
Jim Ingham | b15bfc7 | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 4304 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo (); |
Jim Ingham | 5d88a06 | 2012-10-16 00:09:33 +0000 | [diff] [blame] | 4305 | if (stop_info_sp && stop_info_sp->IsValid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4306 | { |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4307 | does_anybody_have_an_opinion = true; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4308 | bool this_thread_wants_to_stop; |
| 4309 | if (stop_info_sp->GetOverrideShouldStop()) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4310 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4311 | this_thread_wants_to_stop = stop_info_sp->GetOverriddenShouldStopValue(); |
| 4312 | } |
| 4313 | else |
| 4314 | { |
| 4315 | stop_info_sp->PerformAction(event_ptr); |
| 4316 | // The stop action might restart the target. If it does, then we want to mark that in the |
| 4317 | // event so that whoever is receiving it will know to wait for the running event and reflect |
| 4318 | // that state appropriately. |
| 4319 | // We also need to stop processing actions, since they aren't expecting the target to be running. |
| 4320 | |
| 4321 | // FIXME: we might have run. |
| 4322 | if (stop_info_sp->HasTargetRunSinceMe()) |
| 4323 | { |
| 4324 | SetRestarted (true); |
| 4325 | break; |
| 4326 | } |
| 4327 | |
| 4328 | this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4329 | } |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4330 | |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4331 | if (still_should_stop == false) |
| 4332 | still_should_stop = this_thread_wants_to_stop; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4333 | } |
| 4334 | } |
Jim Ingham | 3ebcf7f | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 4335 | |
Ashok Thirumurthi | cf7c55e | 2013-04-18 14:38:20 +0000 | [diff] [blame] | 4336 | |
Jim Ingham | a8ca6e2 | 2013-05-03 23:04:37 +0000 | [diff] [blame] | 4337 | if (!GetRestarted()) |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4338 | { |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4339 | if (!still_should_stop && does_anybody_have_an_opinion) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4340 | { |
| 4341 | // We've been asked to continue, so do that here. |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4342 | SetRestarted(true); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 4343 | // Use the public resume method here, since this is just |
| 4344 | // extending a public resume. |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4345 | m_process_sp->PrivateResume(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4346 | } |
| 4347 | else |
| 4348 | { |
| 4349 | // If we didn't restart, run the Stop Hooks here: |
| 4350 | // They might also restart the target, so watch for that. |
| 4351 | m_process_sp->GetTarget().RunStopHooks(); |
| 4352 | if (m_process_sp->GetPrivateState() == eStateRunning) |
| 4353 | SetRestarted(true); |
| 4354 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4355 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4356 | } |
| 4357 | } |
| 4358 | |
| 4359 | void |
| 4360 | Process::ProcessEventData::Dump (Stream *s) const |
| 4361 | { |
| 4362 | if (m_process_sp) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4363 | s->Printf(" process = %p (pid = %" PRIu64 "), ", |
| 4364 | static_cast<void*>(m_process_sp.get()), m_process_sp->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4365 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 4366 | s->Printf("state = %s", StateAsCString(GetState())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | const Process::ProcessEventData * |
| 4370 | Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr) |
| 4371 | { |
| 4372 | if (event_ptr) |
| 4373 | { |
| 4374 | const EventData *event_data = event_ptr->GetData(); |
| 4375 | if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 4376 | return static_cast <const ProcessEventData *> (event_ptr->GetData()); |
| 4377 | } |
| 4378 | return NULL; |
| 4379 | } |
| 4380 | |
| 4381 | ProcessSP |
| 4382 | Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr) |
| 4383 | { |
| 4384 | ProcessSP process_sp; |
| 4385 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4386 | if (data) |
| 4387 | process_sp = data->GetProcessSP(); |
| 4388 | return process_sp; |
| 4389 | } |
| 4390 | |
| 4391 | StateType |
| 4392 | Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr) |
| 4393 | { |
| 4394 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4395 | if (data == NULL) |
| 4396 | return eStateInvalid; |
| 4397 | else |
| 4398 | return data->GetState(); |
| 4399 | } |
| 4400 | |
| 4401 | bool |
| 4402 | Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr) |
| 4403 | { |
| 4404 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4405 | if (data == NULL) |
| 4406 | return false; |
| 4407 | else |
| 4408 | return data->GetRestarted(); |
| 4409 | } |
| 4410 | |
| 4411 | void |
| 4412 | Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value) |
| 4413 | { |
| 4414 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4415 | if (data != NULL) |
| 4416 | data->SetRestarted(new_value); |
| 4417 | } |
| 4418 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4419 | size_t |
| 4420 | Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr) |
| 4421 | { |
| 4422 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4423 | if (data != NULL) |
| 4424 | return data->GetNumRestartedReasons(); |
| 4425 | else |
| 4426 | return 0; |
| 4427 | } |
| 4428 | |
| 4429 | const char * |
| 4430 | Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx) |
| 4431 | { |
| 4432 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4433 | if (data != NULL) |
| 4434 | return data->GetRestartedReasonAtIndex(idx); |
| 4435 | else |
| 4436 | return NULL; |
| 4437 | } |
| 4438 | |
| 4439 | void |
| 4440 | Process::ProcessEventData::AddRestartedReason (Event *event_ptr, const char *reason) |
| 4441 | { |
| 4442 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4443 | if (data != NULL) |
| 4444 | data->AddRestartedReason(reason); |
| 4445 | } |
| 4446 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4447 | bool |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4448 | Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr) |
| 4449 | { |
| 4450 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4451 | if (data == NULL) |
| 4452 | return false; |
| 4453 | else |
| 4454 | return data->GetInterrupted (); |
| 4455 | } |
| 4456 | |
| 4457 | void |
| 4458 | Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value) |
| 4459 | { |
| 4460 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4461 | if (data != NULL) |
| 4462 | data->SetInterrupted(new_value); |
| 4463 | } |
| 4464 | |
| 4465 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4466 | Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr) |
| 4467 | { |
| 4468 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4469 | if (data) |
| 4470 | { |
| 4471 | data->SetUpdateStateOnRemoval(); |
| 4472 | return true; |
| 4473 | } |
| 4474 | return false; |
| 4475 | } |
| 4476 | |
Greg Clayton | d9e416c | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 4477 | lldb::TargetSP |
| 4478 | Process::CalculateTarget () |
| 4479 | { |
| 4480 | return m_target.shared_from_this(); |
| 4481 | } |
| 4482 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4483 | void |
Greg Clayton | 0603aa9 | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 4484 | Process::CalculateExecutionContext (ExecutionContext &exe_ctx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4485 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4486 | exe_ctx.SetTargetPtr (&m_target); |
| 4487 | exe_ctx.SetProcessPtr (this); |
| 4488 | exe_ctx.SetThreadPtr(NULL); |
| 4489 | exe_ctx.SetFramePtr (NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4492 | //uint32_t |
| 4493 | //Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 4494 | //{ |
| 4495 | // return 0; |
| 4496 | //} |
| 4497 | // |
| 4498 | //ArchSpec |
| 4499 | //Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
| 4500 | //{ |
| 4501 | // return Host::GetArchSpecForExistingProcess (pid); |
| 4502 | //} |
| 4503 | // |
| 4504 | //ArchSpec |
| 4505 | //Process::GetArchSpecForExistingProcess (const char *process_name) |
| 4506 | //{ |
| 4507 | // return Host::GetArchSpecForExistingProcess (process_name); |
| 4508 | //} |
| 4509 | // |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4510 | void |
| 4511 | Process::AppendSTDOUT (const char * s, size_t len) |
| 4512 | { |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 4513 | Mutex::Locker locker (m_stdio_communication_mutex); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4514 | m_stdout_data.append (s, len); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 4515 | BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (shared_from_this(), GetState())); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | void |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4519 | Process::AppendSTDERR (const char * s, size_t len) |
| 4520 | { |
| 4521 | Mutex::Locker locker (m_stdio_communication_mutex); |
| 4522 | m_stderr_data.append (s, len); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 4523 | BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (shared_from_this(), GetState())); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4526 | void |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4527 | Process::BroadcastAsyncProfileData(const std::string &one_profile_data) |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4528 | { |
| 4529 | Mutex::Locker locker (m_profile_data_comm_mutex); |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4530 | m_profile_data.push_back(one_profile_data); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4531 | BroadcastEventIfUnique (eBroadcastBitProfileData, new ProcessEventData (shared_from_this(), GetState())); |
| 4532 | } |
| 4533 | |
| 4534 | size_t |
| 4535 | Process::GetAsyncProfileData (char *buf, size_t buf_size, Error &error) |
| 4536 | { |
| 4537 | Mutex::Locker locker(m_profile_data_comm_mutex); |
Han Ming Ong | 929a94f | 2012-11-29 22:14:45 +0000 | [diff] [blame] | 4538 | if (m_profile_data.empty()) |
| 4539 | return 0; |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4540 | |
| 4541 | std::string &one_profile_data = m_profile_data.front(); |
| 4542 | size_t bytes_available = one_profile_data.size(); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4543 | if (bytes_available > 0) |
| 4544 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4545 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4546 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4547 | log->Printf ("Process::GetProfileData (buf = %p, size = %" PRIu64 ")", |
| 4548 | static_cast<void*>(buf), |
| 4549 | static_cast<uint64_t>(buf_size)); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4550 | if (bytes_available > buf_size) |
| 4551 | { |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4552 | memcpy(buf, one_profile_data.c_str(), buf_size); |
| 4553 | one_profile_data.erase(0, buf_size); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4554 | bytes_available = buf_size; |
| 4555 | } |
| 4556 | else |
| 4557 | { |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4558 | memcpy(buf, one_profile_data.c_str(), bytes_available); |
Han Ming Ong | 929a94f | 2012-11-29 22:14:45 +0000 | [diff] [blame] | 4559 | m_profile_data.erase(m_profile_data.begin()); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4560 | } |
| 4561 | } |
| 4562 | return bytes_available; |
| 4563 | } |
| 4564 | |
| 4565 | |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4566 | //------------------------------------------------------------------ |
| 4567 | // Process STDIO |
| 4568 | //------------------------------------------------------------------ |
| 4569 | |
| 4570 | size_t |
| 4571 | Process::GetSTDOUT (char *buf, size_t buf_size, Error &error) |
| 4572 | { |
| 4573 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 4574 | size_t bytes_available = m_stdout_data.size(); |
| 4575 | if (bytes_available > 0) |
| 4576 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4577 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4578 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4579 | log->Printf ("Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")", |
| 4580 | static_cast<void*>(buf), |
| 4581 | static_cast<uint64_t>(buf_size)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4582 | if (bytes_available > buf_size) |
| 4583 | { |
| 4584 | memcpy(buf, m_stdout_data.c_str(), buf_size); |
| 4585 | m_stdout_data.erase(0, buf_size); |
| 4586 | bytes_available = buf_size; |
| 4587 | } |
| 4588 | else |
| 4589 | { |
| 4590 | memcpy(buf, m_stdout_data.c_str(), bytes_available); |
| 4591 | m_stdout_data.clear(); |
| 4592 | } |
| 4593 | } |
| 4594 | return bytes_available; |
| 4595 | } |
| 4596 | |
| 4597 | |
| 4598 | size_t |
| 4599 | Process::GetSTDERR (char *buf, size_t buf_size, Error &error) |
| 4600 | { |
| 4601 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 4602 | size_t bytes_available = m_stderr_data.size(); |
| 4603 | if (bytes_available > 0) |
| 4604 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4605 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4606 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4607 | log->Printf ("Process::GetSTDERR (buf = %p, size = %" PRIu64 ")", |
| 4608 | static_cast<void*>(buf), |
| 4609 | static_cast<uint64_t>(buf_size)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4610 | if (bytes_available > buf_size) |
| 4611 | { |
| 4612 | memcpy(buf, m_stderr_data.c_str(), buf_size); |
| 4613 | m_stderr_data.erase(0, buf_size); |
| 4614 | bytes_available = buf_size; |
| 4615 | } |
| 4616 | else |
| 4617 | { |
| 4618 | memcpy(buf, m_stderr_data.c_str(), bytes_available); |
| 4619 | m_stderr_data.clear(); |
| 4620 | } |
| 4621 | } |
| 4622 | return bytes_available; |
| 4623 | } |
| 4624 | |
| 4625 | void |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4626 | Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len) |
| 4627 | { |
| 4628 | Process *process = (Process *) baton; |
| 4629 | process->AppendSTDOUT (static_cast<const char *>(src), src_len); |
| 4630 | } |
| 4631 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4632 | class IOHandlerProcessSTDIO : |
| 4633 | public IOHandler |
| 4634 | { |
| 4635 | public: |
| 4636 | IOHandlerProcessSTDIO (Process *process, |
| 4637 | int write_fd) : |
| 4638 | IOHandler(process->GetTarget().GetDebugger()), |
| 4639 | m_process (process), |
| 4640 | m_read_file (), |
| 4641 | m_write_file (write_fd, false), |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 4642 | m_pipe () |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4643 | { |
| 4644 | m_read_file.SetDescriptor(GetInputFD(), false); |
| 4645 | } |
| 4646 | |
| 4647 | virtual |
| 4648 | ~IOHandlerProcessSTDIO () |
| 4649 | { |
| 4650 | |
| 4651 | } |
| 4652 | |
| 4653 | bool |
| 4654 | OpenPipes () |
| 4655 | { |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 4656 | if (m_pipe.IsValid()) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4657 | return true; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 4658 | return m_pipe.Open(); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4659 | } |
| 4660 | |
| 4661 | void |
| 4662 | ClosePipes() |
| 4663 | { |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 4664 | m_pipe.Close(); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4665 | } |
| 4666 | |
| 4667 | // Each IOHandler gets to run until it is done. It should read data |
| 4668 | // from the "in" and place output into "out" and "err and return |
| 4669 | // when done. |
| 4670 | virtual void |
| 4671 | Run () |
| 4672 | { |
| 4673 | if (m_read_file.IsValid() && m_write_file.IsValid()) |
| 4674 | { |
| 4675 | SetIsDone(false); |
| 4676 | if (OpenPipes()) |
| 4677 | { |
| 4678 | const int read_fd = m_read_file.GetDescriptor(); |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 4679 | const int pipe_read_fd = m_pipe.GetReadFileDescriptor(); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4680 | TerminalState terminal_state; |
| 4681 | terminal_state.Save (read_fd, false); |
| 4682 | Terminal terminal(read_fd); |
| 4683 | terminal.SetCanonical(false); |
| 4684 | terminal.SetEcho(false); |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4685 | // FD_ZERO, FD_SET are not supported on windows |
Hafiz Abid Qadeer | 6eff101 | 2014-03-12 10:45:23 +0000 | [diff] [blame] | 4686 | #ifndef _WIN32 |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4687 | while (!GetIsDone()) |
| 4688 | { |
| 4689 | fd_set read_fdset; |
| 4690 | FD_ZERO (&read_fdset); |
| 4691 | FD_SET (read_fd, &read_fdset); |
| 4692 | FD_SET (pipe_read_fd, &read_fdset); |
| 4693 | const int nfds = std::max<int>(read_fd, pipe_read_fd) + 1; |
| 4694 | int num_set_fds = select (nfds, &read_fdset, NULL, NULL, NULL); |
| 4695 | if (num_set_fds < 0) |
| 4696 | { |
| 4697 | const int select_errno = errno; |
| 4698 | |
| 4699 | if (select_errno != EINTR) |
| 4700 | SetIsDone(true); |
| 4701 | } |
| 4702 | else if (num_set_fds > 0) |
| 4703 | { |
| 4704 | char ch = 0; |
| 4705 | size_t n; |
| 4706 | if (FD_ISSET (read_fd, &read_fdset)) |
| 4707 | { |
| 4708 | n = 1; |
| 4709 | if (m_read_file.Read(&ch, n).Success() && n == 1) |
| 4710 | { |
| 4711 | if (m_write_file.Write(&ch, n).Fail() || n != 1) |
| 4712 | SetIsDone(true); |
| 4713 | } |
| 4714 | else |
| 4715 | SetIsDone(true); |
| 4716 | } |
| 4717 | if (FD_ISSET (pipe_read_fd, &read_fdset)) |
| 4718 | { |
| 4719 | // Consume the interrupt byte |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 4720 | if (m_pipe.Read (&ch, 1) == 1) |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4721 | { |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 4722 | switch (ch) |
| 4723 | { |
| 4724 | case 'q': |
| 4725 | SetIsDone(true); |
| 4726 | break; |
| 4727 | case 'i': |
| 4728 | if (StateIsRunningState(m_process->GetState())) |
| 4729 | m_process->Halt(); |
| 4730 | break; |
| 4731 | } |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4732 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4733 | } |
| 4734 | } |
| 4735 | } |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4736 | #endif |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4737 | terminal_state.Restore(); |
| 4738 | |
| 4739 | } |
| 4740 | else |
| 4741 | SetIsDone(true); |
| 4742 | } |
| 4743 | else |
| 4744 | SetIsDone(true); |
| 4745 | } |
| 4746 | |
| 4747 | // Hide any characters that have been displayed so far so async |
| 4748 | // output can be displayed. Refresh() will be called after the |
| 4749 | // output has been displayed. |
| 4750 | virtual void |
| 4751 | Hide () |
| 4752 | { |
| 4753 | |
| 4754 | } |
| 4755 | // Called when the async output has been received in order to update |
| 4756 | // the input reader (refresh the prompt and redisplay any current |
| 4757 | // line(s) that are being edited |
| 4758 | virtual void |
| 4759 | Refresh () |
| 4760 | { |
| 4761 | |
| 4762 | } |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4763 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4764 | virtual void |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4765 | Cancel () |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4766 | { |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4767 | char ch = 'q'; // Send 'q' for quit |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 4768 | m_pipe.Write (&ch, 1); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4769 | } |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4770 | |
Greg Clayton | f0066ad | 2014-05-02 00:45:31 +0000 | [diff] [blame] | 4771 | virtual bool |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4772 | Interrupt () |
| 4773 | { |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 4774 | // Do only things that are safe to do in an interrupt context (like in |
| 4775 | // a SIGINT handler), like write 1 byte to a file descriptor. This will |
| 4776 | // interrupt the IOHandlerProcessSTDIO::Run() and we can look at the byte |
| 4777 | // that was written to the pipe and then call m_process->Halt() from a |
| 4778 | // much safer location in code. |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 4779 | if (m_active) |
| 4780 | { |
| 4781 | char ch = 'i'; // Send 'i' for interrupt |
| 4782 | return m_pipe.Write (&ch, 1) == 1; |
| 4783 | } |
| 4784 | else |
| 4785 | { |
| 4786 | // This IOHandler might be pushed on the stack, but not being run currently |
| 4787 | // so do the right thing if we aren't actively watching for STDIN by sending |
| 4788 | // the interrupt to the process. Otherwise the write to the pipe above would |
| 4789 | // do nothing. This can happen when the command interpreter is running and |
| 4790 | // gets a "expression ...". It will be on the IOHandler thread and sending |
| 4791 | // the input is complete to the delegate which will cause the expression to |
| 4792 | // run, which will push the process IO handler, but not run it. |
| 4793 | |
| 4794 | if (StateIsRunningState(m_process->GetState())) |
| 4795 | { |
| 4796 | m_process->SendAsyncInterrupt(); |
| 4797 | return true; |
| 4798 | } |
| 4799 | } |
| 4800 | return false; |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4801 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4802 | |
| 4803 | virtual void |
| 4804 | GotEOF() |
| 4805 | { |
| 4806 | |
| 4807 | } |
| 4808 | |
| 4809 | protected: |
| 4810 | Process *m_process; |
| 4811 | File m_read_file; // Read from this file (usually actual STDIN for LLDB |
| 4812 | File m_write_file; // Write to this file (usually the master pty for getting io to debuggee) |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 4813 | Pipe m_pipe; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4814 | }; |
| 4815 | |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4816 | void |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4817 | Process::SetSTDIOFileDescriptor (int fd) |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4818 | { |
| 4819 | // First set up the Read Thread for reading/handling process I/O |
| 4820 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4821 | std::unique_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (fd, true)); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4822 | |
| 4823 | if (conn_ap.get()) |
| 4824 | { |
| 4825 | m_stdio_communication.SetConnection (conn_ap.release()); |
| 4826 | if (m_stdio_communication.IsConnected()) |
| 4827 | { |
| 4828 | m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this); |
| 4829 | m_stdio_communication.StartReadThread(); |
| 4830 | |
| 4831 | // Now read thread is set up, set up input reader. |
| 4832 | |
| 4833 | if (!m_process_input_reader.get()) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4834 | m_process_input_reader.reset (new IOHandlerProcessSTDIO (this, fd)); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4835 | } |
| 4836 | } |
| 4837 | } |
| 4838 | |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4839 | bool |
Greg Clayton | 6fea17e | 2014-03-03 19:15:20 +0000 | [diff] [blame] | 4840 | Process::ProcessIOHandlerIsActive () |
| 4841 | { |
| 4842 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 4843 | if (io_handler_sp) |
| 4844 | return m_target.GetDebugger().IsTopIOHandler (io_handler_sp); |
| 4845 | return false; |
| 4846 | } |
| 4847 | bool |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4848 | Process::PushProcessIOHandler () |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4849 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4850 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 4851 | if (io_handler_sp) |
| 4852 | { |
| 4853 | io_handler_sp->SetIsDone(false); |
| 4854 | m_target.GetDebugger().PushIOHandler (io_handler_sp); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4855 | return true; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4856 | } |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4857 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4860 | bool |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4861 | Process::PopProcessIOHandler () |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4862 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4863 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 4864 | if (io_handler_sp) |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4865 | return m_target.GetDebugger().PopIOHandler (io_handler_sp); |
| 4866 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 4869 | // The process needs to know about installed plug-ins |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4870 | void |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 4871 | Process::SettingsInitialize () |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4872 | { |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 4873 | Thread::SettingsInitialize (); |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4874 | } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4875 | |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4876 | void |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 4877 | Process::SettingsTerminate () |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 4878 | { |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 4879 | Thread::SettingsTerminate (); |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 4880 | } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4881 | |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 4882 | ExpressionResults |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4883 | Process::RunThreadPlan (ExecutionContext &exe_ctx, |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4884 | lldb::ThreadPlanSP &thread_plan_sp, |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 4885 | const EvaluateExpressionOptions &options, |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4886 | Stream &errors) |
| 4887 | { |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 4888 | ExpressionResults return_value = eExpressionSetupError; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4889 | |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 4890 | if (thread_plan_sp.get() == NULL) |
| 4891 | { |
| 4892 | errors.Printf("RunThreadPlan called with empty thread plan."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 4893 | return eExpressionSetupError; |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 4894 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4895 | |
Jim Ingham | 7d7931d | 2013-03-28 00:05:34 +0000 | [diff] [blame] | 4896 | if (!thread_plan_sp->ValidatePlan(NULL)) |
| 4897 | { |
| 4898 | errors.Printf ("RunThreadPlan called with an invalid thread plan."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 4899 | return eExpressionSetupError; |
Jim Ingham | 7d7931d | 2013-03-28 00:05:34 +0000 | [diff] [blame] | 4900 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4901 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4902 | if (exe_ctx.GetProcessPtr() != this) |
| 4903 | { |
| 4904 | errors.Printf("RunThreadPlan called on wrong process."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 4905 | return eExpressionSetupError; |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
| 4908 | Thread *thread = exe_ctx.GetThreadPtr(); |
| 4909 | if (thread == NULL) |
| 4910 | { |
| 4911 | errors.Printf("RunThreadPlan called with invalid thread."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 4912 | return eExpressionSetupError; |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4913 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4914 | |
Jim Ingham | 17e5c4e | 2011-05-17 22:24:54 +0000 | [diff] [blame] | 4915 | // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes. |
| 4916 | // For that to be true the plan can't be private - since private plans suppress themselves in the |
| 4917 | // GetCompletedPlan call. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4918 | |
Jim Ingham | 17e5c4e | 2011-05-17 22:24:54 +0000 | [diff] [blame] | 4919 | bool orig_plan_private = thread_plan_sp->GetPrivate(); |
| 4920 | thread_plan_sp->SetPrivate(false); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4921 | |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 4922 | if (m_private_state.GetValue() != eStateStopped) |
| 4923 | { |
| 4924 | errors.Printf ("RunThreadPlan called while the private state was not stopped."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 4925 | return eExpressionSetupError; |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 4926 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4927 | |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 4928 | // Save the thread & frame from the exe_ctx for restoration after we run |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4929 | const uint32_t thread_idx_id = thread->GetIndexID(); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 4930 | StackFrameSP selected_frame_sp = thread->GetSelectedFrame(); |
Jim Ingham | 11b0e05 | 2013-02-19 23:22:45 +0000 | [diff] [blame] | 4931 | if (!selected_frame_sp) |
| 4932 | { |
| 4933 | thread->SetSelectedFrame(0); |
| 4934 | selected_frame_sp = thread->GetSelectedFrame(); |
| 4935 | if (!selected_frame_sp) |
| 4936 | { |
| 4937 | errors.Printf("RunThreadPlan called without a selected frame on thread %d", thread_idx_id); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 4938 | return eExpressionSetupError; |
Jim Ingham | 11b0e05 | 2013-02-19 23:22:45 +0000 | [diff] [blame] | 4939 | } |
| 4940 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4941 | |
Jim Ingham | 11b0e05 | 2013-02-19 23:22:45 +0000 | [diff] [blame] | 4942 | StackID ctx_frame_id = selected_frame_sp->GetStackID(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4943 | |
| 4944 | // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either, |
| 4945 | // so we should arrange to reset them as well. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4946 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4947 | lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4948 | |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 4949 | uint32_t selected_tid; |
| 4950 | StackID selected_stack_id; |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 4951 | if (selected_thread_sp) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4952 | { |
| 4953 | selected_tid = selected_thread_sp->GetIndexID(); |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 4954 | selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4955 | } |
| 4956 | else |
| 4957 | { |
| 4958 | selected_tid = LLDB_INVALID_THREAD_ID; |
| 4959 | } |
| 4960 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 4961 | HostThread backup_private_state_thread; |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4962 | lldb::StateType old_state; |
| 4963 | lldb::ThreadPlanSP stopper_base_plan_sp; |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4964 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4965 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 4966 | if (m_private_state_thread.EqualsThread(Host::GetCurrentThread())) |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4967 | { |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4968 | // Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since |
| 4969 | // we are the thread that is generating public events. |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4970 | // The simplest thing to do is to spin up a temporary thread to handle private state thread events while |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4971 | // we are fielding public events here. |
| 4972 | if (log) |
Jason Molenda | d251c9d | 2012-11-17 01:41:04 +0000 | [diff] [blame] | 4973 | log->Printf ("Running thread plan on private state thread, spinning up another state thread to handle the events."); |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4974 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4975 | backup_private_state_thread = m_private_state_thread; |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4976 | |
| 4977 | // One other bit of business: we want to run just this thread plan and anything it pushes, and then stop, |
| 4978 | // returning control here. |
| 4979 | // But in the normal course of things, the plan above us on the stack would be given a shot at the stop |
| 4980 | // event before deciding to stop, and we don't want that. So we insert a "stopper" base plan on the stack |
| 4981 | // before the plan we want to run. Since base plans always stop and return control to the user, that will |
| 4982 | // do just what we want. |
| 4983 | stopper_base_plan_sp.reset(new ThreadPlanBase (*thread)); |
| 4984 | thread->QueueThreadPlan (stopper_base_plan_sp, false); |
| 4985 | // Have to make sure our public state is stopped, since otherwise the reporting logic below doesn't work correctly. |
| 4986 | old_state = m_public_state.GetValue(); |
| 4987 | m_public_state.SetValueNoLock(eStateStopped); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4988 | |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4989 | // Now spin up the private state thread: |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4990 | StartPrivateStateThread(true); |
| 4991 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4992 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4993 | thread->QueueThreadPlan(thread_plan_sp, false); // This used to pass "true" does that make sense? |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4994 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 4995 | if (options.GetDebug()) |
| 4996 | { |
| 4997 | // In this case, we aren't actually going to run, we just want to stop right away. |
| 4998 | // Flush this thread so we will refetch the stacks and show the correct backtrace. |
| 4999 | // FIXME: To make this prettier we should invent some stop reason for this, but that |
| 5000 | // is only cosmetic, and this functionality is only of use to lldb developers who can |
| 5001 | // live with not pretty... |
| 5002 | thread->Flush(); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5003 | return eExpressionStoppedForDebug; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5004 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5005 | |
Jim Ingham | 1e7a9ee | 2011-01-23 21:14:08 +0000 | [diff] [blame] | 5006 | Listener listener("lldb.process.listener.run-thread-plan"); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5007 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5008 | lldb::EventSP event_to_broadcast_sp; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5009 | |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 5010 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5011 | // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get |
| 5012 | // restored on exit to the function. |
| 5013 | // |
| 5014 | // If the event needs to propagate beyond the hijacker (e.g., the process exits during execution), then the event |
| 5015 | // is put into event_to_broadcast_sp for rebroadcasting. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5016 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5017 | ProcessEventHijacker run_thread_plan_hijacker (*this, &listener); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5018 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5019 | if (log) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5020 | { |
| 5021 | StreamString s; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5022 | thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5023 | log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4" PRIx64 " to run thread plan \"%s\".", |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5024 | thread->GetIndexID(), |
| 5025 | thread->GetID(), |
| 5026 | s.GetData()); |
| 5027 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5028 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5029 | bool got_event; |
| 5030 | lldb::EventSP event_sp; |
| 5031 | lldb::StateType stop_state = lldb::eStateInvalid; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5032 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5033 | TimeValue* timeout_ptr = NULL; |
| 5034 | TimeValue real_timeout; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5035 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5036 | bool before_first_timeout = true; // This is set to false the first time that we have to halt the target. |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5037 | bool do_resume = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5038 | bool handle_running_event = true; |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5039 | const uint64_t default_one_thread_timeout_usec = 250000; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5040 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5041 | // This is just for accounting: |
| 5042 | uint32_t num_resumes = 0; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5043 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5044 | uint32_t timeout_usec = options.GetTimeoutUsec(); |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5045 | uint32_t one_thread_timeout_usec; |
| 5046 | uint32_t all_threads_timeout_usec = 0; |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5047 | |
| 5048 | // If we are going to run all threads the whole time, or if we are only going to run one thread, |
| 5049 | // then we don't need the first timeout. So we set the final timeout, and pretend we are after the |
| 5050 | // first timeout already. |
| 5051 | |
| 5052 | if (!options.GetStopOthers() || !options.GetTryAllThreads()) |
Jim Ingham | 286fb1e | 2014-02-28 02:52:06 +0000 | [diff] [blame] | 5053 | { |
| 5054 | before_first_timeout = false; |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5055 | one_thread_timeout_usec = 0; |
| 5056 | all_threads_timeout_usec = timeout_usec; |
Jim Ingham | 286fb1e | 2014-02-28 02:52:06 +0000 | [diff] [blame] | 5057 | } |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5058 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5059 | { |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5060 | uint32_t option_one_thread_timeout = options.GetOneThreadTimeoutUsec(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5061 | |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5062 | // If the overall wait is forever, then we only need to set the one thread timeout: |
| 5063 | if (timeout_usec == 0) |
| 5064 | { |
Ed Maste | 801335c | 2014-03-31 19:28:14 +0000 | [diff] [blame] | 5065 | if (option_one_thread_timeout != 0) |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5066 | one_thread_timeout_usec = option_one_thread_timeout; |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5067 | else |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5068 | one_thread_timeout_usec = default_one_thread_timeout_usec; |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5069 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5070 | else |
| 5071 | { |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5072 | // Otherwise, if the one thread timeout is set, make sure it isn't longer than the overall timeout, |
| 5073 | // and use it, otherwise use half the total timeout, bounded by the default_one_thread_timeout_usec. |
| 5074 | uint64_t computed_one_thread_timeout; |
| 5075 | if (option_one_thread_timeout != 0) |
| 5076 | { |
| 5077 | if (timeout_usec < option_one_thread_timeout) |
| 5078 | { |
| 5079 | errors.Printf("RunThreadPlan called without one thread timeout greater than total timeout"); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5080 | return eExpressionSetupError; |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5081 | } |
| 5082 | computed_one_thread_timeout = option_one_thread_timeout; |
| 5083 | } |
| 5084 | else |
| 5085 | { |
| 5086 | computed_one_thread_timeout = timeout_usec / 2; |
| 5087 | if (computed_one_thread_timeout > default_one_thread_timeout_usec) |
| 5088 | computed_one_thread_timeout = default_one_thread_timeout_usec; |
| 5089 | } |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5090 | one_thread_timeout_usec = computed_one_thread_timeout; |
| 5091 | all_threads_timeout_usec = timeout_usec - one_thread_timeout_usec; |
| 5092 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5093 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5094 | } |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5095 | |
| 5096 | if (log) |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5097 | log->Printf ("Stop others: %u, try all: %u, before_first: %u, one thread: %" PRIu32 " - all threads: %" PRIu32 ".\n", |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5098 | options.GetStopOthers(), |
| 5099 | options.GetTryAllThreads(), |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5100 | before_first_timeout, |
| 5101 | one_thread_timeout_usec, |
| 5102 | all_threads_timeout_usec); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5103 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5104 | // This isn't going to work if there are unfetched events on the queue. |
| 5105 | // Are there cases where we might want to run the remaining events here, and then try to |
| 5106 | // call the function? That's probably being too tricky for our own good. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5107 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5108 | Event *other_events = listener.PeekAtNextEvent(); |
| 5109 | if (other_events != NULL) |
| 5110 | { |
| 5111 | errors.Printf("Calling RunThreadPlan with pending events on the queue."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5112 | return eExpressionSetupError; |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5113 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5114 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5115 | // We also need to make sure that the next event is delivered. We might be calling a function as part of |
| 5116 | // a thread plan, in which case the last delivered event could be the running event, and we don't want |
| 5117 | // event coalescing to cause us to lose OUR running event... |
| 5118 | ForceNextEventDelivery(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5119 | |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 5120 | // This while loop must exit out the bottom, there's cleanup that we need to do when we are done. |
| 5121 | // So don't call return anywhere within it. |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 5122 | |
| 5123 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
| 5124 | // It's pretty much impossible to write test cases for things like: |
| 5125 | // One thread timeout expires, I go to halt, but the process already stopped |
| 5126 | // on the function call stop breakpoint. Turning on this define will make us not |
| 5127 | // fetch the first event till after the halt. So if you run a quick function, it will have |
| 5128 | // completed, and the completion event will be waiting, when you interrupt for halt. |
| 5129 | // The expression evaluation should still succeed. |
| 5130 | bool miss_first_event = true; |
| 5131 | #endif |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5132 | TimeValue one_thread_timeout; |
| 5133 | TimeValue final_timeout; |
| 5134 | |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 5135 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5136 | while (1) |
| 5137 | { |
| 5138 | // We usually want to resume the process if we get to the top of the loop. |
| 5139 | // The only exception is if we get two running events with no intervening |
| 5140 | // stop, which can happen, we will just wait for then next stop event. |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5141 | if (log) |
| 5142 | log->Printf ("Top of while loop: do_resume: %i handle_running_event: %i before_first_timeout: %i.", |
| 5143 | do_resume, |
| 5144 | handle_running_event, |
| 5145 | before_first_timeout); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5146 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5147 | if (do_resume || handle_running_event) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5148 | { |
| 5149 | // Do the initial resume and wait for the running event before going further. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5150 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5151 | if (do_resume) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5152 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5153 | num_resumes++; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5154 | Error resume_error = PrivateResume (); |
| 5155 | if (!resume_error.Success()) |
| 5156 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5157 | errors.Printf("Error resuming inferior the %d time: \"%s\".\n", |
| 5158 | num_resumes, |
| 5159 | resume_error.AsCString()); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5160 | return_value = eExpressionSetupError; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5161 | break; |
| 5162 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5163 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5164 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5165 | TimeValue resume_timeout = TimeValue::Now(); |
| 5166 | resume_timeout.OffsetWithMicroSeconds(500000); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5167 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5168 | got_event = listener.WaitForEvent(&resume_timeout, event_sp); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5169 | if (!got_event) |
| 5170 | { |
| 5171 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5172 | log->Printf ("Process::RunThreadPlan(): didn't get any event after resume %d, exiting.", |
| 5173 | num_resumes); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5174 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5175 | errors.Printf("Didn't get any event after resume %d, exiting.", num_resumes); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5176 | return_value = eExpressionSetupError; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5177 | break; |
| 5178 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5179 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5180 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5181 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5182 | if (stop_state != eStateRunning) |
| 5183 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5184 | bool restarted = false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5185 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5186 | if (stop_state == eStateStopped) |
| 5187 | { |
| 5188 | restarted = Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()); |
| 5189 | if (log) |
| 5190 | log->Printf("Process::RunThreadPlan(): didn't get running event after " |
| 5191 | "resume %d, got %s instead (restarted: %i, do_resume: %i, handle_running_event: %i).", |
| 5192 | num_resumes, |
| 5193 | StateAsCString(stop_state), |
| 5194 | restarted, |
| 5195 | do_resume, |
| 5196 | handle_running_event); |
| 5197 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5198 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5199 | if (restarted) |
| 5200 | { |
| 5201 | // This is probably an overabundance of caution, I don't think I should ever get a stopped & restarted |
| 5202 | // event here. But if I do, the best thing is to Halt and then get out of here. |
| 5203 | Halt(); |
| 5204 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5205 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5206 | errors.Printf("Didn't get running event after initial resume, got %s instead.", |
| 5207 | StateAsCString(stop_state)); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5208 | return_value = eExpressionSetupError; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5209 | break; |
| 5210 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5211 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5212 | if (log) |
| 5213 | log->PutCString ("Process::RunThreadPlan(): resuming succeeded."); |
| 5214 | // We need to call the function synchronously, so spin waiting for it to return. |
| 5215 | // If we get interrupted while executing, we're going to lose our context, and |
| 5216 | // won't be able to gather the result at this point. |
| 5217 | // We set the timeout AFTER the resume, since the resume takes some time and we |
| 5218 | // don't want to charge that to the timeout. |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5219 | } |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5220 | else |
| 5221 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5222 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5223 | log->PutCString ("Process::RunThreadPlan(): waiting for next event."); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5224 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5225 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5226 | if (before_first_timeout) |
| 5227 | { |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5228 | if (options.GetTryAllThreads()) |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5229 | { |
| 5230 | one_thread_timeout = TimeValue::Now(); |
| 5231 | one_thread_timeout.OffsetWithMicroSeconds(one_thread_timeout_usec); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5232 | timeout_ptr = &one_thread_timeout; |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5233 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5234 | else |
| 5235 | { |
| 5236 | if (timeout_usec == 0) |
| 5237 | timeout_ptr = NULL; |
| 5238 | else |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5239 | { |
| 5240 | final_timeout = TimeValue::Now(); |
| 5241 | final_timeout.OffsetWithMicroSeconds (timeout_usec); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5242 | timeout_ptr = &final_timeout; |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5243 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5244 | } |
| 5245 | } |
| 5246 | else |
| 5247 | { |
| 5248 | if (timeout_usec == 0) |
| 5249 | timeout_ptr = NULL; |
| 5250 | else |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5251 | { |
| 5252 | final_timeout = TimeValue::Now(); |
| 5253 | final_timeout.OffsetWithMicroSeconds (all_threads_timeout_usec); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5254 | timeout_ptr = &final_timeout; |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5255 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5256 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5257 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5258 | do_resume = true; |
| 5259 | handle_running_event = true; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5260 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5261 | // Now wait for the process to stop again: |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5262 | event_sp.reset(); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5263 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5264 | if (log) |
Jim Ingham | 20829ac | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 5265 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5266 | if (timeout_ptr) |
| 5267 | { |
Matt Kopec | 676a487 | 2013-02-21 23:55:31 +0000 | [diff] [blame] | 5268 | log->Printf ("Process::RunThreadPlan(): about to wait - now is %" PRIu64 " - endpoint is %" PRIu64, |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5269 | TimeValue::Now().GetAsMicroSecondsSinceJan1_1970(), |
| 5270 | timeout_ptr->GetAsMicroSecondsSinceJan1_1970()); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5271 | } |
Jim Ingham | 20829ac | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 5272 | else |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5273 | { |
| 5274 | log->Printf ("Process::RunThreadPlan(): about to wait forever."); |
| 5275 | } |
| 5276 | } |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 5277 | |
| 5278 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
| 5279 | // See comment above... |
| 5280 | if (miss_first_event) |
| 5281 | { |
| 5282 | usleep(1000); |
| 5283 | miss_first_event = false; |
| 5284 | got_event = false; |
| 5285 | } |
| 5286 | else |
| 5287 | #endif |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5288 | got_event = listener.WaitForEvent (timeout_ptr, event_sp); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5289 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5290 | if (got_event) |
| 5291 | { |
| 5292 | if (event_sp.get()) |
| 5293 | { |
| 5294 | bool keep_going = false; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5295 | if (event_sp->GetType() == eBroadcastBitInterrupt) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5296 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5297 | Halt(); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5298 | return_value = eExpressionInterrupted; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5299 | errors.Printf ("Execution halted by user interrupt."); |
| 5300 | if (log) |
| 5301 | log->Printf ("Process::RunThreadPlan(): Got interrupted by eBroadcastBitInterrupted, exiting."); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5302 | break; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5303 | } |
| 5304 | else |
| 5305 | { |
| 5306 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5307 | if (log) |
| 5308 | log->Printf("Process::RunThreadPlan(): in while loop, got event: %s.", StateAsCString(stop_state)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5309 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5310 | switch (stop_state) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5311 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5312 | case lldb::eStateStopped: |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5313 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5314 | // We stopped, figure out what we are going to do now. |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5315 | ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id); |
| 5316 | if (!thread_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5317 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5318 | // Ooh, our thread has vanished. Unlikely that this was successful execution... |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5319 | if (log) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5320 | log->Printf ("Process::RunThreadPlan(): execution completed but our thread (index-id=%u) has vanished.", thread_idx_id); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5321 | return_value = eExpressionInterrupted; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5322 | } |
| 5323 | else |
| 5324 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5325 | // If we were restarted, we just need to go back up to fetch another event. |
| 5326 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5327 | { |
| 5328 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5329 | { |
| 5330 | log->Printf ("Process::RunThreadPlan(): Got a stop and restart, so we'll continue waiting."); |
| 5331 | } |
| 5332 | keep_going = true; |
| 5333 | do_resume = false; |
| 5334 | handle_running_event = true; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5335 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5336 | } |
| 5337 | else |
| 5338 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5339 | StopInfoSP stop_info_sp (thread_sp->GetStopInfo ()); |
| 5340 | StopReason stop_reason = eStopReasonInvalid; |
| 5341 | if (stop_info_sp) |
| 5342 | stop_reason = stop_info_sp->GetStopReason(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5343 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5344 | // FIXME: We only check if the stop reason is plan complete, should we make sure that |
| 5345 | // it is OUR plan that is complete? |
| 5346 | if (stop_reason == eStopReasonPlanComplete) |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5347 | { |
| 5348 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5349 | log->PutCString ("Process::RunThreadPlan(): execution completed successfully."); |
| 5350 | // Now mark this plan as private so it doesn't get reported as the stop reason |
| 5351 | // after this point. |
| 5352 | if (thread_plan_sp) |
| 5353 | thread_plan_sp->SetPrivate (orig_plan_private); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5354 | return_value = eExpressionCompleted; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5355 | } |
| 5356 | else |
| 5357 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5358 | // Something restarted the target, so just wait for it to stop for real. |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5359 | if (stop_reason == eStopReasonBreakpoint) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5360 | { |
| 5361 | if (log) |
| 5362 | log->Printf ("Process::RunThreadPlan() stopped for breakpoint: %s.", stop_info_sp->GetDescription()); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5363 | return_value = eExpressionHitBreakpoint; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5364 | if (!options.DoesIgnoreBreakpoints()) |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 5365 | { |
| 5366 | event_to_broadcast_sp = event_sp; |
| 5367 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5368 | } |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5369 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5370 | { |
| 5371 | if (log) |
| 5372 | log->PutCString ("Process::RunThreadPlan(): thread plan didn't successfully complete."); |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5373 | if (!options.DoesUnwindOnError()) |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 5374 | event_to_broadcast_sp = event_sp; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5375 | return_value = eExpressionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5376 | } |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5377 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5378 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5379 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5380 | } |
| 5381 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5382 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5383 | case lldb::eStateRunning: |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5384 | // This shouldn't really happen, but sometimes we do get two running events without an |
| 5385 | // intervening stop, and in that case we should just go back to waiting for the stop. |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5386 | do_resume = false; |
| 5387 | keep_going = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5388 | handle_running_event = false; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5389 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5390 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5391 | default: |
| 5392 | if (log) |
| 5393 | log->Printf("Process::RunThreadPlan(): execution stopped with unexpected state: %s.", StateAsCString(stop_state)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5394 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5395 | if (stop_state == eStateExited) |
| 5396 | event_to_broadcast_sp = event_sp; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5397 | |
Sean Callanan | bf154da | 2012-08-08 17:35:10 +0000 | [diff] [blame] | 5398 | errors.Printf ("Execution stopped with unexpected state.\n"); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5399 | return_value = eExpressionInterrupted; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5400 | break; |
| 5401 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5402 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5403 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5404 | if (keep_going) |
| 5405 | continue; |
| 5406 | else |
| 5407 | break; |
| 5408 | } |
| 5409 | else |
| 5410 | { |
| 5411 | if (log) |
| 5412 | log->PutCString ("Process::RunThreadPlan(): got_event was true, but the event pointer was null. How odd..."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5413 | return_value = eExpressionInterrupted; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5414 | break; |
| 5415 | } |
| 5416 | } |
| 5417 | else |
| 5418 | { |
| 5419 | // If we didn't get an event that means we've timed out... |
| 5420 | // We will interrupt the process here. Depending on what we were asked to do we will |
| 5421 | // either exit, or try with all threads running for the same timeout. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5422 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5423 | if (log) { |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5424 | if (options.GetTryAllThreads()) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5425 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5426 | if (before_first_timeout) |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5427 | { |
| 5428 | if (timeout_usec != 0) |
| 5429 | { |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5430 | log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, " |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5431 | "running for %" PRIu32 " usec with all threads enabled.", |
| 5432 | all_threads_timeout_usec); |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5433 | } |
| 5434 | else |
| 5435 | { |
| 5436 | log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, " |
Ed Maste | e61c7b0 | 2014-04-29 17:48:06 +0000 | [diff] [blame] | 5437 | "running forever with all threads enabled."); |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5438 | } |
| 5439 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5440 | else |
| 5441 | log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled " |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5442 | "and timeout: %u timed out, abandoning execution.", |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5443 | timeout_usec); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5444 | } |
| 5445 | else |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5446 | log->Printf ("Process::RunThreadPlan(): Running function with timeout: %u timed out, " |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5447 | "abandoning execution.", |
| 5448 | timeout_usec); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5449 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5450 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5451 | // It is possible that between the time we issued the Halt, and we get around to calling Halt the target |
| 5452 | // could have stopped. That's fine, Halt will figure that out and send the appropriate Stopped event. |
| 5453 | // BUT it is also possible that we stopped & restarted (e.g. hit a signal with "stop" set to false.) In |
| 5454 | // that case, we'll get the stopped & restarted event, and we should go back to waiting for the Halt's |
| 5455 | // stopped event. That's what this while loop does. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5456 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5457 | bool back_to_top = true; |
| 5458 | uint32_t try_halt_again = 0; |
| 5459 | bool do_halt = true; |
| 5460 | const uint32_t num_retries = 5; |
| 5461 | while (try_halt_again < num_retries) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5462 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5463 | Error halt_error; |
| 5464 | if (do_halt) |
| 5465 | { |
| 5466 | if (log) |
| 5467 | log->Printf ("Process::RunThreadPlan(): Running Halt."); |
| 5468 | halt_error = Halt(); |
| 5469 | } |
| 5470 | if (halt_error.Success()) |
| 5471 | { |
| 5472 | if (log) |
| 5473 | log->PutCString ("Process::RunThreadPlan(): Halt succeeded."); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5474 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5475 | real_timeout = TimeValue::Now(); |
| 5476 | real_timeout.OffsetWithMicroSeconds(500000); |
| 5477 | |
| 5478 | got_event = listener.WaitForEvent(&real_timeout, event_sp); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5479 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5480 | if (got_event) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5481 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5482 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5483 | if (log) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5484 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5485 | log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state)); |
| 5486 | if (stop_state == lldb::eStateStopped |
| 5487 | && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get())) |
| 5488 | log->PutCString (" Event was the Halt interruption event."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5489 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5490 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5491 | if (stop_state == lldb::eStateStopped) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5492 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5493 | // Between the time we initiated the Halt and the time we delivered it, the process could have |
| 5494 | // already finished its job. Check that here: |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5495 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5496 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
| 5497 | { |
| 5498 | if (log) |
| 5499 | log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. " |
| 5500 | "Exiting wait loop."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5501 | return_value = eExpressionCompleted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5502 | back_to_top = false; |
| 5503 | break; |
| 5504 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5505 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5506 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 5507 | { |
| 5508 | if (log) |
| 5509 | log->PutCString ("Process::RunThreadPlan(): Went to halt but got a restarted event, there must be an un-restarted stopped event so try again... " |
| 5510 | "Exiting wait loop."); |
| 5511 | try_halt_again++; |
| 5512 | do_halt = false; |
| 5513 | continue; |
| 5514 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5515 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5516 | if (!options.GetTryAllThreads()) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5517 | { |
| 5518 | if (log) |
| 5519 | log->PutCString ("Process::RunThreadPlan(): try_all_threads was false, we stopped so now we're quitting."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5520 | return_value = eExpressionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5521 | back_to_top = false; |
| 5522 | break; |
| 5523 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5524 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5525 | if (before_first_timeout) |
| 5526 | { |
| 5527 | // Set all the other threads to run, and return to the top of the loop, which will continue; |
| 5528 | before_first_timeout = false; |
| 5529 | thread_plan_sp->SetStopOthers (false); |
| 5530 | if (log) |
| 5531 | log->PutCString ("Process::RunThreadPlan(): about to resume."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5532 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5533 | back_to_top = true; |
| 5534 | break; |
| 5535 | } |
| 5536 | else |
| 5537 | { |
| 5538 | // Running all threads failed, so return Interrupted. |
| 5539 | if (log) |
| 5540 | log->PutCString("Process::RunThreadPlan(): running all threads timed out."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5541 | return_value = eExpressionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5542 | back_to_top = false; |
| 5543 | break; |
| 5544 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5545 | } |
| 5546 | } |
| 5547 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5548 | { if (log) |
| 5549 | log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. " |
| 5550 | "I'm getting out of here passing Interrupted."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5551 | return_value = eExpressionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5552 | back_to_top = false; |
| 5553 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5554 | } |
| 5555 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5556 | else |
| 5557 | { |
| 5558 | try_halt_again++; |
| 5559 | continue; |
| 5560 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5561 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5562 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5563 | if (!back_to_top || try_halt_again > num_retries) |
| 5564 | break; |
| 5565 | else |
| 5566 | continue; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5567 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5568 | } // END WAIT LOOP |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5569 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5570 | // If we had to start up a temporary private state thread to run this thread plan, shut it down now. |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 5571 | if (backup_private_state_thread.GetState() != eThreadStateInvalid) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5572 | { |
| 5573 | StopPrivateStateThread(); |
| 5574 | Error error; |
| 5575 | m_private_state_thread = backup_private_state_thread; |
Sean Callanan | 9a02851 | 2012-08-09 00:50:26 +0000 | [diff] [blame] | 5576 | if (stopper_base_plan_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5577 | { |
| 5578 | thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp); |
| 5579 | } |
| 5580 | m_public_state.SetValueNoLock(old_state); |
| 5581 | |
| 5582 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5583 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5584 | // Restore the thread state if we are going to discard the plan execution. There are three cases where this |
| 5585 | // could happen: |
| 5586 | // 1) The execution successfully completed |
| 5587 | // 2) We hit a breakpoint, and ignore_breakpoints was true |
| 5588 | // 3) We got some other error, and discard_on_error was true |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5589 | bool should_unwind = (return_value == eExpressionInterrupted && options.DoesUnwindOnError()) |
| 5590 | || (return_value == eExpressionHitBreakpoint && options.DoesIgnoreBreakpoints()); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5591 | |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5592 | if (return_value == eExpressionCompleted |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5593 | || should_unwind) |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 5594 | { |
| 5595 | thread_plan_sp->RestoreThreadState(); |
| 5596 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5597 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5598 | // Now do some processing on the results of the run: |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5599 | if (return_value == eExpressionInterrupted || return_value == eExpressionHitBreakpoint) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5600 | { |
| 5601 | if (log) |
| 5602 | { |
| 5603 | StreamString s; |
| 5604 | if (event_sp) |
| 5605 | event_sp->Dump (&s); |
| 5606 | else |
| 5607 | { |
| 5608 | log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL."); |
| 5609 | } |
| 5610 | |
| 5611 | StreamString ts; |
| 5612 | |
| 5613 | const char *event_explanation = NULL; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5614 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5615 | do |
| 5616 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5617 | if (!event_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5618 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5619 | event_explanation = "<no event>"; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5620 | break; |
| 5621 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5622 | else if (event_sp->GetType() == eBroadcastBitInterrupt) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5623 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5624 | event_explanation = "<user interrupt>"; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5625 | break; |
| 5626 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5627 | else |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5628 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5629 | const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get()); |
| 5630 | |
| 5631 | if (!event_data) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5632 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5633 | event_explanation = "<no event data>"; |
| 5634 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5635 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5636 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5637 | Process *process = event_data->GetProcessSP().get(); |
| 5638 | |
| 5639 | if (!process) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5640 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5641 | event_explanation = "<no process>"; |
| 5642 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5643 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5644 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5645 | ThreadList &thread_list = process->GetThreadList(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5646 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5647 | uint32_t num_threads = thread_list.GetSize(); |
| 5648 | uint32_t thread_index; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5649 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5650 | ts.Printf("<%u threads> ", num_threads); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5651 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5652 | for (thread_index = 0; |
| 5653 | thread_index < num_threads; |
| 5654 | ++thread_index) |
| 5655 | { |
| 5656 | Thread *thread = thread_list.GetThreadAtIndex(thread_index).get(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5657 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5658 | if (!thread) |
| 5659 | { |
| 5660 | ts.Printf("<?> "); |
| 5661 | continue; |
| 5662 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5663 | |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5664 | ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5665 | RegisterContext *register_context = thread->GetRegisterContext().get(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5666 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5667 | if (register_context) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5668 | ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5669 | else |
| 5670 | ts.Printf("[ip unknown] "); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5671 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5672 | lldb::StopInfoSP stop_info_sp = thread->GetStopInfo(); |
| 5673 | if (stop_info_sp) |
| 5674 | { |
| 5675 | const char *stop_desc = stop_info_sp->GetDescription(); |
| 5676 | if (stop_desc) |
| 5677 | ts.PutCString (stop_desc); |
| 5678 | } |
| 5679 | ts.Printf(">"); |
| 5680 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5681 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5682 | event_explanation = ts.GetData(); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5683 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5684 | } while (0); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5685 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5686 | if (event_explanation) |
| 5687 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5688 | else |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5689 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData()); |
| 5690 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5691 | |
Jim Ingham | e4483cf | 2013-09-27 01:13:01 +0000 | [diff] [blame] | 5692 | if (should_unwind) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5693 | { |
| 5694 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5695 | log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - discarding thread plans up to %p.", |
| 5696 | static_cast<void*>(thread_plan_sp.get())); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5697 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
| 5698 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 5699 | } |
| 5700 | else |
| 5701 | { |
| 5702 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5703 | log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - for plan: %p not discarding.", |
| 5704 | static_cast<void*>(thread_plan_sp.get())); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5705 | } |
| 5706 | } |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5707 | else if (return_value == eExpressionSetupError) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5708 | { |
| 5709 | if (log) |
| 5710 | log->PutCString("Process::RunThreadPlan(): execution set up error."); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5711 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5712 | if (options.DoesUnwindOnError()) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5713 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5714 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 5715 | thread_plan_sp->SetPrivate (orig_plan_private); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5716 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5717 | } |
| 5718 | else |
| 5719 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5720 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5721 | { |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5722 | if (log) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5723 | log->PutCString("Process::RunThreadPlan(): thread plan is done"); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5724 | return_value = eExpressionCompleted; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5725 | } |
| 5726 | else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get())) |
| 5727 | { |
| 5728 | if (log) |
| 5729 | log->PutCString("Process::RunThreadPlan(): thread plan was discarded"); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5730 | return_value = eExpressionDiscarded; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5731 | } |
| 5732 | else |
| 5733 | { |
| 5734 | if (log) |
| 5735 | log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course"); |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5736 | if (options.DoesUnwindOnError() && thread_plan_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5737 | { |
| 5738 | if (log) |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5739 | log->PutCString("Process::RunThreadPlan(): discarding thread plan 'cause unwind_on_error is set."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5740 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
| 5741 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 5742 | } |
| 5743 | } |
| 5744 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5745 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5746 | // Thread we ran the function in may have gone away because we ran the target |
| 5747 | // Check that it's still there, and if it is put it back in the context. Also restore the |
| 5748 | // frame in the context if it is still present. |
| 5749 | thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get(); |
| 5750 | if (thread) |
| 5751 | { |
| 5752 | exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id)); |
| 5753 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5754 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5755 | // Also restore the current process'es selected frame & thread, since this function calling may |
| 5756 | // be done behind the user's back. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5757 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5758 | if (selected_tid != LLDB_INVALID_THREAD_ID) |
| 5759 | { |
| 5760 | if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid()) |
| 5761 | { |
| 5762 | // We were able to restore the selected thread, now restore the frame: |
Daniel Malea | a012d3a | 2013-07-31 20:21:20 +0000 | [diff] [blame] | 5763 | Mutex::Locker lock(GetThreadList().GetMutex()); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 5764 | StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5765 | if (old_frame_sp) |
| 5766 | GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get()); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5767 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5768 | } |
| 5769 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5770 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5771 | // If the process exited during the run of the thread plan, notify everyone. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5772 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5773 | if (event_to_broadcast_sp) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5774 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5775 | if (log) |
| 5776 | log->PutCString("Process::RunThreadPlan(): rebroadcasting event."); |
| 5777 | BroadcastEvent(event_to_broadcast_sp); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5778 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5779 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5780 | return return_value; |
| 5781 | } |
| 5782 | |
| 5783 | const char * |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 5784 | Process::ExecutionResultAsCString (ExpressionResults result) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5785 | { |
| 5786 | const char *result_name; |
| 5787 | |
| 5788 | switch (result) |
| 5789 | { |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5790 | case eExpressionCompleted: |
| 5791 | result_name = "eExpressionCompleted"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5792 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5793 | case eExpressionDiscarded: |
| 5794 | result_name = "eExpressionDiscarded"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5795 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5796 | case eExpressionInterrupted: |
| 5797 | result_name = "eExpressionInterrupted"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5798 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5799 | case eExpressionHitBreakpoint: |
| 5800 | result_name = "eExpressionHitBreakpoint"; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5801 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5802 | case eExpressionSetupError: |
| 5803 | result_name = "eExpressionSetupError"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5804 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5805 | case eExpressionParseError: |
| 5806 | result_name = "eExpressionParseError"; |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 5807 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5808 | case eExpressionResultUnavailable: |
| 5809 | result_name = "eExpressionResultUnavailable"; |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 5810 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5811 | case eExpressionTimedOut: |
| 5812 | result_name = "eExpressionTimedOut"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5813 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5814 | case eExpressionStoppedForDebug: |
| 5815 | result_name = "eExpressionStoppedForDebug"; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5816 | break; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5817 | } |
| 5818 | return result_name; |
| 5819 | } |
| 5820 | |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5821 | void |
| 5822 | Process::GetStatus (Stream &strm) |
| 5823 | { |
| 5824 | const StateType state = GetState(); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 5825 | if (StateIsStoppedState(state, false)) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5826 | { |
| 5827 | if (state == eStateExited) |
| 5828 | { |
| 5829 | int exit_status = GetExitStatus(); |
| 5830 | const char *exit_description = GetExitDescription(); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5831 | strm.Printf ("Process %" PRIu64 " exited with status = %i (0x%8.8x) %s\n", |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5832 | GetID(), |
| 5833 | exit_status, |
| 5834 | exit_status, |
| 5835 | exit_description ? exit_description : ""); |
| 5836 | } |
| 5837 | else |
| 5838 | { |
| 5839 | if (state == eStateConnected) |
| 5840 | strm.Printf ("Connected to remote target.\n"); |
| 5841 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5842 | strm.Printf ("Process %" PRIu64 " %s\n", GetID(), StateAsCString (state)); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5843 | } |
| 5844 | } |
| 5845 | else |
| 5846 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5847 | strm.Printf ("Process %" PRIu64 " is running.\n", GetID()); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5848 | } |
| 5849 | } |
| 5850 | |
| 5851 | size_t |
| 5852 | Process::GetThreadStatus (Stream &strm, |
| 5853 | bool only_threads_with_stop_reason, |
| 5854 | uint32_t start_frame, |
| 5855 | uint32_t num_frames, |
| 5856 | uint32_t num_frames_with_source) |
| 5857 | { |
| 5858 | size_t num_thread_infos_dumped = 0; |
| 5859 | |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 5860 | // You can't hold the thread list lock while calling Thread::GetStatus. That very well might run code (e.g. if we need it |
| 5861 | // to get return values or arguments.) For that to work the process has to be able to acquire it. So instead copy the thread |
| 5862 | // ID's, and look them up one by one: |
| 5863 | |
| 5864 | uint32_t num_threads; |
| 5865 | std::vector<uint32_t> thread_index_array; |
| 5866 | //Scope for thread list locker; |
| 5867 | { |
| 5868 | Mutex::Locker locker (GetThreadList().GetMutex()); |
| 5869 | ThreadList &curr_thread_list = GetThreadList(); |
| 5870 | num_threads = curr_thread_list.GetSize(); |
| 5871 | uint32_t idx; |
| 5872 | thread_index_array.resize(num_threads); |
| 5873 | for (idx = 0; idx < num_threads; ++idx) |
| 5874 | thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetID(); |
| 5875 | } |
| 5876 | |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5877 | for (uint32_t i = 0; i < num_threads; i++) |
| 5878 | { |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 5879 | ThreadSP thread_sp(GetThreadList().FindThreadByID(thread_index_array[i])); |
| 5880 | if (thread_sp) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5881 | { |
| 5882 | if (only_threads_with_stop_reason) |
| 5883 | { |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 5884 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo(); |
Jim Ingham | 5d88a06 | 2012-10-16 00:09:33 +0000 | [diff] [blame] | 5885 | if (stop_info_sp.get() == NULL || !stop_info_sp->IsValid()) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5886 | continue; |
| 5887 | } |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 5888 | thread_sp->GetStatus (strm, |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5889 | start_frame, |
| 5890 | num_frames, |
| 5891 | num_frames_with_source); |
| 5892 | ++num_thread_infos_dumped; |
| 5893 | } |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 5894 | else |
| 5895 | { |
| 5896 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 5897 | if (log) |
| 5898 | log->Printf("Process::GetThreadStatus - thread 0x" PRIu64 " vanished while running Thread::GetStatus."); |
| 5899 | |
| 5900 | } |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 5901 | } |
| 5902 | return num_thread_infos_dumped; |
| 5903 | } |
| 5904 | |
Greg Clayton | a9f40ad | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 5905 | void |
| 5906 | Process::AddInvalidMemoryRegion (const LoadRange ®ion) |
| 5907 | { |
| 5908 | m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 5909 | } |
| 5910 | |
| 5911 | bool |
| 5912 | Process::RemoveInvalidMemoryRange (const LoadRange ®ion) |
| 5913 | { |
| 5914 | return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 5915 | } |
| 5916 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5917 | void |
| 5918 | Process::AddPreResumeAction (PreResumeActionCallback callback, void *baton) |
| 5919 | { |
| 5920 | m_pre_resume_actions.push_back(PreResumeCallbackAndBaton (callback, baton)); |
| 5921 | } |
| 5922 | |
| 5923 | bool |
| 5924 | Process::RunPreResumeActions () |
| 5925 | { |
| 5926 | bool result = true; |
| 5927 | while (!m_pre_resume_actions.empty()) |
| 5928 | { |
| 5929 | struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back(); |
| 5930 | m_pre_resume_actions.pop_back(); |
| 5931 | bool this_result = action.callback (action.baton); |
| 5932 | if (result == true) result = this_result; |
| 5933 | } |
| 5934 | return result; |
| 5935 | } |
| 5936 | |
| 5937 | void |
| 5938 | Process::ClearPreResumeActions () |
| 5939 | { |
| 5940 | m_pre_resume_actions.clear(); |
| 5941 | } |
Greg Clayton | a9f40ad | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 5942 | |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 5943 | void |
| 5944 | Process::Flush () |
| 5945 | { |
| 5946 | m_thread_list.Flush(); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 5947 | m_extended_thread_list.Flush(); |
| 5948 | m_extended_thread_stop_id = 0; |
| 5949 | m_queue_list.Clear(); |
| 5950 | m_queue_list_stop_id = 0; |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 5951 | } |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5952 | |
| 5953 | void |
| 5954 | Process::DidExec () |
| 5955 | { |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 5956 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 5957 | if (log) |
| 5958 | log->Printf ("Process::%s()", __FUNCTION__); |
| 5959 | |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5960 | Target &target = GetTarget(); |
| 5961 | target.CleanupProcess (); |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame] | 5962 | target.ClearModules(false); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5963 | m_dynamic_checkers_ap.reset(); |
| 5964 | m_abi_sp.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 5965 | m_system_runtime_ap.reset(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5966 | m_os_ap.reset(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 5967 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 5968 | m_jit_loaders_ap.reset(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5969 | m_image_tokens.clear(); |
| 5970 | m_allocated_memory_cache.Clear(); |
| 5971 | m_language_runtimes.clear(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 5972 | m_thread_list.DiscardThreadPlans(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 5973 | m_memory_cache.Clear(true); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5974 | DoDidExec(); |
| 5975 | CompleteAttach (); |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 5976 | // Flush the process (threads and all stack frames) after running CompleteAttach() |
| 5977 | // in case the dynamic loader loaded things in new locations. |
| 5978 | Flush(); |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame] | 5979 | |
| 5980 | // After we figure out what was loaded/unloaded in CompleteAttach, |
| 5981 | // we need to let the target know so it can do any cleanup it needs to. |
| 5982 | target.DidExec(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 5983 | } |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 5984 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5985 | addr_t |
| 5986 | Process::ResolveIndirectFunction(const Address *address, Error &error) |
| 5987 | { |
| 5988 | if (address == nullptr) |
| 5989 | { |
Jean-Daniel Dupas | ef37711f | 2014-02-08 20:22:05 +0000 | [diff] [blame] | 5990 | error.SetErrorString("Invalid address argument"); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5991 | return LLDB_INVALID_ADDRESS; |
| 5992 | } |
| 5993 | |
| 5994 | addr_t function_addr = LLDB_INVALID_ADDRESS; |
| 5995 | |
| 5996 | addr_t addr = address->GetLoadAddress(&GetTarget()); |
| 5997 | std::map<addr_t,addr_t>::const_iterator iter = m_resolved_indirect_addresses.find(addr); |
| 5998 | if (iter != m_resolved_indirect_addresses.end()) |
| 5999 | { |
| 6000 | function_addr = (*iter).second; |
| 6001 | } |
| 6002 | else |
| 6003 | { |
| 6004 | if (!InferiorCall(this, address, function_addr)) |
| 6005 | { |
| 6006 | Symbol *symbol = address->CalculateSymbolContextSymbol(); |
| 6007 | error.SetErrorStringWithFormat ("Unable to call resolver for indirect function %s", |
| 6008 | symbol ? symbol->GetName().AsCString() : "<UNKNOWN>"); |
| 6009 | function_addr = LLDB_INVALID_ADDRESS; |
| 6010 | } |
| 6011 | else |
| 6012 | { |
| 6013 | m_resolved_indirect_addresses.insert(std::pair<addr_t, addr_t>(addr, function_addr)); |
| 6014 | } |
| 6015 | } |
| 6016 | return function_addr; |
| 6017 | } |
| 6018 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 6019 | void |
| 6020 | Process::ModulesDidLoad (ModuleList &module_list) |
| 6021 | { |
| 6022 | SystemRuntime *sys_runtime = GetSystemRuntime(); |
| 6023 | if (sys_runtime) |
| 6024 | { |
| 6025 | sys_runtime->ModulesDidLoad (module_list); |
| 6026 | } |
| 6027 | |
| 6028 | GetJITLoaders().ModulesDidLoad (module_list); |
| 6029 | } |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 6030 | |
| 6031 | ThreadCollectionSP |
| 6032 | Process::GetHistoryThreads(lldb::addr_t addr) |
| 6033 | { |
| 6034 | ThreadCollectionSP threads; |
| 6035 | |
| 6036 | const MemoryHistorySP &memory_history = MemoryHistory::FindPlugin(shared_from_this()); |
| 6037 | |
| 6038 | if (! memory_history.get()) { |
| 6039 | return threads; |
| 6040 | } |
| 6041 | |
| 6042 | threads.reset(new ThreadCollection(memory_history->GetHistoryThreads(addr))); |
| 6043 | |
| 6044 | return threads; |
| 6045 | } |