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 | |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
Greg Clayton | 5cc45e0 | 2016-02-26 19:41:49 +0000 | [diff] [blame] | 12 | #include <atomic> |
Greg Clayton | 04df8ee | 2016-02-26 19:38:18 +0000 | [diff] [blame] | 13 | #include <mutex> |
Eugene Zelenko | da8cf8a | 2016-03-01 00:55:51 +0000 | [diff] [blame] | 14 | |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 15 | // Other libraries and framework includes |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ScopedPrinter.h" |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Threading.h" |
| 18 | |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 19 | // Project includes |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 20 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Breakpoint/BreakpointLocation.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 22 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Core/Debugger.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 24 | #include "lldb/Core/Event.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 25 | #include "lldb/Core/Module.h" |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 26 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 28 | #include "lldb/Core/StreamFile.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 29 | #include "lldb/Expression/DiagnosticManager.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 30 | #include "lldb/Expression/IRDynamicChecks.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 31 | #include "lldb/Expression/UserExpression.h" |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 32 | #include "lldb/Expression/UtilityFunction.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 33 | #include "lldb/Host/ConnectionFileDescriptor.h" |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame] | 34 | #include "lldb/Host/FileSystem.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | #include "lldb/Host/Host.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 36 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | 3eb2b44 | 2017-03-22 23:33:16 +0000 | [diff] [blame] | 37 | #include "lldb/Host/OptionParser.h" |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 38 | #include "lldb/Host/Pipe.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 39 | #include "lldb/Host/Terminal.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 40 | #include "lldb/Host/ThreadLauncher.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 41 | #include "lldb/Interpreter/CommandInterpreter.h" |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 42 | #include "lldb/Interpreter/OptionArgParser.h" |
Zachary Turner | 633a29c | 2015-03-04 01:58:01 +0000 | [diff] [blame] | 43 | #include "lldb/Interpreter/OptionValueProperties.h" |
Jason Molenda | 484900b | 2015-08-10 07:55:25 +0000 | [diff] [blame] | 44 | #include "lldb/Symbol/Function.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 45 | #include "lldb/Symbol/Symbol.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | #include "lldb/Target/ABI.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 47 | #include "lldb/Target/CPPLanguageRuntime.h" |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 48 | #include "lldb/Target/DynamicLoader.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 49 | #include "lldb/Target/InstrumentationRuntime.h" |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 50 | #include "lldb/Target/JITLoader.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 51 | #include "lldb/Target/JITLoaderList.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 52 | #include "lldb/Target/LanguageRuntime.h" |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 53 | #include "lldb/Target/MemoryHistory.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 54 | #include "lldb/Target/MemoryRegionInfo.h" |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 55 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 56 | #include "lldb/Target/OperatingSystem.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 57 | #include "lldb/Target/Platform.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 58 | #include "lldb/Target/Process.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 60 | #include "lldb/Target/StopInfo.h" |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 61 | #include "lldb/Target/StructuredDataPlugin.h" |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 62 | #include "lldb/Target/SystemRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | #include "lldb/Target/Target.h" |
| 64 | #include "lldb/Target/TargetList.h" |
| 65 | #include "lldb/Target/Thread.h" |
| 66 | #include "lldb/Target/ThreadPlan.h" |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 67 | #include "lldb/Target/ThreadPlanBase.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 68 | #include "lldb/Target/UnixSignals.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 69 | #include "lldb/Utility/Log.h" |
Zachary Turner | 5023257 | 2015-03-18 21:31:45 +0000 | [diff] [blame] | 70 | #include "lldb/Utility/NameMatches.h" |
Greg Clayton | ee1f578 | 2016-08-10 22:43:48 +0000 | [diff] [blame] | 71 | #include "lldb/Utility/SelectHelper.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 72 | #include "lldb/Utility/State.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | |
| 74 | using namespace lldb; |
| 75 | using namespace lldb_private; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 76 | using namespace std::chrono; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | // Comment out line below to disable memory caching, overriding the process |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 79 | // setting target.process.disable-memory-cache |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 80 | #define ENABLE_MEMORY_CACHING |
| 81 | |
| 82 | #ifdef ENABLE_MEMORY_CACHING |
| 83 | #define DISABLE_MEM_CACHE_DEFAULT false |
| 84 | #else |
| 85 | #define DISABLE_MEM_CACHE_DEFAULT true |
| 86 | #endif |
| 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | class ProcessOptionValueProperties : public OptionValueProperties { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 89 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | ProcessOptionValueProperties(const ConstString &name) |
| 91 | : OptionValueProperties(name) {} |
| 92 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 93 | // This constructor is used when creating ProcessOptionValueProperties when |
| 94 | // it is part of a new lldb_private::Process instance. It will copy all |
| 95 | // current global property values as needed |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | ProcessOptionValueProperties(ProcessProperties *global_properties) |
| 97 | : OptionValueProperties(*global_properties->GetValueProperties()) {} |
| 98 | |
| 99 | const Property *GetPropertyAtIndex(const ExecutionContext *exe_ctx, |
| 100 | bool will_modify, |
| 101 | uint32_t idx) const override { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 102 | // When getting the value for a key from the process options, we will |
| 103 | // always try and grab the setting from the current process if there is |
| 104 | // one. Else we just use the one from this instance. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | if (exe_ctx) { |
| 106 | Process *process = exe_ctx->GetProcessPtr(); |
| 107 | if (process) { |
| 108 | ProcessOptionValueProperties *instance_properties = |
| 109 | static_cast<ProcessOptionValueProperties *>( |
| 110 | process->GetValueProperties().get()); |
| 111 | if (this != instance_properties) |
| 112 | return instance_properties->ProtectedGetPropertyAtIndex(idx); |
| 113 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 114 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | return ProtectedGetPropertyAtIndex(idx); |
| 116 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | static PropertyDefinition g_properties[] = { |
| 120 | {"disable-memory-cache", OptionValue::eTypeBoolean, false, |
| 121 | DISABLE_MEM_CACHE_DEFAULT, nullptr, nullptr, |
| 122 | "Disable reading and caching of memory in fixed-size units."}, |
| 123 | {"extra-startup-command", OptionValue::eTypeArray, false, |
| 124 | OptionValue::eTypeString, nullptr, nullptr, |
| 125 | "A list containing extra commands understood by the particular process " |
| 126 | "plugin used. " |
| 127 | "For instance, to turn on debugserver logging set this to " |
| 128 | "\"QSetLogging:bitmask=LOG_DEFAULT;\""}, |
| 129 | {"ignore-breakpoints-in-expressions", OptionValue::eTypeBoolean, true, true, |
| 130 | nullptr, nullptr, |
| 131 | "If true, breakpoints will be ignored during expression evaluation."}, |
| 132 | {"unwind-on-error-in-expressions", OptionValue::eTypeBoolean, true, true, |
| 133 | nullptr, nullptr, "If true, errors in expression evaluation will unwind " |
| 134 | "the stack back to the state before the call."}, |
| 135 | {"python-os-plugin-path", OptionValue::eTypeFileSpec, false, true, nullptr, |
| 136 | nullptr, "A path to a python OS plug-in module file that contains a " |
| 137 | "OperatingSystemPlugIn class."}, |
| 138 | {"stop-on-sharedlibrary-events", OptionValue::eTypeBoolean, true, false, |
| 139 | nullptr, nullptr, |
| 140 | "If true, stop when a shared library is loaded or unloaded."}, |
| 141 | {"detach-keeps-stopped", OptionValue::eTypeBoolean, true, false, nullptr, |
| 142 | nullptr, "If true, detach will attempt to keep the process stopped."}, |
| 143 | {"memory-cache-line-size", OptionValue::eTypeUInt64, false, 512, nullptr, |
| 144 | nullptr, "The memory cache line size"}, |
| 145 | {"optimization-warnings", OptionValue::eTypeBoolean, false, true, nullptr, |
| 146 | nullptr, "If true, warn when stopped in code that is optimized where " |
| 147 | "stepping and variable availability may not behave as expected."}, |
Jim Ingham | ba205c1 | 2017-12-05 02:50:45 +0000 | [diff] [blame] | 148 | {"stop-on-exec", OptionValue::eTypeBoolean, true, true, |
| 149 | nullptr, nullptr, |
| 150 | "If true, stop when a shared library is loaded or unloaded."}, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 151 | {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}}; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 152 | |
| 153 | enum { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 154 | ePropertyDisableMemCache, |
| 155 | ePropertyExtraStartCommand, |
| 156 | ePropertyIgnoreBreakpointsInExpressions, |
| 157 | ePropertyUnwindOnErrorInExpressions, |
| 158 | ePropertyPythonOSPluginPath, |
| 159 | ePropertyStopOnSharedLibraryEvents, |
| 160 | ePropertyDetachKeepsStopped, |
| 161 | ePropertyMemCacheLineSize, |
Jim Ingham | ba205c1 | 2017-12-05 02:50:45 +0000 | [diff] [blame] | 162 | ePropertyWarningOptimization, |
| 163 | ePropertyStopOnExec |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 166 | ProcessProperties::ProcessProperties(lldb_private::Process *process) |
| 167 | : Properties(), |
| 168 | m_process(process) // Can be nullptr for global ProcessProperties |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 169 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | if (process == nullptr) { |
| 171 | // Global process properties, set them up one time |
| 172 | m_collection_sp.reset( |
| 173 | new ProcessOptionValueProperties(ConstString("process"))); |
| 174 | m_collection_sp->Initialize(g_properties); |
| 175 | m_collection_sp->AppendProperty( |
| 176 | ConstString("thread"), ConstString("Settings specific to threads."), |
| 177 | true, Thread::GetGlobalProperties()->GetValueProperties()); |
| 178 | } else { |
| 179 | m_collection_sp.reset( |
| 180 | new ProcessOptionValueProperties(Process::GetGlobalProperties().get())); |
| 181 | m_collection_sp->SetValueChangedCallback( |
| 182 | ePropertyPythonOSPluginPath, |
| 183 | ProcessProperties::OptionValueChangedCallback, this); |
| 184 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 187 | ProcessProperties::~ProcessProperties() = default; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 188 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | void ProcessProperties::OptionValueChangedCallback(void *baton, |
| 190 | OptionValue *option_value) { |
| 191 | ProcessProperties *properties = (ProcessProperties *)baton; |
| 192 | if (properties->m_process) |
| 193 | properties->m_process->LoadOperatingSystemPlugin(true); |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | bool ProcessProperties::GetDisableMemoryCache() const { |
| 197 | const uint32_t idx = ePropertyDisableMemCache; |
| 198 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
| 199 | nullptr, idx, g_properties[idx].default_uint_value != 0); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | uint64_t ProcessProperties::GetMemoryCacheLineSize() const { |
| 203 | const uint32_t idx = ePropertyMemCacheLineSize; |
| 204 | return m_collection_sp->GetPropertyAtIndexAsUInt64( |
| 205 | nullptr, idx, g_properties[idx].default_uint_value); |
Jason Molenda | f0340c9 | 2014-09-03 22:30:54 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 208 | Args ProcessProperties::GetExtraStartupCommands() const { |
| 209 | Args args; |
| 210 | const uint32_t idx = ePropertyExtraStartCommand; |
| 211 | m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args); |
| 212 | return args; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 215 | void ProcessProperties::SetExtraStartupCommands(const Args &args) { |
| 216 | const uint32_t idx = ePropertyExtraStartCommand; |
| 217 | m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | FileSpec ProcessProperties::GetPythonOSPluginPath() const { |
| 221 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 222 | return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx); |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | void ProcessProperties::SetPythonOSPluginPath(const FileSpec &file) { |
| 226 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 227 | m_collection_sp->SetPropertyAtIndexAsFileSpec(nullptr, idx, file); |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 230 | bool ProcessProperties::GetIgnoreBreakpointsInExpressions() const { |
| 231 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 232 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
| 233 | nullptr, idx, g_properties[idx].default_uint_value != 0); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 236 | void ProcessProperties::SetIgnoreBreakpointsInExpressions(bool ignore) { |
| 237 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 238 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, ignore); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | bool ProcessProperties::GetUnwindOnErrorInExpressions() const { |
| 242 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 243 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
| 244 | nullptr, idx, g_properties[idx].default_uint_value != 0); |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 247 | void ProcessProperties::SetUnwindOnErrorInExpressions(bool ignore) { |
| 248 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 249 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, ignore); |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 252 | bool ProcessProperties::GetStopOnSharedLibraryEvents() const { |
| 253 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 254 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
| 255 | nullptr, idx, g_properties[idx].default_uint_value != 0); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | void ProcessProperties::SetStopOnSharedLibraryEvents(bool stop) { |
| 259 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 260 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, stop); |
| 261 | } |
| 262 | |
| 263 | bool ProcessProperties::GetDetachKeepsStopped() const { |
| 264 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 265 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
| 266 | nullptr, idx, g_properties[idx].default_uint_value != 0); |
| 267 | } |
| 268 | |
| 269 | void ProcessProperties::SetDetachKeepsStopped(bool stop) { |
| 270 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 271 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, stop); |
| 272 | } |
| 273 | |
| 274 | bool ProcessProperties::GetWarningsOptimization() const { |
| 275 | const uint32_t idx = ePropertyWarningOptimization; |
| 276 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
| 277 | nullptr, idx, g_properties[idx].default_uint_value != 0); |
| 278 | } |
| 279 | |
Jim Ingham | ba205c1 | 2017-12-05 02:50:45 +0000 | [diff] [blame] | 280 | bool ProcessProperties::GetStopOnExec() const { |
| 281 | const uint32_t idx = ePropertyStopOnExec; |
| 282 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
| 283 | nullptr, idx, g_properties[idx].default_uint_value != 0); |
| 284 | } |
| 285 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 286 | void ProcessInstanceInfo::Dump(Stream &s, Platform *platform) const { |
| 287 | const char *cstr; |
| 288 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
| 289 | s.Printf(" pid = %" PRIu64 "\n", m_pid); |
| 290 | |
| 291 | if (m_parent_pid != LLDB_INVALID_PROCESS_ID) |
| 292 | s.Printf(" parent = %" PRIu64 "\n", m_parent_pid); |
| 293 | |
| 294 | if (m_executable) { |
| 295 | s.Printf(" name = %s\n", m_executable.GetFilename().GetCString()); |
| 296 | s.PutCString(" file = "); |
| 297 | m_executable.Dump(&s); |
| 298 | s.EOL(); |
| 299 | } |
| 300 | const uint32_t argc = m_arguments.GetArgumentCount(); |
| 301 | if (argc > 0) { |
| 302 | for (uint32_t i = 0; i < argc; i++) { |
| 303 | const char *arg = m_arguments.GetArgumentAtIndex(i); |
| 304 | if (i < 10) |
| 305 | s.Printf(" arg[%u] = %s\n", i, arg); |
| 306 | else |
| 307 | s.Printf("arg[%u] = %s\n", i, arg); |
| 308 | } |
| 309 | } |
| 310 | |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 311 | s.Format("{0}", m_environment); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 312 | |
| 313 | if (m_arch.IsValid()) { |
| 314 | s.Printf(" arch = "); |
| 315 | m_arch.DumpTriple(s); |
| 316 | s.EOL(); |
| 317 | } |
| 318 | |
| 319 | if (m_uid != UINT32_MAX) { |
| 320 | cstr = platform->GetUserName(m_uid); |
| 321 | s.Printf(" uid = %-5u (%s)\n", m_uid, cstr ? cstr : ""); |
| 322 | } |
| 323 | if (m_gid != UINT32_MAX) { |
| 324 | cstr = platform->GetGroupName(m_gid); |
| 325 | s.Printf(" gid = %-5u (%s)\n", m_gid, cstr ? cstr : ""); |
| 326 | } |
| 327 | if (m_euid != UINT32_MAX) { |
| 328 | cstr = platform->GetUserName(m_euid); |
| 329 | s.Printf(" euid = %-5u (%s)\n", m_euid, cstr ? cstr : ""); |
| 330 | } |
| 331 | if (m_egid != UINT32_MAX) { |
| 332 | cstr = platform->GetGroupName(m_egid); |
| 333 | s.Printf(" egid = %-5u (%s)\n", m_egid, cstr ? cstr : ""); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void ProcessInstanceInfo::DumpTableHeader(Stream &s, Platform *platform, |
| 338 | bool show_args, bool verbose) { |
| 339 | const char *label; |
| 340 | if (show_args || verbose) |
| 341 | label = "ARGUMENTS"; |
| 342 | else |
| 343 | label = "NAME"; |
| 344 | |
| 345 | if (verbose) { |
| 346 | s.Printf("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE " |
| 347 | " %s\n", |
| 348 | label); |
| 349 | s.PutCString("====== ====== ========== ========== ========== ========== " |
| 350 | "======================== ============================\n"); |
| 351 | } else { |
| 352 | s.Printf("PID PARENT USER TRIPLE %s\n", label); |
| 353 | s.PutCString("====== ====== ========== ======================== " |
| 354 | "============================\n"); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void ProcessInstanceInfo::DumpAsTableRow(Stream &s, Platform *platform, |
| 359 | bool show_args, bool verbose) const { |
| 360 | if (m_pid != LLDB_INVALID_PROCESS_ID) { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 361 | const char *cstr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 362 | s.Printf("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 363 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 364 | StreamString arch_strm; |
Todd Fiala | 7df337f | 2015-10-13 23:41:19 +0000 | [diff] [blame] | 365 | if (m_arch.IsValid()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | m_arch.DumpTriple(arch_strm); |
| 367 | |
| 368 | if (verbose) { |
| 369 | cstr = platform->GetUserName(m_uid); |
| 370 | if (cstr && |
| 371 | cstr[0]) // Watch for empty string that indicates lookup failed |
| 372 | s.Printf("%-10s ", cstr); |
| 373 | else |
| 374 | s.Printf("%-10u ", m_uid); |
| 375 | |
| 376 | cstr = platform->GetGroupName(m_gid); |
| 377 | if (cstr && |
| 378 | cstr[0]) // Watch for empty string that indicates lookup failed |
| 379 | s.Printf("%-10s ", cstr); |
| 380 | else |
| 381 | s.Printf("%-10u ", m_gid); |
| 382 | |
| 383 | cstr = platform->GetUserName(m_euid); |
| 384 | if (cstr && |
| 385 | cstr[0]) // Watch for empty string that indicates lookup failed |
| 386 | s.Printf("%-10s ", cstr); |
| 387 | else |
| 388 | s.Printf("%-10u ", m_euid); |
| 389 | |
| 390 | cstr = platform->GetGroupName(m_egid); |
| 391 | if (cstr && |
| 392 | cstr[0]) // Watch for empty string that indicates lookup failed |
| 393 | s.Printf("%-10s ", cstr); |
| 394 | else |
| 395 | s.Printf("%-10u ", m_egid); |
| 396 | |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 397 | s.Printf("%-24s ", arch_strm.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 398 | } else { |
| 399 | s.Printf("%-10s %-24s ", platform->GetUserName(m_euid), |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 400 | arch_strm.GetData()); |
Todd Fiala | 7df337f | 2015-10-13 23:41:19 +0000 | [diff] [blame] | 401 | } |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 402 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 403 | if (verbose || show_args) { |
| 404 | const uint32_t argc = m_arguments.GetArgumentCount(); |
| 405 | if (argc > 0) { |
| 406 | for (uint32_t i = 0; i < argc; i++) { |
| 407 | if (i > 0) |
| 408 | s.PutChar(' '); |
| 409 | s.PutCString(m_arguments.GetArgumentAtIndex(i)); |
| 410 | } |
| 411 | } |
| 412 | } else { |
| 413 | s.PutCString(GetName()); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 414 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 415 | |
| 416 | s.EOL(); |
| 417 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 420 | Status ProcessLaunchCommandOptions::SetOptionValue( |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 421 | uint32_t option_idx, llvm::StringRef option_arg, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 422 | ExecutionContext *execution_context) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 423 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 424 | const int short_option = m_getopt_table[option_idx].val; |
| 425 | |
| 426 | switch (short_option) { |
| 427 | case 's': // Stop at program entry point |
| 428 | launch_info.GetFlags().Set(eLaunchFlagStopAtEntry); |
| 429 | break; |
| 430 | |
| 431 | case 'i': // STDIN for read only |
| 432 | { |
| 433 | FileAction action; |
| 434 | if (action.Open(STDIN_FILENO, FileSpec{option_arg, false}, true, false)) |
| 435 | launch_info.AppendFileAction(action); |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | case 'o': // Open STDOUT for write only |
| 440 | { |
| 441 | FileAction action; |
| 442 | if (action.Open(STDOUT_FILENO, FileSpec{option_arg, false}, false, true)) |
| 443 | launch_info.AppendFileAction(action); |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | case 'e': // STDERR for write only |
| 448 | { |
| 449 | FileAction action; |
| 450 | if (action.Open(STDERR_FILENO, FileSpec{option_arg, false}, false, true)) |
| 451 | launch_info.AppendFileAction(action); |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | case 'p': // Process plug-in name |
| 456 | launch_info.SetProcessPluginName(option_arg); |
| 457 | break; |
| 458 | |
| 459 | case 'n': // Disable STDIO |
| 460 | { |
| 461 | FileAction action; |
| 462 | const FileSpec dev_null{FileSystem::DEV_NULL, false}; |
| 463 | if (action.Open(STDIN_FILENO, dev_null, true, false)) |
| 464 | launch_info.AppendFileAction(action); |
| 465 | if (action.Open(STDOUT_FILENO, dev_null, false, true)) |
| 466 | launch_info.AppendFileAction(action); |
| 467 | if (action.Open(STDERR_FILENO, dev_null, false, true)) |
| 468 | launch_info.AppendFileAction(action); |
| 469 | break; |
| 470 | } |
| 471 | |
| 472 | case 'w': |
| 473 | launch_info.SetWorkingDirectory(FileSpec{option_arg, false}); |
| 474 | break; |
| 475 | |
| 476 | case 't': // Open process in new terminal window |
| 477 | launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY); |
| 478 | break; |
| 479 | |
| 480 | case 'a': { |
| 481 | TargetSP target_sp = |
| 482 | execution_context ? execution_context->GetTargetSP() : TargetSP(); |
| 483 | PlatformSP platform_sp = |
| 484 | target_sp ? target_sp->GetPlatform() : PlatformSP(); |
Pavel Labath | 7263f1b | 2017-10-31 10:56:03 +0000 | [diff] [blame] | 485 | launch_info.GetArchitecture() = |
| 486 | Platform::GetAugmentedArchSpec(platform_sp.get(), option_arg); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 487 | } break; |
| 488 | |
| 489 | case 'A': // Disable ASLR. |
| 490 | { |
| 491 | bool success; |
| 492 | const bool disable_aslr_arg = |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 493 | OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 494 | if (success) |
| 495 | disable_aslr = disable_aslr_arg ? eLazyBoolYes : eLazyBoolNo; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 496 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 497 | error.SetErrorStringWithFormat( |
| 498 | "Invalid boolean value for disable-aslr option: '%s'", |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 499 | option_arg.empty() ? "<null>" : option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 500 | break; |
| 501 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 502 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 503 | case 'X': // shell expand args. |
| 504 | { |
| 505 | bool success; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 506 | const bool expand_args = |
| 507 | OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 508 | if (success) |
| 509 | launch_info.SetShellExpandArguments(expand_args); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 510 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 511 | error.SetErrorStringWithFormat( |
| 512 | "Invalid boolean value for shell-expand-args option: '%s'", |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 513 | option_arg.empty() ? "<null>" : option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 514 | break; |
| 515 | } |
| 516 | |
| 517 | case 'c': |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 518 | if (!option_arg.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 519 | launch_info.SetShell(FileSpec(option_arg, false)); |
| 520 | else |
| 521 | launch_info.SetShell(HostInfo::GetDefaultShell()); |
| 522 | break; |
| 523 | |
| 524 | case 'v': |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 525 | launch_info.GetEnvironment().insert(option_arg); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 526 | break; |
| 527 | |
| 528 | default: |
| 529 | error.SetErrorStringWithFormat("unrecognized short option character '%c'", |
| 530 | short_option); |
| 531 | break; |
| 532 | } |
| 533 | return error; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 536 | static OptionDefinition g_process_launch_options[] = { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 537 | {LLDB_OPT_SET_ALL, false, "stop-at-entry", 's', OptionParser::eNoArgument, |
| 538 | nullptr, nullptr, 0, eArgTypeNone, |
| 539 | "Stop at the entry point of the program when launching a process."}, |
| 540 | {LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', |
| 541 | OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, |
| 542 | "Set whether to disable address space layout randomization when launching " |
| 543 | "a process."}, |
| 544 | {LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, |
| 545 | nullptr, nullptr, 0, eArgTypePlugin, |
| 546 | "Name of the process plugin you want to use."}, |
| 547 | {LLDB_OPT_SET_ALL, false, "working-dir", 'w', |
| 548 | OptionParser::eRequiredArgument, nullptr, nullptr, 0, |
| 549 | eArgTypeDirectoryName, |
| 550 | "Set the current working directory to <path> when running the inferior."}, |
| 551 | {LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, |
| 552 | nullptr, nullptr, 0, eArgTypeArchitecture, |
| 553 | "Set the architecture for the process to launch when ambiguous."}, |
| 554 | {LLDB_OPT_SET_ALL, false, "environment", 'v', |
| 555 | OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, |
| 556 | "Specify an environment variable name/value string (--environment " |
| 557 | "NAME=VALUE). Can be specified multiple times for subsequent environment " |
| 558 | "entries."}, |
| 559 | {LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3, false, "shell", 'c', |
| 560 | OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeFilename, |
| 561 | "Run the process in a shell (not supported on all platforms)."}, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 562 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 563 | {LLDB_OPT_SET_1, false, "stdin", 'i', OptionParser::eRequiredArgument, |
| 564 | nullptr, nullptr, 0, eArgTypeFilename, |
| 565 | "Redirect stdin for the process to <filename>."}, |
| 566 | {LLDB_OPT_SET_1, false, "stdout", 'o', OptionParser::eRequiredArgument, |
| 567 | nullptr, nullptr, 0, eArgTypeFilename, |
| 568 | "Redirect stdout for the process to <filename>."}, |
| 569 | {LLDB_OPT_SET_1, false, "stderr", 'e', OptionParser::eRequiredArgument, |
| 570 | nullptr, nullptr, 0, eArgTypeFilename, |
| 571 | "Redirect stderr for the process to <filename>."}, |
Todd Fiala | 7df337f | 2015-10-13 23:41:19 +0000 | [diff] [blame] | 572 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 573 | {LLDB_OPT_SET_2, false, "tty", 't', OptionParser::eNoArgument, nullptr, |
| 574 | nullptr, 0, eArgTypeNone, |
| 575 | "Start the process in a terminal (not supported on all platforms)."}, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 576 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 577 | {LLDB_OPT_SET_3, false, "no-stdio", 'n', OptionParser::eNoArgument, nullptr, |
| 578 | nullptr, 0, eArgTypeNone, |
| 579 | "Do not set up for terminal I/O to go to running process."}, |
| 580 | {LLDB_OPT_SET_4, false, "shell-expand-args", 'X', |
| 581 | OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, |
| 582 | "Set whether to shell expand arguments to the process when launching."}, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 583 | }; |
| 584 | |
| 585 | llvm::ArrayRef<OptionDefinition> ProcessLaunchCommandOptions::GetDefinitions() { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 586 | return llvm::makeArrayRef(g_process_launch_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 587 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 588 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 589 | bool ProcessInstanceInfoMatch::NameMatches(const char *process_name) const { |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 590 | if (m_name_match_type == NameMatch::Ignore || process_name == nullptr) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 591 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 592 | const char *match_name = m_match_info.GetName(); |
| 593 | if (!match_name) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 594 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 595 | |
| 596 | return lldb_private::NameMatches(process_name, m_name_match_type, match_name); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 599 | bool ProcessInstanceInfoMatch::Matches( |
| 600 | const ProcessInstanceInfo &proc_info) const { |
| 601 | if (!NameMatches(proc_info.GetName())) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 602 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 603 | |
| 604 | if (m_match_info.ProcessIDIsValid() && |
| 605 | m_match_info.GetProcessID() != proc_info.GetProcessID()) |
| 606 | return false; |
| 607 | |
| 608 | if (m_match_info.ParentProcessIDIsValid() && |
| 609 | m_match_info.GetParentProcessID() != proc_info.GetParentProcessID()) |
| 610 | return false; |
| 611 | |
| 612 | if (m_match_info.UserIDIsValid() && |
| 613 | m_match_info.GetUserID() != proc_info.GetUserID()) |
| 614 | return false; |
| 615 | |
| 616 | if (m_match_info.GroupIDIsValid() && |
| 617 | m_match_info.GetGroupID() != proc_info.GetGroupID()) |
| 618 | return false; |
| 619 | |
| 620 | if (m_match_info.EffectiveUserIDIsValid() && |
| 621 | m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID()) |
| 622 | return false; |
| 623 | |
| 624 | if (m_match_info.EffectiveGroupIDIsValid() && |
| 625 | m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID()) |
| 626 | return false; |
| 627 | |
| 628 | if (m_match_info.GetArchitecture().IsValid() && |
| 629 | !m_match_info.GetArchitecture().IsCompatibleMatch( |
| 630 | proc_info.GetArchitecture())) |
| 631 | return false; |
| 632 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 635 | bool ProcessInstanceInfoMatch::MatchAllProcesses() const { |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 636 | if (m_name_match_type != NameMatch::Ignore) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 637 | return false; |
| 638 | |
| 639 | if (m_match_info.ProcessIDIsValid()) |
| 640 | return false; |
| 641 | |
| 642 | if (m_match_info.ParentProcessIDIsValid()) |
| 643 | return false; |
| 644 | |
| 645 | if (m_match_info.UserIDIsValid()) |
| 646 | return false; |
| 647 | |
| 648 | if (m_match_info.GroupIDIsValid()) |
| 649 | return false; |
| 650 | |
| 651 | if (m_match_info.EffectiveUserIDIsValid()) |
| 652 | return false; |
| 653 | |
| 654 | if (m_match_info.EffectiveGroupIDIsValid()) |
| 655 | return false; |
| 656 | |
| 657 | if (m_match_info.GetArchitecture().IsValid()) |
| 658 | return false; |
| 659 | |
| 660 | if (m_match_all_users) |
| 661 | return false; |
| 662 | |
| 663 | return true; |
| 664 | } |
| 665 | |
| 666 | void ProcessInstanceInfoMatch::Clear() { |
| 667 | m_match_info.Clear(); |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 668 | m_name_match_type = NameMatch::Ignore; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 669 | m_match_all_users = false; |
| 670 | } |
| 671 | |
Zachary Turner | 3165945 | 2016-11-17 21:15:14 +0000 | [diff] [blame] | 672 | ProcessSP Process::FindPlugin(lldb::TargetSP target_sp, |
| 673 | llvm::StringRef plugin_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 674 | ListenerSP listener_sp, |
| 675 | const FileSpec *crash_file_path) { |
| 676 | static uint32_t g_process_unique_id = 0; |
| 677 | |
| 678 | ProcessSP process_sp; |
| 679 | ProcessCreateInstance create_callback = nullptr; |
Zachary Turner | 3165945 | 2016-11-17 21:15:14 +0000 | [diff] [blame] | 680 | if (!plugin_name.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 681 | ConstString const_plugin_name(plugin_name); |
| 682 | create_callback = |
| 683 | PluginManager::GetProcessCreateCallbackForPluginName(const_plugin_name); |
| 684 | if (create_callback) { |
| 685 | process_sp = create_callback(target_sp, listener_sp, crash_file_path); |
| 686 | if (process_sp) { |
| 687 | if (process_sp->CanDebug(target_sp, true)) { |
| 688 | process_sp->m_process_unique_id = ++g_process_unique_id; |
| 689 | } else |
| 690 | process_sp.reset(); |
| 691 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 692 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 693 | } else { |
| 694 | for (uint32_t idx = 0; |
| 695 | (create_callback = |
| 696 | PluginManager::GetProcessCreateCallbackAtIndex(idx)) != nullptr; |
| 697 | ++idx) { |
| 698 | process_sp = create_callback(target_sp, listener_sp, crash_file_path); |
| 699 | if (process_sp) { |
| 700 | if (process_sp->CanDebug(target_sp, false)) { |
| 701 | process_sp->m_process_unique_id = ++g_process_unique_id; |
| 702 | break; |
| 703 | } else |
| 704 | process_sp.reset(); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 711 | ConstString &Process::GetStaticBroadcasterClass() { |
| 712 | static ConstString class_name("lldb.process"); |
| 713 | return class_name; |
| 714 | } |
| 715 | |
| 716 | Process::Process(lldb::TargetSP target_sp, ListenerSP listener_sp) |
| 717 | : Process(target_sp, listener_sp, |
| 718 | UnixSignals::Create(HostInfo::GetArchitecture())) { |
| 719 | // This constructor just delegates to the full Process constructor, |
| 720 | // defaulting to using the Host's UnixSignals. |
| 721 | } |
| 722 | |
| 723 | Process::Process(lldb::TargetSP target_sp, ListenerSP listener_sp, |
| 724 | const UnixSignalsSP &unix_signals_sp) |
| 725 | : ProcessProperties(this), UserID(LLDB_INVALID_PROCESS_ID), |
| 726 | Broadcaster((target_sp->GetDebugger().GetBroadcasterManager()), |
| 727 | Process::GetStaticBroadcasterClass().AsCString()), |
Jim Ingham | b87b9e6 | 2018-06-26 23:38:58 +0000 | [diff] [blame] | 728 | m_target_wp(target_sp), m_public_state(eStateUnloaded), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 729 | m_private_state(eStateUnloaded), |
| 730 | m_private_state_broadcaster(nullptr, |
| 731 | "lldb.process.internal_state_broadcaster"), |
| 732 | m_private_state_control_broadcaster( |
| 733 | nullptr, "lldb.process.internal_state_control_broadcaster"), |
| 734 | m_private_state_listener_sp( |
| 735 | Listener::MakeListener("lldb.process.internal_state_listener")), |
| 736 | m_mod_id(), m_process_unique_id(0), m_thread_index_id(0), |
| 737 | m_thread_id_to_index_id_map(), m_exit_status(-1), m_exit_string(), |
| 738 | m_exit_status_mutex(), m_thread_mutex(), m_thread_list_real(this), |
| 739 | m_thread_list(this), m_extended_thread_list(this), |
| 740 | m_extended_thread_stop_id(0), m_queue_list(this), m_queue_list_stop_id(0), |
| 741 | m_notifications(), m_image_tokens(), m_listener_sp(listener_sp), |
| 742 | m_breakpoint_site_list(), m_dynamic_checkers_ap(), |
| 743 | m_unix_signals_sp(unix_signals_sp), m_abi_sp(), m_process_input_reader(), |
| 744 | m_stdio_communication("process.stdio"), m_stdio_communication_mutex(), |
| 745 | m_stdin_forward(false), m_stdout_data(), m_stderr_data(), |
| 746 | m_profile_data_comm_mutex(), m_profile_data(), m_iohandler_sync(0), |
| 747 | m_memory_cache(*this), m_allocated_memory_cache(*this), |
| 748 | m_should_detach(false), m_next_event_action_ap(), m_public_run_lock(), |
Pavel Labath | 13e37d4 | 2017-10-25 21:05:31 +0000 | [diff] [blame] | 749 | m_private_run_lock(), m_finalizing(false), m_finalize_called(false), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 750 | m_clear_thread_plans_on_stop(false), m_force_next_event_delivery(false), |
| 751 | m_last_broadcast_state(eStateInvalid), m_destroy_in_process(false), |
| 752 | m_can_interpret_function_calls(false), m_warnings_issued(), |
| 753 | m_run_thread_plan_lock(), m_can_jit(eCanJITDontKnow) { |
| 754 | CheckInWithManager(); |
| 755 | |
| 756 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
| 757 | if (log) |
| 758 | log->Printf("%p Process::Process()", static_cast<void *>(this)); |
| 759 | |
| 760 | if (!m_unix_signals_sp) |
| 761 | m_unix_signals_sp = std::make_shared<UnixSignals>(); |
| 762 | |
| 763 | SetEventName(eBroadcastBitStateChanged, "state-changed"); |
| 764 | SetEventName(eBroadcastBitInterrupt, "interrupt"); |
| 765 | SetEventName(eBroadcastBitSTDOUT, "stdout-available"); |
| 766 | SetEventName(eBroadcastBitSTDERR, "stderr-available"); |
| 767 | SetEventName(eBroadcastBitProfileData, "profile-data-available"); |
| 768 | SetEventName(eBroadcastBitStructuredData, "structured-data-available"); |
| 769 | |
| 770 | m_private_state_control_broadcaster.SetEventName( |
| 771 | eBroadcastInternalStateControlStop, "control-stop"); |
| 772 | m_private_state_control_broadcaster.SetEventName( |
| 773 | eBroadcastInternalStateControlPause, "control-pause"); |
| 774 | m_private_state_control_broadcaster.SetEventName( |
| 775 | eBroadcastInternalStateControlResume, "control-resume"); |
| 776 | |
| 777 | m_listener_sp->StartListeningForEvents( |
| 778 | this, eBroadcastBitStateChanged | eBroadcastBitInterrupt | |
| 779 | eBroadcastBitSTDOUT | eBroadcastBitSTDERR | |
| 780 | eBroadcastBitProfileData | eBroadcastBitStructuredData); |
| 781 | |
| 782 | m_private_state_listener_sp->StartListeningForEvents( |
| 783 | &m_private_state_broadcaster, |
| 784 | eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
| 785 | |
| 786 | m_private_state_listener_sp->StartListeningForEvents( |
| 787 | &m_private_state_control_broadcaster, |
| 788 | eBroadcastInternalStateControlStop | eBroadcastInternalStateControlPause | |
| 789 | eBroadcastInternalStateControlResume); |
| 790 | // We need something valid here, even if just the default UnixSignalsSP. |
| 791 | assert(m_unix_signals_sp && "null m_unix_signals_sp after initialization"); |
| 792 | |
| 793 | // Allow the platform to override the default cache line size |
| 794 | OptionValueSP value_sp = |
| 795 | m_collection_sp |
| 796 | ->GetPropertyAtIndex(nullptr, true, ePropertyMemCacheLineSize) |
| 797 | ->GetValue(); |
| 798 | uint32_t platform_cache_line_size = |
| 799 | target_sp->GetPlatform()->GetDefaultMemoryCacheLineSize(); |
| 800 | if (!value_sp->OptionWasSet() && platform_cache_line_size != 0) |
| 801 | value_sp->SetUInt64Value(platform_cache_line_size); |
| 802 | } |
| 803 | |
| 804 | Process::~Process() { |
| 805 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
| 806 | if (log) |
| 807 | log->Printf("%p Process::~Process()", static_cast<void *>(this)); |
| 808 | StopPrivateStateThread(); |
| 809 | |
| 810 | // ThreadList::Clear() will try to acquire this process's mutex, so |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 811 | // explicitly clear the thread list here to ensure that the mutex is not |
| 812 | // destroyed before the thread list. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 813 | m_thread_list.Clear(); |
| 814 | } |
| 815 | |
| 816 | const ProcessPropertiesSP &Process::GetGlobalProperties() { |
| 817 | // NOTE: intentional leak so we don't crash if global destructor chain gets |
| 818 | // called as other threads still use the result of this function |
Pavel Labath | 5f05ea8 | 2016-10-19 15:12:45 +0000 | [diff] [blame] | 819 | static ProcessPropertiesSP *g_settings_sp_ptr = |
| 820 | new ProcessPropertiesSP(new ProcessProperties(nullptr)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 821 | return *g_settings_sp_ptr; |
| 822 | } |
| 823 | |
| 824 | void Process::Finalize() { |
| 825 | m_finalizing = true; |
| 826 | |
| 827 | // Destroy this process if needed |
| 828 | switch (GetPrivateState()) { |
| 829 | case eStateConnected: |
| 830 | case eStateAttaching: |
| 831 | case eStateLaunching: |
| 832 | case eStateStopped: |
| 833 | case eStateRunning: |
| 834 | case eStateStepping: |
| 835 | case eStateCrashed: |
| 836 | case eStateSuspended: |
| 837 | Destroy(false); |
| 838 | break; |
| 839 | |
| 840 | case eStateInvalid: |
| 841 | case eStateUnloaded: |
| 842 | case eStateDetached: |
| 843 | case eStateExited: |
| 844 | break; |
| 845 | } |
| 846 | |
| 847 | // Clear our broadcaster before we proceed with destroying |
| 848 | Broadcaster::Clear(); |
| 849 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 850 | // Do any cleanup needed prior to being destructed... Subclasses that |
| 851 | // override this method should call this superclass method as well. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 852 | |
| 853 | // We need to destroy the loader before the derived Process class gets |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 854 | // destroyed since it is very likely that undoing the loader will require |
| 855 | // access to the real process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 856 | m_dynamic_checkers_ap.reset(); |
| 857 | m_abi_sp.reset(); |
| 858 | m_os_ap.reset(); |
| 859 | m_system_runtime_ap.reset(); |
| 860 | m_dyld_ap.reset(); |
| 861 | m_jit_loaders_ap.reset(); |
| 862 | m_thread_list_real.Destroy(); |
| 863 | m_thread_list.Destroy(); |
| 864 | m_extended_thread_list.Destroy(); |
| 865 | m_queue_list.Clear(); |
| 866 | m_queue_list_stop_id = 0; |
| 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(); |
| 873 | m_instrumentation_runtimes.clear(); |
| 874 | m_next_event_action_ap.reset(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 875 | // Clear the last natural stop ID since it has a strong reference to this |
| 876 | // process |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 877 | m_mod_id.SetStopEventForLastNaturalStopID(EventSP()); |
| 878 | //#ifdef LLDB_CONFIGURATION_DEBUG |
| 879 | // StreamFile s(stdout, false); |
| 880 | // EventSP event_sp; |
| 881 | // while (m_private_state_listener_sp->GetNextEvent(event_sp)) |
| 882 | // { |
| 883 | // event_sp->Dump (&s); |
| 884 | // s.EOL(); |
| 885 | // } |
| 886 | //#endif |
| 887 | // We have to be very careful here as the m_private_state_listener might |
| 888 | // contain events that have ProcessSP values in them which can keep this |
| 889 | // process around forever. These events need to be cleared out. |
| 890 | m_private_state_listener_sp->Clear(); |
| 891 | m_public_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 892 | m_public_run_lock.SetStopped(); |
| 893 | m_private_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 894 | m_private_run_lock.SetStopped(); |
Jason Molenda | 3826727 | 2016-12-16 02:48:39 +0000 | [diff] [blame] | 895 | m_structured_data_plugin_map.clear(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 896 | m_finalize_called = true; |
| 897 | } |
| 898 | |
| 899 | void Process::RegisterNotificationCallbacks(const Notifications &callbacks) { |
| 900 | m_notifications.push_back(callbacks); |
| 901 | if (callbacks.initialize != nullptr) |
| 902 | callbacks.initialize(callbacks.baton, this); |
| 903 | } |
| 904 | |
| 905 | bool Process::UnregisterNotificationCallbacks(const Notifications &callbacks) { |
| 906 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 907 | for (pos = m_notifications.begin(); pos != end; ++pos) { |
| 908 | if (pos->baton == callbacks.baton && |
| 909 | pos->initialize == callbacks.initialize && |
| 910 | pos->process_state_changed == callbacks.process_state_changed) { |
| 911 | m_notifications.erase(pos); |
| 912 | return true; |
| 913 | } |
| 914 | } |
| 915 | return false; |
| 916 | } |
| 917 | |
| 918 | void Process::SynchronouslyNotifyStateChanged(StateType state) { |
| 919 | std::vector<Notifications>::iterator notification_pos, |
| 920 | notification_end = m_notifications.end(); |
| 921 | for (notification_pos = m_notifications.begin(); |
| 922 | notification_pos != notification_end; ++notification_pos) { |
| 923 | if (notification_pos->process_state_changed) |
| 924 | notification_pos->process_state_changed(notification_pos->baton, this, |
| 925 | state); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | // FIXME: We need to do some work on events before the general Listener sees |
| 930 | // them. |
| 931 | // For instance if we are continuing from a breakpoint, we need to ensure that |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 932 | // we do the little "insert real insn, step & stop" trick. But we can't do |
| 933 | // that when the event is delivered by the broadcaster - since that is done on |
| 934 | // the thread that is waiting for new events, so if we needed more than one |
| 935 | // event for our handling, we would stall. So instead we do it when we fetch |
| 936 | // the event off of the queue. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 937 | // |
| 938 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 939 | StateType Process::GetNextEvent(EventSP &event_sp) { |
| 940 | StateType state = eStateInvalid; |
| 941 | |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 942 | if (m_listener_sp->GetEventForBroadcaster(this, event_sp, |
| 943 | std::chrono::seconds(0)) && |
| 944 | event_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 945 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 946 | |
| 947 | return state; |
| 948 | } |
| 949 | |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 950 | void Process::SyncIOHandler(uint32_t iohandler_id, |
| 951 | const Timeout<std::micro> &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 952 | // don't sync (potentially context switch) in case where there is no process |
| 953 | // IO |
| 954 | if (!m_process_input_reader) |
| 955 | return; |
| 956 | |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 957 | auto Result = m_iohandler_sync.WaitForValueNotEqualTo(iohandler_id, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 958 | |
| 959 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 960 | if (Result) { |
| 961 | LLDB_LOG( |
| 962 | log, |
| 963 | "waited from m_iohandler_sync to change from {0}. New value is {1}.", |
| 964 | iohandler_id, *Result); |
| 965 | } else { |
| 966 | LLDB_LOG(log, "timed out waiting for m_iohandler_sync to change from {0}.", |
| 967 | iohandler_id); |
| 968 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 971 | StateType Process::WaitForProcessToStop(const Timeout<std::micro> &timeout, |
| 972 | EventSP *event_sp_ptr, bool wait_always, |
| 973 | ListenerSP hijack_listener_sp, |
| 974 | Stream *stream, bool use_run_lock) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 975 | // We can't just wait for a "stopped" event, because the stopped event may |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 976 | // have restarted the target. We have to actually check each event, and in |
| 977 | // the case of a stopped event check the restarted flag on the event. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 978 | if (event_sp_ptr) |
| 979 | event_sp_ptr->reset(); |
| 980 | StateType state = GetState(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 981 | // If we are exited or detached, we won't ever get back to any other valid |
| 982 | // state... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 983 | if (state == eStateDetached || state == eStateExited) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 984 | return state; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 985 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 986 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 987 | LLDB_LOG(log, "timeout = {0}", timeout); |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 988 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 989 | if (!wait_always && StateIsStoppedState(state, true) && |
| 990 | StateIsStoppedState(GetPrivateState(), true)) { |
Pavel Labath | 4446487 | 2015-05-27 12:40:32 +0000 | [diff] [blame] | 991 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 992 | log->Printf("Process::%s returning without waiting for events; process " |
| 993 | "private and public states are already 'stopped'.", |
| 994 | __FUNCTION__); |
| 995 | // We need to toggle the run lock as this won't get done in |
| 996 | // SetPublicState() if the process is hijacked. |
| 997 | if (hijack_listener_sp && use_run_lock) |
| 998 | m_public_run_lock.SetStopped(); |
| 999 | return state; |
| 1000 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1001 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1002 | while (state != eStateInvalid) { |
| 1003 | EventSP event_sp; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1004 | state = GetStateChangedEvents(event_sp, timeout, hijack_listener_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1005 | if (event_sp_ptr && event_sp) |
| 1006 | *event_sp_ptr = event_sp; |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1007 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1008 | bool pop_process_io_handler = (hijack_listener_sp.get() != nullptr); |
| 1009 | Process::HandleProcessStateChangedEvent(event_sp, stream, |
| 1010 | pop_process_io_handler); |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 1011 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1012 | switch (state) { |
| 1013 | case eStateCrashed: |
| 1014 | case eStateDetached: |
| 1015 | case eStateExited: |
| 1016 | case eStateUnloaded: |
| 1017 | // We need to toggle the run lock as this won't get done in |
| 1018 | // SetPublicState() if the process is hijacked. |
| 1019 | if (hijack_listener_sp && use_run_lock) |
| 1020 | m_public_run_lock.SetStopped(); |
| 1021 | return state; |
| 1022 | case eStateStopped: |
| 1023 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 1024 | continue; |
| 1025 | else { |
Pavel Labath | 78521ef | 2015-03-06 10:52:47 +0000 | [diff] [blame] | 1026 | // We need to toggle the run lock as this won't get done in |
| 1027 | // SetPublicState() if the process is hijacked. |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 1028 | if (hijack_listener_sp && use_run_lock) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1029 | m_public_run_lock.SetStopped(); |
| 1030 | return state; |
| 1031 | } |
| 1032 | default: |
| 1033 | continue; |
| 1034 | } |
| 1035 | } |
| 1036 | return state; |
| 1037 | } |
| 1038 | |
| 1039 | bool Process::HandleProcessStateChangedEvent(const EventSP &event_sp, |
| 1040 | Stream *stream, |
| 1041 | bool &pop_process_io_handler) { |
| 1042 | const bool handle_pop = pop_process_io_handler; |
| 1043 | |
| 1044 | pop_process_io_handler = false; |
| 1045 | ProcessSP process_sp = |
| 1046 | Process::ProcessEventData::GetProcessFromEvent(event_sp.get()); |
| 1047 | |
| 1048 | if (!process_sp) |
| 1049 | return false; |
| 1050 | |
| 1051 | StateType event_state = |
| 1052 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1053 | if (event_state == eStateInvalid) |
| 1054 | return false; |
| 1055 | |
| 1056 | switch (event_state) { |
| 1057 | case eStateInvalid: |
| 1058 | case eStateUnloaded: |
| 1059 | case eStateAttaching: |
| 1060 | case eStateLaunching: |
| 1061 | case eStateStepping: |
| 1062 | case eStateDetached: |
| 1063 | if (stream) |
| 1064 | stream->Printf("Process %" PRIu64 " %s\n", process_sp->GetID(), |
| 1065 | StateAsCString(event_state)); |
| 1066 | if (event_state == eStateDetached) |
| 1067 | pop_process_io_handler = true; |
| 1068 | break; |
| 1069 | |
| 1070 | case eStateConnected: |
| 1071 | case eStateRunning: |
| 1072 | // Don't be chatty when we run... |
| 1073 | break; |
| 1074 | |
| 1075 | case eStateExited: |
| 1076 | if (stream) |
| 1077 | process_sp->GetStatus(*stream); |
| 1078 | pop_process_io_handler = true; |
| 1079 | break; |
| 1080 | |
| 1081 | case eStateStopped: |
| 1082 | case eStateCrashed: |
| 1083 | case eStateSuspended: |
| 1084 | // Make sure the program hasn't been auto-restarted: |
| 1085 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) { |
| 1086 | if (stream) { |
| 1087 | size_t num_reasons = |
| 1088 | Process::ProcessEventData::GetNumRestartedReasons(event_sp.get()); |
| 1089 | if (num_reasons > 0) { |
| 1090 | // FIXME: Do we want to report this, or would that just be annoyingly |
| 1091 | // chatty? |
| 1092 | if (num_reasons == 1) { |
| 1093 | const char *reason = |
| 1094 | Process::ProcessEventData::GetRestartedReasonAtIndex( |
| 1095 | event_sp.get(), 0); |
| 1096 | stream->Printf("Process %" PRIu64 " stopped and restarted: %s\n", |
| 1097 | process_sp->GetID(), |
| 1098 | reason ? reason : "<UNKNOWN REASON>"); |
| 1099 | } else { |
| 1100 | stream->Printf("Process %" PRIu64 |
| 1101 | " stopped and restarted, reasons:\n", |
| 1102 | process_sp->GetID()); |
| 1103 | |
| 1104 | for (size_t i = 0; i < num_reasons; i++) { |
| 1105 | const char *reason = |
| 1106 | Process::ProcessEventData::GetRestartedReasonAtIndex( |
| 1107 | event_sp.get(), i); |
| 1108 | stream->Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>"); |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | } else { |
| 1114 | StopInfoSP curr_thread_stop_info_sp; |
| 1115 | // Lock the thread list so it doesn't change on us, this is the scope for |
| 1116 | // the locker: |
| 1117 | { |
| 1118 | ThreadList &thread_list = process_sp->GetThreadList(); |
| 1119 | std::lock_guard<std::recursive_mutex> guard(thread_list.GetMutex()); |
| 1120 | |
| 1121 | ThreadSP curr_thread(thread_list.GetSelectedThread()); |
| 1122 | ThreadSP thread; |
| 1123 | StopReason curr_thread_stop_reason = eStopReasonInvalid; |
| 1124 | if (curr_thread) { |
| 1125 | curr_thread_stop_reason = curr_thread->GetStopReason(); |
| 1126 | curr_thread_stop_info_sp = curr_thread->GetStopInfo(); |
| 1127 | } |
| 1128 | if (!curr_thread || !curr_thread->IsValid() || |
| 1129 | curr_thread_stop_reason == eStopReasonInvalid || |
| 1130 | curr_thread_stop_reason == eStopReasonNone) { |
| 1131 | // Prefer a thread that has just completed its plan over another |
| 1132 | // thread as current thread. |
| 1133 | ThreadSP plan_thread; |
| 1134 | ThreadSP other_thread; |
| 1135 | |
| 1136 | const size_t num_threads = thread_list.GetSize(); |
| 1137 | size_t i; |
| 1138 | for (i = 0; i < num_threads; ++i) { |
| 1139 | thread = thread_list.GetThreadAtIndex(i); |
| 1140 | StopReason thread_stop_reason = thread->GetStopReason(); |
| 1141 | switch (thread_stop_reason) { |
| 1142 | case eStopReasonInvalid: |
| 1143 | case eStopReasonNone: |
| 1144 | break; |
| 1145 | |
| 1146 | case eStopReasonSignal: { |
| 1147 | // Don't select a signal thread if we weren't going to stop at |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1148 | // that signal. We have to have had another reason for stopping |
| 1149 | // here, and the user doesn't want to see this thread. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1150 | uint64_t signo = thread->GetStopInfo()->GetValue(); |
| 1151 | if (process_sp->GetUnixSignals()->GetShouldStop(signo)) { |
| 1152 | if (!other_thread) |
| 1153 | other_thread = thread; |
| 1154 | } |
| 1155 | break; |
| 1156 | } |
| 1157 | case eStopReasonTrace: |
| 1158 | case eStopReasonBreakpoint: |
| 1159 | case eStopReasonWatchpoint: |
| 1160 | case eStopReasonException: |
| 1161 | case eStopReasonExec: |
| 1162 | case eStopReasonThreadExiting: |
| 1163 | case eStopReasonInstrumentation: |
| 1164 | if (!other_thread) |
| 1165 | other_thread = thread; |
| 1166 | break; |
| 1167 | case eStopReasonPlanComplete: |
| 1168 | if (!plan_thread) |
| 1169 | plan_thread = thread; |
| 1170 | break; |
| 1171 | } |
| 1172 | } |
| 1173 | if (plan_thread) |
| 1174 | thread_list.SetSelectedThreadByID(plan_thread->GetID()); |
| 1175 | else if (other_thread) |
| 1176 | thread_list.SetSelectedThreadByID(other_thread->GetID()); |
| 1177 | else { |
| 1178 | if (curr_thread && curr_thread->IsValid()) |
| 1179 | thread = curr_thread; |
| 1180 | else |
| 1181 | thread = thread_list.GetThreadAtIndex(0); |
| 1182 | |
| 1183 | if (thread) |
| 1184 | thread_list.SetSelectedThreadByID(thread->GetID()); |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | // Drop the ThreadList mutex by here, since GetThreadStatus below might |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1189 | // have to run code, e.g. for Data formatters, and if we hold the |
| 1190 | // ThreadList mutex, then the process is going to have a hard time |
| 1191 | // restarting the process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1192 | if (stream) { |
| 1193 | Debugger &debugger = process_sp->GetTarget().GetDebugger(); |
| 1194 | if (debugger.GetTargetList().GetSelectedTarget().get() == |
| 1195 | &process_sp->GetTarget()) { |
| 1196 | const bool only_threads_with_stop_reason = true; |
| 1197 | const uint32_t start_frame = 0; |
| 1198 | const uint32_t num_frames = 1; |
| 1199 | const uint32_t num_frames_with_source = 1; |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 1200 | const bool stop_format = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1201 | process_sp->GetStatus(*stream); |
| 1202 | process_sp->GetThreadStatus(*stream, only_threads_with_stop_reason, |
| 1203 | start_frame, num_frames, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 1204 | num_frames_with_source, |
| 1205 | stop_format); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1206 | if (curr_thread_stop_info_sp) { |
| 1207 | lldb::addr_t crashing_address; |
| 1208 | ValueObjectSP valobj_sp = StopInfo::GetCrashingDereference( |
| 1209 | curr_thread_stop_info_sp, &crashing_address); |
| 1210 | if (valobj_sp) { |
| 1211 | const bool qualify_cxx_base_classes = false; |
| 1212 | |
| 1213 | const ValueObject::GetExpressionPathFormat format = |
| 1214 | ValueObject::GetExpressionPathFormat:: |
| 1215 | eGetExpressionPathFormatHonorPointers; |
| 1216 | stream->PutCString("Likely cause: "); |
| 1217 | valobj_sp->GetExpressionPath(*stream, qualify_cxx_base_classes, |
| 1218 | format); |
| 1219 | stream->Printf(" accessed 0x%" PRIx64 "\n", crashing_address); |
| 1220 | } |
| 1221 | } |
| 1222 | } else { |
| 1223 | uint32_t target_idx = debugger.GetTargetList().GetIndexOfTarget( |
| 1224 | process_sp->GetTarget().shared_from_this()); |
| 1225 | if (target_idx != UINT32_MAX) |
| 1226 | stream->Printf("Target %d: (", target_idx); |
| 1227 | else |
| 1228 | stream->Printf("Target <unknown index>: ("); |
| 1229 | process_sp->GetTarget().Dump(stream, eDescriptionLevelBrief); |
| 1230 | stream->Printf(") stopped.\n"); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | // Pop the process IO handler |
| 1235 | pop_process_io_handler = true; |
| 1236 | } |
| 1237 | break; |
| 1238 | } |
| 1239 | |
| 1240 | if (handle_pop && pop_process_io_handler) |
| 1241 | process_sp->PopProcessIOHandler(); |
| 1242 | |
| 1243 | return true; |
| 1244 | } |
| 1245 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1246 | bool Process::HijackProcessEvents(ListenerSP listener_sp) { |
| 1247 | if (listener_sp) { |
| 1248 | return HijackBroadcaster(listener_sp, eBroadcastBitStateChanged | |
| 1249 | eBroadcastBitInterrupt); |
| 1250 | } else |
| 1251 | return false; |
| 1252 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1253 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1254 | void Process::RestoreProcessEvents() { RestoreBroadcaster(); } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1255 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1256 | StateType Process::GetStateChangedEvents(EventSP &event_sp, |
| 1257 | const Timeout<std::micro> &timeout, |
| 1258 | ListenerSP hijack_listener_sp) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1259 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1260 | LLDB_LOG(log, "timeout = {0}, event_sp)...", timeout); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1261 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1262 | ListenerSP listener_sp = hijack_listener_sp; |
| 1263 | if (!listener_sp) |
| 1264 | listener_sp = m_listener_sp; |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1265 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1266 | StateType state = eStateInvalid; |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 1267 | if (listener_sp->GetEventForBroadcasterWithType( |
| 1268 | this, eBroadcastBitStateChanged | eBroadcastBitInterrupt, event_sp, |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1269 | timeout)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1270 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1271 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1272 | else |
| 1273 | LLDB_LOG(log, "got no event or was interrupted."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1274 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1275 | |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1276 | LLDB_LOG(log, "timeout = {0}, event_sp) => {1}", timeout, state); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1277 | return state; |
| 1278 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1279 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1280 | Event *Process::PeekAtStateChangedEvents() { |
| 1281 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1283 | if (log) |
| 1284 | log->Printf("Process::%s...", __FUNCTION__); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1285 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1286 | Event *event_ptr; |
| 1287 | event_ptr = m_listener_sp->PeekAtNextEventForBroadcasterWithType( |
| 1288 | this, eBroadcastBitStateChanged); |
| 1289 | if (log) { |
| 1290 | if (event_ptr) { |
| 1291 | log->Printf( |
| 1292 | "Process::%s (event_ptr) => %s", __FUNCTION__, |
| 1293 | StateAsCString(ProcessEventData::GetStateFromEvent(event_ptr))); |
| 1294 | } else { |
| 1295 | log->Printf("Process::%s no events found", __FUNCTION__); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1296 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1297 | } |
| 1298 | return event_ptr; |
| 1299 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1300 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1301 | StateType |
| 1302 | Process::GetStateChangedEventsPrivate(EventSP &event_sp, |
| 1303 | const Timeout<std::micro> &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1304 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1305 | LLDB_LOG(log, "timeout = {0}, event_sp)...", timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1306 | |
| 1307 | StateType state = eStateInvalid; |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 1308 | if (m_private_state_listener_sp->GetEventForBroadcasterWithType( |
| 1309 | &m_private_state_broadcaster, |
| 1310 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, event_sp, |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1311 | timeout)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1312 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1313 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1314 | |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1315 | LLDB_LOG(log, "timeout = {0}, event_sp) => {1}", timeout, |
| 1316 | state == eStateInvalid ? "TIMEOUT" : StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1317 | return state; |
| 1318 | } |
| 1319 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1320 | bool Process::GetEventsPrivate(EventSP &event_sp, |
| 1321 | const Timeout<std::micro> &timeout, |
| 1322 | bool control_only) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1323 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1324 | LLDB_LOG(log, "timeout = {0}, event_sp)...", timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1325 | |
| 1326 | if (control_only) |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 1327 | return m_private_state_listener_sp->GetEventForBroadcaster( |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1328 | &m_private_state_control_broadcaster, event_sp, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1329 | else |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1330 | return m_private_state_listener_sp->GetEvent(event_sp, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | bool Process::IsRunning() const { |
| 1334 | return StateIsRunningState(m_public_state.GetValue()); |
| 1335 | } |
| 1336 | |
| 1337 | int Process::GetExitStatus() { |
| 1338 | std::lock_guard<std::mutex> guard(m_exit_status_mutex); |
| 1339 | |
| 1340 | if (m_public_state.GetValue() == eStateExited) |
| 1341 | return m_exit_status; |
| 1342 | return -1; |
| 1343 | } |
| 1344 | |
| 1345 | const char *Process::GetExitDescription() { |
| 1346 | std::lock_guard<std::mutex> guard(m_exit_status_mutex); |
| 1347 | |
| 1348 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 1349 | return m_exit_string.c_str(); |
| 1350 | return nullptr; |
| 1351 | } |
| 1352 | |
| 1353 | bool Process::SetExitStatus(int status, const char *cstr) { |
| 1354 | // Use a mutex to protect setting the exit status. |
| 1355 | std::lock_guard<std::mutex> guard(m_exit_status_mutex); |
| 1356 | |
| 1357 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1358 | LIBLLDB_LOG_PROCESS)); |
| 1359 | if (log) |
| 1360 | log->Printf( |
| 1361 | "Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)", |
| 1362 | status, status, cstr ? "\"" : "", cstr ? cstr : "NULL", |
| 1363 | cstr ? "\"" : ""); |
| 1364 | |
| 1365 | // We were already in the exited state |
| 1366 | if (m_private_state.GetValue() == eStateExited) { |
| 1367 | if (log) |
| 1368 | log->Printf("Process::SetExitStatus () ignoring exit status because " |
| 1369 | "state was already set to eStateExited"); |
| 1370 | return false; |
| 1371 | } |
| 1372 | |
| 1373 | m_exit_status = status; |
| 1374 | if (cstr) |
| 1375 | m_exit_string = cstr; |
| 1376 | else |
| 1377 | m_exit_string.clear(); |
| 1378 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1379 | // Clear the last natural stop ID since it has a strong reference to this |
| 1380 | // process |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1381 | m_mod_id.SetStopEventForLastNaturalStopID(EventSP()); |
| 1382 | |
| 1383 | SetPrivateState(eStateExited); |
| 1384 | |
| 1385 | // Allow subclasses to do some cleanup |
| 1386 | DidExit(); |
| 1387 | |
| 1388 | return true; |
| 1389 | } |
| 1390 | |
| 1391 | bool Process::IsAlive() { |
| 1392 | switch (m_private_state.GetValue()) { |
| 1393 | case eStateConnected: |
| 1394 | case eStateAttaching: |
| 1395 | case eStateLaunching: |
| 1396 | case eStateStopped: |
| 1397 | case eStateRunning: |
| 1398 | case eStateStepping: |
| 1399 | case eStateCrashed: |
| 1400 | case eStateSuspended: |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1401 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1402 | default: |
| 1403 | return false; |
| 1404 | } |
Jason Molenda | a814f70 | 2015-11-05 23:03:44 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1407 | // This static callback can be used to watch for local child processes on the |
| 1408 | // current host. The child process exits, the process will be found in the |
| 1409 | // global target list (we want to be completely sure that the |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1410 | // lldb_private::Process doesn't go away before we can deliver the signal. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1411 | bool Process::SetProcessExitStatus( |
| 1412 | lldb::pid_t pid, bool exited, |
| 1413 | int signo, // Zero for no signal |
| 1414 | int exit_status // Exit value of process if signal is zero |
| 1415 | ) { |
| 1416 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1417 | if (log) |
| 1418 | log->Printf("Process::SetProcessExitStatus (pid=%" PRIu64 |
| 1419 | ", exited=%i, signal=%i, exit_status=%i)\n", |
| 1420 | pid, exited, signo, exit_status); |
| 1421 | |
| 1422 | if (exited) { |
| 1423 | TargetSP target_sp(Debugger::FindTargetWithProcessID(pid)); |
| 1424 | if (target_sp) { |
| 1425 | ProcessSP process_sp(target_sp->GetProcessSP()); |
| 1426 | if (process_sp) { |
| 1427 | const char *signal_cstr = nullptr; |
| 1428 | if (signo) |
| 1429 | signal_cstr = process_sp->GetUnixSignals()->GetSignalAsCString(signo); |
| 1430 | |
| 1431 | process_sp->SetExitStatus(exit_status, signal_cstr); |
| 1432 | } |
| 1433 | } |
| 1434 | return true; |
| 1435 | } |
| 1436 | return false; |
| 1437 | } |
| 1438 | |
| 1439 | void Process::UpdateThreadListIfNeeded() { |
| 1440 | const uint32_t stop_id = GetStopID(); |
| 1441 | if (m_thread_list.GetSize(false) == 0 || |
| 1442 | stop_id != m_thread_list.GetStopID()) { |
| 1443 | const StateType state = GetPrivateState(); |
| 1444 | if (StateIsStoppedState(state, true)) { |
| 1445 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1446 | // m_thread_list does have its own mutex, but we need to hold onto the |
| 1447 | // mutex between the call to UpdateThreadList(...) and the |
| 1448 | // os->UpdateThreadList(...) so it doesn't change on us |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1449 | ThreadList &old_thread_list = m_thread_list; |
| 1450 | ThreadList real_thread_list(this); |
| 1451 | ThreadList new_thread_list(this); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1452 | // Always update the thread list with the protocol specific thread list, |
| 1453 | // but only update if "true" is returned |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1454 | if (UpdateThreadList(m_thread_list_real, real_thread_list)) { |
| 1455 | // Don't call into the OperatingSystem to update the thread list if we |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1456 | // are shutting down, since that may call back into the SBAPI's, |
| 1457 | // requiring the API lock which is already held by whoever is shutting |
| 1458 | // us down, causing a deadlock. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1459 | OperatingSystem *os = GetOperatingSystem(); |
| 1460 | if (os && !m_destroy_in_process) { |
| 1461 | // Clear any old backing threads where memory threads might have been |
| 1462 | // backed by actual threads from the lldb_private::Process subclass |
| 1463 | size_t num_old_threads = old_thread_list.GetSize(false); |
| 1464 | for (size_t i = 0; i < num_old_threads; ++i) |
| 1465 | old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread(); |
| 1466 | |
| 1467 | // Turn off dynamic types to ensure we don't run any expressions. |
Adrian Prantl | 4e8be2c | 2018-06-13 16:21:24 +0000 | [diff] [blame] | 1468 | // Objective-C can run an expression to determine if a SBValue is a |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1469 | // dynamic type or not and we need to avoid this. OperatingSystem |
| 1470 | // plug-ins can't run expressions that require running code... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1471 | |
| 1472 | Target &target = GetTarget(); |
| 1473 | const lldb::DynamicValueType saved_prefer_dynamic = |
| 1474 | target.GetPreferDynamicValue(); |
| 1475 | if (saved_prefer_dynamic != lldb::eNoDynamicValues) |
| 1476 | target.SetPreferDynamicValue(lldb::eNoDynamicValues); |
| 1477 | |
| 1478 | // Now let the OperatingSystem plug-in update the thread list |
| 1479 | |
| 1480 | os->UpdateThreadList( |
| 1481 | old_thread_list, // Old list full of threads created by OS plug-in |
| 1482 | real_thread_list, // The actual thread list full of threads |
| 1483 | // created by each lldb_private::Process |
| 1484 | // subclass |
| 1485 | new_thread_list); // The new thread list that we will show to the |
| 1486 | // user that gets filled in |
| 1487 | |
| 1488 | if (saved_prefer_dynamic != lldb::eNoDynamicValues) |
| 1489 | target.SetPreferDynamicValue(saved_prefer_dynamic); |
| 1490 | } else { |
| 1491 | // No OS plug-in, the new thread list is the same as the real thread |
| 1492 | // list |
| 1493 | new_thread_list = real_thread_list; |
| 1494 | } |
| 1495 | |
| 1496 | m_thread_list_real.Update(real_thread_list); |
| 1497 | m_thread_list.Update(new_thread_list); |
| 1498 | m_thread_list.SetStopID(stop_id); |
| 1499 | |
| 1500 | if (GetLastNaturalStopID() != m_extended_thread_stop_id) { |
| 1501 | // Clear any extended threads that we may have accumulated previously |
| 1502 | m_extended_thread_list.Clear(); |
| 1503 | m_extended_thread_stop_id = GetLastNaturalStopID(); |
| 1504 | |
| 1505 | m_queue_list.Clear(); |
| 1506 | m_queue_list_stop_id = GetLastNaturalStopID(); |
| 1507 | } |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | void Process::UpdateQueueListIfNeeded() { |
| 1514 | if (m_system_runtime_ap) { |
| 1515 | if (m_queue_list.GetSize() == 0 || |
| 1516 | m_queue_list_stop_id != GetLastNaturalStopID()) { |
| 1517 | const StateType state = GetPrivateState(); |
| 1518 | if (StateIsStoppedState(state, true)) { |
| 1519 | m_system_runtime_ap->PopulateQueueList(m_queue_list); |
| 1520 | m_queue_list_stop_id = GetLastNaturalStopID(); |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | ThreadSP Process::CreateOSPluginThread(lldb::tid_t tid, lldb::addr_t context) { |
| 1527 | OperatingSystem *os = GetOperatingSystem(); |
| 1528 | if (os) |
| 1529 | return os->CreateThread(tid, context); |
| 1530 | return ThreadSP(); |
| 1531 | } |
| 1532 | |
| 1533 | uint32_t Process::GetNextThreadIndexID(uint64_t thread_id) { |
| 1534 | return AssignIndexIDToThread(thread_id); |
| 1535 | } |
| 1536 | |
| 1537 | bool Process::HasAssignedIndexIDToThread(uint64_t thread_id) { |
| 1538 | return (m_thread_id_to_index_id_map.find(thread_id) != |
| 1539 | m_thread_id_to_index_id_map.end()); |
| 1540 | } |
| 1541 | |
| 1542 | uint32_t Process::AssignIndexIDToThread(uint64_t thread_id) { |
| 1543 | uint32_t result = 0; |
| 1544 | std::map<uint64_t, uint32_t>::iterator iterator = |
| 1545 | m_thread_id_to_index_id_map.find(thread_id); |
| 1546 | if (iterator == m_thread_id_to_index_id_map.end()) { |
| 1547 | result = ++m_thread_index_id; |
| 1548 | m_thread_id_to_index_id_map[thread_id] = result; |
| 1549 | } else { |
| 1550 | result = iterator->second; |
| 1551 | } |
| 1552 | |
| 1553 | return result; |
| 1554 | } |
| 1555 | |
| 1556 | StateType Process::GetState() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1557 | return m_public_state.GetValue(); |
| 1558 | } |
| 1559 | |
| 1560 | bool Process::StateChangedIsExternallyHijacked() { |
| 1561 | if (IsHijackedForEvent(eBroadcastBitStateChanged)) { |
| 1562 | const char *hijacking_name = GetHijackingListenerName(); |
| 1563 | if (hijacking_name && |
| 1564 | strcmp(hijacking_name, "lldb.Process.ResumeSynchronous.hijack")) |
| 1565 | return true; |
| 1566 | } |
| 1567 | return false; |
| 1568 | } |
| 1569 | |
| 1570 | void Process::SetPublicState(StateType new_state, bool restarted) { |
| 1571 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1572 | LIBLLDB_LOG_PROCESS)); |
| 1573 | if (log) |
| 1574 | log->Printf("Process::SetPublicState (state = %s, restarted = %i)", |
| 1575 | StateAsCString(new_state), restarted); |
| 1576 | const StateType old_state = m_public_state.GetValue(); |
| 1577 | m_public_state.SetValue(new_state); |
| 1578 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1579 | // On the transition from Run to Stopped, we unlock the writer end of the run |
| 1580 | // lock. The lock gets locked in Resume, which is the public API to tell the |
| 1581 | // program to run. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1582 | if (!StateChangedIsExternallyHijacked()) { |
| 1583 | if (new_state == eStateDetached) { |
| 1584 | if (log) |
| 1585 | log->Printf( |
| 1586 | "Process::SetPublicState (%s) -- unlocking run lock for detach", |
| 1587 | StateAsCString(new_state)); |
| 1588 | m_public_run_lock.SetStopped(); |
| 1589 | } else { |
| 1590 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1591 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1592 | if ((old_state_is_stopped != new_state_is_stopped)) { |
| 1593 | if (new_state_is_stopped && !restarted) { |
| 1594 | if (log) |
| 1595 | log->Printf("Process::SetPublicState (%s) -- unlocking run lock", |
| 1596 | StateAsCString(new_state)); |
| 1597 | m_public_run_lock.SetStopped(); |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | } |
| 1603 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1604 | Status Process::Resume() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1605 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1606 | LIBLLDB_LOG_PROCESS)); |
| 1607 | if (log) |
| 1608 | log->Printf("Process::Resume -- locking run lock"); |
| 1609 | if (!m_public_run_lock.TrySetRunning()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1610 | Status error("Resume request failed - process still running."); |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1611 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1612 | log->Printf("Process::Resume: -- TrySetRunning failed, not resuming."); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1613 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1614 | } |
Adrian McCarthy | 3887ba8 | 2017-09-19 18:07:33 +0000 | [diff] [blame] | 1615 | Status error = PrivateResume(); |
| 1616 | if (!error.Success()) { |
| 1617 | // Undo running state change |
| 1618 | m_public_run_lock.SetStopped(); |
| 1619 | } |
| 1620 | return error; |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1623 | Status Process::ResumeSynchronous(Stream *stream) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1624 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1625 | LIBLLDB_LOG_PROCESS)); |
| 1626 | if (log) |
| 1627 | log->Printf("Process::ResumeSynchronous -- locking run lock"); |
| 1628 | if (!m_public_run_lock.TrySetRunning()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1629 | Status error("Resume request failed - process still running."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1630 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1631 | log->Printf("Process::Resume: -- TrySetRunning failed, not resuming."); |
| 1632 | return error; |
| 1633 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1634 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1635 | ListenerSP listener_sp( |
| 1636 | Listener::MakeListener("lldb.Process.ResumeSynchronous.hijack")); |
| 1637 | HijackProcessEvents(listener_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1638 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1639 | Status error = PrivateResume(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1640 | if (error.Success()) { |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1641 | StateType state = |
| 1642 | WaitForProcessToStop(llvm::None, NULL, true, listener_sp, stream); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1643 | const bool must_be_alive = |
| 1644 | false; // eStateExited is ok, so this must be false |
| 1645 | if (!StateIsStoppedState(state, must_be_alive)) |
| 1646 | error.SetErrorStringWithFormat( |
| 1647 | "process not in stopped state after synchronous resume: %s", |
| 1648 | StateAsCString(state)); |
Adrian McCarthy | 3887ba8 | 2017-09-19 18:07:33 +0000 | [diff] [blame] | 1649 | } else { |
| 1650 | // Undo running state change |
| 1651 | m_public_run_lock.SetStopped(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1652 | } |
Ed Maste | c29693f | 2013-07-02 16:35:47 +0000 | [diff] [blame] | 1653 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1654 | // Undo the hijacking of process events... |
| 1655 | RestoreProcessEvents(); |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1656 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1657 | return error; |
| 1658 | } |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1659 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1660 | StateType Process::GetPrivateState() { return m_private_state.GetValue(); } |
Ilia K | 38810f4 | 2015-05-20 10:15:47 +0000 | [diff] [blame] | 1661 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1662 | void Process::SetPrivateState(StateType new_state) { |
| 1663 | if (m_finalize_called) |
| 1664 | return; |
| 1665 | |
| 1666 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1667 | LIBLLDB_LOG_PROCESS)); |
| 1668 | bool state_changed = false; |
| 1669 | |
| 1670 | if (log) |
| 1671 | log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state)); |
| 1672 | |
| 1673 | std::lock_guard<std::recursive_mutex> thread_guard(m_thread_list.GetMutex()); |
| 1674 | std::lock_guard<std::recursive_mutex> guard(m_private_state.GetMutex()); |
| 1675 | |
| 1676 | const StateType old_state = m_private_state.GetValueNoLock(); |
| 1677 | state_changed = old_state != new_state; |
| 1678 | |
| 1679 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1680 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1681 | if (old_state_is_stopped != new_state_is_stopped) { |
| 1682 | if (new_state_is_stopped) |
| 1683 | m_private_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1684 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1685 | m_private_run_lock.SetRunning(); |
| 1686 | } |
| 1687 | |
| 1688 | if (state_changed) { |
| 1689 | m_private_state.SetValueNoLock(new_state); |
| 1690 | EventSP event_sp( |
| 1691 | new Event(eBroadcastBitStateChanged, |
| 1692 | new ProcessEventData(shared_from_this(), new_state))); |
| 1693 | if (StateIsStoppedState(new_state, false)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1694 | // Note, this currently assumes that all threads in the list stop when |
| 1695 | // the process stops. In the future we will want to support a debugging |
| 1696 | // model where some threads continue to run while others are stopped. |
| 1697 | // When that happens we will either need a way for the thread list to |
| 1698 | // identify which threads are stopping or create a special thread list |
| 1699 | // containing only threads which actually stopped. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1700 | // |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1701 | // The process plugin is responsible for managing the actual behavior of |
| 1702 | // the threads and should have stopped any threads that are going to stop |
| 1703 | // before we get here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1704 | m_thread_list.DidStop(); |
| 1705 | |
| 1706 | m_mod_id.BumpStopID(); |
| 1707 | if (!m_mod_id.IsLastResumeForUserExpression()) |
| 1708 | m_mod_id.SetStopEventForLastNaturalStopID(event_sp); |
| 1709 | m_memory_cache.Clear(); |
| 1710 | if (log) |
| 1711 | log->Printf("Process::SetPrivateState (%s) stop_id = %u", |
| 1712 | StateAsCString(new_state), m_mod_id.GetStopID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1713 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1714 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1715 | // Use our target to get a shared pointer to ourselves... |
| 1716 | if (m_finalize_called && !PrivateStateThreadIsValid()) |
| 1717 | BroadcastEvent(event_sp); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1718 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1719 | m_private_state_broadcaster.BroadcastEvent(event_sp); |
| 1720 | } else { |
| 1721 | if (log) |
| 1722 | log->Printf( |
| 1723 | "Process::SetPrivateState (%s) state didn't change. Ignoring...", |
| 1724 | StateAsCString(new_state)); |
| 1725 | } |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1728 | void Process::SetRunningUserExpression(bool on) { |
| 1729 | m_mod_id.SetRunningUserExpression(on); |
| 1730 | } |
| 1731 | |
Raphael Isemann | c01783a | 2018-08-29 22:50:54 +0000 | [diff] [blame] | 1732 | void Process::SetRunningUtilityFunction(bool on) { |
| 1733 | m_mod_id.SetRunningUtilityFunction(on); |
| 1734 | } |
| 1735 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1736 | addr_t Process::GetImageInfoAddress() { return LLDB_INVALID_ADDRESS; } |
| 1737 | |
| 1738 | const lldb::ABISP &Process::GetABI() { |
| 1739 | if (!m_abi_sp) |
Jason Molenda | 43294c9 | 2017-06-29 02:57:03 +0000 | [diff] [blame] | 1740 | m_abi_sp = ABI::FindPlugin(shared_from_this(), GetTarget().GetArchitecture()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1741 | return m_abi_sp; |
| 1742 | } |
| 1743 | |
| 1744 | LanguageRuntime *Process::GetLanguageRuntime(lldb::LanguageType language, |
| 1745 | bool retry_if_null) { |
| 1746 | if (m_finalizing) |
Eugene Zelenko | da8cf8a | 2016-03-01 00:55:51 +0000 | [diff] [blame] | 1747 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1748 | |
| 1749 | LanguageRuntimeCollection::iterator pos; |
| 1750 | pos = m_language_runtimes.find(language); |
| 1751 | if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second)) { |
| 1752 | lldb::LanguageRuntimeSP runtime_sp( |
| 1753 | LanguageRuntime::FindPlugin(this, language)); |
| 1754 | |
| 1755 | m_language_runtimes[language] = runtime_sp; |
| 1756 | return runtime_sp.get(); |
| 1757 | } else |
| 1758 | return (*pos).second.get(); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1761 | CPPLanguageRuntime *Process::GetCPPLanguageRuntime(bool retry_if_null) { |
| 1762 | LanguageRuntime *runtime = |
| 1763 | GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null); |
| 1764 | if (runtime != nullptr && |
| 1765 | runtime->GetLanguageType() == eLanguageTypeC_plus_plus) |
| 1766 | return static_cast<CPPLanguageRuntime *>(runtime); |
| 1767 | return nullptr; |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1770 | ObjCLanguageRuntime *Process::GetObjCLanguageRuntime(bool retry_if_null) { |
| 1771 | LanguageRuntime *runtime = |
| 1772 | GetLanguageRuntime(eLanguageTypeObjC, retry_if_null); |
| 1773 | if (runtime != nullptr && runtime->GetLanguageType() == eLanguageTypeObjC) |
| 1774 | return static_cast<ObjCLanguageRuntime *>(runtime); |
| 1775 | return nullptr; |
Enrico Granata | fd4c84e | 2012-05-21 16:51:35 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1778 | bool Process::IsPossibleDynamicValue(ValueObject &in_value) { |
| 1779 | if (m_finalizing) |
| 1780 | return false; |
| 1781 | |
| 1782 | if (in_value.IsDynamic()) |
| 1783 | return false; |
| 1784 | LanguageType known_type = in_value.GetObjectRuntimeLanguage(); |
| 1785 | |
| 1786 | if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC) { |
| 1787 | LanguageRuntime *runtime = GetLanguageRuntime(known_type); |
| 1788 | return runtime ? runtime->CouldHaveDynamicValue(in_value) : false; |
| 1789 | } |
| 1790 | |
| 1791 | LanguageRuntime *cpp_runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus); |
| 1792 | if (cpp_runtime && cpp_runtime->CouldHaveDynamicValue(in_value)) |
| 1793 | return true; |
| 1794 | |
| 1795 | LanguageRuntime *objc_runtime = GetLanguageRuntime(eLanguageTypeObjC); |
| 1796 | return objc_runtime ? objc_runtime->CouldHaveDynamicValue(in_value) : false; |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1799 | void Process::SetDynamicCheckers(DynamicCheckerFunctions *dynamic_checkers) { |
| 1800 | m_dynamic_checkers_ap.reset(dynamic_checkers); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1803 | BreakpointSiteList &Process::GetBreakpointSiteList() { |
| 1804 | return m_breakpoint_site_list; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1807 | const BreakpointSiteList &Process::GetBreakpointSiteList() const { |
| 1808 | return m_breakpoint_site_list; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1811 | void Process::DisableAllBreakpointSites() { |
| 1812 | m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void { |
| 1813 | // bp_site->SetEnabled(true); |
| 1814 | DisableBreakpointSite(bp_site); |
| 1815 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1818 | Status Process::ClearBreakpointSiteByID(lldb::user_id_t break_id) { |
| 1819 | Status error(DisableBreakpointSiteByID(break_id)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1820 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1821 | if (error.Success()) |
| 1822 | m_breakpoint_site_list.Remove(break_id); |
| 1823 | |
| 1824 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1827 | Status Process::DisableBreakpointSiteByID(lldb::user_id_t break_id) { |
| 1828 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1829 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID(break_id); |
| 1830 | if (bp_site_sp) { |
| 1831 | if (bp_site_sp->IsEnabled()) |
| 1832 | error = DisableBreakpointSite(bp_site_sp.get()); |
| 1833 | } else { |
| 1834 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, |
| 1835 | break_id); |
| 1836 | } |
| 1837 | |
| 1838 | return error; |
| 1839 | } |
| 1840 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1841 | Status Process::EnableBreakpointSiteByID(lldb::user_id_t break_id) { |
| 1842 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1843 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID(break_id); |
| 1844 | if (bp_site_sp) { |
| 1845 | if (!bp_site_sp->IsEnabled()) |
| 1846 | error = EnableBreakpointSite(bp_site_sp.get()); |
| 1847 | } else { |
| 1848 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, |
| 1849 | break_id); |
| 1850 | } |
| 1851 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Stephen Wilson | 50bd94f | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 1854 | lldb::break_id_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1855 | Process::CreateBreakpointSite(const BreakpointLocationSP &owner, |
| 1856 | bool use_hardware) { |
| 1857 | addr_t load_addr = LLDB_INVALID_ADDRESS; |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1858 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1859 | bool show_error = true; |
| 1860 | switch (GetState()) { |
| 1861 | case eStateInvalid: |
| 1862 | case eStateUnloaded: |
| 1863 | case eStateConnected: |
| 1864 | case eStateAttaching: |
| 1865 | case eStateLaunching: |
| 1866 | case eStateDetached: |
| 1867 | case eStateExited: |
| 1868 | show_error = false; |
| 1869 | break; |
| 1870 | |
| 1871 | case eStateStopped: |
| 1872 | case eStateRunning: |
| 1873 | case eStateStepping: |
| 1874 | case eStateCrashed: |
| 1875 | case eStateSuspended: |
| 1876 | show_error = IsAlive(); |
| 1877 | break; |
| 1878 | } |
| 1879 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1880 | // Reset the IsIndirect flag here, in case the location changes from pointing |
| 1881 | // to a indirect symbol to a regular symbol. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1882 | owner->SetIsIndirect(false); |
| 1883 | |
| 1884 | if (owner->ShouldResolveIndirectFunctions()) { |
| 1885 | Symbol *symbol = owner->GetAddress().CalculateSymbolContextSymbol(); |
| 1886 | if (symbol && symbol->IsIndirect()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1887 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1888 | Address symbol_address = symbol->GetAddress(); |
| 1889 | load_addr = ResolveIndirectFunction(&symbol_address, error); |
| 1890 | if (!error.Success() && show_error) { |
| 1891 | GetTarget().GetDebugger().GetErrorFile()->Printf( |
| 1892 | "warning: failed to resolve indirect function at 0x%" PRIx64 |
| 1893 | " for breakpoint %i.%i: %s\n", |
| 1894 | symbol->GetLoadAddress(&GetTarget()), |
| 1895 | owner->GetBreakpoint().GetID(), owner->GetID(), |
| 1896 | error.AsCString() ? error.AsCString() : "unknown error"); |
| 1897 | return LLDB_INVALID_BREAK_ID; |
| 1898 | } |
| 1899 | Address resolved_address(load_addr); |
| 1900 | load_addr = resolved_address.GetOpcodeLoadAddress(&GetTarget()); |
| 1901 | owner->SetIsIndirect(true); |
| 1902 | } else |
| 1903 | load_addr = owner->GetAddress().GetOpcodeLoadAddress(&GetTarget()); |
| 1904 | } else |
| 1905 | load_addr = owner->GetAddress().GetOpcodeLoadAddress(&GetTarget()); |
| 1906 | |
| 1907 | if (load_addr != LLDB_INVALID_ADDRESS) { |
| 1908 | BreakpointSiteSP bp_site_sp; |
| 1909 | |
| 1910 | // Look up this breakpoint site. If it exists, then add this new owner, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1911 | // otherwise create a new breakpoint site and add it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1912 | |
| 1913 | bp_site_sp = m_breakpoint_site_list.FindByAddress(load_addr); |
| 1914 | |
| 1915 | if (bp_site_sp) { |
| 1916 | bp_site_sp->AddOwner(owner); |
| 1917 | owner->SetBreakpointSite(bp_site_sp); |
| 1918 | return bp_site_sp->GetID(); |
| 1919 | } else { |
| 1920 | bp_site_sp.reset(new BreakpointSite(&m_breakpoint_site_list, owner, |
| 1921 | load_addr, use_hardware)); |
| 1922 | if (bp_site_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1923 | Status error = EnableBreakpointSite(bp_site_sp.get()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1924 | if (error.Success()) { |
| 1925 | owner->SetBreakpointSite(bp_site_sp); |
| 1926 | return m_breakpoint_site_list.Add(bp_site_sp); |
| 1927 | } else { |
| 1928 | if (show_error) { |
| 1929 | // Report error for setting breakpoint... |
| 1930 | GetTarget().GetDebugger().GetErrorFile()->Printf( |
| 1931 | "warning: failed to set breakpoint site at 0x%" PRIx64 |
| 1932 | " for breakpoint %i.%i: %s\n", |
| 1933 | load_addr, owner->GetBreakpoint().GetID(), owner->GetID(), |
| 1934 | error.AsCString() ? error.AsCString() : "unknown error"); |
| 1935 | } |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1936 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1937 | } |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1938 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1939 | } |
| 1940 | // We failed to enable the breakpoint |
| 1941 | return LLDB_INVALID_BREAK_ID; |
| 1942 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1943 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1944 | void Process::RemoveOwnerFromBreakpointSite(lldb::user_id_t owner_id, |
| 1945 | lldb::user_id_t owner_loc_id, |
| 1946 | BreakpointSiteSP &bp_site_sp) { |
| 1947 | uint32_t num_owners = bp_site_sp->RemoveOwner(owner_id, owner_loc_id); |
| 1948 | if (num_owners == 0) { |
| 1949 | // Don't try to disable the site if we don't have a live process anymore. |
| 1950 | if (IsAlive()) |
| 1951 | DisableBreakpointSite(bp_site_sp.get()); |
| 1952 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 1953 | } |
| 1954 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1955 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1956 | size_t Process::RemoveBreakpointOpcodesFromBuffer(addr_t bp_addr, size_t size, |
| 1957 | uint8_t *buf) const { |
| 1958 | size_t bytes_removed = 0; |
| 1959 | BreakpointSiteList bp_sites_in_range; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1960 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1961 | if (m_breakpoint_site_list.FindInRange(bp_addr, bp_addr + size, |
| 1962 | bp_sites_in_range)) { |
Zachary Turner | 3bc714b | 2017-03-02 00:05:25 +0000 | [diff] [blame] | 1963 | bp_sites_in_range.ForEach([bp_addr, size, |
| 1964 | buf](BreakpointSite *bp_site) -> void { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1965 | if (bp_site->GetType() == BreakpointSite::eSoftware) { |
| 1966 | addr_t intersect_addr; |
| 1967 | size_t intersect_size; |
| 1968 | size_t opcode_offset; |
| 1969 | if (bp_site->IntersectsRange(bp_addr, size, &intersect_addr, |
| 1970 | &intersect_size, &opcode_offset)) { |
| 1971 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 1972 | assert(bp_addr < intersect_addr + intersect_size && |
| 1973 | intersect_addr + intersect_size <= bp_addr + size); |
| 1974 | assert(opcode_offset + intersect_size <= bp_site->GetByteSize()); |
| 1975 | size_t buf_offset = intersect_addr - bp_addr; |
| 1976 | ::memcpy(buf + buf_offset, |
| 1977 | bp_site->GetSavedOpcodeBytes() + opcode_offset, |
| 1978 | intersect_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1979 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1980 | } |
| 1981 | }); |
| 1982 | } |
| 1983 | return bytes_removed; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1986 | size_t Process::GetSoftwareBreakpointTrapOpcode(BreakpointSite *bp_site) { |
| 1987 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 1988 | if (platform_sp) |
| 1989 | return platform_sp->GetSoftwareBreakpointTrapOpcode(GetTarget(), bp_site); |
| 1990 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1993 | Status Process::EnableSoftwareBreakpoint(BreakpointSite *bp_site) { |
| 1994 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1995 | assert(bp_site != nullptr); |
| 1996 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
| 1997 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
| 1998 | if (log) |
| 1999 | log->Printf( |
| 2000 | "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64, |
| 2001 | bp_site->GetID(), (uint64_t)bp_addr); |
| 2002 | if (bp_site->IsEnabled()) { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2003 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2004 | log->Printf( |
| 2005 | "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 2006 | " -- already enabled", |
| 2007 | bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2008 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | if (bp_addr == LLDB_INVALID_ADDRESS) { |
| 2012 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 2013 | return error; |
| 2014 | } |
| 2015 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 2016 | // trap for the breakpoint site |
| 2017 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 2018 | |
| 2019 | if (bp_opcode_size == 0) { |
| 2020 | error.SetErrorStringWithFormat("Process::GetSoftwareBreakpointTrapOpcode() " |
| 2021 | "returned zero, unable to get breakpoint " |
| 2022 | "trap for address 0x%" PRIx64, |
| 2023 | bp_addr); |
| 2024 | } else { |
| 2025 | const uint8_t *const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 2026 | |
| 2027 | if (bp_opcode_bytes == nullptr) { |
| 2028 | error.SetErrorString( |
| 2029 | "BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 2030 | return error; |
| 2031 | } |
| 2032 | |
| 2033 | // Save the original opcode by reading it |
| 2034 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, |
| 2035 | error) == bp_opcode_size) { |
| 2036 | // Write a software breakpoint in place of the original opcode |
| 2037 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == |
| 2038 | bp_opcode_size) { |
| 2039 | uint8_t verify_bp_opcode_bytes[64]; |
| 2040 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, |
| 2041 | error) == bp_opcode_size) { |
| 2042 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, |
| 2043 | bp_opcode_size) == 0) { |
| 2044 | bp_site->SetEnabled(true); |
| 2045 | bp_site->SetType(BreakpointSite::eSoftware); |
| 2046 | if (log) |
| 2047 | log->Printf("Process::EnableSoftwareBreakpoint (site_id = %d) " |
| 2048 | "addr = 0x%" PRIx64 " -- SUCCESS", |
| 2049 | bp_site->GetID(), (uint64_t)bp_addr); |
| 2050 | } else |
| 2051 | error.SetErrorString( |
| 2052 | "failed to verify the breakpoint trap in memory."); |
| 2053 | } else |
| 2054 | error.SetErrorString( |
| 2055 | "Unable to read memory to verify breakpoint trap."); |
| 2056 | } else |
| 2057 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 2058 | } else |
| 2059 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 2060 | } |
| 2061 | if (log && error.Fail()) |
| 2062 | log->Printf( |
| 2063 | "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 2064 | " -- FAILED: %s", |
| 2065 | bp_site->GetID(), (uint64_t)bp_addr, error.AsCString()); |
| 2066 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2069 | Status Process::DisableSoftwareBreakpoint(BreakpointSite *bp_site) { |
| 2070 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2071 | assert(bp_site != nullptr); |
| 2072 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
| 2073 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 2074 | lldb::user_id_t breakID = bp_site->GetID(); |
| 2075 | if (log) |
| 2076 | log->Printf("Process::DisableSoftwareBreakpoint (breakID = %" PRIu64 |
| 2077 | ") addr = 0x%" PRIx64, |
| 2078 | breakID, (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2079 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2080 | if (bp_site->IsHardware()) { |
| 2081 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 2082 | } else if (bp_site->IsEnabled()) { |
| 2083 | const size_t break_op_size = bp_site->GetByteSize(); |
| 2084 | const uint8_t *const break_op = bp_site->GetTrapOpcodeBytes(); |
| 2085 | if (break_op_size > 0) { |
| 2086 | // Clear a software breakpoint instruction |
| 2087 | uint8_t curr_break_op[8]; |
| 2088 | assert(break_op_size <= sizeof(curr_break_op)); |
| 2089 | bool break_op_found = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2090 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2091 | // Read the breakpoint opcode |
| 2092 | if (DoReadMemory(bp_addr, curr_break_op, break_op_size, error) == |
| 2093 | break_op_size) { |
| 2094 | bool verify = false; |
| 2095 | // Make sure the breakpoint opcode exists at this address |
| 2096 | if (::memcmp(curr_break_op, break_op, break_op_size) == 0) { |
| 2097 | break_op_found = true; |
| 2098 | // We found a valid breakpoint opcode at this address, now restore |
| 2099 | // the saved opcode. |
| 2100 | if (DoWriteMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), |
| 2101 | break_op_size, error) == break_op_size) { |
| 2102 | verify = true; |
| 2103 | } else |
| 2104 | error.SetErrorString( |
| 2105 | "Memory write failed when restoring original opcode."); |
| 2106 | } else { |
| 2107 | error.SetErrorString( |
| 2108 | "Original breakpoint trap is no longer in memory."); |
| 2109 | // Set verify to true and so we can check if the original opcode has |
| 2110 | // already been restored |
| 2111 | verify = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2112 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2114 | if (verify) { |
| 2115 | uint8_t verify_opcode[8]; |
| 2116 | assert(break_op_size < sizeof(verify_opcode)); |
| 2117 | // Verify that our original opcode made it back to the inferior |
| 2118 | if (DoReadMemory(bp_addr, verify_opcode, break_op_size, error) == |
| 2119 | break_op_size) { |
| 2120 | // compare the memory we just read with the original opcode |
| 2121 | if (::memcmp(bp_site->GetSavedOpcodeBytes(), verify_opcode, |
| 2122 | break_op_size) == 0) { |
| 2123 | // SUCCESS |
| 2124 | bp_site->SetEnabled(false); |
| 2125 | if (log) |
| 2126 | log->Printf("Process::DisableSoftwareBreakpoint (site_id = %d) " |
| 2127 | "addr = 0x%" PRIx64 " -- SUCCESS", |
| 2128 | bp_site->GetID(), (uint64_t)bp_addr); |
| 2129 | return error; |
| 2130 | } else { |
| 2131 | if (break_op_found) |
| 2132 | error.SetErrorString("Failed to restore original opcode."); |
| 2133 | } |
| 2134 | } else |
| 2135 | error.SetErrorString("Failed to read memory to verify that " |
| 2136 | "breakpoint trap was restored."); |
| 2137 | } |
| 2138 | } else |
| 2139 | error.SetErrorString( |
| 2140 | "Unable to read memory that should contain the breakpoint trap."); |
| 2141 | } |
| 2142 | } else { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2143 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2144 | log->Printf( |
| 2145 | "Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 2146 | " -- already disabled", |
| 2147 | bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2148 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | if (log) |
| 2152 | log->Printf( |
| 2153 | "Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 2154 | " -- FAILED: %s", |
| 2155 | bp_site->GetID(), (uint64_t)bp_addr, error.AsCString()); |
| 2156 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2159 | // Uncomment to verify memory caching works after making changes to caching |
| 2160 | // code |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2161 | //#define VERIFY_MEMORY_READS |
| 2162 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2163 | size_t Process::ReadMemory(addr_t addr, void *buf, size_t size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2164 | error.Clear(); |
| 2165 | if (!GetDisableMemoryCache()) { |
| 2166 | #if defined(VERIFY_MEMORY_READS) |
| 2167 | // Memory caching is enabled, with debug verification |
| 2168 | |
| 2169 | if (buf && size) { |
| 2170 | // Uncomment the line below to make sure memory caching is working. |
| 2171 | // I ran this through the test suite and got no assertions, so I am |
| 2172 | // pretty confident this is working well. If any changes are made to |
| 2173 | // memory caching, uncomment the line below and test your changes! |
| 2174 | |
| 2175 | // Verify all memory reads by using the cache first, then redundantly |
| 2176 | // reading the same memory from the inferior and comparing to make sure |
| 2177 | // everything is exactly the same. |
| 2178 | std::string verify_buf(size, '\0'); |
| 2179 | assert(verify_buf.size() == size); |
| 2180 | const size_t cache_bytes_read = |
| 2181 | m_memory_cache.Read(this, addr, buf, size, error); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2182 | Status verify_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2183 | const size_t verify_bytes_read = |
| 2184 | ReadMemoryFromInferior(addr, const_cast<char *>(verify_buf.data()), |
| 2185 | verify_buf.size(), verify_error); |
| 2186 | assert(cache_bytes_read == verify_bytes_read); |
| 2187 | assert(memcmp(buf, verify_buf.data(), verify_buf.size()) == 0); |
| 2188 | assert(verify_error.Success() == error.Success()); |
| 2189 | return cache_bytes_read; |
| 2190 | } |
| 2191 | return 0; |
| 2192 | #else // !defined(VERIFY_MEMORY_READS) |
| 2193 | // Memory caching is enabled, without debug verification |
| 2194 | |
| 2195 | return m_memory_cache.Read(addr, buf, size, error); |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2196 | #endif // defined (VERIFY_MEMORY_READS) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2197 | } else { |
| 2198 | // Memory caching is disabled |
| 2199 | |
| 2200 | return ReadMemoryFromInferior(addr, buf, size, error); |
| 2201 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2202 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2203 | |
| 2204 | size_t Process::ReadCStringFromMemory(addr_t addr, std::string &out_str, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2205 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2206 | char buf[256]; |
| 2207 | out_str.clear(); |
| 2208 | addr_t curr_addr = addr; |
| 2209 | while (true) { |
| 2210 | size_t length = ReadCStringFromMemory(curr_addr, buf, sizeof(buf), error); |
| 2211 | if (length == 0) |
| 2212 | break; |
| 2213 | out_str.append(buf, length); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2214 | // If we got "length - 1" bytes, we didn't get the whole C string, we need |
| 2215 | // to read some more characters |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2216 | if (length == sizeof(buf) - 1) |
| 2217 | curr_addr += length; |
| 2218 | else |
| 2219 | break; |
| 2220 | } |
| 2221 | return out_str.size(); |
| 2222 | } |
| 2223 | |
| 2224 | size_t Process::ReadStringFromMemory(addr_t addr, char *dst, size_t max_bytes, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2225 | Status &error, size_t type_width) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2226 | size_t total_bytes_read = 0; |
| 2227 | if (dst && max_bytes && type_width && max_bytes >= type_width) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2228 | // Ensure a null terminator independent of the number of bytes that is |
| 2229 | // read. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2230 | memset(dst, 0, max_bytes); |
| 2231 | size_t bytes_left = max_bytes - type_width; |
| 2232 | |
| 2233 | const char terminator[4] = {'\0', '\0', '\0', '\0'}; |
| 2234 | assert(sizeof(terminator) >= type_width && "Attempting to validate a " |
| 2235 | "string with more than 4 bytes " |
| 2236 | "per character!"); |
| 2237 | |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2238 | addr_t curr_addr = addr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2239 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2240 | char *curr_dst = dst; |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2241 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2242 | error.Clear(); |
| 2243 | while (bytes_left > 0 && error.Success()) { |
| 2244 | addr_t cache_line_bytes_left = |
| 2245 | cache_line_size - (curr_addr % cache_line_size); |
| 2246 | addr_t bytes_to_read = |
| 2247 | std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2248 | size_t bytes_read = ReadMemory(curr_addr, curr_dst, bytes_to_read, error); |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2249 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2250 | if (bytes_read == 0) |
| 2251 | break; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2252 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2253 | // Search for a null terminator of correct size and alignment in |
| 2254 | // bytes_read |
| 2255 | size_t aligned_start = total_bytes_read - total_bytes_read % type_width; |
| 2256 | for (size_t i = aligned_start; |
| 2257 | i + type_width <= total_bytes_read + bytes_read; i += type_width) |
| 2258 | if (::memcmp(&dst[i], terminator, type_width) == 0) { |
| 2259 | error.Clear(); |
| 2260 | return i; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2261 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2262 | |
| 2263 | total_bytes_read += bytes_read; |
| 2264 | curr_dst += bytes_read; |
| 2265 | curr_addr += bytes_read; |
| 2266 | bytes_left -= bytes_read; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2267 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2268 | } else { |
| 2269 | if (max_bytes) |
| 2270 | error.SetErrorString("invalid arguments"); |
| 2271 | } |
| 2272 | return total_bytes_read; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2275 | // Deprecated in favor of ReadStringFromMemory which has wchar support and |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2276 | // correct code to find null terminators. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2277 | size_t Process::ReadCStringFromMemory(addr_t addr, char *dst, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2278 | size_t dst_max_len, |
| 2279 | Status &result_error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2280 | size_t total_cstr_len = 0; |
| 2281 | if (dst && dst_max_len) { |
| 2282 | result_error.Clear(); |
| 2283 | // NULL out everything just to be safe |
| 2284 | memset(dst, 0, dst_max_len); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2285 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2286 | addr_t curr_addr = addr; |
| 2287 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2288 | size_t bytes_left = dst_max_len - 1; |
| 2289 | char *curr_dst = dst; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2291 | while (bytes_left > 0) { |
| 2292 | addr_t cache_line_bytes_left = |
| 2293 | cache_line_size - (curr_addr % cache_line_size); |
| 2294 | addr_t bytes_to_read = |
| 2295 | std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2296 | size_t bytes_read = ReadMemory(curr_addr, curr_dst, bytes_to_read, error); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2297 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2298 | if (bytes_read == 0) { |
| 2299 | result_error = error; |
| 2300 | dst[total_cstr_len] = '\0'; |
| 2301 | break; |
| 2302 | } |
| 2303 | const size_t len = strlen(curr_dst); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2304 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2305 | total_cstr_len += len; |
| 2306 | |
| 2307 | if (len < bytes_to_read) |
| 2308 | break; |
| 2309 | |
| 2310 | curr_dst += bytes_read; |
| 2311 | curr_addr += bytes_read; |
| 2312 | bytes_left -= bytes_read; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2313 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2314 | } else { |
| 2315 | if (dst == nullptr) |
| 2316 | result_error.SetErrorString("invalid arguments"); |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2317 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2318 | result_error.Clear(); |
| 2319 | } |
| 2320 | return total_cstr_len; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2323 | size_t Process::ReadMemoryFromInferior(addr_t addr, void *buf, size_t size, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2324 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2325 | if (buf == nullptr || size == 0) |
| 2326 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2327 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2328 | size_t bytes_read = 0; |
| 2329 | uint8_t *bytes = (uint8_t *)buf; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2330 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2331 | while (bytes_read < size) { |
| 2332 | const size_t curr_size = size - bytes_read; |
| 2333 | const size_t curr_bytes_read = |
| 2334 | DoReadMemory(addr + bytes_read, bytes + bytes_read, curr_size, error); |
| 2335 | bytes_read += curr_bytes_read; |
| 2336 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 2337 | break; |
| 2338 | } |
| 2339 | |
| 2340 | // Replace any software breakpoint opcodes that fall into this range back |
| 2341 | // into "buf" before we return |
| 2342 | if (bytes_read > 0) |
| 2343 | RemoveBreakpointOpcodesFromBuffer(addr, bytes_read, (uint8_t *)buf); |
| 2344 | return bytes_read; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2347 | uint64_t Process::ReadUnsignedIntegerFromMemory(lldb::addr_t vm_addr, |
| 2348 | size_t integer_byte_size, |
| 2349 | uint64_t fail_value, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2350 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2351 | Scalar scalar; |
| 2352 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, |
| 2353 | error)) |
| 2354 | return scalar.ULongLong(fail_value); |
| 2355 | return fail_value; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2358 | int64_t Process::ReadSignedIntegerFromMemory(lldb::addr_t vm_addr, |
| 2359 | size_t integer_byte_size, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2360 | int64_t fail_value, |
| 2361 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2362 | Scalar scalar; |
| 2363 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, true, scalar, |
| 2364 | error)) |
| 2365 | return scalar.SLongLong(fail_value); |
| 2366 | return fail_value; |
Greg Clayton | c226778 | 2016-05-23 20:37:24 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2369 | addr_t Process::ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2370 | Scalar scalar; |
| 2371 | if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, |
| 2372 | error)) |
| 2373 | return scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 2374 | return LLDB_INVALID_ADDRESS; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2377 | bool Process::WritePointerToMemory(lldb::addr_t vm_addr, lldb::addr_t ptr_value, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2378 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2379 | Scalar scalar; |
| 2380 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2381 | if (addr_byte_size <= 4) |
| 2382 | scalar = (uint32_t)ptr_value; |
| 2383 | else |
| 2384 | scalar = ptr_value; |
| 2385 | return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == |
| 2386 | addr_byte_size; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2389 | size_t Process::WriteMemoryPrivate(addr_t addr, const void *buf, size_t size, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2390 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2391 | size_t bytes_written = 0; |
| 2392 | const uint8_t *bytes = (const uint8_t *)buf; |
| 2393 | |
| 2394 | while (bytes_written < size) { |
| 2395 | const size_t curr_size = size - bytes_written; |
| 2396 | const size_t curr_bytes_written = DoWriteMemory( |
| 2397 | addr + bytes_written, bytes + bytes_written, curr_size, error); |
| 2398 | bytes_written += curr_bytes_written; |
| 2399 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 2400 | break; |
| 2401 | } |
| 2402 | return bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2405 | size_t Process::WriteMemory(addr_t addr, const void *buf, size_t size, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2406 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2407 | #if defined(ENABLE_MEMORY_CACHING) |
| 2408 | m_memory_cache.Flush(addr, size); |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2409 | #endif |
| 2410 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2411 | if (buf == nullptr || size == 0) |
| 2412 | return 0; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2413 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2414 | m_mod_id.BumpMemoryID(); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2415 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2416 | // We need to write any data that would go where any current software traps |
| 2417 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 2418 | // may have placed in our tasks memory. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2419 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2420 | BreakpointSiteList bp_sites_in_range; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2421 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2422 | if (m_breakpoint_site_list.FindInRange(addr, addr + size, |
| 2423 | bp_sites_in_range)) { |
| 2424 | // No breakpoint sites overlap |
| 2425 | if (bp_sites_in_range.IsEmpty()) |
| 2426 | return WriteMemoryPrivate(addr, buf, size, error); |
| 2427 | else { |
| 2428 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 2429 | uint64_t bytes_written = 0; |
| 2430 | |
| 2431 | bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, |
| 2432 | &error](BreakpointSite *bp) -> void { |
| 2433 | |
| 2434 | if (error.Success()) { |
| 2435 | addr_t intersect_addr; |
| 2436 | size_t intersect_size; |
| 2437 | size_t opcode_offset; |
| 2438 | const bool intersects = bp->IntersectsRange( |
| 2439 | addr, size, &intersect_addr, &intersect_size, &opcode_offset); |
| 2440 | UNUSED_IF_ASSERT_DISABLED(intersects); |
| 2441 | assert(intersects); |
| 2442 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 2443 | assert(addr < intersect_addr + intersect_size && |
| 2444 | intersect_addr + intersect_size <= addr + size); |
| 2445 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 2446 | |
| 2447 | // Check for bytes before this breakpoint |
| 2448 | const addr_t curr_addr = addr + bytes_written; |
| 2449 | if (intersect_addr > curr_addr) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2450 | // There are some bytes before this breakpoint that we need to just |
| 2451 | // write to memory |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2452 | size_t curr_size = intersect_addr - curr_addr; |
| 2453 | size_t curr_bytes_written = WriteMemoryPrivate( |
| 2454 | curr_addr, ubuf + bytes_written, curr_size, error); |
| 2455 | bytes_written += curr_bytes_written; |
| 2456 | if (curr_bytes_written != curr_size) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2457 | // We weren't able to write all of the requested bytes, we are |
| 2458 | // done looping and will return the number of bytes that we have |
| 2459 | // written so far. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2460 | if (error.Success()) |
| 2461 | error.SetErrorToGenericError(); |
| 2462 | } |
| 2463 | } |
| 2464 | // Now write any bytes that would cover up any software breakpoints |
| 2465 | // directly into the breakpoint opcode buffer |
| 2466 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, |
| 2467 | ubuf + bytes_written, intersect_size); |
| 2468 | bytes_written += intersect_size; |
Greg Clayton | d8cf1a1 | 2013-06-12 00:46:38 +0000 | [diff] [blame] | 2469 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2470 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2471 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2472 | if (bytes_written < size) |
| 2473 | WriteMemoryPrivate(addr + bytes_written, ubuf + bytes_written, |
| 2474 | size - bytes_written, error); |
| 2475 | } |
| 2476 | } else { |
| 2477 | return WriteMemoryPrivate(addr, buf, size, error); |
| 2478 | } |
| 2479 | |
| 2480 | // Write any remaining bytes after the last breakpoint if we have any left |
| 2481 | return 0; // bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2482 | } |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2483 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2484 | size_t Process::WriteScalarToMemory(addr_t addr, const Scalar &scalar, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2485 | size_t byte_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2486 | if (byte_size == UINT32_MAX) |
| 2487 | byte_size = scalar.GetByteSize(); |
| 2488 | if (byte_size > 0) { |
| 2489 | uint8_t buf[32]; |
| 2490 | const size_t mem_size = |
| 2491 | scalar.GetAsMemoryData(buf, byte_size, GetByteOrder(), error); |
| 2492 | if (mem_size > 0) |
| 2493 | return WriteMemory(addr, buf, mem_size, error); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2494 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2495 | error.SetErrorString("failed to get scalar as memory data"); |
| 2496 | } else { |
| 2497 | error.SetErrorString("invalid scalar value"); |
| 2498 | } |
| 2499 | return 0; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2502 | size_t Process::ReadScalarIntegerFromMemory(addr_t addr, uint32_t byte_size, |
| 2503 | bool is_signed, Scalar &scalar, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2504 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2505 | uint64_t uval = 0; |
| 2506 | if (byte_size == 0) { |
| 2507 | error.SetErrorString("byte size is zero"); |
| 2508 | } else if (byte_size & (byte_size - 1)) { |
| 2509 | error.SetErrorStringWithFormat("byte size %u is not a power of 2", |
| 2510 | byte_size); |
| 2511 | } else if (byte_size <= sizeof(uval)) { |
| 2512 | const size_t bytes_read = ReadMemory(addr, &uval, byte_size, error); |
| 2513 | if (bytes_read == byte_size) { |
| 2514 | DataExtractor data(&uval, sizeof(uval), GetByteOrder(), |
| 2515 | GetAddressByteSize()); |
| 2516 | lldb::offset_t offset = 0; |
| 2517 | if (byte_size <= 4) |
| 2518 | scalar = data.GetMaxU32(&offset, byte_size); |
| 2519 | else |
| 2520 | scalar = data.GetMaxU64(&offset, byte_size); |
| 2521 | if (is_signed) |
| 2522 | scalar.SignExtend(byte_size * 8); |
| 2523 | return bytes_read; |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2524 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2525 | } else { |
| 2526 | error.SetErrorStringWithFormat( |
| 2527 | "byte size of %u is too large for integer scalar type", byte_size); |
| 2528 | } |
| 2529 | return 0; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 2532 | Status Process::WriteObjectFile(std::vector<ObjectFile::LoadableData> entries) { |
| 2533 | Status error; |
| 2534 | for (const auto &Entry : entries) { |
| 2535 | WriteMemory(Entry.Dest, Entry.Contents.data(), Entry.Contents.size(), |
| 2536 | error); |
| 2537 | if (!error.Success()) |
| 2538 | break; |
| 2539 | } |
| 2540 | return error; |
| 2541 | } |
| 2542 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2543 | #define USE_ALLOCATE_MEMORY_CACHE 1 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2544 | addr_t Process::AllocateMemory(size_t size, uint32_t permissions, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2545 | Status &error) { |
Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 2546 | if (GetPrivateState() != eStateStopped) { |
| 2547 | error.SetErrorToGenericError(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2548 | return LLDB_INVALID_ADDRESS; |
Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 2549 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2550 | |
| 2551 | #if defined(USE_ALLOCATE_MEMORY_CACHE) |
| 2552 | return m_allocated_memory_cache.AllocateMemory(size, permissions, error); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2553 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2554 | addr_t allocated_addr = DoAllocateMemory(size, permissions, error); |
| 2555 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2556 | if (log) |
| 2557 | log->Printf("Process::AllocateMemory(size=%" PRIu64 |
| 2558 | ", permissions=%s) => 0x%16.16" PRIx64 |
| 2559 | " (m_stop_id = %u m_memory_id = %u)", |
| 2560 | (uint64_t)size, GetPermissionsAsCString(permissions), |
| 2561 | (uint64_t)allocated_addr, m_mod_id.GetStopID(), |
| 2562 | m_mod_id.GetMemoryID()); |
| 2563 | return allocated_addr; |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2564 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2567 | addr_t Process::CallocateMemory(size_t size, uint32_t permissions, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2568 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2569 | addr_t return_addr = AllocateMemory(size, permissions, error); |
| 2570 | if (error.Success()) { |
| 2571 | std::string buffer(size, 0); |
| 2572 | WriteMemory(return_addr, buffer.c_str(), size, error); |
| 2573 | } |
| 2574 | return return_addr; |
| 2575 | } |
| 2576 | |
| 2577 | bool Process::CanJIT() { |
| 2578 | if (m_can_jit == eCanJITDontKnow) { |
| 2579 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2580 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2581 | |
| 2582 | uint64_t allocated_memory = AllocateMemory( |
| 2583 | 8, ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable, |
| 2584 | err); |
| 2585 | |
| 2586 | if (err.Success()) { |
| 2587 | m_can_jit = eCanJITYes; |
| 2588 | if (log) |
| 2589 | log->Printf("Process::%s pid %" PRIu64 |
| 2590 | " allocation test passed, CanJIT () is true", |
| 2591 | __FUNCTION__, GetID()); |
| 2592 | } else { |
| 2593 | m_can_jit = eCanJITNo; |
| 2594 | if (log) |
| 2595 | log->Printf("Process::%s pid %" PRIu64 |
| 2596 | " allocation test failed, CanJIT () is false: %s", |
| 2597 | __FUNCTION__, GetID(), err.AsCString()); |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame] | 2598 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2599 | |
| 2600 | DeallocateMemory(allocated_memory); |
| 2601 | } |
| 2602 | |
| 2603 | return m_can_jit == eCanJITYes; |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2606 | void Process::SetCanJIT(bool can_jit) { |
| 2607 | m_can_jit = (can_jit ? eCanJITYes : eCanJITNo); |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2610 | void Process::SetCanRunCode(bool can_run_code) { |
| 2611 | SetCanJIT(can_run_code); |
| 2612 | m_can_interpret_function_calls = can_run_code; |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2615 | Status Process::DeallocateMemory(addr_t ptr) { |
| 2616 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2617 | #if defined(USE_ALLOCATE_MEMORY_CACHE) |
| 2618 | if (!m_allocated_memory_cache.DeallocateMemory(ptr)) { |
| 2619 | error.SetErrorStringWithFormat( |
| 2620 | "deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr); |
| 2621 | } |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2622 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2623 | error = DoDeallocateMemory(ptr); |
| 2624 | |
| 2625 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2626 | if (log) |
| 2627 | log->Printf("Process::DeallocateMemory(addr=0x%16.16" PRIx64 |
| 2628 | ") => err = %s (m_stop_id = %u, m_memory_id = %u)", |
| 2629 | ptr, error.AsCString("SUCCESS"), m_mod_id.GetStopID(), |
| 2630 | m_mod_id.GetMemoryID()); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2631 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2632 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2635 | ModuleSP Process::ReadModuleFromMemory(const FileSpec &file_spec, |
| 2636 | lldb::addr_t header_addr, |
| 2637 | size_t size_to_read) { |
| 2638 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); |
| 2639 | if (log) { |
| 2640 | log->Printf("Process::ReadModuleFromMemory reading %s binary from memory", |
| 2641 | file_spec.GetPath().c_str()); |
| 2642 | } |
| 2643 | ModuleSP module_sp(new Module(file_spec, ArchSpec())); |
| 2644 | if (module_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2645 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2646 | ObjectFile *objfile = module_sp->GetMemoryObjectFile( |
| 2647 | shared_from_this(), header_addr, error, size_to_read); |
| 2648 | if (objfile) |
| 2649 | return module_sp; |
| 2650 | } |
| 2651 | return ModuleSP(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2654 | bool Process::GetLoadAddressPermissions(lldb::addr_t load_addr, |
| 2655 | uint32_t &permissions) { |
| 2656 | MemoryRegionInfo range_info; |
| 2657 | permissions = 0; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2658 | Status error(GetMemoryRegionInfo(load_addr, range_info)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2659 | if (!error.Success()) |
| 2660 | return false; |
| 2661 | if (range_info.GetReadable() == MemoryRegionInfo::eDontKnow || |
| 2662 | range_info.GetWritable() == MemoryRegionInfo::eDontKnow || |
| 2663 | range_info.GetExecutable() == MemoryRegionInfo::eDontKnow) { |
| 2664 | return false; |
| 2665 | } |
| 2666 | |
| 2667 | if (range_info.GetReadable() == MemoryRegionInfo::eYes) |
| 2668 | permissions |= lldb::ePermissionsReadable; |
| 2669 | |
| 2670 | if (range_info.GetWritable() == MemoryRegionInfo::eYes) |
| 2671 | permissions |= lldb::ePermissionsWritable; |
| 2672 | |
| 2673 | if (range_info.GetExecutable() == MemoryRegionInfo::eYes) |
| 2674 | permissions |= lldb::ePermissionsExecutable; |
| 2675 | |
| 2676 | return true; |
| 2677 | } |
| 2678 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2679 | Status Process::EnableWatchpoint(Watchpoint *watchpoint, bool notify) { |
| 2680 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2681 | error.SetErrorString("watchpoints are not supported"); |
| 2682 | return error; |
| 2683 | } |
| 2684 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2685 | Status Process::DisableWatchpoint(Watchpoint *watchpoint, bool notify) { |
| 2686 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2687 | error.SetErrorString("watchpoints are not supported"); |
| 2688 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | StateType |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 2692 | Process::WaitForProcessStopPrivate(EventSP &event_sp, |
| 2693 | const Timeout<std::micro> &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2694 | StateType state; |
Stella Stamenova | 36d457c | 2018-06-01 19:14:53 +0000 | [diff] [blame] | 2695 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2696 | while (true) { |
| 2697 | event_sp.reset(); |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 2698 | state = GetStateChangedEventsPrivate(event_sp, timeout); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2699 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2700 | if (StateIsStoppedState(state, false)) |
| 2701 | break; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2702 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2703 | // If state is invalid, then we timed out |
| 2704 | if (state == eStateInvalid) |
| 2705 | break; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2706 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2707 | if (event_sp) |
| 2708 | HandlePrivateEvent(event_sp); |
| 2709 | } |
| 2710 | return state; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2713 | void Process::LoadOperatingSystemPlugin(bool flush) { |
| 2714 | if (flush) |
| 2715 | m_thread_list.Clear(); |
| 2716 | m_os_ap.reset(OperatingSystem::FindPlugin(this, nullptr)); |
| 2717 | if (flush) |
| 2718 | Flush(); |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2721 | Status Process::Launch(ProcessLaunchInfo &launch_info) { |
| 2722 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2723 | m_abi_sp.reset(); |
| 2724 | m_dyld_ap.reset(); |
| 2725 | m_jit_loaders_ap.reset(); |
| 2726 | m_system_runtime_ap.reset(); |
| 2727 | m_os_ap.reset(); |
| 2728 | m_process_input_reader.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2729 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2730 | Module *exe_module = GetTarget().GetExecutableModulePointer(); |
| 2731 | if (exe_module) { |
| 2732 | char local_exec_file_path[PATH_MAX]; |
| 2733 | char platform_exec_file_path[PATH_MAX]; |
| 2734 | exe_module->GetFileSpec().GetPath(local_exec_file_path, |
| 2735 | sizeof(local_exec_file_path)); |
| 2736 | exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, |
| 2737 | sizeof(platform_exec_file_path)); |
| 2738 | if (exe_module->GetFileSpec().Exists()) { |
| 2739 | // Install anything that might need to be installed prior to launching. |
| 2740 | // For host systems, this will do nothing, but if we are connected to a |
| 2741 | // remote platform it will install any needed binaries |
| 2742 | error = GetTarget().Install(&launch_info); |
| 2743 | if (error.Fail()) |
| 2744 | return error; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2745 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2746 | if (PrivateStateThreadIsValid()) |
| 2747 | PausePrivateStateThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2748 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2749 | error = WillLaunch(exe_module); |
| 2750 | if (error.Success()) { |
| 2751 | const bool restarted = false; |
| 2752 | SetPublicState(eStateLaunching, restarted); |
| 2753 | m_should_detach = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2754 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2755 | if (m_public_run_lock.TrySetRunning()) { |
| 2756 | // Now launch using these arguments. |
| 2757 | error = DoLaunch(exe_module, launch_info); |
| 2758 | } else { |
| 2759 | // This shouldn't happen |
| 2760 | error.SetErrorString("failed to acquire process run lock"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2761 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2762 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2763 | if (error.Fail()) { |
| 2764 | if (GetID() != LLDB_INVALID_PROCESS_ID) { |
| 2765 | SetID(LLDB_INVALID_PROCESS_ID); |
| 2766 | const char *error_string = error.AsCString(); |
| 2767 | if (error_string == nullptr) |
| 2768 | error_string = "launch failed"; |
| 2769 | SetExitStatus(-1, error_string); |
| 2770 | } |
| 2771 | } else { |
| 2772 | EventSP event_sp; |
Stella Stamenova | 36d457c | 2018-06-01 19:14:53 +0000 | [diff] [blame] | 2773 | |
| 2774 | // Now wait for the process to launch and return control to us, and then call |
| 2775 | // DidLaunch: |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 2776 | StateType state = WaitForProcessStopPrivate(event_sp, seconds(10)); |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 2777 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2778 | if (state == eStateInvalid || !event_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2779 | // We were able to launch the process, but we failed to catch the |
| 2780 | // initial stop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2781 | error.SetErrorString("failed to catch stop after launch"); |
| 2782 | SetExitStatus(0, "failed to catch stop after launch"); |
| 2783 | Destroy(false); |
| 2784 | } else if (state == eStateStopped || state == eStateCrashed) { |
| 2785 | DidLaunch(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2786 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2787 | DynamicLoader *dyld = GetDynamicLoader(); |
| 2788 | if (dyld) |
| 2789 | dyld->DidLaunch(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2790 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2791 | GetJITLoaders().DidLaunch(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2792 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2793 | SystemRuntime *system_runtime = GetSystemRuntime(); |
| 2794 | if (system_runtime) |
| 2795 | system_runtime->DidLaunch(); |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 2796 | |
Greg Clayton | 29eeea0 | 2017-02-09 18:55:41 +0000 | [diff] [blame] | 2797 | if (!m_os_ap) |
| 2798 | LoadOperatingSystemPlugin(false); |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 2799 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2800 | // We successfully launched the process and stopped, now it the |
| 2801 | // right time to set up signal filters before resuming. |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 2802 | UpdateAutomaticSignalFiltering(); |
| 2803 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2804 | // Note, the stop event was consumed above, but not handled. This |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2805 | // was done to give DidLaunch a chance to run. The target is either |
| 2806 | // stopped or crashed. Directly set the state. This is done to |
| 2807 | // prevent a stop message with a bunch of spurious output on thread |
| 2808 | // status, as well as not pop a ProcessIOHandler. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2809 | SetPublicState(state, false); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2810 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2811 | if (PrivateStateThreadIsValid()) |
| 2812 | ResumePrivateStateThread(); |
Eugene Zelenko | da8cf8a | 2016-03-01 00:55:51 +0000 | [diff] [blame] | 2813 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2814 | StartPrivateStateThread(); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 2815 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2816 | // Target was stopped at entry as was intended. Need to notify the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2817 | // listeners about it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2818 | if (state == eStateStopped && |
| 2819 | launch_info.GetFlags().Test(eLaunchFlagStopAtEntry)) |
| 2820 | HandlePrivateEvent(event_sp); |
| 2821 | } else if (state == eStateExited) { |
| 2822 | // We exited while trying to launch somehow. Don't call DidLaunch |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2823 | // as that's not likely to work, and return an invalid pid. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2824 | HandlePrivateEvent(event_sp); |
| 2825 | } |
| 2826 | } |
| 2827 | } |
| 2828 | } else { |
| 2829 | error.SetErrorStringWithFormat("file doesn't exist: '%s'", |
| 2830 | local_exec_file_path); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2831 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2832 | } |
| 2833 | return error; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2834 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2835 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2836 | Status Process::LoadCore() { |
| 2837 | Status error = DoLoadCore(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2838 | if (error.Success()) { |
| 2839 | ListenerSP listener_sp( |
| 2840 | Listener::MakeListener("lldb.process.load_core_listener")); |
| 2841 | HijackProcessEvents(listener_sp); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2842 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2843 | if (PrivateStateThreadIsValid()) |
| 2844 | ResumePrivateStateThread(); |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 2845 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2846 | StartPrivateStateThread(); |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 2847 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2848 | DynamicLoader *dyld = GetDynamicLoader(); |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2849 | if (dyld) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2850 | dyld->DidAttach(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2851 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 2852 | GetJITLoaders().DidAttach(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2853 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2854 | SystemRuntime *system_runtime = GetSystemRuntime(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2855 | if (system_runtime) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2856 | system_runtime->DidAttach(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2857 | |
Greg Clayton | 29eeea0 | 2017-02-09 18:55:41 +0000 | [diff] [blame] | 2858 | if (!m_os_ap) |
| 2859 | LoadOperatingSystemPlugin(false); |
| 2860 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2861 | // We successfully loaded a core file, now pretend we stopped so we can |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2862 | // show all of the threads in the core file and explore the crashed state. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2863 | SetPrivateState(eStateStopped); |
Greg Clayton | a97c4d2 | 2014-12-09 23:31:02 +0000 | [diff] [blame] | 2864 | |
Jonas Devlieghere | 25486b7 | 2018-03-14 11:50:10 +0000 | [diff] [blame] | 2865 | // Wait for a stopped event since we just posted one above... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2866 | lldb::EventSP event_sp; |
Jonas Devlieghere | 25486b7 | 2018-03-14 11:50:10 +0000 | [diff] [blame] | 2867 | StateType state = |
| 2868 | WaitForProcessToStop(seconds(10), &event_sp, true, listener_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2869 | |
| 2870 | if (!StateIsStoppedState(state, false)) { |
| 2871 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2872 | if (log) |
| 2873 | log->Printf("Process::Halt() failed to stop, state is: %s", |
| 2874 | StateAsCString(state)); |
| 2875 | error.SetErrorString( |
| 2876 | "Did not get stopped event after loading the core file."); |
| 2877 | } |
| 2878 | RestoreProcessEvents(); |
| 2879 | } |
| 2880 | return error; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2883 | DynamicLoader *Process::GetDynamicLoader() { |
| 2884 | if (!m_dyld_ap) |
| 2885 | m_dyld_ap.reset(DynamicLoader::FindPlugin(this, nullptr)); |
| 2886 | return m_dyld_ap.get(); |
| 2887 | } |
| 2888 | |
| 2889 | const lldb::DataBufferSP Process::GetAuxvData() { return DataBufferSP(); } |
| 2890 | |
| 2891 | JITLoaderList &Process::GetJITLoaders() { |
| 2892 | if (!m_jit_loaders_ap) { |
| 2893 | m_jit_loaders_ap.reset(new JITLoaderList()); |
| 2894 | JITLoader::LoadPlugins(this, *m_jit_loaders_ap); |
| 2895 | } |
| 2896 | return *m_jit_loaders_ap; |
| 2897 | } |
| 2898 | |
| 2899 | SystemRuntime *Process::GetSystemRuntime() { |
| 2900 | if (!m_system_runtime_ap) |
| 2901 | m_system_runtime_ap.reset(SystemRuntime::FindPlugin(this)); |
| 2902 | return m_system_runtime_ap.get(); |
| 2903 | } |
| 2904 | |
| 2905 | Process::AttachCompletionHandler::AttachCompletionHandler(Process *process, |
| 2906 | uint32_t exec_count) |
| 2907 | : NextEventAction(process), m_exec_count(exec_count) { |
| 2908 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2909 | if (log) |
| 2910 | log->Printf( |
| 2911 | "Process::AttachCompletionHandler::%s process=%p, exec_count=%" PRIu32, |
| 2912 | __FUNCTION__, static_cast<void *>(process), exec_count); |
| 2913 | } |
| 2914 | |
| 2915 | Process::NextEventAction::EventActionResult |
| 2916 | Process::AttachCompletionHandler::PerformAction(lldb::EventSP &event_sp) { |
| 2917 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2918 | |
| 2919 | StateType state = ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 2920 | if (log) |
| 2921 | log->Printf( |
| 2922 | "Process::AttachCompletionHandler::%s called with state %s (%d)", |
| 2923 | __FUNCTION__, StateAsCString(state), static_cast<int>(state)); |
| 2924 | |
| 2925 | switch (state) { |
| 2926 | case eStateAttaching: |
| 2927 | return eEventActionSuccess; |
| 2928 | |
| 2929 | case eStateRunning: |
| 2930 | case eStateConnected: |
| 2931 | return eEventActionRetry; |
| 2932 | |
| 2933 | case eStateStopped: |
| 2934 | case eStateCrashed: |
| 2935 | // During attach, prior to sending the eStateStopped event, |
| 2936 | // lldb_private::Process subclasses must set the new process ID. |
| 2937 | assert(m_process->GetID() != LLDB_INVALID_PROCESS_ID); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2938 | // We don't want these events to be reported, so go set the |
| 2939 | // ShouldReportStop here: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2940 | m_process->GetThreadList().SetShouldReportStop(eVoteNo); |
| 2941 | |
| 2942 | if (m_exec_count > 0) { |
| 2943 | --m_exec_count; |
| 2944 | |
| 2945 | if (log) |
| 2946 | log->Printf("Process::AttachCompletionHandler::%s state %s: reduced " |
| 2947 | "remaining exec count to %" PRIu32 ", requesting resume", |
| 2948 | __FUNCTION__, StateAsCString(state), m_exec_count); |
| 2949 | |
| 2950 | RequestResume(); |
| 2951 | return eEventActionRetry; |
| 2952 | } else { |
| 2953 | if (log) |
| 2954 | log->Printf("Process::AttachCompletionHandler::%s state %s: no more " |
| 2955 | "execs expected to start, continuing with attach", |
| 2956 | __FUNCTION__, StateAsCString(state)); |
| 2957 | |
| 2958 | m_process->CompleteAttach(); |
| 2959 | return eEventActionSuccess; |
| 2960 | } |
| 2961 | break; |
| 2962 | |
| 2963 | default: |
| 2964 | case eStateExited: |
| 2965 | case eStateInvalid: |
| 2966 | break; |
| 2967 | } |
| 2968 | |
| 2969 | m_exit_string.assign("No valid Process"); |
| 2970 | return eEventActionExit; |
| 2971 | } |
| 2972 | |
| 2973 | Process::NextEventAction::EventActionResult |
| 2974 | Process::AttachCompletionHandler::HandleBeingInterrupted() { |
| 2975 | return eEventActionSuccess; |
| 2976 | } |
| 2977 | |
| 2978 | const char *Process::AttachCompletionHandler::GetExitString() { |
| 2979 | return m_exit_string.c_str(); |
| 2980 | } |
| 2981 | |
| 2982 | ListenerSP ProcessAttachInfo::GetListenerForProcess(Debugger &debugger) { |
| 2983 | if (m_listener_sp) |
| 2984 | return m_listener_sp; |
| 2985 | else |
| 2986 | return debugger.GetListener(); |
| 2987 | } |
| 2988 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2989 | Status Process::Attach(ProcessAttachInfo &attach_info) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2990 | m_abi_sp.reset(); |
| 2991 | m_process_input_reader.reset(); |
| 2992 | m_dyld_ap.reset(); |
| 2993 | m_jit_loaders_ap.reset(); |
| 2994 | m_system_runtime_ap.reset(); |
| 2995 | m_os_ap.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2996 | |
| 2997 | lldb::pid_t attach_pid = attach_info.GetProcessID(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2998 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2999 | if (attach_pid == LLDB_INVALID_PROCESS_ID) { |
| 3000 | char process_name[PATH_MAX]; |
| 3001 | |
| 3002 | if (attach_info.GetExecutableFile().GetPath(process_name, |
| 3003 | sizeof(process_name))) { |
| 3004 | const bool wait_for_launch = attach_info.GetWaitForLaunch(); |
| 3005 | |
| 3006 | if (wait_for_launch) { |
| 3007 | error = WillAttachToProcessWithName(process_name, wait_for_launch); |
| 3008 | if (error.Success()) { |
| 3009 | if (m_public_run_lock.TrySetRunning()) { |
| 3010 | m_should_detach = true; |
| 3011 | const bool restarted = false; |
| 3012 | SetPublicState(eStateAttaching, restarted); |
| 3013 | // Now attach using these arguments. |
| 3014 | error = DoAttachToProcessWithName(process_name, attach_info); |
| 3015 | } else { |
| 3016 | // This shouldn't happen |
| 3017 | error.SetErrorString("failed to acquire process run lock"); |
| 3018 | } |
| 3019 | |
| 3020 | if (error.Fail()) { |
| 3021 | if (GetID() != LLDB_INVALID_PROCESS_ID) { |
| 3022 | SetID(LLDB_INVALID_PROCESS_ID); |
| 3023 | if (error.AsCString() == nullptr) |
| 3024 | error.SetErrorString("attach failed"); |
| 3025 | |
| 3026 | SetExitStatus(-1, error.AsCString()); |
| 3027 | } |
| 3028 | } else { |
| 3029 | SetNextEventAction(new Process::AttachCompletionHandler( |
| 3030 | this, attach_info.GetResumeCount())); |
| 3031 | StartPrivateStateThread(); |
| 3032 | } |
| 3033 | return error; |
| 3034 | } |
| 3035 | } else { |
| 3036 | ProcessInstanceInfoList process_infos; |
| 3037 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 3038 | |
| 3039 | if (platform_sp) { |
| 3040 | ProcessInstanceInfoMatch match_info; |
| 3041 | match_info.GetProcessInfo() = attach_info; |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 3042 | match_info.SetNameMatchType(NameMatch::Equals); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3043 | platform_sp->FindProcesses(match_info, process_infos); |
| 3044 | const uint32_t num_matches = process_infos.GetSize(); |
| 3045 | if (num_matches == 1) { |
| 3046 | attach_pid = process_infos.GetProcessIDAtIndex(0); |
| 3047 | // Fall through and attach using the above process ID |
| 3048 | } else { |
| 3049 | match_info.GetProcessInfo().GetExecutableFile().GetPath( |
| 3050 | process_name, sizeof(process_name)); |
| 3051 | if (num_matches > 1) { |
| 3052 | StreamString s; |
| 3053 | ProcessInstanceInfo::DumpTableHeader(s, platform_sp.get(), true, |
| 3054 | false); |
| 3055 | for (size_t i = 0; i < num_matches; i++) { |
| 3056 | process_infos.GetProcessInfoAtIndex(i).DumpAsTableRow( |
| 3057 | s, platform_sp.get(), true, false); |
| 3058 | } |
| 3059 | error.SetErrorStringWithFormat( |
| 3060 | "more than one process named %s:\n%s", process_name, |
| 3061 | s.GetData()); |
| 3062 | } else |
| 3063 | error.SetErrorStringWithFormat( |
| 3064 | "could not find a process named %s", process_name); |
| 3065 | } |
| 3066 | } else { |
| 3067 | error.SetErrorString( |
| 3068 | "invalid platform, can't find processes by name"); |
| 3069 | return error; |
| 3070 | } |
| 3071 | } |
| 3072 | } else { |
| 3073 | error.SetErrorString("invalid process name"); |
| 3074 | } |
| 3075 | } |
| 3076 | |
| 3077 | if (attach_pid != LLDB_INVALID_PROCESS_ID) { |
| 3078 | error = WillAttachToProcessWithID(attach_pid); |
| 3079 | if (error.Success()) { |
| 3080 | |
| 3081 | if (m_public_run_lock.TrySetRunning()) { |
| 3082 | // Now attach using these arguments. |
| 3083 | m_should_detach = true; |
| 3084 | const bool restarted = false; |
| 3085 | SetPublicState(eStateAttaching, restarted); |
| 3086 | error = DoAttachToProcessWithID(attach_pid, attach_info); |
| 3087 | } else { |
| 3088 | // This shouldn't happen |
| 3089 | error.SetErrorString("failed to acquire process run lock"); |
| 3090 | } |
| 3091 | |
| 3092 | if (error.Success()) { |
| 3093 | SetNextEventAction(new Process::AttachCompletionHandler( |
| 3094 | this, attach_info.GetResumeCount())); |
| 3095 | StartPrivateStateThread(); |
| 3096 | } else { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3097 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3098 | SetID(LLDB_INVALID_PROCESS_ID); |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 3099 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3100 | const char *error_string = error.AsCString(); |
| 3101 | if (error_string == nullptr) |
| 3102 | error_string = "attach failed"; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 3103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3104 | SetExitStatus(-1, error_string); |
| 3105 | } |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3106 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3107 | } |
| 3108 | return error; |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3111 | void Process::CompleteAttach() { |
| 3112 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | |
| 3113 | LIBLLDB_LOG_TARGET)); |
| 3114 | if (log) |
| 3115 | log->Printf("Process::%s()", __FUNCTION__); |
| 3116 | |
| 3117 | // Let the process subclass figure out at much as it can about the process |
| 3118 | // before we go looking for a dynamic loader plug-in. |
| 3119 | ArchSpec process_arch; |
| 3120 | DidAttach(process_arch); |
| 3121 | |
| 3122 | if (process_arch.IsValid()) { |
| 3123 | GetTarget().SetArchitecture(process_arch); |
| 3124 | if (log) { |
| 3125 | const char *triple_str = process_arch.GetTriple().getTriple().c_str(); |
| 3126 | log->Printf("Process::%s replacing process architecture with DidAttach() " |
| 3127 | "architecture: %s", |
| 3128 | __FUNCTION__, triple_str ? triple_str : "<null>"); |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | // We just attached. If we have a platform, ask it for the process |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3133 | // architecture, and if it isn't the same as the one we've already set, |
| 3134 | // switch architectures. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3135 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 3136 | assert(platform_sp); |
| 3137 | if (platform_sp) { |
| 3138 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
| 3139 | if (target_arch.IsValid() && |
| 3140 | !platform_sp->IsCompatibleArchitecture(target_arch, false, nullptr)) { |
| 3141 | ArchSpec platform_arch; |
| 3142 | platform_sp = |
| 3143 | platform_sp->GetPlatformForArchitecture(target_arch, &platform_arch); |
| 3144 | if (platform_sp) { |
| 3145 | GetTarget().SetPlatform(platform_sp); |
| 3146 | GetTarget().SetArchitecture(platform_arch); |
| 3147 | if (log) |
| 3148 | log->Printf("Process::%s switching platform to %s and architecture " |
| 3149 | "to %s based on info from attach", |
| 3150 | __FUNCTION__, platform_sp->GetName().AsCString(""), |
| 3151 | platform_arch.GetTriple().getTriple().c_str()); |
| 3152 | } |
| 3153 | } else if (!process_arch.IsValid()) { |
| 3154 | ProcessInstanceInfo process_info; |
| 3155 | GetProcessInfo(process_info); |
| 3156 | const ArchSpec &process_arch = process_info.GetArchitecture(); |
| 3157 | if (process_arch.IsValid() && |
| 3158 | !GetTarget().GetArchitecture().IsExactMatch(process_arch)) { |
| 3159 | GetTarget().SetArchitecture(process_arch); |
| 3160 | if (log) |
| 3161 | log->Printf("Process::%s switching architecture to %s based on info " |
| 3162 | "the platform retrieved for pid %" PRIu64, |
| 3163 | __FUNCTION__, |
| 3164 | process_arch.GetTriple().getTriple().c_str(), GetID()); |
| 3165 | } |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | // We have completed the attach, now it is time to find the dynamic loader |
| 3170 | // plug-in |
| 3171 | DynamicLoader *dyld = GetDynamicLoader(); |
| 3172 | if (dyld) { |
| 3173 | dyld->DidAttach(); |
| 3174 | if (log) { |
| 3175 | ModuleSP exe_module_sp = GetTarget().GetExecutableModule(); |
| 3176 | log->Printf("Process::%s after DynamicLoader::DidAttach(), target " |
| 3177 | "executable is %s (using %s plugin)", |
| 3178 | __FUNCTION__, |
| 3179 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str() |
| 3180 | : "<none>", |
| 3181 | dyld->GetPluginName().AsCString("<unnamed>")); |
| 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | GetJITLoaders().DidAttach(); |
| 3186 | |
| 3187 | SystemRuntime *system_runtime = GetSystemRuntime(); |
| 3188 | if (system_runtime) { |
| 3189 | system_runtime->DidAttach(); |
| 3190 | if (log) { |
| 3191 | ModuleSP exe_module_sp = GetTarget().GetExecutableModule(); |
| 3192 | log->Printf("Process::%s after SystemRuntime::DidAttach(), target " |
| 3193 | "executable is %s (using %s plugin)", |
| 3194 | __FUNCTION__, |
| 3195 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str() |
| 3196 | : "<none>", |
| 3197 | system_runtime->GetPluginName().AsCString("<unnamed>")); |
| 3198 | } |
| 3199 | } |
| 3200 | |
Greg Clayton | 29eeea0 | 2017-02-09 18:55:41 +0000 | [diff] [blame] | 3201 | if (!m_os_ap) |
| 3202 | LoadOperatingSystemPlugin(false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3203 | // Figure out which one is the executable, and set that in our target: |
| 3204 | const ModuleList &target_modules = GetTarget().GetImages(); |
| 3205 | std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); |
| 3206 | size_t num_modules = target_modules.GetSize(); |
| 3207 | ModuleSP new_executable_module_sp; |
| 3208 | |
| 3209 | for (size_t i = 0; i < num_modules; i++) { |
| 3210 | ModuleSP module_sp(target_modules.GetModuleAtIndexUnlocked(i)); |
| 3211 | if (module_sp && module_sp->IsExecutable()) { |
| 3212 | if (GetTarget().GetExecutableModulePointer() != module_sp.get()) |
| 3213 | new_executable_module_sp = module_sp; |
| 3214 | break; |
| 3215 | } |
| 3216 | } |
| 3217 | if (new_executable_module_sp) { |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame^] | 3218 | GetTarget().SetExecutableModule(new_executable_module_sp, |
| 3219 | eLoadDependentsNo); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3220 | if (log) { |
| 3221 | ModuleSP exe_module_sp = GetTarget().GetExecutableModule(); |
| 3222 | log->Printf( |
| 3223 | "Process::%s after looping through modules, target executable is %s", |
| 3224 | __FUNCTION__, |
| 3225 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str() |
| 3226 | : "<none>"); |
| 3227 | } |
| 3228 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3231 | Status Process::ConnectRemote(Stream *strm, llvm::StringRef remote_url) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3232 | m_abi_sp.reset(); |
| 3233 | m_process_input_reader.reset(); |
| 3234 | |
| 3235 | // Find the process and its architecture. Make sure it matches the |
Zachary Turner | 3165945 | 2016-11-17 21:15:14 +0000 | [diff] [blame] | 3236 | // architecture of the current Target, and if not adjust it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3237 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3238 | Status error(DoConnectRemote(strm, remote_url)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3239 | if (error.Success()) { |
| 3240 | if (GetID() != LLDB_INVALID_PROCESS_ID) { |
| 3241 | EventSP event_sp; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3242 | StateType state = WaitForProcessStopPrivate(event_sp, llvm::None); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3243 | |
| 3244 | if (state == eStateStopped || state == eStateCrashed) { |
| 3245 | // If we attached and actually have a process on the other end, then |
| 3246 | // this ended up being the equivalent of an attach. |
| 3247 | CompleteAttach(); |
| 3248 | |
| 3249 | // This delays passing the stopped event to listeners till |
| 3250 | // CompleteAttach gets a chance to complete... |
| 3251 | HandlePrivateEvent(event_sp); |
| 3252 | } |
| 3253 | } |
| 3254 | |
| 3255 | if (PrivateStateThreadIsValid()) |
| 3256 | ResumePrivateStateThread(); |
| 3257 | else |
| 3258 | StartPrivateStateThread(); |
| 3259 | } |
| 3260 | return error; |
| 3261 | } |
| 3262 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3263 | Status Process::PrivateResume() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3264 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | |
| 3265 | LIBLLDB_LOG_STEP)); |
| 3266 | if (log) |
| 3267 | log->Printf("Process::PrivateResume() m_stop_id = %u, public state: %s " |
| 3268 | "private state: %s", |
| 3269 | m_mod_id.GetStopID(), StateAsCString(m_public_state.GetValue()), |
| 3270 | StateAsCString(m_private_state.GetValue())); |
| 3271 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3272 | // If signals handing status changed we might want to update our signal |
| 3273 | // filters before resuming. |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 3274 | UpdateAutomaticSignalFiltering(); |
| 3275 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3276 | Status error(WillResume()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3277 | // Tell the process it is about to resume before the thread list |
| 3278 | if (error.Success()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3279 | // Now let the thread list know we are about to resume so it can let all of |
| 3280 | // our threads know that they are about to be resumed. Threads will each be |
| 3281 | // called with Thread::WillResume(StateType) where StateType contains the |
| 3282 | // state that they are supposed to have when the process is resumed |
| 3283 | // (suspended/running/stepping). Threads should also check their resume |
| 3284 | // signal in lldb::Thread::GetResumeSignal() to see if they are supposed to |
| 3285 | // start back up with a signal. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3286 | if (m_thread_list.WillResume()) { |
| 3287 | // Last thing, do the PreResumeActions. |
| 3288 | if (!RunPreResumeActions()) { |
| 3289 | error.SetErrorStringWithFormat( |
| 3290 | "Process::PrivateResume PreResumeActions failed, not resuming."); |
| 3291 | } else { |
| 3292 | m_mod_id.BumpResumeID(); |
| 3293 | error = DoResume(); |
| 3294 | if (error.Success()) { |
| 3295 | DidResume(); |
| 3296 | m_thread_list.DidResume(); |
| 3297 | if (log) |
| 3298 | log->Printf("Process thinks the process has resumed."); |
| 3299 | } |
| 3300 | } |
| 3301 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3302 | // Somebody wanted to run without running (e.g. we were faking a step |
| 3303 | // from one frame of a set of inlined frames that share the same PC to |
| 3304 | // another.) So generate a continue & a stopped event, and let the world |
| 3305 | // handle them. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3306 | if (log) |
| 3307 | log->Printf( |
| 3308 | "Process::PrivateResume() asked to simulate a start & stop."); |
| 3309 | |
| 3310 | SetPrivateState(eStateRunning); |
| 3311 | SetPrivateState(eStateStopped); |
| 3312 | } |
| 3313 | } else if (log) |
| 3314 | log->Printf("Process::PrivateResume() got an error \"%s\".", |
| 3315 | error.AsCString("<unknown error>")); |
| 3316 | return error; |
| 3317 | } |
| 3318 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3319 | Status Process::Halt(bool clear_thread_plans, bool use_run_lock) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3320 | if (!StateIsRunningState(m_public_state.GetValue())) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3321 | return Status("Process is not running."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3322 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3323 | // Don't clear the m_clear_thread_plans_on_stop, only set it to true if in |
| 3324 | // case it was already set and some thread plan logic calls halt on its own. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3325 | m_clear_thread_plans_on_stop |= clear_thread_plans; |
| 3326 | |
| 3327 | ListenerSP halt_listener_sp( |
| 3328 | Listener::MakeListener("lldb.process.halt_listener")); |
| 3329 | HijackProcessEvents(halt_listener_sp); |
| 3330 | |
| 3331 | EventSP event_sp; |
| 3332 | |
| 3333 | SendAsyncInterrupt(); |
| 3334 | |
| 3335 | if (m_public_state.GetValue() == eStateAttaching) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3336 | // Don't hijack and eat the eStateExited as the code that was doing the |
| 3337 | // attach will be waiting for this event... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3338 | RestoreProcessEvents(); |
| 3339 | SetExitStatus(SIGKILL, "Cancelled async attach."); |
| 3340 | Destroy(false); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3341 | return Status(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | // Wait for 10 second for the process to stop. |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3345 | StateType state = WaitForProcessToStop( |
| 3346 | seconds(10), &event_sp, true, halt_listener_sp, nullptr, use_run_lock); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3347 | RestoreProcessEvents(); |
| 3348 | |
| 3349 | if (state == eStateInvalid || !event_sp) { |
| 3350 | // We timed out and didn't get a stop event... |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3351 | return Status("Halt timed out. State = %s", StateAsCString(GetState())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | BroadcastEvent(event_sp); |
| 3355 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3356 | return Status(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3359 | Status Process::StopForDestroyOrDetach(lldb::EventSP &exit_event_sp) { |
| 3360 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3361 | |
| 3362 | // Check both the public & private states here. If we're hung evaluating an |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3363 | // expression, for instance, then the public state will be stopped, but we |
| 3364 | // still need to interrupt. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3365 | if (m_public_state.GetValue() == eStateRunning || |
| 3366 | m_private_state.GetValue() == eStateRunning) { |
| 3367 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3368 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3369 | log->Printf("Process::%s() About to stop.", __FUNCTION__); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3370 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3371 | ListenerSP listener_sp( |
| 3372 | Listener::MakeListener("lldb.Process.StopForDestroyOrDetach.hijack")); |
| 3373 | HijackProcessEvents(listener_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3374 | |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3375 | SendAsyncInterrupt(); |
| 3376 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3377 | // Consume the interrupt event. |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3378 | StateType state = |
| 3379 | WaitForProcessToStop(seconds(10), &exit_event_sp, true, listener_sp); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3380 | |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3381 | RestoreProcessEvents(); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3382 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3383 | // If the process exited while we were waiting for it to stop, put the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3384 | // exited event into the shared pointer passed in and return. Our caller |
| 3385 | // doesn't need to do anything else, since they don't have a process |
| 3386 | // anymore... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3387 | |
| 3388 | if (state == eStateExited || m_private_state.GetValue() == eStateExited) { |
| 3389 | if (log) |
| 3390 | log->Printf("Process::%s() Process exited while waiting to stop.", |
| 3391 | __FUNCTION__); |
| 3392 | return error; |
| 3393 | } else |
| 3394 | exit_event_sp.reset(); // It is ok to consume any non-exit stop events |
| 3395 | |
| 3396 | if (state != eStateStopped) { |
| 3397 | if (log) |
| 3398 | log->Printf("Process::%s() failed to stop, state is: %s", __FUNCTION__, |
| 3399 | StateAsCString(state)); |
| 3400 | // If we really couldn't stop the process then we should just error out |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3401 | // here, but if the lower levels just bobbled sending the event and we |
| 3402 | // really are stopped, then continue on. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3403 | StateType private_state = m_private_state.GetValue(); |
| 3404 | if (private_state != eStateStopped) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3405 | return Status( |
| 3406 | "Attempt to stop the target in order to detach timed out. " |
| 3407 | "State = %s", |
| 3408 | StateAsCString(GetState())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3409 | } |
| 3410 | } |
| 3411 | } |
| 3412 | return error; |
| 3413 | } |
| 3414 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3415 | Status Process::Detach(bool keep_stopped) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3416 | EventSP exit_event_sp; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3417 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3418 | m_destroy_in_process = true; |
| 3419 | |
| 3420 | error = WillDetach(); |
| 3421 | |
| 3422 | if (error.Success()) { |
| 3423 | if (DetachRequiresHalt()) { |
| 3424 | error = StopForDestroyOrDetach(exit_event_sp); |
| 3425 | if (!error.Success()) { |
| 3426 | m_destroy_in_process = false; |
| 3427 | return error; |
| 3428 | } else if (exit_event_sp) { |
| 3429 | // We shouldn't need to do anything else here. There's no process left |
| 3430 | // to detach from... |
| 3431 | StopPrivateStateThread(); |
| 3432 | m_destroy_in_process = false; |
| 3433 | return error; |
| 3434 | } |
| 3435 | } |
| 3436 | |
| 3437 | m_thread_list.DiscardThreadPlans(); |
| 3438 | DisableAllBreakpointSites(); |
| 3439 | |
| 3440 | error = DoDetach(keep_stopped); |
| 3441 | if (error.Success()) { |
| 3442 | DidDetach(); |
| 3443 | StopPrivateStateThread(); |
| 3444 | } else { |
| 3445 | return error; |
| 3446 | } |
| 3447 | } |
| 3448 | m_destroy_in_process = false; |
| 3449 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3450 | // If we exited when we were waiting for a process to stop, then forward the |
| 3451 | // event here so we don't lose the event |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3452 | if (exit_event_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3453 | // Directly broadcast our exited event because we shut down our private |
| 3454 | // state thread above |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3455 | BroadcastEvent(exit_event_sp); |
| 3456 | } |
| 3457 | |
| 3458 | // If we have been interrupted (to kill us) in the middle of running, we may |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3459 | // not end up propagating the last events through the event system, in which |
| 3460 | // case we might strand the write lock. Unlock it here so when we do to tear |
| 3461 | // down the process we don't get an error destroying the lock. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3462 | |
| 3463 | m_public_run_lock.SetStopped(); |
| 3464 | return error; |
| 3465 | } |
| 3466 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3467 | Status Process::Destroy(bool force_kill) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3468 | |
| 3469 | // Tell ourselves we are in the process of destroying the process, so that we |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3470 | // don't do any unnecessary work that might hinder the destruction. Remember |
| 3471 | // to set this back to false when we are done. That way if the attempt |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3472 | // failed and the process stays around for some reason it won't be in a |
| 3473 | // confused state. |
| 3474 | |
| 3475 | if (force_kill) |
| 3476 | m_should_detach = false; |
| 3477 | |
| 3478 | if (GetShouldDetach()) { |
| 3479 | // FIXME: This will have to be a process setting: |
| 3480 | bool keep_stopped = false; |
| 3481 | Detach(keep_stopped); |
| 3482 | } |
| 3483 | |
| 3484 | m_destroy_in_process = true; |
| 3485 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3486 | Status error(WillDestroy()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3487 | if (error.Success()) { |
| 3488 | EventSP exit_event_sp; |
| 3489 | if (DestroyRequiresHalt()) { |
| 3490 | error = StopForDestroyOrDetach(exit_event_sp); |
| 3491 | } |
| 3492 | |
| 3493 | if (m_public_state.GetValue() != eStateRunning) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3494 | // Ditch all thread plans, and remove all our breakpoints: in case we |
| 3495 | // have to restart the target to kill it, we don't want it hitting a |
| 3496 | // breakpoint... Only do this if we've stopped, however, since if we |
| 3497 | // didn't manage to halt it above, then we're not going to have much luck |
| 3498 | // doing this now. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3499 | m_thread_list.DiscardThreadPlans(); |
| 3500 | DisableAllBreakpointSites(); |
| 3501 | } |
| 3502 | |
| 3503 | error = DoDestroy(); |
| 3504 | if (error.Success()) { |
| 3505 | DidDestroy(); |
| 3506 | StopPrivateStateThread(); |
| 3507 | } |
| 3508 | m_stdio_communication.Disconnect(); |
| 3509 | m_stdio_communication.StopReadThread(); |
| 3510 | m_stdin_forward = false; |
| 3511 | |
| 3512 | if (m_process_input_reader) { |
| 3513 | m_process_input_reader->SetIsDone(true); |
| 3514 | m_process_input_reader->Cancel(); |
| 3515 | m_process_input_reader.reset(); |
| 3516 | } |
| 3517 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3518 | // If we exited when we were waiting for a process to stop, then forward |
| 3519 | // the event here so we don't lose the event |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3520 | if (exit_event_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3521 | // Directly broadcast our exited event because we shut down our private |
| 3522 | // state thread above |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3523 | BroadcastEvent(exit_event_sp); |
| 3524 | } |
| 3525 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3526 | // If we have been interrupted (to kill us) in the middle of running, we |
| 3527 | // may not end up propagating the last events through the event system, in |
| 3528 | // which case we might strand the write lock. Unlock it here so when we do |
| 3529 | // to tear down the process we don't get an error destroying the lock. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3530 | m_public_run_lock.SetStopped(); |
| 3531 | } |
| 3532 | |
| 3533 | m_destroy_in_process = false; |
| 3534 | |
| 3535 | return error; |
| 3536 | } |
| 3537 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3538 | Status Process::Signal(int signal) { |
| 3539 | Status error(WillSignal()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3540 | if (error.Success()) { |
| 3541 | error = DoSignal(signal); |
| 3542 | if (error.Success()) |
| 3543 | DidSignal(); |
| 3544 | } |
| 3545 | return error; |
| 3546 | } |
| 3547 | |
| 3548 | void Process::SetUnixSignals(UnixSignalsSP &&signals_sp) { |
| 3549 | assert(signals_sp && "null signals_sp"); |
| 3550 | m_unix_signals_sp = signals_sp; |
| 3551 | } |
| 3552 | |
| 3553 | const lldb::UnixSignalsSP &Process::GetUnixSignals() { |
| 3554 | assert(m_unix_signals_sp && "null m_unix_signals_sp"); |
| 3555 | return m_unix_signals_sp; |
| 3556 | } |
| 3557 | |
| 3558 | lldb::ByteOrder Process::GetByteOrder() const { |
| 3559 | return GetTarget().GetArchitecture().GetByteOrder(); |
| 3560 | } |
| 3561 | |
| 3562 | uint32_t Process::GetAddressByteSize() const { |
| 3563 | return GetTarget().GetArchitecture().GetAddressByteSize(); |
| 3564 | } |
| 3565 | |
| 3566 | bool Process::ShouldBroadcastEvent(Event *event_ptr) { |
| 3567 | const StateType state = |
| 3568 | Process::ProcessEventData::GetStateFromEvent(event_ptr); |
| 3569 | bool return_value = true; |
| 3570 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | |
| 3571 | LIBLLDB_LOG_PROCESS)); |
| 3572 | |
| 3573 | switch (state) { |
| 3574 | case eStateDetached: |
| 3575 | case eStateExited: |
| 3576 | case eStateUnloaded: |
| 3577 | m_stdio_communication.SynchronizeWithReadThread(); |
| 3578 | m_stdio_communication.Disconnect(); |
| 3579 | m_stdio_communication.StopReadThread(); |
| 3580 | m_stdin_forward = false; |
| 3581 | |
| 3582 | LLVM_FALLTHROUGH; |
| 3583 | case eStateConnected: |
| 3584 | case eStateAttaching: |
| 3585 | case eStateLaunching: |
| 3586 | // These events indicate changes in the state of the debugging session, |
| 3587 | // always report them. |
| 3588 | return_value = true; |
| 3589 | break; |
| 3590 | case eStateInvalid: |
| 3591 | // We stopped for no apparent reason, don't report it. |
| 3592 | return_value = false; |
| 3593 | break; |
| 3594 | case eStateRunning: |
| 3595 | case eStateStepping: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3596 | // If we've started the target running, we handle the cases where we are |
| 3597 | // already running and where there is a transition from stopped to running |
| 3598 | // differently. running -> running: Automatically suppress extra running |
| 3599 | // events stopped -> running: Report except when there is one or more no |
| 3600 | // votes |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3601 | // and no yes votes. |
| 3602 | SynchronouslyNotifyStateChanged(state); |
| 3603 | if (m_force_next_event_delivery) |
| 3604 | return_value = true; |
| 3605 | else { |
| 3606 | switch (m_last_broadcast_state) { |
| 3607 | case eStateRunning: |
| 3608 | case eStateStepping: |
| 3609 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 3610 | return_value = false; |
| 3611 | break; |
| 3612 | default: |
| 3613 | // TODO: make this work correctly. For now always report |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3614 | // run if we aren't running so we don't miss any running events. If I |
| 3615 | // run the lldb/test/thread/a.out file and break at main.cpp:58, run |
| 3616 | // and hit the breakpoints on multiple threads, then somehow during the |
| 3617 | // stepping over of all breakpoints no run gets reported. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3618 | |
| 3619 | // This is a transition from stop to run. |
| 3620 | switch (m_thread_list.ShouldReportRun(event_ptr)) { |
| 3621 | case eVoteYes: |
| 3622 | case eVoteNoOpinion: |
| 3623 | return_value = true; |
| 3624 | break; |
| 3625 | case eVoteNo: |
| 3626 | return_value = false; |
| 3627 | break; |
| 3628 | } |
| 3629 | break; |
| 3630 | } |
| 3631 | } |
| 3632 | break; |
| 3633 | case eStateStopped: |
| 3634 | case eStateCrashed: |
| 3635 | case eStateSuspended: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3636 | // We've stopped. First see if we're going to restart the target. If we |
| 3637 | // are going to stop, then we always broadcast the event. If we aren't |
| 3638 | // going to stop, let the thread plans decide if we're going to report this |
| 3639 | // event. If no thread has an opinion, we don't report it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3640 | |
| 3641 | m_stdio_communication.SynchronizeWithReadThread(); |
| 3642 | RefreshStateAfterStop(); |
| 3643 | if (ProcessEventData::GetInterruptedFromEvent(event_ptr)) { |
| 3644 | if (log) |
| 3645 | log->Printf("Process::ShouldBroadcastEvent (%p) stopped due to an " |
| 3646 | "interrupt, state: %s", |
| 3647 | static_cast<void *>(event_ptr), StateAsCString(state)); |
| 3648 | // Even though we know we are going to stop, we should let the threads |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3649 | // have a look at the stop, so they can properly set their state. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3650 | m_thread_list.ShouldStop(event_ptr); |
| 3651 | return_value = true; |
| 3652 | } else { |
| 3653 | bool was_restarted = ProcessEventData::GetRestartedFromEvent(event_ptr); |
| 3654 | bool should_resume = false; |
| 3655 | |
| 3656 | // It makes no sense to ask "ShouldStop" if we've already been |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3657 | // restarted... Asking the thread list is also not likely to go well, |
| 3658 | // since we are running again. So in that case just report the event. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3659 | |
| 3660 | if (!was_restarted) |
| 3661 | should_resume = !m_thread_list.ShouldStop(event_ptr); |
| 3662 | |
| 3663 | if (was_restarted || should_resume || m_resume_requested) { |
| 3664 | Vote stop_vote = m_thread_list.ShouldReportStop(event_ptr); |
| 3665 | if (log) |
| 3666 | log->Printf("Process::ShouldBroadcastEvent: should_resume: %i state: " |
| 3667 | "%s was_restarted: %i stop_vote: %d.", |
| 3668 | should_resume, StateAsCString(state), was_restarted, |
| 3669 | stop_vote); |
| 3670 | |
| 3671 | switch (stop_vote) { |
| 3672 | case eVoteYes: |
| 3673 | return_value = true; |
| 3674 | break; |
| 3675 | case eVoteNoOpinion: |
| 3676 | case eVoteNo: |
| 3677 | return_value = false; |
| 3678 | break; |
| 3679 | } |
| 3680 | |
| 3681 | if (!was_restarted) { |
| 3682 | if (log) |
| 3683 | log->Printf("Process::ShouldBroadcastEvent (%p) Restarting process " |
| 3684 | "from state: %s", |
| 3685 | static_cast<void *>(event_ptr), StateAsCString(state)); |
| 3686 | ProcessEventData::SetRestartedInEvent(event_ptr, true); |
| 3687 | PrivateResume(); |
| 3688 | } |
| 3689 | } else { |
| 3690 | return_value = true; |
| 3691 | SynchronouslyNotifyStateChanged(state); |
| 3692 | } |
| 3693 | } |
| 3694 | break; |
| 3695 | } |
| 3696 | |
| 3697 | // Forcing the next event delivery is a one shot deal. So reset it here. |
| 3698 | m_force_next_event_delivery = false; |
| 3699 | |
| 3700 | // We do some coalescing of events (for instance two consecutive running |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3701 | // events get coalesced.) But we only coalesce against events we actually |
| 3702 | // broadcast. So we use m_last_broadcast_state to track that. NB - you |
| 3703 | // can't use "m_public_state.GetValue()" for that purpose, as was originally |
| 3704 | // done, because the PublicState reflects the last event pulled off the |
| 3705 | // queue, and there may be several events stacked up on the queue unserviced. |
| 3706 | // So the PublicState may not reflect the last broadcasted event yet. |
| 3707 | // m_last_broadcast_state gets updated here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3708 | |
| 3709 | if (return_value) |
| 3710 | m_last_broadcast_state = state; |
| 3711 | |
| 3712 | if (log) |
| 3713 | log->Printf("Process::ShouldBroadcastEvent (%p) => new state: %s, last " |
| 3714 | "broadcast state: %s - %s", |
| 3715 | static_cast<void *>(event_ptr), StateAsCString(state), |
| 3716 | StateAsCString(m_last_broadcast_state), |
| 3717 | return_value ? "YES" : "NO"); |
| 3718 | return return_value; |
| 3719 | } |
| 3720 | |
| 3721 | bool Process::StartPrivateStateThread(bool is_secondary_thread) { |
| 3722 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
| 3723 | |
| 3724 | bool already_running = PrivateStateThreadIsValid(); |
| 3725 | if (log) |
| 3726 | log->Printf("Process::%s()%s ", __FUNCTION__, |
| 3727 | already_running ? " already running" |
| 3728 | : " starting private state thread"); |
| 3729 | |
| 3730 | if (!is_secondary_thread && already_running) |
| 3731 | return true; |
| 3732 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3733 | // Create a thread that watches our internal state and controls which events |
| 3734 | // make it to clients (into the DCProcess event queue). |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3735 | char thread_name[1024]; |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 3736 | uint32_t max_len = llvm::get_max_thread_name_length(); |
| 3737 | if (max_len > 0 && max_len <= 30) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3738 | // On platforms with abbreviated thread name lengths, choose thread names |
| 3739 | // that fit within the limit. |
| 3740 | if (already_running) |
| 3741 | snprintf(thread_name, sizeof(thread_name), "intern-state-OV"); |
| 3742 | else |
| 3743 | snprintf(thread_name, sizeof(thread_name), "intern-state"); |
| 3744 | } else { |
| 3745 | if (already_running) |
| 3746 | snprintf(thread_name, sizeof(thread_name), |
| 3747 | "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", |
| 3748 | GetID()); |
| 3749 | else |
| 3750 | snprintf(thread_name, sizeof(thread_name), |
| 3751 | "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID()); |
| 3752 | } |
| 3753 | |
| 3754 | // Create the private state thread, and start it running. |
| 3755 | PrivateStateThreadArgs *args_ptr = |
| 3756 | new PrivateStateThreadArgs(this, is_secondary_thread); |
| 3757 | m_private_state_thread = |
| 3758 | ThreadLauncher::LaunchThread(thread_name, Process::PrivateStateThread, |
| 3759 | (void *)args_ptr, nullptr, 8 * 1024 * 1024); |
| 3760 | if (m_private_state_thread.IsJoinable()) { |
| 3761 | ResumePrivateStateThread(); |
| 3762 | return true; |
| 3763 | } else |
| 3764 | return false; |
| 3765 | } |
| 3766 | |
| 3767 | void Process::PausePrivateStateThread() { |
| 3768 | ControlPrivateStateThread(eBroadcastInternalStateControlPause); |
| 3769 | } |
| 3770 | |
| 3771 | void Process::ResumePrivateStateThread() { |
| 3772 | ControlPrivateStateThread(eBroadcastInternalStateControlResume); |
| 3773 | } |
| 3774 | |
| 3775 | void Process::StopPrivateStateThread() { |
| 3776 | if (m_private_state_thread.IsJoinable()) |
| 3777 | ControlPrivateStateThread(eBroadcastInternalStateControlStop); |
| 3778 | else { |
| 3779 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 3780 | if (log) |
| 3781 | log->Printf( |
| 3782 | "Went to stop the private state thread, but it was already invalid."); |
| 3783 | } |
| 3784 | } |
| 3785 | |
| 3786 | void Process::ControlPrivateStateThread(uint32_t signal) { |
| 3787 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 3788 | |
| 3789 | assert(signal == eBroadcastInternalStateControlStop || |
| 3790 | signal == eBroadcastInternalStateControlPause || |
| 3791 | signal == eBroadcastInternalStateControlResume); |
| 3792 | |
| 3793 | if (log) |
| 3794 | log->Printf("Process::%s (signal = %d)", __FUNCTION__, signal); |
| 3795 | |
| 3796 | // Signal the private state thread |
| 3797 | if (m_private_state_thread.IsJoinable()) { |
| 3798 | // Broadcast the event. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3799 | // It is important to do this outside of the if below, because it's |
| 3800 | // possible that the thread state is invalid but that the thread is waiting |
| 3801 | // on a control event instead of simply being on its way out (this should |
| 3802 | // not happen, but it apparently can). |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3803 | if (log) |
| 3804 | log->Printf("Sending control event of type: %d.", signal); |
| 3805 | std::shared_ptr<EventDataReceipt> event_receipt_sp(new EventDataReceipt()); |
| 3806 | m_private_state_control_broadcaster.BroadcastEvent(signal, |
| 3807 | event_receipt_sp); |
| 3808 | |
| 3809 | // Wait for the event receipt or for the private state thread to exit |
| 3810 | bool receipt_received = false; |
| 3811 | if (PrivateStateThreadIsValid()) { |
| 3812 | while (!receipt_received) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3813 | // Check for a receipt for 2 seconds and then check if the private |
| 3814 | // state thread is still around. |
Pavel Labath | 38d67db | 2018-05-03 15:33:41 +0000 | [diff] [blame] | 3815 | receipt_received = |
| 3816 | event_receipt_sp->WaitForEventReceived(std::chrono::seconds(2)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3817 | if (!receipt_received) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3818 | // Check if the private state thread is still around. If it isn't |
| 3819 | // then we are done waiting |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3820 | if (!PrivateStateThreadIsValid()) |
| 3821 | break; // Private state thread exited or is exiting, we are done |
| 3822 | } |
| 3823 | } |
| 3824 | } |
| 3825 | |
| 3826 | if (signal == eBroadcastInternalStateControlStop) { |
| 3827 | thread_result_t result = NULL; |
| 3828 | m_private_state_thread.Join(&result); |
| 3829 | m_private_state_thread.Reset(); |
| 3830 | } |
| 3831 | } else { |
| 3832 | if (log) |
| 3833 | log->Printf( |
| 3834 | "Private state thread already dead, no need to signal it to stop."); |
| 3835 | } |
| 3836 | } |
| 3837 | |
| 3838 | void Process::SendAsyncInterrupt() { |
| 3839 | if (PrivateStateThreadIsValid()) |
| 3840 | m_private_state_broadcaster.BroadcastEvent(Process::eBroadcastBitInterrupt, |
| 3841 | nullptr); |
| 3842 | else |
| 3843 | BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr); |
| 3844 | } |
| 3845 | |
| 3846 | void Process::HandlePrivateEvent(EventSP &event_sp) { |
| 3847 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 3848 | m_resume_requested = false; |
| 3849 | |
| 3850 | const StateType new_state = |
| 3851 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 3852 | |
| 3853 | // First check to see if anybody wants a shot at this event: |
| 3854 | if (m_next_event_action_ap) { |
| 3855 | NextEventAction::EventActionResult action_result = |
| 3856 | m_next_event_action_ap->PerformAction(event_sp); |
| 3857 | if (log) |
| 3858 | log->Printf("Ran next event action, result was %d.", action_result); |
| 3859 | |
| 3860 | switch (action_result) { |
| 3861 | case NextEventAction::eEventActionSuccess: |
| 3862 | SetNextEventAction(nullptr); |
| 3863 | break; |
| 3864 | |
| 3865 | case NextEventAction::eEventActionRetry: |
| 3866 | break; |
| 3867 | |
| 3868 | case NextEventAction::eEventActionExit: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3869 | // Handle Exiting Here. If we already got an exited event, we should |
| 3870 | // just propagate it. Otherwise, swallow this event, and set our state |
| 3871 | // to exit so the next event will kill us. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3872 | if (new_state != eStateExited) { |
| 3873 | // FIXME: should cons up an exited event, and discard this one. |
| 3874 | SetExitStatus(0, m_next_event_action_ap->GetExitString()); |
| 3875 | SetNextEventAction(nullptr); |
| 3876 | return; |
| 3877 | } |
| 3878 | SetNextEventAction(nullptr); |
| 3879 | break; |
| 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | // See if we should broadcast this state to external clients? |
| 3884 | const bool should_broadcast = ShouldBroadcastEvent(event_sp.get()); |
| 3885 | |
| 3886 | if (should_broadcast) { |
| 3887 | const bool is_hijacked = IsHijackedForEvent(eBroadcastBitStateChanged); |
| 3888 | if (log) { |
| 3889 | log->Printf("Process::%s (pid = %" PRIu64 |
| 3890 | ") broadcasting new state %s (old state %s) to %s", |
| 3891 | __FUNCTION__, GetID(), StateAsCString(new_state), |
| 3892 | StateAsCString(GetState()), |
| 3893 | is_hijacked ? "hijacked" : "public"); |
| 3894 | } |
| 3895 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
| 3896 | if (StateIsRunningState(new_state)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3897 | // Only push the input handler if we aren't fowarding events, as this |
| 3898 | // means the curses GUI is in use... Or don't push it if we are launching |
| 3899 | // since it will come up stopped. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3900 | if (!GetTarget().GetDebugger().IsForwardingEvents() && |
| 3901 | new_state != eStateLaunching && new_state != eStateAttaching) { |
| 3902 | PushProcessIOHandler(); |
| 3903 | m_iohandler_sync.SetValue(m_iohandler_sync.GetValue() + 1, |
| 3904 | eBroadcastAlways); |
| 3905 | if (log) |
| 3906 | log->Printf("Process::%s updated m_iohandler_sync to %d", |
| 3907 | __FUNCTION__, m_iohandler_sync.GetValue()); |
| 3908 | } |
| 3909 | } else if (StateIsStoppedState(new_state, false)) { |
| 3910 | if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3911 | // If the lldb_private::Debugger is handling the events, we don't want |
| 3912 | // to pop the process IOHandler here, we want to do it when we receive |
| 3913 | // the stopped event so we can carefully control when the process |
| 3914 | // IOHandler is popped because when we stop we want to display some |
| 3915 | // text stating how and why we stopped, then maybe some |
| 3916 | // process/thread/frame info, and then we want the "(lldb) " prompt to |
| 3917 | // show up. If we pop the process IOHandler here, then we will cause |
| 3918 | // the command interpreter to become the top IOHandler after the |
| 3919 | // process pops off and it will update its prompt right away... See the |
| 3920 | // Debugger.cpp file where it calls the function as |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3921 | // "process_sp->PopProcessIOHandler()" to see where I am talking about. |
| 3922 | // Otherwise we end up getting overlapping "(lldb) " prompts and |
| 3923 | // garbled output. |
| 3924 | // |
| 3925 | // If we aren't handling the events in the debugger (which is indicated |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3926 | // by "m_target.GetDebugger().IsHandlingEvents()" returning false) or |
| 3927 | // we are hijacked, then we always pop the process IO handler manually. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3928 | // Hijacking happens when the internal process state thread is running |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3929 | // thread plans, or when commands want to run in synchronous mode and |
| 3930 | // they call "process->WaitForProcessToStop()". An example of something |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3931 | // that will hijack the events is a simple expression: |
| 3932 | // |
| 3933 | // (lldb) expr (int)puts("hello") |
| 3934 | // |
| 3935 | // This will cause the internal process state thread to resume and halt |
| 3936 | // the process (and _it_ will hijack the eBroadcastBitStateChanged |
| 3937 | // events) and we do need the IO handler to be pushed and popped |
| 3938 | // correctly. |
| 3939 | |
| 3940 | if (is_hijacked || !GetTarget().GetDebugger().IsHandlingEvents()) |
| 3941 | PopProcessIOHandler(); |
| 3942 | } |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | BroadcastEvent(event_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3946 | } else { |
| 3947 | if (log) { |
| 3948 | log->Printf( |
| 3949 | "Process::%s (pid = %" PRIu64 |
| 3950 | ") suppressing state %s (old state %s): should_broadcast == false", |
| 3951 | __FUNCTION__, GetID(), StateAsCString(new_state), |
| 3952 | StateAsCString(GetState())); |
| 3953 | } |
| 3954 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3957 | Status Process::HaltPrivate() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3958 | EventSP event_sp; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3959 | Status error(WillHalt()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3960 | if (error.Fail()) |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3961 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3962 | |
| 3963 | // Ask the process subclass to actually halt our process |
| 3964 | bool caused_stop; |
| 3965 | error = DoHalt(caused_stop); |
| 3966 | |
| 3967 | DidHalt(); |
| 3968 | return error; |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3971 | thread_result_t Process::PrivateStateThread(void *arg) { |
| 3972 | std::unique_ptr<PrivateStateThreadArgs> args_up( |
| 3973 | static_cast<PrivateStateThreadArgs *>(arg)); |
| 3974 | thread_result_t result = |
| 3975 | args_up->process->RunPrivateStateThread(args_up->is_secondary_thread); |
| 3976 | return result; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3979 | thread_result_t Process::RunPrivateStateThread(bool is_secondary_thread) { |
| 3980 | bool control_only = true; |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3981 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3982 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 3983 | if (log) |
| 3984 | log->Printf("Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...", |
| 3985 | __FUNCTION__, static_cast<void *>(this), GetID()); |
Jason Molenda | ede3193 | 2015-04-17 05:01:58 +0000 | [diff] [blame] | 3986 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3987 | bool exit_now = false; |
| 3988 | bool interrupt_requested = false; |
| 3989 | while (!exit_now) { |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3990 | EventSP event_sp; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3991 | GetEventsPrivate(event_sp, llvm::None, control_only); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3992 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) { |
| 3993 | if (log) |
| 3994 | log->Printf("Process::%s (arg = %p, pid = %" PRIu64 |
| 3995 | ") got a control event: %d", |
| 3996 | __FUNCTION__, static_cast<void *>(this), GetID(), |
| 3997 | event_sp->GetType()); |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3998 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3999 | switch (event_sp->GetType()) { |
| 4000 | case eBroadcastInternalStateControlStop: |
| 4001 | exit_now = true; |
| 4002 | break; // doing any internal state management below |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 4003 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4004 | case eBroadcastInternalStateControlPause: |
| 4005 | control_only = true; |
| 4006 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4007 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4008 | case eBroadcastInternalStateControlResume: |
| 4009 | control_only = false; |
| 4010 | break; |
| 4011 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4012 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4013 | continue; |
| 4014 | } else if (event_sp->GetType() == eBroadcastBitInterrupt) { |
| 4015 | if (m_public_state.GetValue() == eStateAttaching) { |
| 4016 | if (log) |
| 4017 | log->Printf("Process::%s (arg = %p, pid = %" PRIu64 |
| 4018 | ") woke up with an interrupt while attaching - " |
| 4019 | "forwarding interrupt.", |
| 4020 | __FUNCTION__, static_cast<void *>(this), GetID()); |
| 4021 | BroadcastEvent(eBroadcastBitInterrupt, nullptr); |
| 4022 | } else if (StateIsRunningState(m_last_broadcast_state)) { |
| 4023 | if (log) |
| 4024 | log->Printf("Process::%s (arg = %p, pid = %" PRIu64 |
| 4025 | ") woke up with an interrupt - Halting.", |
| 4026 | __FUNCTION__, static_cast<void *>(this), GetID()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4027 | Status error = HaltPrivate(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4028 | if (error.Fail() && log) |
| 4029 | log->Printf("Process::%s (arg = %p, pid = %" PRIu64 |
| 4030 | ") failed to halt the process: %s", |
| 4031 | __FUNCTION__, static_cast<void *>(this), GetID(), |
| 4032 | error.AsCString()); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4033 | // Halt should generate a stopped event. Make a note of the fact that |
| 4034 | // we were doing the interrupt, so we can set the interrupted flag |
| 4035 | // after we receive the event. We deliberately set this to true even if |
| 4036 | // HaltPrivate failed, so that we can interrupt on the next natural |
| 4037 | // stop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4038 | interrupt_requested = true; |
| 4039 | } else { |
| 4040 | // This can happen when someone (e.g. Process::Halt) sees that we are |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4041 | // running and sends an interrupt request, but the process actually |
| 4042 | // stops before we receive it. In that case, we can just ignore the |
| 4043 | // request. We use m_last_broadcast_state, because the Stopped event |
| 4044 | // may not have been popped of the event queue yet, which is when the |
| 4045 | // public state gets updated. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4046 | if (log) |
| 4047 | log->Printf( |
| 4048 | "Process::%s ignoring interrupt as we have already stopped.", |
| 4049 | __FUNCTION__); |
| 4050 | } |
| 4051 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4054 | const StateType internal_state = |
| 4055 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4056 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4057 | if (internal_state != eStateInvalid) { |
| 4058 | if (m_clear_thread_plans_on_stop && |
| 4059 | StateIsStoppedState(internal_state, true)) { |
| 4060 | m_clear_thread_plans_on_stop = false; |
| 4061 | m_thread_list.DiscardThreadPlans(); |
| 4062 | } |
| 4063 | |
| 4064 | if (interrupt_requested) { |
| 4065 | if (StateIsStoppedState(internal_state, true)) { |
| 4066 | // We requested the interrupt, so mark this as such in the stop event |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4067 | // so clients can tell an interrupted process from a natural stop |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4068 | ProcessEventData::SetInterruptedInEvent(event_sp.get(), true); |
| 4069 | interrupt_requested = false; |
| 4070 | } else if (log) { |
| 4071 | log->Printf("Process::%s interrupt_requested, but a non-stopped " |
| 4072 | "state '%s' received.", |
| 4073 | __FUNCTION__, StateAsCString(internal_state)); |
| 4074 | } |
| 4075 | } |
| 4076 | |
| 4077 | HandlePrivateEvent(event_sp); |
| 4078 | } |
| 4079 | |
| 4080 | if (internal_state == eStateInvalid || internal_state == eStateExited || |
| 4081 | internal_state == eStateDetached) { |
| 4082 | if (log) |
| 4083 | log->Printf("Process::%s (arg = %p, pid = %" PRIu64 |
| 4084 | ") about to exit with internal state %s...", |
| 4085 | __FUNCTION__, static_cast<void *>(this), GetID(), |
| 4086 | StateAsCString(internal_state)); |
| 4087 | |
| 4088 | break; |
| 4089 | } |
| 4090 | } |
| 4091 | |
| 4092 | // Verify log is still enabled before attempting to write to it... |
| 4093 | if (log) |
| 4094 | log->Printf("Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", |
| 4095 | __FUNCTION__, static_cast<void *>(this), GetID()); |
| 4096 | |
| 4097 | // If we are a secondary thread, then the primary thread we are working for |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4098 | // will have already acquired the public_run_lock, and isn't done with what |
| 4099 | // it was doing yet, so don't try to change it on the way out. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4100 | if (!is_secondary_thread) |
| 4101 | m_public_run_lock.SetStopped(); |
| 4102 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4105 | //------------------------------------------------------------------ |
| 4106 | // Process Event Data |
| 4107 | //------------------------------------------------------------------ |
| 4108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4109 | Process::ProcessEventData::ProcessEventData() |
| 4110 | : EventData(), m_process_wp(), m_state(eStateInvalid), m_restarted(false), |
| 4111 | m_update_state(0), m_interrupted(false) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4113 | Process::ProcessEventData::ProcessEventData(const ProcessSP &process_sp, |
| 4114 | StateType state) |
| 4115 | : EventData(), m_process_wp(), m_state(state), m_restarted(false), |
| 4116 | m_update_state(0), m_interrupted(false) { |
| 4117 | if (process_sp) |
| 4118 | m_process_wp = process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 4121 | Process::ProcessEventData::~ProcessEventData() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4123 | const ConstString &Process::ProcessEventData::GetFlavorString() { |
| 4124 | static ConstString g_flavor("Process::ProcessEventData"); |
| 4125 | return g_flavor; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4128 | const ConstString &Process::ProcessEventData::GetFlavor() const { |
| 4129 | return ProcessEventData::GetFlavorString(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4132 | void Process::ProcessEventData::DoOnRemoval(Event *event_ptr) { |
| 4133 | ProcessSP process_sp(m_process_wp.lock()); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 4134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4135 | if (!process_sp) |
| 4136 | return; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 4137 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4138 | // This function gets called twice for each event, once when the event gets |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4139 | // pulled off of the private process event queue, and then any number of |
| 4140 | // times, first when it gets pulled off of the public event queue, then other |
| 4141 | // times when we're pretending that this is where we stopped at the end of |
| 4142 | // expression evaluation. m_update_state is used to distinguish these three |
| 4143 | // cases; it is 0 when we're just pulling it off for private handling, and > |
| 4144 | // 1 for expression evaluation, and we don't want to do the breakpoint |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4145 | // command handling then. |
| 4146 | if (m_update_state != 1) |
| 4147 | return; |
| 4148 | |
| 4149 | process_sp->SetPublicState( |
| 4150 | m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr)); |
| 4151 | |
Jason Molenda | b3a3cd1 | 2016-12-08 06:27:29 +0000 | [diff] [blame] | 4152 | if (m_state == eStateStopped && !m_restarted) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4153 | // Let process subclasses know we are about to do a public stop and do |
| 4154 | // anything they might need to in order to speed up register and memory |
| 4155 | // accesses. |
Jason Molenda | b3a3cd1 | 2016-12-08 06:27:29 +0000 | [diff] [blame] | 4156 | process_sp->WillPublicStop(); |
| 4157 | } |
| 4158 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4159 | // If this is a halt event, even if the halt stopped with some reason other |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4160 | // than a plain interrupt (e.g. we had already stopped for a breakpoint when |
| 4161 | // the halt request came through) don't do the StopInfo actions, as they may |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4162 | // end up restarting the process. |
| 4163 | if (m_interrupted) |
| 4164 | return; |
| 4165 | |
| 4166 | // If we're stopped and haven't restarted, then do the StopInfo actions here: |
| 4167 | if (m_state == eStateStopped && !m_restarted) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4168 | ThreadList &curr_thread_list = process_sp->GetThreadList(); |
| 4169 | uint32_t num_threads = curr_thread_list.GetSize(); |
| 4170 | uint32_t idx; |
| 4171 | |
| 4172 | // The actions might change one of the thread's stop_info's opinions about |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4173 | // whether we should stop the process, so we need to query that as we go. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4174 | |
| 4175 | // One other complication here, is that we try to catch any case where the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4176 | // target has run (except for expressions) and immediately exit, but if we |
| 4177 | // get that wrong (which is possible) then the thread list might have |
| 4178 | // changed, and that would cause our iteration here to crash. We could |
| 4179 | // make a copy of the thread list, but we'd really like to also know if it |
| 4180 | // has changed at all, so we make up a vector of the thread ID's and check |
| 4181 | // what we get back against this list & bag out if anything differs. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4182 | std::vector<uint32_t> thread_index_array(num_threads); |
| 4183 | for (idx = 0; idx < num_threads; ++idx) |
| 4184 | thread_index_array[idx] = |
| 4185 | curr_thread_list.GetThreadAtIndex(idx)->GetIndexID(); |
| 4186 | |
| 4187 | // Use this to track whether we should continue from here. We will only |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4188 | // continue the target running if no thread says we should stop. Of course |
| 4189 | // if some thread's PerformAction actually sets the target running, then it |
| 4190 | // doesn't matter what the other threads say... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4191 | |
| 4192 | bool still_should_stop = false; |
| 4193 | |
| 4194 | // Sometimes - for instance if we have a bug in the stub we are talking to, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4195 | // we stop but no thread has a valid stop reason. In that case we should |
| 4196 | // just stop, because we have no way of telling what the right thing to do |
| 4197 | // is, and it's better to let the user decide than continue behind their |
| 4198 | // backs. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4199 | |
| 4200 | bool does_anybody_have_an_opinion = false; |
| 4201 | |
| 4202 | for (idx = 0; idx < num_threads; ++idx) { |
| 4203 | curr_thread_list = process_sp->GetThreadList(); |
| 4204 | if (curr_thread_list.GetSize() != num_threads) { |
| 4205 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | |
| 4206 | LIBLLDB_LOG_PROCESS)); |
| 4207 | if (log) |
| 4208 | log->Printf( |
| 4209 | "Number of threads changed from %u to %u while processing event.", |
| 4210 | num_threads, curr_thread_list.GetSize()); |
| 4211 | break; |
| 4212 | } |
| 4213 | |
| 4214 | lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx); |
| 4215 | |
| 4216 | if (thread_sp->GetIndexID() != thread_index_array[idx]) { |
| 4217 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | |
| 4218 | LIBLLDB_LOG_PROCESS)); |
| 4219 | if (log) |
| 4220 | log->Printf("The thread at position %u changed from %u to %u while " |
| 4221 | "processing event.", |
| 4222 | idx, thread_index_array[idx], thread_sp->GetIndexID()); |
| 4223 | break; |
| 4224 | } |
| 4225 | |
| 4226 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo(); |
| 4227 | if (stop_info_sp && stop_info_sp->IsValid()) { |
| 4228 | does_anybody_have_an_opinion = true; |
| 4229 | bool this_thread_wants_to_stop; |
| 4230 | if (stop_info_sp->GetOverrideShouldStop()) { |
| 4231 | this_thread_wants_to_stop = |
| 4232 | stop_info_sp->GetOverriddenShouldStopValue(); |
| 4233 | } else { |
| 4234 | stop_info_sp->PerformAction(event_ptr); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4235 | // The stop action might restart the target. If it does, then we |
| 4236 | // want to mark that in the event so that whoever is receiving it |
| 4237 | // will know to wait for the running event and reflect that state |
| 4238 | // appropriately. We also need to stop processing actions, since they |
| 4239 | // aren't expecting the target to be running. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4240 | |
| 4241 | // FIXME: we might have run. |
| 4242 | if (stop_info_sp->HasTargetRunSinceMe()) { |
| 4243 | SetRestarted(true); |
| 4244 | break; |
| 4245 | } |
| 4246 | |
| 4247 | this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4248 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4249 | |
| 4250 | if (!still_should_stop) |
| 4251 | still_should_stop = this_thread_wants_to_stop; |
| 4252 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4253 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4254 | |
| 4255 | if (!GetRestarted()) { |
| 4256 | if (!still_should_stop && does_anybody_have_an_opinion) { |
| 4257 | // We've been asked to continue, so do that here. |
| 4258 | SetRestarted(true); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4259 | // Use the public resume method here, since this is just extending a |
| 4260 | // public resume. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4261 | process_sp->PrivateResume(); |
| 4262 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4263 | // If we didn't restart, run the Stop Hooks here: They might also |
| 4264 | // restart the target, so watch for that. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4265 | process_sp->GetTarget().RunStopHooks(); |
| 4266 | if (process_sp->GetPrivateState() == eStateRunning) |
| 4267 | SetRestarted(true); |
| 4268 | } |
| 4269 | } |
| 4270 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4273 | void Process::ProcessEventData::Dump(Stream *s) const { |
| 4274 | ProcessSP process_sp(m_process_wp.lock()); |
Greg Clayton | aeb3b8b | 2015-05-29 03:20:37 +0000 | [diff] [blame] | 4275 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4276 | if (process_sp) |
| 4277 | s->Printf(" process = %p (pid = %" PRIu64 "), ", |
| 4278 | static_cast<void *>(process_sp.get()), process_sp->GetID()); |
| 4279 | else |
| 4280 | s->PutCString(" process = NULL, "); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4281 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4282 | s->Printf("state = %s", StateAsCString(GetState())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | const Process::ProcessEventData * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4286 | Process::ProcessEventData::GetEventDataFromEvent(const Event *event_ptr) { |
| 4287 | if (event_ptr) { |
| 4288 | const EventData *event_data = event_ptr->GetData(); |
| 4289 | if (event_data && |
| 4290 | event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 4291 | return static_cast<const ProcessEventData *>(event_ptr->GetData()); |
| 4292 | } |
| 4293 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
| 4296 | ProcessSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4297 | Process::ProcessEventData::GetProcessFromEvent(const Event *event_ptr) { |
| 4298 | ProcessSP process_sp; |
| 4299 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4300 | if (data) |
| 4301 | process_sp = data->GetProcessSP(); |
| 4302 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4305 | StateType Process::ProcessEventData::GetStateFromEvent(const Event *event_ptr) { |
| 4306 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4307 | if (data == nullptr) |
| 4308 | return eStateInvalid; |
| 4309 | else |
| 4310 | return data->GetState(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4313 | bool Process::ProcessEventData::GetRestartedFromEvent(const Event *event_ptr) { |
| 4314 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4315 | if (data == nullptr) |
| 4316 | return false; |
| 4317 | else |
| 4318 | return data->GetRestarted(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4321 | void Process::ProcessEventData::SetRestartedInEvent(Event *event_ptr, |
| 4322 | bool new_value) { |
| 4323 | ProcessEventData *data = |
| 4324 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4325 | if (data != nullptr) |
| 4326 | data->SetRestarted(new_value); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4329 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4330 | Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr) { |
| 4331 | ProcessEventData *data = |
| 4332 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4333 | if (data != nullptr) |
| 4334 | return data->GetNumRestartedReasons(); |
| 4335 | else |
| 4336 | return 0; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | const char * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4340 | Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, |
| 4341 | size_t idx) { |
| 4342 | ProcessEventData *data = |
| 4343 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4344 | if (data != nullptr) |
| 4345 | return data->GetRestartedReasonAtIndex(idx); |
| 4346 | else |
| 4347 | return nullptr; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4350 | void Process::ProcessEventData::AddRestartedReason(Event *event_ptr, |
| 4351 | const char *reason) { |
| 4352 | ProcessEventData *data = |
| 4353 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4354 | if (data != nullptr) |
| 4355 | data->AddRestartedReason(reason); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4358 | bool Process::ProcessEventData::GetInterruptedFromEvent( |
| 4359 | const Event *event_ptr) { |
| 4360 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4361 | if (data == nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4362 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4363 | else |
| 4364 | return data->GetInterrupted(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4367 | void Process::ProcessEventData::SetInterruptedInEvent(Event *event_ptr, |
| 4368 | bool new_value) { |
| 4369 | ProcessEventData *data = |
| 4370 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4371 | if (data != nullptr) |
| 4372 | data->SetInterrupted(new_value); |
Greg Clayton | d9e416c | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4375 | bool Process::ProcessEventData::SetUpdateStateOnRemoval(Event *event_ptr) { |
| 4376 | ProcessEventData *data = |
| 4377 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4378 | if (data) { |
| 4379 | data->SetUpdateStateOnRemoval(); |
| 4380 | return true; |
| 4381 | } |
| 4382 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
Jim Ingham | b87b9e6 | 2018-06-26 23:38:58 +0000 | [diff] [blame] | 4385 | lldb::TargetSP Process::CalculateTarget() { return m_target_wp.lock(); } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4386 | |
| 4387 | void Process::CalculateExecutionContext(ExecutionContext &exe_ctx) { |
| 4388 | exe_ctx.SetTargetPtr(&GetTarget()); |
| 4389 | exe_ctx.SetProcessPtr(this); |
| 4390 | exe_ctx.SetThreadPtr(nullptr); |
| 4391 | exe_ctx.SetFramePtr(nullptr); |
| 4392 | } |
| 4393 | |
| 4394 | // uint32_t |
| 4395 | // Process::ListProcessesMatchingName (const char *name, StringList &matches, |
| 4396 | // std::vector<lldb::pid_t> &pids) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4397 | //{ |
| 4398 | // return 0; |
| 4399 | //} |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4400 | // |
| 4401 | // ArchSpec |
| 4402 | // Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4403 | //{ |
| 4404 | // return Host::GetArchSpecForExistingProcess (pid); |
| 4405 | //} |
| 4406 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4407 | // ArchSpec |
| 4408 | // Process::GetArchSpecForExistingProcess (const char *process_name) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4409 | //{ |
| 4410 | // return Host::GetArchSpecForExistingProcess (process_name); |
| 4411 | //} |
Eugene Zelenko | da8cf8a | 2016-03-01 00:55:51 +0000 | [diff] [blame] | 4412 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4413 | void Process::AppendSTDOUT(const char *s, size_t len) { |
| 4414 | std::lock_guard<std::recursive_mutex> guard(m_stdio_communication_mutex); |
| 4415 | m_stdout_data.append(s, len); |
| 4416 | BroadcastEventIfUnique(eBroadcastBitSTDOUT, |
| 4417 | new ProcessEventData(shared_from_this(), GetState())); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4420 | void Process::AppendSTDERR(const char *s, size_t len) { |
| 4421 | std::lock_guard<std::recursive_mutex> guard(m_stdio_communication_mutex); |
| 4422 | m_stderr_data.append(s, len); |
| 4423 | BroadcastEventIfUnique(eBroadcastBitSTDERR, |
| 4424 | new ProcessEventData(shared_from_this(), GetState())); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4427 | void Process::BroadcastAsyncProfileData(const std::string &one_profile_data) { |
| 4428 | std::lock_guard<std::recursive_mutex> guard(m_profile_data_comm_mutex); |
| 4429 | m_profile_data.push_back(one_profile_data); |
| 4430 | BroadcastEventIfUnique(eBroadcastBitProfileData, |
| 4431 | new ProcessEventData(shared_from_this(), GetState())); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4434 | void Process::BroadcastStructuredData(const StructuredData::ObjectSP &object_sp, |
| 4435 | const StructuredDataPluginSP &plugin_sp) { |
| 4436 | BroadcastEvent( |
| 4437 | eBroadcastBitStructuredData, |
| 4438 | new EventDataStructuredData(shared_from_this(), object_sp, plugin_sp)); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | StructuredDataPluginSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4442 | Process::GetStructuredDataPlugin(const ConstString &type_name) const { |
| 4443 | auto find_it = m_structured_data_plugin_map.find(type_name); |
| 4444 | if (find_it != m_structured_data_plugin_map.end()) |
| 4445 | return find_it->second; |
| 4446 | else |
| 4447 | return StructuredDataPluginSP(); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4450 | size_t Process::GetAsyncProfileData(char *buf, size_t buf_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4451 | std::lock_guard<std::recursive_mutex> guard(m_profile_data_comm_mutex); |
| 4452 | if (m_profile_data.empty()) |
| 4453 | return 0; |
| 4454 | |
| 4455 | std::string &one_profile_data = m_profile_data.front(); |
| 4456 | size_t bytes_available = one_profile_data.size(); |
| 4457 | if (bytes_available > 0) { |
| 4458 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 4459 | if (log) |
| 4460 | log->Printf("Process::GetProfileData (buf = %p, size = %" PRIu64 ")", |
| 4461 | static_cast<void *>(buf), static_cast<uint64_t>(buf_size)); |
| 4462 | if (bytes_available > buf_size) { |
| 4463 | memcpy(buf, one_profile_data.c_str(), buf_size); |
| 4464 | one_profile_data.erase(0, buf_size); |
| 4465 | bytes_available = buf_size; |
| 4466 | } else { |
| 4467 | memcpy(buf, one_profile_data.c_str(), bytes_available); |
| 4468 | m_profile_data.erase(m_profile_data.begin()); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4469 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4470 | } |
| 4471 | return bytes_available; |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4474 | //------------------------------------------------------------------ |
| 4475 | // Process STDIO |
| 4476 | //------------------------------------------------------------------ |
| 4477 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4478 | size_t Process::GetSTDOUT(char *buf, size_t buf_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4479 | std::lock_guard<std::recursive_mutex> guard(m_stdio_communication_mutex); |
| 4480 | size_t bytes_available = m_stdout_data.size(); |
| 4481 | if (bytes_available > 0) { |
| 4482 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 4483 | if (log) |
| 4484 | log->Printf("Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")", |
| 4485 | static_cast<void *>(buf), static_cast<uint64_t>(buf_size)); |
| 4486 | if (bytes_available > buf_size) { |
| 4487 | memcpy(buf, m_stdout_data.c_str(), buf_size); |
| 4488 | m_stdout_data.erase(0, buf_size); |
| 4489 | bytes_available = buf_size; |
| 4490 | } else { |
| 4491 | memcpy(buf, m_stdout_data.c_str(), bytes_available); |
| 4492 | m_stdout_data.clear(); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4493 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4494 | } |
| 4495 | return bytes_available; |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4498 | size_t Process::GetSTDERR(char *buf, size_t buf_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4499 | std::lock_guard<std::recursive_mutex> gaurd(m_stdio_communication_mutex); |
| 4500 | size_t bytes_available = m_stderr_data.size(); |
| 4501 | if (bytes_available > 0) { |
| 4502 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 4503 | if (log) |
| 4504 | log->Printf("Process::GetSTDERR (buf = %p, size = %" PRIu64 ")", |
| 4505 | static_cast<void *>(buf), static_cast<uint64_t>(buf_size)); |
| 4506 | if (bytes_available > buf_size) { |
| 4507 | memcpy(buf, m_stderr_data.c_str(), buf_size); |
| 4508 | m_stderr_data.erase(0, buf_size); |
| 4509 | bytes_available = buf_size; |
| 4510 | } else { |
| 4511 | memcpy(buf, m_stderr_data.c_str(), bytes_available); |
| 4512 | m_stderr_data.clear(); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4513 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4514 | } |
| 4515 | return bytes_available; |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4518 | void Process::STDIOReadThreadBytesReceived(void *baton, const void *src, |
| 4519 | size_t src_len) { |
| 4520 | Process *process = (Process *)baton; |
| 4521 | process->AppendSTDOUT(static_cast<const char *>(src), src_len); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4524 | class IOHandlerProcessSTDIO : public IOHandler { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4525 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4526 | IOHandlerProcessSTDIO(Process *process, int write_fd) |
| 4527 | : IOHandler(process->GetTarget().GetDebugger(), |
| 4528 | IOHandler::Type::ProcessIO), |
Sam McCall | 6f43d9d | 2016-11-15 10:58:16 +0000 | [diff] [blame] | 4529 | m_process(process), m_write_file(write_fd, false) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4530 | m_pipe.CreateNew(false); |
| 4531 | m_read_file.SetDescriptor(GetInputFD(), false); |
| 4532 | } |
| 4533 | |
| 4534 | ~IOHandlerProcessSTDIO() override = default; |
| 4535 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4536 | // Each IOHandler gets to run until it is done. It should read data from the |
| 4537 | // "in" and place output into "out" and "err and return when done. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4538 | void Run() override { |
| 4539 | if (!m_read_file.IsValid() || !m_write_file.IsValid() || |
| 4540 | !m_pipe.CanRead() || !m_pipe.CanWrite()) { |
| 4541 | SetIsDone(true); |
| 4542 | return; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4545 | SetIsDone(false); |
| 4546 | const int read_fd = m_read_file.GetDescriptor(); |
| 4547 | TerminalState terminal_state; |
| 4548 | terminal_state.Save(read_fd, false); |
| 4549 | Terminal terminal(read_fd); |
| 4550 | terminal.SetCanonical(false); |
| 4551 | terminal.SetEcho(false); |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4552 | // FD_ZERO, FD_SET are not supported on windows |
Hafiz Abid Qadeer | 6eff101 | 2014-03-12 10:45:23 +0000 | [diff] [blame] | 4553 | #ifndef _WIN32 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4554 | const int pipe_read_fd = m_pipe.GetReadFileDescriptor(); |
| 4555 | m_is_running = true; |
| 4556 | while (!GetIsDone()) { |
| 4557 | SelectHelper select_helper; |
| 4558 | select_helper.FDSetRead(read_fd); |
| 4559 | select_helper.FDSetRead(pipe_read_fd); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4560 | Status error = select_helper.Select(); |
Greg Clayton | 860582f | 2016-02-26 17:36:44 +0000 | [diff] [blame] | 4561 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4562 | if (error.Fail()) { |
Greg Clayton | 860582f | 2016-02-26 17:36:44 +0000 | [diff] [blame] | 4563 | SetIsDone(true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4564 | } else { |
| 4565 | char ch = 0; |
| 4566 | size_t n; |
| 4567 | if (select_helper.FDIsSetRead(read_fd)) { |
| 4568 | n = 1; |
| 4569 | if (m_read_file.Read(&ch, n).Success() && n == 1) { |
| 4570 | if (m_write_file.Write(&ch, n).Fail() || n != 1) |
| 4571 | SetIsDone(true); |
| 4572 | } else |
| 4573 | SetIsDone(true); |
Greg Clayton | 860582f | 2016-02-26 17:36:44 +0000 | [diff] [blame] | 4574 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4575 | if (select_helper.FDIsSetRead(pipe_read_fd)) { |
| 4576 | size_t bytes_read; |
| 4577 | // Consume the interrupt byte |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4578 | Status error = m_pipe.Read(&ch, 1, bytes_read); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4579 | if (error.Success()) { |
| 4580 | switch (ch) { |
| 4581 | case 'q': |
| 4582 | SetIsDone(true); |
| 4583 | break; |
| 4584 | case 'i': |
| 4585 | if (StateIsRunningState(m_process->GetState())) |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 4586 | m_process->SendAsyncInterrupt(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4587 | break; |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 4588 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4589 | } |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 4590 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4591 | } |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4592 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4593 | m_is_running = false; |
| 4594 | #endif |
| 4595 | terminal_state.Restore(); |
| 4596 | } |
| 4597 | |
| 4598 | void Cancel() override { |
| 4599 | SetIsDone(true); |
| 4600 | // Only write to our pipe to cancel if we are in |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4601 | // IOHandlerProcessSTDIO::Run(). We can end up with a python command that |
| 4602 | // is being run from the command interpreter: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4603 | // |
| 4604 | // (lldb) step_process_thousands_of_times |
| 4605 | // |
| 4606 | // In this case the command interpreter will be in the middle of handling |
| 4607 | // the command and if the process pushes and pops the IOHandler thousands |
| 4608 | // of times, we can end up writing to m_pipe without ever consuming the |
| 4609 | // bytes from the pipe in IOHandlerProcessSTDIO::Run() and end up |
| 4610 | // deadlocking when the pipe gets fed up and blocks until data is consumed. |
| 4611 | if (m_is_running) { |
| 4612 | char ch = 'q'; // Send 'q' for quit |
| 4613 | size_t bytes_written = 0; |
| 4614 | m_pipe.Write(&ch, 1, bytes_written); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4615 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4616 | } |
| 4617 | |
| 4618 | bool Interrupt() override { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4619 | // Do only things that are safe to do in an interrupt context (like in a |
| 4620 | // SIGINT handler), like write 1 byte to a file descriptor. This will |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4621 | // interrupt the IOHandlerProcessSTDIO::Run() and we can look at the byte |
| 4622 | // that was written to the pipe and then call |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4623 | // m_process->SendAsyncInterrupt() from a much safer location in code. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4624 | if (m_active) { |
| 4625 | char ch = 'i'; // Send 'i' for interrupt |
| 4626 | size_t bytes_written = 0; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4627 | Status result = m_pipe.Write(&ch, 1, bytes_written); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4628 | return result.Success(); |
| 4629 | } else { |
| 4630 | // This IOHandler might be pushed on the stack, but not being run |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4631 | // currently so do the right thing if we aren't actively watching for |
| 4632 | // STDIN by sending the interrupt to the process. Otherwise the write to |
| 4633 | // the pipe above would do nothing. This can happen when the command |
| 4634 | // interpreter is running and gets a "expression ...". It will be on the |
| 4635 | // IOHandler thread and sending the input is complete to the delegate |
| 4636 | // which will cause the expression to run, which will push the process IO |
| 4637 | // handler, but not run it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4638 | |
| 4639 | if (StateIsRunningState(m_process->GetState())) { |
| 4640 | m_process->SendAsyncInterrupt(); |
| 4641 | return true; |
| 4642 | } |
| 4643 | } |
| 4644 | return false; |
| 4645 | } |
| 4646 | |
| 4647 | void GotEOF() override {} |
| 4648 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4649 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4650 | Process *m_process; |
| 4651 | File m_read_file; // Read from this file (usually actual STDIN for LLDB |
| 4652 | File m_write_file; // Write to this file (usually the master pty for getting |
| 4653 | // io to debuggee) |
| 4654 | Pipe m_pipe; |
Sam McCall | 6f43d9d | 2016-11-15 10:58:16 +0000 | [diff] [blame] | 4655 | std::atomic<bool> m_is_running{false}; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4656 | }; |
| 4657 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4658 | void Process::SetSTDIOFileDescriptor(int fd) { |
| 4659 | // First set up the Read Thread for reading/handling process I/O |
| 4660 | |
| 4661 | std::unique_ptr<ConnectionFileDescriptor> conn_ap( |
| 4662 | new ConnectionFileDescriptor(fd, true)); |
| 4663 | |
| 4664 | if (conn_ap) { |
| 4665 | m_stdio_communication.SetConnection(conn_ap.release()); |
| 4666 | if (m_stdio_communication.IsConnected()) { |
| 4667 | m_stdio_communication.SetReadThreadBytesReceivedCallback( |
| 4668 | STDIOReadThreadBytesReceived, this); |
| 4669 | m_stdio_communication.StartReadThread(); |
| 4670 | |
| 4671 | // Now read thread is set up, set up input reader. |
| 4672 | |
| 4673 | if (!m_process_input_reader) |
| 4674 | m_process_input_reader.reset(new IOHandlerProcessSTDIO(this, fd)); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4675 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4676 | } |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4677 | } |
| 4678 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4679 | bool Process::ProcessIOHandlerIsActive() { |
| 4680 | IOHandlerSP io_handler_sp(m_process_input_reader); |
| 4681 | if (io_handler_sp) |
| 4682 | return GetTarget().GetDebugger().IsTopIOHandler(io_handler_sp); |
| 4683 | return false; |
Greg Clayton | 6fea17e | 2014-03-03 19:15:20 +0000 | [diff] [blame] | 4684 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4685 | bool Process::PushProcessIOHandler() { |
| 4686 | IOHandlerSP io_handler_sp(m_process_input_reader); |
| 4687 | if (io_handler_sp) { |
| 4688 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 4689 | if (log) |
| 4690 | log->Printf("Process::%s pushing IO handler", __FUNCTION__); |
Pavel Labath | 4446487 | 2015-05-27 12:40:32 +0000 | [diff] [blame] | 4691 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4692 | io_handler_sp->SetIsDone(false); |
Raphael Isemann | c01783a | 2018-08-29 22:50:54 +0000 | [diff] [blame] | 4693 | // If we evaluate an utility function, then we don't cancel the current |
| 4694 | // IOHandler. Our IOHandler is non-interactive and shouldn't disturb the |
| 4695 | // existing IOHandler that potentially provides the user interface (e.g. |
| 4696 | // the IOHandler for Editline). |
| 4697 | bool cancel_top_handler = !m_mod_id.IsRunningUtilityFunction(); |
| 4698 | GetTarget().GetDebugger().PushIOHandler(io_handler_sp, cancel_top_handler); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4699 | return true; |
| 4700 | } |
| 4701 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4704 | bool Process::PopProcessIOHandler() { |
| 4705 | IOHandlerSP io_handler_sp(m_process_input_reader); |
| 4706 | if (io_handler_sp) |
| 4707 | return GetTarget().GetDebugger().PopIOHandler(io_handler_sp); |
| 4708 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 4711 | // The process needs to know about installed plug-ins |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4712 | void Process::SettingsInitialize() { Thread::SettingsInitialize(); } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4713 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4714 | void Process::SettingsTerminate() { Thread::SettingsTerminate(); } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4715 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4716 | namespace { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4717 | // RestorePlanState is used to record the "is private", "is master" and "okay |
| 4718 | // to discard" fields of the plan we are running, and reset it on Clean or on |
| 4719 | // destruction. It will only reset the state once, so you can call Clean and |
| 4720 | // then monkey with the state and it won't get reset on you again. |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 4721 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4722 | class RestorePlanState { |
| 4723 | public: |
| 4724 | RestorePlanState(lldb::ThreadPlanSP thread_plan_sp) |
| 4725 | : m_thread_plan_sp(thread_plan_sp), m_already_reset(false) { |
| 4726 | if (m_thread_plan_sp) { |
| 4727 | m_private = m_thread_plan_sp->GetPrivate(); |
| 4728 | m_is_master = m_thread_plan_sp->IsMasterPlan(); |
| 4729 | m_okay_to_discard = m_thread_plan_sp->OkayToDiscard(); |
| 4730 | } |
| 4731 | } |
| 4732 | |
| 4733 | ~RestorePlanState() { Clean(); } |
| 4734 | |
| 4735 | void Clean() { |
| 4736 | if (!m_already_reset && m_thread_plan_sp) { |
| 4737 | m_already_reset = true; |
| 4738 | m_thread_plan_sp->SetPrivate(m_private); |
| 4739 | m_thread_plan_sp->SetIsMasterPlan(m_is_master); |
| 4740 | m_thread_plan_sp->SetOkayToDiscard(m_okay_to_discard); |
| 4741 | } |
| 4742 | } |
| 4743 | |
| 4744 | private: |
| 4745 | lldb::ThreadPlanSP m_thread_plan_sp; |
| 4746 | bool m_already_reset; |
| 4747 | bool m_private; |
| 4748 | bool m_is_master; |
| 4749 | bool m_okay_to_discard; |
| 4750 | }; |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 4751 | } // anonymous namespace |
Jim Ingham | c60963c | 2015-10-12 19:11:03 +0000 | [diff] [blame] | 4752 | |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4753 | static microseconds |
| 4754 | GetOneThreadExpressionTimeout(const EvaluateExpressionOptions &options) { |
| 4755 | const milliseconds default_one_thread_timeout(250); |
| 4756 | |
| 4757 | // If the overall wait is forever, then we don't need to worry about it. |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4758 | if (!options.GetTimeout()) { |
| 4759 | return options.GetOneThreadTimeout() ? *options.GetOneThreadTimeout() |
| 4760 | : default_one_thread_timeout; |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | // If the one thread timeout is set, use it. |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4764 | if (options.GetOneThreadTimeout()) |
| 4765 | return *options.GetOneThreadTimeout(); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4766 | |
| 4767 | // Otherwise use half the total timeout, bounded by the |
| 4768 | // default_one_thread_timeout. |
| 4769 | return std::min<microseconds>(default_one_thread_timeout, |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4770 | *options.GetTimeout() / 2); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | static Timeout<std::micro> |
| 4774 | GetExpressionTimeout(const EvaluateExpressionOptions &options, |
| 4775 | bool before_first_timeout) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4776 | // If we are going to run all threads the whole time, or if we are only going |
| 4777 | // to run one thread, we can just return the overall timeout. |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4778 | if (!options.GetStopOthers() || !options.GetTryAllThreads()) |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4779 | return options.GetTimeout(); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4780 | |
| 4781 | if (before_first_timeout) |
| 4782 | return GetOneThreadExpressionTimeout(options); |
| 4783 | |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4784 | if (!options.GetTimeout()) |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4785 | return llvm::None; |
| 4786 | else |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4787 | return *options.GetTimeout() - GetOneThreadExpressionTimeout(options); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 4790 | static llvm::Optional<ExpressionResults> |
| 4791 | HandleStoppedEvent(Thread &thread, const ThreadPlanSP &thread_plan_sp, |
| 4792 | RestorePlanState &restorer, const EventSP &event_sp, |
| 4793 | EventSP &event_to_broadcast_sp, |
| 4794 | const EvaluateExpressionOptions &options, bool handle_interrupts) { |
| 4795 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS); |
| 4796 | |
| 4797 | ThreadPlanSP plan = thread.GetCompletedPlan(); |
| 4798 | if (plan == thread_plan_sp && plan->PlanSucceeded()) { |
| 4799 | LLDB_LOG(log, "execution completed successfully"); |
| 4800 | |
| 4801 | // Restore the plan state so it will get reported as intended when we are |
| 4802 | // done. |
| 4803 | restorer.Clean(); |
| 4804 | return eExpressionCompleted; |
| 4805 | } |
| 4806 | |
| 4807 | StopInfoSP stop_info_sp = thread.GetStopInfo(); |
| 4808 | if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint && |
| 4809 | stop_info_sp->ShouldNotify(event_sp.get())) { |
| 4810 | LLDB_LOG(log, "stopped for breakpoint: {0}.", stop_info_sp->GetDescription()); |
| 4811 | if (!options.DoesIgnoreBreakpoints()) { |
| 4812 | // Restore the plan state and then force Private to false. We are going |
| 4813 | // to stop because of this plan so we need it to become a public plan or |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4814 | // it won't report correctly when we continue to its termination later |
| 4815 | // on. |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 4816 | restorer.Clean(); |
| 4817 | thread_plan_sp->SetPrivate(false); |
| 4818 | event_to_broadcast_sp = event_sp; |
| 4819 | } |
| 4820 | return eExpressionHitBreakpoint; |
| 4821 | } |
| 4822 | |
| 4823 | if (!handle_interrupts && |
| 4824 | Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get())) |
| 4825 | return llvm::None; |
| 4826 | |
| 4827 | LLDB_LOG(log, "thread plan did not successfully complete"); |
| 4828 | if (!options.DoesUnwindOnError()) |
| 4829 | event_to_broadcast_sp = event_sp; |
| 4830 | return eExpressionInterrupted; |
| 4831 | } |
| 4832 | |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 4833 | ExpressionResults |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4834 | Process::RunThreadPlan(ExecutionContext &exe_ctx, |
| 4835 | lldb::ThreadPlanSP &thread_plan_sp, |
| 4836 | const EvaluateExpressionOptions &options, |
| 4837 | DiagnosticManager &diagnostic_manager) { |
| 4838 | ExpressionResults return_value = eExpressionSetupError; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4839 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4840 | std::lock_guard<std::mutex> run_thread_plan_locker(m_run_thread_plan_lock); |
| 4841 | |
| 4842 | if (!thread_plan_sp) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4843 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4844 | eDiagnosticSeverityError, |
| 4845 | "RunThreadPlan called with empty thread plan."); |
| 4846 | return eExpressionSetupError; |
| 4847 | } |
| 4848 | |
| 4849 | if (!thread_plan_sp->ValidatePlan(nullptr)) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4850 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4851 | eDiagnosticSeverityError, |
| 4852 | "RunThreadPlan called with an invalid thread plan."); |
| 4853 | return eExpressionSetupError; |
| 4854 | } |
| 4855 | |
| 4856 | if (exe_ctx.GetProcessPtr() != this) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4857 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 4858 | "RunThreadPlan called on wrong process."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4859 | return eExpressionSetupError; |
| 4860 | } |
| 4861 | |
| 4862 | Thread *thread = exe_ctx.GetThreadPtr(); |
| 4863 | if (thread == nullptr) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4864 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 4865 | "RunThreadPlan called with invalid thread."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4866 | return eExpressionSetupError; |
| 4867 | } |
| 4868 | |
| 4869 | // We need to change some of the thread plan attributes for the thread plan |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4870 | // runner. This will restore them when we are done: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4871 | |
| 4872 | RestorePlanState thread_plan_restorer(thread_plan_sp); |
| 4873 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4874 | // We rely on the thread plan we are running returning "PlanCompleted" if |
| 4875 | // when it successfully completes. For that to be true the plan can't be |
| 4876 | // private - since private plans suppress themselves in the GetCompletedPlan |
| 4877 | // call. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4878 | |
| 4879 | thread_plan_sp->SetPrivate(false); |
| 4880 | |
| 4881 | // The plans run with RunThreadPlan also need to be terminal master plans or |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4882 | // when they are done we will end up asking the plan above us whether we |
| 4883 | // should stop, which may give the wrong answer. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4884 | |
| 4885 | thread_plan_sp->SetIsMasterPlan(true); |
| 4886 | thread_plan_sp->SetOkayToDiscard(false); |
| 4887 | |
Raphael Isemann | c01783a | 2018-08-29 22:50:54 +0000 | [diff] [blame] | 4888 | // If we are running some utility expression for LLDB, we now have to mark |
| 4889 | // this in the ProcesModID of this process. This RAII takes care of marking |
| 4890 | // and reverting the mark it once we are done running the expression. |
| 4891 | UtilityFunctionScope util_scope(options.IsForUtilityExpr() ? this : nullptr); |
| 4892 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4893 | if (m_private_state.GetValue() != eStateStopped) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4894 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4895 | eDiagnosticSeverityError, |
| 4896 | "RunThreadPlan called while the private state was not stopped."); |
| 4897 | return eExpressionSetupError; |
| 4898 | } |
| 4899 | |
| 4900 | // Save the thread & frame from the exe_ctx for restoration after we run |
| 4901 | const uint32_t thread_idx_id = thread->GetIndexID(); |
| 4902 | StackFrameSP selected_frame_sp = thread->GetSelectedFrame(); |
| 4903 | if (!selected_frame_sp) { |
| 4904 | thread->SetSelectedFrame(nullptr); |
| 4905 | selected_frame_sp = thread->GetSelectedFrame(); |
| 4906 | if (!selected_frame_sp) { |
| 4907 | diagnostic_manager.Printf( |
| 4908 | eDiagnosticSeverityError, |
| 4909 | "RunThreadPlan called without a selected frame on thread %d", |
| 4910 | thread_idx_id); |
| 4911 | return eExpressionSetupError; |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 4912 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4913 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4914 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4915 | // Make sure the timeout values make sense. The one thread timeout needs to |
| 4916 | // be smaller than the overall timeout. |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4917 | if (options.GetOneThreadTimeout() && options.GetTimeout() && |
| 4918 | *options.GetTimeout() < *options.GetOneThreadTimeout()) { |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4919 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 4920 | "RunThreadPlan called with one thread " |
| 4921 | "timeout greater than total timeout"); |
| 4922 | return eExpressionSetupError; |
| 4923 | } |
| 4924 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4925 | StackID ctx_frame_id = selected_frame_sp->GetStackID(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4926 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4927 | // N.B. Running the target may unset the currently selected thread and frame. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4928 | // We don't want to do that either, so we should arrange to reset them as |
| 4929 | // well. |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4930 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4931 | lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4932 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4933 | uint32_t selected_tid; |
| 4934 | StackID selected_stack_id; |
| 4935 | if (selected_thread_sp) { |
| 4936 | selected_tid = selected_thread_sp->GetIndexID(); |
| 4937 | selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID(); |
| 4938 | } else { |
| 4939 | selected_tid = LLDB_INVALID_THREAD_ID; |
| 4940 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4941 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4942 | HostThread backup_private_state_thread; |
| 4943 | lldb::StateType old_state = eStateInvalid; |
| 4944 | lldb::ThreadPlanSP stopper_base_plan_sp; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4945 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4946 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | |
| 4947 | LIBLLDB_LOG_PROCESS)); |
| 4948 | if (m_private_state_thread.EqualsThread(Host::GetCurrentThread())) { |
| 4949 | // Yikes, we are running on the private state thread! So we can't wait for |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4950 | // public events on this thread, since we are the thread that is generating |
| 4951 | // public events. The simplest thing to do is to spin up a temporary thread |
| 4952 | // to handle private state thread events while we are fielding public |
| 4953 | // events here. |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 4954 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4955 | log->Printf("Running thread plan on private state thread, spinning up " |
| 4956 | "another state thread to handle the events."); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 4957 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4958 | backup_private_state_thread = m_private_state_thread; |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 4959 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4960 | // One other bit of business: we want to run just this thread plan and |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4961 | // anything it pushes, and then stop, returning control here. But in the |
| 4962 | // normal course of things, the plan above us on the stack would be given a |
| 4963 | // shot at the stop event before deciding to stop, and we don't want that. |
| 4964 | // So we insert a "stopper" base plan on the stack before the plan we want |
| 4965 | // to run. Since base plans always stop and return control to the user, |
| 4966 | // that will do just what we want. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4967 | stopper_base_plan_sp.reset(new ThreadPlanBase(*thread)); |
| 4968 | thread->QueueThreadPlan(stopper_base_plan_sp, false); |
| 4969 | // Have to make sure our public state is stopped, since otherwise the |
| 4970 | // reporting logic below doesn't work correctly. |
| 4971 | old_state = m_public_state.GetValue(); |
| 4972 | m_public_state.SetValueNoLock(eStateStopped); |
| 4973 | |
| 4974 | // Now spin up the private state thread: |
| 4975 | StartPrivateStateThread(true); |
| 4976 | } |
| 4977 | |
| 4978 | thread->QueueThreadPlan( |
| 4979 | thread_plan_sp, false); // This used to pass "true" does that make sense? |
| 4980 | |
| 4981 | if (options.GetDebug()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4982 | // In this case, we aren't actually going to run, we just want to stop |
| 4983 | // right away. Flush this thread so we will refetch the stacks and show the |
| 4984 | // correct backtrace. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4985 | // FIXME: To make this prettier we should invent some stop reason for this, |
| 4986 | // but that |
| 4987 | // is only cosmetic, and this functionality is only of use to lldb |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4988 | // developers who can live with not pretty... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4989 | thread->Flush(); |
| 4990 | return eExpressionStoppedForDebug; |
| 4991 | } |
| 4992 | |
| 4993 | ListenerSP listener_sp( |
| 4994 | Listener::MakeListener("lldb.process.listener.run-thread-plan")); |
| 4995 | |
| 4996 | lldb::EventSP event_to_broadcast_sp; |
| 4997 | |
| 4998 | { |
| 4999 | // This process event hijacker Hijacks the Public events and its destructor |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5000 | // makes sure that the process events get restored on exit to the function. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5001 | // |
| 5002 | // If the event needs to propagate beyond the hijacker (e.g., the process |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5003 | // exits during execution), then the event is put into |
| 5004 | // event_to_broadcast_sp for rebroadcasting. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5005 | |
| 5006 | ProcessEventHijacker run_thread_plan_hijacker(*this, listener_sp); |
| 5007 | |
| 5008 | if (log) { |
| 5009 | StreamString s; |
| 5010 | thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
| 5011 | log->Printf("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4" PRIx64 |
| 5012 | " to run thread plan \"%s\".", |
| 5013 | thread->GetIndexID(), thread->GetID(), s.GetData()); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 5014 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5015 | |
| 5016 | bool got_event; |
| 5017 | lldb::EventSP event_sp; |
| 5018 | lldb::StateType stop_state = lldb::eStateInvalid; |
| 5019 | |
| 5020 | bool before_first_timeout = true; // This is set to false the first time |
| 5021 | // that we have to halt the target. |
| 5022 | bool do_resume = true; |
| 5023 | bool handle_running_event = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5024 | |
| 5025 | // This is just for accounting: |
| 5026 | uint32_t num_resumes = 0; |
| 5027 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5028 | // If we are going to run all threads the whole time, or if we are only |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 5029 | // going to run one thread, then we don't need the first timeout. So we |
| 5030 | // pretend we are after the first timeout already. |
| 5031 | if (!options.GetStopOthers() || !options.GetTryAllThreads()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5032 | before_first_timeout = false; |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 5033 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5034 | if (log) |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 5035 | log->Printf("Stop others: %u, try all: %u, before_first: %u.\n", |
| 5036 | options.GetStopOthers(), options.GetTryAllThreads(), |
| 5037 | before_first_timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5038 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5039 | // This isn't going to work if there are unfetched events on the queue. Are |
| 5040 | // there cases where we might want to run the remaining events here, and |
| 5041 | // then try to call the function? That's probably being too tricky for our |
| 5042 | // own good. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5043 | |
| 5044 | Event *other_events = listener_sp->PeekAtNextEvent(); |
| 5045 | if (other_events != nullptr) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 5046 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5047 | eDiagnosticSeverityError, |
| 5048 | "RunThreadPlan called with pending events on the queue."); |
| 5049 | return eExpressionSetupError; |
Kuba Brecka | afdf842 | 2014-10-10 23:43:03 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5052 | // We also need to make sure that the next event is delivered. We might be |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5053 | // calling a function as part of a thread plan, in which case the last |
| 5054 | // delivered event could be the running event, and we don't want event |
| 5055 | // coalescing to cause us to lose OUR running event... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5056 | ForceNextEventDelivery(); |
Ryan Brown | 65d4d5c | 2015-09-16 21:20:44 +0000 | [diff] [blame] | 5057 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5058 | // This while loop must exit out the bottom, there's cleanup that we need to do |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5059 | // when we are done. So don't call return anywhere within it. |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5060 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5061 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5062 | // It's pretty much impossible to write test cases for things like: One |
| 5063 | // thread timeout expires, I go to halt, but the process already stopped on |
| 5064 | // the function call stop breakpoint. Turning on this define will make us |
| 5065 | // not fetch the first event till after the halt. So if you run a quick |
| 5066 | // function, it will have completed, and the completion event will be |
| 5067 | // waiting, when you interrupt for halt. The expression evaluation should |
| 5068 | // still succeed. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5069 | bool miss_first_event = true; |
| 5070 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5071 | while (true) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5072 | // We usually want to resume the process if we get to the top of the |
| 5073 | // loop. The only exception is if we get two running events with no |
| 5074 | // intervening stop, which can happen, we will just wait for then next |
| 5075 | // stop event. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5076 | if (log) |
| 5077 | log->Printf("Top of while loop: do_resume: %i handle_running_event: %i " |
| 5078 | "before_first_timeout: %i.", |
| 5079 | do_resume, handle_running_event, before_first_timeout); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5080 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5081 | if (do_resume || handle_running_event) { |
| 5082 | // Do the initial resume and wait for the running event before going |
| 5083 | // further. |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5084 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5085 | if (do_resume) { |
| 5086 | num_resumes++; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5087 | Status resume_error = PrivateResume(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5088 | if (!resume_error.Success()) { |
| 5089 | diagnostic_manager.Printf( |
| 5090 | eDiagnosticSeverityError, |
| 5091 | "couldn't resume inferior the %d time: \"%s\".", num_resumes, |
| 5092 | resume_error.AsCString()); |
| 5093 | return_value = eExpressionSetupError; |
| 5094 | break; |
| 5095 | } |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5096 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5097 | |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 5098 | got_event = |
| 5099 | listener_sp->GetEvent(event_sp, std::chrono::milliseconds(500)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5100 | if (!got_event) { |
| 5101 | if (log) |
| 5102 | log->Printf("Process::RunThreadPlan(): didn't get any event after " |
| 5103 | "resume %" PRIu32 ", exiting.", |
| 5104 | num_resumes); |
| 5105 | |
| 5106 | diagnostic_manager.Printf(eDiagnosticSeverityError, |
| 5107 | "didn't get any event after resume %" PRIu32 |
| 5108 | ", exiting.", |
| 5109 | num_resumes); |
| 5110 | return_value = eExpressionSetupError; |
| 5111 | break; |
| 5112 | } |
| 5113 | |
| 5114 | stop_state = |
| 5115 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5116 | |
| 5117 | if (stop_state != eStateRunning) { |
| 5118 | bool restarted = false; |
| 5119 | |
| 5120 | if (stop_state == eStateStopped) { |
| 5121 | restarted = Process::ProcessEventData::GetRestartedFromEvent( |
| 5122 | event_sp.get()); |
| 5123 | if (log) |
| 5124 | log->Printf( |
| 5125 | "Process::RunThreadPlan(): didn't get running event after " |
| 5126 | "resume %d, got %s instead (restarted: %i, do_resume: %i, " |
| 5127 | "handle_running_event: %i).", |
| 5128 | num_resumes, StateAsCString(stop_state), restarted, do_resume, |
| 5129 | handle_running_event); |
| 5130 | } |
| 5131 | |
| 5132 | if (restarted) { |
| 5133 | // This is probably an overabundance of caution, I don't think I |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5134 | // should ever get a stopped & restarted event here. But if I do, |
| 5135 | // the best thing is to Halt and then get out of here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5136 | const bool clear_thread_plans = false; |
| 5137 | const bool use_run_lock = false; |
| 5138 | Halt(clear_thread_plans, use_run_lock); |
| 5139 | } |
| 5140 | |
| 5141 | diagnostic_manager.Printf( |
| 5142 | eDiagnosticSeverityError, |
| 5143 | "didn't get running event after initial resume, got %s instead.", |
| 5144 | StateAsCString(stop_state)); |
| 5145 | return_value = eExpressionSetupError; |
| 5146 | break; |
| 5147 | } |
| 5148 | |
| 5149 | if (log) |
| 5150 | log->PutCString("Process::RunThreadPlan(): resuming succeeded."); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5151 | // We need to call the function synchronously, so spin waiting for it |
| 5152 | // to return. If we get interrupted while executing, we're going to |
| 5153 | // lose our context, and won't be able to gather the result at this |
| 5154 | // point. We set the timeout AFTER the resume, since the resume takes |
| 5155 | // some time and we don't want to charge that to the timeout. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5156 | } else { |
| 5157 | if (log) |
| 5158 | log->PutCString("Process::RunThreadPlan(): waiting for next event."); |
| 5159 | } |
| 5160 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5161 | do_resume = true; |
| 5162 | handle_running_event = true; |
| 5163 | |
| 5164 | // Now wait for the process to stop again: |
| 5165 | event_sp.reset(); |
| 5166 | |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 5167 | Timeout<std::micro> timeout = |
| 5168 | GetExpressionTimeout(options, before_first_timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5169 | if (log) { |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 5170 | if (timeout) { |
| 5171 | auto now = system_clock::now(); |
| 5172 | log->Printf("Process::RunThreadPlan(): about to wait - now is %s - " |
| 5173 | "endpoint is %s", |
| 5174 | llvm::to_string(now).c_str(), |
| 5175 | llvm::to_string(now + *timeout).c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5176 | } else { |
| 5177 | log->Printf("Process::RunThreadPlan(): about to wait forever."); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5178 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
| 5182 | // See comment above... |
| 5183 | if (miss_first_event) { |
| 5184 | usleep(1000); |
| 5185 | miss_first_event = false; |
| 5186 | got_event = false; |
| 5187 | } else |
| 5188 | #endif |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 5189 | got_event = listener_sp->GetEvent(event_sp, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5190 | |
| 5191 | if (got_event) { |
| 5192 | if (event_sp) { |
| 5193 | bool keep_going = false; |
| 5194 | if (event_sp->GetType() == eBroadcastBitInterrupt) { |
| 5195 | const bool clear_thread_plans = false; |
| 5196 | const bool use_run_lock = false; |
| 5197 | Halt(clear_thread_plans, use_run_lock); |
| 5198 | return_value = eExpressionInterrupted; |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 5199 | diagnostic_manager.PutString(eDiagnosticSeverityRemark, |
| 5200 | "execution halted by user interrupt."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5201 | if (log) |
| 5202 | log->Printf("Process::RunThreadPlan(): Got interrupted by " |
| 5203 | "eBroadcastBitInterrupted, exiting."); |
| 5204 | break; |
| 5205 | } else { |
| 5206 | stop_state = |
| 5207 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5208 | if (log) |
| 5209 | log->Printf( |
| 5210 | "Process::RunThreadPlan(): in while loop, got event: %s.", |
| 5211 | StateAsCString(stop_state)); |
| 5212 | |
| 5213 | switch (stop_state) { |
| 5214 | case lldb::eStateStopped: { |
| 5215 | // We stopped, figure out what we are going to do now. |
| 5216 | ThreadSP thread_sp = |
| 5217 | GetThreadList().FindThreadByIndexID(thread_idx_id); |
| 5218 | if (!thread_sp) { |
| 5219 | // Ooh, our thread has vanished. Unlikely that this was |
| 5220 | // successful execution... |
| 5221 | if (log) |
| 5222 | log->Printf("Process::RunThreadPlan(): execution completed " |
| 5223 | "but our thread (index-id=%u) has vanished.", |
| 5224 | thread_idx_id); |
| 5225 | return_value = eExpressionInterrupted; |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5226 | } else if (Process::ProcessEventData::GetRestartedFromEvent( |
| 5227 | event_sp.get())) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5228 | // If we were restarted, we just need to go back up to fetch |
| 5229 | // another event. |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5230 | if (log) { |
| 5231 | log->Printf("Process::RunThreadPlan(): Got a stop and " |
| 5232 | "restart, so we'll continue waiting."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5233 | } |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5234 | keep_going = true; |
| 5235 | do_resume = false; |
| 5236 | handle_running_event = true; |
| 5237 | } else { |
| 5238 | const bool handle_interrupts = true; |
| 5239 | return_value = *HandleStoppedEvent( |
| 5240 | *thread, thread_plan_sp, thread_plan_restorer, event_sp, |
| 5241 | event_to_broadcast_sp, options, handle_interrupts); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5242 | } |
| 5243 | } break; |
| 5244 | |
| 5245 | case lldb::eStateRunning: |
| 5246 | // This shouldn't really happen, but sometimes we do get two |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5247 | // running events without an intervening stop, and in that case |
| 5248 | // we should just go back to waiting for the stop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5249 | do_resume = false; |
| 5250 | keep_going = true; |
| 5251 | handle_running_event = false; |
| 5252 | break; |
| 5253 | |
| 5254 | default: |
| 5255 | if (log) |
| 5256 | log->Printf("Process::RunThreadPlan(): execution stopped with " |
| 5257 | "unexpected state: %s.", |
| 5258 | StateAsCString(stop_state)); |
| 5259 | |
| 5260 | if (stop_state == eStateExited) |
| 5261 | event_to_broadcast_sp = event_sp; |
| 5262 | |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 5263 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5264 | eDiagnosticSeverityError, |
| 5265 | "execution stopped with unexpected state."); |
| 5266 | return_value = eExpressionInterrupted; |
| 5267 | break; |
| 5268 | } |
| 5269 | } |
| 5270 | |
| 5271 | if (keep_going) |
| 5272 | continue; |
| 5273 | else |
| 5274 | break; |
| 5275 | } else { |
| 5276 | if (log) |
| 5277 | log->PutCString("Process::RunThreadPlan(): got_event was true, but " |
| 5278 | "the event pointer was null. How odd..."); |
| 5279 | return_value = eExpressionInterrupted; |
| 5280 | break; |
| 5281 | } |
| 5282 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5283 | // If we didn't get an event that means we've timed out... We will |
| 5284 | // interrupt the process here. Depending on what we were asked to do |
| 5285 | // we will either exit, or try with all threads running for the same |
| 5286 | // timeout. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5287 | |
| 5288 | if (log) { |
| 5289 | if (options.GetTryAllThreads()) { |
| 5290 | if (before_first_timeout) { |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 5291 | LLDB_LOG(log, |
| 5292 | "Running function with one thread timeout timed out."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5293 | } else |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 5294 | LLDB_LOG(log, "Restarting function with all threads enabled and " |
| 5295 | "timeout: {0} timed out, abandoning execution.", |
| 5296 | timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5297 | } else |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 5298 | LLDB_LOG(log, "Running function with timeout: {0} timed out, " |
| 5299 | "abandoning execution.", |
| 5300 | timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5301 | } |
| 5302 | |
| 5303 | // It is possible that between the time we issued the Halt, and we get |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5304 | // around to calling Halt the target could have stopped. That's fine, |
| 5305 | // Halt will figure that out and send the appropriate Stopped event. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5306 | // BUT it is also possible that we stopped & restarted (e.g. hit a |
| 5307 | // signal with "stop" set to false.) In |
| 5308 | // that case, we'll get the stopped & restarted event, and we should go |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5309 | // back to waiting for the Halt's stopped event. That's what this |
| 5310 | // while loop does. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5311 | |
| 5312 | bool back_to_top = true; |
| 5313 | uint32_t try_halt_again = 0; |
| 5314 | bool do_halt = true; |
| 5315 | const uint32_t num_retries = 5; |
| 5316 | while (try_halt_again < num_retries) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5317 | Status halt_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5318 | if (do_halt) { |
| 5319 | if (log) |
| 5320 | log->Printf("Process::RunThreadPlan(): Running Halt."); |
| 5321 | const bool clear_thread_plans = false; |
| 5322 | const bool use_run_lock = false; |
| 5323 | Halt(clear_thread_plans, use_run_lock); |
| 5324 | } |
| 5325 | if (halt_error.Success()) { |
| 5326 | if (log) |
| 5327 | log->PutCString("Process::RunThreadPlan(): Halt succeeded."); |
| 5328 | |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 5329 | got_event = |
| 5330 | listener_sp->GetEvent(event_sp, std::chrono::milliseconds(500)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5331 | |
| 5332 | if (got_event) { |
| 5333 | stop_state = |
| 5334 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5335 | if (log) { |
| 5336 | log->Printf("Process::RunThreadPlan(): Stopped with event: %s", |
| 5337 | StateAsCString(stop_state)); |
| 5338 | if (stop_state == lldb::eStateStopped && |
| 5339 | Process::ProcessEventData::GetInterruptedFromEvent( |
| 5340 | event_sp.get())) |
| 5341 | log->PutCString(" Event was the Halt interruption event."); |
| 5342 | } |
| 5343 | |
| 5344 | if (stop_state == lldb::eStateStopped) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5345 | if (Process::ProcessEventData::GetRestartedFromEvent( |
| 5346 | event_sp.get())) { |
| 5347 | if (log) |
| 5348 | log->PutCString("Process::RunThreadPlan(): Went to halt " |
| 5349 | "but got a restarted event, there must be " |
| 5350 | "an un-restarted stopped event so try " |
| 5351 | "again... " |
| 5352 | "Exiting wait loop."); |
| 5353 | try_halt_again++; |
| 5354 | do_halt = false; |
| 5355 | continue; |
| 5356 | } |
| 5357 | |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5358 | // Between the time we initiated the Halt and the time we |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5359 | // delivered it, the process could have already finished its |
| 5360 | // job. Check that here: |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5361 | const bool handle_interrupts = false; |
| 5362 | if (auto result = HandleStoppedEvent( |
| 5363 | *thread, thread_plan_sp, thread_plan_restorer, event_sp, |
| 5364 | event_to_broadcast_sp, options, handle_interrupts)) { |
| 5365 | return_value = *result; |
| 5366 | back_to_top = false; |
| 5367 | break; |
| 5368 | } |
| 5369 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5370 | if (!options.GetTryAllThreads()) { |
| 5371 | if (log) |
| 5372 | log->PutCString("Process::RunThreadPlan(): try_all_threads " |
| 5373 | "was false, we stopped so now we're " |
| 5374 | "quitting."); |
| 5375 | return_value = eExpressionInterrupted; |
| 5376 | back_to_top = false; |
| 5377 | break; |
| 5378 | } |
| 5379 | |
| 5380 | if (before_first_timeout) { |
| 5381 | // Set all the other threads to run, and return to the top of |
| 5382 | // the loop, which will continue; |
| 5383 | before_first_timeout = false; |
| 5384 | thread_plan_sp->SetStopOthers(false); |
| 5385 | if (log) |
| 5386 | log->PutCString( |
| 5387 | "Process::RunThreadPlan(): about to resume."); |
| 5388 | |
| 5389 | back_to_top = true; |
| 5390 | break; |
| 5391 | } else { |
| 5392 | // Running all threads failed, so return Interrupted. |
| 5393 | if (log) |
| 5394 | log->PutCString("Process::RunThreadPlan(): running all " |
| 5395 | "threads timed out."); |
| 5396 | return_value = eExpressionInterrupted; |
| 5397 | back_to_top = false; |
| 5398 | break; |
| 5399 | } |
| 5400 | } |
| 5401 | } else { |
| 5402 | if (log) |
| 5403 | log->PutCString("Process::RunThreadPlan(): halt said it " |
| 5404 | "succeeded, but I got no event. " |
| 5405 | "I'm getting out of here passing Interrupted."); |
| 5406 | return_value = eExpressionInterrupted; |
| 5407 | back_to_top = false; |
| 5408 | break; |
| 5409 | } |
| 5410 | } else { |
| 5411 | try_halt_again++; |
| 5412 | continue; |
| 5413 | } |
| 5414 | } |
| 5415 | |
| 5416 | if (!back_to_top || try_halt_again > num_retries) |
| 5417 | break; |
| 5418 | else |
| 5419 | continue; |
| 5420 | } |
| 5421 | } // END WAIT LOOP |
| 5422 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5423 | // If we had to start up a temporary private state thread to run this |
| 5424 | // thread plan, shut it down now. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5425 | if (backup_private_state_thread.IsJoinable()) { |
| 5426 | StopPrivateStateThread(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5427 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5428 | m_private_state_thread = backup_private_state_thread; |
| 5429 | if (stopper_base_plan_sp) { |
| 5430 | thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp); |
| 5431 | } |
| 5432 | if (old_state != eStateInvalid) |
| 5433 | m_public_state.SetValueNoLock(old_state); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5434 | } |
| 5435 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5436 | if (return_value != eExpressionCompleted && log) { |
| 5437 | // Print a backtrace into the log so we can figure out where we are: |
| 5438 | StreamString s; |
| 5439 | s.PutCString("Thread state after unsuccessful completion: \n"); |
| 5440 | thread->GetStackFrameStatus(s, 0, UINT32_MAX, true, UINT32_MAX); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 5441 | log->PutString(s.GetString()); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5442 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5443 | // Restore the thread state if we are going to discard the plan execution. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5444 | // There are three cases where this could happen: 1) The execution |
| 5445 | // successfully completed 2) We hit a breakpoint, and ignore_breakpoints |
| 5446 | // was true 3) We got some other error, and discard_on_error was true |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5447 | bool should_unwind = (return_value == eExpressionInterrupted && |
| 5448 | options.DoesUnwindOnError()) || |
| 5449 | (return_value == eExpressionHitBreakpoint && |
| 5450 | options.DoesIgnoreBreakpoints()); |
| 5451 | |
| 5452 | if (return_value == eExpressionCompleted || should_unwind) { |
| 5453 | thread_plan_sp->RestoreThreadState(); |
| 5454 | } |
| 5455 | |
| 5456 | // Now do some processing on the results of the run: |
| 5457 | if (return_value == eExpressionInterrupted || |
| 5458 | return_value == eExpressionHitBreakpoint) { |
| 5459 | if (log) { |
| 5460 | StreamString s; |
| 5461 | if (event_sp) |
| 5462 | event_sp->Dump(&s); |
| 5463 | else { |
| 5464 | log->PutCString("Process::RunThreadPlan(): Stop event that " |
| 5465 | "interrupted us is NULL."); |
| 5466 | } |
| 5467 | |
| 5468 | StreamString ts; |
| 5469 | |
| 5470 | const char *event_explanation = nullptr; |
| 5471 | |
| 5472 | do { |
| 5473 | if (!event_sp) { |
| 5474 | event_explanation = "<no event>"; |
| 5475 | break; |
| 5476 | } else if (event_sp->GetType() == eBroadcastBitInterrupt) { |
| 5477 | event_explanation = "<user interrupt>"; |
| 5478 | break; |
| 5479 | } else { |
| 5480 | const Process::ProcessEventData *event_data = |
| 5481 | Process::ProcessEventData::GetEventDataFromEvent( |
| 5482 | event_sp.get()); |
| 5483 | |
| 5484 | if (!event_data) { |
| 5485 | event_explanation = "<no event data>"; |
| 5486 | break; |
| 5487 | } |
| 5488 | |
| 5489 | Process *process = event_data->GetProcessSP().get(); |
| 5490 | |
| 5491 | if (!process) { |
| 5492 | event_explanation = "<no process>"; |
| 5493 | break; |
| 5494 | } |
| 5495 | |
| 5496 | ThreadList &thread_list = process->GetThreadList(); |
| 5497 | |
| 5498 | uint32_t num_threads = thread_list.GetSize(); |
| 5499 | uint32_t thread_index; |
| 5500 | |
| 5501 | ts.Printf("<%u threads> ", num_threads); |
| 5502 | |
| 5503 | for (thread_index = 0; thread_index < num_threads; ++thread_index) { |
| 5504 | Thread *thread = thread_list.GetThreadAtIndex(thread_index).get(); |
| 5505 | |
| 5506 | if (!thread) { |
| 5507 | ts.Printf("<?> "); |
| 5508 | continue; |
| 5509 | } |
| 5510 | |
| 5511 | ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID()); |
| 5512 | RegisterContext *register_context = |
| 5513 | thread->GetRegisterContext().get(); |
| 5514 | |
| 5515 | if (register_context) |
| 5516 | ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC()); |
| 5517 | else |
| 5518 | ts.Printf("[ip unknown] "); |
| 5519 | |
| 5520 | // Show the private stop info here, the public stop info will be |
| 5521 | // from the last natural stop. |
| 5522 | lldb::StopInfoSP stop_info_sp = thread->GetPrivateStopInfo(); |
| 5523 | if (stop_info_sp) { |
| 5524 | const char *stop_desc = stop_info_sp->GetDescription(); |
| 5525 | if (stop_desc) |
| 5526 | ts.PutCString(stop_desc); |
| 5527 | } |
| 5528 | ts.Printf(">"); |
| 5529 | } |
| 5530 | |
| 5531 | event_explanation = ts.GetData(); |
| 5532 | } |
| 5533 | } while (0); |
| 5534 | |
| 5535 | if (event_explanation) |
| 5536 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", |
| 5537 | s.GetData(), event_explanation); |
| 5538 | else |
| 5539 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s", |
| 5540 | s.GetData()); |
| 5541 | } |
| 5542 | |
| 5543 | if (should_unwind) { |
| 5544 | if (log) |
| 5545 | log->Printf("Process::RunThreadPlan: ExecutionInterrupted - " |
| 5546 | "discarding thread plans up to %p.", |
| 5547 | static_cast<void *>(thread_plan_sp.get())); |
| 5548 | thread->DiscardThreadPlansUpToPlan(thread_plan_sp); |
| 5549 | } else { |
| 5550 | if (log) |
| 5551 | log->Printf("Process::RunThreadPlan: ExecutionInterrupted - for " |
| 5552 | "plan: %p not discarding.", |
| 5553 | static_cast<void *>(thread_plan_sp.get())); |
| 5554 | } |
| 5555 | } else if (return_value == eExpressionSetupError) { |
| 5556 | if (log) |
| 5557 | log->PutCString("Process::RunThreadPlan(): execution set up error."); |
| 5558 | |
| 5559 | if (options.DoesUnwindOnError()) { |
| 5560 | thread->DiscardThreadPlansUpToPlan(thread_plan_sp); |
| 5561 | } |
| 5562 | } else { |
| 5563 | if (thread->IsThreadPlanDone(thread_plan_sp.get())) { |
| 5564 | if (log) |
| 5565 | log->PutCString("Process::RunThreadPlan(): thread plan is done"); |
| 5566 | return_value = eExpressionCompleted; |
| 5567 | } else if (thread->WasThreadPlanDiscarded(thread_plan_sp.get())) { |
| 5568 | if (log) |
| 5569 | log->PutCString( |
| 5570 | "Process::RunThreadPlan(): thread plan was discarded"); |
| 5571 | return_value = eExpressionDiscarded; |
| 5572 | } else { |
| 5573 | if (log) |
| 5574 | log->PutCString( |
| 5575 | "Process::RunThreadPlan(): thread plan stopped in mid course"); |
| 5576 | if (options.DoesUnwindOnError() && thread_plan_sp) { |
| 5577 | if (log) |
| 5578 | log->PutCString("Process::RunThreadPlan(): discarding thread plan " |
| 5579 | "'cause unwind_on_error is set."); |
| 5580 | thread->DiscardThreadPlansUpToPlan(thread_plan_sp); |
| 5581 | } |
| 5582 | } |
| 5583 | } |
| 5584 | |
| 5585 | // Thread we ran the function in may have gone away because we ran the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5586 | // target Check that it's still there, and if it is put it back in the |
| 5587 | // context. Also restore the frame in the context if it is still present. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5588 | thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get(); |
| 5589 | if (thread) { |
| 5590 | exe_ctx.SetFrameSP(thread->GetFrameWithStackID(ctx_frame_id)); |
| 5591 | } |
| 5592 | |
| 5593 | // Also restore the current process'es selected frame & thread, since this |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5594 | // function calling may be done behind the user's back. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5595 | |
| 5596 | if (selected_tid != LLDB_INVALID_THREAD_ID) { |
| 5597 | if (GetThreadList().SetSelectedThreadByIndexID(selected_tid) && |
| 5598 | selected_stack_id.IsValid()) { |
| 5599 | // We were able to restore the selected thread, now restore the frame: |
| 5600 | std::lock_guard<std::recursive_mutex> guard(GetThreadList().GetMutex()); |
| 5601 | StackFrameSP old_frame_sp = |
| 5602 | GetThreadList().GetSelectedThread()->GetFrameWithStackID( |
| 5603 | selected_stack_id); |
| 5604 | if (old_frame_sp) |
| 5605 | GetThreadList().GetSelectedThread()->SetSelectedFrame( |
| 5606 | old_frame_sp.get()); |
| 5607 | } |
| 5608 | } |
| 5609 | } |
| 5610 | |
| 5611 | // If the process exited during the run of the thread plan, notify everyone. |
| 5612 | |
| 5613 | if (event_to_broadcast_sp) { |
| 5614 | if (log) |
| 5615 | log->PutCString("Process::RunThreadPlan(): rebroadcasting event."); |
| 5616 | BroadcastEvent(event_to_broadcast_sp); |
| 5617 | } |
| 5618 | |
| 5619 | return return_value; |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5620 | } |
| 5621 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5622 | const char *Process::ExecutionResultAsCString(ExpressionResults result) { |
| 5623 | const char *result_name; |
| 5624 | |
| 5625 | switch (result) { |
| 5626 | case eExpressionCompleted: |
| 5627 | result_name = "eExpressionCompleted"; |
| 5628 | break; |
| 5629 | case eExpressionDiscarded: |
| 5630 | result_name = "eExpressionDiscarded"; |
| 5631 | break; |
| 5632 | case eExpressionInterrupted: |
| 5633 | result_name = "eExpressionInterrupted"; |
| 5634 | break; |
| 5635 | case eExpressionHitBreakpoint: |
| 5636 | result_name = "eExpressionHitBreakpoint"; |
| 5637 | break; |
| 5638 | case eExpressionSetupError: |
| 5639 | result_name = "eExpressionSetupError"; |
| 5640 | break; |
| 5641 | case eExpressionParseError: |
| 5642 | result_name = "eExpressionParseError"; |
| 5643 | break; |
| 5644 | case eExpressionResultUnavailable: |
| 5645 | result_name = "eExpressionResultUnavailable"; |
| 5646 | break; |
| 5647 | case eExpressionTimedOut: |
| 5648 | result_name = "eExpressionTimedOut"; |
| 5649 | break; |
| 5650 | case eExpressionStoppedForDebug: |
| 5651 | result_name = "eExpressionStoppedForDebug"; |
| 5652 | break; |
| 5653 | } |
| 5654 | return result_name; |
Jason Molenda | 484900b | 2015-08-10 07:55:25 +0000 | [diff] [blame] | 5655 | } |
| 5656 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5657 | void Process::GetStatus(Stream &strm) { |
| 5658 | const StateType state = GetState(); |
| 5659 | if (StateIsStoppedState(state, false)) { |
| 5660 | if (state == eStateExited) { |
| 5661 | int exit_status = GetExitStatus(); |
| 5662 | const char *exit_description = GetExitDescription(); |
| 5663 | strm.Printf("Process %" PRIu64 " exited with status = %i (0x%8.8x) %s\n", |
| 5664 | GetID(), exit_status, exit_status, |
| 5665 | exit_description ? exit_description : ""); |
| 5666 | } else { |
| 5667 | if (state == eStateConnected) |
| 5668 | strm.Printf("Connected to remote target.\n"); |
| 5669 | else |
| 5670 | strm.Printf("Process %" PRIu64 " %s\n", GetID(), StateAsCString(state)); |
| 5671 | } |
| 5672 | } else { |
| 5673 | strm.Printf("Process %" PRIu64 " is running.\n", GetID()); |
| 5674 | } |
Pavel Labath | a933d51 | 2016-04-05 13:07:16 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5677 | size_t Process::GetThreadStatus(Stream &strm, |
| 5678 | bool only_threads_with_stop_reason, |
| 5679 | uint32_t start_frame, uint32_t num_frames, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 5680 | uint32_t num_frames_with_source, |
| 5681 | bool stop_format) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5682 | size_t num_thread_infos_dumped = 0; |
Greg Clayton | 35ca64b | 2015-04-16 17:13:34 +0000 | [diff] [blame] | 5683 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5684 | // You can't hold the thread list lock while calling Thread::GetStatus. That |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5685 | // very well might run code (e.g. if we need it to get return values or |
| 5686 | // arguments.) For that to work the process has to be able to acquire it. |
| 5687 | // So instead copy the thread ID's, and look them up one by one: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5688 | |
| 5689 | uint32_t num_threads; |
| 5690 | std::vector<lldb::tid_t> thread_id_array; |
| 5691 | // Scope for thread list locker; |
| 5692 | { |
| 5693 | std::lock_guard<std::recursive_mutex> guard(GetThreadList().GetMutex()); |
| 5694 | ThreadList &curr_thread_list = GetThreadList(); |
| 5695 | num_threads = curr_thread_list.GetSize(); |
| 5696 | uint32_t idx; |
| 5697 | thread_id_array.resize(num_threads); |
| 5698 | for (idx = 0; idx < num_threads; ++idx) |
| 5699 | thread_id_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetID(); |
| 5700 | } |
| 5701 | |
| 5702 | for (uint32_t i = 0; i < num_threads; i++) { |
| 5703 | ThreadSP thread_sp(GetThreadList().FindThreadByID(thread_id_array[i])); |
| 5704 | if (thread_sp) { |
| 5705 | if (only_threads_with_stop_reason) { |
| 5706 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo(); |
| 5707 | if (!stop_info_sp || !stop_info_sp->IsValid()) |
| 5708 | continue; |
| 5709 | } |
| 5710 | thread_sp->GetStatus(strm, start_frame, num_frames, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 5711 | num_frames_with_source, |
| 5712 | stop_format); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5713 | ++num_thread_infos_dumped; |
| 5714 | } else { |
| 5715 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 5716 | if (log) |
| 5717 | log->Printf("Process::GetThreadStatus - thread 0x" PRIu64 |
| 5718 | " vanished while running Thread::GetStatus."); |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 5719 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5720 | } |
| 5721 | return num_thread_infos_dumped; |
| 5722 | } |
| 5723 | |
| 5724 | void Process::AddInvalidMemoryRegion(const LoadRange ®ion) { |
| 5725 | m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 5726 | } |
| 5727 | |
| 5728 | bool Process::RemoveInvalidMemoryRange(const LoadRange ®ion) { |
| 5729 | return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), |
| 5730 | region.GetByteSize()); |
| 5731 | } |
| 5732 | |
| 5733 | void Process::AddPreResumeAction(PreResumeActionCallback callback, |
| 5734 | void *baton) { |
| 5735 | m_pre_resume_actions.push_back(PreResumeCallbackAndBaton(callback, baton)); |
| 5736 | } |
| 5737 | |
| 5738 | bool Process::RunPreResumeActions() { |
| 5739 | bool result = true; |
| 5740 | while (!m_pre_resume_actions.empty()) { |
| 5741 | struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back(); |
| 5742 | m_pre_resume_actions.pop_back(); |
| 5743 | bool this_result = action.callback(action.baton); |
| 5744 | if (result) |
| 5745 | result = this_result; |
| 5746 | } |
| 5747 | return result; |
| 5748 | } |
| 5749 | |
| 5750 | void Process::ClearPreResumeActions() { m_pre_resume_actions.clear(); } |
| 5751 | |
Jim Ingham | ffd9175 | 2016-10-20 22:50:00 +0000 | [diff] [blame] | 5752 | void Process::ClearPreResumeAction(PreResumeActionCallback callback, void *baton) |
| 5753 | { |
| 5754 | PreResumeCallbackAndBaton element(callback, baton); |
| 5755 | auto found_iter = std::find(m_pre_resume_actions.begin(), m_pre_resume_actions.end(), element); |
| 5756 | if (found_iter != m_pre_resume_actions.end()) |
| 5757 | { |
| 5758 | m_pre_resume_actions.erase(found_iter); |
| 5759 | } |
| 5760 | } |
| 5761 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5762 | ProcessRunLock &Process::GetRunLock() { |
| 5763 | if (m_private_state_thread.EqualsThread(Host::GetCurrentThread())) |
| 5764 | return m_private_run_lock; |
| 5765 | else |
| 5766 | return m_public_run_lock; |
| 5767 | } |
| 5768 | |
| 5769 | void Process::Flush() { |
| 5770 | m_thread_list.Flush(); |
| 5771 | m_extended_thread_list.Flush(); |
| 5772 | m_extended_thread_stop_id = 0; |
| 5773 | m_queue_list.Clear(); |
| 5774 | m_queue_list_stop_id = 0; |
| 5775 | } |
| 5776 | |
| 5777 | void Process::DidExec() { |
| 5778 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 5779 | if (log) |
| 5780 | log->Printf("Process::%s()", __FUNCTION__); |
| 5781 | |
| 5782 | Target &target = GetTarget(); |
| 5783 | target.CleanupProcess(); |
| 5784 | target.ClearModules(false); |
| 5785 | m_dynamic_checkers_ap.reset(); |
| 5786 | m_abi_sp.reset(); |
| 5787 | m_system_runtime_ap.reset(); |
| 5788 | m_os_ap.reset(); |
| 5789 | m_dyld_ap.reset(); |
| 5790 | m_jit_loaders_ap.reset(); |
| 5791 | m_image_tokens.clear(); |
| 5792 | m_allocated_memory_cache.Clear(); |
| 5793 | m_language_runtimes.clear(); |
| 5794 | m_instrumentation_runtimes.clear(); |
| 5795 | m_thread_list.DiscardThreadPlans(); |
| 5796 | m_memory_cache.Clear(true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5797 | DoDidExec(); |
| 5798 | CompleteAttach(); |
| 5799 | // Flush the process (threads and all stack frames) after running |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5800 | // CompleteAttach() in case the dynamic loader loaded things in new |
| 5801 | // locations. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5802 | Flush(); |
| 5803 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5804 | // After we figure out what was loaded/unloaded in CompleteAttach, we need to |
| 5805 | // let the target know so it can do any cleanup it needs to. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5806 | target.DidExec(); |
| 5807 | } |
| 5808 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5809 | addr_t Process::ResolveIndirectFunction(const Address *address, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5810 | if (address == nullptr) { |
| 5811 | error.SetErrorString("Invalid address argument"); |
| 5812 | return LLDB_INVALID_ADDRESS; |
| 5813 | } |
| 5814 | |
| 5815 | addr_t function_addr = LLDB_INVALID_ADDRESS; |
| 5816 | |
| 5817 | addr_t addr = address->GetLoadAddress(&GetTarget()); |
| 5818 | std::map<addr_t, addr_t>::const_iterator iter = |
| 5819 | m_resolved_indirect_addresses.find(addr); |
| 5820 | if (iter != m_resolved_indirect_addresses.end()) { |
| 5821 | function_addr = (*iter).second; |
| 5822 | } else { |
| 5823 | if (!InferiorCall(this, address, function_addr)) { |
| 5824 | Symbol *symbol = address->CalculateSymbolContextSymbol(); |
| 5825 | error.SetErrorStringWithFormat( |
| 5826 | "Unable to call resolver for indirect function %s", |
| 5827 | symbol ? symbol->GetName().AsCString() : "<UNKNOWN>"); |
| 5828 | function_addr = LLDB_INVALID_ADDRESS; |
| 5829 | } else { |
| 5830 | m_resolved_indirect_addresses.insert( |
| 5831 | std::pair<addr_t, addr_t>(addr, function_addr)); |
| 5832 | } |
| 5833 | } |
| 5834 | return function_addr; |
| 5835 | } |
| 5836 | |
| 5837 | void Process::ModulesDidLoad(ModuleList &module_list) { |
| 5838 | SystemRuntime *sys_runtime = GetSystemRuntime(); |
| 5839 | if (sys_runtime) { |
| 5840 | sys_runtime->ModulesDidLoad(module_list); |
| 5841 | } |
| 5842 | |
| 5843 | GetJITLoaders().ModulesDidLoad(module_list); |
| 5844 | |
| 5845 | // Give runtimes a chance to be created. |
| 5846 | InstrumentationRuntime::ModulesDidLoad(module_list, this, |
| 5847 | m_instrumentation_runtimes); |
| 5848 | |
| 5849 | // Tell runtimes about new modules. |
| 5850 | for (auto pos = m_instrumentation_runtimes.begin(); |
| 5851 | pos != m_instrumentation_runtimes.end(); ++pos) { |
| 5852 | InstrumentationRuntimeSP runtime = pos->second; |
| 5853 | runtime->ModulesDidLoad(module_list); |
| 5854 | } |
| 5855 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5856 | // Let any language runtimes we have already created know about the modules |
| 5857 | // that loaded. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5858 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5859 | // Iterate over a copy of this language runtime list in case the language |
Leonard Mosescu | 9ba5157 | 2018-08-07 18:00:30 +0000 | [diff] [blame] | 5860 | // runtime ModulesDidLoad somehow causes the language runtime to be |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5861 | // unloaded. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5862 | LanguageRuntimeCollection language_runtimes(m_language_runtimes); |
| 5863 | for (const auto &pair : language_runtimes) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5864 | // We must check language_runtime_sp to make sure it is not nullptr as we |
| 5865 | // might cache the fact that we didn't have a language runtime for a |
| 5866 | // language. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5867 | LanguageRuntimeSP language_runtime_sp = pair.second; |
| 5868 | if (language_runtime_sp) |
| 5869 | language_runtime_sp->ModulesDidLoad(module_list); |
| 5870 | } |
| 5871 | |
| 5872 | // If we don't have an operating system plug-in, try to load one since |
| 5873 | // loading shared libraries might cause a new one to try and load |
| 5874 | if (!m_os_ap) |
| 5875 | LoadOperatingSystemPlugin(false); |
| 5876 | |
| 5877 | // Give structured-data plugins a chance to see the modified modules. |
| 5878 | for (auto pair : m_structured_data_plugin_map) { |
| 5879 | if (pair.second) |
| 5880 | pair.second->ModulesDidLoad(*this, module_list); |
| 5881 | } |
| 5882 | } |
| 5883 | |
| 5884 | void Process::PrintWarning(uint64_t warning_type, const void *repeat_key, |
| 5885 | const char *fmt, ...) { |
| 5886 | bool print_warning = true; |
| 5887 | |
| 5888 | StreamSP stream_sp = GetTarget().GetDebugger().GetAsyncOutputStream(); |
| 5889 | if (!stream_sp) |
| 5890 | return; |
| 5891 | if (warning_type == eWarningsOptimization && !GetWarningsOptimization()) { |
| 5892 | return; |
| 5893 | } |
| 5894 | |
| 5895 | if (repeat_key != nullptr) { |
| 5896 | WarningsCollection::iterator it = m_warnings_issued.find(warning_type); |
| 5897 | if (it == m_warnings_issued.end()) { |
| 5898 | m_warnings_issued[warning_type] = WarningsPointerSet(); |
| 5899 | m_warnings_issued[warning_type].insert(repeat_key); |
| 5900 | } else { |
| 5901 | if (it->second.find(repeat_key) != it->second.end()) { |
| 5902 | print_warning = false; |
| 5903 | } else { |
| 5904 | it->second.insert(repeat_key); |
| 5905 | } |
| 5906 | } |
| 5907 | } |
| 5908 | |
| 5909 | if (print_warning) { |
| 5910 | va_list args; |
| 5911 | va_start(args, fmt); |
| 5912 | stream_sp->PrintfVarArg(fmt, args); |
| 5913 | va_end(args); |
| 5914 | } |
| 5915 | } |
| 5916 | |
| 5917 | void Process::PrintWarningOptimization(const SymbolContext &sc) { |
| 5918 | if (GetWarningsOptimization() && sc.module_sp && |
| 5919 | !sc.module_sp->GetFileSpec().GetFilename().IsEmpty() && sc.function && |
| 5920 | sc.function->GetIsOptimized()) { |
| 5921 | PrintWarning(Process::Warnings::eWarningsOptimization, sc.module_sp.get(), |
| 5922 | "%s was compiled with optimization - stepping may behave " |
| 5923 | "oddly; variables may not be available.\n", |
| 5924 | sc.module_sp->GetFileSpec().GetFilename().GetCString()); |
| 5925 | } |
| 5926 | } |
| 5927 | |
| 5928 | bool Process::GetProcessInfo(ProcessInstanceInfo &info) { |
| 5929 | info.Clear(); |
| 5930 | |
| 5931 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
| 5932 | if (!platform_sp) |
| 5933 | return false; |
| 5934 | |
| 5935 | return platform_sp->GetProcessInfo(GetID(), info); |
| 5936 | } |
| 5937 | |
| 5938 | ThreadCollectionSP Process::GetHistoryThreads(lldb::addr_t addr) { |
| 5939 | ThreadCollectionSP threads; |
| 5940 | |
| 5941 | const MemoryHistorySP &memory_history = |
| 5942 | MemoryHistory::FindPlugin(shared_from_this()); |
| 5943 | |
| 5944 | if (!memory_history) { |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 5945 | return threads; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
| 5948 | threads.reset(new ThreadCollection(memory_history->GetHistoryThreads(addr))); |
| 5949 | |
| 5950 | return threads; |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 5951 | } |
Kuba Brecka | 6392754 | 2014-10-11 01:59:32 +0000 | [diff] [blame] | 5952 | |
| 5953 | InstrumentationRuntimeSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5954 | Process::GetInstrumentationRuntime(lldb::InstrumentationRuntimeType type) { |
| 5955 | InstrumentationRuntimeCollection::iterator pos; |
| 5956 | pos = m_instrumentation_runtimes.find(type); |
| 5957 | if (pos == m_instrumentation_runtimes.end()) { |
| 5958 | return InstrumentationRuntimeSP(); |
| 5959 | } else |
| 5960 | return (*pos).second; |
Kuba Brecka | 6392754 | 2014-10-11 01:59:32 +0000 | [diff] [blame] | 5961 | } |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 5962 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5963 | bool Process::GetModuleSpec(const FileSpec &module_file_spec, |
| 5964 | const ArchSpec &arch, ModuleSpec &module_spec) { |
| 5965 | module_spec.Clear(); |
| 5966 | return false; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 5967 | } |
Tamas Berghammer | 3cb132a | 2015-12-02 11:58:51 +0000 | [diff] [blame] | 5968 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5969 | size_t Process::AddImageToken(lldb::addr_t image_ptr) { |
| 5970 | m_image_tokens.push_back(image_ptr); |
| 5971 | return m_image_tokens.size() - 1; |
Tamas Berghammer | 3cb132a | 2015-12-02 11:58:51 +0000 | [diff] [blame] | 5972 | } |
| 5973 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5974 | lldb::addr_t Process::GetImagePtrFromToken(size_t token) const { |
| 5975 | if (token < m_image_tokens.size()) |
| 5976 | return m_image_tokens[token]; |
| 5977 | return LLDB_INVALID_IMAGE_TOKEN; |
Tamas Berghammer | 3cb132a | 2015-12-02 11:58:51 +0000 | [diff] [blame] | 5978 | } |
| 5979 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5980 | void Process::ResetImageToken(size_t token) { |
| 5981 | if (token < m_image_tokens.size()) |
| 5982 | m_image_tokens[token] = LLDB_INVALID_IMAGE_TOKEN; |
Enrico Granata | f3129cb | 2015-12-03 23:53:45 +0000 | [diff] [blame] | 5983 | } |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5984 | |
| 5985 | Address |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5986 | Process::AdvanceAddressToNextBranchInstruction(Address default_stop_addr, |
| 5987 | AddressRange range_bounds) { |
| 5988 | Target &target = GetTarget(); |
| 5989 | DisassemblerSP disassembler_sp; |
| 5990 | InstructionList *insn_list = nullptr; |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5991 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5992 | Address retval = default_stop_addr; |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5993 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5994 | if (!target.GetUseFastStepping()) |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5995 | return retval; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5996 | if (!default_stop_addr.IsValid()) |
| 5997 | return retval; |
| 5998 | |
| 5999 | ExecutionContext exe_ctx(this); |
| 6000 | const char *plugin_name = nullptr; |
| 6001 | const char *flavor = nullptr; |
| 6002 | const bool prefer_file_cache = true; |
| 6003 | disassembler_sp = Disassembler::DisassembleRange( |
| 6004 | target.GetArchitecture(), plugin_name, flavor, exe_ctx, range_bounds, |
| 6005 | prefer_file_cache); |
| 6006 | if (disassembler_sp) |
| 6007 | insn_list = &disassembler_sp->GetInstructionList(); |
| 6008 | |
| 6009 | if (insn_list == nullptr) { |
| 6010 | return retval; |
| 6011 | } |
| 6012 | |
| 6013 | size_t insn_offset = |
| 6014 | insn_list->GetIndexOfInstructionAtAddress(default_stop_addr); |
| 6015 | if (insn_offset == UINT32_MAX) { |
| 6016 | return retval; |
| 6017 | } |
| 6018 | |
| 6019 | uint32_t branch_index = |
| 6020 | insn_list->GetIndexOfNextBranchInstruction(insn_offset, target); |
| 6021 | if (branch_index == UINT32_MAX) { |
| 6022 | return retval; |
| 6023 | } |
| 6024 | |
| 6025 | if (branch_index > insn_offset) { |
| 6026 | Address next_branch_insn_address = |
| 6027 | insn_list->GetInstructionAtIndex(branch_index)->GetAddress(); |
| 6028 | if (next_branch_insn_address.IsValid() && |
| 6029 | range_bounds.ContainsFileAddress(next_branch_insn_address)) { |
| 6030 | retval = next_branch_insn_address; |
| 6031 | } |
| 6032 | } |
| 6033 | |
| 6034 | return retval; |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 6035 | } |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 6036 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6037 | Status |
| 6038 | Process::GetMemoryRegions(std::vector<lldb::MemoryRegionInfoSP> ®ion_list) { |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 6039 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6040 | Status error; |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 6041 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6042 | lldb::addr_t range_end = 0; |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 6043 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6044 | region_list.clear(); |
| 6045 | do { |
| 6046 | lldb::MemoryRegionInfoSP region_info(new lldb_private::MemoryRegionInfo()); |
| 6047 | error = GetMemoryRegionInfo(range_end, *region_info); |
| 6048 | // GetMemoryRegionInfo should only return an error if it is unimplemented. |
| 6049 | if (error.Fail()) { |
| 6050 | region_list.clear(); |
| 6051 | break; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6054 | range_end = region_info->GetRange().GetRangeEnd(); |
| 6055 | if (region_info->GetMapped() == MemoryRegionInfo::eYes) { |
| 6056 | region_list.push_back(region_info); |
| 6057 | } |
| 6058 | } while (range_end != LLDB_INVALID_ADDRESS); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6059 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6060 | return error; |
| 6061 | } |
| 6062 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6063 | Status |
| 6064 | Process::ConfigureStructuredData(const ConstString &type_name, |
| 6065 | const StructuredData::ObjectSP &config_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6066 | // If you get this, the Process-derived class needs to implement a method to |
| 6067 | // enable an already-reported asynchronous structured data feature. See |
| 6068 | // ProcessGDBRemote for an example implementation over gdb-remote. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6069 | return Status("unimplemented"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |
| 6072 | void Process::MapSupportedStructuredDataPlugins( |
| 6073 | const StructuredData::Array &supported_type_names) { |
| 6074 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 6075 | |
| 6076 | // Bail out early if there are no type names to map. |
| 6077 | if (supported_type_names.GetSize() == 0) { |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6078 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6079 | log->Printf("Process::%s(): no structured data types supported", |
| 6080 | __FUNCTION__); |
| 6081 | return; |
| 6082 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6083 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6084 | // Convert StructuredData type names to ConstString instances. |
| 6085 | std::set<ConstString> const_type_names; |
| 6086 | |
| 6087 | if (log) |
| 6088 | log->Printf("Process::%s(): the process supports the following async " |
| 6089 | "structured data types:", |
| 6090 | __FUNCTION__); |
| 6091 | |
| 6092 | supported_type_names.ForEach( |
| 6093 | [&const_type_names, &log](StructuredData::Object *object) { |
| 6094 | if (!object) { |
| 6095 | // Invalid - shouldn't be null objects in the array. |
| 6096 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6097 | } |
| 6098 | |
| 6099 | auto type_name = object->GetAsString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6100 | if (!type_name) { |
| 6101 | // Invalid format - all type names should be strings. |
| 6102 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6103 | } |
| 6104 | |
| 6105 | const_type_names.insert(ConstString(type_name->GetValue())); |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 6106 | LLDB_LOG(log, "- {0}", type_name->GetValue()); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6107 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6108 | }); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6109 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6110 | // For each StructuredDataPlugin, if the plugin handles any of the types in |
| 6111 | // the supported_type_names, map that type name to that plugin. Stop when |
| 6112 | // we've consumed all the type names. |
Leonard Mosescu | 9ba5157 | 2018-08-07 18:00:30 +0000 | [diff] [blame] | 6113 | // FIXME: should we return an error if there are type names nobody |
Jim Ingham | 93979f6 | 2018-04-27 01:57:40 +0000 | [diff] [blame] | 6114 | // supports? |
| 6115 | for (uint32_t plugin_index = 0; !const_type_names.empty(); plugin_index++) { |
| 6116 | auto create_instance = |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6117 | PluginManager::GetStructuredDataPluginCreateCallbackAtIndex( |
| 6118 | plugin_index); |
Jim Ingham | 93979f6 | 2018-04-27 01:57:40 +0000 | [diff] [blame] | 6119 | if (!create_instance) |
| 6120 | break; |
Leonard Mosescu | 9ba5157 | 2018-08-07 18:00:30 +0000 | [diff] [blame] | 6121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6122 | // Create the plugin. |
| 6123 | StructuredDataPluginSP plugin_sp = (*create_instance)(*this); |
| 6124 | if (!plugin_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6125 | // This plugin doesn't think it can work with the process. Move on to the |
| 6126 | // next. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6127 | continue; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6128 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6129 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6130 | // For any of the remaining type names, map any that this plugin supports. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6131 | std::vector<ConstString> names_to_remove; |
| 6132 | for (auto &type_name : const_type_names) { |
| 6133 | if (plugin_sp->SupportsStructuredDataType(type_name)) { |
| 6134 | m_structured_data_plugin_map.insert( |
| 6135 | std::make_pair(type_name, plugin_sp)); |
| 6136 | names_to_remove.push_back(type_name); |
| 6137 | if (log) |
| 6138 | log->Printf("Process::%s(): using plugin %s for type name " |
| 6139 | "%s", |
| 6140 | __FUNCTION__, plugin_sp->GetPluginName().GetCString(), |
| 6141 | type_name.GetCString()); |
| 6142 | } |
| 6143 | } |
| 6144 | |
| 6145 | // Remove the type names that were consumed by this plugin. |
| 6146 | for (auto &type_name : names_to_remove) |
| 6147 | const_type_names.erase(type_name); |
| 6148 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6149 | } |
| 6150 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6151 | bool Process::RouteAsyncStructuredData( |
| 6152 | const StructuredData::ObjectSP object_sp) { |
| 6153 | // Nothing to do if there's no data. |
| 6154 | if (!object_sp) |
| 6155 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6156 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6157 | // The contract is this must be a dictionary, so we can look up the routing |
| 6158 | // key via the top-level 'type' string value within the dictionary. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6159 | StructuredData::Dictionary *dictionary = object_sp->GetAsDictionary(); |
| 6160 | if (!dictionary) |
| 6161 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6163 | // Grab the async structured type name (i.e. the feature/plugin name). |
| 6164 | ConstString type_name; |
| 6165 | if (!dictionary->GetValueForKeyAsString("type", type_name)) |
| 6166 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6167 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6168 | // Check if there's a plugin registered for this type name. |
| 6169 | auto find_it = m_structured_data_plugin_map.find(type_name); |
| 6170 | if (find_it == m_structured_data_plugin_map.end()) { |
| 6171 | // We don't have a mapping for this structured data type. |
| 6172 | return false; |
| 6173 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6175 | // Route the structured data to the plugin. |
| 6176 | find_it->second->HandleArrivalOfStructuredData(*this, type_name, object_sp); |
| 6177 | return true; |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 6178 | } |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 6179 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6180 | Status Process::UpdateAutomaticSignalFiltering() { |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 6181 | // Default implementation does nothign. |
| 6182 | // No automatic signal filtering to speak of. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6183 | return Status(); |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 6184 | } |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 6185 | |
Frederic Riss | d10d379 | 2018-05-11 18:21:11 +0000 | [diff] [blame] | 6186 | UtilityFunction *Process::GetLoadImageUtilityFunction( |
| 6187 | Platform *platform, |
| 6188 | llvm::function_ref<std::unique_ptr<UtilityFunction>()> factory) { |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 6189 | if (platform != GetTarget().GetPlatform().get()) |
| 6190 | return nullptr; |
Frederic Riss | d10d379 | 2018-05-11 18:21:11 +0000 | [diff] [blame] | 6191 | std::call_once(m_dlopen_utility_func_flag_once, |
| 6192 | [&] { m_dlopen_utility_func_up = factory(); }); |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 6193 | return m_dlopen_utility_func_up.get(); |
| 6194 | } |