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" |
| 19 | #include "lldb/Core/Debugger.h" |
| 20 | #include "lldb/Core/Log.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 21 | #include "lldb/Core/Module.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Core/PluginManager.h" |
| 23 | #include "lldb/Core/State.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 24 | #include "lldb/Core/StreamFile.h" |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 25 | #include "lldb/Expression/ClangUserExpression.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 26 | #include "lldb/Host/ConnectionFileDescriptor.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Host/Host.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 28 | #include "lldb/Host/HostInfo.h" |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 29 | #include "lldb/Host/Pipe.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 30 | #include "lldb/Host/Terminal.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 31 | #include "lldb/Host/ThreadLauncher.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 32 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 33 | #include "lldb/Symbol/Symbol.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" |
Kuba Brecka | afdf842 | 2014-10-10 23:43:03 +0000 | [diff] [blame] | 51 | #include "lldb/Target/InstrumentationRuntime.h" |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 52 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | |
| 54 | using namespace lldb; |
| 55 | using namespace lldb_private; |
| 56 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 57 | |
| 58 | // Comment out line below to disable memory caching, overriding the process setting |
| 59 | // target.process.disable-memory-cache |
| 60 | #define ENABLE_MEMORY_CACHING |
| 61 | |
| 62 | #ifdef ENABLE_MEMORY_CACHING |
| 63 | #define DISABLE_MEM_CACHE_DEFAULT false |
| 64 | #else |
| 65 | #define DISABLE_MEM_CACHE_DEFAULT true |
| 66 | #endif |
| 67 | |
| 68 | class ProcessOptionValueProperties : public OptionValueProperties |
| 69 | { |
| 70 | public: |
| 71 | ProcessOptionValueProperties (const ConstString &name) : |
| 72 | OptionValueProperties (name) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | // This constructor is used when creating ProcessOptionValueProperties when it |
| 77 | // is part of a new lldb_private::Process instance. It will copy all current |
| 78 | // global property values as needed |
| 79 | ProcessOptionValueProperties (ProcessProperties *global_properties) : |
| 80 | OptionValueProperties(*global_properties->GetValueProperties()) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | virtual const Property * |
| 85 | GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const |
| 86 | { |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 87 | // 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] | 88 | // try and grab the setting from the current process if there is one. Else we just |
| 89 | // use the one from this instance. |
| 90 | if (exe_ctx) |
| 91 | { |
| 92 | Process *process = exe_ctx->GetProcessPtr(); |
| 93 | if (process) |
| 94 | { |
| 95 | ProcessOptionValueProperties *instance_properties = static_cast<ProcessOptionValueProperties *>(process->GetValueProperties().get()); |
| 96 | if (this != instance_properties) |
| 97 | return instance_properties->ProtectedGetPropertyAtIndex (idx); |
| 98 | } |
| 99 | } |
| 100 | return ProtectedGetPropertyAtIndex (idx); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | static PropertyDefinition |
| 105 | g_properties[] = |
| 106 | { |
| 107 | { "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] | 108 | { "extra-startup-command", OptionValue::eTypeArray , false, OptionValue::eTypeString, NULL, NULL, "A list containing extra commands understood by the particular process plugin used. " |
| 109 | "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] | 110 | { "ignore-breakpoints-in-expressions", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, breakpoints will be ignored during expression evaluation." }, |
| 111 | { "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] | 112 | { "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] | 113 | { "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] | 114 | { "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] | 115 | { "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] | 116 | { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } |
| 117 | }; |
| 118 | |
| 119 | enum { |
| 120 | ePropertyDisableMemCache, |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 121 | ePropertyExtraStartCommand, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 122 | ePropertyIgnoreBreakpointsInExpressions, |
| 123 | ePropertyUnwindOnErrorInExpressions, |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 124 | ePropertyPythonOSPluginPath, |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 125 | ePropertyStopOnSharedLibraryEvents, |
Jason Molenda | f0340c9 | 2014-09-03 22:30:54 +0000 | [diff] [blame] | 126 | ePropertyDetachKeepsStopped, |
| 127 | ePropertyMemCacheLineSize |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 130 | ProcessProperties::ProcessProperties (lldb_private::Process *process) : |
| 131 | Properties (), |
| 132 | m_process (process) // Can be NULL for global ProcessProperties |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 133 | { |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 134 | if (process == NULL) |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 135 | { |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 136 | // Global process properties, set them up one time |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 137 | m_collection_sp.reset (new ProcessOptionValueProperties(ConstString("process"))); |
| 138 | m_collection_sp->Initialize(g_properties); |
| 139 | m_collection_sp->AppendProperty(ConstString("thread"), |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 140 | ConstString("Settings specific to threads."), |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 141 | true, |
| 142 | Thread::GetGlobalProperties()->GetValueProperties()); |
| 143 | } |
| 144 | else |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 145 | { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 146 | m_collection_sp.reset (new ProcessOptionValueProperties(Process::GetGlobalProperties().get())); |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 147 | m_collection_sp->SetValueChangedCallback(ePropertyPythonOSPluginPath, ProcessProperties::OptionValueChangedCallback, this); |
| 148 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | ProcessProperties::~ProcessProperties() |
| 152 | { |
| 153 | } |
| 154 | |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 155 | void |
| 156 | ProcessProperties::OptionValueChangedCallback (void *baton, OptionValue *option_value) |
| 157 | { |
| 158 | ProcessProperties *properties = (ProcessProperties *)baton; |
| 159 | if (properties->m_process) |
| 160 | properties->m_process->LoadOperatingSystemPlugin(true); |
| 161 | } |
| 162 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 163 | bool |
| 164 | ProcessProperties::GetDisableMemoryCache() const |
| 165 | { |
| 166 | const uint32_t idx = ePropertyDisableMemCache; |
| 167 | return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); |
| 168 | } |
| 169 | |
Jason Molenda | f0340c9 | 2014-09-03 22:30:54 +0000 | [diff] [blame] | 170 | uint64_t |
| 171 | ProcessProperties::GetMemoryCacheLineSize() const |
| 172 | { |
| 173 | const uint32_t idx = ePropertyMemCacheLineSize; |
| 174 | return m_collection_sp->GetPropertyAtIndexAsUInt64 (NULL, idx, g_properties[idx].default_uint_value); |
| 175 | } |
| 176 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 177 | Args |
| 178 | ProcessProperties::GetExtraStartupCommands () const |
| 179 | { |
| 180 | Args args; |
| 181 | const uint32_t idx = ePropertyExtraStartCommand; |
| 182 | m_collection_sp->GetPropertyAtIndexAsArgs(NULL, idx, args); |
| 183 | return args; |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | ProcessProperties::SetExtraStartupCommands (const Args &args) |
| 188 | { |
| 189 | const uint32_t idx = ePropertyExtraStartCommand; |
| 190 | m_collection_sp->SetPropertyAtIndexFromArgs(NULL, idx, args); |
| 191 | } |
| 192 | |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 193 | FileSpec |
| 194 | ProcessProperties::GetPythonOSPluginPath () const |
| 195 | { |
| 196 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 197 | return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx); |
| 198 | } |
| 199 | |
| 200 | void |
| 201 | ProcessProperties::SetPythonOSPluginPath (const FileSpec &file) |
| 202 | { |
| 203 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 204 | m_collection_sp->SetPropertyAtIndexAsFileSpec(NULL, idx, file); |
| 205 | } |
| 206 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 207 | |
| 208 | bool |
| 209 | ProcessProperties::GetIgnoreBreakpointsInExpressions () const |
| 210 | { |
| 211 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 212 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | ProcessProperties::SetIgnoreBreakpointsInExpressions (bool ignore) |
| 217 | { |
| 218 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 219 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore); |
| 220 | } |
| 221 | |
| 222 | bool |
| 223 | ProcessProperties::GetUnwindOnErrorInExpressions () const |
| 224 | { |
| 225 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 226 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | ProcessProperties::SetUnwindOnErrorInExpressions (bool ignore) |
| 231 | { |
| 232 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 233 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, ignore); |
| 234 | } |
| 235 | |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 236 | bool |
| 237 | ProcessProperties::GetStopOnSharedLibraryEvents () const |
| 238 | { |
| 239 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 240 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 241 | } |
| 242 | |
| 243 | void |
| 244 | ProcessProperties::SetStopOnSharedLibraryEvents (bool stop) |
| 245 | { |
| 246 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 247 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop); |
| 248 | } |
| 249 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 250 | bool |
| 251 | ProcessProperties::GetDetachKeepsStopped () const |
| 252 | { |
| 253 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 254 | return m_collection_sp->GetPropertyAtIndexAsBoolean(NULL, idx, g_properties[idx].default_uint_value != 0); |
| 255 | } |
| 256 | |
| 257 | void |
| 258 | ProcessProperties::SetDetachKeepsStopped (bool stop) |
| 259 | { |
| 260 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 261 | m_collection_sp->SetPropertyAtIndexAsBoolean(NULL, idx, stop); |
| 262 | } |
| 263 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 264 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 265 | ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 266 | { |
| 267 | const char *cstr; |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 268 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 269 | s.Printf (" pid = %" PRIu64 "\n", m_pid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 270 | |
| 271 | if (m_parent_pid != LLDB_INVALID_PROCESS_ID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 272 | s.Printf (" parent = %" PRIu64 "\n", m_parent_pid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 273 | |
| 274 | if (m_executable) |
| 275 | { |
| 276 | s.Printf (" name = %s\n", m_executable.GetFilename().GetCString()); |
| 277 | s.PutCString (" file = "); |
| 278 | m_executable.Dump(&s); |
| 279 | s.EOL(); |
| 280 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 281 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 282 | if (argc > 0) |
| 283 | { |
| 284 | for (uint32_t i=0; i<argc; i++) |
| 285 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 286 | const char *arg = m_arguments.GetArgumentAtIndex(i); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 287 | if (i < 10) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 288 | s.Printf (" arg[%u] = %s\n", i, arg); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 289 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 290 | s.Printf ("arg[%u] = %s\n", i, arg); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 293 | |
| 294 | const uint32_t envc = m_environment.GetArgumentCount(); |
| 295 | if (envc > 0) |
| 296 | { |
| 297 | for (uint32_t i=0; i<envc; i++) |
| 298 | { |
| 299 | const char *env = m_environment.GetArgumentAtIndex(i); |
| 300 | if (i < 10) |
| 301 | s.Printf (" env[%u] = %s\n", i, env); |
| 302 | else |
| 303 | s.Printf ("env[%u] = %s\n", i, env); |
| 304 | } |
| 305 | } |
| 306 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 307 | if (m_arch.IsValid()) |
| 308 | s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str()); |
| 309 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 310 | if (m_uid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 311 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 312 | cstr = platform->GetUserName (m_uid); |
| 313 | s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 314 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 315 | if (m_gid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 316 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 317 | cstr = platform->GetGroupName (m_gid); |
| 318 | s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 319 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 320 | if (m_euid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 321 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 322 | cstr = platform->GetUserName (m_euid); |
| 323 | s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 324 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 325 | if (m_egid != UINT32_MAX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 326 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 327 | cstr = platform->GetGroupName (m_egid); |
| 328 | s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : ""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 333 | ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 334 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 335 | const char *label; |
| 336 | if (show_args || verbose) |
| 337 | label = "ARGUMENTS"; |
| 338 | else |
| 339 | label = "NAME"; |
| 340 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 341 | if (verbose) |
| 342 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 343 | 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] | 344 | s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n"); |
| 345 | } |
| 346 | else |
| 347 | { |
Jim Ingham | 368ac22 | 2014-08-15 17:05:27 +0000 | [diff] [blame] | 348 | s.Printf ("PID PARENT USER TRIPLE %s\n", label); |
| 349 | s.PutCString ("====== ====== ========== ======================== ============================\n"); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 350 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 354 | ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 355 | { |
| 356 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
| 357 | { |
| 358 | const char *cstr; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 359 | s.Printf ("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 360 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 361 | |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 362 | if (verbose) |
| 363 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 364 | cstr = platform->GetUserName (m_uid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 365 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 366 | s.Printf ("%-10s ", cstr); |
| 367 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 368 | s.Printf ("%-10u ", m_uid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 369 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 370 | cstr = platform->GetGroupName (m_gid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 371 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 372 | s.Printf ("%-10s ", cstr); |
| 373 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 374 | s.Printf ("%-10u ", m_gid); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 375 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 376 | cstr = platform->GetUserName (m_euid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 377 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 378 | s.Printf ("%-10s ", cstr); |
| 379 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 380 | s.Printf ("%-10u ", m_euid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 381 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 382 | cstr = platform->GetGroupName (m_egid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 383 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 384 | s.Printf ("%-10s ", cstr); |
| 385 | else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 386 | s.Printf ("%-10u ", m_egid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 387 | s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : ""); |
| 388 | } |
| 389 | else |
| 390 | { |
Jim Ingham | 368ac22 | 2014-08-15 17:05:27 +0000 | [diff] [blame] | 391 | s.Printf ("%-10s %-24s ", |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 392 | platform->GetUserName (m_euid), |
Jim Ingham | 368ac22 | 2014-08-15 17:05:27 +0000 | [diff] [blame] | 393 | m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : ""); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 396 | if (verbose || show_args) |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 397 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 398 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 399 | if (argc > 0) |
| 400 | { |
| 401 | for (uint32_t i=0; i<argc; i++) |
| 402 | { |
| 403 | if (i > 0) |
| 404 | s.PutChar (' '); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 405 | s.PutCString (m_arguments.GetArgumentAtIndex(i)); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | s.PutCString (GetName()); |
| 412 | } |
| 413 | |
| 414 | s.EOL(); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 418 | Error |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 419 | ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 420 | { |
| 421 | Error error; |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 422 | const int short_option = m_getopt_table[option_idx].val; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 423 | |
| 424 | switch (short_option) |
| 425 | { |
| 426 | case 's': // Stop at program entry point |
| 427 | launch_info.GetFlags().Set (eLaunchFlagStopAtEntry); |
| 428 | break; |
| 429 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 430 | case 'i': // STDIN for read only |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 431 | { |
| 432 | FileAction action; |
| 433 | if (action.Open (STDIN_FILENO, option_arg, true, false)) |
| 434 | launch_info.AppendFileAction (action); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 435 | break; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 436 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 437 | |
| 438 | case 'o': // Open STDOUT for write only |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 439 | { |
| 440 | FileAction action; |
| 441 | if (action.Open (STDOUT_FILENO, option_arg, false, true)) |
| 442 | launch_info.AppendFileAction (action); |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 443 | break; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 444 | } |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 445 | |
| 446 | case 'e': // STDERR for write only |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 447 | { |
| 448 | FileAction action; |
| 449 | if (action.Open (STDERR_FILENO, option_arg, false, true)) |
| 450 | launch_info.AppendFileAction (action); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 451 | break; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 452 | } |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 453 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 454 | case 'p': // Process plug-in name |
| 455 | launch_info.SetProcessPluginName (option_arg); |
| 456 | break; |
| 457 | |
| 458 | case 'n': // Disable STDIO |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 459 | { |
| 460 | FileAction action; |
| 461 | if (action.Open (STDIN_FILENO, "/dev/null", true, false)) |
| 462 | launch_info.AppendFileAction (action); |
| 463 | if (action.Open (STDOUT_FILENO, "/dev/null", false, true)) |
| 464 | launch_info.AppendFileAction (action); |
| 465 | if (action.Open (STDERR_FILENO, "/dev/null", false, true)) |
| 466 | launch_info.AppendFileAction (action); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 467 | break; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 468 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 469 | |
| 470 | case 'w': |
| 471 | launch_info.SetWorkingDirectory (option_arg); |
| 472 | break; |
| 473 | |
| 474 | case 't': // Open process in new terminal window |
| 475 | launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY); |
| 476 | break; |
| 477 | |
| 478 | case 'a': |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 479 | if (!launch_info.GetArchitecture().SetTriple (option_arg, m_interpreter.GetPlatform(true).get())) |
| 480 | launch_info.GetArchitecture().SetTriple (option_arg); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 481 | break; |
| 482 | |
Todd Fiala | 5163792 | 2014-08-19 17:40:43 +0000 | [diff] [blame] | 483 | case 'A': // Disable ASLR. |
| 484 | { |
| 485 | bool success; |
| 486 | const bool disable_aslr_arg = Args::StringToBoolean (option_arg, true, &success); |
| 487 | if (success) |
| 488 | disable_aslr = disable_aslr_arg ? eLazyBoolYes : eLazyBoolNo; |
| 489 | else |
| 490 | 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] | 491 | break; |
Todd Fiala | 5163792 | 2014-08-19 17:40:43 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 494 | case 'X': // shell expand args. |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 495 | { |
| 496 | bool success; |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 497 | const bool expand_args = Args::StringToBoolean (option_arg, true, &success); |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 498 | if (success) |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 499 | launch_info.SetShellExpandArguments(expand_args); |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 500 | else |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 501 | error.SetErrorStringWithFormat ("Invalid boolean value for shell-expand-args option: '%s'", option_arg ? option_arg : "<null>"); |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 502 | break; |
| 503 | } |
| 504 | |
Todd Fiala | 5163792 | 2014-08-19 17:40:43 +0000 | [diff] [blame] | 505 | case 'c': |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 506 | if (option_arg && option_arg[0]) |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 507 | launch_info.SetShell (FileSpec(option_arg, false)); |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 508 | else |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 509 | launch_info.SetShell (HostInfo::GetDefaultShell()); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 510 | break; |
| 511 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 512 | case 'v': |
| 513 | launch_info.GetEnvironmentEntries().AppendArgument(option_arg); |
| 514 | break; |
| 515 | |
| 516 | default: |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 517 | error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 518 | break; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 519 | } |
| 520 | return error; |
| 521 | } |
| 522 | |
| 523 | OptionDefinition |
| 524 | ProcessLaunchCommandOptions::g_option_table[] = |
| 525 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 526 | { 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] | 527 | { 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] | 528 | { LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."}, |
| 529 | { 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."}, |
| 530 | { LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."}, |
| 531 | { 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."}, |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 532 | { LLDB_OPT_SET_1|LLDB_OPT_SET_2|LLDB_OPT_SET_3, 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] | 533 | |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 534 | { LLDB_OPT_SET_1 , false, "stdin", 'i', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stdin for the process to <filename>."}, |
| 535 | { LLDB_OPT_SET_1 , false, "stdout", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Redirect stdout for the process to <filename>."}, |
| 536 | { 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] | 537 | |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 538 | { 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] | 539 | |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 540 | { 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."}, |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 541 | { LLDB_OPT_SET_4, false, "shell-expand-args", 'X', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Set whether to shell expand arguments to the process when launching."}, |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 542 | { 0 , false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 543 | }; |
| 544 | |
| 545 | |
| 546 | |
| 547 | bool |
| 548 | ProcessInstanceInfoMatch::NameMatches (const char *process_name) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 549 | { |
| 550 | if (m_name_match_type == eNameMatchIgnore || process_name == NULL) |
| 551 | return true; |
| 552 | const char *match_name = m_match_info.GetName(); |
| 553 | if (!match_name) |
| 554 | return true; |
| 555 | |
| 556 | return lldb_private::NameMatches (process_name, m_name_match_type, match_name); |
| 557 | } |
| 558 | |
| 559 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 560 | ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 561 | { |
| 562 | if (!NameMatches (proc_info.GetName())) |
| 563 | return false; |
| 564 | |
| 565 | if (m_match_info.ProcessIDIsValid() && |
| 566 | m_match_info.GetProcessID() != proc_info.GetProcessID()) |
| 567 | return false; |
| 568 | |
| 569 | if (m_match_info.ParentProcessIDIsValid() && |
| 570 | m_match_info.GetParentProcessID() != proc_info.GetParentProcessID()) |
| 571 | return false; |
| 572 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 573 | if (m_match_info.UserIDIsValid () && |
| 574 | m_match_info.GetUserID() != proc_info.GetUserID()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 575 | return false; |
| 576 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 577 | if (m_match_info.GroupIDIsValid () && |
| 578 | m_match_info.GetGroupID() != proc_info.GetGroupID()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 579 | return false; |
| 580 | |
| 581 | if (m_match_info.EffectiveUserIDIsValid () && |
| 582 | m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID()) |
| 583 | return false; |
| 584 | |
| 585 | if (m_match_info.EffectiveGroupIDIsValid () && |
| 586 | m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID()) |
| 587 | return false; |
| 588 | |
| 589 | if (m_match_info.GetArchitecture().IsValid() && |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 590 | !m_match_info.GetArchitecture().IsCompatibleMatch(proc_info.GetArchitecture())) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 591 | return false; |
| 592 | return true; |
| 593 | } |
| 594 | |
| 595 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 596 | ProcessInstanceInfoMatch::MatchAllProcesses () const |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 597 | { |
| 598 | if (m_name_match_type != eNameMatchIgnore) |
| 599 | return false; |
| 600 | |
| 601 | if (m_match_info.ProcessIDIsValid()) |
| 602 | return false; |
| 603 | |
| 604 | if (m_match_info.ParentProcessIDIsValid()) |
| 605 | return false; |
| 606 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 607 | if (m_match_info.UserIDIsValid ()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 608 | return false; |
| 609 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 610 | if (m_match_info.GroupIDIsValid ()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 611 | return false; |
| 612 | |
| 613 | if (m_match_info.EffectiveUserIDIsValid ()) |
| 614 | return false; |
| 615 | |
| 616 | if (m_match_info.EffectiveGroupIDIsValid ()) |
| 617 | return false; |
| 618 | |
| 619 | if (m_match_info.GetArchitecture().IsValid()) |
| 620 | return false; |
| 621 | |
| 622 | if (m_match_all_users) |
| 623 | return false; |
| 624 | |
| 625 | return true; |
| 626 | |
| 627 | } |
| 628 | |
| 629 | void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 630 | ProcessInstanceInfoMatch::Clear() |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 631 | { |
| 632 | m_match_info.Clear(); |
| 633 | m_name_match_type = eNameMatchIgnore; |
| 634 | m_match_all_users = false; |
| 635 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 636 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 637 | ProcessSP |
| 638 | 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] | 639 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 640 | static uint32_t g_process_unique_id = 0; |
| 641 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 642 | ProcessSP process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 643 | ProcessCreateInstance create_callback = NULL; |
| 644 | if (plugin_name) |
| 645 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 646 | ConstString const_plugin_name(plugin_name); |
| 647 | create_callback = PluginManager::GetProcessCreateCallbackForPluginName (const_plugin_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 648 | if (create_callback) |
| 649 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 650 | process_sp = create_callback(target, listener, crash_file_path); |
| 651 | if (process_sp) |
| 652 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 653 | if (process_sp->CanDebug(target, true)) |
| 654 | { |
| 655 | process_sp->m_process_unique_id = ++g_process_unique_id; |
| 656 | } |
| 657 | else |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 658 | process_sp.reset(); |
| 659 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | else |
| 663 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 664 | for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 665 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 666 | process_sp = create_callback(target, listener, crash_file_path); |
| 667 | if (process_sp) |
| 668 | { |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 669 | if (process_sp->CanDebug(target, false)) |
| 670 | { |
| 671 | process_sp->m_process_unique_id = ++g_process_unique_id; |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 672 | break; |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 673 | } |
| 674 | else |
| 675 | process_sp.reset(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 676 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 679 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 682 | ConstString & |
| 683 | Process::GetStaticBroadcasterClass () |
| 684 | { |
| 685 | static ConstString class_name ("lldb.process"); |
| 686 | return class_name; |
| 687 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 688 | |
| 689 | //---------------------------------------------------------------------- |
| 690 | // Process constructor |
| 691 | //---------------------------------------------------------------------- |
| 692 | Process::Process(Target &target, Listener &listener) : |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 693 | Process(target, listener, Host::GetUnixSignals ()) |
| 694 | { |
| 695 | // This constructor just delegates to the full Process constructor, |
| 696 | // defaulting to using the Host's UnixSignals. |
| 697 | } |
| 698 | |
| 699 | Process::Process(Target &target, Listener &listener, const UnixSignalsSP &unix_signals_sp) : |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 700 | ProcessProperties (this), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 701 | UserID (LLDB_INVALID_PROCESS_ID), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 702 | Broadcaster (&(target.GetDebugger()), "lldb.process"), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 703 | m_target (target), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 704 | m_public_state (eStateUnloaded), |
| 705 | m_private_state (eStateUnloaded), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 706 | m_private_state_broadcaster (NULL, "lldb.process.internal_state_broadcaster"), |
| 707 | m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 708 | m_private_state_listener ("lldb.process.internal_state_listener"), |
| 709 | m_private_state_control_wait(), |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 710 | m_mod_id (), |
Greg Clayton | 949e822 | 2013-01-16 17:29:04 +0000 | [diff] [blame] | 711 | m_process_unique_id(0), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 712 | m_thread_index_id (0), |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 713 | m_thread_id_to_index_id_map (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 714 | m_exit_status (-1), |
| 715 | m_exit_string (), |
Todd Fiala | 7b0917a | 2014-09-15 20:07:33 +0000 | [diff] [blame] | 716 | m_exit_status_mutex(), |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 717 | m_thread_mutex (Mutex::eMutexTypeRecursive), |
| 718 | m_thread_list_real (this), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 719 | m_thread_list (this), |
Jason Molenda | 864f1cc | 2013-11-11 05:20:44 +0000 | [diff] [blame] | 720 | m_extended_thread_list (this), |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 721 | m_extended_thread_stop_id (0), |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 722 | m_queue_list (this), |
| 723 | m_queue_list_stop_id (0), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 724 | m_notifications (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 725 | m_image_tokens (), |
| 726 | m_listener (listener), |
| 727 | m_breakpoint_site_list (), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 728 | m_dynamic_checkers_ap (), |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 729 | m_unix_signals_sp (unix_signals_sp), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 730 | m_abi_sp (), |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 731 | m_process_input_reader (), |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 732 | m_stdio_communication ("process.stdio"), |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 733 | m_stdio_communication_mutex (Mutex::eMutexTypeRecursive), |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 734 | m_stdin_forward (false), |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 735 | m_stdout_data (), |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 736 | m_stderr_data (), |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 737 | m_profile_data_comm_mutex (Mutex::eMutexTypeRecursive), |
| 738 | m_profile_data (), |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 739 | m_iohandler_sync (false), |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 740 | m_memory_cache (*this), |
| 741 | m_allocated_memory_cache (*this), |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 742 | m_should_detach (false), |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 743 | m_next_event_action_ap(), |
Greg Clayton | 9624985 | 2013-04-18 16:57:27 +0000 | [diff] [blame] | 744 | m_public_run_lock (), |
Greg Clayton | 9624985 | 2013-04-18 16:57:27 +0000 | [diff] [blame] | 745 | m_private_run_lock (), |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 746 | m_currently_handling_event(false), |
Greg Clayton | a97c4d2 | 2014-12-09 23:31:02 +0000 | [diff] [blame] | 747 | m_stop_info_override_callback (NULL), |
Jim Ingham | 4fc6cb9 | 2012-08-22 21:34:33 +0000 | [diff] [blame] | 748 | m_finalize_called(false), |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 749 | m_clear_thread_plans_on_stop (false), |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 750 | m_force_next_event_delivery(false), |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 751 | m_last_broadcast_state (eStateInvalid), |
Jason Molenda | 69b6b63 | 2013-03-05 03:33:59 +0000 | [diff] [blame] | 752 | m_destroy_in_process (false), |
| 753 | m_can_jit(eCanJITDontKnow) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 754 | { |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 755 | CheckInWithManager (); |
Caroline Tice | 1559a46 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 756 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 757 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 758 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 759 | log->Printf ("%p Process::Process()", static_cast<void*>(this)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 760 | |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 761 | if (!m_unix_signals_sp) |
| 762 | m_unix_signals_sp.reset (new UnixSignals ()); |
| 763 | |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 764 | SetEventName (eBroadcastBitStateChanged, "state-changed"); |
| 765 | SetEventName (eBroadcastBitInterrupt, "interrupt"); |
| 766 | SetEventName (eBroadcastBitSTDOUT, "stdout-available"); |
| 767 | SetEventName (eBroadcastBitSTDERR, "stderr-available"); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 768 | SetEventName (eBroadcastBitProfileData, "profile-data-available"); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 769 | |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 770 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlStop , "control-stop" ); |
| 771 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlPause , "control-pause" ); |
| 772 | m_private_state_control_broadcaster.SetEventName (eBroadcastInternalStateControlResume, "control-resume"); |
| 773 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 774 | listener.StartListeningForEvents (this, |
| 775 | eBroadcastBitStateChanged | |
| 776 | eBroadcastBitInterrupt | |
| 777 | eBroadcastBitSTDOUT | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 778 | eBroadcastBitSTDERR | |
| 779 | eBroadcastBitProfileData); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 780 | |
| 781 | m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster, |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 782 | eBroadcastBitStateChanged | |
| 783 | eBroadcastBitInterrupt); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 784 | |
| 785 | m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster, |
| 786 | eBroadcastInternalStateControlStop | |
| 787 | eBroadcastInternalStateControlPause | |
| 788 | eBroadcastInternalStateControlResume); |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 789 | // We need something valid here, even if just the default UnixSignalsSP. |
| 790 | assert (m_unix_signals_sp && "null m_unix_signals_sp after initialization"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | //---------------------------------------------------------------------- |
| 794 | // Destructor |
| 795 | //---------------------------------------------------------------------- |
| 796 | Process::~Process() |
| 797 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 798 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 799 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 800 | log->Printf ("%p Process::~Process()", static_cast<void*>(this)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 801 | StopPrivateStateThread(); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 802 | |
| 803 | // ThreadList::Clear() will try to acquire this process's mutex, so |
| 804 | // explicitly clear the thread list here to ensure that the mutex |
| 805 | // is not destroyed before the thread list. |
| 806 | m_thread_list.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 809 | const ProcessPropertiesSP & |
| 810 | Process::GetGlobalProperties() |
| 811 | { |
| 812 | static ProcessPropertiesSP g_settings_sp; |
| 813 | if (!g_settings_sp) |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 814 | g_settings_sp.reset (new ProcessProperties (NULL)); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 815 | return g_settings_sp; |
| 816 | } |
| 817 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 818 | void |
| 819 | Process::Finalize() |
| 820 | { |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 821 | switch (GetPrivateState()) |
| 822 | { |
| 823 | case eStateConnected: |
| 824 | case eStateAttaching: |
| 825 | case eStateLaunching: |
| 826 | case eStateStopped: |
| 827 | case eStateRunning: |
| 828 | case eStateStepping: |
| 829 | case eStateCrashed: |
| 830 | case eStateSuspended: |
| 831 | if (GetShouldDetach()) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 832 | { |
| 833 | // FIXME: This will have to be a process setting: |
| 834 | bool keep_stopped = false; |
| 835 | Detach(keep_stopped); |
| 836 | } |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 837 | else |
| 838 | Destroy(); |
| 839 | break; |
| 840 | |
| 841 | case eStateInvalid: |
| 842 | case eStateUnloaded: |
| 843 | case eStateDetached: |
| 844 | case eStateExited: |
| 845 | break; |
| 846 | } |
| 847 | |
Greg Clayton | 1ed54f5 | 2011-10-01 00:45:15 +0000 | [diff] [blame] | 848 | // Clear our broadcaster before we proceed with destroying |
| 849 | Broadcaster::Clear(); |
| 850 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 851 | // Do any cleanup needed prior to being destructed... Subclasses |
| 852 | // that override this method should call this superclass method as well. |
Jim Ingham | d0a3e12 | 2011-02-16 17:54:55 +0000 | [diff] [blame] | 853 | |
| 854 | // We need to destroy the loader before the derived Process class gets destroyed |
| 855 | // 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] | 856 | m_dynamic_checkers_ap.reset(); |
| 857 | m_abi_sp.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 858 | m_os_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 859 | m_system_runtime_ap.reset(); |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 860 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 861 | m_jit_loaders_ap.reset(); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 862 | m_thread_list_real.Destroy(); |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 863 | m_thread_list.Destroy(); |
Jason Molenda | 864f1cc | 2013-11-11 05:20:44 +0000 | [diff] [blame] | 864 | m_extended_thread_list.Destroy(); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 865 | m_queue_list.Clear(); |
| 866 | m_queue_list_stop_id = 0; |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 867 | std::vector<Notifications> empty_notifications; |
| 868 | m_notifications.swap(empty_notifications); |
| 869 | m_image_tokens.clear(); |
| 870 | m_memory_cache.Clear(); |
| 871 | m_allocated_memory_cache.Clear(); |
| 872 | m_language_runtimes.clear(); |
Kuba Brecka | afdf842 | 2014-10-10 23:43:03 +0000 | [diff] [blame] | 873 | m_instrumentation_runtimes.clear(); |
Greg Clayton | 894f82f | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 874 | m_next_event_action_ap.reset(); |
Greg Clayton | a97c4d2 | 2014-12-09 23:31:02 +0000 | [diff] [blame] | 875 | m_stop_info_override_callback = NULL; |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 876 | //#ifdef LLDB_CONFIGURATION_DEBUG |
| 877 | // StreamFile s(stdout, false); |
| 878 | // EventSP event_sp; |
| 879 | // while (m_private_state_listener.GetNextEvent(event_sp)) |
| 880 | // { |
| 881 | // event_sp->Dump (&s); |
| 882 | // s.EOL(); |
| 883 | // } |
| 884 | //#endif |
| 885 | // We have to be very careful here as the m_private_state_listener might |
| 886 | // contain events that have ProcessSP values in them which can keep this |
| 887 | // process around forever. These events need to be cleared out. |
| 888 | m_private_state_listener.Clear(); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 889 | m_public_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 890 | m_public_run_lock.SetStopped(); |
| 891 | m_private_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 892 | m_private_run_lock.SetStopped(); |
Jim Ingham | 4fc6cb9 | 2012-08-22 21:34:33 +0000 | [diff] [blame] | 893 | m_finalize_called = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | void |
| 897 | Process::RegisterNotificationCallbacks (const Notifications& callbacks) |
| 898 | { |
| 899 | m_notifications.push_back(callbacks); |
| 900 | if (callbacks.initialize != NULL) |
| 901 | callbacks.initialize (callbacks.baton, this); |
| 902 | } |
| 903 | |
| 904 | bool |
| 905 | Process::UnregisterNotificationCallbacks(const Notifications& callbacks) |
| 906 | { |
| 907 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 908 | for (pos = m_notifications.begin(); pos != end; ++pos) |
| 909 | { |
| 910 | if (pos->baton == callbacks.baton && |
| 911 | pos->initialize == callbacks.initialize && |
| 912 | pos->process_state_changed == callbacks.process_state_changed) |
| 913 | { |
| 914 | m_notifications.erase(pos); |
| 915 | return true; |
| 916 | } |
| 917 | } |
| 918 | return false; |
| 919 | } |
| 920 | |
| 921 | void |
| 922 | Process::SynchronouslyNotifyStateChanged (StateType state) |
| 923 | { |
| 924 | std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end(); |
| 925 | for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos) |
| 926 | { |
| 927 | if (notification_pos->process_state_changed) |
| 928 | notification_pos->process_state_changed (notification_pos->baton, this, state); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | // FIXME: We need to do some work on events before the general Listener sees them. |
| 933 | // For instance if we are continuing from a breakpoint, we need to ensure that we do |
| 934 | // the little "insert real insn, step & stop" trick. But we can't do that when the |
| 935 | // event is delivered by the broadcaster - since that is done on the thread that is |
| 936 | // waiting for new events, so if we needed more than one event for our handling, we would |
| 937 | // stall. So instead we do it when we fetch the event off of the queue. |
| 938 | // |
| 939 | |
| 940 | StateType |
| 941 | Process::GetNextEvent (EventSP &event_sp) |
| 942 | { |
| 943 | StateType state = eStateInvalid; |
| 944 | |
| 945 | if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp) |
| 946 | state = Process::ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 947 | |
| 948 | return state; |
| 949 | } |
| 950 | |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 951 | bool |
| 952 | Process::SyncIOHandler (uint64_t timeout_msec) |
| 953 | { |
| 954 | bool timed_out = false; |
| 955 | |
| 956 | // don't sync (potentially context switch) in case where there is no process IO |
| 957 | if (m_process_input_reader) |
| 958 | { |
| 959 | TimeValue timeout = TimeValue::Now(); |
| 960 | timeout.OffsetWithMicroSeconds(timeout_msec*1000); |
| 961 | |
| 962 | m_iohandler_sync.WaitForValueEqualTo(true, &timeout, &timed_out); |
| 963 | |
| 964 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 965 | if(log) |
| 966 | { |
| 967 | if(timed_out) |
| 968 | log->Printf ("Process::%s pid %" PRIu64 " (timeout=%" PRIu64 "ms): FAIL", __FUNCTION__, GetID (), timeout_msec); |
| 969 | else |
| 970 | log->Printf ("Process::%s pid %" PRIu64 ": SUCCESS", __FUNCTION__, GetID ()); |
| 971 | } |
| 972 | |
Shawn Best | 1ded74a | 2014-10-08 01:50:37 +0000 | [diff] [blame] | 973 | // reset sync one-shot so it will be ready for next launch |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 974 | m_iohandler_sync.SetValue(false, eBroadcastNever); |
| 975 | } |
| 976 | |
| 977 | return !timed_out; |
| 978 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 979 | |
| 980 | StateType |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 981 | Process::WaitForProcessToStop (const TimeValue *timeout, |
| 982 | EventSP *event_sp_ptr, |
| 983 | bool wait_always, |
| 984 | Listener *hijack_listener, |
| 985 | Stream *stream) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 986 | { |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 987 | // We can't just wait for a "stopped" event, because the stopped event may have restarted the target. |
| 988 | // We have to actually check each event, and in the case of a stopped event check the restarted flag |
| 989 | // on the event. |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 990 | if (event_sp_ptr) |
| 991 | event_sp_ptr->reset(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 992 | StateType state = GetState(); |
| 993 | // If we are exited or detached, we won't ever get back to any |
| 994 | // other valid state... |
| 995 | if (state == eStateDetached || state == eStateExited) |
| 996 | return state; |
| 997 | |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 998 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 999 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1000 | log->Printf ("Process::%s (timeout = %p)", __FUNCTION__, |
| 1001 | static_cast<const void*>(timeout)); |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 1002 | |
| 1003 | if (!wait_always && |
| 1004 | StateIsStoppedState(state, true) && |
| 1005 | StateIsStoppedState(GetPrivateState(), true)) { |
| 1006 | if (log) |
| 1007 | log->Printf("Process::%s returning without waiting for events; process private and public states are already 'stopped'.", |
| 1008 | __FUNCTION__); |
| 1009 | return state; |
| 1010 | } |
| 1011 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1012 | while (state != eStateInvalid) |
| 1013 | { |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 1014 | EventSP event_sp; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1015 | state = WaitForStateChangedEvents (timeout, event_sp, hijack_listener); |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 1016 | if (event_sp_ptr && event_sp) |
| 1017 | *event_sp_ptr = event_sp; |
| 1018 | |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1019 | bool pop_process_io_handler = hijack_listener != NULL; |
| 1020 | Process::HandleProcessStateChangedEvent (event_sp, stream, pop_process_io_handler); |
| 1021 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1022 | switch (state) |
| 1023 | { |
| 1024 | case eStateCrashed: |
| 1025 | case eStateDetached: |
| 1026 | case eStateExited: |
| 1027 | case eStateUnloaded: |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1028 | // We need to toggle the run lock as this won't get done in |
| 1029 | // SetPublicState() if the process is hijacked. |
| 1030 | if (hijack_listener) |
| 1031 | m_public_run_lock.SetStopped(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1032 | return state; |
| 1033 | case eStateStopped: |
| 1034 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 1035 | continue; |
| 1036 | else |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1037 | { |
| 1038 | // We need to toggle the run lock as this won't get done in |
| 1039 | // SetPublicState() if the process is hijacked. |
| 1040 | if (hijack_listener) |
| 1041 | m_public_run_lock.SetStopped(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1042 | return state; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1043 | } |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1044 | default: |
| 1045 | continue; |
| 1046 | } |
| 1047 | } |
| 1048 | return state; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1051 | bool |
| 1052 | Process::HandleProcessStateChangedEvent (const EventSP &event_sp, |
| 1053 | Stream *stream, |
| 1054 | bool &pop_process_io_handler) |
| 1055 | { |
| 1056 | const bool handle_pop = pop_process_io_handler == true; |
| 1057 | |
| 1058 | pop_process_io_handler = false; |
| 1059 | ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get()); |
| 1060 | |
| 1061 | if (!process_sp) |
| 1062 | return false; |
| 1063 | |
| 1064 | StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 1065 | if (event_state == eStateInvalid) |
| 1066 | return false; |
| 1067 | |
| 1068 | switch (event_state) |
| 1069 | { |
| 1070 | case eStateInvalid: |
| 1071 | case eStateUnloaded: |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1072 | case eStateAttaching: |
| 1073 | case eStateLaunching: |
| 1074 | case eStateStepping: |
| 1075 | case eStateDetached: |
| 1076 | { |
| 1077 | if (stream) |
| 1078 | stream->Printf ("Process %" PRIu64 " %s\n", |
| 1079 | process_sp->GetID(), |
| 1080 | StateAsCString (event_state)); |
| 1081 | |
| 1082 | if (event_state == eStateDetached) |
| 1083 | pop_process_io_handler = true; |
| 1084 | } |
| 1085 | break; |
| 1086 | |
Stephane Sezer | f2ef94e | 2014-12-13 05:23:51 +0000 | [diff] [blame] | 1087 | case eStateConnected: |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1088 | case eStateRunning: |
| 1089 | // Don't be chatty when we run... |
| 1090 | break; |
| 1091 | |
| 1092 | case eStateExited: |
| 1093 | if (stream) |
| 1094 | process_sp->GetStatus(*stream); |
| 1095 | pop_process_io_handler = true; |
| 1096 | break; |
| 1097 | |
| 1098 | case eStateStopped: |
| 1099 | case eStateCrashed: |
| 1100 | case eStateSuspended: |
| 1101 | // Make sure the program hasn't been auto-restarted: |
| 1102 | if (Process::ProcessEventData::GetRestartedFromEvent (event_sp.get())) |
| 1103 | { |
| 1104 | if (stream) |
| 1105 | { |
| 1106 | size_t num_reasons = Process::ProcessEventData::GetNumRestartedReasons(event_sp.get()); |
| 1107 | if (num_reasons > 0) |
| 1108 | { |
| 1109 | // FIXME: Do we want to report this, or would that just be annoyingly chatty? |
| 1110 | if (num_reasons == 1) |
| 1111 | { |
| 1112 | const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), 0); |
| 1113 | stream->Printf ("Process %" PRIu64 " stopped and restarted: %s\n", |
| 1114 | process_sp->GetID(), |
| 1115 | reason ? reason : "<UNKNOWN REASON>"); |
| 1116 | } |
| 1117 | else |
| 1118 | { |
| 1119 | stream->Printf ("Process %" PRIu64 " stopped and restarted, reasons:\n", |
| 1120 | process_sp->GetID()); |
| 1121 | |
| 1122 | |
| 1123 | for (size_t i = 0; i < num_reasons; i++) |
| 1124 | { |
| 1125 | const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), i); |
| 1126 | stream->Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>"); |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | else |
| 1133 | { |
| 1134 | // Lock the thread list so it doesn't change on us, this is the scope for the locker: |
| 1135 | { |
| 1136 | ThreadList &thread_list = process_sp->GetThreadList(); |
| 1137 | Mutex::Locker locker (thread_list.GetMutex()); |
| 1138 | |
| 1139 | ThreadSP curr_thread (thread_list.GetSelectedThread()); |
| 1140 | ThreadSP thread; |
| 1141 | StopReason curr_thread_stop_reason = eStopReasonInvalid; |
| 1142 | if (curr_thread) |
| 1143 | curr_thread_stop_reason = curr_thread->GetStopReason(); |
| 1144 | if (!curr_thread || |
| 1145 | !curr_thread->IsValid() || |
| 1146 | curr_thread_stop_reason == eStopReasonInvalid || |
| 1147 | curr_thread_stop_reason == eStopReasonNone) |
| 1148 | { |
| 1149 | // Prefer a thread that has just completed its plan over another thread as current thread. |
| 1150 | ThreadSP plan_thread; |
| 1151 | ThreadSP other_thread; |
| 1152 | const size_t num_threads = thread_list.GetSize(); |
| 1153 | size_t i; |
| 1154 | for (i = 0; i < num_threads; ++i) |
| 1155 | { |
| 1156 | thread = thread_list.GetThreadAtIndex(i); |
| 1157 | StopReason thread_stop_reason = thread->GetStopReason(); |
| 1158 | switch (thread_stop_reason) |
| 1159 | { |
| 1160 | case eStopReasonInvalid: |
| 1161 | case eStopReasonNone: |
| 1162 | break; |
| 1163 | |
| 1164 | case eStopReasonTrace: |
| 1165 | case eStopReasonBreakpoint: |
| 1166 | case eStopReasonWatchpoint: |
| 1167 | case eStopReasonSignal: |
| 1168 | case eStopReasonException: |
| 1169 | case eStopReasonExec: |
| 1170 | case eStopReasonThreadExiting: |
| 1171 | case eStopReasonInstrumentation: |
| 1172 | if (!other_thread) |
| 1173 | other_thread = thread; |
| 1174 | break; |
| 1175 | case eStopReasonPlanComplete: |
| 1176 | if (!plan_thread) |
| 1177 | plan_thread = thread; |
| 1178 | break; |
| 1179 | } |
| 1180 | } |
| 1181 | if (plan_thread) |
| 1182 | thread_list.SetSelectedThreadByID (plan_thread->GetID()); |
| 1183 | else if (other_thread) |
| 1184 | thread_list.SetSelectedThreadByID (other_thread->GetID()); |
| 1185 | else |
| 1186 | { |
| 1187 | if (curr_thread && curr_thread->IsValid()) |
| 1188 | thread = curr_thread; |
| 1189 | else |
| 1190 | thread = thread_list.GetThreadAtIndex(0); |
| 1191 | |
| 1192 | if (thread) |
| 1193 | thread_list.SetSelectedThreadByID (thread->GetID()); |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | // Drop the ThreadList mutex by here, since GetThreadStatus below might have to run code, |
| 1198 | // e.g. for Data formatters, and if we hold the ThreadList mutex, then the process is going to |
| 1199 | // have a hard time restarting the process. |
| 1200 | if (stream) |
| 1201 | { |
| 1202 | Debugger &debugger = process_sp->GetTarget().GetDebugger(); |
| 1203 | if (debugger.GetTargetList().GetSelectedTarget().get() == &process_sp->GetTarget()) |
| 1204 | { |
| 1205 | const bool only_threads_with_stop_reason = true; |
| 1206 | const uint32_t start_frame = 0; |
| 1207 | const uint32_t num_frames = 1; |
| 1208 | const uint32_t num_frames_with_source = 1; |
| 1209 | process_sp->GetStatus(*stream); |
| 1210 | process_sp->GetThreadStatus (*stream, |
| 1211 | only_threads_with_stop_reason, |
| 1212 | start_frame, |
| 1213 | num_frames, |
| 1214 | num_frames_with_source); |
| 1215 | } |
| 1216 | else |
| 1217 | { |
| 1218 | uint32_t target_idx = debugger.GetTargetList().GetIndexOfTarget(process_sp->GetTarget().shared_from_this()); |
| 1219 | if (target_idx != UINT32_MAX) |
| 1220 | stream->Printf ("Target %d: (", target_idx); |
| 1221 | else |
| 1222 | stream->Printf ("Target <unknown index>: ("); |
| 1223 | process_sp->GetTarget().Dump (stream, eDescriptionLevelBrief); |
| 1224 | stream->Printf (") stopped.\n"); |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | // Pop the process IO handler |
| 1229 | pop_process_io_handler = true; |
| 1230 | } |
| 1231 | break; |
| 1232 | } |
| 1233 | |
| 1234 | if (handle_pop && pop_process_io_handler) |
| 1235 | process_sp->PopProcessIOHandler(); |
| 1236 | |
| 1237 | return true; |
| 1238 | } |
| 1239 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1240 | |
| 1241 | StateType |
| 1242 | Process::WaitForState |
| 1243 | ( |
| 1244 | const TimeValue *timeout, |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1245 | const StateType *match_states, |
| 1246 | const uint32_t num_match_states |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1247 | ) |
| 1248 | { |
| 1249 | EventSP event_sp; |
| 1250 | uint32_t i; |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1251 | StateType state = GetState(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1252 | while (state != eStateInvalid) |
| 1253 | { |
Greg Clayton | 05faeb7 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1254 | // If we are exited or detached, we won't ever get back to any |
| 1255 | // other valid state... |
| 1256 | if (state == eStateDetached || state == eStateExited) |
| 1257 | return state; |
| 1258 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1259 | state = WaitForStateChangedEvents (timeout, event_sp, NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1260 | |
| 1261 | for (i=0; i<num_match_states; ++i) |
| 1262 | { |
| 1263 | if (match_states[i] == state) |
| 1264 | return state; |
| 1265 | } |
| 1266 | } |
| 1267 | return state; |
| 1268 | } |
| 1269 | |
Jim Ingham | 30f9b21 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 1270 | bool |
| 1271 | Process::HijackProcessEvents (Listener *listener) |
| 1272 | { |
| 1273 | if (listener != NULL) |
| 1274 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1275 | return HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
Jim Ingham | 30f9b21 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 1276 | } |
| 1277 | else |
| 1278 | return false; |
| 1279 | } |
| 1280 | |
| 1281 | void |
| 1282 | Process::RestoreProcessEvents () |
| 1283 | { |
| 1284 | RestoreBroadcaster(); |
| 1285 | } |
| 1286 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 1287 | bool |
| 1288 | Process::HijackPrivateProcessEvents (Listener *listener) |
| 1289 | { |
| 1290 | if (listener != NULL) |
| 1291 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1292 | return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 1293 | } |
| 1294 | else |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
| 1298 | void |
| 1299 | Process::RestorePrivateProcessEvents () |
| 1300 | { |
| 1301 | m_private_state_broadcaster.RestoreBroadcaster(); |
| 1302 | } |
| 1303 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1304 | StateType |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1305 | Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp, Listener *hijack_listener) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1306 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1307 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1308 | |
| 1309 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1310 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, |
| 1311 | static_cast<const void*>(timeout)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1312 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1313 | Listener *listener = hijack_listener; |
| 1314 | if (listener == NULL) |
| 1315 | listener = &m_listener; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1316 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1317 | StateType state = eStateInvalid; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1318 | if (listener->WaitForEventForBroadcasterWithType (timeout, |
| 1319 | this, |
| 1320 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, |
| 1321 | event_sp)) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1322 | { |
| 1323 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1324 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1325 | else if (log) |
| 1326 | log->Printf ("Process::%s got no event or was interrupted.", __FUNCTION__); |
| 1327 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1328 | |
| 1329 | if (log) |
| 1330 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1331 | __FUNCTION__, static_cast<const void*>(timeout), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1332 | StateAsCString(state)); |
| 1333 | return state; |
| 1334 | } |
| 1335 | |
| 1336 | Event * |
| 1337 | Process::PeekAtStateChangedEvents () |
| 1338 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1339 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1340 | |
| 1341 | if (log) |
| 1342 | log->Printf ("Process::%s...", __FUNCTION__); |
| 1343 | |
| 1344 | Event *event_ptr; |
Greg Clayton | 3fcbed6 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1345 | event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this, |
| 1346 | eBroadcastBitStateChanged); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1347 | if (log) |
| 1348 | { |
| 1349 | if (event_ptr) |
| 1350 | { |
| 1351 | log->Printf ("Process::%s (event_ptr) => %s", |
| 1352 | __FUNCTION__, |
| 1353 | StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr))); |
| 1354 | } |
| 1355 | else |
| 1356 | { |
| 1357 | log->Printf ("Process::%s no events found", |
| 1358 | __FUNCTION__); |
| 1359 | } |
| 1360 | } |
| 1361 | return event_ptr; |
| 1362 | } |
| 1363 | |
| 1364 | StateType |
| 1365 | Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 1366 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1367 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1368 | |
| 1369 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1370 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, |
| 1371 | static_cast<const void*>(timeout)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1372 | |
| 1373 | StateType state = eStateInvalid; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1374 | if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout, |
| 1375 | &m_private_state_broadcaster, |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1376 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1377 | event_sp)) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1378 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1379 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1380 | |
| 1381 | // This is a bit of a hack, but when we wait here we could very well return |
| 1382 | // to the command-line, and that could disable the log, which would render the |
| 1383 | // log we got above invalid. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1384 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1385 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
| 1386 | __FUNCTION__, static_cast<const void *>(timeout), |
| 1387 | state == eStateInvalid ? "TIMEOUT" : StateAsCString(state)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1388 | return state; |
| 1389 | } |
| 1390 | |
| 1391 | bool |
| 1392 | Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only) |
| 1393 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1394 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1395 | |
| 1396 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1397 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, |
| 1398 | static_cast<const void*>(timeout)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1399 | |
| 1400 | if (control_only) |
| 1401 | return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp); |
| 1402 | else |
| 1403 | return m_private_state_listener.WaitForEvent(timeout, event_sp); |
| 1404 | } |
| 1405 | |
| 1406 | bool |
| 1407 | Process::IsRunning () const |
| 1408 | { |
| 1409 | return StateIsRunningState (m_public_state.GetValue()); |
| 1410 | } |
| 1411 | |
| 1412 | int |
| 1413 | Process::GetExitStatus () |
| 1414 | { |
Todd Fiala | 7b0917a | 2014-09-15 20:07:33 +0000 | [diff] [blame] | 1415 | Mutex::Locker locker (m_exit_status_mutex); |
| 1416 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1417 | if (m_public_state.GetValue() == eStateExited) |
| 1418 | return m_exit_status; |
| 1419 | return -1; |
| 1420 | } |
| 1421 | |
Greg Clayton | 85851dd | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 1422 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1423 | const char * |
| 1424 | Process::GetExitDescription () |
| 1425 | { |
Todd Fiala | 7b0917a | 2014-09-15 20:07:33 +0000 | [diff] [blame] | 1426 | Mutex::Locker locker (m_exit_status_mutex); |
| 1427 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1428 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 1429 | return m_exit_string.c_str(); |
| 1430 | return NULL; |
| 1431 | } |
| 1432 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1433 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1434 | Process::SetExitStatus (int status, const char *cstr) |
| 1435 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1436 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1437 | if (log) |
| 1438 | log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)", |
| 1439 | status, status, |
| 1440 | cstr ? "\"" : "", |
| 1441 | cstr ? cstr : "NULL", |
| 1442 | cstr ? "\"" : ""); |
| 1443 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1444 | // We were already in the exited state |
| 1445 | if (m_private_state.GetValue() == eStateExited) |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1446 | { |
Greg Clayton | 385d603 | 2011-01-26 23:47:29 +0000 | [diff] [blame] | 1447 | if (log) |
| 1448 | 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] | 1449 | return false; |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1450 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1451 | |
Todd Fiala | 7b0917a | 2014-09-15 20:07:33 +0000 | [diff] [blame] | 1452 | // use a mutex to protect the status and string during updating |
| 1453 | { |
| 1454 | Mutex::Locker locker (m_exit_status_mutex); |
| 1455 | |
| 1456 | m_exit_status = status; |
| 1457 | if (cstr) |
| 1458 | m_exit_string = cstr; |
| 1459 | else |
| 1460 | m_exit_string.clear(); |
| 1461 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1462 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1463 | DidExit (); |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 1464 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1465 | SetPrivateState (eStateExited); |
| 1466 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | // 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] | 1470 | // the current host. The child process exits, the process will be |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1471 | // found in the global target list (we want to be completely sure that the |
| 1472 | // lldb_private::Process doesn't go away before we can deliver the signal. |
| 1473 | bool |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1474 | Process::SetProcessExitStatus (void *callback_baton, |
| 1475 | lldb::pid_t pid, |
| 1476 | bool exited, |
| 1477 | int signo, // Zero for no signal |
| 1478 | int exit_status // Exit value of process if signal is zero |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1479 | ) |
| 1480 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1481 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1482 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1483 | 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] | 1484 | callback_baton, |
| 1485 | pid, |
| 1486 | exited, |
| 1487 | signo, |
| 1488 | exit_status); |
| 1489 | |
| 1490 | if (exited) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1491 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1492 | TargetSP target_sp(Debugger::FindTargetWithProcessID (pid)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1493 | if (target_sp) |
| 1494 | { |
| 1495 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 1496 | if (process_sp) |
| 1497 | { |
| 1498 | const char *signal_cstr = NULL; |
| 1499 | if (signo) |
| 1500 | signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo); |
| 1501 | |
| 1502 | process_sp->SetExitStatus (exit_status, signal_cstr); |
| 1503 | } |
| 1504 | } |
| 1505 | return true; |
| 1506 | } |
| 1507 | return false; |
| 1508 | } |
| 1509 | |
| 1510 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1511 | void |
| 1512 | Process::UpdateThreadListIfNeeded () |
| 1513 | { |
| 1514 | const uint32_t stop_id = GetStopID(); |
| 1515 | if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID()) |
| 1516 | { |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1517 | const StateType state = GetPrivateState(); |
| 1518 | if (StateIsStoppedState (state, true)) |
| 1519 | { |
| 1520 | Mutex::Locker locker (m_thread_list.GetMutex ()); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1521 | // m_thread_list does have its own mutex, but we need to |
| 1522 | // hold onto the mutex between the call to UpdateThreadList(...) |
| 1523 | // and the os->UpdateThreadList(...) so it doesn't change on us |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1524 | ThreadList &old_thread_list = m_thread_list; |
| 1525 | ThreadList real_thread_list(this); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1526 | ThreadList new_thread_list(this); |
| 1527 | // Always update the thread list with the protocol specific |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1528 | // thread list, but only update if "true" is returned |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1529 | if (UpdateThreadList (m_thread_list_real, real_thread_list)) |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1530 | { |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 1531 | // Don't call into the OperatingSystem to update the thread list if we are shutting down, since |
| 1532 | // that may call back into the SBAPI's, requiring the API lock which is already held by whoever is |
| 1533 | // shutting us down, causing a deadlock. |
| 1534 | if (!m_destroy_in_process) |
| 1535 | { |
| 1536 | OperatingSystem *os = GetOperatingSystem (); |
| 1537 | if (os) |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1538 | { |
| 1539 | // Clear any old backing threads where memory threads might have been |
| 1540 | // backed by actual threads from the lldb_private::Process subclass |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1541 | size_t num_old_threads = old_thread_list.GetSize(false); |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1542 | for (size_t i=0; i<num_old_threads; ++i) |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1543 | old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread(); |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 1544 | |
| 1545 | // Now let the OperatingSystem plug-in update the thread list |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1546 | os->UpdateThreadList (old_thread_list, // Old list full of threads created by OS plug-in |
| 1547 | real_thread_list, // The actual thread list full of threads created by each lldb_private::Process subclass |
| 1548 | 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] | 1549 | } |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1550 | else |
| 1551 | { |
| 1552 | // No OS plug-in, the new thread list is the same as the real thread list |
| 1553 | new_thread_list = real_thread_list; |
| 1554 | } |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 1555 | } |
Jim Ingham | 02ff8e0 | 2013-06-22 00:55:02 +0000 | [diff] [blame] | 1556 | |
| 1557 | m_thread_list_real.Update(real_thread_list); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1558 | m_thread_list.Update (new_thread_list); |
| 1559 | m_thread_list.SetStopID (stop_id); |
Jason Molenda | a6e9130 | 2013-11-19 05:44:41 +0000 | [diff] [blame] | 1560 | |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 1561 | if (GetLastNaturalStopID () != m_extended_thread_stop_id) |
| 1562 | { |
| 1563 | // Clear any extended threads that we may have accumulated previously |
| 1564 | m_extended_thread_list.Clear(); |
| 1565 | m_extended_thread_stop_id = GetLastNaturalStopID (); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1566 | |
| 1567 | m_queue_list.Clear(); |
| 1568 | m_queue_list_stop_id = GetLastNaturalStopID (); |
Jason Molenda | 4ff1326 | 2013-11-20 00:31:38 +0000 | [diff] [blame] | 1569 | } |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1570 | } |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1571 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 1575 | void |
| 1576 | Process::UpdateQueueListIfNeeded () |
| 1577 | { |
| 1578 | if (m_system_runtime_ap.get()) |
| 1579 | { |
| 1580 | if (m_queue_list.GetSize() == 0 || m_queue_list_stop_id != GetLastNaturalStopID()) |
| 1581 | { |
| 1582 | const StateType state = GetPrivateState(); |
| 1583 | if (StateIsStoppedState (state, true)) |
| 1584 | { |
| 1585 | m_system_runtime_ap->PopulateQueueList (m_queue_list); |
| 1586 | m_queue_list_stop_id = GetLastNaturalStopID(); |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | |
Greg Clayton | a4d8747 | 2013-01-18 23:41:08 +0000 | [diff] [blame] | 1592 | ThreadSP |
| 1593 | Process::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context) |
| 1594 | { |
| 1595 | OperatingSystem *os = GetOperatingSystem (); |
| 1596 | if (os) |
| 1597 | return os->CreateThread(tid, context); |
| 1598 | return ThreadSP(); |
| 1599 | } |
| 1600 | |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1601 | uint32_t |
| 1602 | Process::GetNextThreadIndexID (uint64_t thread_id) |
| 1603 | { |
| 1604 | return AssignIndexIDToThread(thread_id); |
| 1605 | } |
| 1606 | |
| 1607 | bool |
| 1608 | Process::HasAssignedIndexIDToThread(uint64_t thread_id) |
| 1609 | { |
| 1610 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id); |
| 1611 | if (iterator == m_thread_id_to_index_id_map.end()) |
| 1612 | { |
| 1613 | return false; |
| 1614 | } |
| 1615 | else |
| 1616 | { |
| 1617 | return true; |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | uint32_t |
| 1622 | Process::AssignIndexIDToThread(uint64_t thread_id) |
| 1623 | { |
| 1624 | uint32_t result = 0; |
| 1625 | std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_index_id_map.find(thread_id); |
| 1626 | if (iterator == m_thread_id_to_index_id_map.end()) |
| 1627 | { |
| 1628 | result = ++m_thread_index_id; |
| 1629 | m_thread_id_to_index_id_map[thread_id] = result; |
| 1630 | } |
| 1631 | else |
| 1632 | { |
| 1633 | result = iterator->second; |
| 1634 | } |
| 1635 | |
| 1636 | return result; |
| 1637 | } |
| 1638 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1639 | StateType |
| 1640 | Process::GetState() |
| 1641 | { |
| 1642 | // If any other threads access this we will need a mutex for it |
| 1643 | return m_public_state.GetValue (); |
| 1644 | } |
| 1645 | |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1646 | bool |
| 1647 | Process::StateChangedIsExternallyHijacked() |
| 1648 | { |
| 1649 | if (IsHijackedForEvent(eBroadcastBitStateChanged)) |
| 1650 | { |
| 1651 | if (strcmp(m_hijacking_listeners.back()->GetName(), "lldb.Process.ResumeSynchronous.hijack")) |
| 1652 | return true; |
| 1653 | } |
| 1654 | return false; |
| 1655 | } |
| 1656 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1657 | void |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1658 | Process::SetPublicState (StateType new_state, bool restarted) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1659 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1660 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1661 | if (log) |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1662 | log->Printf("Process::SetPublicState (state = %s, restarted = %i)", StateAsCString(new_state), restarted); |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1663 | const StateType old_state = m_public_state.GetValue(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1664 | m_public_state.SetValue (new_state); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1665 | |
| 1666 | // On the transition from Run to Stopped, we unlock the writer end of the |
| 1667 | // run lock. The lock gets locked in Resume, which is the public API |
| 1668 | // to tell the program to run. |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1669 | if (!StateChangedIsExternallyHijacked()) |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1670 | { |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1671 | if (new_state == eStateDetached) |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1672 | { |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1673 | if (log) |
| 1674 | 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] | 1675 | m_public_run_lock.SetStopped(); |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1676 | } |
| 1677 | else |
| 1678 | { |
| 1679 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1680 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1681 | if ((old_state_is_stopped != new_state_is_stopped)) |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1682 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 1683 | if (new_state_is_stopped && !restarted) |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1684 | { |
| 1685 | if (log) |
| 1686 | log->Printf("Process::SetPublicState (%s) -- unlocking run lock", StateAsCString(new_state)); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1687 | m_public_run_lock.SetStopped(); |
Sean Callanan | 8b0737f | 2012-06-02 01:16:20 +0000 | [diff] [blame] | 1688 | } |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1689 | } |
Greg Clayton | 7fdf9ef | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1690 | } |
| 1691 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1694 | Error |
| 1695 | Process::Resume () |
| 1696 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1697 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1698 | if (log) |
| 1699 | log->Printf("Process::Resume -- locking run lock"); |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1700 | if (!m_public_run_lock.TrySetRunning()) |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1701 | { |
| 1702 | Error error("Resume request failed - process still running."); |
| 1703 | if (log) |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1704 | log->Printf ("Process::Resume: -- TrySetRunning failed, not resuming."); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 1705 | return error; |
| 1706 | } |
| 1707 | return PrivateResume(); |
| 1708 | } |
| 1709 | |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1710 | Error |
| 1711 | Process::ResumeSynchronous (Stream *stream) |
| 1712 | { |
| 1713 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
| 1714 | if (log) |
| 1715 | log->Printf("Process::ResumeSynchronous -- locking run lock"); |
| 1716 | if (!m_public_run_lock.TrySetRunning()) |
| 1717 | { |
| 1718 | Error error("Resume request failed - process still running."); |
| 1719 | if (log) |
| 1720 | log->Printf ("Process::Resume: -- TrySetRunning failed, not resuming."); |
| 1721 | return error; |
| 1722 | } |
| 1723 | |
| 1724 | ListenerSP listener_sp (new Listener("lldb.Process.ResumeSynchronous.hijack")); |
| 1725 | HijackProcessEvents(listener_sp.get()); |
| 1726 | |
| 1727 | Error error = PrivateResume(); |
| 1728 | |
| 1729 | StateType state = WaitForProcessToStop (NULL, NULL, true, listener_sp.get(), stream); |
| 1730 | |
| 1731 | // Undo the hijacking of process events... |
| 1732 | RestoreProcessEvents(); |
| 1733 | |
| 1734 | if (error.Success() && !StateIsStoppedState(state, false)) |
| 1735 | error.SetErrorStringWithFormat("process not in stopped state after synchronous resume: %s", StateAsCString(state)); |
| 1736 | |
| 1737 | return error; |
| 1738 | } |
| 1739 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1740 | StateType |
| 1741 | Process::GetPrivateState () |
| 1742 | { |
| 1743 | return m_private_state.GetValue(); |
| 1744 | } |
| 1745 | |
| 1746 | void |
| 1747 | Process::SetPrivateState (StateType new_state) |
| 1748 | { |
Greg Clayton | fb8b37a | 2014-07-14 23:09:29 +0000 | [diff] [blame] | 1749 | if (m_finalize_called) |
| 1750 | return; |
| 1751 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1752 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1753 | bool state_changed = false; |
| 1754 | |
| 1755 | if (log) |
| 1756 | log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state)); |
| 1757 | |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1758 | Mutex::Locker thread_locker(m_thread_list.GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1759 | Mutex::Locker locker(m_private_state.GetMutex()); |
| 1760 | |
| 1761 | const StateType old_state = m_private_state.GetValueNoLock (); |
| 1762 | state_changed = old_state != new_state; |
Ed Maste | c29693f | 2013-07-02 16:35:47 +0000 | [diff] [blame] | 1763 | |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1764 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1765 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1766 | if (old_state_is_stopped != new_state_is_stopped) |
| 1767 | { |
| 1768 | if (new_state_is_stopped) |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1769 | m_private_run_lock.SetStopped(); |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1770 | else |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 1771 | m_private_run_lock.SetRunning(); |
Greg Clayton | aa49c83 | 2013-05-03 22:25:56 +0000 | [diff] [blame] | 1772 | } |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1773 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1774 | if (state_changed) |
| 1775 | { |
| 1776 | m_private_state.SetValueNoLock (new_state); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1777 | if (StateIsStoppedState(new_state, false)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1778 | { |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1779 | // Note, this currently assumes that all threads in the list |
| 1780 | // stop when the process stops. In the future we will want to |
| 1781 | // support a debugging model where some threads continue to run |
| 1782 | // while others are stopped. When that happens we will either need |
| 1783 | // a way for the thread list to identify which threads are stopping |
| 1784 | // or create a special thread list containing only threads which |
| 1785 | // actually stopped. |
| 1786 | // |
| 1787 | // The process plugin is responsible for managing the actual |
| 1788 | // behavior of the threads and should have stopped any threads |
| 1789 | // that are going to stop before we get here. |
| 1790 | m_thread_list.DidStop(); |
| 1791 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1792 | m_mod_id.BumpStopID(); |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1793 | m_memory_cache.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1794 | if (log) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1795 | 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] | 1796 | } |
| 1797 | // Use our target to get a shared pointer to ourselves... |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 1798 | if (m_finalize_called && PrivateStateThreadIsValid() == false) |
| 1799 | BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (shared_from_this(), new_state)); |
| 1800 | else |
| 1801 | 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] | 1802 | } |
| 1803 | else |
| 1804 | { |
| 1805 | if (log) |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1806 | 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] | 1807 | } |
| 1808 | } |
| 1809 | |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 1810 | void |
| 1811 | Process::SetRunningUserExpression (bool on) |
| 1812 | { |
| 1813 | m_mod_id.SetRunningUserExpression (on); |
| 1814 | } |
| 1815 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1816 | addr_t |
| 1817 | Process::GetImageInfoAddress() |
| 1818 | { |
| 1819 | return LLDB_INVALID_ADDRESS; |
| 1820 | } |
| 1821 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1822 | //---------------------------------------------------------------------- |
| 1823 | // LoadImage |
| 1824 | // |
| 1825 | // This function provides a default implementation that works for most |
| 1826 | // unix variants. Any Process subclasses that need to do shared library |
| 1827 | // loading differently should override LoadImage and UnloadImage and |
| 1828 | // do what is needed. |
| 1829 | //---------------------------------------------------------------------- |
| 1830 | uint32_t |
| 1831 | Process::LoadImage (const FileSpec &image_spec, Error &error) |
| 1832 | { |
Greg Clayton | ac7a3db | 2012-04-18 00:05:19 +0000 | [diff] [blame] | 1833 | char path[PATH_MAX]; |
| 1834 | image_spec.GetPath(path, sizeof(path)); |
| 1835 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1836 | DynamicLoader *loader = GetDynamicLoader(); |
| 1837 | if (loader) |
| 1838 | { |
| 1839 | error = loader->CanLoadImage(); |
| 1840 | if (error.Fail()) |
| 1841 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1842 | } |
| 1843 | |
| 1844 | if (error.Success()) |
| 1845 | { |
| 1846 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1847 | |
| 1848 | if (thread_sp) |
| 1849 | { |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 1850 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1851 | |
| 1852 | if (frame_sp) |
| 1853 | { |
| 1854 | ExecutionContext exe_ctx; |
| 1855 | frame_sp->CalculateExecutionContext (exe_ctx); |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1856 | EvaluateExpressionOptions expr_options; |
| 1857 | expr_options.SetUnwindOnError(true); |
| 1858 | expr_options.SetIgnoreBreakpoints(true); |
| 1859 | expr_options.SetExecutionPolicy(eExecutionPolicyAlways); |
Jim Ingham | 4ac0443 | 2014-07-19 01:09:16 +0000 | [diff] [blame] | 1860 | expr_options.SetResultIsInternal(true); |
| 1861 | |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1862 | StreamString expr; |
Jim Ingham | 6971b86 | 2014-07-19 00:37:06 +0000 | [diff] [blame] | 1863 | expr.Printf(R"( |
| 1864 | struct __lldb_dlopen_result { void *image_ptr; const char *error_str; } the_result; |
| 1865 | the_result.image_ptr = dlopen ("%s", 2); |
| 1866 | if (the_result.image_ptr == (void *) 0x0) |
| 1867 | { |
| 1868 | the_result.error_str = dlerror(); |
| 1869 | } |
| 1870 | else |
| 1871 | { |
| 1872 | the_result.error_str = (const char *) 0x0; |
| 1873 | } |
| 1874 | the_result; |
| 1875 | )", |
| 1876 | path); |
| 1877 | const char *prefix = R"( |
| 1878 | extern "C" void* dlopen (const char *path, int mode); |
| 1879 | extern "C" const char *dlerror (void); |
| 1880 | )"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1881 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1882 | Error expr_error; |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1883 | ClangUserExpression::Evaluate (exe_ctx, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1884 | expr_options, |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1885 | expr.GetData(), |
| 1886 | prefix, |
| 1887 | result_valobj_sp, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1888 | expr_error); |
| 1889 | if (expr_error.Success()) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1890 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1891 | error = result_valobj_sp->GetError(); |
| 1892 | if (error.Success()) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1893 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1894 | Scalar scalar; |
Jim Ingham | 6971b86 | 2014-07-19 00:37:06 +0000 | [diff] [blame] | 1895 | ValueObjectSP image_ptr_sp = result_valobj_sp->GetChildAtIndex(0, true); |
| 1896 | if (image_ptr_sp && image_ptr_sp->ResolveValue (scalar)) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1897 | { |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1898 | addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 1899 | if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS) |
| 1900 | { |
| 1901 | uint32_t image_token = m_image_tokens.size(); |
| 1902 | m_image_tokens.push_back (image_ptr); |
| 1903 | return image_token; |
| 1904 | } |
Jim Ingham | 3ac7cf3 | 2014-07-17 18:55:25 +0000 | [diff] [blame] | 1905 | else if (image_ptr == 0) |
| 1906 | { |
Jim Ingham | 6971b86 | 2014-07-19 00:37:06 +0000 | [diff] [blame] | 1907 | ValueObjectSP error_str_sp = result_valobj_sp->GetChildAtIndex(1, true); |
| 1908 | if (error_str_sp) |
Jim Ingham | 3ac7cf3 | 2014-07-17 18:55:25 +0000 | [diff] [blame] | 1909 | { |
Jim Ingham | 6971b86 | 2014-07-19 00:37:06 +0000 | [diff] [blame] | 1910 | if (error_str_sp->IsCStringContainer(true)) |
Jim Ingham | 3ac7cf3 | 2014-07-17 18:55:25 +0000 | [diff] [blame] | 1911 | { |
Enrico Granata | 2206b48 | 2014-10-30 18:27:31 +0000 | [diff] [blame] | 1912 | DataBufferSP buffer_sp(new DataBufferHeap(10240,0)); |
| 1913 | size_t num_chars = error_str_sp->ReadPointedString (buffer_sp, error, 10240); |
Jim Ingham | 3ac7cf3 | 2014-07-17 18:55:25 +0000 | [diff] [blame] | 1914 | if (error.Success() && num_chars > 0) |
| 1915 | { |
| 1916 | error.Clear(); |
Enrico Granata | 2206b48 | 2014-10-30 18:27:31 +0000 | [diff] [blame] | 1917 | error.SetErrorStringWithFormat("dlopen error: %s", buffer_sp->GetBytes()); |
| 1918 | } |
| 1919 | else |
| 1920 | { |
| 1921 | error.Clear(); |
| 1922 | error.SetErrorStringWithFormat("dlopen failed for unknown reasons."); |
Jim Ingham | 3ac7cf3 | 2014-07-17 18:55:25 +0000 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | } |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | } |
Jim Ingham | 6c9ed91 | 2014-04-03 01:26:14 +0000 | [diff] [blame] | 1930 | else |
| 1931 | error = expr_error; |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1932 | } |
| 1933 | } |
| 1934 | } |
Greg Clayton | ac7a3db | 2012-04-18 00:05:19 +0000 | [diff] [blame] | 1935 | if (!error.AsCString()) |
| 1936 | error.SetErrorStringWithFormat("unable to load '%s'", path); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1937 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1938 | } |
| 1939 | |
| 1940 | //---------------------------------------------------------------------- |
| 1941 | // UnloadImage |
| 1942 | // |
| 1943 | // This function provides a default implementation that works for most |
| 1944 | // unix variants. Any Process subclasses that need to do shared library |
| 1945 | // loading differently should override LoadImage and UnloadImage and |
| 1946 | // do what is needed. |
| 1947 | //---------------------------------------------------------------------- |
| 1948 | Error |
| 1949 | Process::UnloadImage (uint32_t image_token) |
| 1950 | { |
| 1951 | Error error; |
| 1952 | if (image_token < m_image_tokens.size()) |
| 1953 | { |
| 1954 | const addr_t image_addr = m_image_tokens[image_token]; |
| 1955 | if (image_addr == LLDB_INVALID_ADDRESS) |
| 1956 | { |
| 1957 | error.SetErrorString("image already unloaded"); |
| 1958 | } |
| 1959 | else |
| 1960 | { |
| 1961 | DynamicLoader *loader = GetDynamicLoader(); |
| 1962 | if (loader) |
| 1963 | error = loader->CanLoadImage(); |
| 1964 | |
| 1965 | if (error.Success()) |
| 1966 | { |
| 1967 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1968 | |
| 1969 | if (thread_sp) |
| 1970 | { |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 1971 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1972 | |
| 1973 | if (frame_sp) |
| 1974 | { |
| 1975 | ExecutionContext exe_ctx; |
| 1976 | frame_sp->CalculateExecutionContext (exe_ctx); |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1977 | EvaluateExpressionOptions expr_options; |
| 1978 | expr_options.SetUnwindOnError(true); |
| 1979 | expr_options.SetIgnoreBreakpoints(true); |
| 1980 | expr_options.SetExecutionPolicy(eExecutionPolicyAlways); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1981 | StreamString expr; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1982 | expr.Printf("dlclose ((void *)0x%" PRIx64 ")", image_addr); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1983 | const char *prefix = "extern \"C\" int dlclose(void* handle);\n"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1984 | lldb::ValueObjectSP result_valobj_sp; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1985 | Error expr_error; |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1986 | ClangUserExpression::Evaluate (exe_ctx, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1987 | expr_options, |
Greg Clayton | 26ab83d | 2012-10-31 20:49:04 +0000 | [diff] [blame] | 1988 | expr.GetData(), |
| 1989 | prefix, |
| 1990 | result_valobj_sp, |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 1991 | expr_error); |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1992 | if (result_valobj_sp->GetError().Success()) |
| 1993 | { |
| 1994 | Scalar scalar; |
Jim Ingham | 6035b67 | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 1995 | if (result_valobj_sp->ResolveValue (scalar)) |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1996 | { |
| 1997 | if (scalar.UInt(1)) |
| 1998 | { |
| 1999 | error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData()); |
| 2000 | } |
| 2001 | else |
| 2002 | { |
| 2003 | m_image_tokens[image_token] = LLDB_INVALID_ADDRESS; |
| 2004 | } |
| 2005 | } |
| 2006 | } |
| 2007 | else |
| 2008 | { |
| 2009 | error = result_valobj_sp->GetError(); |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | } |
| 2014 | } |
| 2015 | } |
| 2016 | else |
| 2017 | { |
| 2018 | error.SetErrorString("invalid image token"); |
| 2019 | } |
| 2020 | return error; |
| 2021 | } |
| 2022 | |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 2023 | const lldb::ABISP & |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2024 | Process::GetABI() |
| 2025 | { |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 2026 | if (!m_abi_sp) |
| 2027 | m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture()); |
| 2028 | return m_abi_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2031 | LanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2032 | Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2033 | { |
| 2034 | LanguageRuntimeCollection::iterator pos; |
| 2035 | pos = m_language_runtimes.find (language); |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2036 | if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second)) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2037 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2038 | lldb::LanguageRuntimeSP runtime_sp(LanguageRuntime::FindPlugin(this, language)); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2039 | |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2040 | m_language_runtimes[language] = runtime_sp; |
| 2041 | return runtime_sp.get(); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2042 | } |
| 2043 | else |
| 2044 | return (*pos).second.get(); |
| 2045 | } |
| 2046 | |
| 2047 | CPPLanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2048 | Process::GetCPPLanguageRuntime (bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2049 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2050 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2051 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus) |
| 2052 | return static_cast<CPPLanguageRuntime *> (runtime); |
| 2053 | return NULL; |
| 2054 | } |
| 2055 | |
| 2056 | ObjCLanguageRuntime * |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2057 | Process::GetObjCLanguageRuntime (bool retry_if_null) |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2058 | { |
Jim Ingham | ab17524 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 2059 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC, retry_if_null); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 2060 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC) |
| 2061 | return static_cast<ObjCLanguageRuntime *> (runtime); |
| 2062 | return NULL; |
| 2063 | } |
| 2064 | |
Enrico Granata | fd4c84e | 2012-05-21 16:51:35 +0000 | [diff] [blame] | 2065 | bool |
| 2066 | Process::IsPossibleDynamicValue (ValueObject& in_value) |
| 2067 | { |
| 2068 | if (in_value.IsDynamic()) |
| 2069 | return false; |
| 2070 | LanguageType known_type = in_value.GetObjectRuntimeLanguage(); |
| 2071 | |
| 2072 | if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC) |
| 2073 | { |
| 2074 | LanguageRuntime *runtime = GetLanguageRuntime (known_type); |
| 2075 | return runtime ? runtime->CouldHaveDynamicValue(in_value) : false; |
| 2076 | } |
| 2077 | |
| 2078 | LanguageRuntime *cpp_runtime = GetLanguageRuntime (eLanguageTypeC_plus_plus); |
| 2079 | if (cpp_runtime && cpp_runtime->CouldHaveDynamicValue(in_value)) |
| 2080 | return true; |
| 2081 | |
| 2082 | LanguageRuntime *objc_runtime = GetLanguageRuntime (eLanguageTypeObjC); |
| 2083 | return objc_runtime ? objc_runtime->CouldHaveDynamicValue(in_value) : false; |
| 2084 | } |
| 2085 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2086 | BreakpointSiteList & |
| 2087 | Process::GetBreakpointSiteList() |
| 2088 | { |
| 2089 | return m_breakpoint_site_list; |
| 2090 | } |
| 2091 | |
| 2092 | const BreakpointSiteList & |
| 2093 | Process::GetBreakpointSiteList() const |
| 2094 | { |
| 2095 | return m_breakpoint_site_list; |
| 2096 | } |
| 2097 | |
| 2098 | |
| 2099 | void |
| 2100 | Process::DisableAllBreakpointSites () |
| 2101 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2102 | m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void { |
| 2103 | // bp_site->SetEnabled(true); |
| 2104 | DisableBreakpointSite(bp_site); |
| 2105 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | Error |
| 2109 | Process::ClearBreakpointSiteByID (lldb::user_id_t break_id) |
| 2110 | { |
| 2111 | Error error (DisableBreakpointSiteByID (break_id)); |
| 2112 | |
| 2113 | if (error.Success()) |
| 2114 | m_breakpoint_site_list.Remove(break_id); |
| 2115 | |
| 2116 | return error; |
| 2117 | } |
| 2118 | |
| 2119 | Error |
| 2120 | Process::DisableBreakpointSiteByID (lldb::user_id_t break_id) |
| 2121 | { |
| 2122 | Error error; |
| 2123 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 2124 | if (bp_site_sp) |
| 2125 | { |
| 2126 | if (bp_site_sp->IsEnabled()) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2127 | error = DisableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2128 | } |
| 2129 | else |
| 2130 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2131 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | return error; |
| 2135 | } |
| 2136 | |
| 2137 | Error |
| 2138 | Process::EnableBreakpointSiteByID (lldb::user_id_t break_id) |
| 2139 | { |
| 2140 | Error error; |
| 2141 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 2142 | if (bp_site_sp) |
| 2143 | { |
| 2144 | if (!bp_site_sp->IsEnabled()) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2145 | error = EnableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2146 | } |
| 2147 | else |
| 2148 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2149 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, break_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2150 | } |
| 2151 | return error; |
| 2152 | } |
| 2153 | |
Stephen Wilson | 50bd94f | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 2154 | lldb::break_id_t |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 2155 | Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2156 | { |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 2157 | addr_t load_addr = LLDB_INVALID_ADDRESS; |
| 2158 | |
| 2159 | bool show_error = true; |
| 2160 | switch (GetState()) |
| 2161 | { |
| 2162 | case eStateInvalid: |
| 2163 | case eStateUnloaded: |
| 2164 | case eStateConnected: |
| 2165 | case eStateAttaching: |
| 2166 | case eStateLaunching: |
| 2167 | case eStateDetached: |
| 2168 | case eStateExited: |
| 2169 | show_error = false; |
| 2170 | break; |
| 2171 | |
| 2172 | case eStateStopped: |
| 2173 | case eStateRunning: |
| 2174 | case eStateStepping: |
| 2175 | case eStateCrashed: |
| 2176 | case eStateSuspended: |
| 2177 | show_error = IsAlive(); |
| 2178 | break; |
| 2179 | } |
| 2180 | |
| 2181 | // Reset the IsIndirect flag here, in case the location changes from |
| 2182 | // pointing to a indirect symbol to a regular symbol. |
| 2183 | owner->SetIsIndirect (false); |
| 2184 | |
| 2185 | if (owner->ShouldResolveIndirectFunctions()) |
| 2186 | { |
| 2187 | Symbol *symbol = owner->GetAddress().CalculateSymbolContextSymbol(); |
| 2188 | if (symbol && symbol->IsIndirect()) |
| 2189 | { |
| 2190 | Error error; |
| 2191 | load_addr = ResolveIndirectFunction (&symbol->GetAddress(), error); |
| 2192 | if (!error.Success() && show_error) |
| 2193 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 2194 | m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to resolve indirect function at 0x%" PRIx64 " for breakpoint %i.%i: %s\n", |
| 2195 | symbol->GetAddress().GetLoadAddress(&m_target), |
| 2196 | owner->GetBreakpoint().GetID(), |
| 2197 | owner->GetID(), |
Sylvestre Ledru | f610289 | 2014-08-11 18:06:28 +0000 | [diff] [blame] | 2198 | error.AsCString() ? error.AsCString() : "unknown error"); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 2199 | return LLDB_INVALID_BREAK_ID; |
| 2200 | } |
| 2201 | Address resolved_address(load_addr); |
| 2202 | load_addr = resolved_address.GetOpcodeLoadAddress (&m_target); |
| 2203 | owner->SetIsIndirect(true); |
| 2204 | } |
| 2205 | else |
| 2206 | load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target); |
| 2207 | } |
| 2208 | else |
| 2209 | load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target); |
| 2210 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2211 | if (load_addr != LLDB_INVALID_ADDRESS) |
| 2212 | { |
| 2213 | BreakpointSiteSP bp_site_sp; |
| 2214 | |
| 2215 | // Look up this breakpoint site. If it exists, then add this new owner, otherwise |
| 2216 | // create a new breakpoint site and add it. |
| 2217 | |
| 2218 | bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr); |
| 2219 | |
| 2220 | if (bp_site_sp) |
| 2221 | { |
| 2222 | bp_site_sp->AddOwner (owner); |
| 2223 | owner->SetBreakpointSite (bp_site_sp); |
| 2224 | return bp_site_sp->GetID(); |
| 2225 | } |
| 2226 | else |
| 2227 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2228 | 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] | 2229 | if (bp_site_sp) |
| 2230 | { |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2231 | Error error = EnableBreakpointSite (bp_site_sp.get()); |
| 2232 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2233 | { |
| 2234 | owner->SetBreakpointSite (bp_site_sp); |
| 2235 | return m_breakpoint_site_list.Add (bp_site_sp); |
| 2236 | } |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2237 | else |
| 2238 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2239 | if (show_error) |
| 2240 | { |
| 2241 | // Report error for setting breakpoint... |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 2242 | m_target.GetDebugger().GetErrorFile()->Printf ("warning: failed to set breakpoint site at 0x%" PRIx64 " for breakpoint %i.%i: %s\n", |
| 2243 | load_addr, |
| 2244 | owner->GetBreakpoint().GetID(), |
| 2245 | owner->GetID(), |
Sylvestre Ledru | f610289 | 2014-08-11 18:06:28 +0000 | [diff] [blame] | 2246 | error.AsCString() ? error.AsCString() : "unknown error"); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2247 | } |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2248 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2249 | } |
| 2250 | } |
| 2251 | } |
| 2252 | // We failed to enable the breakpoint |
| 2253 | return LLDB_INVALID_BREAK_ID; |
| 2254 | |
| 2255 | } |
| 2256 | |
| 2257 | void |
| 2258 | Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp) |
| 2259 | { |
| 2260 | uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id); |
| 2261 | if (num_owners == 0) |
| 2262 | { |
Jim Ingham | f1ff3bb | 2013-04-06 00:16:39 +0000 | [diff] [blame] | 2263 | // Don't try to disable the site if we don't have a live process anymore. |
| 2264 | if (IsAlive()) |
| 2265 | DisableBreakpointSite (bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2266 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | |
| 2271 | size_t |
| 2272 | Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const |
| 2273 | { |
| 2274 | size_t bytes_removed = 0; |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2275 | BreakpointSiteList bp_sites_in_range; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2276 | |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2277 | 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] | 2278 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2279 | bp_sites_in_range.ForEach([bp_addr, size, buf, &bytes_removed](BreakpointSite *bp_site) -> void { |
| 2280 | if (bp_site->GetType() == BreakpointSite::eSoftware) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2281 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2282 | addr_t intersect_addr; |
| 2283 | size_t intersect_size; |
| 2284 | size_t opcode_offset; |
| 2285 | 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] | 2286 | { |
| 2287 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 2288 | 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] | 2289 | assert(opcode_offset + intersect_size <= bp_site->GetByteSize()); |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2290 | size_t buf_offset = intersect_addr - bp_addr; |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2291 | ::memcpy(buf + buf_offset, bp_site->GetSavedOpcodeBytes() + opcode_offset, intersect_size); |
Jim Ingham | 20c7719 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 2292 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2293 | } |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2294 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2295 | } |
| 2296 | return bytes_removed; |
| 2297 | } |
| 2298 | |
| 2299 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 2300 | |
| 2301 | size_t |
| 2302 | Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site) |
| 2303 | { |
| 2304 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 2305 | if (platform_sp) |
| 2306 | return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site); |
| 2307 | return 0; |
| 2308 | } |
| 2309 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2310 | Error |
| 2311 | Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 2312 | { |
| 2313 | Error error; |
| 2314 | assert (bp_site != NULL); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2315 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2316 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
| 2317 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2318 | 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] | 2319 | if (bp_site->IsEnabled()) |
| 2320 | { |
| 2321 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2322 | 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] | 2323 | return error; |
| 2324 | } |
| 2325 | |
| 2326 | if (bp_addr == LLDB_INVALID_ADDRESS) |
| 2327 | { |
| 2328 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 2329 | return error; |
| 2330 | } |
| 2331 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 2332 | // trap for the breakpoint site |
| 2333 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 2334 | |
| 2335 | if (bp_opcode_size == 0) |
| 2336 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2337 | 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] | 2338 | } |
| 2339 | else |
| 2340 | { |
| 2341 | const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 2342 | |
| 2343 | if (bp_opcode_bytes == NULL) |
| 2344 | { |
| 2345 | error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 2346 | return error; |
| 2347 | } |
| 2348 | |
| 2349 | // Save the original opcode by reading it |
| 2350 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size) |
| 2351 | { |
| 2352 | // Write a software breakpoint in place of the original opcode |
| 2353 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 2354 | { |
| 2355 | uint8_t verify_bp_opcode_bytes[64]; |
| 2356 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 2357 | { |
| 2358 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0) |
| 2359 | { |
| 2360 | bp_site->SetEnabled(true); |
| 2361 | bp_site->SetType (BreakpointSite::eSoftware); |
| 2362 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2363 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- SUCCESS", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2364 | bp_site->GetID(), |
| 2365 | (uint64_t)bp_addr); |
| 2366 | } |
| 2367 | else |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 2368 | error.SetErrorString("failed to verify the breakpoint trap in memory."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2369 | } |
| 2370 | else |
| 2371 | error.SetErrorString("Unable to read memory to verify breakpoint trap."); |
| 2372 | } |
| 2373 | else |
| 2374 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 2375 | } |
| 2376 | else |
| 2377 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 2378 | } |
Stephen Wilson | 78a4feb | 2011-01-12 04:20:03 +0000 | [diff] [blame] | 2379 | if (log && error.Fail()) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2380 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2381 | bp_site->GetID(), |
| 2382 | (uint64_t)bp_addr, |
| 2383 | error.AsCString()); |
| 2384 | return error; |
| 2385 | } |
| 2386 | |
| 2387 | Error |
| 2388 | Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 2389 | { |
| 2390 | Error error; |
| 2391 | assert (bp_site != NULL); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2392 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2393 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 2394 | lldb::user_id_t breakID = bp_site->GetID(); |
| 2395 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2396 | 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] | 2397 | |
| 2398 | if (bp_site->IsHardware()) |
| 2399 | { |
| 2400 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 2401 | } |
| 2402 | else if (bp_site->IsEnabled()) |
| 2403 | { |
| 2404 | const size_t break_op_size = bp_site->GetByteSize(); |
| 2405 | const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes(); |
| 2406 | if (break_op_size > 0) |
| 2407 | { |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 2408 | // Clear a software breakpoint instruction |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2409 | uint8_t curr_break_op[8]; |
Stephen Wilson | 4ab4768 | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 2410 | assert (break_op_size <= sizeof(curr_break_op)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2411 | bool break_op_found = false; |
| 2412 | |
| 2413 | // Read the breakpoint opcode |
| 2414 | if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size) |
| 2415 | { |
| 2416 | bool verify = false; |
| 2417 | // Make sure we have the a breakpoint opcode exists at this address |
| 2418 | if (::memcmp (curr_break_op, break_op, break_op_size) == 0) |
| 2419 | { |
| 2420 | break_op_found = true; |
| 2421 | // We found a valid breakpoint opcode at this address, now restore |
| 2422 | // the saved opcode. |
| 2423 | if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size) |
| 2424 | { |
| 2425 | verify = true; |
| 2426 | } |
| 2427 | else |
| 2428 | error.SetErrorString("Memory write failed when restoring original opcode."); |
| 2429 | } |
| 2430 | else |
| 2431 | { |
| 2432 | error.SetErrorString("Original breakpoint trap is no longer in memory."); |
| 2433 | // Set verify to true and so we can check if the original opcode has already been restored |
| 2434 | verify = true; |
| 2435 | } |
| 2436 | |
| 2437 | if (verify) |
| 2438 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2439 | uint8_t verify_opcode[8]; |
Stephen Wilson | 4ab4768 | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 2440 | assert (break_op_size < sizeof(verify_opcode)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2441 | // Verify that our original opcode made it back to the inferior |
| 2442 | if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size) |
| 2443 | { |
| 2444 | // compare the memory we just read with the original opcode |
| 2445 | if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0) |
| 2446 | { |
| 2447 | // SUCCESS |
| 2448 | bp_site->SetEnabled(false); |
| 2449 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2450 | 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] | 2451 | return error; |
| 2452 | } |
| 2453 | else |
| 2454 | { |
| 2455 | if (break_op_found) |
| 2456 | error.SetErrorString("Failed to restore original opcode."); |
| 2457 | } |
| 2458 | } |
| 2459 | else |
| 2460 | error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored."); |
| 2461 | } |
| 2462 | } |
| 2463 | else |
| 2464 | error.SetErrorString("Unable to read memory that should contain the breakpoint trap."); |
| 2465 | } |
| 2466 | } |
| 2467 | else |
| 2468 | { |
| 2469 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2470 | 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] | 2471 | return error; |
| 2472 | } |
| 2473 | |
| 2474 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2475 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 " -- FAILED: %s", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2476 | bp_site->GetID(), |
| 2477 | (uint64_t)bp_addr, |
| 2478 | error.AsCString()); |
| 2479 | return error; |
| 2480 | |
| 2481 | } |
| 2482 | |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2483 | // Uncomment to verify memory caching works after making changes to caching code |
| 2484 | //#define VERIFY_MEMORY_READS |
| 2485 | |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2486 | size_t |
| 2487 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 2488 | { |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 2489 | error.Clear(); |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2490 | if (!GetDisableMemoryCache()) |
| 2491 | { |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2492 | #if defined (VERIFY_MEMORY_READS) |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2493 | // Memory caching is enabled, with debug verification |
| 2494 | |
| 2495 | if (buf && size) |
| 2496 | { |
| 2497 | // Uncomment the line below to make sure memory caching is working. |
| 2498 | // I ran this through the test suite and got no assertions, so I am |
| 2499 | // pretty confident this is working well. If any changes are made to |
| 2500 | // memory caching, uncomment the line below and test your changes! |
| 2501 | |
| 2502 | // Verify all memory reads by using the cache first, then redundantly |
| 2503 | // reading the same memory from the inferior and comparing to make sure |
| 2504 | // everything is exactly the same. |
| 2505 | std::string verify_buf (size, '\0'); |
| 2506 | assert (verify_buf.size() == size); |
| 2507 | const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error); |
| 2508 | Error verify_error; |
| 2509 | const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error); |
| 2510 | assert (cache_bytes_read == verify_bytes_read); |
| 2511 | assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0); |
| 2512 | assert (verify_error.Success() == error.Success()); |
| 2513 | return cache_bytes_read; |
| 2514 | } |
| 2515 | return 0; |
| 2516 | #else // !defined(VERIFY_MEMORY_READS) |
| 2517 | // Memory caching is enabled, without debug verification |
| 2518 | |
| 2519 | return m_memory_cache.Read (addr, buf, size, error); |
| 2520 | #endif // defined (VERIFY_MEMORY_READS) |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2521 | } |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2522 | else |
| 2523 | { |
| 2524 | // Memory caching is disabled |
| 2525 | |
| 2526 | return ReadMemoryFromInferior (addr, buf, size, error); |
| 2527 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2528 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2529 | |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2530 | size_t |
| 2531 | Process::ReadCStringFromMemory (addr_t addr, std::string &out_str, Error &error) |
| 2532 | { |
Greg Clayton | de87c0f | 2012-05-19 00:18:00 +0000 | [diff] [blame] | 2533 | char buf[256]; |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2534 | out_str.clear(); |
| 2535 | addr_t curr_addr = addr; |
| 2536 | while (1) |
| 2537 | { |
| 2538 | size_t length = ReadCStringFromMemory (curr_addr, buf, sizeof(buf), error); |
| 2539 | if (length == 0) |
| 2540 | break; |
| 2541 | out_str.append(buf, length); |
| 2542 | // If we got "length - 1" bytes, we didn't get the whole C string, we |
| 2543 | // need to read some more characters |
| 2544 | if (length == sizeof(buf) - 1) |
| 2545 | curr_addr += length; |
| 2546 | else |
| 2547 | break; |
| 2548 | } |
| 2549 | return out_str.size(); |
| 2550 | } |
| 2551 | |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2552 | |
| 2553 | size_t |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2554 | Process::ReadStringFromMemory (addr_t addr, char *dst, size_t max_bytes, Error &error, |
| 2555 | size_t type_width) |
| 2556 | { |
| 2557 | size_t total_bytes_read = 0; |
| 2558 | if (dst && max_bytes && type_width && max_bytes >= type_width) |
| 2559 | { |
| 2560 | // Ensure a null terminator independent of the number of bytes that is read. |
| 2561 | memset (dst, 0, max_bytes); |
| 2562 | size_t bytes_left = max_bytes - type_width; |
| 2563 | |
| 2564 | const char terminator[4] = {'\0', '\0', '\0', '\0'}; |
| 2565 | assert(sizeof(terminator) >= type_width && |
| 2566 | "Attempting to validate a string with more than 4 bytes per character!"); |
| 2567 | |
| 2568 | addr_t curr_addr = addr; |
| 2569 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2570 | char *curr_dst = dst; |
| 2571 | |
| 2572 | error.Clear(); |
| 2573 | while (bytes_left > 0 && error.Success()) |
| 2574 | { |
| 2575 | addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); |
| 2576 | addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2577 | size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error); |
| 2578 | |
| 2579 | if (bytes_read == 0) |
| 2580 | break; |
| 2581 | |
| 2582 | // Search for a null terminator of correct size and alignment in bytes_read |
| 2583 | size_t aligned_start = total_bytes_read - total_bytes_read % type_width; |
| 2584 | for (size_t i = aligned_start; i + type_width <= total_bytes_read + bytes_read; i += type_width) |
| 2585 | if (::strncmp(&dst[i], terminator, type_width) == 0) |
| 2586 | { |
| 2587 | error.Clear(); |
| 2588 | return i; |
| 2589 | } |
| 2590 | |
| 2591 | total_bytes_read += bytes_read; |
| 2592 | curr_dst += bytes_read; |
| 2593 | curr_addr += bytes_read; |
| 2594 | bytes_left -= bytes_read; |
| 2595 | } |
| 2596 | } |
| 2597 | else |
| 2598 | { |
| 2599 | if (max_bytes) |
| 2600 | error.SetErrorString("invalid arguments"); |
| 2601 | } |
| 2602 | return total_bytes_read; |
| 2603 | } |
| 2604 | |
| 2605 | // Deprecated in favor of ReadStringFromMemory which has wchar support and correct code to find |
| 2606 | // null terminators. |
| 2607 | size_t |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2608 | 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] | 2609 | { |
| 2610 | size_t total_cstr_len = 0; |
| 2611 | if (dst && dst_max_len) |
| 2612 | { |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2613 | result_error.Clear(); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2614 | // NULL out everything just to be safe |
| 2615 | memset (dst, 0, dst_max_len); |
| 2616 | Error error; |
| 2617 | addr_t curr_addr = addr; |
| 2618 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2619 | size_t bytes_left = dst_max_len - 1; |
| 2620 | char *curr_dst = dst; |
| 2621 | |
| 2622 | while (bytes_left > 0) |
| 2623 | { |
| 2624 | addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); |
| 2625 | addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2626 | size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error); |
| 2627 | |
| 2628 | if (bytes_read == 0) |
| 2629 | { |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2630 | result_error = error; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2631 | dst[total_cstr_len] = '\0'; |
| 2632 | break; |
| 2633 | } |
| 2634 | const size_t len = strlen(curr_dst); |
| 2635 | |
| 2636 | total_cstr_len += len; |
| 2637 | |
| 2638 | if (len < bytes_to_read) |
| 2639 | break; |
| 2640 | |
| 2641 | curr_dst += bytes_read; |
| 2642 | curr_addr += bytes_read; |
| 2643 | bytes_left -= bytes_read; |
| 2644 | } |
| 2645 | } |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2646 | else |
| 2647 | { |
| 2648 | if (dst == NULL) |
| 2649 | result_error.SetErrorString("invalid arguments"); |
| 2650 | else |
| 2651 | result_error.Clear(); |
| 2652 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2653 | return total_cstr_len; |
| 2654 | } |
| 2655 | |
| 2656 | size_t |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2657 | Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error) |
| 2658 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2659 | if (buf == NULL || size == 0) |
| 2660 | return 0; |
| 2661 | |
| 2662 | size_t bytes_read = 0; |
| 2663 | uint8_t *bytes = (uint8_t *)buf; |
| 2664 | |
| 2665 | while (bytes_read < size) |
| 2666 | { |
| 2667 | const size_t curr_size = size - bytes_read; |
| 2668 | const size_t curr_bytes_read = DoReadMemory (addr + bytes_read, |
| 2669 | bytes + bytes_read, |
| 2670 | curr_size, |
| 2671 | error); |
| 2672 | bytes_read += curr_bytes_read; |
| 2673 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 2674 | break; |
| 2675 | } |
| 2676 | |
| 2677 | // Replace any software breakpoint opcodes that fall into this range back |
| 2678 | // into "buf" before we return |
| 2679 | if (bytes_read > 0) |
| 2680 | RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf); |
| 2681 | return bytes_read; |
| 2682 | } |
| 2683 | |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2684 | uint64_t |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2685 | 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] | 2686 | { |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2687 | Scalar scalar; |
| 2688 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error)) |
| 2689 | return scalar.ULongLong(fail_value); |
| 2690 | return fail_value; |
| 2691 | } |
| 2692 | |
| 2693 | addr_t |
| 2694 | Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error) |
| 2695 | { |
| 2696 | Scalar scalar; |
| 2697 | if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error)) |
| 2698 | return scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 2699 | return LLDB_INVALID_ADDRESS; |
| 2700 | } |
| 2701 | |
| 2702 | |
| 2703 | bool |
| 2704 | Process::WritePointerToMemory (lldb::addr_t vm_addr, |
| 2705 | lldb::addr_t ptr_value, |
| 2706 | Error &error) |
| 2707 | { |
| 2708 | Scalar scalar; |
| 2709 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2710 | if (addr_byte_size <= 4) |
| 2711 | scalar = (uint32_t)ptr_value; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2712 | else |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2713 | scalar = ptr_value; |
| 2714 | return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2717 | size_t |
| 2718 | Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error) |
| 2719 | { |
| 2720 | size_t bytes_written = 0; |
| 2721 | const uint8_t *bytes = (const uint8_t *)buf; |
| 2722 | |
| 2723 | while (bytes_written < size) |
| 2724 | { |
| 2725 | const size_t curr_size = size - bytes_written; |
| 2726 | const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written, |
| 2727 | bytes + bytes_written, |
| 2728 | curr_size, |
| 2729 | error); |
| 2730 | bytes_written += curr_bytes_written; |
| 2731 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 2732 | break; |
| 2733 | } |
| 2734 | return bytes_written; |
| 2735 | } |
| 2736 | |
| 2737 | size_t |
| 2738 | Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 2739 | { |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2740 | #if defined (ENABLE_MEMORY_CACHING) |
| 2741 | m_memory_cache.Flush (addr, size); |
| 2742 | #endif |
| 2743 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2744 | if (buf == NULL || size == 0) |
| 2745 | return 0; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2746 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2747 | m_mod_id.BumpMemoryID(); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2748 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2749 | // We need to write any data that would go where any current software traps |
| 2750 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 2751 | // may have placed in our tasks memory. |
| 2752 | |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2753 | BreakpointSiteList bp_sites_in_range; |
| 2754 | |
| 2755 | 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] | 2756 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2757 | // No breakpoint sites overlap |
| 2758 | if (bp_sites_in_range.IsEmpty()) |
| 2759 | return WriteMemoryPrivate (addr, buf, size, error); |
| 2760 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2761 | { |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2762 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 2763 | uint64_t bytes_written = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2764 | |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2765 | bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, &error](BreakpointSite *bp) -> void { |
| 2766 | |
| 2767 | if (error.Success()) |
| 2768 | { |
| 2769 | addr_t intersect_addr; |
| 2770 | size_t intersect_size; |
| 2771 | size_t opcode_offset; |
| 2772 | const bool intersects = bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset); |
| 2773 | assert(intersects); |
| 2774 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 2775 | assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size); |
| 2776 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 2777 | |
| 2778 | // Check for bytes before this breakpoint |
| 2779 | const addr_t curr_addr = addr + bytes_written; |
| 2780 | if (intersect_addr > curr_addr) |
| 2781 | { |
| 2782 | // There are some bytes before this breakpoint that we need to |
| 2783 | // just write to memory |
| 2784 | size_t curr_size = intersect_addr - curr_addr; |
| 2785 | size_t curr_bytes_written = WriteMemoryPrivate (curr_addr, |
| 2786 | ubuf + bytes_written, |
| 2787 | curr_size, |
| 2788 | error); |
| 2789 | bytes_written += curr_bytes_written; |
| 2790 | if (curr_bytes_written != curr_size) |
| 2791 | { |
| 2792 | // We weren't able to write all of the requested bytes, we |
| 2793 | // are done looping and will return the number of bytes that |
| 2794 | // we have written so far. |
| 2795 | if (error.Success()) |
| 2796 | error.SetErrorToGenericError(); |
| 2797 | } |
| 2798 | } |
| 2799 | // Now write any bytes that would cover up any software breakpoints |
| 2800 | // directly into the breakpoint opcode buffer |
| 2801 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size); |
| 2802 | bytes_written += intersect_size; |
| 2803 | } |
| 2804 | }); |
| 2805 | |
| 2806 | if (bytes_written < size) |
Jason Molenda | 8b5f2cf | 2014-10-16 07:49:27 +0000 | [diff] [blame] | 2807 | WriteMemoryPrivate (addr + bytes_written, |
| 2808 | ubuf + bytes_written, |
| 2809 | size - bytes_written, |
| 2810 | error); |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2811 | } |
| 2812 | } |
| 2813 | else |
| 2814 | { |
| 2815 | return WriteMemoryPrivate (addr, buf, size, error); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | // 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] | 2819 | return 0; //bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2820 | } |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2821 | |
| 2822 | size_t |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2823 | 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] | 2824 | { |
| 2825 | if (byte_size == UINT32_MAX) |
| 2826 | byte_size = scalar.GetByteSize(); |
| 2827 | if (byte_size > 0) |
| 2828 | { |
| 2829 | uint8_t buf[32]; |
| 2830 | const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error); |
| 2831 | if (mem_size > 0) |
| 2832 | return WriteMemory(addr, buf, mem_size, error); |
| 2833 | else |
| 2834 | error.SetErrorString ("failed to get scalar as memory data"); |
| 2835 | } |
| 2836 | else |
| 2837 | { |
| 2838 | error.SetErrorString ("invalid scalar value"); |
| 2839 | } |
| 2840 | return 0; |
| 2841 | } |
| 2842 | |
| 2843 | size_t |
| 2844 | Process::ReadScalarIntegerFromMemory (addr_t addr, |
| 2845 | uint32_t byte_size, |
| 2846 | bool is_signed, |
| 2847 | Scalar &scalar, |
| 2848 | Error &error) |
| 2849 | { |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2850 | uint64_t uval = 0; |
| 2851 | if (byte_size == 0) |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2852 | { |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2853 | error.SetErrorString ("byte size is zero"); |
| 2854 | } |
| 2855 | else if (byte_size & (byte_size - 1)) |
| 2856 | { |
| 2857 | error.SetErrorStringWithFormat ("byte size %u is not a power of 2", byte_size); |
| 2858 | } |
| 2859 | else if (byte_size <= sizeof(uval)) |
| 2860 | { |
| 2861 | const size_t bytes_read = ReadMemory (addr, &uval, byte_size, error); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2862 | if (bytes_read == byte_size) |
| 2863 | { |
| 2864 | DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize()); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2865 | lldb::offset_t offset = 0; |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2866 | if (byte_size <= 4) |
| 2867 | scalar = data.GetMaxU32 (&offset, byte_size); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2868 | else |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2869 | scalar = data.GetMaxU64 (&offset, byte_size); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2870 | if (is_signed) |
| 2871 | scalar.SignExtend(byte_size * 8); |
| 2872 | return bytes_read; |
| 2873 | } |
| 2874 | } |
| 2875 | else |
| 2876 | { |
| 2877 | error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size); |
| 2878 | } |
| 2879 | return 0; |
| 2880 | } |
| 2881 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2882 | #define USE_ALLOCATE_MEMORY_CACHE 1 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2883 | addr_t |
| 2884 | Process::AllocateMemory(size_t size, uint32_t permissions, Error &error) |
| 2885 | { |
Jim Ingham | f72ce3a | 2011-06-20 17:32:44 +0000 | [diff] [blame] | 2886 | if (GetPrivateState() != eStateStopped) |
| 2887 | return LLDB_INVALID_ADDRESS; |
| 2888 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2889 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2890 | return m_allocated_memory_cache.AllocateMemory(size, permissions, error); |
| 2891 | #else |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2892 | addr_t allocated_addr = DoAllocateMemory (size, permissions, error); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2893 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2894 | if (log) |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 2895 | 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] | 2896 | (uint64_t)size, |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2897 | GetPermissionsAsCString (permissions), |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2898 | (uint64_t)allocated_addr, |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2899 | m_mod_id.GetStopID(), |
| 2900 | m_mod_id.GetMemoryID()); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2901 | return allocated_addr; |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2902 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2905 | bool |
| 2906 | Process::CanJIT () |
| 2907 | { |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2908 | if (m_can_jit == eCanJITDontKnow) |
| 2909 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2910 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2911 | Error err; |
| 2912 | |
| 2913 | uint64_t allocated_memory = AllocateMemory(8, |
| 2914 | ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable, |
| 2915 | err); |
| 2916 | |
| 2917 | if (err.Success()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2918 | { |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2919 | m_can_jit = eCanJITYes; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2920 | if (log) |
| 2921 | log->Printf ("Process::%s pid %" PRIu64 " allocation test passed, CanJIT () is true", __FUNCTION__, GetID ()); |
| 2922 | } |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2923 | else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2924 | { |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2925 | m_can_jit = eCanJITNo; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2926 | if (log) |
| 2927 | log->Printf ("Process::%s pid %" PRIu64 " allocation test failed, CanJIT () is false: %s", __FUNCTION__, GetID (), err.AsCString ()); |
| 2928 | } |
Sean Callanan | a7b443a | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2929 | |
| 2930 | DeallocateMemory (allocated_memory); |
| 2931 | } |
| 2932 | |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2933 | return m_can_jit == eCanJITYes; |
| 2934 | } |
| 2935 | |
| 2936 | void |
| 2937 | Process::SetCanJIT (bool can_jit) |
| 2938 | { |
| 2939 | m_can_jit = (can_jit ? eCanJITYes : eCanJITNo); |
| 2940 | } |
| 2941 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2942 | Error |
| 2943 | Process::DeallocateMemory (addr_t ptr) |
| 2944 | { |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2945 | Error error; |
| 2946 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2947 | if (!m_allocated_memory_cache.DeallocateMemory(ptr)) |
| 2948 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2949 | error.SetErrorStringWithFormat ("deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2950 | } |
| 2951 | #else |
| 2952 | error = DoDeallocateMemory (ptr); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2953 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2954 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2955 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2956 | 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] | 2957 | ptr, |
| 2958 | error.AsCString("SUCCESS"), |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2959 | m_mod_id.GetStopID(), |
| 2960 | m_mod_id.GetMemoryID()); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2961 | #endif |
Greg Clayton | b2daec9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2962 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Han Ming Ong | c811d38 | 2012-11-17 00:33:14 +0000 | [diff] [blame] | 2965 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2966 | ModuleSP |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2967 | Process::ReadModuleFromMemory (const FileSpec& file_spec, |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2968 | lldb::addr_t header_addr, |
| 2969 | size_t size_to_read) |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2970 | { |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2971 | ModuleSP module_sp (new Module (file_spec, ArchSpec())); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2972 | if (module_sp) |
| 2973 | { |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2974 | Error error; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2975 | 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] | 2976 | if (objfile) |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2977 | return module_sp; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2978 | } |
Greg Clayton | c7f09cc | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2979 | return ModuleSP(); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2980 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2981 | |
| 2982 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2983 | Process::EnableWatchpoint (Watchpoint *watchpoint, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2984 | { |
| 2985 | Error error; |
| 2986 | error.SetErrorString("watchpoints are not supported"); |
| 2987 | return error; |
| 2988 | } |
| 2989 | |
| 2990 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2991 | Process::DisableWatchpoint (Watchpoint *watchpoint, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2992 | { |
| 2993 | Error error; |
| 2994 | error.SetErrorString("watchpoints are not supported"); |
| 2995 | return error; |
| 2996 | } |
| 2997 | |
| 2998 | StateType |
| 2999 | Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 3000 | { |
| 3001 | StateType state; |
| 3002 | // Now wait for the process to launch and return control to us, and then |
| 3003 | // call DidLaunch: |
| 3004 | while (1) |
| 3005 | { |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 3006 | event_sp.reset(); |
| 3007 | state = WaitForStateChangedEventsPrivate (timeout, event_sp); |
| 3008 | |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 3009 | if (StateIsStoppedState(state, false)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3010 | break; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 3011 | |
| 3012 | // If state is invalid, then we timed out |
| 3013 | if (state == eStateInvalid) |
| 3014 | break; |
| 3015 | |
| 3016 | if (event_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3017 | HandlePrivateEvent (event_sp); |
| 3018 | } |
| 3019 | return state; |
| 3020 | } |
| 3021 | |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 3022 | void |
| 3023 | Process::LoadOperatingSystemPlugin(bool flush) |
| 3024 | { |
| 3025 | if (flush) |
| 3026 | m_thread_list.Clear(); |
| 3027 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
| 3028 | if (flush) |
| 3029 | Flush(); |
| 3030 | } |
| 3031 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3032 | Error |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 3033 | Process::Launch (ProcessLaunchInfo &launch_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3034 | { |
| 3035 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3036 | m_abi_sp.reset(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3037 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3038 | m_jit_loaders_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3039 | m_system_runtime_ap.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3040 | m_os_ap.reset(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3041 | m_process_input_reader.reset(); |
Greg Clayton | a97c4d2 | 2014-12-09 23:31:02 +0000 | [diff] [blame] | 3042 | m_stop_info_override_callback = NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3043 | |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 3044 | Module *exe_module = m_target.GetExecutableModulePointer(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3045 | if (exe_module) |
| 3046 | { |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 3047 | char local_exec_file_path[PATH_MAX]; |
| 3048 | char platform_exec_file_path[PATH_MAX]; |
| 3049 | exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path)); |
| 3050 | 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] | 3051 | if (exe_module->GetFileSpec().Exists()) |
| 3052 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 3053 | // Install anything that might need to be installed prior to launching. |
| 3054 | // For host systems, this will do nothing, but if we are connected to a |
| 3055 | // remote platform it will install any needed binaries |
| 3056 | error = GetTarget().Install(&launch_info); |
| 3057 | if (error.Fail()) |
| 3058 | return error; |
| 3059 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3060 | if (PrivateStateThreadIsValid ()) |
| 3061 | PausePrivateStateThread (); |
| 3062 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3063 | error = WillLaunch (exe_module); |
| 3064 | if (error.Success()) |
| 3065 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3066 | const bool restarted = false; |
| 3067 | SetPublicState (eStateLaunching, restarted); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 3068 | m_should_detach = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3069 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3070 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 69fd4be | 2012-09-04 20:29:05 +0000 | [diff] [blame] | 3071 | { |
| 3072 | // Now launch using these arguments. |
| 3073 | error = DoLaunch (exe_module, launch_info); |
| 3074 | } |
| 3075 | else |
| 3076 | { |
| 3077 | // This shouldn't happen |
| 3078 | error.SetErrorString("failed to acquire process run lock"); |
| 3079 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3080 | |
| 3081 | if (error.Fail()) |
| 3082 | { |
| 3083 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3084 | { |
| 3085 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3086 | const char *error_string = error.AsCString(); |
| 3087 | if (error_string == NULL) |
| 3088 | error_string = "launch failed"; |
| 3089 | SetExitStatus (-1, error_string); |
| 3090 | } |
| 3091 | } |
| 3092 | else |
| 3093 | { |
| 3094 | EventSP event_sp; |
Greg Clayton | 1a38ea7 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 3095 | TimeValue timeout_time; |
| 3096 | timeout_time = TimeValue::Now(); |
| 3097 | timeout_time.OffsetWithSeconds(10); |
| 3098 | StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3099 | |
Greg Clayton | 1a38ea7 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 3100 | if (state == eStateInvalid || event_sp.get() == NULL) |
| 3101 | { |
| 3102 | // We were able to launch the process, but we failed to |
| 3103 | // catch the initial stop. |
Tamas Berghammer | 04e6314 | 2015-02-23 10:59:54 +0000 | [diff] [blame] | 3104 | error.SetErrorString ("failed to catch stop after launch"); |
Greg Clayton | 1a38ea7 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 3105 | SetExitStatus (0, "failed to catch stop after launch"); |
| 3106 | Destroy(); |
| 3107 | } |
| 3108 | else if (state == eStateStopped || state == eStateCrashed) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3109 | { |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3111 | DidLaunch (); |
| 3112 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3113 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3114 | if (dyld) |
| 3115 | dyld->DidLaunch(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3116 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 3117 | GetJITLoaders().DidLaunch(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3118 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3119 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3120 | if (system_runtime) |
| 3121 | system_runtime->DidLaunch(); |
| 3122 | |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 3123 | LoadOperatingSystemPlugin(false); |
Todd Fiala | f72fa67 | 2014-10-07 16:05:21 +0000 | [diff] [blame] | 3124 | |
| 3125 | // Note, the stop event was consumed above, but not handled. This was done |
| 3126 | // to give DidLaunch a chance to run. The target is either stopped or crashed. |
| 3127 | // Directly set the state. This is done to prevent a stop message with a bunch |
| 3128 | // of spurious output on thread status, as well as not pop a ProcessIOHandler. |
| 3129 | SetPublicState(state, false); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3130 | |
| 3131 | if (PrivateStateThreadIsValid ()) |
| 3132 | ResumePrivateStateThread (); |
| 3133 | else |
| 3134 | StartPrivateStateThread (); |
Greg Clayton | a97c4d2 | 2014-12-09 23:31:02 +0000 | [diff] [blame] | 3135 | |
| 3136 | m_stop_info_override_callback = GetTarget().GetArchitecture().GetStopInfoOverrideCallback(); |
Ilia K | 6af632f | 2015-02-06 18:15:05 +0000 | [diff] [blame] | 3137 | |
| 3138 | // Target was stopped at entry as was intended. Need to notify the listeners |
| 3139 | // about it. |
Ilia K | 333fc18 | 2015-02-11 11:24:20 +0000 | [diff] [blame] | 3140 | if (state == eStateStopped && launch_info.GetFlags().Test(eLaunchFlagStopAtEntry)) |
Ilia K | 6af632f | 2015-02-06 18:15:05 +0000 | [diff] [blame] | 3141 | HandlePrivateEvent(event_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3142 | } |
| 3143 | else if (state == eStateExited) |
| 3144 | { |
| 3145 | // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 3146 | // not likely to work, and return an invalid pid. |
| 3147 | HandlePrivateEvent (event_sp); |
| 3148 | } |
| 3149 | } |
| 3150 | } |
| 3151 | } |
| 3152 | else |
| 3153 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 3154 | error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3155 | } |
| 3156 | } |
| 3157 | return error; |
| 3158 | } |
| 3159 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3160 | |
| 3161 | Error |
| 3162 | Process::LoadCore () |
| 3163 | { |
| 3164 | Error error = DoLoadCore(); |
| 3165 | if (error.Success()) |
| 3166 | { |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 3167 | Listener listener ("lldb.process.load_core_listener"); |
Greg Clayton | 338d0bd | 2015-02-20 21:51:06 +0000 | [diff] [blame] | 3168 | HijackProcessEvents(&listener); |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 3169 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3170 | if (PrivateStateThreadIsValid ()) |
| 3171 | ResumePrivateStateThread (); |
| 3172 | else |
| 3173 | StartPrivateStateThread (); |
| 3174 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3175 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3176 | if (dyld) |
| 3177 | dyld->DidAttach(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3178 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 3179 | GetJITLoaders().DidAttach(); |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3180 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3181 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3182 | if (system_runtime) |
| 3183 | system_runtime->DidAttach(); |
| 3184 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3185 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3186 | // We successfully loaded a core file, now pretend we stopped so we can |
| 3187 | // show all of the threads in the core file and explore the crashed |
| 3188 | // state. |
| 3189 | SetPrivateState (eStateStopped); |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 3190 | |
| 3191 | // Wait indefinitely for a stopped event since we just posted one above... |
| 3192 | lldb::EventSP event_sp; |
| 3193 | listener.WaitForEvent (NULL, event_sp); |
| 3194 | StateType state = ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 3195 | |
| 3196 | if (!StateIsStoppedState (state, false)) |
| 3197 | { |
| 3198 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3199 | if (log) |
| 3200 | log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state)); |
| 3201 | error.SetErrorString ("Did not get stopped event after loading the core file."); |
| 3202 | } |
Greg Clayton | 338d0bd | 2015-02-20 21:51:06 +0000 | [diff] [blame] | 3203 | RestoreProcessEvents (); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3204 | } |
| 3205 | return error; |
| 3206 | } |
| 3207 | |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3208 | DynamicLoader * |
| 3209 | Process::GetDynamicLoader () |
| 3210 | { |
| 3211 | if (m_dyld_ap.get() == NULL) |
| 3212 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL)); |
| 3213 | return m_dyld_ap.get(); |
| 3214 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3215 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3216 | const lldb::DataBufferSP |
| 3217 | Process::GetAuxvData() |
| 3218 | { |
| 3219 | return DataBufferSP (); |
| 3220 | } |
| 3221 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3222 | JITLoaderList & |
| 3223 | Process::GetJITLoaders () |
| 3224 | { |
| 3225 | if (!m_jit_loaders_ap) |
| 3226 | { |
| 3227 | m_jit_loaders_ap.reset(new JITLoaderList()); |
| 3228 | JITLoader::LoadPlugins(this, *m_jit_loaders_ap); |
| 3229 | } |
| 3230 | return *m_jit_loaders_ap; |
| 3231 | } |
| 3232 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3233 | SystemRuntime * |
| 3234 | Process::GetSystemRuntime () |
| 3235 | { |
| 3236 | if (m_system_runtime_ap.get() == NULL) |
| 3237 | m_system_runtime_ap.reset (SystemRuntime::FindPlugin(this)); |
| 3238 | return m_system_runtime_ap.get(); |
| 3239 | } |
| 3240 | |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3241 | Process::AttachCompletionHandler::AttachCompletionHandler (Process *process, uint32_t exec_count) : |
| 3242 | NextEventAction (process), |
| 3243 | m_exec_count (exec_count) |
| 3244 | { |
| 3245 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3246 | if (log) |
| 3247 | log->Printf ("Process::AttachCompletionHandler::%s process=%p, exec_count=%" PRIu32, __FUNCTION__, static_cast<void*>(process), exec_count); |
| 3248 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3249 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3250 | Process::NextEventAction::EventActionResult |
| 3251 | Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3252 | { |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3253 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3254 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3255 | StateType state = ProcessEventData::GetStateFromEvent (event_sp.get()); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3256 | if (log) |
| 3257 | log->Printf ("Process::AttachCompletionHandler::%s called with state %s (%d)", __FUNCTION__, StateAsCString(state), static_cast<int> (state)); |
| 3258 | |
| 3259 | switch (state) |
Greg Clayton | 19388cf | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 3260 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3261 | case eStateRunning: |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3262 | case eStateConnected: |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3263 | return eEventActionRetry; |
| 3264 | |
| 3265 | case eStateStopped: |
| 3266 | case eStateCrashed: |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3267 | { |
| 3268 | // During attach, prior to sending the eStateStopped event, |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3269 | // lldb_private::Process subclasses must set the new process ID. |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3270 | assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3271 | // We don't want these events to be reported, so go set the ShouldReportStop here: |
| 3272 | m_process->GetThreadList().SetShouldReportStop (eVoteNo); |
| 3273 | |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3274 | if (m_exec_count > 0) |
| 3275 | { |
| 3276 | --m_exec_count; |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3277 | |
| 3278 | if (log) |
| 3279 | log->Printf ("Process::AttachCompletionHandler::%s state %s: reduced remaining exec count to %" PRIu32 ", requesting resume", __FUNCTION__, StateAsCString(state), m_exec_count); |
| 3280 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3281 | RequestResume(); |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3282 | return eEventActionRetry; |
| 3283 | } |
| 3284 | else |
| 3285 | { |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3286 | if (log) |
| 3287 | log->Printf ("Process::AttachCompletionHandler::%s state %s: no more execs expected to start, continuing with attach", __FUNCTION__, StateAsCString(state)); |
| 3288 | |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3289 | m_process->CompleteAttach (); |
| 3290 | return eEventActionSuccess; |
| 3291 | } |
| 3292 | } |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3293 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3294 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3295 | default: |
| 3296 | case eStateExited: |
| 3297 | case eStateInvalid: |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3298 | break; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3299 | } |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3300 | |
| 3301 | m_exit_string.assign ("No valid Process"); |
| 3302 | return eEventActionExit; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3303 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3304 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3305 | Process::NextEventAction::EventActionResult |
| 3306 | Process::AttachCompletionHandler::HandleBeingInterrupted() |
| 3307 | { |
| 3308 | return eEventActionSuccess; |
| 3309 | } |
| 3310 | |
| 3311 | const char * |
| 3312 | Process::AttachCompletionHandler::GetExitString () |
| 3313 | { |
| 3314 | return m_exit_string.c_str(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 3317 | Listener & |
| 3318 | ProcessAttachInfo::GetListenerForProcess (Debugger &debugger) |
| 3319 | { |
| 3320 | if (m_listener_sp) |
| 3321 | return *m_listener_sp; |
| 3322 | else |
| 3323 | return debugger.GetListener(); |
| 3324 | } |
| 3325 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3326 | Error |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3327 | Process::Attach (ProcessAttachInfo &attach_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3328 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3329 | m_abi_sp.reset(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3330 | m_process_input_reader.reset(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3331 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3332 | m_jit_loaders_ap.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3333 | m_system_runtime_ap.reset(); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3334 | m_os_ap.reset(); |
Greg Clayton | a97c4d2 | 2014-12-09 23:31:02 +0000 | [diff] [blame] | 3335 | m_stop_info_override_callback = NULL; |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 3336 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3337 | lldb::pid_t attach_pid = attach_info.GetProcessID(); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3338 | Error error; |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3339 | if (attach_pid == LLDB_INVALID_PROCESS_ID) |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 3340 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3341 | char process_name[PATH_MAX]; |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3342 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3343 | if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name))) |
Jim Ingham | 2ecb742 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 3344 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3345 | const bool wait_for_launch = attach_info.GetWaitForLaunch(); |
| 3346 | |
| 3347 | if (wait_for_launch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3348 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3349 | error = WillAttachToProcessWithName(process_name, wait_for_launch); |
| 3350 | if (error.Success()) |
| 3351 | { |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3352 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3353 | { |
| 3354 | m_should_detach = true; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3355 | const bool restarted = false; |
| 3356 | SetPublicState (eStateAttaching, restarted); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3357 | // Now attach using these arguments. |
Jean-Daniel Dupas | 9c517c0 | 2013-12-23 22:32:54 +0000 | [diff] [blame] | 3358 | error = DoAttachToProcessWithName (process_name, attach_info); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3359 | } |
| 3360 | else |
| 3361 | { |
| 3362 | // This shouldn't happen |
| 3363 | error.SetErrorString("failed to acquire process run lock"); |
| 3364 | } |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 3365 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3366 | if (error.Fail()) |
| 3367 | { |
| 3368 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3369 | { |
| 3370 | SetID (LLDB_INVALID_PROCESS_ID); |
| 3371 | if (error.AsCString() == NULL) |
| 3372 | error.SetErrorString("attach failed"); |
| 3373 | |
| 3374 | SetExitStatus(-1, error.AsCString()); |
| 3375 | } |
| 3376 | } |
| 3377 | else |
| 3378 | { |
| 3379 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 3380 | StartPrivateStateThread(); |
| 3381 | } |
| 3382 | return error; |
| 3383 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3384 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3385 | else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3386 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3387 | ProcessInstanceInfoList process_infos; |
| 3388 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 3389 | |
| 3390 | if (platform_sp) |
| 3391 | { |
| 3392 | ProcessInstanceInfoMatch match_info; |
| 3393 | match_info.GetProcessInfo() = attach_info; |
| 3394 | match_info.SetNameMatchType (eNameMatchEquals); |
| 3395 | platform_sp->FindProcesses (match_info, process_infos); |
| 3396 | const uint32_t num_matches = process_infos.GetSize(); |
| 3397 | if (num_matches == 1) |
| 3398 | { |
| 3399 | attach_pid = process_infos.GetProcessIDAtIndex(0); |
| 3400 | // Fall through and attach using the above process ID |
| 3401 | } |
| 3402 | else |
| 3403 | { |
| 3404 | match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name)); |
| 3405 | if (num_matches > 1) |
Jim Ingham | 368ac22 | 2014-08-15 17:05:27 +0000 | [diff] [blame] | 3406 | { |
| 3407 | StreamString s; |
| 3408 | ProcessInstanceInfo::DumpTableHeader (s, platform_sp.get(), true, false); |
| 3409 | for (size_t i = 0; i < num_matches; i++) |
| 3410 | { |
| 3411 | process_infos.GetProcessInfoAtIndex(i).DumpAsTableRow(s, platform_sp.get(), true, false); |
| 3412 | } |
| 3413 | error.SetErrorStringWithFormat ("more than one process named %s:\n%s", |
| 3414 | process_name, |
| 3415 | s.GetData()); |
| 3416 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3417 | else |
| 3418 | error.SetErrorStringWithFormat ("could not find a process named %s", process_name); |
| 3419 | } |
| 3420 | } |
| 3421 | else |
| 3422 | { |
| 3423 | error.SetErrorString ("invalid platform, can't find processes by name"); |
| 3424 | return error; |
| 3425 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3426 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3427 | } |
| 3428 | else |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3429 | { |
| 3430 | error.SetErrorString ("invalid process name"); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3431 | } |
| 3432 | } |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3433 | |
| 3434 | if (attach_pid != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3435 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3436 | error = WillAttachToProcessWithID(attach_pid); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3437 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3438 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3439 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3440 | if (m_public_run_lock.TrySetRunning()) |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3441 | { |
| 3442 | // Now attach using these arguments. |
| 3443 | m_should_detach = true; |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3444 | const bool restarted = false; |
| 3445 | SetPublicState (eStateAttaching, restarted); |
Greg Clayton | 926cce7 | 2012-10-12 16:10:12 +0000 | [diff] [blame] | 3446 | error = DoAttachToProcessWithID (attach_pid, attach_info); |
| 3447 | } |
| 3448 | else |
| 3449 | { |
| 3450 | // This shouldn't happen |
| 3451 | error.SetErrorString("failed to acquire process run lock"); |
| 3452 | } |
| 3453 | |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 3454 | if (error.Success()) |
| 3455 | { |
| 3456 | |
| 3457 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 3458 | StartPrivateStateThread(); |
| 3459 | } |
| 3460 | else |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3461 | { |
| 3462 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3463 | SetID (LLDB_INVALID_PROCESS_ID); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3464 | |
Oleksiy Vyalov | 5d06474 | 2014-11-19 18:27:45 +0000 | [diff] [blame] | 3465 | const char *error_string = error.AsCString(); |
| 3466 | if (error_string == NULL) |
| 3467 | error_string = "attach failed"; |
| 3468 | |
| 3469 | SetExitStatus(-1, error_string); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3470 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3471 | } |
| 3472 | } |
| 3473 | return error; |
| 3474 | } |
| 3475 | |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3476 | void |
| 3477 | Process::CompleteAttach () |
| 3478 | { |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3479 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3480 | if (log) |
| 3481 | log->Printf ("Process::%s()", __FUNCTION__); |
| 3482 | |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3483 | // Let the process subclass figure out at much as it can about the process |
| 3484 | // before we go looking for a dynamic loader plug-in. |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 3485 | ArchSpec process_arch; |
| 3486 | DidAttach(process_arch); |
| 3487 | |
| 3488 | if (process_arch.IsValid()) |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3489 | { |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 3490 | m_target.SetArchitecture(process_arch); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3491 | if (log) |
| 3492 | { |
| 3493 | const char *triple_str = process_arch.GetTriple().getTriple().c_str (); |
| 3494 | log->Printf ("Process::%s replacing process architecture with DidAttach() architecture: %s", |
| 3495 | __FUNCTION__, |
| 3496 | triple_str ? triple_str : "<null>"); |
| 3497 | } |
| 3498 | } |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3499 | |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3500 | // We just attached. If we have a platform, ask it for the process architecture, and if it isn't |
| 3501 | // the same as the one we've already set, switch architectures. |
| 3502 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 3503 | assert (platform_sp.get()); |
| 3504 | if (platform_sp) |
| 3505 | { |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3506 | const ArchSpec &target_arch = m_target.GetArchitecture(); |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 3507 | if (target_arch.IsValid() && !platform_sp->IsCompatibleArchitecture (target_arch, false, NULL)) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3508 | { |
| 3509 | ArchSpec platform_arch; |
| 3510 | platform_sp = platform_sp->GetPlatformForArchitecture (target_arch, &platform_arch); |
| 3511 | if (platform_sp) |
| 3512 | { |
| 3513 | m_target.SetPlatform (platform_sp); |
| 3514 | m_target.SetArchitecture(platform_arch); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3515 | if (log) |
| 3516 | 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] | 3517 | } |
| 3518 | } |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 3519 | else if (!process_arch.IsValid()) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3520 | { |
| 3521 | ProcessInstanceInfo process_info; |
| 3522 | platform_sp->GetProcessInfo (GetID(), process_info); |
| 3523 | const ArchSpec &process_arch = process_info.GetArchitecture(); |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 3524 | if (process_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(process_arch)) |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3525 | { |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3526 | m_target.SetArchitecture (process_arch); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3527 | if (log) |
| 3528 | 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 ()); |
| 3529 | } |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 3530 | } |
Jim Ingham | 4299fdb | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | // 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] | 3534 | // plug-in |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3535 | DynamicLoader *dyld = GetDynamicLoader (); |
| 3536 | if (dyld) |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3537 | { |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3538 | dyld->DidAttach(); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3539 | if (log) |
| 3540 | { |
| 3541 | ModuleSP exe_module_sp = m_target.GetExecutableModule (); |
| 3542 | log->Printf ("Process::%s after DynamicLoader::DidAttach(), target executable is %s (using %s plugin)", |
| 3543 | __FUNCTION__, |
| 3544 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>", |
| 3545 | dyld->GetPluginName().AsCString ("<unnamed>")); |
| 3546 | } |
| 3547 | } |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3548 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 3549 | GetJITLoaders().DidAttach(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3550 | |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3551 | SystemRuntime *system_runtime = GetSystemRuntime (); |
| 3552 | if (system_runtime) |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3553 | { |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3554 | system_runtime->DidAttach(); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3555 | if (log) |
| 3556 | { |
| 3557 | ModuleSP exe_module_sp = m_target.GetExecutableModule (); |
| 3558 | log->Printf ("Process::%s after SystemRuntime::DidAttach(), target executable is %s (using %s plugin)", |
| 3559 | __FUNCTION__, |
| 3560 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>", |
| 3561 | system_runtime->GetPluginName().AsCString("<unnamed>")); |
| 3562 | } |
| 3563 | } |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 3564 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 3565 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3566 | // 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] | 3567 | const ModuleList &target_modules = m_target.GetImages(); |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3568 | Mutex::Locker modules_locker(target_modules.GetMutex()); |
| 3569 | size_t num_modules = target_modules.GetSize(); |
| 3570 | ModuleSP new_executable_module_sp; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3571 | |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 3572 | for (size_t i = 0; i < num_modules; i++) |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3573 | { |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3574 | ModuleSP module_sp (target_modules.GetModuleAtIndexUnlocked (i)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3575 | if (module_sp && module_sp->IsExecutable()) |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3576 | { |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 3577 | if (m_target.GetExecutableModulePointer() != module_sp.get()) |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3578 | new_executable_module_sp = module_sp; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3579 | break; |
| 3580 | } |
| 3581 | } |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3582 | if (new_executable_module_sp) |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3583 | { |
Jim Ingham | 3ee12ef | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 3584 | m_target.SetExecutableModule (new_executable_module_sp, false); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 3585 | if (log) |
| 3586 | { |
| 3587 | ModuleSP exe_module_sp = m_target.GetExecutableModule (); |
| 3588 | log->Printf ("Process::%s after looping through modules, target executable is %s", |
| 3589 | __FUNCTION__, |
| 3590 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str () : "<none>"); |
| 3591 | } |
| 3592 | } |
Greg Clayton | a97c4d2 | 2014-12-09 23:31:02 +0000 | [diff] [blame] | 3593 | |
| 3594 | m_stop_info_override_callback = process_arch.GetStopInfoOverrideCallback(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3597 | Error |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 3598 | Process::ConnectRemote (Stream *strm, const char *remote_url) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3599 | { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3600 | m_abi_sp.reset(); |
| 3601 | m_process_input_reader.reset(); |
| 3602 | |
| 3603 | // Find the process and its architecture. Make sure it matches the architecture |
| 3604 | // of the current Target, and if not adjust it. |
| 3605 | |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 3606 | Error error (DoConnectRemote (strm, remote_url)); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3607 | if (error.Success()) |
| 3608 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3609 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 3610 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 3611 | EventSP event_sp; |
| 3612 | StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 3613 | |
| 3614 | if (state == eStateStopped || state == eStateCrashed) |
| 3615 | { |
| 3616 | // If we attached and actually have a process on the other end, then |
| 3617 | // this ended up being the equivalent of an attach. |
| 3618 | CompleteAttach (); |
| 3619 | |
| 3620 | // This delays passing the stopped event to listeners till |
| 3621 | // CompleteAttach gets a chance to complete... |
| 3622 | HandlePrivateEvent (event_sp); |
| 3623 | |
| 3624 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3625 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 3626 | |
| 3627 | if (PrivateStateThreadIsValid ()) |
| 3628 | ResumePrivateStateThread (); |
| 3629 | else |
| 3630 | StartPrivateStateThread (); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3631 | } |
| 3632 | return error; |
| 3633 | } |
| 3634 | |
| 3635 | |
| 3636 | Error |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 3637 | Process::PrivateResume () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3638 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3639 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_STEP)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3640 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3641 | 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] | 3642 | m_mod_id.GetStopID(), |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3643 | StateAsCString(m_public_state.GetValue()), |
| 3644 | StateAsCString(m_private_state.GetValue())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3645 | |
| 3646 | Error error (WillResume()); |
| 3647 | // Tell the process it is about to resume before the thread list |
| 3648 | if (error.Success()) |
| 3649 | { |
Johnny Chen | c4221e4 | 2010-12-02 20:53:05 +0000 | [diff] [blame] | 3650 | // 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] | 3651 | // can let all of our threads know that they are about to be |
| 3652 | // resumed. Threads will each be called with |
| 3653 | // Thread::WillResume(StateType) where StateType contains the state |
| 3654 | // that they are supposed to have when the process is resumed |
| 3655 | // (suspended/running/stepping). Threads should also check |
| 3656 | // their resume signal in lldb::Thread::GetResumeSignal() |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 3657 | // 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] | 3658 | if (m_thread_list.WillResume()) |
| 3659 | { |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3660 | // Last thing, do the PreResumeActions. |
| 3661 | if (!RunPreResumeActions()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3662 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3663 | error.SetErrorStringWithFormat ("Process::PrivateResume PreResumeActions failed, not resuming."); |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3664 | } |
| 3665 | else |
| 3666 | { |
| 3667 | m_mod_id.BumpResumeID(); |
| 3668 | error = DoResume(); |
| 3669 | if (error.Success()) |
| 3670 | { |
| 3671 | DidResume(); |
| 3672 | m_thread_list.DidResume(); |
| 3673 | if (log) |
| 3674 | log->Printf ("Process thinks the process has resumed."); |
| 3675 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3676 | } |
| 3677 | } |
| 3678 | else |
| 3679 | { |
Jim Ingham | d5ac1ab | 2015-01-19 23:51:51 +0000 | [diff] [blame] | 3680 | // Somebody wanted to run without running (e.g. we were faking a step from one frame of a set of inlined |
| 3681 | // frames that share the same PC to another.) So generate a continue & a stopped event, |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 3682 | // and let the world handle them. |
| 3683 | if (log) |
| 3684 | log->Printf ("Process::PrivateResume() asked to simulate a start & stop."); |
| 3685 | |
| 3686 | SetPrivateState(eStateRunning); |
| 3687 | SetPrivateState(eStateStopped); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3688 | } |
| 3689 | } |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3690 | else if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 3691 | log->Printf ("Process::PrivateResume() got an error \"%s\".", error.AsCString("<unknown error>")); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3692 | return error; |
| 3693 | } |
| 3694 | |
| 3695 | Error |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 3696 | Process::Halt (bool clear_thread_plans) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3697 | { |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 3698 | // Don't clear the m_clear_thread_plans_on_stop, only set it to true if |
| 3699 | // in case it was already set and some thread plan logic calls halt on its |
| 3700 | // own. |
| 3701 | m_clear_thread_plans_on_stop |= clear_thread_plans; |
| 3702 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3703 | // First make sure we aren't in the middle of handling an event, or we might restart. This is pretty weak, since |
| 3704 | // we could just straightaway get another event. It just narrows the window... |
| 3705 | m_currently_handling_event.WaitForValueEqualTo(false); |
| 3706 | |
| 3707 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3708 | // Pause our private state thread so we can ensure no one else eats |
| 3709 | // the stop event out from under us. |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3710 | Listener halt_listener ("lldb.process.halt_listener"); |
| 3711 | HijackPrivateProcessEvents(&halt_listener); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3712 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3713 | EventSP event_sp; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3714 | Error error (WillHalt()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3715 | |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 3716 | bool restored_process_events = false; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3717 | if (error.Success()) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3718 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3719 | |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3720 | bool caused_stop = false; |
| 3721 | |
| 3722 | // Ask the process subclass to actually halt our process |
| 3723 | error = DoHalt(caused_stop); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3724 | if (error.Success()) |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3725 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3726 | if (m_public_state.GetValue() == eStateAttaching) |
| 3727 | { |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 3728 | // Don't hijack and eat the eStateExited as the code that was doing |
| 3729 | // the attach will be waiting for this event... |
| 3730 | RestorePrivateProcessEvents(); |
| 3731 | restored_process_events = true; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 3732 | SetExitStatus(SIGKILL, "Cancelled async attach."); |
| 3733 | Destroy (); |
| 3734 | } |
| 3735 | else |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3736 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3737 | // If "caused_stop" is true, then DoHalt stopped the process. If |
| 3738 | // "caused_stop" is false, the process was already stopped. |
| 3739 | // If the DoHalt caused the process to stop, then we want to catch |
| 3740 | // this event and set the interrupted bool to true before we pass |
| 3741 | // this along so clients know that the process was interrupted by |
| 3742 | // a halt command. |
| 3743 | if (caused_stop) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3744 | { |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3745 | // Wait for 1 second for the process to stop. |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3746 | TimeValue timeout_time; |
| 3747 | timeout_time = TimeValue::Now(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 3748 | timeout_time.OffsetWithSeconds(10); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3749 | bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp); |
| 3750 | StateType state = ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3751 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3752 | if (!got_event || state == eStateInvalid) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3753 | { |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3754 | // We timeout out and didn't get a stop event... |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3755 | error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState())); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3756 | } |
| 3757 | else |
| 3758 | { |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 3759 | if (StateIsStoppedState (state, false)) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3760 | { |
| 3761 | // We caused the process to interrupt itself, so mark this |
| 3762 | // as such in the stop event so clients can tell an interrupted |
| 3763 | // process from a natural stop |
| 3764 | ProcessEventData::SetInterruptedInEvent (event_sp.get(), true); |
| 3765 | } |
| 3766 | else |
| 3767 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3768 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3769 | if (log) |
| 3770 | log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state)); |
| 3771 | error.SetErrorString ("Did not get stopped event after halt."); |
| 3772 | } |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3773 | } |
| 3774 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3775 | DidHalt(); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3776 | } |
| 3777 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3778 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3779 | // Resume our private state thread before we post the event (if any) |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 3780 | if (!restored_process_events) |
| 3781 | RestorePrivateProcessEvents(); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3782 | |
| 3783 | // Post any event we might have consumed. If all goes well, we will have |
| 3784 | // stopped the process, intercepted the event and set the interrupted |
| 3785 | // bool in the event. Post it to the private event queue and that will end up |
| 3786 | // correctly setting the state. |
| 3787 | if (event_sp) |
| 3788 | m_private_state_broadcaster.BroadcastEvent(event_sp); |
| 3789 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3790 | return error; |
| 3791 | } |
| 3792 | |
| 3793 | Error |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3794 | Process::HaltForDestroyOrDetach(lldb::EventSP &exit_event_sp) |
| 3795 | { |
| 3796 | Error error; |
| 3797 | if (m_public_state.GetValue() == eStateRunning) |
| 3798 | { |
| 3799 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3800 | if (log) |
| 3801 | log->Printf("Process::Destroy() About to halt."); |
| 3802 | error = Halt(); |
| 3803 | if (error.Success()) |
| 3804 | { |
| 3805 | // Consume the halt event. |
| 3806 | TimeValue timeout (TimeValue::Now()); |
| 3807 | timeout.OffsetWithSeconds(1); |
| 3808 | StateType state = WaitForProcessToStop (&timeout, &exit_event_sp); |
| 3809 | |
| 3810 | // If the process exited while we were waiting for it to stop, put the exited event into |
| 3811 | // the shared pointer passed in and return. Our caller doesn't need to do anything else, since |
| 3812 | // they don't have a process anymore... |
| 3813 | |
| 3814 | if (state == eStateExited || m_private_state.GetValue() == eStateExited) |
| 3815 | { |
| 3816 | if (log) |
| 3817 | log->Printf("Process::HaltForDestroyOrDetach() Process exited while waiting to Halt."); |
| 3818 | return error; |
| 3819 | } |
| 3820 | else |
| 3821 | exit_event_sp.reset(); // It is ok to consume any non-exit stop events |
| 3822 | |
| 3823 | if (state != eStateStopped) |
| 3824 | { |
| 3825 | if (log) |
| 3826 | log->Printf("Process::HaltForDestroyOrDetach() Halt failed to stop, state is: %s", StateAsCString(state)); |
| 3827 | // If we really couldn't stop the process then we should just error out here, but if the |
| 3828 | // lower levels just bobbled sending the event and we really are stopped, then continue on. |
| 3829 | StateType private_state = m_private_state.GetValue(); |
| 3830 | if (private_state != eStateStopped) |
| 3831 | { |
| 3832 | return error; |
| 3833 | } |
| 3834 | } |
| 3835 | } |
| 3836 | else |
| 3837 | { |
| 3838 | if (log) |
| 3839 | log->Printf("Process::HaltForDestroyOrDetach() Halt got error: %s", error.AsCString()); |
| 3840 | } |
| 3841 | } |
| 3842 | return error; |
| 3843 | } |
| 3844 | |
| 3845 | Error |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3846 | Process::Detach (bool keep_stopped) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3847 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3848 | EventSP exit_event_sp; |
| 3849 | Error error; |
| 3850 | m_destroy_in_process = true; |
| 3851 | |
| 3852 | error = WillDetach(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3853 | |
| 3854 | if (error.Success()) |
| 3855 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3856 | if (DetachRequiresHalt()) |
| 3857 | { |
| 3858 | error = HaltForDestroyOrDetach (exit_event_sp); |
| 3859 | if (!error.Success()) |
| 3860 | { |
| 3861 | m_destroy_in_process = false; |
| 3862 | return error; |
| 3863 | } |
| 3864 | else if (exit_event_sp) |
| 3865 | { |
| 3866 | // We shouldn't need to do anything else here. There's no process left to detach from... |
| 3867 | StopPrivateStateThread(); |
| 3868 | m_destroy_in_process = false; |
| 3869 | return error; |
| 3870 | } |
| 3871 | } |
| 3872 | |
Andrew MacPherson | c3826b5 | 2014-03-25 19:59:36 +0000 | [diff] [blame] | 3873 | m_thread_list.DiscardThreadPlans(); |
| 3874 | DisableAllBreakpointSites(); |
| 3875 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3876 | error = DoDetach(keep_stopped); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3877 | if (error.Success()) |
| 3878 | { |
| 3879 | DidDetach(); |
| 3880 | StopPrivateStateThread(); |
| 3881 | } |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 3882 | else |
| 3883 | { |
| 3884 | return error; |
| 3885 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3886 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3887 | m_destroy_in_process = false; |
| 3888 | |
| 3889 | // If we exited when we were waiting for a process to stop, then |
| 3890 | // forward the event here so we don't lose the event |
| 3891 | if (exit_event_sp) |
| 3892 | { |
| 3893 | // Directly broadcast our exited event because we shut down our |
| 3894 | // private state thread above |
| 3895 | BroadcastEvent(exit_event_sp); |
| 3896 | } |
| 3897 | |
| 3898 | // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating |
| 3899 | // the last events through the event system, in which case we might strand the write lock. Unlock |
| 3900 | // it here so when we do to tear down the process we don't get an error destroying the lock. |
| 3901 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 3902 | m_public_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3903 | return error; |
| 3904 | } |
| 3905 | |
| 3906 | Error |
| 3907 | Process::Destroy () |
| 3908 | { |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 3909 | |
| 3910 | // Tell ourselves we are in the process of destroying the process, so that we don't do any unnecessary work |
| 3911 | // that might hinder the destruction. Remember to set this back to false when we are done. That way if the attempt |
| 3912 | // failed and the process stays around for some reason it won't be in a confused state. |
| 3913 | |
| 3914 | m_destroy_in_process = true; |
| 3915 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3916 | Error error (WillDestroy()); |
| 3917 | if (error.Success()) |
| 3918 | { |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 3919 | EventSP exit_event_sp; |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3920 | if (DestroyRequiresHalt()) |
Jim Ingham | 04e0a22 | 2012-05-23 15:46:31 +0000 | [diff] [blame] | 3921 | { |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3922 | error = HaltForDestroyOrDetach(exit_event_sp); |
Jim Ingham | 04e0a22 | 2012-05-23 15:46:31 +0000 | [diff] [blame] | 3923 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3924 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 3925 | if (m_public_state.GetValue() != eStateRunning) |
| 3926 | { |
| 3927 | // Ditch all thread plans, and remove all our breakpoints: in case we have to restart the target to |
| 3928 | // kill it, we don't want it hitting a breakpoint... |
| 3929 | // Only do this if we've stopped, however, since if we didn't manage to halt it above, then |
| 3930 | // we're not going to have much luck doing this now. |
| 3931 | m_thread_list.DiscardThreadPlans(); |
| 3932 | DisableAllBreakpointSites(); |
| 3933 | } |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3934 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3935 | error = DoDestroy(); |
| 3936 | if (error.Success()) |
| 3937 | { |
| 3938 | DidDestroy(); |
| 3939 | StopPrivateStateThread(); |
| 3940 | } |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3941 | m_stdio_communication.StopReadThread(); |
| 3942 | m_stdio_communication.Disconnect(); |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 3943 | m_stdin_forward = false; |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3944 | |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3945 | if (m_process_input_reader) |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3946 | { |
| 3947 | m_process_input_reader->SetIsDone(true); |
| 3948 | m_process_input_reader->Cancel(); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3949 | m_process_input_reader.reset(); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
Greg Clayton | 85fb1b9 | 2012-09-11 02:33:37 +0000 | [diff] [blame] | 3952 | // If we exited when we were waiting for a process to stop, then |
| 3953 | // forward the event here so we don't lose the event |
| 3954 | if (exit_event_sp) |
| 3955 | { |
| 3956 | // Directly broadcast our exited event because we shut down our |
| 3957 | // private state thread above |
| 3958 | BroadcastEvent(exit_event_sp); |
| 3959 | } |
| 3960 | |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 3961 | // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating |
| 3962 | // the last events through the event system, in which case we might strand the write lock. Unlock |
| 3963 | // 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] | 3964 | m_public_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3965 | } |
Jim Ingham | 0943792 | 2013-03-01 20:04:25 +0000 | [diff] [blame] | 3966 | |
| 3967 | m_destroy_in_process = false; |
| 3968 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3969 | return error; |
| 3970 | } |
| 3971 | |
| 3972 | Error |
| 3973 | Process::Signal (int signal) |
| 3974 | { |
| 3975 | Error error (WillSignal()); |
| 3976 | if (error.Success()) |
| 3977 | { |
| 3978 | error = DoSignal(signal); |
| 3979 | if (error.Success()) |
| 3980 | DidSignal(); |
| 3981 | } |
| 3982 | return error; |
| 3983 | } |
| 3984 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3985 | lldb::ByteOrder |
| 3986 | Process::GetByteOrder () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3987 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3988 | return m_target.GetArchitecture().GetByteOrder(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | uint32_t |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3992 | Process::GetAddressByteSize () const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3993 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3994 | return m_target.GetArchitecture().GetAddressByteSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 3997 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3998 | bool |
| 3999 | Process::ShouldBroadcastEvent (Event *event_ptr) |
| 4000 | { |
| 4001 | const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr); |
| 4002 | bool return_value = true; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4003 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4004 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4005 | switch (state) |
| 4006 | { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 4007 | case eStateConnected: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4008 | case eStateAttaching: |
| 4009 | case eStateLaunching: |
| 4010 | case eStateDetached: |
| 4011 | case eStateExited: |
| 4012 | case eStateUnloaded: |
| 4013 | // These events indicate changes in the state of the debugging session, always report them. |
| 4014 | return_value = true; |
| 4015 | break; |
| 4016 | case eStateInvalid: |
| 4017 | // We stopped for no apparent reason, don't report it. |
| 4018 | return_value = false; |
| 4019 | break; |
| 4020 | case eStateRunning: |
| 4021 | case eStateStepping: |
| 4022 | // If we've started the target running, we handle the cases where we |
| 4023 | // are already running and where there is a transition from stopped to |
| 4024 | // running differently. |
| 4025 | // running -> running: Automatically suppress extra running events |
| 4026 | // stopped -> running: Report except when there is one or more no votes |
| 4027 | // and no yes votes. |
| 4028 | SynchronouslyNotifyStateChanged (state); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 4029 | if (m_force_next_event_delivery) |
| 4030 | return_value = true; |
| 4031 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4032 | { |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 4033 | switch (m_last_broadcast_state) |
| 4034 | { |
| 4035 | case eStateRunning: |
| 4036 | case eStateStepping: |
| 4037 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 4038 | return_value = false; |
| 4039 | break; |
| 4040 | default: |
| 4041 | // TODO: make this work correctly. For now always report |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 4042 | // 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] | 4043 | // events. If I run the lldb/test/thread/a.out file and |
| 4044 | // break at main.cpp:58, run and hit the breakpoints on |
| 4045 | // multiple threads, then somehow during the stepping over |
| 4046 | // of all breakpoints no run gets reported. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4047 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 4048 | // This is a transition from stop to run. |
| 4049 | switch (m_thread_list.ShouldReportRun (event_ptr)) |
| 4050 | { |
| 4051 | case eVoteYes: |
| 4052 | case eVoteNoOpinion: |
| 4053 | return_value = true; |
| 4054 | break; |
| 4055 | case eVoteNo: |
| 4056 | return_value = false; |
| 4057 | break; |
| 4058 | } |
| 4059 | break; |
| 4060 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4061 | } |
| 4062 | break; |
| 4063 | case eStateStopped: |
| 4064 | case eStateCrashed: |
| 4065 | case eStateSuspended: |
| 4066 | { |
| 4067 | // We've stopped. First see if we're going to restart the target. |
| 4068 | // If we are going to stop, then we always broadcast the event. |
| 4069 | // 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] | 4070 | // If no thread has an opinion, we don't report it. |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4071 | |
Jim Ingham | cb4ca11 | 2012-05-16 01:32:14 +0000 | [diff] [blame] | 4072 | RefreshStateAfterStop (); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4073 | if (ProcessEventData::GetInterruptedFromEvent (event_ptr)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4074 | { |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 4075 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4076 | log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4077 | static_cast<void*>(event_ptr), |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4078 | StateAsCString(state)); |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 4079 | // Even though we know we are going to stop, we should let the threads have a look at the stop, |
| 4080 | // so they can properly set their state. |
| 4081 | m_thread_list.ShouldStop (event_ptr); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4082 | return_value = true; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4083 | } |
| 4084 | else |
| 4085 | { |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4086 | bool was_restarted = ProcessEventData::GetRestartedFromEvent (event_ptr); |
| 4087 | bool should_resume = false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4088 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4089 | // It makes no sense to ask "ShouldStop" if we've already been restarted... |
| 4090 | // Asking the thread list is also not likely to go well, since we are running again. |
| 4091 | // So in that case just report the event. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4092 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4093 | if (!was_restarted) |
| 4094 | should_resume = m_thread_list.ShouldStop (event_ptr) == false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4095 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4096 | if (was_restarted || should_resume || m_resume_requested) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4097 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4098 | Vote stop_vote = m_thread_list.ShouldReportStop (event_ptr); |
| 4099 | if (log) |
| 4100 | 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] | 4101 | should_resume, StateAsCString(state), |
| 4102 | was_restarted, stop_vote); |
| 4103 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4104 | switch (stop_vote) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4105 | { |
| 4106 | case eVoteYes: |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4107 | return_value = true; |
| 4108 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4109 | case eVoteNoOpinion: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4110 | case eVoteNo: |
| 4111 | return_value = false; |
| 4112 | break; |
| 4113 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4114 | |
Jim Ingham | cb95f34 | 2012-09-05 21:13:56 +0000 | [diff] [blame] | 4115 | if (!was_restarted) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4116 | { |
| 4117 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4118 | log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", |
| 4119 | static_cast<void*>(event_ptr), |
| 4120 | StateAsCString(state)); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4121 | ProcessEventData::SetRestartedInEvent(event_ptr, true); |
Jim Ingham | cb95f34 | 2012-09-05 21:13:56 +0000 | [diff] [blame] | 4122 | PrivateResume (); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4123 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4124 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4125 | } |
| 4126 | else |
| 4127 | { |
| 4128 | return_value = true; |
| 4129 | SynchronouslyNotifyStateChanged (state); |
| 4130 | } |
| 4131 | } |
| 4132 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4133 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4134 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4135 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 4136 | // Forcing the next event delivery is a one shot deal. So reset it here. |
| 4137 | m_force_next_event_delivery = false; |
| 4138 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4139 | // We do some coalescing of events (for instance two consecutive running events get coalesced.) |
| 4140 | // But we only coalesce against events we actually broadcast. So we use m_last_broadcast_state |
| 4141 | // to track that. NB - you can't use "m_public_state.GetValue()" for that purpose, as was originally done, |
| 4142 | // because the PublicState reflects the last event pulled off the queue, and there may be several |
| 4143 | // events stacked up on the queue unserviced. So the PublicState may not reflect the last broadcasted event |
| 4144 | // yet. m_last_broadcast_state gets updated here. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4145 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4146 | if (return_value) |
| 4147 | m_last_broadcast_state = state; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4148 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4149 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4150 | 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] | 4151 | static_cast<void*>(event_ptr), StateAsCString(state), |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4152 | StateAsCString(m_last_broadcast_state), |
| 4153 | return_value ? "YES" : "NO"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4154 | return return_value; |
| 4155 | } |
| 4156 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4157 | |
| 4158 | bool |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4159 | Process::StartPrivateStateThread (bool force) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4160 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4161 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4162 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 4163 | bool already_running = PrivateStateThreadIsValid (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4164 | if (log) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 4165 | log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread"); |
| 4166 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4167 | if (!force && already_running) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 4168 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4169 | |
| 4170 | // Create a thread that watches our internal state and controls which |
| 4171 | // events make it to clients (into the DCProcess event queue). |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 4172 | char thread_name[1024]; |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 4173 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 4174 | if (HostInfo::GetMaxThreadNameLength() <= 30) |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 4175 | { |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 4176 | // On platforms with abbreviated thread name lengths, choose thread names that fit within the limit. |
| 4177 | if (already_running) |
| 4178 | snprintf(thread_name, sizeof(thread_name), "intern-state-OV"); |
| 4179 | else |
| 4180 | snprintf(thread_name, sizeof(thread_name), "intern-state"); |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 4181 | } |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4182 | else |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 4183 | { |
| 4184 | if (already_running) |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 4185 | 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] | 4186 | else |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 4187 | 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] | 4188 | } |
| 4189 | |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4190 | // Create the private state thread, and start it running. |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 4191 | m_private_state_thread = ThreadLauncher::LaunchThread(thread_name, Process::PrivateStateThread, this, NULL); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 4192 | if (m_private_state_thread.IsJoinable()) |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4193 | { |
| 4194 | ResumePrivateStateThread(); |
| 4195 | return true; |
| 4196 | } |
| 4197 | else |
| 4198 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | void |
| 4202 | Process::PausePrivateStateThread () |
| 4203 | { |
| 4204 | ControlPrivateStateThread (eBroadcastInternalStateControlPause); |
| 4205 | } |
| 4206 | |
| 4207 | void |
| 4208 | Process::ResumePrivateStateThread () |
| 4209 | { |
| 4210 | ControlPrivateStateThread (eBroadcastInternalStateControlResume); |
| 4211 | } |
| 4212 | |
| 4213 | void |
| 4214 | Process::StopPrivateStateThread () |
| 4215 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 4216 | if (PrivateStateThreadIsValid ()) |
| 4217 | ControlPrivateStateThread (eBroadcastInternalStateControlStop); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4218 | else |
| 4219 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4220 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4221 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4222 | 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] | 4223 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
| 4226 | void |
| 4227 | Process::ControlPrivateStateThread (uint32_t signal) |
| 4228 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4229 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4230 | |
| 4231 | assert (signal == eBroadcastInternalStateControlStop || |
| 4232 | signal == eBroadcastInternalStateControlPause || |
| 4233 | signal == eBroadcastInternalStateControlResume); |
| 4234 | |
| 4235 | if (log) |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 4236 | log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4237 | |
Greg Clayton | 7ecb3a0 | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 4238 | // Signal the private state thread. First we should copy this is case the |
| 4239 | // thread starts exiting since the private state thread will NULL this out |
| 4240 | // when it exits |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 4241 | HostThread private_state_thread(m_private_state_thread); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 4242 | if (private_state_thread.IsJoinable()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4243 | { |
| 4244 | TimeValue timeout_time; |
| 4245 | bool timed_out; |
| 4246 | |
| 4247 | m_private_state_control_broadcaster.BroadcastEvent (signal, NULL); |
| 4248 | |
| 4249 | timeout_time = TimeValue::Now(); |
| 4250 | timeout_time.OffsetWithSeconds(2); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4251 | if (log) |
| 4252 | log->Printf ("Sending control event of type: %d.", signal); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4253 | m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out); |
| 4254 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 4255 | |
| 4256 | if (signal == eBroadcastInternalStateControlStop) |
| 4257 | { |
| 4258 | if (timed_out) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4259 | { |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 4260 | Error error = private_state_thread.Cancel(); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4261 | if (log) |
| 4262 | log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString()); |
| 4263 | } |
| 4264 | else |
| 4265 | { |
| 4266 | if (log) |
| 4267 | log->Printf ("The control event killed the private state thread without having to cancel."); |
| 4268 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4269 | |
| 4270 | thread_result_t result = NULL; |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 4271 | private_state_thread.Join(&result); |
| 4272 | m_private_state_thread.Reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4273 | } |
| 4274 | } |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4275 | else |
| 4276 | { |
| 4277 | if (log) |
| 4278 | log->Printf ("Private state thread already dead, no need to signal it to stop."); |
| 4279 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4280 | } |
| 4281 | |
| 4282 | void |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4283 | Process::SendAsyncInterrupt () |
| 4284 | { |
| 4285 | if (PrivateStateThreadIsValid()) |
| 4286 | m_private_state_broadcaster.BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 4287 | else |
| 4288 | BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 4289 | } |
| 4290 | |
| 4291 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4292 | Process::HandlePrivateEvent (EventSP &event_sp) |
| 4293 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4294 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4295 | m_resume_requested = false; |
| 4296 | |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 4297 | m_currently_handling_event.SetValue(true, eBroadcastNever); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4298 | |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4299 | const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4300 | |
| 4301 | // First check to see if anybody wants a shot at this event: |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 4302 | if (m_next_event_action_ap.get() != NULL) |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4303 | { |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 4304 | NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4305 | if (log) |
| 4306 | log->Printf ("Ran next event action, result was %d.", action_result); |
| 4307 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4308 | switch (action_result) |
| 4309 | { |
| 4310 | case NextEventAction::eEventActionSuccess: |
| 4311 | SetNextEventAction(NULL); |
| 4312 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 4313 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4314 | case NextEventAction::eEventActionRetry: |
| 4315 | break; |
Greg Clayton | c9ed478 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 4316 | |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4317 | case NextEventAction::eEventActionExit: |
Jim Ingham | 2a5fdd4 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 4318 | // Handle Exiting Here. If we already got an exited event, |
| 4319 | // we should just propagate it. Otherwise, swallow this event, |
| 4320 | // and set our state to exit so the next event will kill us. |
| 4321 | if (new_state != eStateExited) |
| 4322 | { |
| 4323 | // FIXME: should cons up an exited event, and discard this one. |
Jim Ingham | 754ab98 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 4324 | SetExitStatus(0, m_next_event_action_ap->GetExitString()); |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4325 | m_currently_handling_event.SetValue(false, eBroadcastAlways); |
Jim Ingham | 2a5fdd4 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 4326 | SetNextEventAction(NULL); |
| 4327 | return; |
| 4328 | } |
| 4329 | SetNextEventAction(NULL); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 4330 | break; |
| 4331 | } |
| 4332 | } |
| 4333 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4334 | // See if we should broadcast this state to external clients? |
| 4335 | const bool should_broadcast = ShouldBroadcastEvent (event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4336 | |
| 4337 | if (should_broadcast) |
| 4338 | { |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4339 | const bool is_hijacked = IsHijackedForEvent(eBroadcastBitStateChanged); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4340 | if (log) |
| 4341 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4342 | 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] | 4343 | __FUNCTION__, |
| 4344 | GetID(), |
| 4345 | StateAsCString(new_state), |
| 4346 | StateAsCString (GetState ()), |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4347 | is_hijacked ? "hijacked" : "public"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4348 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4349 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
Greg Clayton | 414f5d3 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4350 | if (StateIsRunningState (new_state)) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4351 | { |
| 4352 | // Only push the input handler if we aren't fowarding events, |
| 4353 | // as this means the curses GUI is in use... |
Todd Fiala | f72fa67 | 2014-10-07 16:05:21 +0000 | [diff] [blame] | 4354 | // Or don't push it if we are launching since it will come up stopped. |
| 4355 | if (!GetTarget().GetDebugger().IsForwardingEvents() && new_state != eStateLaunching) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4356 | PushProcessIOHandler (); |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 4357 | m_iohandler_sync.SetValue(true, eBroadcastAlways); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4358 | } |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4359 | else if (StateIsStoppedState(new_state, false)) |
| 4360 | { |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 4361 | m_iohandler_sync.SetValue(false, eBroadcastNever); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 4362 | if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 4363 | { |
| 4364 | // If the lldb_private::Debugger is handling the events, we don't |
| 4365 | // want to pop the process IOHandler here, we want to do it when |
| 4366 | // we receive the stopped event so we can carefully control when |
| 4367 | // the process IOHandler is popped because when we stop we want to |
| 4368 | // display some text stating how and why we stopped, then maybe some |
| 4369 | // process/thread/frame info, and then we want the "(lldb) " prompt |
| 4370 | // to show up. If we pop the process IOHandler here, then we will |
| 4371 | // cause the command interpreter to become the top IOHandler after |
| 4372 | // the process pops off and it will update its prompt right away... |
| 4373 | // See the Debugger.cpp file where it calls the function as |
| 4374 | // "process_sp->PopProcessIOHandler()" to see where I am talking about. |
| 4375 | // Otherwise we end up getting overlapping "(lldb) " prompts and |
| 4376 | // garbled output. |
| 4377 | // |
| 4378 | // If we aren't handling the events in the debugger (which is indicated |
| 4379 | // by "m_target.GetDebugger().IsHandlingEvents()" returning false) or we |
| 4380 | // are hijacked, then we always pop the process IO handler manually. |
| 4381 | // Hijacking happens when the internal process state thread is running |
| 4382 | // thread plans, or when commands want to run in synchronous mode |
| 4383 | // and they call "process->WaitForProcessToStop()". An example of something |
| 4384 | // that will hijack the events is a simple expression: |
| 4385 | // |
| 4386 | // (lldb) expr (int)puts("hello") |
| 4387 | // |
| 4388 | // This will cause the internal process state thread to resume and halt |
| 4389 | // the process (and _it_ will hijack the eBroadcastBitStateChanged |
| 4390 | // events) and we do need the IO handler to be pushed and popped |
| 4391 | // correctly. |
| 4392 | |
| 4393 | if (is_hijacked || m_target.GetDebugger().IsHandlingEvents() == false) |
| 4394 | PopProcessIOHandler (); |
| 4395 | } |
| 4396 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4397 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4398 | BroadcastEvent (event_sp); |
| 4399 | } |
| 4400 | else |
| 4401 | { |
| 4402 | if (log) |
| 4403 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 4404 | 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] | 4405 | __FUNCTION__, |
| 4406 | GetID(), |
| 4407 | StateAsCString(new_state), |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 4408 | StateAsCString (GetState ())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4409 | } |
| 4410 | } |
Jim Ingham | aacc318 | 2012-06-06 00:29:30 +0000 | [diff] [blame] | 4411 | m_currently_handling_event.SetValue(false, eBroadcastAlways); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4412 | } |
| 4413 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4414 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4415 | Process::PrivateStateThread (void *arg) |
| 4416 | { |
| 4417 | Process *proc = static_cast<Process*> (arg); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4418 | thread_result_t result = proc->RunPrivateStateThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4419 | return result; |
| 4420 | } |
| 4421 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 4422 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4423 | Process::RunPrivateStateThread () |
| 4424 | { |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4425 | bool control_only = true; |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4426 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4427 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4428 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4429 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4430 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...", |
| 4431 | __FUNCTION__, static_cast<void*>(this), GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4432 | |
| 4433 | bool exit_now = false; |
| 4434 | while (!exit_now) |
| 4435 | { |
| 4436 | EventSP event_sp; |
| 4437 | WaitForEventsPrivate (NULL, event_sp, control_only); |
| 4438 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) |
| 4439 | { |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4440 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4441 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") got a control event: %d", |
| 4442 | __FUNCTION__, static_cast<void*>(this), GetID(), |
| 4443 | event_sp->GetType()); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 4444 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4445 | switch (event_sp->GetType()) |
| 4446 | { |
| 4447 | case eBroadcastInternalStateControlStop: |
| 4448 | exit_now = true; |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 4449 | break; // doing any internal state management below |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4450 | |
| 4451 | case eBroadcastInternalStateControlPause: |
| 4452 | control_only = true; |
| 4453 | break; |
| 4454 | |
| 4455 | case eBroadcastInternalStateControlResume: |
| 4456 | control_only = false; |
| 4457 | break; |
| 4458 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4459 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4460 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4461 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4462 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4463 | else if (event_sp->GetType() == eBroadcastBitInterrupt) |
| 4464 | { |
| 4465 | if (m_public_state.GetValue() == eStateAttaching) |
| 4466 | { |
| 4467 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4468 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt while attaching - forwarding interrupt.", |
| 4469 | __FUNCTION__, static_cast<void*>(this), |
| 4470 | GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4471 | BroadcastEvent (eBroadcastBitInterrupt, NULL); |
| 4472 | } |
| 4473 | else |
| 4474 | { |
| 4475 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4476 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") woke up with an interrupt - Halting.", |
| 4477 | __FUNCTION__, static_cast<void*>(this), |
| 4478 | GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 4479 | Halt(); |
| 4480 | } |
| 4481 | continue; |
| 4482 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4483 | |
| 4484 | const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 4485 | |
| 4486 | if (internal_state != eStateInvalid) |
| 4487 | { |
Greg Clayton | f9b57b9 | 2013-05-10 23:48:10 +0000 | [diff] [blame] | 4488 | if (m_clear_thread_plans_on_stop && |
| 4489 | StateIsStoppedState(internal_state, true)) |
| 4490 | { |
| 4491 | m_clear_thread_plans_on_stop = false; |
| 4492 | m_thread_list.DiscardThreadPlans(); |
| 4493 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4494 | HandlePrivateEvent (event_sp); |
| 4495 | } |
| 4496 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 4497 | if (internal_state == eStateInvalid || |
| 4498 | internal_state == eStateExited || |
| 4499 | internal_state == eStateDetached ) |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4500 | { |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4501 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4502 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") about to exit with internal state %s...", |
| 4503 | __FUNCTION__, static_cast<void*>(this), GetID(), |
| 4504 | StateAsCString(internal_state)); |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4505 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4506 | break; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4507 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 4510 | // Verify log is still enabled before attempting to write to it... |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4511 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4512 | log->Printf ("Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", |
| 4513 | __FUNCTION__, static_cast<void*>(this), GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4514 | |
Ed Maste | 64fad60 | 2013-07-29 20:58:06 +0000 | [diff] [blame] | 4515 | m_public_run_lock.SetStopped(); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 4516 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 4517 | m_private_state_thread.Reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4518 | return NULL; |
| 4519 | } |
| 4520 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4521 | //------------------------------------------------------------------ |
| 4522 | // Process Event Data |
| 4523 | //------------------------------------------------------------------ |
| 4524 | |
| 4525 | Process::ProcessEventData::ProcessEventData () : |
| 4526 | EventData (), |
| 4527 | m_process_sp (), |
| 4528 | m_state (eStateInvalid), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 4529 | m_restarted (false), |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4530 | m_update_state (0), |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4531 | m_interrupted (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4532 | { |
| 4533 | } |
| 4534 | |
| 4535 | Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) : |
| 4536 | EventData (), |
| 4537 | m_process_sp (process_sp), |
| 4538 | m_state (state), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 4539 | m_restarted (false), |
Jim Ingham | a860469 | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 4540 | m_update_state (0), |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4541 | m_interrupted (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4542 | { |
| 4543 | } |
| 4544 | |
| 4545 | Process::ProcessEventData::~ProcessEventData() |
| 4546 | { |
| 4547 | } |
| 4548 | |
| 4549 | const ConstString & |
| 4550 | Process::ProcessEventData::GetFlavorString () |
| 4551 | { |
| 4552 | static ConstString g_flavor ("Process::ProcessEventData"); |
| 4553 | return g_flavor; |
| 4554 | } |
| 4555 | |
| 4556 | const ConstString & |
| 4557 | Process::ProcessEventData::GetFlavor () const |
| 4558 | { |
| 4559 | return ProcessEventData::GetFlavorString (); |
| 4560 | } |
| 4561 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4562 | void |
| 4563 | Process::ProcessEventData::DoOnRemoval (Event *event_ptr) |
| 4564 | { |
| 4565 | // 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] | 4566 | // off of the private process event queue, and then any number of times, first when it gets pulled off of |
| 4567 | // the public event queue, then other times when we're pretending that this is where we stopped at the |
| 4568 | // end of expression evaluation. m_update_state is used to distinguish these |
| 4569 | // 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] | 4570 | // 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] | 4571 | if (m_update_state != 1) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4572 | return; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4573 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 4574 | m_process_sp->SetPublicState (m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr)); |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 4575 | |
| 4576 | // If this is a halt event, even if the halt stopped with some reason other than a plain interrupt (e.g. we had |
| 4577 | // already stopped for a breakpoint when the halt request came through) don't do the StopInfo actions, as they may |
| 4578 | // end up restarting the process. |
| 4579 | if (m_interrupted) |
| 4580 | return; |
| 4581 | |
| 4582 | // 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] | 4583 | if (m_state == eStateStopped && ! m_restarted) |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4584 | { |
| 4585 | ThreadList &curr_thread_list = m_process_sp->GetThreadList(); |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4586 | uint32_t num_threads = curr_thread_list.GetSize(); |
| 4587 | uint32_t idx; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 4588 | |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4589 | // The actions might change one of the thread's stop_info's opinions about whether we should |
| 4590 | // stop the process, so we need to query that as we go. |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4591 | |
| 4592 | // One other complication here, is that we try to catch any case where the target has run (except for expressions) |
| 4593 | // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and |
| 4594 | // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like |
| 4595 | // 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 |
| 4596 | // against this list & bag out if anything differs. |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4597 | std::vector<uint32_t> thread_index_array(num_threads); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4598 | for (idx = 0; idx < num_threads; ++idx) |
| 4599 | thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID(); |
| 4600 | |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4601 | // Use this to track whether we should continue from here. We will only continue the target running if |
| 4602 | // no thread says we should stop. Of course if some thread's PerformAction actually sets the target running, |
| 4603 | // then it doesn't matter what the other threads say... |
| 4604 | |
| 4605 | bool still_should_stop = false; |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4606 | |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4607 | // Sometimes - for instance if we have a bug in the stub we are talking to, we stop but no thread has a |
| 4608 | // valid stop reason. In that case we should just stop, because we have no way of telling what the right |
| 4609 | // thing to do is, and it's better to let the user decide than continue behind their backs. |
| 4610 | |
| 4611 | bool does_anybody_have_an_opinion = false; |
| 4612 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4613 | for (idx = 0; idx < num_threads; ++idx) |
| 4614 | { |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4615 | curr_thread_list = m_process_sp->GetThreadList(); |
| 4616 | if (curr_thread_list.GetSize() != num_threads) |
| 4617 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4618 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4619 | if (log) |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4620 | 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] | 4621 | break; |
| 4622 | } |
| 4623 | |
| 4624 | lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx); |
| 4625 | |
| 4626 | if (thread_sp->GetIndexID() != thread_index_array[idx]) |
| 4627 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4628 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 87c665f | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 4629 | if (log) |
Greg Clayton | 61e7a58 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 4630 | 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] | 4631 | idx, |
| 4632 | thread_index_array[idx], |
| 4633 | thread_sp->GetIndexID()); |
Jim Ingham | 0faa43f | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 4634 | break; |
| 4635 | } |
| 4636 | |
Jim Ingham | b15bfc7 | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 4637 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo (); |
Jim Ingham | 5d88a06 | 2012-10-16 00:09:33 +0000 | [diff] [blame] | 4638 | if (stop_info_sp && stop_info_sp->IsValid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4639 | { |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4640 | does_anybody_have_an_opinion = true; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4641 | bool this_thread_wants_to_stop; |
| 4642 | if (stop_info_sp->GetOverrideShouldStop()) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4643 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4644 | this_thread_wants_to_stop = stop_info_sp->GetOverriddenShouldStopValue(); |
| 4645 | } |
| 4646 | else |
| 4647 | { |
| 4648 | stop_info_sp->PerformAction(event_ptr); |
| 4649 | // The stop action might restart the target. If it does, then we want to mark that in the |
| 4650 | // event so that whoever is receiving it will know to wait for the running event and reflect |
| 4651 | // that state appropriately. |
| 4652 | // We also need to stop processing actions, since they aren't expecting the target to be running. |
| 4653 | |
| 4654 | // FIXME: we might have run. |
| 4655 | if (stop_info_sp->HasTargetRunSinceMe()) |
| 4656 | { |
| 4657 | SetRestarted (true); |
| 4658 | break; |
| 4659 | } |
| 4660 | |
| 4661 | this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4662 | } |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4663 | |
Jim Ingham | c7078c2 | 2012-12-13 22:24:15 +0000 | [diff] [blame] | 4664 | if (still_should_stop == false) |
| 4665 | still_should_stop = this_thread_wants_to_stop; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4666 | } |
| 4667 | } |
Jim Ingham | 3ebcf7f | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 4668 | |
Ashok Thirumurthi | cf7c55e | 2013-04-18 14:38:20 +0000 | [diff] [blame] | 4669 | |
Jim Ingham | a8ca6e2 | 2013-05-03 23:04:37 +0000 | [diff] [blame] | 4670 | if (!GetRestarted()) |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4671 | { |
Jim Ingham | 0ad7e05 | 2013-04-25 02:04:59 +0000 | [diff] [blame] | 4672 | if (!still_should_stop && does_anybody_have_an_opinion) |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4673 | { |
| 4674 | // We've been asked to continue, so do that here. |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4675 | SetRestarted(true); |
Jim Ingham | 3b8285d | 2012-04-19 01:40:33 +0000 | [diff] [blame] | 4676 | // Use the public resume method here, since this is just |
| 4677 | // extending a public resume. |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4678 | m_process_sp->PrivateResume(); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4679 | } |
| 4680 | else |
| 4681 | { |
| 4682 | // If we didn't restart, run the Stop Hooks here: |
| 4683 | // They might also restart the target, so watch for that. |
| 4684 | m_process_sp->GetTarget().RunStopHooks(); |
| 4685 | if (m_process_sp->GetPrivateState() == eStateRunning) |
| 4686 | SetRestarted(true); |
| 4687 | } |
Jim Ingham | 9575d84 | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 4688 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4689 | } |
| 4690 | } |
| 4691 | |
| 4692 | void |
| 4693 | Process::ProcessEventData::Dump (Stream *s) const |
| 4694 | { |
| 4695 | if (m_process_sp) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4696 | s->Printf(" process = %p (pid = %" PRIu64 "), ", |
| 4697 | static_cast<void*>(m_process_sp.get()), m_process_sp->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4698 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 4699 | s->Printf("state = %s", StateAsCString(GetState())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | const Process::ProcessEventData * |
| 4703 | Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr) |
| 4704 | { |
| 4705 | if (event_ptr) |
| 4706 | { |
| 4707 | const EventData *event_data = event_ptr->GetData(); |
| 4708 | if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 4709 | return static_cast <const ProcessEventData *> (event_ptr->GetData()); |
| 4710 | } |
| 4711 | return NULL; |
| 4712 | } |
| 4713 | |
| 4714 | ProcessSP |
| 4715 | Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr) |
| 4716 | { |
| 4717 | ProcessSP process_sp; |
| 4718 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4719 | if (data) |
| 4720 | process_sp = data->GetProcessSP(); |
| 4721 | return process_sp; |
| 4722 | } |
| 4723 | |
| 4724 | StateType |
| 4725 | Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr) |
| 4726 | { |
| 4727 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4728 | if (data == NULL) |
| 4729 | return eStateInvalid; |
| 4730 | else |
| 4731 | return data->GetState(); |
| 4732 | } |
| 4733 | |
| 4734 | bool |
| 4735 | Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr) |
| 4736 | { |
| 4737 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4738 | if (data == NULL) |
| 4739 | return false; |
| 4740 | else |
| 4741 | return data->GetRestarted(); |
| 4742 | } |
| 4743 | |
| 4744 | void |
| 4745 | Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value) |
| 4746 | { |
| 4747 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4748 | if (data != NULL) |
| 4749 | data->SetRestarted(new_value); |
| 4750 | } |
| 4751 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4752 | size_t |
| 4753 | Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr) |
| 4754 | { |
| 4755 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4756 | if (data != NULL) |
| 4757 | return data->GetNumRestartedReasons(); |
| 4758 | else |
| 4759 | return 0; |
| 4760 | } |
| 4761 | |
| 4762 | const char * |
| 4763 | Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, size_t idx) |
| 4764 | { |
| 4765 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4766 | if (data != NULL) |
| 4767 | return data->GetRestartedReasonAtIndex(idx); |
| 4768 | else |
| 4769 | return NULL; |
| 4770 | } |
| 4771 | |
| 4772 | void |
| 4773 | Process::ProcessEventData::AddRestartedReason (Event *event_ptr, const char *reason) |
| 4774 | { |
| 4775 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4776 | if (data != NULL) |
| 4777 | data->AddRestartedReason(reason); |
| 4778 | } |
| 4779 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4780 | bool |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 4781 | Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr) |
| 4782 | { |
| 4783 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 4784 | if (data == NULL) |
| 4785 | return false; |
| 4786 | else |
| 4787 | return data->GetInterrupted (); |
| 4788 | } |
| 4789 | |
| 4790 | void |
| 4791 | Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value) |
| 4792 | { |
| 4793 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4794 | if (data != NULL) |
| 4795 | data->SetInterrupted(new_value); |
| 4796 | } |
| 4797 | |
| 4798 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4799 | Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr) |
| 4800 | { |
| 4801 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 4802 | if (data) |
| 4803 | { |
| 4804 | data->SetUpdateStateOnRemoval(); |
| 4805 | return true; |
| 4806 | } |
| 4807 | return false; |
| 4808 | } |
| 4809 | |
Greg Clayton | d9e416c | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 4810 | lldb::TargetSP |
| 4811 | Process::CalculateTarget () |
| 4812 | { |
| 4813 | return m_target.shared_from_this(); |
| 4814 | } |
| 4815 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4816 | void |
Greg Clayton | 0603aa9 | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 4817 | Process::CalculateExecutionContext (ExecutionContext &exe_ctx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4818 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4819 | exe_ctx.SetTargetPtr (&m_target); |
| 4820 | exe_ctx.SetProcessPtr (this); |
| 4821 | exe_ctx.SetThreadPtr(NULL); |
| 4822 | exe_ctx.SetFramePtr (NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4823 | } |
| 4824 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4825 | //uint32_t |
| 4826 | //Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 4827 | //{ |
| 4828 | // return 0; |
| 4829 | //} |
| 4830 | // |
| 4831 | //ArchSpec |
| 4832 | //Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
| 4833 | //{ |
| 4834 | // return Host::GetArchSpecForExistingProcess (pid); |
| 4835 | //} |
| 4836 | // |
| 4837 | //ArchSpec |
| 4838 | //Process::GetArchSpecForExistingProcess (const char *process_name) |
| 4839 | //{ |
| 4840 | // return Host::GetArchSpecForExistingProcess (process_name); |
| 4841 | //} |
| 4842 | // |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4843 | void |
| 4844 | Process::AppendSTDOUT (const char * s, size_t len) |
| 4845 | { |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 4846 | Mutex::Locker locker (m_stdio_communication_mutex); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4847 | m_stdout_data.append (s, len); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 4848 | BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (shared_from_this(), GetState())); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4849 | } |
| 4850 | |
| 4851 | void |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4852 | Process::AppendSTDERR (const char * s, size_t len) |
| 4853 | { |
| 4854 | Mutex::Locker locker (m_stdio_communication_mutex); |
| 4855 | m_stderr_data.append (s, len); |
Greg Clayton | 35a4cc5 | 2012-10-29 20:52:08 +0000 | [diff] [blame] | 4856 | BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (shared_from_this(), GetState())); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4859 | void |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4860 | Process::BroadcastAsyncProfileData(const std::string &one_profile_data) |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4861 | { |
| 4862 | Mutex::Locker locker (m_profile_data_comm_mutex); |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4863 | m_profile_data.push_back(one_profile_data); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4864 | BroadcastEventIfUnique (eBroadcastBitProfileData, new ProcessEventData (shared_from_this(), GetState())); |
| 4865 | } |
| 4866 | |
| 4867 | size_t |
| 4868 | Process::GetAsyncProfileData (char *buf, size_t buf_size, Error &error) |
| 4869 | { |
| 4870 | Mutex::Locker locker(m_profile_data_comm_mutex); |
Han Ming Ong | 929a94f | 2012-11-29 22:14:45 +0000 | [diff] [blame] | 4871 | if (m_profile_data.empty()) |
| 4872 | return 0; |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4873 | |
| 4874 | std::string &one_profile_data = m_profile_data.front(); |
| 4875 | size_t bytes_available = one_profile_data.size(); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4876 | if (bytes_available > 0) |
| 4877 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4878 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4879 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4880 | log->Printf ("Process::GetProfileData (buf = %p, size = %" PRIu64 ")", |
| 4881 | static_cast<void*>(buf), |
| 4882 | static_cast<uint64_t>(buf_size)); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4883 | if (bytes_available > buf_size) |
| 4884 | { |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4885 | memcpy(buf, one_profile_data.c_str(), buf_size); |
| 4886 | one_profile_data.erase(0, buf_size); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4887 | bytes_available = buf_size; |
| 4888 | } |
| 4889 | else |
| 4890 | { |
Han Ming Ong | 91ed6b8 | 2013-06-24 18:15:05 +0000 | [diff] [blame] | 4891 | memcpy(buf, one_profile_data.c_str(), bytes_available); |
Han Ming Ong | 929a94f | 2012-11-29 22:14:45 +0000 | [diff] [blame] | 4892 | m_profile_data.erase(m_profile_data.begin()); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4893 | } |
| 4894 | } |
| 4895 | return bytes_available; |
| 4896 | } |
| 4897 | |
| 4898 | |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4899 | //------------------------------------------------------------------ |
| 4900 | // Process STDIO |
| 4901 | //------------------------------------------------------------------ |
| 4902 | |
| 4903 | size_t |
| 4904 | Process::GetSTDOUT (char *buf, size_t buf_size, Error &error) |
| 4905 | { |
| 4906 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 4907 | size_t bytes_available = m_stdout_data.size(); |
| 4908 | if (bytes_available > 0) |
| 4909 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4910 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4911 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4912 | log->Printf ("Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")", |
| 4913 | static_cast<void*>(buf), |
| 4914 | static_cast<uint64_t>(buf_size)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4915 | if (bytes_available > buf_size) |
| 4916 | { |
| 4917 | memcpy(buf, m_stdout_data.c_str(), buf_size); |
| 4918 | m_stdout_data.erase(0, buf_size); |
| 4919 | bytes_available = buf_size; |
| 4920 | } |
| 4921 | else |
| 4922 | { |
| 4923 | memcpy(buf, m_stdout_data.c_str(), bytes_available); |
| 4924 | m_stdout_data.clear(); |
| 4925 | } |
| 4926 | } |
| 4927 | return bytes_available; |
| 4928 | } |
| 4929 | |
| 4930 | |
| 4931 | size_t |
| 4932 | Process::GetSTDERR (char *buf, size_t buf_size, Error &error) |
| 4933 | { |
| 4934 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 4935 | size_t bytes_available = m_stderr_data.size(); |
| 4936 | if (bytes_available > 0) |
| 4937 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 4938 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4939 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4940 | log->Printf ("Process::GetSTDERR (buf = %p, size = %" PRIu64 ")", |
| 4941 | static_cast<void*>(buf), |
| 4942 | static_cast<uint64_t>(buf_size)); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4943 | if (bytes_available > buf_size) |
| 4944 | { |
| 4945 | memcpy(buf, m_stderr_data.c_str(), buf_size); |
| 4946 | m_stderr_data.erase(0, buf_size); |
| 4947 | bytes_available = buf_size; |
| 4948 | } |
| 4949 | else |
| 4950 | { |
| 4951 | memcpy(buf, m_stderr_data.c_str(), bytes_available); |
| 4952 | m_stderr_data.clear(); |
| 4953 | } |
| 4954 | } |
| 4955 | return bytes_available; |
| 4956 | } |
| 4957 | |
| 4958 | void |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4959 | Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len) |
| 4960 | { |
| 4961 | Process *process = (Process *) baton; |
| 4962 | process->AppendSTDOUT (static_cast<const char *>(src), src_len); |
| 4963 | } |
| 4964 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4965 | class IOHandlerProcessSTDIO : |
| 4966 | public IOHandler |
| 4967 | { |
| 4968 | public: |
| 4969 | IOHandlerProcessSTDIO (Process *process, |
| 4970 | int write_fd) : |
Kate Stone | e30f11d | 2014-11-17 19:06:59 +0000 | [diff] [blame] | 4971 | IOHandler(process->GetTarget().GetDebugger(), IOHandler::Type::ProcessIO), |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4972 | m_process (process), |
| 4973 | m_read_file (), |
| 4974 | m_write_file (write_fd, false), |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 4975 | m_pipe () |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4976 | { |
| 4977 | m_read_file.SetDescriptor(GetInputFD(), false); |
| 4978 | } |
| 4979 | |
| 4980 | virtual |
| 4981 | ~IOHandlerProcessSTDIO () |
| 4982 | { |
| 4983 | |
| 4984 | } |
| 4985 | |
| 4986 | bool |
| 4987 | OpenPipes () |
| 4988 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 4989 | if (m_pipe.CanRead() && m_pipe.CanWrite()) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4990 | return true; |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 4991 | Error result = m_pipe.CreateNew(false); |
| 4992 | return result.Success(); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
| 4995 | void |
| 4996 | ClosePipes() |
| 4997 | { |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 4998 | m_pipe.Close(); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4999 | } |
| 5000 | |
| 5001 | // Each IOHandler gets to run until it is done. It should read data |
| 5002 | // from the "in" and place output into "out" and "err and return |
| 5003 | // when done. |
| 5004 | virtual void |
| 5005 | Run () |
| 5006 | { |
| 5007 | if (m_read_file.IsValid() && m_write_file.IsValid()) |
| 5008 | { |
| 5009 | SetIsDone(false); |
| 5010 | if (OpenPipes()) |
| 5011 | { |
| 5012 | const int read_fd = m_read_file.GetDescriptor(); |
Zachary Turner | 49be160 | 2015-02-25 19:52:41 +0000 | [diff] [blame] | 5013 | const int pipe_read_fd = m_pipe.GetReadFileDescriptor(); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5014 | TerminalState terminal_state; |
| 5015 | terminal_state.Save (read_fd, false); |
| 5016 | Terminal terminal(read_fd); |
| 5017 | terminal.SetCanonical(false); |
| 5018 | terminal.SetEcho(false); |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 5019 | // FD_ZERO, FD_SET are not supported on windows |
Hafiz Abid Qadeer | 6eff101 | 2014-03-12 10:45:23 +0000 | [diff] [blame] | 5020 | #ifndef _WIN32 |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5021 | while (!GetIsDone()) |
| 5022 | { |
| 5023 | fd_set read_fdset; |
| 5024 | FD_ZERO (&read_fdset); |
| 5025 | FD_SET (read_fd, &read_fdset); |
| 5026 | FD_SET (pipe_read_fd, &read_fdset); |
| 5027 | const int nfds = std::max<int>(read_fd, pipe_read_fd) + 1; |
| 5028 | int num_set_fds = select (nfds, &read_fdset, NULL, NULL, NULL); |
| 5029 | if (num_set_fds < 0) |
| 5030 | { |
| 5031 | const int select_errno = errno; |
| 5032 | |
| 5033 | if (select_errno != EINTR) |
| 5034 | SetIsDone(true); |
| 5035 | } |
| 5036 | else if (num_set_fds > 0) |
| 5037 | { |
| 5038 | char ch = 0; |
| 5039 | size_t n; |
| 5040 | if (FD_ISSET (read_fd, &read_fdset)) |
| 5041 | { |
| 5042 | n = 1; |
| 5043 | if (m_read_file.Read(&ch, n).Success() && n == 1) |
| 5044 | { |
| 5045 | if (m_write_file.Write(&ch, n).Fail() || n != 1) |
| 5046 | SetIsDone(true); |
| 5047 | } |
| 5048 | else |
| 5049 | SetIsDone(true); |
| 5050 | } |
| 5051 | if (FD_ISSET (pipe_read_fd, &read_fdset)) |
| 5052 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 5053 | size_t bytes_read; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5054 | // Consume the interrupt byte |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 5055 | Error error = m_pipe.Read(&ch, 1, bytes_read); |
| 5056 | if (error.Success()) |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 5057 | { |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 5058 | switch (ch) |
| 5059 | { |
| 5060 | case 'q': |
| 5061 | SetIsDone(true); |
| 5062 | break; |
| 5063 | case 'i': |
| 5064 | if (StateIsRunningState(m_process->GetState())) |
| 5065 | m_process->Halt(); |
| 5066 | break; |
| 5067 | } |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 5068 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5069 | } |
| 5070 | } |
| 5071 | } |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 5072 | #endif |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5073 | terminal_state.Restore(); |
| 5074 | |
| 5075 | } |
| 5076 | else |
| 5077 | SetIsDone(true); |
| 5078 | } |
| 5079 | else |
| 5080 | SetIsDone(true); |
| 5081 | } |
| 5082 | |
| 5083 | // Hide any characters that have been displayed so far so async |
| 5084 | // output can be displayed. Refresh() will be called after the |
| 5085 | // output has been displayed. |
| 5086 | virtual void |
| 5087 | Hide () |
| 5088 | { |
| 5089 | |
| 5090 | } |
| 5091 | // Called when the async output has been received in order to update |
| 5092 | // the input reader (refresh the prompt and redisplay any current |
| 5093 | // line(s) that are being edited |
| 5094 | virtual void |
| 5095 | Refresh () |
| 5096 | { |
| 5097 | |
| 5098 | } |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 5099 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5100 | virtual void |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 5101 | Cancel () |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5102 | { |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 5103 | char ch = 'q'; // Send 'q' for quit |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 5104 | size_t bytes_written = 0; |
| 5105 | m_pipe.Write(&ch, 1, bytes_written); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5106 | } |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 5107 | |
Greg Clayton | f0066ad | 2014-05-02 00:45:31 +0000 | [diff] [blame] | 5108 | virtual bool |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 5109 | Interrupt () |
| 5110 | { |
Greg Clayton | 19e1135 | 2014-02-26 22:47:33 +0000 | [diff] [blame] | 5111 | // Do only things that are safe to do in an interrupt context (like in |
| 5112 | // a SIGINT handler), like write 1 byte to a file descriptor. This will |
| 5113 | // interrupt the IOHandlerProcessSTDIO::Run() and we can look at the byte |
| 5114 | // that was written to the pipe and then call m_process->Halt() from a |
| 5115 | // much safer location in code. |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 5116 | if (m_active) |
| 5117 | { |
| 5118 | char ch = 'i'; // Send 'i' for interrupt |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 5119 | size_t bytes_written = 0; |
| 5120 | Error result = m_pipe.Write(&ch, 1, bytes_written); |
| 5121 | return result.Success(); |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 5122 | } |
| 5123 | else |
| 5124 | { |
| 5125 | // This IOHandler might be pushed on the stack, but not being run currently |
| 5126 | // so do the right thing if we aren't actively watching for STDIN by sending |
| 5127 | // the interrupt to the process. Otherwise the write to the pipe above would |
| 5128 | // do nothing. This can happen when the command interpreter is running and |
| 5129 | // gets a "expression ...". It will be on the IOHandler thread and sending |
| 5130 | // the input is complete to the delegate which will cause the expression to |
| 5131 | // run, which will push the process IO handler, but not run it. |
| 5132 | |
| 5133 | if (StateIsRunningState(m_process->GetState())) |
| 5134 | { |
| 5135 | m_process->SendAsyncInterrupt(); |
| 5136 | return true; |
| 5137 | } |
| 5138 | } |
| 5139 | return false; |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 5140 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5141 | |
| 5142 | virtual void |
| 5143 | GotEOF() |
| 5144 | { |
| 5145 | |
| 5146 | } |
| 5147 | |
| 5148 | protected: |
| 5149 | Process *m_process; |
| 5150 | File m_read_file; // Read from this file (usually actual STDIN for LLDB |
| 5151 | 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] | 5152 | Pipe m_pipe; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5153 | }; |
| 5154 | |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5155 | void |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5156 | Process::SetSTDIOFileDescriptor (int fd) |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5157 | { |
| 5158 | // First set up the Read Thread for reading/handling process I/O |
| 5159 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5160 | std::unique_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (fd, true)); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5161 | |
| 5162 | if (conn_ap.get()) |
| 5163 | { |
| 5164 | m_stdio_communication.SetConnection (conn_ap.release()); |
| 5165 | if (m_stdio_communication.IsConnected()) |
| 5166 | { |
| 5167 | m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this); |
| 5168 | m_stdio_communication.StartReadThread(); |
| 5169 | |
| 5170 | // Now read thread is set up, set up input reader. |
| 5171 | |
| 5172 | if (!m_process_input_reader.get()) |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5173 | m_process_input_reader.reset (new IOHandlerProcessSTDIO (this, fd)); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5174 | } |
| 5175 | } |
| 5176 | } |
| 5177 | |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 5178 | bool |
Greg Clayton | 6fea17e | 2014-03-03 19:15:20 +0000 | [diff] [blame] | 5179 | Process::ProcessIOHandlerIsActive () |
| 5180 | { |
| 5181 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 5182 | if (io_handler_sp) |
| 5183 | return m_target.GetDebugger().IsTopIOHandler (io_handler_sp); |
| 5184 | return false; |
| 5185 | } |
| 5186 | bool |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5187 | Process::PushProcessIOHandler () |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5188 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5189 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 5190 | if (io_handler_sp) |
| 5191 | { |
| 5192 | io_handler_sp->SetIsDone(false); |
| 5193 | m_target.GetDebugger().PushIOHandler (io_handler_sp); |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 5194 | return true; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5195 | } |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 5196 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 5199 | bool |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5200 | Process::PopProcessIOHandler () |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5201 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 5202 | IOHandlerSP io_handler_sp (m_process_input_reader); |
| 5203 | if (io_handler_sp) |
Greg Clayton | b4874f1 | 2014-02-28 18:22:24 +0000 | [diff] [blame] | 5204 | return m_target.GetDebugger().PopIOHandler (io_handler_sp); |
| 5205 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 5206 | } |
| 5207 | |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 5208 | // The process needs to know about installed plug-ins |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 5209 | void |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 5210 | Process::SettingsInitialize () |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 5211 | { |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 5212 | Thread::SettingsInitialize (); |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 5213 | } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 5214 | |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 5215 | void |
Caroline Tice | 20bd37f | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 5216 | Process::SettingsTerminate () |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 5217 | { |
Greg Clayton | 6920b52 | 2012-08-22 18:39:03 +0000 | [diff] [blame] | 5218 | Thread::SettingsTerminate (); |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 5219 | } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 5220 | |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 5221 | ExpressionResults |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5222 | Process::RunThreadPlan (ExecutionContext &exe_ctx, |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5223 | lldb::ThreadPlanSP &thread_plan_sp, |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5224 | const EvaluateExpressionOptions &options, |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5225 | Stream &errors) |
| 5226 | { |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5227 | ExpressionResults return_value = eExpressionSetupError; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5228 | |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 5229 | if (thread_plan_sp.get() == NULL) |
| 5230 | { |
| 5231 | errors.Printf("RunThreadPlan called with empty thread plan."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5232 | return eExpressionSetupError; |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 5233 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5234 | |
Jim Ingham | 7d7931d | 2013-03-28 00:05:34 +0000 | [diff] [blame] | 5235 | if (!thread_plan_sp->ValidatePlan(NULL)) |
| 5236 | { |
| 5237 | errors.Printf ("RunThreadPlan called with an invalid thread plan."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5238 | return eExpressionSetupError; |
Jim Ingham | 7d7931d | 2013-03-28 00:05:34 +0000 | [diff] [blame] | 5239 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5240 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5241 | if (exe_ctx.GetProcessPtr() != this) |
| 5242 | { |
| 5243 | errors.Printf("RunThreadPlan called on wrong process."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5244 | return eExpressionSetupError; |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | Thread *thread = exe_ctx.GetThreadPtr(); |
| 5248 | if (thread == NULL) |
| 5249 | { |
| 5250 | errors.Printf("RunThreadPlan called with invalid thread."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5251 | return eExpressionSetupError; |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5252 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5253 | |
Jim Ingham | 17e5c4e | 2011-05-17 22:24:54 +0000 | [diff] [blame] | 5254 | // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes. |
| 5255 | // For that to be true the plan can't be private - since private plans suppress themselves in the |
| 5256 | // GetCompletedPlan call. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5257 | |
Jim Ingham | 17e5c4e | 2011-05-17 22:24:54 +0000 | [diff] [blame] | 5258 | bool orig_plan_private = thread_plan_sp->GetPrivate(); |
| 5259 | thread_plan_sp->SetPrivate(false); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5260 | |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 5261 | if (m_private_state.GetValue() != eStateStopped) |
| 5262 | { |
| 5263 | errors.Printf ("RunThreadPlan called while the private state was not stopped."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5264 | return eExpressionSetupError; |
Jim Ingham | 444586b | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 5265 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5266 | |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 5267 | // 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] | 5268 | const uint32_t thread_idx_id = thread->GetIndexID(); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 5269 | StackFrameSP selected_frame_sp = thread->GetSelectedFrame(); |
Jim Ingham | 11b0e05 | 2013-02-19 23:22:45 +0000 | [diff] [blame] | 5270 | if (!selected_frame_sp) |
| 5271 | { |
| 5272 | thread->SetSelectedFrame(0); |
| 5273 | selected_frame_sp = thread->GetSelectedFrame(); |
| 5274 | if (!selected_frame_sp) |
| 5275 | { |
| 5276 | 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] | 5277 | return eExpressionSetupError; |
Jim Ingham | 11b0e05 | 2013-02-19 23:22:45 +0000 | [diff] [blame] | 5278 | } |
| 5279 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5280 | |
Jim Ingham | 11b0e05 | 2013-02-19 23:22:45 +0000 | [diff] [blame] | 5281 | StackID ctx_frame_id = selected_frame_sp->GetStackID(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5282 | |
| 5283 | // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either, |
| 5284 | // so we should arrange to reset them as well. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5285 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 5286 | lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5287 | |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 5288 | uint32_t selected_tid; |
| 5289 | StackID selected_stack_id; |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 5290 | if (selected_thread_sp) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5291 | { |
| 5292 | selected_tid = selected_thread_sp->GetIndexID(); |
Jim Ingham | 6624384 | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 5293 | selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID(); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5294 | } |
| 5295 | else |
| 5296 | { |
| 5297 | selected_tid = LLDB_INVALID_THREAD_ID; |
| 5298 | } |
| 5299 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 5300 | HostThread backup_private_state_thread; |
Jason Molenda | 76513fd | 2014-10-15 23:39:31 +0000 | [diff] [blame] | 5301 | lldb::StateType old_state = eStateInvalid; |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5302 | lldb::ThreadPlanSP stopper_base_plan_sp; |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5303 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 5304 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 5305 | if (m_private_state_thread.EqualsThread(Host::GetCurrentThread())) |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5306 | { |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5307 | // Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since |
| 5308 | // we are the thread that is generating public events. |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5309 | // 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] | 5310 | // we are fielding public events here. |
| 5311 | if (log) |
Jason Molenda | d251c9d | 2012-11-17 01:41:04 +0000 | [diff] [blame] | 5312 | 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] | 5313 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5314 | backup_private_state_thread = m_private_state_thread; |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5315 | |
| 5316 | // One other bit of business: we want to run just this thread plan and anything it pushes, and then stop, |
| 5317 | // returning control here. |
| 5318 | // But in the normal course of things, the plan above us on the stack would be given a shot at the stop |
| 5319 | // event before deciding to stop, and we don't want that. So we insert a "stopper" base plan on the stack |
| 5320 | // before the plan we want to run. Since base plans always stop and return control to the user, that will |
| 5321 | // do just what we want. |
| 5322 | stopper_base_plan_sp.reset(new ThreadPlanBase (*thread)); |
| 5323 | thread->QueueThreadPlan (stopper_base_plan_sp, false); |
| 5324 | // Have to make sure our public state is stopped, since otherwise the reporting logic below doesn't work correctly. |
| 5325 | old_state = m_public_state.GetValue(); |
| 5326 | m_public_state.SetValueNoLock(eStateStopped); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5327 | |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 5328 | // Now spin up the private state thread: |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5329 | StartPrivateStateThread(true); |
| 5330 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5331 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 5332 | 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] | 5333 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5334 | if (options.GetDebug()) |
| 5335 | { |
| 5336 | // In this case, we aren't actually going to run, we just want to stop right away. |
| 5337 | // Flush this thread so we will refetch the stacks and show the correct backtrace. |
| 5338 | // FIXME: To make this prettier we should invent some stop reason for this, but that |
| 5339 | // is only cosmetic, and this functionality is only of use to lldb developers who can |
| 5340 | // live with not pretty... |
| 5341 | thread->Flush(); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5342 | return eExpressionStoppedForDebug; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5343 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5344 | |
Jim Ingham | 1e7a9ee | 2011-01-23 21:14:08 +0000 | [diff] [blame] | 5345 | Listener listener("lldb.process.listener.run-thread-plan"); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5346 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5347 | lldb::EventSP event_to_broadcast_sp; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5348 | |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 5349 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5350 | // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get |
| 5351 | // restored on exit to the function. |
| 5352 | // |
| 5353 | // If the event needs to propagate beyond the hijacker (e.g., the process exits during execution), then the event |
| 5354 | // is put into event_to_broadcast_sp for rebroadcasting. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5355 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5356 | ProcessEventHijacker run_thread_plan_hijacker (*this, &listener); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5357 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 5358 | if (log) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5359 | { |
| 5360 | StreamString s; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5361 | thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 5362 | 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] | 5363 | thread->GetIndexID(), |
| 5364 | thread->GetID(), |
| 5365 | s.GetData()); |
| 5366 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5367 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5368 | bool got_event; |
| 5369 | lldb::EventSP event_sp; |
| 5370 | lldb::StateType stop_state = lldb::eStateInvalid; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5371 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5372 | TimeValue* timeout_ptr = NULL; |
| 5373 | TimeValue real_timeout; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5374 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5375 | 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] | 5376 | bool do_resume = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5377 | bool handle_running_event = true; |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5378 | const uint64_t default_one_thread_timeout_usec = 250000; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5379 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5380 | // This is just for accounting: |
| 5381 | uint32_t num_resumes = 0; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5382 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5383 | uint32_t timeout_usec = options.GetTimeoutUsec(); |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5384 | uint32_t one_thread_timeout_usec; |
| 5385 | uint32_t all_threads_timeout_usec = 0; |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5386 | |
| 5387 | // If we are going to run all threads the whole time, or if we are only going to run one thread, |
| 5388 | // then we don't need the first timeout. So we set the final timeout, and pretend we are after the |
| 5389 | // first timeout already. |
| 5390 | |
| 5391 | if (!options.GetStopOthers() || !options.GetTryAllThreads()) |
Jim Ingham | 286fb1e | 2014-02-28 02:52:06 +0000 | [diff] [blame] | 5392 | { |
| 5393 | before_first_timeout = false; |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5394 | one_thread_timeout_usec = 0; |
| 5395 | all_threads_timeout_usec = timeout_usec; |
Jim Ingham | 286fb1e | 2014-02-28 02:52:06 +0000 | [diff] [blame] | 5396 | } |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5397 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5398 | { |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5399 | uint32_t option_one_thread_timeout = options.GetOneThreadTimeoutUsec(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5400 | |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5401 | // If the overall wait is forever, then we only need to set the one thread timeout: |
| 5402 | if (timeout_usec == 0) |
| 5403 | { |
Ed Maste | 801335c | 2014-03-31 19:28:14 +0000 | [diff] [blame] | 5404 | if (option_one_thread_timeout != 0) |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5405 | one_thread_timeout_usec = option_one_thread_timeout; |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5406 | else |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5407 | one_thread_timeout_usec = default_one_thread_timeout_usec; |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5408 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5409 | else |
| 5410 | { |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5411 | // Otherwise, if the one thread timeout is set, make sure it isn't longer than the overall timeout, |
| 5412 | // and use it, otherwise use half the total timeout, bounded by the default_one_thread_timeout_usec. |
| 5413 | uint64_t computed_one_thread_timeout; |
| 5414 | if (option_one_thread_timeout != 0) |
| 5415 | { |
| 5416 | if (timeout_usec < option_one_thread_timeout) |
| 5417 | { |
| 5418 | errors.Printf("RunThreadPlan called without one thread timeout greater than total timeout"); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5419 | return eExpressionSetupError; |
Jim Ingham | 914f4e7 | 2014-03-28 21:58:28 +0000 | [diff] [blame] | 5420 | } |
| 5421 | computed_one_thread_timeout = option_one_thread_timeout; |
| 5422 | } |
| 5423 | else |
| 5424 | { |
| 5425 | computed_one_thread_timeout = timeout_usec / 2; |
| 5426 | if (computed_one_thread_timeout > default_one_thread_timeout_usec) |
| 5427 | computed_one_thread_timeout = default_one_thread_timeout_usec; |
| 5428 | } |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5429 | one_thread_timeout_usec = computed_one_thread_timeout; |
| 5430 | all_threads_timeout_usec = timeout_usec - one_thread_timeout_usec; |
| 5431 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5432 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5433 | } |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5434 | |
| 5435 | if (log) |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5436 | 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] | 5437 | options.GetStopOthers(), |
| 5438 | options.GetTryAllThreads(), |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5439 | before_first_timeout, |
| 5440 | one_thread_timeout_usec, |
| 5441 | all_threads_timeout_usec); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5442 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5443 | // This isn't going to work if there are unfetched events on the queue. |
| 5444 | // Are there cases where we might want to run the remaining events here, and then try to |
| 5445 | // 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] | 5446 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5447 | Event *other_events = listener.PeekAtNextEvent(); |
| 5448 | if (other_events != NULL) |
| 5449 | { |
| 5450 | errors.Printf("Calling RunThreadPlan with pending events on the queue."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5451 | return eExpressionSetupError; |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5452 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5453 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5454 | // We also need to make sure that the next event is delivered. We might be calling a function as part of |
| 5455 | // a thread plan, in which case the last delivered event could be the running event, and we don't want |
| 5456 | // event coalescing to cause us to lose OUR running event... |
| 5457 | ForceNextEventDelivery(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5458 | |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 5459 | // This while loop must exit out the bottom, there's cleanup that we need to do when we are done. |
| 5460 | // So don't call return anywhere within it. |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 5461 | |
| 5462 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
| 5463 | // It's pretty much impossible to write test cases for things like: |
| 5464 | // One thread timeout expires, I go to halt, but the process already stopped |
| 5465 | // on the function call stop breakpoint. Turning on this define will make us not |
| 5466 | // fetch the first event till after the halt. So if you run a quick function, it will have |
| 5467 | // completed, and the completion event will be waiting, when you interrupt for halt. |
| 5468 | // The expression evaluation should still succeed. |
| 5469 | bool miss_first_event = true; |
| 5470 | #endif |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5471 | TimeValue one_thread_timeout; |
| 5472 | TimeValue final_timeout; |
| 5473 | |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 5474 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5475 | while (1) |
| 5476 | { |
| 5477 | // We usually want to resume the process if we get to the top of the loop. |
| 5478 | // The only exception is if we get two running events with no intervening |
| 5479 | // 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] | 5480 | if (log) |
| 5481 | log->Printf ("Top of while loop: do_resume: %i handle_running_event: %i before_first_timeout: %i.", |
| 5482 | do_resume, |
| 5483 | handle_running_event, |
| 5484 | before_first_timeout); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5485 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5486 | if (do_resume || handle_running_event) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5487 | { |
| 5488 | // 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] | 5489 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5490 | if (do_resume) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5491 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5492 | num_resumes++; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5493 | Error resume_error = PrivateResume (); |
| 5494 | if (!resume_error.Success()) |
| 5495 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5496 | errors.Printf("Error resuming inferior the %d time: \"%s\".\n", |
| 5497 | num_resumes, |
| 5498 | resume_error.AsCString()); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5499 | return_value = eExpressionSetupError; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5500 | break; |
| 5501 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5502 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5503 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5504 | TimeValue resume_timeout = TimeValue::Now(); |
| 5505 | resume_timeout.OffsetWithMicroSeconds(500000); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5506 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5507 | got_event = listener.WaitForEvent(&resume_timeout, event_sp); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5508 | if (!got_event) |
| 5509 | { |
| 5510 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5511 | log->Printf ("Process::RunThreadPlan(): didn't get any event after resume %d, exiting.", |
| 5512 | num_resumes); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5513 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5514 | 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] | 5515 | return_value = eExpressionSetupError; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5516 | break; |
| 5517 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5518 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5519 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5520 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5521 | if (stop_state != eStateRunning) |
| 5522 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5523 | bool restarted = false; |
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 (stop_state == eStateStopped) |
| 5526 | { |
| 5527 | restarted = Process::ProcessEventData::GetRestartedFromEvent(event_sp.get()); |
| 5528 | if (log) |
| 5529 | log->Printf("Process::RunThreadPlan(): didn't get running event after " |
| 5530 | "resume %d, got %s instead (restarted: %i, do_resume: %i, handle_running_event: %i).", |
| 5531 | num_resumes, |
| 5532 | StateAsCString(stop_state), |
| 5533 | restarted, |
| 5534 | do_resume, |
| 5535 | handle_running_event); |
| 5536 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5537 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5538 | if (restarted) |
| 5539 | { |
| 5540 | // This is probably an overabundance of caution, I don't think I should ever get a stopped & restarted |
| 5541 | // event here. But if I do, the best thing is to Halt and then get out of here. |
| 5542 | Halt(); |
| 5543 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5544 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5545 | errors.Printf("Didn't get running event after initial resume, got %s instead.", |
| 5546 | StateAsCString(stop_state)); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5547 | return_value = eExpressionSetupError; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5548 | break; |
| 5549 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5550 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5551 | if (log) |
| 5552 | log->PutCString ("Process::RunThreadPlan(): resuming succeeded."); |
| 5553 | // We need to call the function synchronously, so spin waiting for it to return. |
| 5554 | // If we get interrupted while executing, we're going to lose our context, and |
| 5555 | // won't be able to gather the result at this point. |
| 5556 | // We set the timeout AFTER the resume, since the resume takes some time and we |
| 5557 | // don't want to charge that to the timeout. |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5558 | } |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5559 | else |
| 5560 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5561 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5562 | log->PutCString ("Process::RunThreadPlan(): waiting for next event."); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5563 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5564 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5565 | if (before_first_timeout) |
| 5566 | { |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5567 | if (options.GetTryAllThreads()) |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5568 | { |
| 5569 | one_thread_timeout = TimeValue::Now(); |
| 5570 | one_thread_timeout.OffsetWithMicroSeconds(one_thread_timeout_usec); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5571 | timeout_ptr = &one_thread_timeout; |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5572 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5573 | else |
| 5574 | { |
| 5575 | if (timeout_usec == 0) |
| 5576 | timeout_ptr = NULL; |
| 5577 | else |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5578 | { |
| 5579 | final_timeout = TimeValue::Now(); |
| 5580 | final_timeout.OffsetWithMicroSeconds (timeout_usec); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5581 | timeout_ptr = &final_timeout; |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5582 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5583 | } |
| 5584 | } |
| 5585 | else |
| 5586 | { |
| 5587 | if (timeout_usec == 0) |
| 5588 | timeout_ptr = NULL; |
| 5589 | else |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5590 | { |
| 5591 | final_timeout = TimeValue::Now(); |
| 5592 | final_timeout.OffsetWithMicroSeconds (all_threads_timeout_usec); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5593 | timeout_ptr = &final_timeout; |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5594 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5595 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5596 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5597 | do_resume = true; |
| 5598 | handle_running_event = true; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5599 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5600 | // Now wait for the process to stop again: |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5601 | event_sp.reset(); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5602 | |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 5603 | if (log) |
Jim Ingham | 20829ac | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 5604 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5605 | if (timeout_ptr) |
| 5606 | { |
Matt Kopec | 676a487 | 2013-02-21 23:55:31 +0000 | [diff] [blame] | 5607 | 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] | 5608 | TimeValue::Now().GetAsMicroSecondsSinceJan1_1970(), |
| 5609 | timeout_ptr->GetAsMicroSecondsSinceJan1_1970()); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5610 | } |
Jim Ingham | 20829ac | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 5611 | else |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5612 | { |
| 5613 | log->Printf ("Process::RunThreadPlan(): about to wait forever."); |
| 5614 | } |
| 5615 | } |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 5616 | |
| 5617 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
| 5618 | // See comment above... |
| 5619 | if (miss_first_event) |
| 5620 | { |
| 5621 | usleep(1000); |
| 5622 | miss_first_event = false; |
| 5623 | got_event = false; |
| 5624 | } |
| 5625 | else |
| 5626 | #endif |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5627 | got_event = listener.WaitForEvent (timeout_ptr, event_sp); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5628 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5629 | if (got_event) |
| 5630 | { |
| 5631 | if (event_sp.get()) |
| 5632 | { |
| 5633 | bool keep_going = false; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5634 | if (event_sp->GetType() == eBroadcastBitInterrupt) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5635 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5636 | Halt(); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5637 | return_value = eExpressionInterrupted; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5638 | errors.Printf ("Execution halted by user interrupt."); |
| 5639 | if (log) |
| 5640 | log->Printf ("Process::RunThreadPlan(): Got interrupted by eBroadcastBitInterrupted, exiting."); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5641 | break; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5642 | } |
| 5643 | else |
| 5644 | { |
| 5645 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5646 | if (log) |
| 5647 | 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] | 5648 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5649 | switch (stop_state) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5650 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5651 | case lldb::eStateStopped: |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5652 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5653 | // We stopped, figure out what we are going to do now. |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5654 | ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id); |
| 5655 | if (!thread_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5656 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5657 | // Ooh, our thread has vanished. Unlikely that this was successful execution... |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5658 | if (log) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5659 | 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] | 5660 | return_value = eExpressionInterrupted; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5661 | } |
| 5662 | else |
| 5663 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5664 | // If we were restarted, we just need to go back up to fetch another event. |
| 5665 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5666 | { |
| 5667 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5668 | { |
| 5669 | log->Printf ("Process::RunThreadPlan(): Got a stop and restart, so we'll continue waiting."); |
| 5670 | } |
| 5671 | keep_going = true; |
| 5672 | do_resume = false; |
| 5673 | handle_running_event = true; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5674 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5675 | } |
| 5676 | else |
| 5677 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5678 | StopInfoSP stop_info_sp (thread_sp->GetStopInfo ()); |
| 5679 | StopReason stop_reason = eStopReasonInvalid; |
| 5680 | if (stop_info_sp) |
| 5681 | stop_reason = stop_info_sp->GetStopReason(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5682 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5683 | // FIXME: We only check if the stop reason is plan complete, should we make sure that |
| 5684 | // it is OUR plan that is complete? |
| 5685 | if (stop_reason == eStopReasonPlanComplete) |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5686 | { |
| 5687 | if (log) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5688 | log->PutCString ("Process::RunThreadPlan(): execution completed successfully."); |
| 5689 | // Now mark this plan as private so it doesn't get reported as the stop reason |
| 5690 | // after this point. |
| 5691 | if (thread_plan_sp) |
| 5692 | thread_plan_sp->SetPrivate (orig_plan_private); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5693 | return_value = eExpressionCompleted; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5694 | } |
| 5695 | else |
| 5696 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5697 | // 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] | 5698 | if (stop_reason == eStopReasonBreakpoint) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5699 | { |
| 5700 | if (log) |
| 5701 | log->Printf ("Process::RunThreadPlan() stopped for breakpoint: %s.", stop_info_sp->GetDescription()); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5702 | return_value = eExpressionHitBreakpoint; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5703 | if (!options.DoesIgnoreBreakpoints()) |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 5704 | { |
| 5705 | event_to_broadcast_sp = event_sp; |
| 5706 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5707 | } |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5708 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5709 | { |
| 5710 | if (log) |
| 5711 | log->PutCString ("Process::RunThreadPlan(): thread plan didn't successfully complete."); |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5712 | if (!options.DoesUnwindOnError()) |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 5713 | event_to_broadcast_sp = event_sp; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5714 | return_value = eExpressionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5715 | } |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5716 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5717 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5718 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5719 | } |
| 5720 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5721 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5722 | case lldb::eStateRunning: |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5723 | // This shouldn't really happen, but sometimes we do get two running events without an |
| 5724 | // 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] | 5725 | do_resume = false; |
| 5726 | keep_going = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5727 | handle_running_event = false; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5728 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5729 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5730 | default: |
| 5731 | if (log) |
| 5732 | 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] | 5733 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5734 | if (stop_state == eStateExited) |
| 5735 | event_to_broadcast_sp = event_sp; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5736 | |
Sean Callanan | bf154da | 2012-08-08 17:35:10 +0000 | [diff] [blame] | 5737 | errors.Printf ("Execution stopped with unexpected state.\n"); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5738 | return_value = eExpressionInterrupted; |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5739 | break; |
| 5740 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5741 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5742 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5743 | if (keep_going) |
| 5744 | continue; |
| 5745 | else |
| 5746 | break; |
| 5747 | } |
| 5748 | else |
| 5749 | { |
| 5750 | if (log) |
| 5751 | 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] | 5752 | return_value = eExpressionInterrupted; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5753 | break; |
| 5754 | } |
| 5755 | } |
| 5756 | else |
| 5757 | { |
| 5758 | // If we didn't get an event that means we've timed out... |
| 5759 | // We will interrupt the process here. Depending on what we were asked to do we will |
| 5760 | // either exit, or try with all threads running for the same timeout. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5761 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5762 | if (log) { |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5763 | if (options.GetTryAllThreads()) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5764 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5765 | if (before_first_timeout) |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5766 | { |
| 5767 | if (timeout_usec != 0) |
| 5768 | { |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5769 | log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, " |
Jim Ingham | fd95f89 | 2014-04-22 01:41:52 +0000 | [diff] [blame] | 5770 | "running for %" PRIu32 " usec with all threads enabled.", |
| 5771 | all_threads_timeout_usec); |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5772 | } |
| 5773 | else |
| 5774 | { |
| 5775 | log->Printf ("Process::RunThreadPlan(): Running function with one thread timeout timed out, " |
Ed Maste | e61c7b0 | 2014-04-29 17:48:06 +0000 | [diff] [blame] | 5776 | "running forever with all threads enabled."); |
Jim Ingham | fe1c342 | 2014-04-16 02:24:48 +0000 | [diff] [blame] | 5777 | } |
| 5778 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5779 | else |
| 5780 | log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled " |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5781 | "and timeout: %u timed out, abandoning execution.", |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5782 | timeout_usec); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5783 | } |
| 5784 | else |
Jason Molenda | 6a8658a | 2013-10-27 02:32:23 +0000 | [diff] [blame] | 5785 | log->Printf ("Process::RunThreadPlan(): Running function with timeout: %u timed out, " |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 5786 | "abandoning execution.", |
| 5787 | timeout_usec); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5788 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5789 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5790 | // It is possible that between the time we issued the Halt, and we get around to calling Halt the target |
| 5791 | // could have stopped. That's fine, Halt will figure that out and send the appropriate Stopped event. |
| 5792 | // BUT it is also possible that we stopped & restarted (e.g. hit a signal with "stop" set to false.) In |
| 5793 | // that case, we'll get the stopped & restarted event, and we should go back to waiting for the Halt's |
| 5794 | // stopped event. That's what this while loop does. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5795 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5796 | bool back_to_top = true; |
| 5797 | uint32_t try_halt_again = 0; |
| 5798 | bool do_halt = true; |
| 5799 | const uint32_t num_retries = 5; |
| 5800 | while (try_halt_again < num_retries) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5801 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5802 | Error halt_error; |
| 5803 | if (do_halt) |
| 5804 | { |
| 5805 | if (log) |
| 5806 | log->Printf ("Process::RunThreadPlan(): Running Halt."); |
| 5807 | halt_error = Halt(); |
| 5808 | } |
| 5809 | if (halt_error.Success()) |
| 5810 | { |
| 5811 | if (log) |
| 5812 | log->PutCString ("Process::RunThreadPlan(): Halt succeeded."); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5813 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5814 | real_timeout = TimeValue::Now(); |
| 5815 | real_timeout.OffsetWithMicroSeconds(500000); |
| 5816 | |
| 5817 | got_event = listener.WaitForEvent(&real_timeout, event_sp); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5818 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5819 | if (got_event) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5820 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5821 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5822 | if (log) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5823 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5824 | log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state)); |
| 5825 | if (stop_state == lldb::eStateStopped |
| 5826 | && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get())) |
| 5827 | log->PutCString (" Event was the Halt interruption event."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5828 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5829 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5830 | if (stop_state == lldb::eStateStopped) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5831 | { |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5832 | // Between the time we initiated the Halt and the time we delivered it, the process could have |
| 5833 | // already finished its job. Check that here: |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5834 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5835 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
| 5836 | { |
| 5837 | if (log) |
| 5838 | log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. " |
| 5839 | "Exiting wait loop."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5840 | return_value = eExpressionCompleted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5841 | back_to_top = false; |
| 5842 | break; |
| 5843 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5844 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5845 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 5846 | { |
| 5847 | if (log) |
| 5848 | log->PutCString ("Process::RunThreadPlan(): Went to halt but got a restarted event, there must be an un-restarted stopped event so try again... " |
| 5849 | "Exiting wait loop."); |
| 5850 | try_halt_again++; |
| 5851 | do_halt = false; |
| 5852 | continue; |
| 5853 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5854 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 5855 | if (!options.GetTryAllThreads()) |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5856 | { |
| 5857 | if (log) |
| 5858 | 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] | 5859 | return_value = eExpressionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5860 | back_to_top = false; |
| 5861 | break; |
| 5862 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5863 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5864 | if (before_first_timeout) |
| 5865 | { |
| 5866 | // Set all the other threads to run, and return to the top of the loop, which will continue; |
| 5867 | before_first_timeout = false; |
| 5868 | thread_plan_sp->SetStopOthers (false); |
| 5869 | if (log) |
| 5870 | log->PutCString ("Process::RunThreadPlan(): about to resume."); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5871 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5872 | back_to_top = true; |
| 5873 | break; |
| 5874 | } |
| 5875 | else |
| 5876 | { |
| 5877 | // Running all threads failed, so return Interrupted. |
| 5878 | if (log) |
| 5879 | log->PutCString("Process::RunThreadPlan(): running all threads timed out."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5880 | return_value = eExpressionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5881 | back_to_top = false; |
| 5882 | break; |
| 5883 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5884 | } |
| 5885 | } |
| 5886 | else |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5887 | { if (log) |
| 5888 | log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. " |
| 5889 | "I'm getting out of here passing Interrupted."); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5890 | return_value = eExpressionInterrupted; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5891 | back_to_top = false; |
| 5892 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5893 | } |
| 5894 | } |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5895 | else |
| 5896 | { |
| 5897 | try_halt_again++; |
| 5898 | continue; |
| 5899 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5900 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5901 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 5902 | if (!back_to_top || try_halt_again > num_retries) |
| 5903 | break; |
| 5904 | else |
| 5905 | continue; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5906 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5907 | } // END WAIT LOOP |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5908 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5909 | // If we had to start up a temporary private state thread to run this thread plan, shut it down now. |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 5910 | if (backup_private_state_thread.IsJoinable()) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5911 | { |
| 5912 | StopPrivateStateThread(); |
| 5913 | Error error; |
| 5914 | m_private_state_thread = backup_private_state_thread; |
Sean Callanan | 9a02851 | 2012-08-09 00:50:26 +0000 | [diff] [blame] | 5915 | if (stopper_base_plan_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5916 | { |
| 5917 | thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp); |
| 5918 | } |
Jason Molenda | 76513fd | 2014-10-15 23:39:31 +0000 | [diff] [blame] | 5919 | if (old_state != eStateInvalid) |
| 5920 | m_public_state.SetValueNoLock(old_state); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5921 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5922 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5923 | // Restore the thread state if we are going to discard the plan execution. There are three cases where this |
| 5924 | // could happen: |
| 5925 | // 1) The execution successfully completed |
| 5926 | // 2) We hit a breakpoint, and ignore_breakpoints was true |
| 5927 | // 3) We got some other error, and discard_on_error was true |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5928 | bool should_unwind = (return_value == eExpressionInterrupted && options.DoesUnwindOnError()) |
| 5929 | || (return_value == eExpressionHitBreakpoint && options.DoesIgnoreBreakpoints()); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5930 | |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5931 | if (return_value == eExpressionCompleted |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 5932 | || should_unwind) |
Jim Ingham | 8559a35 | 2012-11-26 23:52:18 +0000 | [diff] [blame] | 5933 | { |
| 5934 | thread_plan_sp->RestoreThreadState(); |
| 5935 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5936 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5937 | // Now do some processing on the results of the run: |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 5938 | if (return_value == eExpressionInterrupted || return_value == eExpressionHitBreakpoint) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5939 | { |
| 5940 | if (log) |
| 5941 | { |
| 5942 | StreamString s; |
| 5943 | if (event_sp) |
| 5944 | event_sp->Dump (&s); |
| 5945 | else |
| 5946 | { |
| 5947 | log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL."); |
| 5948 | } |
| 5949 | |
| 5950 | StreamString ts; |
| 5951 | |
| 5952 | const char *event_explanation = NULL; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5953 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5954 | do |
| 5955 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5956 | if (!event_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5957 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5958 | event_explanation = "<no event>"; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5959 | break; |
| 5960 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5961 | else if (event_sp->GetType() == eBroadcastBitInterrupt) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5962 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5963 | event_explanation = "<user interrupt>"; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5964 | break; |
| 5965 | } |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5966 | else |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5967 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5968 | const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get()); |
| 5969 | |
| 5970 | if (!event_data) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5971 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5972 | event_explanation = "<no event data>"; |
| 5973 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5974 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5975 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5976 | Process *process = event_data->GetProcessSP().get(); |
| 5977 | |
| 5978 | if (!process) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5979 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5980 | event_explanation = "<no process>"; |
| 5981 | break; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 5982 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5983 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5984 | ThreadList &thread_list = process->GetThreadList(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5985 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5986 | uint32_t num_threads = thread_list.GetSize(); |
| 5987 | uint32_t thread_index; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5988 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5989 | ts.Printf("<%u threads> ", num_threads); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5990 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5991 | for (thread_index = 0; |
| 5992 | thread_index < num_threads; |
| 5993 | ++thread_index) |
| 5994 | { |
| 5995 | Thread *thread = thread_list.GetThreadAtIndex(thread_index).get(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 5996 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 5997 | if (!thread) |
| 5998 | { |
| 5999 | ts.Printf("<?> "); |
| 6000 | continue; |
| 6001 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6002 | |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 6003 | ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 6004 | RegisterContext *register_context = thread->GetRegisterContext().get(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6005 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 6006 | if (register_context) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 6007 | ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC()); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 6008 | else |
| 6009 | ts.Printf("[ip unknown] "); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6010 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 6011 | lldb::StopInfoSP stop_info_sp = thread->GetStopInfo(); |
| 6012 | if (stop_info_sp) |
| 6013 | { |
| 6014 | const char *stop_desc = stop_info_sp->GetDescription(); |
| 6015 | if (stop_desc) |
| 6016 | ts.PutCString (stop_desc); |
| 6017 | } |
| 6018 | ts.Printf(">"); |
| 6019 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6020 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 6021 | event_explanation = ts.GetData(); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6022 | } |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6023 | } while (0); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6024 | |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 6025 | if (event_explanation) |
| 6026 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6027 | else |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 6028 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData()); |
| 6029 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6030 | |
Jim Ingham | e4483cf | 2013-09-27 01:13:01 +0000 | [diff] [blame] | 6031 | if (should_unwind) |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 6032 | { |
| 6033 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6034 | log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - discarding thread plans up to %p.", |
| 6035 | static_cast<void*>(thread_plan_sp.get())); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 6036 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
| 6037 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 6038 | } |
| 6039 | else |
| 6040 | { |
| 6041 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6042 | log->Printf ("Process::RunThreadPlan: ExecutionInterrupted - for plan: %p not discarding.", |
| 6043 | static_cast<void*>(thread_plan_sp.get())); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6044 | } |
| 6045 | } |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6046 | else if (return_value == eExpressionSetupError) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6047 | { |
| 6048 | if (log) |
| 6049 | log->PutCString("Process::RunThreadPlan(): execution set up error."); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6050 | |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 6051 | if (options.DoesUnwindOnError()) |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 6052 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 6053 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 6054 | thread_plan_sp->SetPrivate (orig_plan_private); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 6055 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6056 | } |
| 6057 | else |
| 6058 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6059 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6060 | { |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 6061 | if (log) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6062 | log->PutCString("Process::RunThreadPlan(): thread plan is done"); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6063 | return_value = eExpressionCompleted; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6064 | } |
| 6065 | else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get())) |
| 6066 | { |
| 6067 | if (log) |
| 6068 | log->PutCString("Process::RunThreadPlan(): thread plan was discarded"); |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6069 | return_value = eExpressionDiscarded; |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6070 | } |
| 6071 | else |
| 6072 | { |
| 6073 | if (log) |
| 6074 | log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course"); |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 6075 | if (options.DoesUnwindOnError() && thread_plan_sp) |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6076 | { |
| 6077 | if (log) |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 6078 | 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] | 6079 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
| 6080 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 6081 | } |
| 6082 | } |
| 6083 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6084 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6085 | // Thread we ran the function in may have gone away because we ran the target |
| 6086 | // Check that it's still there, and if it is put it back in the context. Also restore the |
| 6087 | // frame in the context if it is still present. |
| 6088 | thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get(); |
| 6089 | if (thread) |
| 6090 | { |
| 6091 | exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id)); |
| 6092 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6093 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6094 | // Also restore the current process'es selected frame & thread, since this function calling may |
| 6095 | // be done behind the user's back. |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6096 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6097 | if (selected_tid != LLDB_INVALID_THREAD_ID) |
| 6098 | { |
| 6099 | if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid()) |
| 6100 | { |
| 6101 | // We were able to restore the selected thread, now restore the frame: |
Daniel Malea | a012d3a | 2013-07-31 20:21:20 +0000 | [diff] [blame] | 6102 | Mutex::Locker lock(GetThreadList().GetMutex()); |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 6103 | StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id); |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6104 | if (old_frame_sp) |
| 6105 | GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get()); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6106 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6107 | } |
| 6108 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6109 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6110 | // 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] | 6111 | |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6112 | if (event_to_broadcast_sp) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6113 | { |
Sean Callanan | a46ec45 | 2012-07-11 21:31:24 +0000 | [diff] [blame] | 6114 | if (log) |
| 6115 | log->PutCString("Process::RunThreadPlan(): rebroadcasting event."); |
| 6116 | BroadcastEvent(event_to_broadcast_sp); |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6117 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 6118 | |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6119 | return return_value; |
| 6120 | } |
| 6121 | |
| 6122 | const char * |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 6123 | Process::ExecutionResultAsCString (ExpressionResults result) |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6124 | { |
| 6125 | const char *result_name; |
| 6126 | |
| 6127 | switch (result) |
| 6128 | { |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6129 | case eExpressionCompleted: |
| 6130 | result_name = "eExpressionCompleted"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6131 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6132 | case eExpressionDiscarded: |
| 6133 | result_name = "eExpressionDiscarded"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6134 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6135 | case eExpressionInterrupted: |
| 6136 | result_name = "eExpressionInterrupted"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6137 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6138 | case eExpressionHitBreakpoint: |
| 6139 | result_name = "eExpressionHitBreakpoint"; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 6140 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6141 | case eExpressionSetupError: |
| 6142 | result_name = "eExpressionSetupError"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6143 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6144 | case eExpressionParseError: |
| 6145 | result_name = "eExpressionParseError"; |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 6146 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6147 | case eExpressionResultUnavailable: |
| 6148 | result_name = "eExpressionResultUnavailable"; |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 6149 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6150 | case eExpressionTimedOut: |
| 6151 | result_name = "eExpressionTimedOut"; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6152 | break; |
Jim Ingham | 8646d3c | 2014-05-05 02:47:44 +0000 | [diff] [blame] | 6153 | case eExpressionStoppedForDebug: |
| 6154 | result_name = "eExpressionStoppedForDebug"; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 6155 | break; |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 6156 | } |
| 6157 | return result_name; |
| 6158 | } |
| 6159 | |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6160 | void |
| 6161 | Process::GetStatus (Stream &strm) |
| 6162 | { |
| 6163 | const StateType state = GetState(); |
Greg Clayton | 2637f82 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 6164 | if (StateIsStoppedState(state, false)) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6165 | { |
| 6166 | if (state == eStateExited) |
| 6167 | { |
| 6168 | int exit_status = GetExitStatus(); |
| 6169 | const char *exit_description = GetExitDescription(); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 6170 | 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] | 6171 | GetID(), |
| 6172 | exit_status, |
| 6173 | exit_status, |
| 6174 | exit_description ? exit_description : ""); |
| 6175 | } |
| 6176 | else |
| 6177 | { |
| 6178 | if (state == eStateConnected) |
| 6179 | strm.Printf ("Connected to remote target.\n"); |
| 6180 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 6181 | strm.Printf ("Process %" PRIu64 " %s\n", GetID(), StateAsCString (state)); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6182 | } |
| 6183 | } |
| 6184 | else |
| 6185 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 6186 | strm.Printf ("Process %" PRIu64 " is running.\n", GetID()); |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6187 | } |
| 6188 | } |
| 6189 | |
| 6190 | size_t |
| 6191 | Process::GetThreadStatus (Stream &strm, |
| 6192 | bool only_threads_with_stop_reason, |
| 6193 | uint32_t start_frame, |
| 6194 | uint32_t num_frames, |
| 6195 | uint32_t num_frames_with_source) |
| 6196 | { |
| 6197 | size_t num_thread_infos_dumped = 0; |
| 6198 | |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6199 | // You can't hold the thread list lock while calling Thread::GetStatus. That very well might run code (e.g. if we need it |
| 6200 | // to get return values or arguments.) For that to work the process has to be able to acquire it. So instead copy the thread |
| 6201 | // ID's, and look them up one by one: |
| 6202 | |
| 6203 | uint32_t num_threads; |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 6204 | std::vector<lldb::tid_t> thread_id_array; |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6205 | //Scope for thread list locker; |
| 6206 | { |
| 6207 | Mutex::Locker locker (GetThreadList().GetMutex()); |
| 6208 | ThreadList &curr_thread_list = GetThreadList(); |
| 6209 | num_threads = curr_thread_list.GetSize(); |
| 6210 | uint32_t idx; |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 6211 | thread_id_array.resize(num_threads); |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6212 | for (idx = 0; idx < num_threads; ++idx) |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 6213 | thread_id_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetID(); |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6214 | } |
| 6215 | |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6216 | for (uint32_t i = 0; i < num_threads; i++) |
| 6217 | { |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 6218 | ThreadSP thread_sp(GetThreadList().FindThreadByID(thread_id_array[i])); |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6219 | if (thread_sp) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6220 | { |
| 6221 | if (only_threads_with_stop_reason) |
| 6222 | { |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6223 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo(); |
Jim Ingham | 5d88a06 | 2012-10-16 00:09:33 +0000 | [diff] [blame] | 6224 | if (stop_info_sp.get() == NULL || !stop_info_sp->IsValid()) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6225 | continue; |
| 6226 | } |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6227 | thread_sp->GetStatus (strm, |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6228 | start_frame, |
| 6229 | num_frames, |
| 6230 | num_frames_with_source); |
| 6231 | ++num_thread_infos_dumped; |
| 6232 | } |
Jim Ingham | 4a65fb1 | 2014-03-07 11:20:03 +0000 | [diff] [blame] | 6233 | else |
| 6234 | { |
| 6235 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 6236 | if (log) |
| 6237 | log->Printf("Process::GetThreadStatus - thread 0x" PRIu64 " vanished while running Thread::GetStatus."); |
| 6238 | |
| 6239 | } |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 6240 | } |
| 6241 | return num_thread_infos_dumped; |
| 6242 | } |
| 6243 | |
Greg Clayton | a9f40ad | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 6244 | void |
| 6245 | Process::AddInvalidMemoryRegion (const LoadRange ®ion) |
| 6246 | { |
| 6247 | m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 6248 | } |
| 6249 | |
| 6250 | bool |
| 6251 | Process::RemoveInvalidMemoryRange (const LoadRange ®ion) |
| 6252 | { |
| 6253 | return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 6254 | } |
| 6255 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 6256 | void |
| 6257 | Process::AddPreResumeAction (PreResumeActionCallback callback, void *baton) |
| 6258 | { |
| 6259 | m_pre_resume_actions.push_back(PreResumeCallbackAndBaton (callback, baton)); |
| 6260 | } |
| 6261 | |
| 6262 | bool |
| 6263 | Process::RunPreResumeActions () |
| 6264 | { |
| 6265 | bool result = true; |
| 6266 | while (!m_pre_resume_actions.empty()) |
| 6267 | { |
| 6268 | struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back(); |
| 6269 | m_pre_resume_actions.pop_back(); |
| 6270 | bool this_result = action.callback (action.baton); |
Jason Molenda | 5571093 | 2014-11-17 20:10:15 +0000 | [diff] [blame] | 6271 | if (result == true) |
| 6272 | result = this_result; |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 6273 | } |
| 6274 | return result; |
| 6275 | } |
| 6276 | |
| 6277 | void |
| 6278 | Process::ClearPreResumeActions () |
| 6279 | { |
| 6280 | m_pre_resume_actions.clear(); |
| 6281 | } |
Greg Clayton | a9f40ad | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 6282 | |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 6283 | void |
| 6284 | Process::Flush () |
| 6285 | { |
| 6286 | m_thread_list.Flush(); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 6287 | m_extended_thread_list.Flush(); |
| 6288 | m_extended_thread_stop_id = 0; |
| 6289 | m_queue_list.Clear(); |
| 6290 | m_queue_list_stop_id = 0; |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 6291 | } |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6292 | |
| 6293 | void |
| 6294 | Process::DidExec () |
| 6295 | { |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 6296 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 6297 | if (log) |
| 6298 | log->Printf ("Process::%s()", __FUNCTION__); |
| 6299 | |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6300 | Target &target = GetTarget(); |
| 6301 | target.CleanupProcess (); |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame] | 6302 | target.ClearModules(false); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6303 | m_dynamic_checkers_ap.reset(); |
| 6304 | m_abi_sp.reset(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 6305 | m_system_runtime_ap.reset(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6306 | m_os_ap.reset(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 6307 | m_dyld_ap.reset(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 6308 | m_jit_loaders_ap.reset(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6309 | m_image_tokens.clear(); |
| 6310 | m_allocated_memory_cache.Clear(); |
| 6311 | m_language_runtimes.clear(); |
Kuba Brecka | afdf842 | 2014-10-10 23:43:03 +0000 | [diff] [blame] | 6312 | m_instrumentation_runtimes.clear(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 6313 | m_thread_list.DiscardThreadPlans(); |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 6314 | m_memory_cache.Clear(true); |
Greg Clayton | a97c4d2 | 2014-12-09 23:31:02 +0000 | [diff] [blame] | 6315 | m_stop_info_override_callback = NULL; |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6316 | DoDidExec(); |
| 6317 | CompleteAttach (); |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 6318 | // Flush the process (threads and all stack frames) after running CompleteAttach() |
| 6319 | // in case the dynamic loader loaded things in new locations. |
| 6320 | Flush(); |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame] | 6321 | |
| 6322 | // After we figure out what was loaded/unloaded in CompleteAttach, |
| 6323 | // we need to let the target know so it can do any cleanup it needs to. |
| 6324 | target.DidExec(); |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 6325 | } |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 6326 | |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 6327 | addr_t |
| 6328 | Process::ResolveIndirectFunction(const Address *address, Error &error) |
| 6329 | { |
| 6330 | if (address == nullptr) |
| 6331 | { |
Jean-Daniel Dupas | ef37711f | 2014-02-08 20:22:05 +0000 | [diff] [blame] | 6332 | error.SetErrorString("Invalid address argument"); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 6333 | return LLDB_INVALID_ADDRESS; |
| 6334 | } |
| 6335 | |
| 6336 | addr_t function_addr = LLDB_INVALID_ADDRESS; |
| 6337 | |
| 6338 | addr_t addr = address->GetLoadAddress(&GetTarget()); |
| 6339 | std::map<addr_t,addr_t>::const_iterator iter = m_resolved_indirect_addresses.find(addr); |
| 6340 | if (iter != m_resolved_indirect_addresses.end()) |
| 6341 | { |
| 6342 | function_addr = (*iter).second; |
| 6343 | } |
| 6344 | else |
| 6345 | { |
| 6346 | if (!InferiorCall(this, address, function_addr)) |
| 6347 | { |
| 6348 | Symbol *symbol = address->CalculateSymbolContextSymbol(); |
| 6349 | error.SetErrorStringWithFormat ("Unable to call resolver for indirect function %s", |
| 6350 | symbol ? symbol->GetName().AsCString() : "<UNKNOWN>"); |
| 6351 | function_addr = LLDB_INVALID_ADDRESS; |
| 6352 | } |
| 6353 | else |
| 6354 | { |
| 6355 | m_resolved_indirect_addresses.insert(std::pair<addr_t, addr_t>(addr, function_addr)); |
| 6356 | } |
| 6357 | } |
| 6358 | return function_addr; |
| 6359 | } |
| 6360 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 6361 | void |
| 6362 | Process::ModulesDidLoad (ModuleList &module_list) |
| 6363 | { |
Kuba Brecka | afdf842 | 2014-10-10 23:43:03 +0000 | [diff] [blame] | 6364 | SystemRuntime *sys_runtime = GetSystemRuntime(); |
| 6365 | if (sys_runtime) |
| 6366 | { |
| 6367 | sys_runtime->ModulesDidLoad (module_list); |
| 6368 | } |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 6369 | |
Kuba Brecka | afdf842 | 2014-10-10 23:43:03 +0000 | [diff] [blame] | 6370 | GetJITLoaders().ModulesDidLoad (module_list); |
| 6371 | |
| 6372 | // Give runtimes a chance to be created. |
| 6373 | InstrumentationRuntime::ModulesDidLoad(module_list, this, m_instrumentation_runtimes); |
| 6374 | |
| 6375 | // Tell runtimes about new modules. |
| 6376 | for (auto pos = m_instrumentation_runtimes.begin(); pos != m_instrumentation_runtimes.end(); ++pos) |
| 6377 | { |
| 6378 | InstrumentationRuntimeSP runtime = pos->second; |
| 6379 | runtime->ModulesDidLoad(module_list); |
| 6380 | } |
| 6381 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 6382 | } |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 6383 | |
| 6384 | ThreadCollectionSP |
| 6385 | Process::GetHistoryThreads(lldb::addr_t addr) |
| 6386 | { |
| 6387 | ThreadCollectionSP threads; |
| 6388 | |
| 6389 | const MemoryHistorySP &memory_history = MemoryHistory::FindPlugin(shared_from_this()); |
| 6390 | |
| 6391 | if (! memory_history.get()) { |
| 6392 | return threads; |
| 6393 | } |
| 6394 | |
| 6395 | threads.reset(new ThreadCollection(memory_history->GetHistoryThreads(addr))); |
| 6396 | |
| 6397 | return threads; |
| 6398 | } |
Kuba Brecka | 6392754 | 2014-10-11 01:59:32 +0000 | [diff] [blame] | 6399 | |
| 6400 | InstrumentationRuntimeSP |
| 6401 | Process::GetInstrumentationRuntime(lldb::InstrumentationRuntimeType type) |
| 6402 | { |
| 6403 | InstrumentationRuntimeCollection::iterator pos; |
| 6404 | pos = m_instrumentation_runtimes.find (type); |
| 6405 | if (pos == m_instrumentation_runtimes.end()) |
| 6406 | { |
| 6407 | return InstrumentationRuntimeSP(); |
| 6408 | } |
| 6409 | else |
| 6410 | return (*pos).second; |
| 6411 | } |