Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- Process.cpp -------------------------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Greg Clayton | 5cc45e0 | 2016-02-26 19:41:49 +0000 | [diff] [blame] | 9 | #include <atomic> |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 10 | #include <memory> |
Greg Clayton | 04df8ee | 2016-02-26 19:38:18 +0000 | [diff] [blame] | 11 | #include <mutex> |
Eugene Zelenko | da8cf8a | 2016-03-01 00:55:51 +0000 | [diff] [blame] | 12 | |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 13 | #include "llvm/Support/ScopedPrinter.h" |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Threading.h" |
| 15 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Breakpoint/BreakpointLocation.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 17 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Module.h" |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 20 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 22 | #include "lldb/Core/StreamFile.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 23 | #include "lldb/Expression/DiagnosticManager.h" |
Alex Langford | bab7e3d | 2019-07-12 00:58:02 +0000 | [diff] [blame] | 24 | #include "lldb/Expression/DynamicCheckerFunctions.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 25 | #include "lldb/Expression/UserExpression.h" |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 26 | #include "lldb/Expression/UtilityFunction.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 27 | #include "lldb/Host/ConnectionFileDescriptor.h" |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame] | 28 | #include "lldb/Host/FileSystem.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Host/Host.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 30 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | 3eb2b44 | 2017-03-22 23:33:16 +0000 | [diff] [blame] | 31 | #include "lldb/Host/OptionParser.h" |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 32 | #include "lldb/Host/Pipe.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 33 | #include "lldb/Host/Terminal.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 34 | #include "lldb/Host/ThreadLauncher.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 35 | #include "lldb/Interpreter/CommandInterpreter.h" |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 36 | #include "lldb/Interpreter/OptionArgParser.h" |
Zachary Turner | 633a29c | 2015-03-04 01:58:01 +0000 | [diff] [blame] | 37 | #include "lldb/Interpreter/OptionValueProperties.h" |
Jason Molenda | 484900b | 2015-08-10 07:55:25 +0000 | [diff] [blame] | 38 | #include "lldb/Symbol/Function.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 39 | #include "lldb/Symbol/Symbol.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | #include "lldb/Target/ABI.h" |
Med Ismail Bennani | 7ebe9cc | 2020-02-04 17:53:26 +0100 | [diff] [blame] | 41 | #include "lldb/Target/AssertFrameRecognizer.h" |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 42 | #include "lldb/Target/DynamicLoader.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 43 | #include "lldb/Target/InstrumentationRuntime.h" |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 44 | #include "lldb/Target/JITLoader.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 45 | #include "lldb/Target/JITLoaderList.h" |
Alex Langford | 03e1a82 | 2019-05-29 18:08:22 +0000 | [diff] [blame] | 46 | #include "lldb/Target/Language.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 47 | #include "lldb/Target/LanguageRuntime.h" |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 48 | #include "lldb/Target/MemoryHistory.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 49 | #include "lldb/Target/MemoryRegionInfo.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 50 | #include "lldb/Target/OperatingSystem.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 51 | #include "lldb/Target/Platform.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 52 | #include "lldb/Target/Process.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 54 | #include "lldb/Target/StopInfo.h" |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 55 | #include "lldb/Target/StructuredDataPlugin.h" |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 56 | #include "lldb/Target/SystemRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | #include "lldb/Target/Target.h" |
| 58 | #include "lldb/Target/TargetList.h" |
| 59 | #include "lldb/Target/Thread.h" |
| 60 | #include "lldb/Target/ThreadPlan.h" |
Jim Ingham | 076b304 | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 61 | #include "lldb/Target/ThreadPlanBase.h" |
Alex Langford | 5b2b38e | 2019-09-13 00:02:05 +0000 | [diff] [blame] | 62 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Jim Ingham | 61e8e68 | 2020-03-10 16:18:11 -0700 | [diff] [blame] | 63 | #include "lldb/Target/ThreadPlanStack.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 64 | #include "lldb/Target/UnixSignals.h" |
Pavel Labath | 181b823 | 2018-12-14 15:59:49 +0000 | [diff] [blame] | 65 | #include "lldb/Utility/Event.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 66 | #include "lldb/Utility/Log.h" |
Zachary Turner | 5023257 | 2015-03-18 21:31:45 +0000 | [diff] [blame] | 67 | #include "lldb/Utility/NameMatches.h" |
Zachary Turner | 805e710 | 2019-03-04 21:51:03 +0000 | [diff] [blame] | 68 | #include "lldb/Utility/ProcessInfo.h" |
Greg Clayton | ee1f578 | 2016-08-10 22:43:48 +0000 | [diff] [blame] | 69 | #include "lldb/Utility/SelectHelper.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 70 | #include "lldb/Utility/State.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | |
| 72 | using namespace lldb; |
| 73 | using namespace lldb_private; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 74 | using namespace std::chrono; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | // Comment out line below to disable memory caching, overriding the process |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 77 | // setting target.process.disable-memory-cache |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 78 | #define ENABLE_MEMORY_CACHING |
| 79 | |
| 80 | #ifdef ENABLE_MEMORY_CACHING |
| 81 | #define DISABLE_MEM_CACHE_DEFAULT false |
| 82 | #else |
| 83 | #define DISABLE_MEM_CACHE_DEFAULT true |
| 84 | #endif |
| 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | class ProcessOptionValueProperties : public OptionValueProperties { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 87 | public: |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 88 | ProcessOptionValueProperties(ConstString name) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | : OptionValueProperties(name) {} |
| 90 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 91 | // This constructor is used when creating ProcessOptionValueProperties when |
| 92 | // it is part of a new lldb_private::Process instance. It will copy all |
| 93 | // current global property values as needed |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | ProcessOptionValueProperties(ProcessProperties *global_properties) |
| 95 | : OptionValueProperties(*global_properties->GetValueProperties()) {} |
| 96 | |
| 97 | const Property *GetPropertyAtIndex(const ExecutionContext *exe_ctx, |
| 98 | bool will_modify, |
| 99 | uint32_t idx) const override { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 100 | // When getting the value for a key from the process options, we will |
| 101 | // always try and grab the setting from the current process if there is |
| 102 | // one. Else we just use the one from this instance. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | if (exe_ctx) { |
| 104 | Process *process = exe_ctx->GetProcessPtr(); |
| 105 | if (process) { |
| 106 | ProcessOptionValueProperties *instance_properties = |
| 107 | static_cast<ProcessOptionValueProperties *>( |
| 108 | process->GetValueProperties().get()); |
| 109 | if (this != instance_properties) |
| 110 | return instance_properties->ProtectedGetPropertyAtIndex(idx); |
| 111 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 112 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | return ProtectedGetPropertyAtIndex(idx); |
| 114 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
Jonas Devlieghere | 971f9ca | 2019-07-25 21:36:37 +0000 | [diff] [blame] | 117 | #define LLDB_PROPERTIES_process |
Jordan Rupprecht | 6a253d3 | 2019-07-29 17:22:10 +0000 | [diff] [blame] | 118 | #include "TargetProperties.inc" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 119 | |
| 120 | enum { |
Jonas Devlieghere | 971f9ca | 2019-07-25 21:36:37 +0000 | [diff] [blame] | 121 | #define LLDB_PROPERTIES_process |
Jordan Rupprecht | 6a253d3 | 2019-07-29 17:22:10 +0000 | [diff] [blame] | 122 | #include "TargetPropertiesEnum.inc" |
Jonas Devlieghere | 018e5a9 | 2020-05-19 11:16:57 -0700 | [diff] [blame] | 123 | ePropertyExperimental, |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
Jonas Devlieghere | 018e5a9 | 2020-05-19 11:16:57 -0700 | [diff] [blame] | 126 | #define LLDB_PROPERTIES_process_experimental |
| 127 | #include "TargetProperties.inc" |
| 128 | |
| 129 | enum { |
| 130 | #define LLDB_PROPERTIES_process_experimental |
| 131 | #include "TargetPropertiesEnum.inc" |
| 132 | }; |
| 133 | |
| 134 | class ProcessExperimentalOptionValueProperties : public OptionValueProperties { |
| 135 | public: |
| 136 | ProcessExperimentalOptionValueProperties() |
| 137 | : OptionValueProperties( |
| 138 | ConstString(Properties::GetExperimentalSettingsName())) {} |
| 139 | }; |
| 140 | |
| 141 | ProcessExperimentalProperties::ProcessExperimentalProperties() |
| 142 | : Properties(OptionValuePropertiesSP( |
| 143 | new ProcessExperimentalOptionValueProperties())) { |
| 144 | m_collection_sp->Initialize(g_process_experimental_properties); |
| 145 | } |
| 146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | ProcessProperties::ProcessProperties(lldb_private::Process *process) |
| 148 | : Properties(), |
| 149 | m_process(process) // Can be nullptr for global ProcessProperties |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 150 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 151 | if (process == nullptr) { |
| 152 | // Global process properties, set them up one time |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 153 | m_collection_sp = |
| 154 | std::make_shared<ProcessOptionValueProperties>(ConstString("process")); |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 155 | m_collection_sp->Initialize(g_process_properties); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | m_collection_sp->AppendProperty( |
| 157 | ConstString("thread"), ConstString("Settings specific to threads."), |
| 158 | true, Thread::GetGlobalProperties()->GetValueProperties()); |
| 159 | } else { |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 160 | m_collection_sp = std::make_shared<ProcessOptionValueProperties>( |
| 161 | Process::GetGlobalProperties().get()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | m_collection_sp->SetValueChangedCallback( |
| 163 | ePropertyPythonOSPluginPath, |
Pavel Labath | 5c4661b | 2020-01-09 14:14:54 +0100 | [diff] [blame] | 164 | [this] { m_process->LoadOperatingSystemPlugin(true); }); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | } |
Jonas Devlieghere | 018e5a9 | 2020-05-19 11:16:57 -0700 | [diff] [blame] | 166 | |
Jonas Devlieghere | 06412da | 2020-06-24 17:44:33 -0700 | [diff] [blame] | 167 | m_experimental_properties_up = |
| 168 | std::make_unique<ProcessExperimentalProperties>(); |
Jonas Devlieghere | 018e5a9 | 2020-05-19 11:16:57 -0700 | [diff] [blame] | 169 | m_collection_sp->AppendProperty( |
| 170 | ConstString(Properties::GetExperimentalSettingsName()), |
| 171 | ConstString("Experimental settings - setting these won't produce " |
| 172 | "errors if the setting is not present."), |
| 173 | true, m_experimental_properties_up->GetValueProperties()); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 176 | ProcessProperties::~ProcessProperties() = default; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 177 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | bool ProcessProperties::GetDisableMemoryCache() const { |
| 179 | const uint32_t idx = ePropertyDisableMemCache; |
| 180 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 181 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 184 | uint64_t ProcessProperties::GetMemoryCacheLineSize() const { |
| 185 | const uint32_t idx = ePropertyMemCacheLineSize; |
| 186 | return m_collection_sp->GetPropertyAtIndexAsUInt64( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 187 | nullptr, idx, g_process_properties[idx].default_uint_value); |
Jason Molenda | f0340c9 | 2014-09-03 22:30:54 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 190 | Args ProcessProperties::GetExtraStartupCommands() const { |
| 191 | Args args; |
| 192 | const uint32_t idx = ePropertyExtraStartCommand; |
| 193 | m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args); |
| 194 | return args; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | void ProcessProperties::SetExtraStartupCommands(const Args &args) { |
| 198 | const uint32_t idx = ePropertyExtraStartCommand; |
| 199 | m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args); |
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 | FileSpec ProcessProperties::GetPythonOSPluginPath() const { |
| 203 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 204 | return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx); |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | void ProcessProperties::SetPythonOSPluginPath(const FileSpec &file) { |
| 208 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 209 | m_collection_sp->SetPropertyAtIndexAsFileSpec(nullptr, idx, file); |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 212 | bool ProcessProperties::GetIgnoreBreakpointsInExpressions() const { |
| 213 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 214 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 215 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 218 | void ProcessProperties::SetIgnoreBreakpointsInExpressions(bool ignore) { |
| 219 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 220 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, ignore); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | bool ProcessProperties::GetUnwindOnErrorInExpressions() const { |
| 224 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 225 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 226 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | void ProcessProperties::SetUnwindOnErrorInExpressions(bool ignore) { |
| 230 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 231 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, ignore); |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 234 | bool ProcessProperties::GetStopOnSharedLibraryEvents() const { |
| 235 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 236 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 237 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 240 | void ProcessProperties::SetStopOnSharedLibraryEvents(bool stop) { |
| 241 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 242 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, stop); |
| 243 | } |
| 244 | |
| 245 | bool ProcessProperties::GetDetachKeepsStopped() const { |
| 246 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 247 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 248 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void ProcessProperties::SetDetachKeepsStopped(bool stop) { |
| 252 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 253 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, stop); |
| 254 | } |
| 255 | |
| 256 | bool ProcessProperties::GetWarningsOptimization() const { |
| 257 | const uint32_t idx = ePropertyWarningOptimization; |
| 258 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 259 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Adrian Prantl | 220c17f | 2020-05-21 17:42:24 -0700 | [diff] [blame] | 262 | bool ProcessProperties::GetWarningsUnsupportedLanguage() const { |
| 263 | const uint32_t idx = ePropertyWarningUnsupportedLanguage; |
| 264 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
| 265 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
| 266 | } |
| 267 | |
Jim Ingham | ba205c1 | 2017-12-05 02:50:45 +0000 | [diff] [blame] | 268 | bool ProcessProperties::GetStopOnExec() const { |
| 269 | const uint32_t idx = ePropertyStopOnExec; |
| 270 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 271 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Jim Ingham | ba205c1 | 2017-12-05 02:50:45 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 274 | std::chrono::seconds ProcessProperties::GetUtilityExpressionTimeout() const { |
| 275 | const uint32_t idx = ePropertyUtilityExpressionTimeout; |
| 276 | uint64_t value = m_collection_sp->GetPropertyAtIndexAsUInt64( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 277 | nullptr, idx, g_process_properties[idx].default_uint_value); |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 278 | return std::chrono::seconds(value); |
| 279 | } |
| 280 | |
Jim Ingham | d3dfd8c | 2020-08-07 14:44:01 -0700 | [diff] [blame] | 281 | bool ProcessProperties::GetSteppingRunsAllThreads() const { |
| 282 | const uint32_t idx = ePropertySteppingRunsAllThreads; |
| 283 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
| 284 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
| 285 | } |
| 286 | |
Jonas Devlieghere | 018e5a9 | 2020-05-19 11:16:57 -0700 | [diff] [blame] | 287 | bool ProcessProperties::GetOSPluginReportsAllThreads() const { |
| 288 | const bool fail_value = true; |
| 289 | const Property *exp_property = |
| 290 | m_collection_sp->GetPropertyAtIndex(nullptr, true, ePropertyExperimental); |
| 291 | OptionValueProperties *exp_values = |
| 292 | exp_property->GetValue()->GetAsProperties(); |
| 293 | if (!exp_values) |
| 294 | return fail_value; |
| 295 | |
| 296 | return exp_values->GetPropertyAtIndexAsBoolean( |
| 297 | nullptr, ePropertyOSPluginReportsAllThreads, fail_value); |
| 298 | } |
| 299 | |
| 300 | void ProcessProperties::SetOSPluginReportsAllThreads(bool does_report) { |
| 301 | const Property *exp_property = |
| 302 | m_collection_sp->GetPropertyAtIndex(nullptr, true, ePropertyExperimental); |
| 303 | OptionValueProperties *exp_values = |
| 304 | exp_property->GetValue()->GetAsProperties(); |
| 305 | if (exp_values) |
| 306 | exp_values->SetPropertyAtIndexAsBoolean( |
| 307 | nullptr, ePropertyOSPluginReportsAllThreads, does_report); |
| 308 | } |
| 309 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 310 | Status ProcessLaunchCommandOptions::SetOptionValue( |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 311 | uint32_t option_idx, llvm::StringRef option_arg, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 312 | ExecutionContext *execution_context) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 313 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 314 | const int short_option = m_getopt_table[option_idx].val; |
| 315 | |
| 316 | switch (short_option) { |
| 317 | case 's': // Stop at program entry point |
| 318 | launch_info.GetFlags().Set(eLaunchFlagStopAtEntry); |
| 319 | break; |
| 320 | |
| 321 | case 'i': // STDIN for read only |
| 322 | { |
| 323 | FileAction action; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 324 | if (action.Open(STDIN_FILENO, FileSpec(option_arg), true, false)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 325 | launch_info.AppendFileAction(action); |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | case 'o': // Open STDOUT for write only |
| 330 | { |
| 331 | FileAction action; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 332 | if (action.Open(STDOUT_FILENO, FileSpec(option_arg), false, true)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 333 | launch_info.AppendFileAction(action); |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | case 'e': // STDERR for write only |
| 338 | { |
| 339 | FileAction action; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 340 | if (action.Open(STDERR_FILENO, FileSpec(option_arg), false, true)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 341 | launch_info.AppendFileAction(action); |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | case 'p': // Process plug-in name |
| 346 | launch_info.SetProcessPluginName(option_arg); |
| 347 | break; |
| 348 | |
| 349 | case 'n': // Disable STDIO |
| 350 | { |
| 351 | FileAction action; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 352 | const FileSpec dev_null(FileSystem::DEV_NULL); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 353 | if (action.Open(STDIN_FILENO, dev_null, true, false)) |
| 354 | launch_info.AppendFileAction(action); |
| 355 | if (action.Open(STDOUT_FILENO, dev_null, false, true)) |
| 356 | launch_info.AppendFileAction(action); |
| 357 | if (action.Open(STDERR_FILENO, dev_null, false, true)) |
| 358 | launch_info.AppendFileAction(action); |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | case 'w': |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 363 | launch_info.SetWorkingDirectory(FileSpec(option_arg)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 364 | break; |
| 365 | |
| 366 | case 't': // Open process in new terminal window |
| 367 | launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY); |
| 368 | break; |
| 369 | |
| 370 | case 'a': { |
| 371 | TargetSP target_sp = |
| 372 | execution_context ? execution_context->GetTargetSP() : TargetSP(); |
| 373 | PlatformSP platform_sp = |
| 374 | target_sp ? target_sp->GetPlatform() : PlatformSP(); |
Pavel Labath | 7263f1b | 2017-10-31 10:56:03 +0000 | [diff] [blame] | 375 | launch_info.GetArchitecture() = |
| 376 | Platform::GetAugmentedArchSpec(platform_sp.get(), option_arg); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | } break; |
| 378 | |
| 379 | case 'A': // Disable ASLR. |
| 380 | { |
| 381 | bool success; |
| 382 | const bool disable_aslr_arg = |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 383 | OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 384 | if (success) |
| 385 | disable_aslr = disable_aslr_arg ? eLazyBoolYes : eLazyBoolNo; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 386 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 387 | error.SetErrorStringWithFormat( |
| 388 | "Invalid boolean value for disable-aslr option: '%s'", |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 389 | option_arg.empty() ? "<null>" : option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 390 | break; |
| 391 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 392 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 393 | case 'X': // shell expand args. |
| 394 | { |
| 395 | bool success; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 396 | const bool expand_args = |
| 397 | OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 398 | if (success) |
| 399 | launch_info.SetShellExpandArguments(expand_args); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 400 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 401 | error.SetErrorStringWithFormat( |
| 402 | "Invalid boolean value for shell-expand-args option: '%s'", |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 403 | option_arg.empty() ? "<null>" : option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 404 | break; |
| 405 | } |
| 406 | |
| 407 | case 'c': |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 408 | if (!option_arg.empty()) |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 409 | launch_info.SetShell(FileSpec(option_arg)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 410 | else |
| 411 | launch_info.SetShell(HostInfo::GetDefaultShell()); |
| 412 | break; |
| 413 | |
| 414 | case 'v': |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 415 | launch_info.GetEnvironment().insert(option_arg); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 416 | break; |
| 417 | |
| 418 | default: |
| 419 | error.SetErrorStringWithFormat("unrecognized short option character '%c'", |
| 420 | short_option); |
| 421 | break; |
| 422 | } |
| 423 | return error; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 426 | static constexpr OptionDefinition g_process_launch_options[] = { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 427 | {LLDB_OPT_SET_ALL, false, "stop-at-entry", 's', OptionParser::eNoArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 428 | nullptr, {}, 0, eArgTypeNone, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 429 | "Stop at the entry point of the program when launching a process."}, |
| 430 | {LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 431 | OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 432 | "Set whether to disable address space layout randomization when launching " |
| 433 | "a process."}, |
| 434 | {LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 435 | nullptr, {}, 0, eArgTypePlugin, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 436 | "Name of the process plugin you want to use."}, |
| 437 | {LLDB_OPT_SET_ALL, false, "working-dir", 'w', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 438 | OptionParser::eRequiredArgument, nullptr, {}, 0, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 439 | eArgTypeDirectoryName, |
| 440 | "Set the current working directory to <path> when running the inferior."}, |
| 441 | {LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 442 | nullptr, {}, 0, eArgTypeArchitecture, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 443 | "Set the architecture for the process to launch when ambiguous."}, |
| 444 | {LLDB_OPT_SET_ALL, false, "environment", 'v', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 445 | OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeNone, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 446 | "Specify an environment variable name/value string (--environment " |
| 447 | "NAME=VALUE). Can be specified multiple times for subsequent environment " |
| 448 | "entries."}, |
| 449 | {LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3, false, "shell", 'c', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 450 | OptionParser::eOptionalArgument, nullptr, {}, 0, eArgTypeFilename, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 451 | "Run the process in a shell (not supported on all platforms)."}, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 452 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 453 | {LLDB_OPT_SET_1, false, "stdin", 'i', OptionParser::eRequiredArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 454 | nullptr, {}, 0, eArgTypeFilename, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 455 | "Redirect stdin for the process to <filename>."}, |
| 456 | {LLDB_OPT_SET_1, false, "stdout", 'o', OptionParser::eRequiredArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 457 | nullptr, {}, 0, eArgTypeFilename, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 458 | "Redirect stdout for the process to <filename>."}, |
| 459 | {LLDB_OPT_SET_1, false, "stderr", 'e', OptionParser::eRequiredArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 460 | nullptr, {}, 0, eArgTypeFilename, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 461 | "Redirect stderr for the process to <filename>."}, |
Todd Fiala | 7df337f | 2015-10-13 23:41:19 +0000 | [diff] [blame] | 462 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 463 | {LLDB_OPT_SET_2, false, "tty", 't', OptionParser::eNoArgument, nullptr, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 464 | {}, 0, eArgTypeNone, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 465 | "Start the process in a terminal (not supported on all platforms)."}, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 466 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 467 | {LLDB_OPT_SET_3, false, "no-stdio", 'n', OptionParser::eNoArgument, nullptr, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 468 | {}, 0, eArgTypeNone, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 469 | "Do not set up for terminal I/O to go to running process."}, |
| 470 | {LLDB_OPT_SET_4, false, "shell-expand-args", 'X', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 471 | OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 472 | "Set whether to shell expand arguments to the process when launching."}, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | llvm::ArrayRef<OptionDefinition> ProcessLaunchCommandOptions::GetDefinitions() { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 476 | return llvm::makeArrayRef(g_process_launch_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 477 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 478 | |
Zachary Turner | 3165945 | 2016-11-17 21:15:14 +0000 | [diff] [blame] | 479 | ProcessSP Process::FindPlugin(lldb::TargetSP target_sp, |
| 480 | llvm::StringRef plugin_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 481 | ListenerSP listener_sp, |
Michał Górny | 18e4272 | 2020-11-20 17:12:22 +0100 | [diff] [blame] | 482 | const FileSpec *crash_file_path, |
| 483 | bool can_connect) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 484 | static uint32_t g_process_unique_id = 0; |
| 485 | |
| 486 | ProcessSP process_sp; |
| 487 | ProcessCreateInstance create_callback = nullptr; |
Zachary Turner | 3165945 | 2016-11-17 21:15:14 +0000 | [diff] [blame] | 488 | if (!plugin_name.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 489 | ConstString const_plugin_name(plugin_name); |
| 490 | create_callback = |
| 491 | PluginManager::GetProcessCreateCallbackForPluginName(const_plugin_name); |
| 492 | if (create_callback) { |
Michał Górny | 18e4272 | 2020-11-20 17:12:22 +0100 | [diff] [blame] | 493 | process_sp = create_callback(target_sp, listener_sp, crash_file_path, |
| 494 | can_connect); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 495 | if (process_sp) { |
| 496 | if (process_sp->CanDebug(target_sp, true)) { |
| 497 | process_sp->m_process_unique_id = ++g_process_unique_id; |
| 498 | } else |
| 499 | process_sp.reset(); |
| 500 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 501 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 502 | } else { |
| 503 | for (uint32_t idx = 0; |
| 504 | (create_callback = |
| 505 | PluginManager::GetProcessCreateCallbackAtIndex(idx)) != nullptr; |
| 506 | ++idx) { |
Michał Górny | 18e4272 | 2020-11-20 17:12:22 +0100 | [diff] [blame] | 507 | process_sp = create_callback(target_sp, listener_sp, crash_file_path, |
| 508 | can_connect); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 509 | if (process_sp) { |
| 510 | if (process_sp->CanDebug(target_sp, false)) { |
| 511 | process_sp->m_process_unique_id = ++g_process_unique_id; |
| 512 | break; |
| 513 | } else |
| 514 | process_sp.reset(); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 521 | ConstString &Process::GetStaticBroadcasterClass() { |
| 522 | static ConstString class_name("lldb.process"); |
| 523 | return class_name; |
| 524 | } |
| 525 | |
| 526 | Process::Process(lldb::TargetSP target_sp, ListenerSP listener_sp) |
| 527 | : Process(target_sp, listener_sp, |
| 528 | UnixSignals::Create(HostInfo::GetArchitecture())) { |
| 529 | // This constructor just delegates to the full Process constructor, |
| 530 | // defaulting to using the Host's UnixSignals. |
| 531 | } |
| 532 | |
| 533 | Process::Process(lldb::TargetSP target_sp, ListenerSP listener_sp, |
| 534 | const UnixSignalsSP &unix_signals_sp) |
Raphael Isemann | ccd9091 | 2020-11-18 14:30:06 +0100 | [diff] [blame] | 535 | : ProcessProperties(this), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 536 | Broadcaster((target_sp->GetDebugger().GetBroadcasterManager()), |
| 537 | Process::GetStaticBroadcasterClass().AsCString()), |
Jim Ingham | b87b9e6 | 2018-06-26 23:38:58 +0000 | [diff] [blame] | 538 | m_target_wp(target_sp), m_public_state(eStateUnloaded), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 539 | m_private_state(eStateUnloaded), |
| 540 | m_private_state_broadcaster(nullptr, |
| 541 | "lldb.process.internal_state_broadcaster"), |
| 542 | m_private_state_control_broadcaster( |
| 543 | nullptr, "lldb.process.internal_state_control_broadcaster"), |
| 544 | m_private_state_listener_sp( |
| 545 | Listener::MakeListener("lldb.process.internal_state_listener")), |
| 546 | m_mod_id(), m_process_unique_id(0), m_thread_index_id(0), |
| 547 | m_thread_id_to_index_id_map(), m_exit_status(-1), m_exit_string(), |
| 548 | m_exit_status_mutex(), m_thread_mutex(), m_thread_list_real(this), |
Jim Ingham | 1893065 | 2020-03-18 12:05:08 -0700 | [diff] [blame] | 549 | m_thread_list(this), m_thread_plans(*this), m_extended_thread_list(this), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 550 | m_extended_thread_stop_id(0), m_queue_list(this), m_queue_list_stop_id(0), |
| 551 | m_notifications(), m_image_tokens(), m_listener_sp(listener_sp), |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 552 | m_breakpoint_site_list(), m_dynamic_checkers_up(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 553 | m_unix_signals_sp(unix_signals_sp), m_abi_sp(), m_process_input_reader(), |
| 554 | m_stdio_communication("process.stdio"), m_stdio_communication_mutex(), |
| 555 | m_stdin_forward(false), m_stdout_data(), m_stderr_data(), |
| 556 | m_profile_data_comm_mutex(), m_profile_data(), m_iohandler_sync(0), |
| 557 | m_memory_cache(*this), m_allocated_memory_cache(*this), |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 558 | m_should_detach(false), m_next_event_action_up(), m_public_run_lock(), |
Pavel Labath | 13e37d4 | 2017-10-25 21:05:31 +0000 | [diff] [blame] | 559 | m_private_run_lock(), m_finalizing(false), m_finalize_called(false), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 560 | m_clear_thread_plans_on_stop(false), m_force_next_event_delivery(false), |
| 561 | m_last_broadcast_state(eStateInvalid), m_destroy_in_process(false), |
| 562 | m_can_interpret_function_calls(false), m_warnings_issued(), |
| 563 | m_run_thread_plan_lock(), m_can_jit(eCanJITDontKnow) { |
| 564 | CheckInWithManager(); |
| 565 | |
| 566 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 567 | LLDB_LOGF(log, "%p Process::Process()", static_cast<void *>(this)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 568 | |
| 569 | if (!m_unix_signals_sp) |
| 570 | m_unix_signals_sp = std::make_shared<UnixSignals>(); |
| 571 | |
| 572 | SetEventName(eBroadcastBitStateChanged, "state-changed"); |
| 573 | SetEventName(eBroadcastBitInterrupt, "interrupt"); |
| 574 | SetEventName(eBroadcastBitSTDOUT, "stdout-available"); |
| 575 | SetEventName(eBroadcastBitSTDERR, "stderr-available"); |
| 576 | SetEventName(eBroadcastBitProfileData, "profile-data-available"); |
| 577 | SetEventName(eBroadcastBitStructuredData, "structured-data-available"); |
| 578 | |
| 579 | m_private_state_control_broadcaster.SetEventName( |
| 580 | eBroadcastInternalStateControlStop, "control-stop"); |
| 581 | m_private_state_control_broadcaster.SetEventName( |
| 582 | eBroadcastInternalStateControlPause, "control-pause"); |
| 583 | m_private_state_control_broadcaster.SetEventName( |
| 584 | eBroadcastInternalStateControlResume, "control-resume"); |
| 585 | |
| 586 | m_listener_sp->StartListeningForEvents( |
| 587 | this, eBroadcastBitStateChanged | eBroadcastBitInterrupt | |
| 588 | eBroadcastBitSTDOUT | eBroadcastBitSTDERR | |
| 589 | eBroadcastBitProfileData | eBroadcastBitStructuredData); |
| 590 | |
| 591 | m_private_state_listener_sp->StartListeningForEvents( |
| 592 | &m_private_state_broadcaster, |
| 593 | eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
| 594 | |
| 595 | m_private_state_listener_sp->StartListeningForEvents( |
| 596 | &m_private_state_control_broadcaster, |
| 597 | eBroadcastInternalStateControlStop | eBroadcastInternalStateControlPause | |
| 598 | eBroadcastInternalStateControlResume); |
| 599 | // We need something valid here, even if just the default UnixSignalsSP. |
| 600 | assert(m_unix_signals_sp && "null m_unix_signals_sp after initialization"); |
| 601 | |
| 602 | // Allow the platform to override the default cache line size |
| 603 | OptionValueSP value_sp = |
| 604 | m_collection_sp |
| 605 | ->GetPropertyAtIndex(nullptr, true, ePropertyMemCacheLineSize) |
| 606 | ->GetValue(); |
| 607 | uint32_t platform_cache_line_size = |
| 608 | target_sp->GetPlatform()->GetDefaultMemoryCacheLineSize(); |
| 609 | if (!value_sp->OptionWasSet() && platform_cache_line_size != 0) |
| 610 | value_sp->SetUInt64Value(platform_cache_line_size); |
Med Ismail Bennani | 7ebe9cc | 2020-02-04 17:53:26 +0100 | [diff] [blame] | 611 | |
| 612 | RegisterAssertFrameRecognizer(this); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | Process::~Process() { |
| 616 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 617 | LLDB_LOGF(log, "%p Process::~Process()", static_cast<void *>(this)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 618 | StopPrivateStateThread(); |
| 619 | |
| 620 | // ThreadList::Clear() will try to acquire this process's mutex, so |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 621 | // explicitly clear the thread list here to ensure that the mutex is not |
| 622 | // destroyed before the thread list. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 623 | m_thread_list.Clear(); |
| 624 | } |
| 625 | |
| 626 | const ProcessPropertiesSP &Process::GetGlobalProperties() { |
| 627 | // NOTE: intentional leak so we don't crash if global destructor chain gets |
| 628 | // called as other threads still use the result of this function |
Pavel Labath | 5f05ea8 | 2016-10-19 15:12:45 +0000 | [diff] [blame] | 629 | static ProcessPropertiesSP *g_settings_sp_ptr = |
| 630 | new ProcessPropertiesSP(new ProcessProperties(nullptr)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 631 | return *g_settings_sp_ptr; |
| 632 | } |
| 633 | |
| 634 | void Process::Finalize() { |
| 635 | m_finalizing = true; |
| 636 | |
| 637 | // Destroy this process if needed |
| 638 | switch (GetPrivateState()) { |
| 639 | case eStateConnected: |
| 640 | case eStateAttaching: |
| 641 | case eStateLaunching: |
| 642 | case eStateStopped: |
| 643 | case eStateRunning: |
| 644 | case eStateStepping: |
| 645 | case eStateCrashed: |
| 646 | case eStateSuspended: |
| 647 | Destroy(false); |
| 648 | break; |
| 649 | |
| 650 | case eStateInvalid: |
| 651 | case eStateUnloaded: |
| 652 | case eStateDetached: |
| 653 | case eStateExited: |
| 654 | break; |
| 655 | } |
| 656 | |
| 657 | // Clear our broadcaster before we proceed with destroying |
| 658 | Broadcaster::Clear(); |
| 659 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 660 | // Do any cleanup needed prior to being destructed... Subclasses that |
| 661 | // override this method should call this superclass method as well. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 662 | |
| 663 | // We need to destroy the loader before the derived Process class gets |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 664 | // destroyed since it is very likely that undoing the loader will require |
| 665 | // access to the real process. |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 666 | m_dynamic_checkers_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 667 | m_abi_sp.reset(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 668 | m_os_up.reset(); |
| 669 | m_system_runtime_up.reset(); |
| 670 | m_dyld_up.reset(); |
| 671 | m_jit_loaders_up.reset(); |
Jim Ingham | 61e8e68 | 2020-03-10 16:18:11 -0700 | [diff] [blame] | 672 | m_thread_plans.Clear(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 673 | m_thread_list_real.Destroy(); |
| 674 | m_thread_list.Destroy(); |
| 675 | m_extended_thread_list.Destroy(); |
| 676 | m_queue_list.Clear(); |
| 677 | m_queue_list_stop_id = 0; |
| 678 | std::vector<Notifications> empty_notifications; |
| 679 | m_notifications.swap(empty_notifications); |
| 680 | m_image_tokens.clear(); |
| 681 | m_memory_cache.Clear(); |
| 682 | m_allocated_memory_cache.Clear(); |
Alex Langford | 74eb76f | 2019-05-22 23:01:18 +0000 | [diff] [blame] | 683 | { |
| 684 | std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex); |
| 685 | m_language_runtimes.clear(); |
| 686 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 687 | m_instrumentation_runtimes.clear(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 688 | m_next_event_action_up.reset(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 689 | // Clear the last natural stop ID since it has a strong reference to this |
| 690 | // process |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 691 | m_mod_id.SetStopEventForLastNaturalStopID(EventSP()); |
| 692 | //#ifdef LLDB_CONFIGURATION_DEBUG |
| 693 | // StreamFile s(stdout, false); |
| 694 | // EventSP event_sp; |
| 695 | // while (m_private_state_listener_sp->GetNextEvent(event_sp)) |
| 696 | // { |
| 697 | // event_sp->Dump (&s); |
| 698 | // s.EOL(); |
| 699 | // } |
| 700 | //#endif |
| 701 | // We have to be very careful here as the m_private_state_listener might |
| 702 | // contain events that have ProcessSP values in them which can keep this |
| 703 | // process around forever. These events need to be cleared out. |
| 704 | m_private_state_listener_sp->Clear(); |
| 705 | m_public_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 706 | m_public_run_lock.SetStopped(); |
| 707 | m_private_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 708 | m_private_run_lock.SetStopped(); |
Jason Molenda | 3826727 | 2016-12-16 02:48:39 +0000 | [diff] [blame] | 709 | m_structured_data_plugin_map.clear(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 710 | m_finalize_called = true; |
| 711 | } |
| 712 | |
| 713 | void Process::RegisterNotificationCallbacks(const Notifications &callbacks) { |
| 714 | m_notifications.push_back(callbacks); |
| 715 | if (callbacks.initialize != nullptr) |
| 716 | callbacks.initialize(callbacks.baton, this); |
| 717 | } |
| 718 | |
| 719 | bool Process::UnregisterNotificationCallbacks(const Notifications &callbacks) { |
| 720 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 721 | for (pos = m_notifications.begin(); pos != end; ++pos) { |
| 722 | if (pos->baton == callbacks.baton && |
| 723 | pos->initialize == callbacks.initialize && |
| 724 | pos->process_state_changed == callbacks.process_state_changed) { |
| 725 | m_notifications.erase(pos); |
| 726 | return true; |
| 727 | } |
| 728 | } |
| 729 | return false; |
| 730 | } |
| 731 | |
| 732 | void Process::SynchronouslyNotifyStateChanged(StateType state) { |
| 733 | std::vector<Notifications>::iterator notification_pos, |
| 734 | notification_end = m_notifications.end(); |
| 735 | for (notification_pos = m_notifications.begin(); |
| 736 | notification_pos != notification_end; ++notification_pos) { |
| 737 | if (notification_pos->process_state_changed) |
| 738 | notification_pos->process_state_changed(notification_pos->baton, this, |
| 739 | state); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | // FIXME: We need to do some work on events before the general Listener sees |
| 744 | // them. |
| 745 | // 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] | 746 | // we do the little "insert real insn, step & stop" trick. But we can't do |
| 747 | // that when the event is delivered by the broadcaster - since that is done on |
| 748 | // the thread that is waiting for new events, so if we needed more than one |
| 749 | // event for our handling, we would stall. So instead we do it when we fetch |
| 750 | // the event off of the queue. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 751 | // |
| 752 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 753 | StateType Process::GetNextEvent(EventSP &event_sp) { |
| 754 | StateType state = eStateInvalid; |
| 755 | |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 756 | if (m_listener_sp->GetEventForBroadcaster(this, event_sp, |
| 757 | std::chrono::seconds(0)) && |
| 758 | event_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 759 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 760 | |
| 761 | return state; |
| 762 | } |
| 763 | |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 764 | void Process::SyncIOHandler(uint32_t iohandler_id, |
| 765 | const Timeout<std::micro> &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 766 | // don't sync (potentially context switch) in case where there is no process |
| 767 | // IO |
| 768 | if (!m_process_input_reader) |
| 769 | return; |
| 770 | |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 771 | auto Result = m_iohandler_sync.WaitForValueNotEqualTo(iohandler_id, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 772 | |
| 773 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 774 | if (Result) { |
| 775 | LLDB_LOG( |
| 776 | log, |
| 777 | "waited from m_iohandler_sync to change from {0}. New value is {1}.", |
| 778 | iohandler_id, *Result); |
| 779 | } else { |
| 780 | LLDB_LOG(log, "timed out waiting for m_iohandler_sync to change from {0}.", |
| 781 | iohandler_id); |
| 782 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 785 | StateType Process::WaitForProcessToStop(const Timeout<std::micro> &timeout, |
| 786 | EventSP *event_sp_ptr, bool wait_always, |
| 787 | ListenerSP hijack_listener_sp, |
| 788 | Stream *stream, bool use_run_lock) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 789 | // 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] | 790 | // have restarted the target. We have to actually check each event, and in |
| 791 | // 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] | 792 | if (event_sp_ptr) |
| 793 | event_sp_ptr->reset(); |
| 794 | StateType state = GetState(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 795 | // If we are exited or detached, we won't ever get back to any other valid |
| 796 | // state... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 797 | if (state == eStateDetached || state == eStateExited) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 798 | return state; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 799 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 800 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 801 | LLDB_LOG(log, "timeout = {0}", timeout); |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 802 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 803 | if (!wait_always && StateIsStoppedState(state, true) && |
| 804 | StateIsStoppedState(GetPrivateState(), true)) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 805 | LLDB_LOGF(log, |
| 806 | "Process::%s returning without waiting for events; process " |
| 807 | "private and public states are already 'stopped'.", |
| 808 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 809 | // We need to toggle the run lock as this won't get done in |
| 810 | // SetPublicState() if the process is hijacked. |
| 811 | if (hijack_listener_sp && use_run_lock) |
| 812 | m_public_run_lock.SetStopped(); |
| 813 | return state; |
| 814 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 815 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 816 | while (state != eStateInvalid) { |
| 817 | EventSP event_sp; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 818 | state = GetStateChangedEvents(event_sp, timeout, hijack_listener_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 819 | if (event_sp_ptr && event_sp) |
| 820 | *event_sp_ptr = event_sp; |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 821 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 822 | bool pop_process_io_handler = (hijack_listener_sp.get() != nullptr); |
| 823 | Process::HandleProcessStateChangedEvent(event_sp, stream, |
| 824 | pop_process_io_handler); |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 825 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 826 | switch (state) { |
| 827 | case eStateCrashed: |
| 828 | case eStateDetached: |
| 829 | case eStateExited: |
| 830 | case eStateUnloaded: |
| 831 | // We need to toggle the run lock as this won't get done in |
| 832 | // SetPublicState() if the process is hijacked. |
| 833 | if (hijack_listener_sp && use_run_lock) |
| 834 | m_public_run_lock.SetStopped(); |
| 835 | return state; |
| 836 | case eStateStopped: |
| 837 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 838 | continue; |
| 839 | else { |
Pavel Labath | 78521ef | 2015-03-06 10:52:47 +0000 | [diff] [blame] | 840 | // We need to toggle the run lock as this won't get done in |
| 841 | // SetPublicState() if the process is hijacked. |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 842 | if (hijack_listener_sp && use_run_lock) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 843 | m_public_run_lock.SetStopped(); |
| 844 | return state; |
| 845 | } |
| 846 | default: |
| 847 | continue; |
| 848 | } |
| 849 | } |
| 850 | return state; |
| 851 | } |
| 852 | |
| 853 | bool Process::HandleProcessStateChangedEvent(const EventSP &event_sp, |
| 854 | Stream *stream, |
| 855 | bool &pop_process_io_handler) { |
| 856 | const bool handle_pop = pop_process_io_handler; |
| 857 | |
| 858 | pop_process_io_handler = false; |
| 859 | ProcessSP process_sp = |
| 860 | Process::ProcessEventData::GetProcessFromEvent(event_sp.get()); |
| 861 | |
| 862 | if (!process_sp) |
| 863 | return false; |
| 864 | |
| 865 | StateType event_state = |
| 866 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 867 | if (event_state == eStateInvalid) |
| 868 | return false; |
| 869 | |
| 870 | switch (event_state) { |
| 871 | case eStateInvalid: |
| 872 | case eStateUnloaded: |
| 873 | case eStateAttaching: |
| 874 | case eStateLaunching: |
| 875 | case eStateStepping: |
| 876 | case eStateDetached: |
| 877 | if (stream) |
| 878 | stream->Printf("Process %" PRIu64 " %s\n", process_sp->GetID(), |
| 879 | StateAsCString(event_state)); |
| 880 | if (event_state == eStateDetached) |
| 881 | pop_process_io_handler = true; |
| 882 | break; |
| 883 | |
| 884 | case eStateConnected: |
| 885 | case eStateRunning: |
| 886 | // Don't be chatty when we run... |
| 887 | break; |
| 888 | |
| 889 | case eStateExited: |
| 890 | if (stream) |
| 891 | process_sp->GetStatus(*stream); |
| 892 | pop_process_io_handler = true; |
| 893 | break; |
| 894 | |
| 895 | case eStateStopped: |
| 896 | case eStateCrashed: |
| 897 | case eStateSuspended: |
| 898 | // Make sure the program hasn't been auto-restarted: |
| 899 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) { |
| 900 | if (stream) { |
| 901 | size_t num_reasons = |
| 902 | Process::ProcessEventData::GetNumRestartedReasons(event_sp.get()); |
| 903 | if (num_reasons > 0) { |
| 904 | // FIXME: Do we want to report this, or would that just be annoyingly |
| 905 | // chatty? |
| 906 | if (num_reasons == 1) { |
| 907 | const char *reason = |
| 908 | Process::ProcessEventData::GetRestartedReasonAtIndex( |
| 909 | event_sp.get(), 0); |
| 910 | stream->Printf("Process %" PRIu64 " stopped and restarted: %s\n", |
| 911 | process_sp->GetID(), |
| 912 | reason ? reason : "<UNKNOWN REASON>"); |
| 913 | } else { |
| 914 | stream->Printf("Process %" PRIu64 |
| 915 | " stopped and restarted, reasons:\n", |
| 916 | process_sp->GetID()); |
| 917 | |
| 918 | for (size_t i = 0; i < num_reasons; i++) { |
| 919 | const char *reason = |
| 920 | Process::ProcessEventData::GetRestartedReasonAtIndex( |
| 921 | event_sp.get(), i); |
| 922 | stream->Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>"); |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | } else { |
| 928 | StopInfoSP curr_thread_stop_info_sp; |
| 929 | // Lock the thread list so it doesn't change on us, this is the scope for |
| 930 | // the locker: |
| 931 | { |
| 932 | ThreadList &thread_list = process_sp->GetThreadList(); |
| 933 | std::lock_guard<std::recursive_mutex> guard(thread_list.GetMutex()); |
| 934 | |
| 935 | ThreadSP curr_thread(thread_list.GetSelectedThread()); |
| 936 | ThreadSP thread; |
| 937 | StopReason curr_thread_stop_reason = eStopReasonInvalid; |
| 938 | if (curr_thread) { |
| 939 | curr_thread_stop_reason = curr_thread->GetStopReason(); |
| 940 | curr_thread_stop_info_sp = curr_thread->GetStopInfo(); |
| 941 | } |
| 942 | if (!curr_thread || !curr_thread->IsValid() || |
| 943 | curr_thread_stop_reason == eStopReasonInvalid || |
| 944 | curr_thread_stop_reason == eStopReasonNone) { |
| 945 | // Prefer a thread that has just completed its plan over another |
| 946 | // thread as current thread. |
| 947 | ThreadSP plan_thread; |
| 948 | ThreadSP other_thread; |
| 949 | |
| 950 | const size_t num_threads = thread_list.GetSize(); |
| 951 | size_t i; |
| 952 | for (i = 0; i < num_threads; ++i) { |
| 953 | thread = thread_list.GetThreadAtIndex(i); |
| 954 | StopReason thread_stop_reason = thread->GetStopReason(); |
| 955 | switch (thread_stop_reason) { |
| 956 | case eStopReasonInvalid: |
| 957 | case eStopReasonNone: |
| 958 | break; |
| 959 | |
| 960 | case eStopReasonSignal: { |
| 961 | // 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] | 962 | // that signal. We have to have had another reason for stopping |
| 963 | // here, and the user doesn't want to see this thread. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 964 | uint64_t signo = thread->GetStopInfo()->GetValue(); |
| 965 | if (process_sp->GetUnixSignals()->GetShouldStop(signo)) { |
| 966 | if (!other_thread) |
| 967 | other_thread = thread; |
| 968 | } |
| 969 | break; |
| 970 | } |
| 971 | case eStopReasonTrace: |
| 972 | case eStopReasonBreakpoint: |
| 973 | case eStopReasonWatchpoint: |
| 974 | case eStopReasonException: |
| 975 | case eStopReasonExec: |
| 976 | case eStopReasonThreadExiting: |
| 977 | case eStopReasonInstrumentation: |
| 978 | if (!other_thread) |
| 979 | other_thread = thread; |
| 980 | break; |
| 981 | case eStopReasonPlanComplete: |
| 982 | if (!plan_thread) |
| 983 | plan_thread = thread; |
| 984 | break; |
| 985 | } |
| 986 | } |
| 987 | if (plan_thread) |
| 988 | thread_list.SetSelectedThreadByID(plan_thread->GetID()); |
| 989 | else if (other_thread) |
| 990 | thread_list.SetSelectedThreadByID(other_thread->GetID()); |
| 991 | else { |
| 992 | if (curr_thread && curr_thread->IsValid()) |
| 993 | thread = curr_thread; |
| 994 | else |
| 995 | thread = thread_list.GetThreadAtIndex(0); |
| 996 | |
| 997 | if (thread) |
| 998 | thread_list.SetSelectedThreadByID(thread->GetID()); |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | // Drop the ThreadList mutex by here, since GetThreadStatus below might |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1003 | // have to run code, e.g. for Data formatters, and if we hold the |
| 1004 | // ThreadList mutex, then the process is going to have a hard time |
| 1005 | // restarting the process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1006 | if (stream) { |
| 1007 | Debugger &debugger = process_sp->GetTarget().GetDebugger(); |
| 1008 | if (debugger.GetTargetList().GetSelectedTarget().get() == |
| 1009 | &process_sp->GetTarget()) { |
Med Ismail Bennani | 7ebe9cc | 2020-02-04 17:53:26 +0100 | [diff] [blame] | 1010 | ThreadSP thread_sp = process_sp->GetThreadList().GetSelectedThread(); |
| 1011 | |
| 1012 | if (!thread_sp || !thread_sp->IsValid()) |
| 1013 | return false; |
| 1014 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1015 | const bool only_threads_with_stop_reason = true; |
Med Ismail Bennani | 7ebe9cc | 2020-02-04 17:53:26 +0100 | [diff] [blame] | 1016 | const uint32_t start_frame = thread_sp->GetSelectedFrameIndex(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1017 | const uint32_t num_frames = 1; |
| 1018 | const uint32_t num_frames_with_source = 1; |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 1019 | const bool stop_format = true; |
Med Ismail Bennani | 7ebe9cc | 2020-02-04 17:53:26 +0100 | [diff] [blame] | 1020 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1021 | process_sp->GetStatus(*stream); |
| 1022 | process_sp->GetThreadStatus(*stream, only_threads_with_stop_reason, |
| 1023 | start_frame, num_frames, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 1024 | num_frames_with_source, |
| 1025 | stop_format); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1026 | if (curr_thread_stop_info_sp) { |
| 1027 | lldb::addr_t crashing_address; |
| 1028 | ValueObjectSP valobj_sp = StopInfo::GetCrashingDereference( |
| 1029 | curr_thread_stop_info_sp, &crashing_address); |
| 1030 | if (valobj_sp) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1031 | const ValueObject::GetExpressionPathFormat format = |
| 1032 | ValueObject::GetExpressionPathFormat:: |
| 1033 | eGetExpressionPathFormatHonorPointers; |
| 1034 | stream->PutCString("Likely cause: "); |
Alex Langford | 3014efe | 2020-02-02 14:17:02 -0800 | [diff] [blame] | 1035 | valobj_sp->GetExpressionPath(*stream, format); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1036 | stream->Printf(" accessed 0x%" PRIx64 "\n", crashing_address); |
| 1037 | } |
| 1038 | } |
| 1039 | } else { |
| 1040 | uint32_t target_idx = debugger.GetTargetList().GetIndexOfTarget( |
| 1041 | process_sp->GetTarget().shared_from_this()); |
| 1042 | if (target_idx != UINT32_MAX) |
| 1043 | stream->Printf("Target %d: (", target_idx); |
| 1044 | else |
| 1045 | stream->Printf("Target <unknown index>: ("); |
| 1046 | process_sp->GetTarget().Dump(stream, eDescriptionLevelBrief); |
| 1047 | stream->Printf(") stopped.\n"); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | // Pop the process IO handler |
| 1052 | pop_process_io_handler = true; |
| 1053 | } |
| 1054 | break; |
| 1055 | } |
| 1056 | |
| 1057 | if (handle_pop && pop_process_io_handler) |
| 1058 | process_sp->PopProcessIOHandler(); |
| 1059 | |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1063 | bool Process::HijackProcessEvents(ListenerSP listener_sp) { |
| 1064 | if (listener_sp) { |
| 1065 | return HijackBroadcaster(listener_sp, eBroadcastBitStateChanged | |
| 1066 | eBroadcastBitInterrupt); |
| 1067 | } else |
| 1068 | return false; |
| 1069 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1070 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1071 | void Process::RestoreProcessEvents() { RestoreBroadcaster(); } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1072 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1073 | StateType Process::GetStateChangedEvents(EventSP &event_sp, |
| 1074 | const Timeout<std::micro> &timeout, |
| 1075 | ListenerSP hijack_listener_sp) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1076 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1077 | LLDB_LOG(log, "timeout = {0}, event_sp)...", timeout); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1078 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1079 | ListenerSP listener_sp = hijack_listener_sp; |
| 1080 | if (!listener_sp) |
| 1081 | listener_sp = m_listener_sp; |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1082 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1083 | StateType state = eStateInvalid; |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 1084 | if (listener_sp->GetEventForBroadcasterWithType( |
| 1085 | this, eBroadcastBitStateChanged | eBroadcastBitInterrupt, event_sp, |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1086 | timeout)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1087 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1088 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1089 | else |
| 1090 | LLDB_LOG(log, "got no event or was interrupted."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1091 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1092 | |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1093 | LLDB_LOG(log, "timeout = {0}, event_sp) => {1}", timeout, state); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1094 | return state; |
| 1095 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1096 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1097 | Event *Process::PeekAtStateChangedEvents() { |
| 1098 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1099 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1100 | LLDB_LOGF(log, "Process::%s...", __FUNCTION__); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1102 | Event *event_ptr; |
| 1103 | event_ptr = m_listener_sp->PeekAtNextEventForBroadcasterWithType( |
| 1104 | this, eBroadcastBitStateChanged); |
| 1105 | if (log) { |
| 1106 | if (event_ptr) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1107 | LLDB_LOGF(log, "Process::%s (event_ptr) => %s", __FUNCTION__, |
| 1108 | StateAsCString(ProcessEventData::GetStateFromEvent(event_ptr))); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1109 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1110 | LLDB_LOGF(log, "Process::%s no events found", __FUNCTION__); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1111 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1112 | } |
| 1113 | return event_ptr; |
| 1114 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1115 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1116 | StateType |
| 1117 | Process::GetStateChangedEventsPrivate(EventSP &event_sp, |
| 1118 | const Timeout<std::micro> &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1119 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1120 | LLDB_LOG(log, "timeout = {0}, event_sp)...", timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1121 | |
| 1122 | StateType state = eStateInvalid; |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 1123 | if (m_private_state_listener_sp->GetEventForBroadcasterWithType( |
| 1124 | &m_private_state_broadcaster, |
| 1125 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, event_sp, |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1126 | timeout)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1127 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1128 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1129 | |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1130 | LLDB_LOG(log, "timeout = {0}, event_sp) => {1}", timeout, |
| 1131 | state == eStateInvalid ? "TIMEOUT" : StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1132 | return state; |
| 1133 | } |
| 1134 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1135 | bool Process::GetEventsPrivate(EventSP &event_sp, |
| 1136 | const Timeout<std::micro> &timeout, |
| 1137 | bool control_only) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1138 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1139 | LLDB_LOG(log, "timeout = {0}, event_sp)...", timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1140 | |
| 1141 | if (control_only) |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 1142 | return m_private_state_listener_sp->GetEventForBroadcaster( |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1143 | &m_private_state_control_broadcaster, event_sp, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1144 | else |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1145 | return m_private_state_listener_sp->GetEvent(event_sp, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | bool Process::IsRunning() const { |
| 1149 | return StateIsRunningState(m_public_state.GetValue()); |
| 1150 | } |
| 1151 | |
| 1152 | int Process::GetExitStatus() { |
| 1153 | std::lock_guard<std::mutex> guard(m_exit_status_mutex); |
| 1154 | |
| 1155 | if (m_public_state.GetValue() == eStateExited) |
| 1156 | return m_exit_status; |
| 1157 | return -1; |
| 1158 | } |
| 1159 | |
| 1160 | const char *Process::GetExitDescription() { |
| 1161 | std::lock_guard<std::mutex> guard(m_exit_status_mutex); |
| 1162 | |
| 1163 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 1164 | return m_exit_string.c_str(); |
| 1165 | return nullptr; |
| 1166 | } |
| 1167 | |
| 1168 | bool Process::SetExitStatus(int status, const char *cstr) { |
| 1169 | // Use a mutex to protect setting the exit status. |
| 1170 | std::lock_guard<std::mutex> guard(m_exit_status_mutex); |
| 1171 | |
| 1172 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1173 | LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1174 | LLDB_LOGF( |
| 1175 | log, "Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)", |
| 1176 | status, status, cstr ? "\"" : "", cstr ? cstr : "NULL", cstr ? "\"" : ""); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1177 | |
| 1178 | // We were already in the exited state |
| 1179 | if (m_private_state.GetValue() == eStateExited) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1180 | LLDB_LOGF(log, "Process::SetExitStatus () ignoring exit status because " |
| 1181 | "state was already set to eStateExited"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1182 | return false; |
| 1183 | } |
| 1184 | |
| 1185 | m_exit_status = status; |
| 1186 | if (cstr) |
| 1187 | m_exit_string = cstr; |
| 1188 | else |
| 1189 | m_exit_string.clear(); |
| 1190 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1191 | // Clear the last natural stop ID since it has a strong reference to this |
| 1192 | // process |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1193 | m_mod_id.SetStopEventForLastNaturalStopID(EventSP()); |
| 1194 | |
| 1195 | SetPrivateState(eStateExited); |
| 1196 | |
| 1197 | // Allow subclasses to do some cleanup |
| 1198 | DidExit(); |
| 1199 | |
| 1200 | return true; |
| 1201 | } |
| 1202 | |
| 1203 | bool Process::IsAlive() { |
| 1204 | switch (m_private_state.GetValue()) { |
| 1205 | case eStateConnected: |
| 1206 | case eStateAttaching: |
| 1207 | case eStateLaunching: |
| 1208 | case eStateStopped: |
| 1209 | case eStateRunning: |
| 1210 | case eStateStepping: |
| 1211 | case eStateCrashed: |
| 1212 | case eStateSuspended: |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1213 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1214 | default: |
| 1215 | return false; |
| 1216 | } |
Jason Molenda | a814f70 | 2015-11-05 23:03:44 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1219 | // This static callback can be used to watch for local child processes on the |
| 1220 | // current host. The child process exits, the process will be found in the |
| 1221 | // global target list (we want to be completely sure that the |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1222 | // 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] | 1223 | bool Process::SetProcessExitStatus( |
| 1224 | lldb::pid_t pid, bool exited, |
| 1225 | int signo, // Zero for no signal |
| 1226 | int exit_status // Exit value of process if signal is zero |
| 1227 | ) { |
| 1228 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1229 | LLDB_LOGF(log, |
| 1230 | "Process::SetProcessExitStatus (pid=%" PRIu64 |
| 1231 | ", exited=%i, signal=%i, exit_status=%i)\n", |
| 1232 | pid, exited, signo, exit_status); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1233 | |
| 1234 | if (exited) { |
| 1235 | TargetSP target_sp(Debugger::FindTargetWithProcessID(pid)); |
| 1236 | if (target_sp) { |
| 1237 | ProcessSP process_sp(target_sp->GetProcessSP()); |
| 1238 | if (process_sp) { |
| 1239 | const char *signal_cstr = nullptr; |
| 1240 | if (signo) |
| 1241 | signal_cstr = process_sp->GetUnixSignals()->GetSignalAsCString(signo); |
| 1242 | |
| 1243 | process_sp->SetExitStatus(exit_status, signal_cstr); |
| 1244 | } |
| 1245 | } |
| 1246 | return true; |
| 1247 | } |
| 1248 | return false; |
| 1249 | } |
| 1250 | |
| 1251 | void Process::UpdateThreadListIfNeeded() { |
| 1252 | const uint32_t stop_id = GetStopID(); |
| 1253 | if (m_thread_list.GetSize(false) == 0 || |
| 1254 | stop_id != m_thread_list.GetStopID()) { |
Jim Ingham | 1893065 | 2020-03-18 12:05:08 -0700 | [diff] [blame] | 1255 | bool clear_unused_threads = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1256 | const StateType state = GetPrivateState(); |
| 1257 | if (StateIsStoppedState(state, true)) { |
| 1258 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
Jim Ingham | 1893065 | 2020-03-18 12:05:08 -0700 | [diff] [blame] | 1259 | m_thread_list.SetStopID(stop_id); |
| 1260 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1261 | // m_thread_list does have its own mutex, but we need to hold onto the |
| 1262 | // mutex between the call to UpdateThreadList(...) and the |
| 1263 | // os->UpdateThreadList(...) so it doesn't change on us |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1264 | ThreadList &old_thread_list = m_thread_list; |
| 1265 | ThreadList real_thread_list(this); |
| 1266 | ThreadList new_thread_list(this); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1267 | // Always update the thread list with the protocol specific thread list, |
| 1268 | // but only update if "true" is returned |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1269 | if (UpdateThreadList(m_thread_list_real, real_thread_list)) { |
| 1270 | // 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] | 1271 | // are shutting down, since that may call back into the SBAPI's, |
| 1272 | // requiring the API lock which is already held by whoever is shutting |
| 1273 | // us down, causing a deadlock. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1274 | OperatingSystem *os = GetOperatingSystem(); |
| 1275 | if (os && !m_destroy_in_process) { |
| 1276 | // Clear any old backing threads where memory threads might have been |
| 1277 | // backed by actual threads from the lldb_private::Process subclass |
| 1278 | size_t num_old_threads = old_thread_list.GetSize(false); |
| 1279 | for (size_t i = 0; i < num_old_threads; ++i) |
| 1280 | old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread(); |
Jim Ingham | 1893065 | 2020-03-18 12:05:08 -0700 | [diff] [blame] | 1281 | // See if the OS plugin reports all threads. If it does, then |
Jonas Devlieghere | 32d35fb | 2020-07-13 13:44:01 -0700 | [diff] [blame] | 1282 | // it is safe to clear unseen thread's plans here. Otherwise we |
Jim Ingham | 1893065 | 2020-03-18 12:05:08 -0700 | [diff] [blame] | 1283 | // should preserve them in case they show up again: |
Jonas Devlieghere | 018e5a9 | 2020-05-19 11:16:57 -0700 | [diff] [blame] | 1284 | clear_unused_threads = GetOSPluginReportsAllThreads(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1285 | |
| 1286 | // Turn off dynamic types to ensure we don't run any expressions. |
Adrian Prantl | 4e8be2c | 2018-06-13 16:21:24 +0000 | [diff] [blame] | 1287 | // 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] | 1288 | // dynamic type or not and we need to avoid this. OperatingSystem |
| 1289 | // plug-ins can't run expressions that require running code... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1290 | |
| 1291 | Target &target = GetTarget(); |
| 1292 | const lldb::DynamicValueType saved_prefer_dynamic = |
| 1293 | target.GetPreferDynamicValue(); |
| 1294 | if (saved_prefer_dynamic != lldb::eNoDynamicValues) |
| 1295 | target.SetPreferDynamicValue(lldb::eNoDynamicValues); |
| 1296 | |
| 1297 | // Now let the OperatingSystem plug-in update the thread list |
| 1298 | |
| 1299 | os->UpdateThreadList( |
| 1300 | old_thread_list, // Old list full of threads created by OS plug-in |
| 1301 | real_thread_list, // The actual thread list full of threads |
| 1302 | // created by each lldb_private::Process |
| 1303 | // subclass |
| 1304 | new_thread_list); // The new thread list that we will show to the |
| 1305 | // user that gets filled in |
| 1306 | |
| 1307 | if (saved_prefer_dynamic != lldb::eNoDynamicValues) |
| 1308 | target.SetPreferDynamicValue(saved_prefer_dynamic); |
| 1309 | } else { |
| 1310 | // No OS plug-in, the new thread list is the same as the real thread |
Jim Ingham | 1893065 | 2020-03-18 12:05:08 -0700 | [diff] [blame] | 1311 | // list. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1312 | new_thread_list = real_thread_list; |
| 1313 | } |
| 1314 | |
| 1315 | m_thread_list_real.Update(real_thread_list); |
| 1316 | m_thread_list.Update(new_thread_list); |
| 1317 | m_thread_list.SetStopID(stop_id); |
| 1318 | |
| 1319 | if (GetLastNaturalStopID() != m_extended_thread_stop_id) { |
| 1320 | // Clear any extended threads that we may have accumulated previously |
| 1321 | m_extended_thread_list.Clear(); |
| 1322 | m_extended_thread_stop_id = GetLastNaturalStopID(); |
| 1323 | |
| 1324 | m_queue_list.Clear(); |
| 1325 | m_queue_list_stop_id = GetLastNaturalStopID(); |
| 1326 | } |
| 1327 | } |
Jim Ingham | 1893065 | 2020-03-18 12:05:08 -0700 | [diff] [blame] | 1328 | // Now update the plan stack map. |
| 1329 | // If we do have an OS plugin, any absent real threads in the |
| 1330 | // m_thread_list have already been removed from the ThreadPlanStackMap. |
| 1331 | // So any remaining threads are OS Plugin threads, and those we want to |
| 1332 | // preserve in case they show up again. |
| 1333 | m_thread_plans.Update(m_thread_list, clear_unused_threads); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | |
Jim Ingham | 61e8e68 | 2020-03-10 16:18:11 -0700 | [diff] [blame] | 1338 | ThreadPlanStack *Process::FindThreadPlans(lldb::tid_t tid) { |
| 1339 | return m_thread_plans.Find(tid); |
| 1340 | } |
| 1341 | |
Jim Ingham | 1893065 | 2020-03-18 12:05:08 -0700 | [diff] [blame] | 1342 | bool Process::PruneThreadPlansForTID(lldb::tid_t tid) { |
| 1343 | return m_thread_plans.PrunePlansForTID(tid); |
Jim Ingham | 61e8e68 | 2020-03-10 16:18:11 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Jim Ingham | 1893065 | 2020-03-18 12:05:08 -0700 | [diff] [blame] | 1346 | void Process::PruneThreadPlans() { |
| 1347 | m_thread_plans.Update(GetThreadList(), true, false); |
| 1348 | } |
| 1349 | |
| 1350 | bool Process::DumpThreadPlansForTID(Stream &strm, lldb::tid_t tid, |
| 1351 | lldb::DescriptionLevel desc_level, |
| 1352 | bool internal, bool condense_trivial, |
| 1353 | bool skip_unreported_plans) { |
| 1354 | return m_thread_plans.DumpPlansForTID( |
| 1355 | strm, tid, desc_level, internal, condense_trivial, skip_unreported_plans); |
| 1356 | } |
| 1357 | void Process::DumpThreadPlans(Stream &strm, lldb::DescriptionLevel desc_level, |
| 1358 | bool internal, bool condense_trivial, |
| 1359 | bool skip_unreported_plans) { |
| 1360 | m_thread_plans.DumpPlans(strm, desc_level, internal, condense_trivial, |
| 1361 | skip_unreported_plans); |
Jim Ingham | 61e8e68 | 2020-03-10 16:18:11 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1364 | void Process::UpdateQueueListIfNeeded() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1365 | if (m_system_runtime_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1366 | if (m_queue_list.GetSize() == 0 || |
| 1367 | m_queue_list_stop_id != GetLastNaturalStopID()) { |
| 1368 | const StateType state = GetPrivateState(); |
| 1369 | if (StateIsStoppedState(state, true)) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1370 | m_system_runtime_up->PopulateQueueList(m_queue_list); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1371 | m_queue_list_stop_id = GetLastNaturalStopID(); |
| 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | ThreadSP Process::CreateOSPluginThread(lldb::tid_t tid, lldb::addr_t context) { |
| 1378 | OperatingSystem *os = GetOperatingSystem(); |
| 1379 | if (os) |
| 1380 | return os->CreateThread(tid, context); |
| 1381 | return ThreadSP(); |
| 1382 | } |
| 1383 | |
| 1384 | uint32_t Process::GetNextThreadIndexID(uint64_t thread_id) { |
| 1385 | return AssignIndexIDToThread(thread_id); |
| 1386 | } |
| 1387 | |
| 1388 | bool Process::HasAssignedIndexIDToThread(uint64_t thread_id) { |
| 1389 | return (m_thread_id_to_index_id_map.find(thread_id) != |
| 1390 | m_thread_id_to_index_id_map.end()); |
| 1391 | } |
| 1392 | |
| 1393 | uint32_t Process::AssignIndexIDToThread(uint64_t thread_id) { |
| 1394 | uint32_t result = 0; |
| 1395 | std::map<uint64_t, uint32_t>::iterator iterator = |
| 1396 | m_thread_id_to_index_id_map.find(thread_id); |
| 1397 | if (iterator == m_thread_id_to_index_id_map.end()) { |
| 1398 | result = ++m_thread_index_id; |
| 1399 | m_thread_id_to_index_id_map[thread_id] = result; |
| 1400 | } else { |
| 1401 | result = iterator->second; |
| 1402 | } |
| 1403 | |
| 1404 | return result; |
| 1405 | } |
| 1406 | |
| 1407 | StateType Process::GetState() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1408 | return m_public_state.GetValue(); |
| 1409 | } |
| 1410 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1411 | void Process::SetPublicState(StateType new_state, bool restarted) { |
| 1412 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1413 | LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1414 | LLDB_LOGF(log, "Process::SetPublicState (state = %s, restarted = %i)", |
| 1415 | StateAsCString(new_state), restarted); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1416 | const StateType old_state = m_public_state.GetValue(); |
| 1417 | m_public_state.SetValue(new_state); |
| 1418 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1419 | // On the transition from Run to Stopped, we unlock the writer end of the run |
| 1420 | // lock. The lock gets locked in Resume, which is the public API to tell the |
| 1421 | // program to run. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1422 | if (!StateChangedIsExternallyHijacked()) { |
| 1423 | if (new_state == eStateDetached) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1424 | LLDB_LOGF(log, |
| 1425 | "Process::SetPublicState (%s) -- unlocking run lock for detach", |
| 1426 | StateAsCString(new_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1427 | m_public_run_lock.SetStopped(); |
| 1428 | } else { |
| 1429 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1430 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1431 | if ((old_state_is_stopped != new_state_is_stopped)) { |
| 1432 | if (new_state_is_stopped && !restarted) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1433 | LLDB_LOGF(log, "Process::SetPublicState (%s) -- unlocking run lock", |
| 1434 | StateAsCString(new_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1435 | m_public_run_lock.SetStopped(); |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1442 | Status Process::Resume() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1443 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1444 | LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1445 | LLDB_LOGF(log, "Process::Resume -- locking run lock"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1446 | if (!m_public_run_lock.TrySetRunning()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1447 | Status error("Resume request failed - process still running."); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1448 | LLDB_LOGF(log, "Process::Resume: -- TrySetRunning failed, not resuming."); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1449 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1450 | } |
Adrian McCarthy | 3887ba8 | 2017-09-19 18:07:33 +0000 | [diff] [blame] | 1451 | Status error = PrivateResume(); |
| 1452 | if (!error.Success()) { |
| 1453 | // Undo running state change |
| 1454 | m_public_run_lock.SetStopped(); |
| 1455 | } |
| 1456 | return error; |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Jim Ingham | 3139fc9 | 2019-03-01 18:13:38 +0000 | [diff] [blame] | 1459 | static const char *g_resume_sync_name = "lldb.Process.ResumeSynchronous.hijack"; |
| 1460 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1461 | Status Process::ResumeSynchronous(Stream *stream) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1462 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1463 | LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1464 | LLDB_LOGF(log, "Process::ResumeSynchronous -- locking run lock"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1465 | if (!m_public_run_lock.TrySetRunning()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1466 | Status error("Resume request failed - process still running."); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1467 | LLDB_LOGF(log, "Process::Resume: -- TrySetRunning failed, not resuming."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1468 | return error; |
| 1469 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1470 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1471 | ListenerSP listener_sp( |
Jim Ingham | 3139fc9 | 2019-03-01 18:13:38 +0000 | [diff] [blame] | 1472 | Listener::MakeListener(g_resume_sync_name)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1473 | HijackProcessEvents(listener_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1474 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1475 | Status error = PrivateResume(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1476 | if (error.Success()) { |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1477 | StateType state = |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 1478 | WaitForProcessToStop(llvm::None, nullptr, true, listener_sp, stream); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1479 | const bool must_be_alive = |
| 1480 | false; // eStateExited is ok, so this must be false |
| 1481 | if (!StateIsStoppedState(state, must_be_alive)) |
| 1482 | error.SetErrorStringWithFormat( |
| 1483 | "process not in stopped state after synchronous resume: %s", |
| 1484 | StateAsCString(state)); |
Adrian McCarthy | 3887ba8 | 2017-09-19 18:07:33 +0000 | [diff] [blame] | 1485 | } else { |
| 1486 | // Undo running state change |
| 1487 | m_public_run_lock.SetStopped(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1488 | } |
Ed Maste | c29693f | 2013-07-02 16:35:47 +0000 | [diff] [blame] | 1489 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1490 | // Undo the hijacking of process events... |
| 1491 | RestoreProcessEvents(); |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1492 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1493 | return error; |
| 1494 | } |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1495 | |
Jim Ingham | 3139fc9 | 2019-03-01 18:13:38 +0000 | [diff] [blame] | 1496 | bool Process::StateChangedIsExternallyHijacked() { |
| 1497 | if (IsHijackedForEvent(eBroadcastBitStateChanged)) { |
| 1498 | const char *hijacking_name = GetHijackingListenerName(); |
| 1499 | if (hijacking_name && |
| 1500 | strcmp(hijacking_name, g_resume_sync_name)) |
| 1501 | return true; |
| 1502 | } |
| 1503 | return false; |
| 1504 | } |
| 1505 | |
| 1506 | bool Process::StateChangedIsHijackedForSynchronousResume() { |
| 1507 | if (IsHijackedForEvent(eBroadcastBitStateChanged)) { |
| 1508 | const char *hijacking_name = GetHijackingListenerName(); |
| 1509 | if (hijacking_name && |
| 1510 | strcmp(hijacking_name, g_resume_sync_name) == 0) |
| 1511 | return true; |
| 1512 | } |
| 1513 | return false; |
| 1514 | } |
| 1515 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1516 | StateType Process::GetPrivateState() { return m_private_state.GetValue(); } |
Ilia K | 38810f4 | 2015-05-20 10:15:47 +0000 | [diff] [blame] | 1517 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1518 | void Process::SetPrivateState(StateType new_state) { |
| 1519 | if (m_finalize_called) |
| 1520 | return; |
| 1521 | |
| 1522 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1523 | LIBLLDB_LOG_PROCESS)); |
| 1524 | bool state_changed = false; |
| 1525 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1526 | LLDB_LOGF(log, "Process::SetPrivateState (%s)", StateAsCString(new_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1527 | |
| 1528 | std::lock_guard<std::recursive_mutex> thread_guard(m_thread_list.GetMutex()); |
| 1529 | std::lock_guard<std::recursive_mutex> guard(m_private_state.GetMutex()); |
| 1530 | |
| 1531 | const StateType old_state = m_private_state.GetValueNoLock(); |
| 1532 | state_changed = old_state != new_state; |
| 1533 | |
| 1534 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1535 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1536 | if (old_state_is_stopped != new_state_is_stopped) { |
| 1537 | if (new_state_is_stopped) |
| 1538 | m_private_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1539 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1540 | m_private_run_lock.SetRunning(); |
| 1541 | } |
| 1542 | |
| 1543 | if (state_changed) { |
| 1544 | m_private_state.SetValueNoLock(new_state); |
| 1545 | EventSP event_sp( |
| 1546 | new Event(eBroadcastBitStateChanged, |
| 1547 | new ProcessEventData(shared_from_this(), new_state))); |
| 1548 | if (StateIsStoppedState(new_state, false)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1549 | // Note, this currently assumes that all threads in the list stop when |
| 1550 | // the process stops. In the future we will want to support a debugging |
| 1551 | // model where some threads continue to run while others are stopped. |
| 1552 | // When that happens we will either need a way for the thread list to |
| 1553 | // identify which threads are stopping or create a special thread list |
| 1554 | // containing only threads which actually stopped. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1555 | // |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1556 | // The process plugin is responsible for managing the actual behavior of |
| 1557 | // the threads and should have stopped any threads that are going to stop |
| 1558 | // before we get here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1559 | m_thread_list.DidStop(); |
| 1560 | |
| 1561 | m_mod_id.BumpStopID(); |
| 1562 | if (!m_mod_id.IsLastResumeForUserExpression()) |
| 1563 | m_mod_id.SetStopEventForLastNaturalStopID(event_sp); |
| 1564 | m_memory_cache.Clear(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1565 | LLDB_LOGF(log, "Process::SetPrivateState (%s) stop_id = %u", |
| 1566 | StateAsCString(new_state), m_mod_id.GetStopID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1567 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1568 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1569 | // Use our target to get a shared pointer to ourselves... |
| 1570 | if (m_finalize_called && !PrivateStateThreadIsValid()) |
| 1571 | BroadcastEvent(event_sp); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1572 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1573 | m_private_state_broadcaster.BroadcastEvent(event_sp); |
| 1574 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1575 | LLDB_LOGF(log, |
| 1576 | "Process::SetPrivateState (%s) state didn't change. Ignoring...", |
| 1577 | StateAsCString(new_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1578 | } |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1581 | void Process::SetRunningUserExpression(bool on) { |
| 1582 | m_mod_id.SetRunningUserExpression(on); |
| 1583 | } |
| 1584 | |
Raphael Isemann | c01783a | 2018-08-29 22:50:54 +0000 | [diff] [blame] | 1585 | void Process::SetRunningUtilityFunction(bool on) { |
| 1586 | m_mod_id.SetRunningUtilityFunction(on); |
| 1587 | } |
| 1588 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1589 | addr_t Process::GetImageInfoAddress() { return LLDB_INVALID_ADDRESS; } |
| 1590 | |
| 1591 | const lldb::ABISP &Process::GetABI() { |
| 1592 | if (!m_abi_sp) |
Jason Molenda | 43294c9 | 2017-06-29 02:57:03 +0000 | [diff] [blame] | 1593 | m_abi_sp = ABI::FindPlugin(shared_from_this(), GetTarget().GetArchitecture()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1594 | return m_abi_sp; |
| 1595 | } |
| 1596 | |
Davide Italiano | cec8263 | 2019-12-03 16:54:04 -0800 | [diff] [blame] | 1597 | std::vector<LanguageRuntime *> Process::GetLanguageRuntimes() { |
Alex Langford | 03e1a82 | 2019-05-29 18:08:22 +0000 | [diff] [blame] | 1598 | std::vector<LanguageRuntime *> language_runtimes; |
| 1599 | |
| 1600 | if (m_finalizing) |
| 1601 | return language_runtimes; |
| 1602 | |
| 1603 | std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex); |
| 1604 | // Before we pass off a copy of the language runtimes, we must make sure that |
| 1605 | // our collection is properly populated. It's possible that some of the |
| 1606 | // language runtimes were not loaded yet, either because nobody requested it |
| 1607 | // yet or the proper condition for loading wasn't yet met (e.g. libc++.so |
| 1608 | // hadn't been loaded). |
| 1609 | for (const lldb::LanguageType lang_type : Language::GetSupportedLanguages()) { |
Davide Italiano | cec8263 | 2019-12-03 16:54:04 -0800 | [diff] [blame] | 1610 | if (LanguageRuntime *runtime = GetLanguageRuntime(lang_type)) |
Alex Langford | 03e1a82 | 2019-05-29 18:08:22 +0000 | [diff] [blame] | 1611 | language_runtimes.emplace_back(runtime); |
| 1612 | } |
| 1613 | |
| 1614 | return language_runtimes; |
| 1615 | } |
| 1616 | |
Davide Italiano | cec8263 | 2019-12-03 16:54:04 -0800 | [diff] [blame] | 1617 | LanguageRuntime *Process::GetLanguageRuntime(lldb::LanguageType language) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1618 | if (m_finalizing) |
Eugene Zelenko | da8cf8a | 2016-03-01 00:55:51 +0000 | [diff] [blame] | 1619 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1620 | |
Alex Langford | 7d3e97f | 2019-05-29 21:07:53 +0000 | [diff] [blame] | 1621 | LanguageRuntime *runtime = nullptr; |
| 1622 | |
Alex Langford | 74eb76f | 2019-05-22 23:01:18 +0000 | [diff] [blame] | 1623 | std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1624 | LanguageRuntimeCollection::iterator pos; |
| 1625 | pos = m_language_runtimes.find(language); |
Davide Italiano | cec8263 | 2019-12-03 16:54:04 -0800 | [diff] [blame] | 1626 | if (pos == m_language_runtimes.end() || !pos->second) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1627 | lldb::LanguageRuntimeSP runtime_sp( |
| 1628 | LanguageRuntime::FindPlugin(this, language)); |
| 1629 | |
| 1630 | m_language_runtimes[language] = runtime_sp; |
Alex Langford | 7d3e97f | 2019-05-29 21:07:53 +0000 | [diff] [blame] | 1631 | runtime = runtime_sp.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1632 | } else |
Alex Langford | 7d3e97f | 2019-05-29 21:07:53 +0000 | [diff] [blame] | 1633 | runtime = pos->second.get(); |
| 1634 | |
| 1635 | if (runtime) |
| 1636 | // It's possible that a language runtime can support multiple LanguageTypes, |
| 1637 | // for example, CPPLanguageRuntime will support eLanguageTypeC_plus_plus, |
| 1638 | // eLanguageTypeC_plus_plus_03, etc. Because of this, we should get the |
| 1639 | // primary language type and make sure that our runtime supports it. |
| 1640 | assert(runtime->GetLanguageType() == Language::GetPrimaryLanguage(language)); |
| 1641 | |
| 1642 | return runtime; |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1645 | bool Process::IsPossibleDynamicValue(ValueObject &in_value) { |
| 1646 | if (m_finalizing) |
| 1647 | return false; |
| 1648 | |
| 1649 | if (in_value.IsDynamic()) |
| 1650 | return false; |
| 1651 | LanguageType known_type = in_value.GetObjectRuntimeLanguage(); |
| 1652 | |
| 1653 | if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC) { |
| 1654 | LanguageRuntime *runtime = GetLanguageRuntime(known_type); |
| 1655 | return runtime ? runtime->CouldHaveDynamicValue(in_value) : false; |
| 1656 | } |
| 1657 | |
Alex Langford | 41dc552 | 2019-05-30 21:03:53 +0000 | [diff] [blame] | 1658 | for (LanguageRuntime *runtime : GetLanguageRuntimes()) { |
| 1659 | if (runtime->CouldHaveDynamicValue(in_value)) |
| 1660 | return true; |
| 1661 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1662 | |
Alex Langford | 41dc552 | 2019-05-30 21:03:53 +0000 | [diff] [blame] | 1663 | return false; |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1666 | void Process::SetDynamicCheckers(DynamicCheckerFunctions *dynamic_checkers) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1667 | m_dynamic_checkers_up.reset(dynamic_checkers); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1670 | BreakpointSiteList &Process::GetBreakpointSiteList() { |
| 1671 | return m_breakpoint_site_list; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1674 | const BreakpointSiteList &Process::GetBreakpointSiteList() const { |
| 1675 | return m_breakpoint_site_list; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1678 | void Process::DisableAllBreakpointSites() { |
| 1679 | m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void { |
| 1680 | // bp_site->SetEnabled(true); |
| 1681 | DisableBreakpointSite(bp_site); |
| 1682 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1685 | Status Process::ClearBreakpointSiteByID(lldb::user_id_t break_id) { |
| 1686 | Status error(DisableBreakpointSiteByID(break_id)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1687 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1688 | if (error.Success()) |
| 1689 | m_breakpoint_site_list.Remove(break_id); |
| 1690 | |
| 1691 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1694 | Status Process::DisableBreakpointSiteByID(lldb::user_id_t break_id) { |
| 1695 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1696 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID(break_id); |
| 1697 | if (bp_site_sp) { |
| 1698 | if (bp_site_sp->IsEnabled()) |
| 1699 | error = DisableBreakpointSite(bp_site_sp.get()); |
| 1700 | } else { |
| 1701 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, |
| 1702 | break_id); |
| 1703 | } |
| 1704 | |
| 1705 | return error; |
| 1706 | } |
| 1707 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1708 | Status Process::EnableBreakpointSiteByID(lldb::user_id_t break_id) { |
| 1709 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1710 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID(break_id); |
| 1711 | if (bp_site_sp) { |
| 1712 | if (!bp_site_sp->IsEnabled()) |
| 1713 | error = EnableBreakpointSite(bp_site_sp.get()); |
| 1714 | } else { |
| 1715 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, |
| 1716 | break_id); |
| 1717 | } |
| 1718 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Stephen Wilson | 50bd94f | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 1721 | lldb::break_id_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1722 | Process::CreateBreakpointSite(const BreakpointLocationSP &owner, |
| 1723 | bool use_hardware) { |
| 1724 | addr_t load_addr = LLDB_INVALID_ADDRESS; |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1725 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1726 | bool show_error = true; |
| 1727 | switch (GetState()) { |
| 1728 | case eStateInvalid: |
| 1729 | case eStateUnloaded: |
| 1730 | case eStateConnected: |
| 1731 | case eStateAttaching: |
| 1732 | case eStateLaunching: |
| 1733 | case eStateDetached: |
| 1734 | case eStateExited: |
| 1735 | show_error = false; |
| 1736 | break; |
| 1737 | |
| 1738 | case eStateStopped: |
| 1739 | case eStateRunning: |
| 1740 | case eStateStepping: |
| 1741 | case eStateCrashed: |
| 1742 | case eStateSuspended: |
| 1743 | show_error = IsAlive(); |
| 1744 | break; |
| 1745 | } |
| 1746 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1747 | // Reset the IsIndirect flag here, in case the location changes from pointing |
| 1748 | // to a indirect symbol to a regular symbol. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1749 | owner->SetIsIndirect(false); |
| 1750 | |
| 1751 | if (owner->ShouldResolveIndirectFunctions()) { |
| 1752 | Symbol *symbol = owner->GetAddress().CalculateSymbolContextSymbol(); |
| 1753 | if (symbol && symbol->IsIndirect()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1754 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1755 | Address symbol_address = symbol->GetAddress(); |
| 1756 | load_addr = ResolveIndirectFunction(&symbol_address, error); |
| 1757 | if (!error.Success() && show_error) { |
Lawrence D'Anna | 7ca15ba | 2019-09-27 14:33:35 +0000 | [diff] [blame] | 1758 | GetTarget().GetDebugger().GetErrorStream().Printf( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1759 | "warning: failed to resolve indirect function at 0x%" PRIx64 |
| 1760 | " for breakpoint %i.%i: %s\n", |
| 1761 | symbol->GetLoadAddress(&GetTarget()), |
| 1762 | owner->GetBreakpoint().GetID(), owner->GetID(), |
| 1763 | error.AsCString() ? error.AsCString() : "unknown error"); |
| 1764 | return LLDB_INVALID_BREAK_ID; |
| 1765 | } |
| 1766 | Address resolved_address(load_addr); |
| 1767 | load_addr = resolved_address.GetOpcodeLoadAddress(&GetTarget()); |
| 1768 | owner->SetIsIndirect(true); |
| 1769 | } else |
| 1770 | load_addr = owner->GetAddress().GetOpcodeLoadAddress(&GetTarget()); |
| 1771 | } else |
| 1772 | load_addr = owner->GetAddress().GetOpcodeLoadAddress(&GetTarget()); |
| 1773 | |
| 1774 | if (load_addr != LLDB_INVALID_ADDRESS) { |
| 1775 | BreakpointSiteSP bp_site_sp; |
| 1776 | |
| 1777 | // 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] | 1778 | // otherwise create a new breakpoint site and add it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1779 | |
| 1780 | bp_site_sp = m_breakpoint_site_list.FindByAddress(load_addr); |
| 1781 | |
| 1782 | if (bp_site_sp) { |
| 1783 | bp_site_sp->AddOwner(owner); |
| 1784 | owner->SetBreakpointSite(bp_site_sp); |
| 1785 | return bp_site_sp->GetID(); |
| 1786 | } else { |
| 1787 | bp_site_sp.reset(new BreakpointSite(&m_breakpoint_site_list, owner, |
| 1788 | load_addr, use_hardware)); |
| 1789 | if (bp_site_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1790 | Status error = EnableBreakpointSite(bp_site_sp.get()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1791 | if (error.Success()) { |
| 1792 | owner->SetBreakpointSite(bp_site_sp); |
| 1793 | return m_breakpoint_site_list.Add(bp_site_sp); |
| 1794 | } else { |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 +0000 | [diff] [blame] | 1795 | if (show_error || use_hardware) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1796 | // Report error for setting breakpoint... |
Lawrence D'Anna | 7ca15ba | 2019-09-27 14:33:35 +0000 | [diff] [blame] | 1797 | GetTarget().GetDebugger().GetErrorStream().Printf( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1798 | "warning: failed to set breakpoint site at 0x%" PRIx64 |
| 1799 | " for breakpoint %i.%i: %s\n", |
| 1800 | load_addr, owner->GetBreakpoint().GetID(), owner->GetID(), |
| 1801 | error.AsCString() ? error.AsCString() : "unknown error"); |
| 1802 | } |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1803 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1804 | } |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1805 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1806 | } |
| 1807 | // We failed to enable the breakpoint |
| 1808 | return LLDB_INVALID_BREAK_ID; |
| 1809 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1810 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1811 | void Process::RemoveOwnerFromBreakpointSite(lldb::user_id_t owner_id, |
| 1812 | lldb::user_id_t owner_loc_id, |
| 1813 | BreakpointSiteSP &bp_site_sp) { |
| 1814 | uint32_t num_owners = bp_site_sp->RemoveOwner(owner_id, owner_loc_id); |
| 1815 | if (num_owners == 0) { |
| 1816 | // Don't try to disable the site if we don't have a live process anymore. |
| 1817 | if (IsAlive()) |
| 1818 | DisableBreakpointSite(bp_site_sp.get()); |
| 1819 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 1820 | } |
| 1821 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1822 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1823 | size_t Process::RemoveBreakpointOpcodesFromBuffer(addr_t bp_addr, size_t size, |
| 1824 | uint8_t *buf) const { |
| 1825 | size_t bytes_removed = 0; |
| 1826 | BreakpointSiteList bp_sites_in_range; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1827 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1828 | if (m_breakpoint_site_list.FindInRange(bp_addr, bp_addr + size, |
| 1829 | bp_sites_in_range)) { |
Zachary Turner | 3bc714b | 2017-03-02 00:05:25 +0000 | [diff] [blame] | 1830 | bp_sites_in_range.ForEach([bp_addr, size, |
| 1831 | buf](BreakpointSite *bp_site) -> void { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1832 | if (bp_site->GetType() == BreakpointSite::eSoftware) { |
| 1833 | addr_t intersect_addr; |
| 1834 | size_t intersect_size; |
| 1835 | size_t opcode_offset; |
| 1836 | if (bp_site->IntersectsRange(bp_addr, size, &intersect_addr, |
| 1837 | &intersect_size, &opcode_offset)) { |
| 1838 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 1839 | assert(bp_addr < intersect_addr + intersect_size && |
| 1840 | intersect_addr + intersect_size <= bp_addr + size); |
| 1841 | assert(opcode_offset + intersect_size <= bp_site->GetByteSize()); |
| 1842 | size_t buf_offset = intersect_addr - bp_addr; |
| 1843 | ::memcpy(buf + buf_offset, |
| 1844 | bp_site->GetSavedOpcodeBytes() + opcode_offset, |
| 1845 | intersect_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1846 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1847 | } |
| 1848 | }); |
| 1849 | } |
| 1850 | return bytes_removed; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1853 | size_t Process::GetSoftwareBreakpointTrapOpcode(BreakpointSite *bp_site) { |
| 1854 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 1855 | if (platform_sp) |
| 1856 | return platform_sp->GetSoftwareBreakpointTrapOpcode(GetTarget(), bp_site); |
| 1857 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1860 | Status Process::EnableSoftwareBreakpoint(BreakpointSite *bp_site) { |
| 1861 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1862 | assert(bp_site != nullptr); |
| 1863 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
| 1864 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1865 | LLDB_LOGF( |
| 1866 | log, "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64, |
| 1867 | bp_site->GetID(), (uint64_t)bp_addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1868 | if (bp_site->IsEnabled()) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1869 | LLDB_LOGF( |
| 1870 | log, |
| 1871 | "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 1872 | " -- already enabled", |
| 1873 | bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1874 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | if (bp_addr == LLDB_INVALID_ADDRESS) { |
| 1878 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 1879 | return error; |
| 1880 | } |
| 1881 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 1882 | // trap for the breakpoint site |
| 1883 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 1884 | |
| 1885 | if (bp_opcode_size == 0) { |
| 1886 | error.SetErrorStringWithFormat("Process::GetSoftwareBreakpointTrapOpcode() " |
| 1887 | "returned zero, unable to get breakpoint " |
| 1888 | "trap for address 0x%" PRIx64, |
| 1889 | bp_addr); |
| 1890 | } else { |
| 1891 | const uint8_t *const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 1892 | |
| 1893 | if (bp_opcode_bytes == nullptr) { |
| 1894 | error.SetErrorString( |
| 1895 | "BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 1896 | return error; |
| 1897 | } |
| 1898 | |
| 1899 | // Save the original opcode by reading it |
| 1900 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, |
| 1901 | error) == bp_opcode_size) { |
| 1902 | // Write a software breakpoint in place of the original opcode |
| 1903 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == |
| 1904 | bp_opcode_size) { |
| 1905 | uint8_t verify_bp_opcode_bytes[64]; |
| 1906 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, |
| 1907 | error) == bp_opcode_size) { |
| 1908 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, |
| 1909 | bp_opcode_size) == 0) { |
| 1910 | bp_site->SetEnabled(true); |
| 1911 | bp_site->SetType(BreakpointSite::eSoftware); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1912 | LLDB_LOGF(log, |
| 1913 | "Process::EnableSoftwareBreakpoint (site_id = %d) " |
| 1914 | "addr = 0x%" PRIx64 " -- SUCCESS", |
| 1915 | bp_site->GetID(), (uint64_t)bp_addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1916 | } else |
| 1917 | error.SetErrorString( |
| 1918 | "failed to verify the breakpoint trap in memory."); |
| 1919 | } else |
| 1920 | error.SetErrorString( |
| 1921 | "Unable to read memory to verify breakpoint trap."); |
| 1922 | } else |
| 1923 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 1924 | } else |
| 1925 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 1926 | } |
| 1927 | if (log && error.Fail()) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1928 | LLDB_LOGF( |
| 1929 | log, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1930 | "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 1931 | " -- FAILED: %s", |
| 1932 | bp_site->GetID(), (uint64_t)bp_addr, error.AsCString()); |
| 1933 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1936 | Status Process::DisableSoftwareBreakpoint(BreakpointSite *bp_site) { |
| 1937 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1938 | assert(bp_site != nullptr); |
| 1939 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
| 1940 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 1941 | lldb::user_id_t breakID = bp_site->GetID(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1942 | LLDB_LOGF(log, |
| 1943 | "Process::DisableSoftwareBreakpoint (breakID = %" PRIu64 |
| 1944 | ") addr = 0x%" PRIx64, |
| 1945 | breakID, (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1946 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1947 | if (bp_site->IsHardware()) { |
| 1948 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 1949 | } else if (bp_site->IsEnabled()) { |
| 1950 | const size_t break_op_size = bp_site->GetByteSize(); |
| 1951 | const uint8_t *const break_op = bp_site->GetTrapOpcodeBytes(); |
| 1952 | if (break_op_size > 0) { |
| 1953 | // Clear a software breakpoint instruction |
| 1954 | uint8_t curr_break_op[8]; |
| 1955 | assert(break_op_size <= sizeof(curr_break_op)); |
| 1956 | bool break_op_found = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1957 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1958 | // Read the breakpoint opcode |
| 1959 | if (DoReadMemory(bp_addr, curr_break_op, break_op_size, error) == |
| 1960 | break_op_size) { |
| 1961 | bool verify = false; |
| 1962 | // Make sure the breakpoint opcode exists at this address |
| 1963 | if (::memcmp(curr_break_op, break_op, break_op_size) == 0) { |
| 1964 | break_op_found = true; |
| 1965 | // We found a valid breakpoint opcode at this address, now restore |
| 1966 | // the saved opcode. |
| 1967 | if (DoWriteMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), |
| 1968 | break_op_size, error) == break_op_size) { |
| 1969 | verify = true; |
| 1970 | } else |
| 1971 | error.SetErrorString( |
| 1972 | "Memory write failed when restoring original opcode."); |
| 1973 | } else { |
| 1974 | error.SetErrorString( |
| 1975 | "Original breakpoint trap is no longer in memory."); |
| 1976 | // Set verify to true and so we can check if the original opcode has |
| 1977 | // already been restored |
| 1978 | verify = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1979 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1980 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1981 | if (verify) { |
| 1982 | uint8_t verify_opcode[8]; |
| 1983 | assert(break_op_size < sizeof(verify_opcode)); |
| 1984 | // Verify that our original opcode made it back to the inferior |
| 1985 | if (DoReadMemory(bp_addr, verify_opcode, break_op_size, error) == |
| 1986 | break_op_size) { |
| 1987 | // compare the memory we just read with the original opcode |
| 1988 | if (::memcmp(bp_site->GetSavedOpcodeBytes(), verify_opcode, |
| 1989 | break_op_size) == 0) { |
| 1990 | // SUCCESS |
| 1991 | bp_site->SetEnabled(false); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1992 | LLDB_LOGF(log, |
| 1993 | "Process::DisableSoftwareBreakpoint (site_id = %d) " |
| 1994 | "addr = 0x%" PRIx64 " -- SUCCESS", |
| 1995 | bp_site->GetID(), (uint64_t)bp_addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1996 | return error; |
| 1997 | } else { |
| 1998 | if (break_op_found) |
| 1999 | error.SetErrorString("Failed to restore original opcode."); |
| 2000 | } |
| 2001 | } else |
| 2002 | error.SetErrorString("Failed to read memory to verify that " |
| 2003 | "breakpoint trap was restored."); |
| 2004 | } |
| 2005 | } else |
| 2006 | error.SetErrorString( |
| 2007 | "Unable to read memory that should contain the breakpoint trap."); |
| 2008 | } |
| 2009 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2010 | LLDB_LOGF( |
| 2011 | log, |
| 2012 | "Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 2013 | " -- already disabled", |
| 2014 | bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2015 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2018 | LLDB_LOGF( |
| 2019 | log, |
| 2020 | "Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 2021 | " -- FAILED: %s", |
| 2022 | bp_site->GetID(), (uint64_t)bp_addr, error.AsCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2023 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2026 | // Uncomment to verify memory caching works after making changes to caching |
| 2027 | // code |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2028 | //#define VERIFY_MEMORY_READS |
| 2029 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2030 | 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] | 2031 | error.Clear(); |
| 2032 | if (!GetDisableMemoryCache()) { |
| 2033 | #if defined(VERIFY_MEMORY_READS) |
| 2034 | // Memory caching is enabled, with debug verification |
| 2035 | |
| 2036 | if (buf && size) { |
| 2037 | // Uncomment the line below to make sure memory caching is working. |
| 2038 | // I ran this through the test suite and got no assertions, so I am |
| 2039 | // pretty confident this is working well. If any changes are made to |
| 2040 | // memory caching, uncomment the line below and test your changes! |
| 2041 | |
| 2042 | // Verify all memory reads by using the cache first, then redundantly |
| 2043 | // reading the same memory from the inferior and comparing to make sure |
| 2044 | // everything is exactly the same. |
| 2045 | std::string verify_buf(size, '\0'); |
| 2046 | assert(verify_buf.size() == size); |
| 2047 | const size_t cache_bytes_read = |
| 2048 | m_memory_cache.Read(this, addr, buf, size, error); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2049 | Status verify_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2050 | const size_t verify_bytes_read = |
| 2051 | ReadMemoryFromInferior(addr, const_cast<char *>(verify_buf.data()), |
| 2052 | verify_buf.size(), verify_error); |
| 2053 | assert(cache_bytes_read == verify_bytes_read); |
| 2054 | assert(memcmp(buf, verify_buf.data(), verify_buf.size()) == 0); |
| 2055 | assert(verify_error.Success() == error.Success()); |
| 2056 | return cache_bytes_read; |
| 2057 | } |
| 2058 | return 0; |
| 2059 | #else // !defined(VERIFY_MEMORY_READS) |
| 2060 | // Memory caching is enabled, without debug verification |
| 2061 | |
| 2062 | return m_memory_cache.Read(addr, buf, size, error); |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 2063 | #endif // defined (VERIFY_MEMORY_READS) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2064 | } else { |
| 2065 | // Memory caching is disabled |
| 2066 | |
| 2067 | return ReadMemoryFromInferior(addr, buf, size, error); |
| 2068 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2069 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2070 | |
| 2071 | size_t Process::ReadCStringFromMemory(addr_t addr, std::string &out_str, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2072 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2073 | char buf[256]; |
| 2074 | out_str.clear(); |
| 2075 | addr_t curr_addr = addr; |
| 2076 | while (true) { |
| 2077 | size_t length = ReadCStringFromMemory(curr_addr, buf, sizeof(buf), error); |
| 2078 | if (length == 0) |
| 2079 | break; |
| 2080 | out_str.append(buf, length); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2081 | // If we got "length - 1" bytes, we didn't get the whole C string, we need |
| 2082 | // to read some more characters |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2083 | if (length == sizeof(buf) - 1) |
| 2084 | curr_addr += length; |
| 2085 | else |
| 2086 | break; |
| 2087 | } |
| 2088 | return out_str.size(); |
| 2089 | } |
| 2090 | |
| 2091 | 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] | 2092 | Status &error, size_t type_width) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2093 | size_t total_bytes_read = 0; |
| 2094 | if (dst && max_bytes && type_width && max_bytes >= type_width) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2095 | // Ensure a null terminator independent of the number of bytes that is |
| 2096 | // read. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2097 | memset(dst, 0, max_bytes); |
| 2098 | size_t bytes_left = max_bytes - type_width; |
| 2099 | |
| 2100 | const char terminator[4] = {'\0', '\0', '\0', '\0'}; |
| 2101 | assert(sizeof(terminator) >= type_width && "Attempting to validate a " |
| 2102 | "string with more than 4 bytes " |
| 2103 | "per character!"); |
| 2104 | |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2105 | addr_t curr_addr = addr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2106 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2107 | char *curr_dst = dst; |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2109 | error.Clear(); |
| 2110 | while (bytes_left > 0 && error.Success()) { |
| 2111 | addr_t cache_line_bytes_left = |
| 2112 | cache_line_size - (curr_addr % cache_line_size); |
| 2113 | addr_t bytes_to_read = |
| 2114 | std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2115 | 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] | 2116 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2117 | if (bytes_read == 0) |
| 2118 | break; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2120 | // Search for a null terminator of correct size and alignment in |
| 2121 | // bytes_read |
| 2122 | size_t aligned_start = total_bytes_read - total_bytes_read % type_width; |
| 2123 | for (size_t i = aligned_start; |
| 2124 | i + type_width <= total_bytes_read + bytes_read; i += type_width) |
| 2125 | if (::memcmp(&dst[i], terminator, type_width) == 0) { |
| 2126 | error.Clear(); |
| 2127 | return i; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2128 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2129 | |
| 2130 | total_bytes_read += bytes_read; |
| 2131 | curr_dst += bytes_read; |
| 2132 | curr_addr += bytes_read; |
| 2133 | bytes_left -= bytes_read; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2134 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2135 | } else { |
| 2136 | if (max_bytes) |
| 2137 | error.SetErrorString("invalid arguments"); |
| 2138 | } |
| 2139 | return total_bytes_read; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2142 | // Deprecated in favor of ReadStringFromMemory which has wchar support and |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2143 | // correct code to find null terminators. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2144 | size_t Process::ReadCStringFromMemory(addr_t addr, char *dst, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2145 | size_t dst_max_len, |
| 2146 | Status &result_error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2147 | size_t total_cstr_len = 0; |
| 2148 | if (dst && dst_max_len) { |
| 2149 | result_error.Clear(); |
| 2150 | // NULL out everything just to be safe |
| 2151 | memset(dst, 0, dst_max_len); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2152 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2153 | addr_t curr_addr = addr; |
| 2154 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2155 | size_t bytes_left = dst_max_len - 1; |
| 2156 | char *curr_dst = dst; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2157 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2158 | while (bytes_left > 0) { |
| 2159 | addr_t cache_line_bytes_left = |
| 2160 | cache_line_size - (curr_addr % cache_line_size); |
| 2161 | addr_t bytes_to_read = |
| 2162 | std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2163 | 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] | 2164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2165 | if (bytes_read == 0) { |
| 2166 | result_error = error; |
| 2167 | dst[total_cstr_len] = '\0'; |
| 2168 | break; |
| 2169 | } |
| 2170 | const size_t len = strlen(curr_dst); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2172 | total_cstr_len += len; |
| 2173 | |
| 2174 | if (len < bytes_to_read) |
| 2175 | break; |
| 2176 | |
| 2177 | curr_dst += bytes_read; |
| 2178 | curr_addr += bytes_read; |
| 2179 | bytes_left -= bytes_read; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2180 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2181 | } else { |
| 2182 | if (dst == nullptr) |
| 2183 | result_error.SetErrorString("invalid arguments"); |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2184 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2185 | result_error.Clear(); |
| 2186 | } |
| 2187 | return total_cstr_len; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2190 | size_t Process::ReadMemoryFromInferior(addr_t addr, void *buf, size_t size, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2191 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2192 | if (buf == nullptr || size == 0) |
| 2193 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2195 | size_t bytes_read = 0; |
| 2196 | uint8_t *bytes = (uint8_t *)buf; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2197 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2198 | while (bytes_read < size) { |
| 2199 | const size_t curr_size = size - bytes_read; |
| 2200 | const size_t curr_bytes_read = |
| 2201 | DoReadMemory(addr + bytes_read, bytes + bytes_read, curr_size, error); |
| 2202 | bytes_read += curr_bytes_read; |
| 2203 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 2204 | break; |
| 2205 | } |
| 2206 | |
| 2207 | // Replace any software breakpoint opcodes that fall into this range back |
| 2208 | // into "buf" before we return |
| 2209 | if (bytes_read > 0) |
| 2210 | RemoveBreakpointOpcodesFromBuffer(addr, bytes_read, (uint8_t *)buf); |
| 2211 | return bytes_read; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2214 | uint64_t Process::ReadUnsignedIntegerFromMemory(lldb::addr_t vm_addr, |
| 2215 | size_t integer_byte_size, |
| 2216 | uint64_t fail_value, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2217 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2218 | Scalar scalar; |
| 2219 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, |
| 2220 | error)) |
| 2221 | return scalar.ULongLong(fail_value); |
| 2222 | return fail_value; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2225 | int64_t Process::ReadSignedIntegerFromMemory(lldb::addr_t vm_addr, |
| 2226 | size_t integer_byte_size, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2227 | int64_t fail_value, |
| 2228 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2229 | Scalar scalar; |
| 2230 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, true, scalar, |
| 2231 | error)) |
| 2232 | return scalar.SLongLong(fail_value); |
| 2233 | return fail_value; |
Greg Clayton | c226778 | 2016-05-23 20:37:24 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2236 | addr_t Process::ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2237 | Scalar scalar; |
| 2238 | if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, |
| 2239 | error)) |
| 2240 | return scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 2241 | return LLDB_INVALID_ADDRESS; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2244 | 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] | 2245 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2246 | Scalar scalar; |
| 2247 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2248 | if (addr_byte_size <= 4) |
| 2249 | scalar = (uint32_t)ptr_value; |
| 2250 | else |
| 2251 | scalar = ptr_value; |
| 2252 | return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == |
| 2253 | addr_byte_size; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2256 | 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] | 2257 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2258 | size_t bytes_written = 0; |
| 2259 | const uint8_t *bytes = (const uint8_t *)buf; |
| 2260 | |
| 2261 | while (bytes_written < size) { |
| 2262 | const size_t curr_size = size - bytes_written; |
| 2263 | const size_t curr_bytes_written = DoWriteMemory( |
| 2264 | addr + bytes_written, bytes + bytes_written, curr_size, error); |
| 2265 | bytes_written += curr_bytes_written; |
| 2266 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 2267 | break; |
| 2268 | } |
| 2269 | return bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2272 | 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] | 2273 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2274 | #if defined(ENABLE_MEMORY_CACHING) |
| 2275 | m_memory_cache.Flush(addr, size); |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2276 | #endif |
| 2277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2278 | if (buf == nullptr || size == 0) |
| 2279 | return 0; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2280 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2281 | m_mod_id.BumpMemoryID(); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2283 | // We need to write any data that would go where any current software traps |
| 2284 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 2285 | // may have placed in our tasks memory. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2286 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2287 | BreakpointSiteList bp_sites_in_range; |
Jonas Devlieghere | 533fe61 | 2019-04-01 20:39:03 +0000 | [diff] [blame] | 2288 | if (!m_breakpoint_site_list.FindInRange(addr, addr + size, bp_sites_in_range)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2289 | return WriteMemoryPrivate(addr, buf, size, error); |
Jonas Devlieghere | 533fe61 | 2019-04-01 20:39:03 +0000 | [diff] [blame] | 2290 | |
| 2291 | // No breakpoint sites overlap |
| 2292 | if (bp_sites_in_range.IsEmpty()) |
| 2293 | return WriteMemoryPrivate(addr, buf, size, error); |
| 2294 | |
| 2295 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 2296 | uint64_t bytes_written = 0; |
| 2297 | |
| 2298 | bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, |
| 2299 | &error](BreakpointSite *bp) -> void { |
| 2300 | if (error.Fail()) |
| 2301 | return; |
| 2302 | |
Tatyana Krasnukha | f7ec3e3 | 2020-07-22 14:45:10 +0300 | [diff] [blame] | 2303 | if (bp->GetType() != BreakpointSite::eSoftware) |
| 2304 | return; |
| 2305 | |
Jonas Devlieghere | 533fe61 | 2019-04-01 20:39:03 +0000 | [diff] [blame] | 2306 | addr_t intersect_addr; |
| 2307 | size_t intersect_size; |
| 2308 | size_t opcode_offset; |
| 2309 | const bool intersects = bp->IntersectsRange( |
| 2310 | addr, size, &intersect_addr, &intersect_size, &opcode_offset); |
| 2311 | UNUSED_IF_ASSERT_DISABLED(intersects); |
| 2312 | assert(intersects); |
| 2313 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 2314 | assert(addr < intersect_addr + intersect_size && |
| 2315 | intersect_addr + intersect_size <= addr + size); |
| 2316 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 2317 | |
| 2318 | // Check for bytes before this breakpoint |
| 2319 | const addr_t curr_addr = addr + bytes_written; |
| 2320 | if (intersect_addr > curr_addr) { |
| 2321 | // There are some bytes before this breakpoint that we need to just |
| 2322 | // write to memory |
| 2323 | size_t curr_size = intersect_addr - curr_addr; |
| 2324 | size_t curr_bytes_written = |
| 2325 | WriteMemoryPrivate(curr_addr, ubuf + bytes_written, curr_size, error); |
| 2326 | bytes_written += curr_bytes_written; |
| 2327 | if (curr_bytes_written != curr_size) { |
| 2328 | // We weren't able to write all of the requested bytes, we are |
| 2329 | // done looping and will return the number of bytes that we have |
| 2330 | // written so far. |
| 2331 | if (error.Success()) |
| 2332 | error.SetErrorToGenericError(); |
| 2333 | } |
| 2334 | } |
| 2335 | // Now write any bytes that would cover up any software breakpoints |
| 2336 | // directly into the breakpoint opcode buffer |
| 2337 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, |
| 2338 | intersect_size); |
| 2339 | bytes_written += intersect_size; |
| 2340 | }); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2341 | |
| 2342 | // Write any remaining bytes after the last breakpoint if we have any left |
Jonas Devlieghere | 533fe61 | 2019-04-01 20:39:03 +0000 | [diff] [blame] | 2343 | if (bytes_written < size) |
| 2344 | bytes_written += |
| 2345 | WriteMemoryPrivate(addr + bytes_written, ubuf + bytes_written, |
| 2346 | size - bytes_written, error); |
| 2347 | |
| 2348 | return bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2349 | } |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2350 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2351 | size_t Process::WriteScalarToMemory(addr_t addr, const Scalar &scalar, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2352 | size_t byte_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2353 | if (byte_size == UINT32_MAX) |
| 2354 | byte_size = scalar.GetByteSize(); |
| 2355 | if (byte_size > 0) { |
| 2356 | uint8_t buf[32]; |
| 2357 | const size_t mem_size = |
| 2358 | scalar.GetAsMemoryData(buf, byte_size, GetByteOrder(), error); |
| 2359 | if (mem_size > 0) |
| 2360 | return WriteMemory(addr, buf, mem_size, error); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2361 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2362 | error.SetErrorString("failed to get scalar as memory data"); |
| 2363 | } else { |
| 2364 | error.SetErrorString("invalid scalar value"); |
| 2365 | } |
| 2366 | return 0; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2369 | size_t Process::ReadScalarIntegerFromMemory(addr_t addr, uint32_t byte_size, |
| 2370 | bool is_signed, Scalar &scalar, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2371 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2372 | uint64_t uval = 0; |
| 2373 | if (byte_size == 0) { |
| 2374 | error.SetErrorString("byte size is zero"); |
| 2375 | } else if (byte_size & (byte_size - 1)) { |
| 2376 | error.SetErrorStringWithFormat("byte size %u is not a power of 2", |
| 2377 | byte_size); |
| 2378 | } else if (byte_size <= sizeof(uval)) { |
| 2379 | const size_t bytes_read = ReadMemory(addr, &uval, byte_size, error); |
| 2380 | if (bytes_read == byte_size) { |
| 2381 | DataExtractor data(&uval, sizeof(uval), GetByteOrder(), |
| 2382 | GetAddressByteSize()); |
| 2383 | lldb::offset_t offset = 0; |
| 2384 | if (byte_size <= 4) |
| 2385 | scalar = data.GetMaxU32(&offset, byte_size); |
| 2386 | else |
| 2387 | scalar = data.GetMaxU64(&offset, byte_size); |
| 2388 | if (is_signed) |
| 2389 | scalar.SignExtend(byte_size * 8); |
| 2390 | return bytes_read; |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2391 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2392 | } else { |
| 2393 | error.SetErrorStringWithFormat( |
| 2394 | "byte size of %u is too large for integer scalar type", byte_size); |
| 2395 | } |
| 2396 | return 0; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 2399 | Status Process::WriteObjectFile(std::vector<ObjectFile::LoadableData> entries) { |
| 2400 | Status error; |
| 2401 | for (const auto &Entry : entries) { |
| 2402 | WriteMemory(Entry.Dest, Entry.Contents.data(), Entry.Contents.size(), |
| 2403 | error); |
| 2404 | if (!error.Success()) |
| 2405 | break; |
| 2406 | } |
| 2407 | return error; |
| 2408 | } |
| 2409 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2410 | #define USE_ALLOCATE_MEMORY_CACHE 1 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2411 | addr_t Process::AllocateMemory(size_t size, uint32_t permissions, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2412 | Status &error) { |
Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 2413 | if (GetPrivateState() != eStateStopped) { |
| 2414 | error.SetErrorToGenericError(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2415 | return LLDB_INVALID_ADDRESS; |
Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 2416 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2417 | |
| 2418 | #if defined(USE_ALLOCATE_MEMORY_CACHE) |
| 2419 | return m_allocated_memory_cache.AllocateMemory(size, permissions, error); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2420 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2421 | addr_t allocated_addr = DoAllocateMemory(size, permissions, error); |
| 2422 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2423 | LLDB_LOGF(log, |
| 2424 | "Process::AllocateMemory(size=%" PRIu64 |
| 2425 | ", permissions=%s) => 0x%16.16" PRIx64 |
| 2426 | " (m_stop_id = %u m_memory_id = %u)", |
| 2427 | (uint64_t)size, GetPermissionsAsCString(permissions), |
| 2428 | (uint64_t)allocated_addr, m_mod_id.GetStopID(), |
| 2429 | m_mod_id.GetMemoryID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2430 | return allocated_addr; |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2431 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2434 | addr_t Process::CallocateMemory(size_t size, uint32_t permissions, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2435 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2436 | addr_t return_addr = AllocateMemory(size, permissions, error); |
| 2437 | if (error.Success()) { |
| 2438 | std::string buffer(size, 0); |
| 2439 | WriteMemory(return_addr, buffer.c_str(), size, error); |
| 2440 | } |
| 2441 | return return_addr; |
| 2442 | } |
| 2443 | |
| 2444 | bool Process::CanJIT() { |
| 2445 | if (m_can_jit == eCanJITDontKnow) { |
| 2446 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2447 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2448 | |
| 2449 | uint64_t allocated_memory = AllocateMemory( |
| 2450 | 8, ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable, |
| 2451 | err); |
| 2452 | |
| 2453 | if (err.Success()) { |
| 2454 | m_can_jit = eCanJITYes; |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2455 | LLDB_LOGF(log, |
| 2456 | "Process::%s pid %" PRIu64 |
| 2457 | " allocation test passed, CanJIT () is true", |
| 2458 | __FUNCTION__, GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2459 | } else { |
| 2460 | m_can_jit = eCanJITNo; |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2461 | LLDB_LOGF(log, |
| 2462 | "Process::%s pid %" PRIu64 |
| 2463 | " allocation test failed, CanJIT () is false: %s", |
| 2464 | __FUNCTION__, GetID(), err.AsCString()); |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame] | 2465 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2466 | |
| 2467 | DeallocateMemory(allocated_memory); |
| 2468 | } |
| 2469 | |
| 2470 | return m_can_jit == eCanJITYes; |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2473 | void Process::SetCanJIT(bool can_jit) { |
| 2474 | m_can_jit = (can_jit ? eCanJITYes : eCanJITNo); |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2477 | void Process::SetCanRunCode(bool can_run_code) { |
| 2478 | SetCanJIT(can_run_code); |
| 2479 | m_can_interpret_function_calls = can_run_code; |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2482 | Status Process::DeallocateMemory(addr_t ptr) { |
| 2483 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2484 | #if defined(USE_ALLOCATE_MEMORY_CACHE) |
| 2485 | if (!m_allocated_memory_cache.DeallocateMemory(ptr)) { |
| 2486 | error.SetErrorStringWithFormat( |
| 2487 | "deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr); |
| 2488 | } |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2489 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2490 | error = DoDeallocateMemory(ptr); |
| 2491 | |
| 2492 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2493 | LLDB_LOGF(log, |
| 2494 | "Process::DeallocateMemory(addr=0x%16.16" PRIx64 |
| 2495 | ") => err = %s (m_stop_id = %u, m_memory_id = %u)", |
| 2496 | ptr, error.AsCString("SUCCESS"), m_mod_id.GetStopID(), |
| 2497 | m_mod_id.GetMemoryID()); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2498 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2499 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2502 | ModuleSP Process::ReadModuleFromMemory(const FileSpec &file_spec, |
| 2503 | lldb::addr_t header_addr, |
| 2504 | size_t size_to_read) { |
| 2505 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); |
| 2506 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2507 | LLDB_LOGF(log, |
| 2508 | "Process::ReadModuleFromMemory reading %s binary from memory", |
| 2509 | file_spec.GetPath().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2510 | } |
| 2511 | ModuleSP module_sp(new Module(file_spec, ArchSpec())); |
| 2512 | if (module_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2513 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2514 | ObjectFile *objfile = module_sp->GetMemoryObjectFile( |
| 2515 | shared_from_this(), header_addr, error, size_to_read); |
| 2516 | if (objfile) |
| 2517 | return module_sp; |
| 2518 | } |
| 2519 | return ModuleSP(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2522 | bool Process::GetLoadAddressPermissions(lldb::addr_t load_addr, |
| 2523 | uint32_t &permissions) { |
| 2524 | MemoryRegionInfo range_info; |
| 2525 | permissions = 0; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2526 | Status error(GetMemoryRegionInfo(load_addr, range_info)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2527 | if (!error.Success()) |
| 2528 | return false; |
| 2529 | if (range_info.GetReadable() == MemoryRegionInfo::eDontKnow || |
| 2530 | range_info.GetWritable() == MemoryRegionInfo::eDontKnow || |
| 2531 | range_info.GetExecutable() == MemoryRegionInfo::eDontKnow) { |
| 2532 | return false; |
| 2533 | } |
| 2534 | |
| 2535 | if (range_info.GetReadable() == MemoryRegionInfo::eYes) |
| 2536 | permissions |= lldb::ePermissionsReadable; |
| 2537 | |
| 2538 | if (range_info.GetWritable() == MemoryRegionInfo::eYes) |
| 2539 | permissions |= lldb::ePermissionsWritable; |
| 2540 | |
| 2541 | if (range_info.GetExecutable() == MemoryRegionInfo::eYes) |
| 2542 | permissions |= lldb::ePermissionsExecutable; |
| 2543 | |
| 2544 | return true; |
| 2545 | } |
| 2546 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2547 | Status Process::EnableWatchpoint(Watchpoint *watchpoint, bool notify) { |
| 2548 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2549 | error.SetErrorString("watchpoints are not supported"); |
| 2550 | return error; |
| 2551 | } |
| 2552 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2553 | Status Process::DisableWatchpoint(Watchpoint *watchpoint, bool notify) { |
| 2554 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2555 | error.SetErrorString("watchpoints are not supported"); |
| 2556 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | StateType |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 2560 | Process::WaitForProcessStopPrivate(EventSP &event_sp, |
| 2561 | const Timeout<std::micro> &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2562 | StateType state; |
Stella Stamenova | 36d457c | 2018-06-01 19:14:53 +0000 | [diff] [blame] | 2563 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2564 | while (true) { |
| 2565 | event_sp.reset(); |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 2566 | state = GetStateChangedEventsPrivate(event_sp, timeout); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2567 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2568 | if (StateIsStoppedState(state, false)) |
| 2569 | break; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2570 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2571 | // If state is invalid, then we timed out |
| 2572 | if (state == eStateInvalid) |
| 2573 | break; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2574 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2575 | if (event_sp) |
| 2576 | HandlePrivateEvent(event_sp); |
| 2577 | } |
| 2578 | return state; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2581 | void Process::LoadOperatingSystemPlugin(bool flush) { |
| 2582 | if (flush) |
| 2583 | m_thread_list.Clear(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2584 | m_os_up.reset(OperatingSystem::FindPlugin(this, nullptr)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2585 | if (flush) |
| 2586 | Flush(); |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2589 | Status Process::Launch(ProcessLaunchInfo &launch_info) { |
| 2590 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2591 | m_abi_sp.reset(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2592 | m_dyld_up.reset(); |
| 2593 | m_jit_loaders_up.reset(); |
| 2594 | m_system_runtime_up.reset(); |
| 2595 | m_os_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2596 | m_process_input_reader.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2597 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2598 | Module *exe_module = GetTarget().GetExecutableModulePointer(); |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2599 | if (!exe_module) { |
| 2600 | error.SetErrorString("executable module does not exist"); |
| 2601 | return error; |
| 2602 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2603 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2604 | char local_exec_file_path[PATH_MAX]; |
| 2605 | char platform_exec_file_path[PATH_MAX]; |
| 2606 | exe_module->GetFileSpec().GetPath(local_exec_file_path, |
| 2607 | sizeof(local_exec_file_path)); |
| 2608 | exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, |
| 2609 | sizeof(platform_exec_file_path)); |
| 2610 | if (FileSystem::Instance().Exists(exe_module->GetFileSpec())) { |
| 2611 | // Install anything that might need to be installed prior to launching. |
| 2612 | // For host systems, this will do nothing, but if we are connected to a |
| 2613 | // remote platform it will install any needed binaries |
| 2614 | error = GetTarget().Install(&launch_info); |
| 2615 | if (error.Fail()) |
| 2616 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2617 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2618 | if (PrivateStateThreadIsValid()) |
| 2619 | PausePrivateStateThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2620 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2621 | error = WillLaunch(exe_module); |
| 2622 | if (error.Success()) { |
| 2623 | const bool restarted = false; |
| 2624 | SetPublicState(eStateLaunching, restarted); |
| 2625 | m_should_detach = false; |
| 2626 | |
| 2627 | if (m_public_run_lock.TrySetRunning()) { |
| 2628 | // Now launch using these arguments. |
| 2629 | error = DoLaunch(exe_module, launch_info); |
| 2630 | } else { |
| 2631 | // This shouldn't happen |
| 2632 | error.SetErrorString("failed to acquire process run lock"); |
| 2633 | } |
| 2634 | |
| 2635 | if (error.Fail()) { |
| 2636 | if (GetID() != LLDB_INVALID_PROCESS_ID) { |
| 2637 | SetID(LLDB_INVALID_PROCESS_ID); |
| 2638 | const char *error_string = error.AsCString(); |
| 2639 | if (error_string == nullptr) |
| 2640 | error_string = "launch failed"; |
| 2641 | SetExitStatus(-1, error_string); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2642 | } |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2643 | } else { |
| 2644 | EventSP event_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2645 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2646 | // Now wait for the process to launch and return control to us, and then |
| 2647 | // call DidLaunch: |
| 2648 | StateType state = WaitForProcessStopPrivate(event_sp, seconds(10)); |
Stella Stamenova | 36d457c | 2018-06-01 19:14:53 +0000 | [diff] [blame] | 2649 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2650 | if (state == eStateInvalid || !event_sp) { |
| 2651 | // We were able to launch the process, but we failed to catch the |
| 2652 | // initial stop. |
| 2653 | error.SetErrorString("failed to catch stop after launch"); |
| 2654 | SetExitStatus(0, "failed to catch stop after launch"); |
| 2655 | Destroy(false); |
| 2656 | } else if (state == eStateStopped || state == eStateCrashed) { |
| 2657 | DidLaunch(); |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 2658 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2659 | DynamicLoader *dyld = GetDynamicLoader(); |
| 2660 | if (dyld) |
| 2661 | dyld->DidLaunch(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2662 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2663 | GetJITLoaders().DidLaunch(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2664 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2665 | SystemRuntime *system_runtime = GetSystemRuntime(); |
| 2666 | if (system_runtime) |
| 2667 | system_runtime->DidLaunch(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2668 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2669 | if (!m_os_up) |
| 2670 | LoadOperatingSystemPlugin(false); |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 2671 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2672 | // We successfully launched the process and stopped, now it the |
| 2673 | // right time to set up signal filters before resuming. |
| 2674 | UpdateAutomaticSignalFiltering(); |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 2675 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2676 | // Note, the stop event was consumed above, but not handled. This |
| 2677 | // was done to give DidLaunch a chance to run. The target is either |
| 2678 | // stopped or crashed. Directly set the state. This is done to |
| 2679 | // prevent a stop message with a bunch of spurious output on thread |
| 2680 | // status, as well as not pop a ProcessIOHandler. |
| 2681 | SetPublicState(state, false); |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 2682 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2683 | if (PrivateStateThreadIsValid()) |
| 2684 | ResumePrivateStateThread(); |
| 2685 | else |
| 2686 | StartPrivateStateThread(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2687 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2688 | // Target was stopped at entry as was intended. Need to notify the |
| 2689 | // listeners about it. |
| 2690 | if (state == eStateStopped && |
| 2691 | launch_info.GetFlags().Test(eLaunchFlagStopAtEntry)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2692 | HandlePrivateEvent(event_sp); |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2693 | } else if (state == eStateExited) { |
| 2694 | // We exited while trying to launch somehow. Don't call DidLaunch |
| 2695 | // as that's not likely to work, and return an invalid pid. |
| 2696 | HandlePrivateEvent(event_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2697 | } |
| 2698 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2699 | } |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2700 | } else { |
| 2701 | error.SetErrorStringWithFormat("file doesn't exist: '%s'", |
| 2702 | local_exec_file_path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2703 | } |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2704 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2705 | return error; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2706 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2707 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2708 | Status Process::LoadCore() { |
| 2709 | Status error = DoLoadCore(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2710 | if (error.Success()) { |
| 2711 | ListenerSP listener_sp( |
| 2712 | Listener::MakeListener("lldb.process.load_core_listener")); |
| 2713 | HijackProcessEvents(listener_sp); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2714 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2715 | if (PrivateStateThreadIsValid()) |
| 2716 | ResumePrivateStateThread(); |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 2717 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2718 | StartPrivateStateThread(); |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 2719 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2720 | DynamicLoader *dyld = GetDynamicLoader(); |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2721 | if (dyld) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2722 | dyld->DidAttach(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2723 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 2724 | GetJITLoaders().DidAttach(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2725 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2726 | SystemRuntime *system_runtime = GetSystemRuntime(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2727 | if (system_runtime) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2728 | system_runtime->DidAttach(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2729 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2730 | if (!m_os_up) |
Greg Clayton | 29eeea0 | 2017-02-09 18:55:41 +0000 | [diff] [blame] | 2731 | LoadOperatingSystemPlugin(false); |
| 2732 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2733 | // 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] | 2734 | // 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] | 2735 | SetPrivateState(eStateStopped); |
Greg Clayton | a97c4d2 | 2014-12-09 23:31:02 +0000 | [diff] [blame] | 2736 | |
Jonas Devlieghere | 25486b7 | 2018-03-14 11:50:10 +0000 | [diff] [blame] | 2737 | // Wait for a stopped event since we just posted one above... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2738 | lldb::EventSP event_sp; |
Jonas Devlieghere | 25486b7 | 2018-03-14 11:50:10 +0000 | [diff] [blame] | 2739 | StateType state = |
Pavel Labath | 7ada1c5 | 2019-06-25 07:14:29 +0000 | [diff] [blame] | 2740 | WaitForProcessToStop(llvm::None, &event_sp, true, listener_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2741 | |
| 2742 | if (!StateIsStoppedState(state, false)) { |
| 2743 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2744 | LLDB_LOGF(log, "Process::Halt() failed to stop, state is: %s", |
| 2745 | StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2746 | error.SetErrorString( |
| 2747 | "Did not get stopped event after loading the core file."); |
| 2748 | } |
| 2749 | RestoreProcessEvents(); |
| 2750 | } |
| 2751 | return error; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2754 | DynamicLoader *Process::GetDynamicLoader() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2755 | if (!m_dyld_up) |
| 2756 | m_dyld_up.reset(DynamicLoader::FindPlugin(this, nullptr)); |
| 2757 | return m_dyld_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Antonio Afonso | 943faef | 2019-06-11 20:16:13 +0000 | [diff] [blame] | 2760 | DataExtractor Process::GetAuxvData() { return DataExtractor(); } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2761 | |
| 2762 | JITLoaderList &Process::GetJITLoaders() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2763 | if (!m_jit_loaders_up) { |
Jonas Devlieghere | 06412da | 2020-06-24 17:44:33 -0700 | [diff] [blame] | 2764 | m_jit_loaders_up = std::make_unique<JITLoaderList>(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2765 | JITLoader::LoadPlugins(this, *m_jit_loaders_up); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2766 | } |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2767 | return *m_jit_loaders_up; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | SystemRuntime *Process::GetSystemRuntime() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2771 | if (!m_system_runtime_up) |
| 2772 | m_system_runtime_up.reset(SystemRuntime::FindPlugin(this)); |
| 2773 | return m_system_runtime_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | Process::AttachCompletionHandler::AttachCompletionHandler(Process *process, |
| 2777 | uint32_t exec_count) |
| 2778 | : NextEventAction(process), m_exec_count(exec_count) { |
| 2779 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2780 | LLDB_LOGF( |
| 2781 | log, |
| 2782 | "Process::AttachCompletionHandler::%s process=%p, exec_count=%" PRIu32, |
| 2783 | __FUNCTION__, static_cast<void *>(process), exec_count); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | Process::NextEventAction::EventActionResult |
| 2787 | Process::AttachCompletionHandler::PerformAction(lldb::EventSP &event_sp) { |
| 2788 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2789 | |
| 2790 | StateType state = ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2791 | LLDB_LOGF(log, |
| 2792 | "Process::AttachCompletionHandler::%s called with state %s (%d)", |
| 2793 | __FUNCTION__, StateAsCString(state), static_cast<int>(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2794 | |
| 2795 | switch (state) { |
| 2796 | case eStateAttaching: |
| 2797 | return eEventActionSuccess; |
| 2798 | |
| 2799 | case eStateRunning: |
| 2800 | case eStateConnected: |
| 2801 | return eEventActionRetry; |
| 2802 | |
| 2803 | case eStateStopped: |
| 2804 | case eStateCrashed: |
| 2805 | // During attach, prior to sending the eStateStopped event, |
| 2806 | // lldb_private::Process subclasses must set the new process ID. |
| 2807 | assert(m_process->GetID() != LLDB_INVALID_PROCESS_ID); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2808 | // We don't want these events to be reported, so go set the |
| 2809 | // ShouldReportStop here: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2810 | m_process->GetThreadList().SetShouldReportStop(eVoteNo); |
| 2811 | |
| 2812 | if (m_exec_count > 0) { |
| 2813 | --m_exec_count; |
| 2814 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2815 | LLDB_LOGF(log, |
| 2816 | "Process::AttachCompletionHandler::%s state %s: reduced " |
| 2817 | "remaining exec count to %" PRIu32 ", requesting resume", |
| 2818 | __FUNCTION__, StateAsCString(state), m_exec_count); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2819 | |
| 2820 | RequestResume(); |
| 2821 | return eEventActionRetry; |
| 2822 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2823 | LLDB_LOGF(log, |
| 2824 | "Process::AttachCompletionHandler::%s state %s: no more " |
| 2825 | "execs expected to start, continuing with attach", |
| 2826 | __FUNCTION__, StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2827 | |
| 2828 | m_process->CompleteAttach(); |
| 2829 | return eEventActionSuccess; |
| 2830 | } |
| 2831 | break; |
| 2832 | |
| 2833 | default: |
| 2834 | case eStateExited: |
| 2835 | case eStateInvalid: |
| 2836 | break; |
| 2837 | } |
| 2838 | |
| 2839 | m_exit_string.assign("No valid Process"); |
| 2840 | return eEventActionExit; |
| 2841 | } |
| 2842 | |
| 2843 | Process::NextEventAction::EventActionResult |
| 2844 | Process::AttachCompletionHandler::HandleBeingInterrupted() { |
| 2845 | return eEventActionSuccess; |
| 2846 | } |
| 2847 | |
| 2848 | const char *Process::AttachCompletionHandler::GetExitString() { |
| 2849 | return m_exit_string.c_str(); |
| 2850 | } |
| 2851 | |
| 2852 | ListenerSP ProcessAttachInfo::GetListenerForProcess(Debugger &debugger) { |
| 2853 | if (m_listener_sp) |
| 2854 | return m_listener_sp; |
| 2855 | else |
| 2856 | return debugger.GetListener(); |
| 2857 | } |
| 2858 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2859 | Status Process::Attach(ProcessAttachInfo &attach_info) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2860 | m_abi_sp.reset(); |
| 2861 | m_process_input_reader.reset(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2862 | m_dyld_up.reset(); |
| 2863 | m_jit_loaders_up.reset(); |
| 2864 | m_system_runtime_up.reset(); |
| 2865 | m_os_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2866 | |
| 2867 | lldb::pid_t attach_pid = attach_info.GetProcessID(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2868 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2869 | if (attach_pid == LLDB_INVALID_PROCESS_ID) { |
| 2870 | char process_name[PATH_MAX]; |
| 2871 | |
| 2872 | if (attach_info.GetExecutableFile().GetPath(process_name, |
| 2873 | sizeof(process_name))) { |
| 2874 | const bool wait_for_launch = attach_info.GetWaitForLaunch(); |
| 2875 | |
| 2876 | if (wait_for_launch) { |
| 2877 | error = WillAttachToProcessWithName(process_name, wait_for_launch); |
| 2878 | if (error.Success()) { |
| 2879 | if (m_public_run_lock.TrySetRunning()) { |
| 2880 | m_should_detach = true; |
| 2881 | const bool restarted = false; |
| 2882 | SetPublicState(eStateAttaching, restarted); |
| 2883 | // Now attach using these arguments. |
| 2884 | error = DoAttachToProcessWithName(process_name, attach_info); |
| 2885 | } else { |
| 2886 | // This shouldn't happen |
| 2887 | error.SetErrorString("failed to acquire process run lock"); |
| 2888 | } |
| 2889 | |
| 2890 | if (error.Fail()) { |
| 2891 | if (GetID() != LLDB_INVALID_PROCESS_ID) { |
| 2892 | SetID(LLDB_INVALID_PROCESS_ID); |
| 2893 | if (error.AsCString() == nullptr) |
| 2894 | error.SetErrorString("attach failed"); |
| 2895 | |
| 2896 | SetExitStatus(-1, error.AsCString()); |
| 2897 | } |
| 2898 | } else { |
| 2899 | SetNextEventAction(new Process::AttachCompletionHandler( |
| 2900 | this, attach_info.GetResumeCount())); |
| 2901 | StartPrivateStateThread(); |
| 2902 | } |
| 2903 | return error; |
| 2904 | } |
| 2905 | } else { |
| 2906 | ProcessInstanceInfoList process_infos; |
| 2907 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 2908 | |
| 2909 | if (platform_sp) { |
| 2910 | ProcessInstanceInfoMatch match_info; |
| 2911 | match_info.GetProcessInfo() = attach_info; |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 2912 | match_info.SetNameMatchType(NameMatch::Equals); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2913 | platform_sp->FindProcesses(match_info, process_infos); |
Jonas Devlieghere | 638b06c | 2020-03-12 14:10:25 -0700 | [diff] [blame] | 2914 | const uint32_t num_matches = process_infos.size(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2915 | if (num_matches == 1) { |
Jonas Devlieghere | 638b06c | 2020-03-12 14:10:25 -0700 | [diff] [blame] | 2916 | attach_pid = process_infos[0].GetProcessID(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2917 | // Fall through and attach using the above process ID |
| 2918 | } else { |
| 2919 | match_info.GetProcessInfo().GetExecutableFile().GetPath( |
| 2920 | process_name, sizeof(process_name)); |
| 2921 | if (num_matches > 1) { |
| 2922 | StreamString s; |
Pavel Labath | aa51e6a | 2019-03-04 18:48:00 +0000 | [diff] [blame] | 2923 | ProcessInstanceInfo::DumpTableHeader(s, true, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2924 | for (size_t i = 0; i < num_matches; i++) { |
Jonas Devlieghere | 638b06c | 2020-03-12 14:10:25 -0700 | [diff] [blame] | 2925 | process_infos[i].DumpAsTableRow( |
Pavel Labath | aa51e6a | 2019-03-04 18:48:00 +0000 | [diff] [blame] | 2926 | s, platform_sp->GetUserIDResolver(), true, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2927 | } |
| 2928 | error.SetErrorStringWithFormat( |
| 2929 | "more than one process named %s:\n%s", process_name, |
| 2930 | s.GetData()); |
| 2931 | } else |
| 2932 | error.SetErrorStringWithFormat( |
| 2933 | "could not find a process named %s", process_name); |
| 2934 | } |
| 2935 | } else { |
| 2936 | error.SetErrorString( |
| 2937 | "invalid platform, can't find processes by name"); |
| 2938 | return error; |
| 2939 | } |
| 2940 | } |
| 2941 | } else { |
| 2942 | error.SetErrorString("invalid process name"); |
| 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | if (attach_pid != LLDB_INVALID_PROCESS_ID) { |
| 2947 | error = WillAttachToProcessWithID(attach_pid); |
| 2948 | if (error.Success()) { |
| 2949 | |
| 2950 | if (m_public_run_lock.TrySetRunning()) { |
| 2951 | // Now attach using these arguments. |
| 2952 | m_should_detach = true; |
| 2953 | const bool restarted = false; |
| 2954 | SetPublicState(eStateAttaching, restarted); |
| 2955 | error = DoAttachToProcessWithID(attach_pid, attach_info); |
| 2956 | } else { |
| 2957 | // This shouldn't happen |
| 2958 | error.SetErrorString("failed to acquire process run lock"); |
| 2959 | } |
| 2960 | |
| 2961 | if (error.Success()) { |
| 2962 | SetNextEventAction(new Process::AttachCompletionHandler( |
| 2963 | this, attach_info.GetResumeCount())); |
| 2964 | StartPrivateStateThread(); |
| 2965 | } else { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2966 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2967 | SetID(LLDB_INVALID_PROCESS_ID); |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 2968 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2969 | const char *error_string = error.AsCString(); |
| 2970 | if (error_string == nullptr) |
| 2971 | error_string = "attach failed"; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2972 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2973 | SetExitStatus(-1, error_string); |
| 2974 | } |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 2975 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2976 | } |
| 2977 | return error; |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2980 | void Process::CompleteAttach() { |
| 2981 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | |
| 2982 | LIBLLDB_LOG_TARGET)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2983 | LLDB_LOGF(log, "Process::%s()", __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2984 | |
| 2985 | // Let the process subclass figure out at much as it can about the process |
| 2986 | // before we go looking for a dynamic loader plug-in. |
| 2987 | ArchSpec process_arch; |
| 2988 | DidAttach(process_arch); |
| 2989 | |
| 2990 | if (process_arch.IsValid()) { |
| 2991 | GetTarget().SetArchitecture(process_arch); |
| 2992 | if (log) { |
| 2993 | const char *triple_str = process_arch.GetTriple().getTriple().c_str(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2994 | LLDB_LOGF(log, |
| 2995 | "Process::%s replacing process architecture with DidAttach() " |
| 2996 | "architecture: %s", |
| 2997 | __FUNCTION__, triple_str ? triple_str : "<null>"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2998 | } |
| 2999 | } |
| 3000 | |
| 3001 | // 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] | 3002 | // architecture, and if it isn't the same as the one we've already set, |
| 3003 | // switch architectures. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3004 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 3005 | assert(platform_sp); |
| 3006 | if (platform_sp) { |
| 3007 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
| 3008 | if (target_arch.IsValid() && |
| 3009 | !platform_sp->IsCompatibleArchitecture(target_arch, false, nullptr)) { |
| 3010 | ArchSpec platform_arch; |
| 3011 | platform_sp = |
| 3012 | platform_sp->GetPlatformForArchitecture(target_arch, &platform_arch); |
| 3013 | if (platform_sp) { |
| 3014 | GetTarget().SetPlatform(platform_sp); |
| 3015 | GetTarget().SetArchitecture(platform_arch); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3016 | LLDB_LOGF(log, |
| 3017 | "Process::%s switching platform to %s and architecture " |
| 3018 | "to %s based on info from attach", |
| 3019 | __FUNCTION__, platform_sp->GetName().AsCString(""), |
| 3020 | platform_arch.GetTriple().getTriple().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3021 | } |
| 3022 | } else if (!process_arch.IsValid()) { |
| 3023 | ProcessInstanceInfo process_info; |
| 3024 | GetProcessInfo(process_info); |
| 3025 | const ArchSpec &process_arch = process_info.GetArchitecture(); |
| 3026 | if (process_arch.IsValid() && |
| 3027 | !GetTarget().GetArchitecture().IsExactMatch(process_arch)) { |
| 3028 | GetTarget().SetArchitecture(process_arch); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3029 | LLDB_LOGF(log, |
| 3030 | "Process::%s switching architecture to %s based on info " |
| 3031 | "the platform retrieved for pid %" PRIu64, |
| 3032 | __FUNCTION__, process_arch.GetTriple().getTriple().c_str(), |
| 3033 | GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3034 | } |
| 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | // We have completed the attach, now it is time to find the dynamic loader |
| 3039 | // plug-in |
| 3040 | DynamicLoader *dyld = GetDynamicLoader(); |
| 3041 | if (dyld) { |
| 3042 | dyld->DidAttach(); |
| 3043 | if (log) { |
| 3044 | ModuleSP exe_module_sp = GetTarget().GetExecutableModule(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3045 | LLDB_LOGF(log, |
| 3046 | "Process::%s after DynamicLoader::DidAttach(), target " |
| 3047 | "executable is %s (using %s plugin)", |
| 3048 | __FUNCTION__, |
| 3049 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str() |
| 3050 | : "<none>", |
| 3051 | dyld->GetPluginName().AsCString("<unnamed>")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | GetJITLoaders().DidAttach(); |
| 3056 | |
| 3057 | SystemRuntime *system_runtime = GetSystemRuntime(); |
| 3058 | if (system_runtime) { |
| 3059 | system_runtime->DidAttach(); |
| 3060 | if (log) { |
| 3061 | ModuleSP exe_module_sp = GetTarget().GetExecutableModule(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3062 | LLDB_LOGF(log, |
| 3063 | "Process::%s after SystemRuntime::DidAttach(), target " |
| 3064 | "executable is %s (using %s plugin)", |
| 3065 | __FUNCTION__, |
| 3066 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str() |
| 3067 | : "<none>", |
| 3068 | system_runtime->GetPluginName().AsCString("<unnamed>")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3069 | } |
| 3070 | } |
| 3071 | |
Jim Ingham | 8864b43 | 2019-06-28 17:57:19 +0000 | [diff] [blame] | 3072 | if (!m_os_up) { |
Greg Clayton | 29eeea0 | 2017-02-09 18:55:41 +0000 | [diff] [blame] | 3073 | LoadOperatingSystemPlugin(false); |
Jim Ingham | 8864b43 | 2019-06-28 17:57:19 +0000 | [diff] [blame] | 3074 | if (m_os_up) { |
| 3075 | // Somebody might have gotten threads before now, but we need to force the |
| 3076 | // update after we've loaded the OperatingSystem plugin or it won't get a |
| 3077 | // chance to process the threads. |
| 3078 | m_thread_list.Clear(); |
| 3079 | UpdateThreadListIfNeeded(); |
| 3080 | } |
| 3081 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3082 | // Figure out which one is the executable, and set that in our target: |
| 3083 | const ModuleList &target_modules = GetTarget().GetImages(); |
| 3084 | std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); |
| 3085 | size_t num_modules = target_modules.GetSize(); |
| 3086 | ModuleSP new_executable_module_sp; |
| 3087 | |
| 3088 | for (size_t i = 0; i < num_modules; i++) { |
| 3089 | ModuleSP module_sp(target_modules.GetModuleAtIndexUnlocked(i)); |
| 3090 | if (module_sp && module_sp->IsExecutable()) { |
| 3091 | if (GetTarget().GetExecutableModulePointer() != module_sp.get()) |
| 3092 | new_executable_module_sp = module_sp; |
| 3093 | break; |
| 3094 | } |
| 3095 | } |
| 3096 | if (new_executable_module_sp) { |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 3097 | GetTarget().SetExecutableModule(new_executable_module_sp, |
| 3098 | eLoadDependentsNo); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3099 | if (log) { |
| 3100 | ModuleSP exe_module_sp = GetTarget().GetExecutableModule(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3101 | LLDB_LOGF( |
| 3102 | log, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3103 | "Process::%s after looping through modules, target executable is %s", |
| 3104 | __FUNCTION__, |
| 3105 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str() |
| 3106 | : "<none>"); |
| 3107 | } |
| 3108 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
Jonas Devlieghere | 32d35fb | 2020-07-13 13:44:01 -0700 | [diff] [blame] | 3111 | Status Process::ConnectRemote(llvm::StringRef remote_url) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3112 | m_abi_sp.reset(); |
| 3113 | m_process_input_reader.reset(); |
| 3114 | |
| 3115 | // Find the process and its architecture. Make sure it matches the |
Zachary Turner | 3165945 | 2016-11-17 21:15:14 +0000 | [diff] [blame] | 3116 | // architecture of the current Target, and if not adjust it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3117 | |
Jonas Devlieghere | 32d35fb | 2020-07-13 13:44:01 -0700 | [diff] [blame] | 3118 | Status error(DoConnectRemote(remote_url)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3119 | if (error.Success()) { |
| 3120 | if (GetID() != LLDB_INVALID_PROCESS_ID) { |
| 3121 | EventSP event_sp; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3122 | StateType state = WaitForProcessStopPrivate(event_sp, llvm::None); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3123 | |
| 3124 | if (state == eStateStopped || state == eStateCrashed) { |
| 3125 | // If we attached and actually have a process on the other end, then |
| 3126 | // this ended up being the equivalent of an attach. |
| 3127 | CompleteAttach(); |
| 3128 | |
| 3129 | // This delays passing the stopped event to listeners till |
| 3130 | // CompleteAttach gets a chance to complete... |
| 3131 | HandlePrivateEvent(event_sp); |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | if (PrivateStateThreadIsValid()) |
| 3136 | ResumePrivateStateThread(); |
| 3137 | else |
| 3138 | StartPrivateStateThread(); |
| 3139 | } |
| 3140 | return error; |
| 3141 | } |
| 3142 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3143 | Status Process::PrivateResume() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3144 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | |
| 3145 | LIBLLDB_LOG_STEP)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3146 | LLDB_LOGF(log, |
| 3147 | "Process::PrivateResume() m_stop_id = %u, public state: %s " |
| 3148 | "private state: %s", |
| 3149 | m_mod_id.GetStopID(), StateAsCString(m_public_state.GetValue()), |
| 3150 | StateAsCString(m_private_state.GetValue())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3151 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3152 | // If signals handing status changed we might want to update our signal |
| 3153 | // filters before resuming. |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 3154 | UpdateAutomaticSignalFiltering(); |
| 3155 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3156 | Status error(WillResume()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3157 | // Tell the process it is about to resume before the thread list |
| 3158 | if (error.Success()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3159 | // Now let the thread list know we are about to resume so it can let all of |
| 3160 | // our threads know that they are about to be resumed. Threads will each be |
| 3161 | // called with Thread::WillResume(StateType) where StateType contains the |
| 3162 | // state that they are supposed to have when the process is resumed |
| 3163 | // (suspended/running/stepping). Threads should also check their resume |
| 3164 | // signal in lldb::Thread::GetResumeSignal() to see if they are supposed to |
| 3165 | // start back up with a signal. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3166 | if (m_thread_list.WillResume()) { |
| 3167 | // Last thing, do the PreResumeActions. |
| 3168 | if (!RunPreResumeActions()) { |
Jonas Devlieghere | 8953376 | 2020-07-20 22:57:06 -0700 | [diff] [blame] | 3169 | error.SetErrorString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3170 | "Process::PrivateResume PreResumeActions failed, not resuming."); |
| 3171 | } else { |
| 3172 | m_mod_id.BumpResumeID(); |
| 3173 | error = DoResume(); |
| 3174 | if (error.Success()) { |
| 3175 | DidResume(); |
| 3176 | m_thread_list.DidResume(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3177 | LLDB_LOGF(log, "Process thinks the process has resumed."); |
Jason Molenda | f47c734 | 2018-12-17 23:33:40 +0000 | [diff] [blame] | 3178 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3179 | LLDB_LOGF(log, "Process::PrivateResume() DoResume failed."); |
Jason Molenda | f47c734 | 2018-12-17 23:33:40 +0000 | [diff] [blame] | 3180 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3181 | } |
| 3182 | } |
| 3183 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3184 | // Somebody wanted to run without running (e.g. we were faking a step |
| 3185 | // from one frame of a set of inlined frames that share the same PC to |
| 3186 | // another.) So generate a continue & a stopped event, and let the world |
| 3187 | // handle them. |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3188 | LLDB_LOGF(log, |
| 3189 | "Process::PrivateResume() asked to simulate a start & stop."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3190 | |
| 3191 | SetPrivateState(eStateRunning); |
| 3192 | SetPrivateState(eStateStopped); |
| 3193 | } |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3194 | } else |
| 3195 | LLDB_LOGF(log, "Process::PrivateResume() got an error \"%s\".", |
| 3196 | error.AsCString("<unknown error>")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3197 | return error; |
| 3198 | } |
| 3199 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3200 | Status Process::Halt(bool clear_thread_plans, bool use_run_lock) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3201 | if (!StateIsRunningState(m_public_state.GetValue())) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3202 | return Status("Process is not running."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3203 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3204 | // Don't clear the m_clear_thread_plans_on_stop, only set it to true if in |
| 3205 | // 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] | 3206 | m_clear_thread_plans_on_stop |= clear_thread_plans; |
| 3207 | |
| 3208 | ListenerSP halt_listener_sp( |
| 3209 | Listener::MakeListener("lldb.process.halt_listener")); |
| 3210 | HijackProcessEvents(halt_listener_sp); |
| 3211 | |
| 3212 | EventSP event_sp; |
| 3213 | |
| 3214 | SendAsyncInterrupt(); |
| 3215 | |
| 3216 | if (m_public_state.GetValue() == eStateAttaching) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3217 | // Don't hijack and eat the eStateExited as the code that was doing the |
| 3218 | // attach will be waiting for this event... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3219 | RestoreProcessEvents(); |
| 3220 | SetExitStatus(SIGKILL, "Cancelled async attach."); |
| 3221 | Destroy(false); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3222 | return Status(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | // Wait for 10 second for the process to stop. |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3226 | StateType state = WaitForProcessToStop( |
| 3227 | seconds(10), &event_sp, true, halt_listener_sp, nullptr, use_run_lock); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3228 | RestoreProcessEvents(); |
| 3229 | |
| 3230 | if (state == eStateInvalid || !event_sp) { |
| 3231 | // We timed out and didn't get a stop event... |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3232 | return Status("Halt timed out. State = %s", StateAsCString(GetState())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | BroadcastEvent(event_sp); |
| 3236 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3237 | return Status(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3240 | Status Process::StopForDestroyOrDetach(lldb::EventSP &exit_event_sp) { |
| 3241 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3242 | |
| 3243 | // 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] | 3244 | // expression, for instance, then the public state will be stopped, but we |
| 3245 | // still need to interrupt. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3246 | if (m_public_state.GetValue() == eStateRunning || |
| 3247 | m_private_state.GetValue() == eStateRunning) { |
| 3248 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3249 | LLDB_LOGF(log, "Process::%s() About to stop.", __FUNCTION__); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3250 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3251 | ListenerSP listener_sp( |
| 3252 | Listener::MakeListener("lldb.Process.StopForDestroyOrDetach.hijack")); |
| 3253 | HijackProcessEvents(listener_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3254 | |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3255 | SendAsyncInterrupt(); |
| 3256 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3257 | // Consume the interrupt event. |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3258 | StateType state = |
| 3259 | WaitForProcessToStop(seconds(10), &exit_event_sp, true, listener_sp); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3260 | |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3261 | RestoreProcessEvents(); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3262 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3263 | // 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] | 3264 | // exited event into the shared pointer passed in and return. Our caller |
| 3265 | // doesn't need to do anything else, since they don't have a process |
| 3266 | // anymore... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3267 | |
| 3268 | if (state == eStateExited || m_private_state.GetValue() == eStateExited) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3269 | LLDB_LOGF(log, "Process::%s() Process exited while waiting to stop.", |
| 3270 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3271 | return error; |
| 3272 | } else |
| 3273 | exit_event_sp.reset(); // It is ok to consume any non-exit stop events |
| 3274 | |
| 3275 | if (state != eStateStopped) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3276 | LLDB_LOGF(log, "Process::%s() failed to stop, state is: %s", __FUNCTION__, |
| 3277 | StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3278 | // 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] | 3279 | // here, but if the lower levels just bobbled sending the event and we |
| 3280 | // really are stopped, then continue on. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3281 | StateType private_state = m_private_state.GetValue(); |
| 3282 | if (private_state != eStateStopped) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3283 | return Status( |
| 3284 | "Attempt to stop the target in order to detach timed out. " |
| 3285 | "State = %s", |
| 3286 | StateAsCString(GetState())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3287 | } |
| 3288 | } |
| 3289 | } |
| 3290 | return error; |
| 3291 | } |
| 3292 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3293 | Status Process::Detach(bool keep_stopped) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3294 | EventSP exit_event_sp; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3295 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3296 | m_destroy_in_process = true; |
| 3297 | |
| 3298 | error = WillDetach(); |
| 3299 | |
| 3300 | if (error.Success()) { |
| 3301 | if (DetachRequiresHalt()) { |
| 3302 | error = StopForDestroyOrDetach(exit_event_sp); |
| 3303 | if (!error.Success()) { |
| 3304 | m_destroy_in_process = false; |
| 3305 | return error; |
| 3306 | } else if (exit_event_sp) { |
| 3307 | // We shouldn't need to do anything else here. There's no process left |
| 3308 | // to detach from... |
| 3309 | StopPrivateStateThread(); |
| 3310 | m_destroy_in_process = false; |
| 3311 | return error; |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | m_thread_list.DiscardThreadPlans(); |
| 3316 | DisableAllBreakpointSites(); |
| 3317 | |
| 3318 | error = DoDetach(keep_stopped); |
| 3319 | if (error.Success()) { |
| 3320 | DidDetach(); |
| 3321 | StopPrivateStateThread(); |
| 3322 | } else { |
| 3323 | return error; |
| 3324 | } |
| 3325 | } |
| 3326 | m_destroy_in_process = false; |
| 3327 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3328 | // If we exited when we were waiting for a process to stop, then forward the |
| 3329 | // event here so we don't lose the event |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3330 | if (exit_event_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3331 | // Directly broadcast our exited event because we shut down our private |
| 3332 | // state thread above |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3333 | BroadcastEvent(exit_event_sp); |
| 3334 | } |
| 3335 | |
| 3336 | // 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] | 3337 | // not end up propagating the last events through the event system, in which |
| 3338 | // case we might strand the write lock. Unlock it here so when we do to tear |
| 3339 | // down the process we don't get an error destroying the lock. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3340 | |
| 3341 | m_public_run_lock.SetStopped(); |
| 3342 | return error; |
| 3343 | } |
| 3344 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3345 | Status Process::Destroy(bool force_kill) { |
Jim Ingham | 61e8e68 | 2020-03-10 16:18:11 -0700 | [diff] [blame] | 3346 | // If we've already called Process::Finalize then there's nothing useful to |
| 3347 | // be done here. Finalize has actually called Destroy already. |
| 3348 | if (m_finalize_called) |
| 3349 | return {}; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3350 | |
| 3351 | // 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] | 3352 | // don't do any unnecessary work that might hinder the destruction. Remember |
| 3353 | // 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] | 3354 | // failed and the process stays around for some reason it won't be in a |
| 3355 | // confused state. |
| 3356 | |
| 3357 | if (force_kill) |
| 3358 | m_should_detach = false; |
| 3359 | |
| 3360 | if (GetShouldDetach()) { |
| 3361 | // FIXME: This will have to be a process setting: |
| 3362 | bool keep_stopped = false; |
| 3363 | Detach(keep_stopped); |
| 3364 | } |
| 3365 | |
| 3366 | m_destroy_in_process = true; |
| 3367 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3368 | Status error(WillDestroy()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3369 | if (error.Success()) { |
| 3370 | EventSP exit_event_sp; |
| 3371 | if (DestroyRequiresHalt()) { |
| 3372 | error = StopForDestroyOrDetach(exit_event_sp); |
| 3373 | } |
| 3374 | |
| 3375 | if (m_public_state.GetValue() != eStateRunning) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3376 | // Ditch all thread plans, and remove all our breakpoints: in case we |
| 3377 | // have to restart the target to kill it, we don't want it hitting a |
| 3378 | // breakpoint... Only do this if we've stopped, however, since if we |
| 3379 | // didn't manage to halt it above, then we're not going to have much luck |
| 3380 | // doing this now. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3381 | m_thread_list.DiscardThreadPlans(); |
| 3382 | DisableAllBreakpointSites(); |
| 3383 | } |
| 3384 | |
| 3385 | error = DoDestroy(); |
| 3386 | if (error.Success()) { |
| 3387 | DidDestroy(); |
| 3388 | StopPrivateStateThread(); |
| 3389 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3390 | m_stdio_communication.StopReadThread(); |
Pavel Labath | 9321255 | 2020-04-23 16:27:01 +0200 | [diff] [blame] | 3391 | m_stdio_communication.Disconnect(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3392 | m_stdin_forward = false; |
| 3393 | |
| 3394 | if (m_process_input_reader) { |
| 3395 | m_process_input_reader->SetIsDone(true); |
| 3396 | m_process_input_reader->Cancel(); |
| 3397 | m_process_input_reader.reset(); |
| 3398 | } |
| 3399 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3400 | // If we exited when we were waiting for a process to stop, then forward |
| 3401 | // the event here so we don't lose the event |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3402 | if (exit_event_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3403 | // Directly broadcast our exited event because we shut down our private |
| 3404 | // state thread above |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3405 | BroadcastEvent(exit_event_sp); |
| 3406 | } |
| 3407 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3408 | // If we have been interrupted (to kill us) in the middle of running, we |
| 3409 | // may not end up propagating the last events through the event system, in |
| 3410 | // which case we might strand the write lock. Unlock it here so when we do |
| 3411 | // 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] | 3412 | m_public_run_lock.SetStopped(); |
| 3413 | } |
| 3414 | |
| 3415 | m_destroy_in_process = false; |
| 3416 | |
| 3417 | return error; |
| 3418 | } |
| 3419 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3420 | Status Process::Signal(int signal) { |
| 3421 | Status error(WillSignal()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3422 | if (error.Success()) { |
| 3423 | error = DoSignal(signal); |
| 3424 | if (error.Success()) |
| 3425 | DidSignal(); |
| 3426 | } |
| 3427 | return error; |
| 3428 | } |
| 3429 | |
| 3430 | void Process::SetUnixSignals(UnixSignalsSP &&signals_sp) { |
| 3431 | assert(signals_sp && "null signals_sp"); |
| 3432 | m_unix_signals_sp = signals_sp; |
| 3433 | } |
| 3434 | |
| 3435 | const lldb::UnixSignalsSP &Process::GetUnixSignals() { |
| 3436 | assert(m_unix_signals_sp && "null m_unix_signals_sp"); |
| 3437 | return m_unix_signals_sp; |
| 3438 | } |
| 3439 | |
| 3440 | lldb::ByteOrder Process::GetByteOrder() const { |
| 3441 | return GetTarget().GetArchitecture().GetByteOrder(); |
| 3442 | } |
| 3443 | |
| 3444 | uint32_t Process::GetAddressByteSize() const { |
| 3445 | return GetTarget().GetArchitecture().GetAddressByteSize(); |
| 3446 | } |
| 3447 | |
| 3448 | bool Process::ShouldBroadcastEvent(Event *event_ptr) { |
| 3449 | const StateType state = |
| 3450 | Process::ProcessEventData::GetStateFromEvent(event_ptr); |
| 3451 | bool return_value = true; |
| 3452 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | |
| 3453 | LIBLLDB_LOG_PROCESS)); |
| 3454 | |
| 3455 | switch (state) { |
| 3456 | case eStateDetached: |
| 3457 | case eStateExited: |
| 3458 | case eStateUnloaded: |
| 3459 | m_stdio_communication.SynchronizeWithReadThread(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3460 | m_stdio_communication.StopReadThread(); |
Jonas Devlieghere | b424b0b | 2020-04-22 16:51:56 -0700 | [diff] [blame] | 3461 | m_stdio_communication.Disconnect(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3462 | m_stdin_forward = false; |
| 3463 | |
| 3464 | LLVM_FALLTHROUGH; |
| 3465 | case eStateConnected: |
| 3466 | case eStateAttaching: |
| 3467 | case eStateLaunching: |
| 3468 | // These events indicate changes in the state of the debugging session, |
| 3469 | // always report them. |
| 3470 | return_value = true; |
| 3471 | break; |
| 3472 | case eStateInvalid: |
| 3473 | // We stopped for no apparent reason, don't report it. |
| 3474 | return_value = false; |
| 3475 | break; |
| 3476 | case eStateRunning: |
| 3477 | case eStateStepping: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3478 | // If we've started the target running, we handle the cases where we are |
| 3479 | // already running and where there is a transition from stopped to running |
| 3480 | // differently. running -> running: Automatically suppress extra running |
| 3481 | // events stopped -> running: Report except when there is one or more no |
| 3482 | // votes |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3483 | // and no yes votes. |
| 3484 | SynchronouslyNotifyStateChanged(state); |
| 3485 | if (m_force_next_event_delivery) |
| 3486 | return_value = true; |
| 3487 | else { |
| 3488 | switch (m_last_broadcast_state) { |
| 3489 | case eStateRunning: |
| 3490 | case eStateStepping: |
| 3491 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 3492 | return_value = false; |
| 3493 | break; |
| 3494 | default: |
| 3495 | // TODO: make this work correctly. For now always report |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3496 | // run if we aren't running so we don't miss any running events. If I |
| 3497 | // run the lldb/test/thread/a.out file and break at main.cpp:58, run |
| 3498 | // and hit the breakpoints on multiple threads, then somehow during the |
| 3499 | // stepping over of all breakpoints no run gets reported. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3500 | |
| 3501 | // This is a transition from stop to run. |
| 3502 | switch (m_thread_list.ShouldReportRun(event_ptr)) { |
| 3503 | case eVoteYes: |
| 3504 | case eVoteNoOpinion: |
| 3505 | return_value = true; |
| 3506 | break; |
| 3507 | case eVoteNo: |
| 3508 | return_value = false; |
| 3509 | break; |
| 3510 | } |
| 3511 | break; |
| 3512 | } |
| 3513 | } |
| 3514 | break; |
| 3515 | case eStateStopped: |
| 3516 | case eStateCrashed: |
| 3517 | case eStateSuspended: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3518 | // We've stopped. First see if we're going to restart the target. If we |
| 3519 | // are going to stop, then we always broadcast the event. If we aren't |
| 3520 | // going to stop, let the thread plans decide if we're going to report this |
| 3521 | // event. If no thread has an opinion, we don't report it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3522 | |
| 3523 | m_stdio_communication.SynchronizeWithReadThread(); |
| 3524 | RefreshStateAfterStop(); |
| 3525 | if (ProcessEventData::GetInterruptedFromEvent(event_ptr)) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3526 | LLDB_LOGF(log, |
| 3527 | "Process::ShouldBroadcastEvent (%p) stopped due to an " |
| 3528 | "interrupt, state: %s", |
| 3529 | static_cast<void *>(event_ptr), StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3530 | // 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] | 3531 | // 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] | 3532 | m_thread_list.ShouldStop(event_ptr); |
| 3533 | return_value = true; |
| 3534 | } else { |
| 3535 | bool was_restarted = ProcessEventData::GetRestartedFromEvent(event_ptr); |
| 3536 | bool should_resume = false; |
| 3537 | |
| 3538 | // It makes no sense to ask "ShouldStop" if we've already been |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3539 | // restarted... Asking the thread list is also not likely to go well, |
| 3540 | // 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] | 3541 | |
| 3542 | if (!was_restarted) |
| 3543 | should_resume = !m_thread_list.ShouldStop(event_ptr); |
| 3544 | |
| 3545 | if (was_restarted || should_resume || m_resume_requested) { |
| 3546 | Vote stop_vote = m_thread_list.ShouldReportStop(event_ptr); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3547 | LLDB_LOGF(log, |
| 3548 | "Process::ShouldBroadcastEvent: should_resume: %i state: " |
| 3549 | "%s was_restarted: %i stop_vote: %d.", |
| 3550 | should_resume, StateAsCString(state), was_restarted, |
| 3551 | stop_vote); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3552 | |
| 3553 | switch (stop_vote) { |
| 3554 | case eVoteYes: |
| 3555 | return_value = true; |
| 3556 | break; |
| 3557 | case eVoteNoOpinion: |
| 3558 | case eVoteNo: |
| 3559 | return_value = false; |
| 3560 | break; |
| 3561 | } |
| 3562 | |
| 3563 | if (!was_restarted) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3564 | LLDB_LOGF(log, |
| 3565 | "Process::ShouldBroadcastEvent (%p) Restarting process " |
| 3566 | "from state: %s", |
| 3567 | static_cast<void *>(event_ptr), StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3568 | ProcessEventData::SetRestartedInEvent(event_ptr, true); |
| 3569 | PrivateResume(); |
| 3570 | } |
| 3571 | } else { |
| 3572 | return_value = true; |
| 3573 | SynchronouslyNotifyStateChanged(state); |
| 3574 | } |
| 3575 | } |
| 3576 | break; |
| 3577 | } |
| 3578 | |
| 3579 | // Forcing the next event delivery is a one shot deal. So reset it here. |
| 3580 | m_force_next_event_delivery = false; |
| 3581 | |
| 3582 | // We do some coalescing of events (for instance two consecutive running |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3583 | // events get coalesced.) But we only coalesce against events we actually |
| 3584 | // broadcast. So we use m_last_broadcast_state to track that. NB - you |
| 3585 | // can't use "m_public_state.GetValue()" for that purpose, as was originally |
| 3586 | // done, because the PublicState reflects the last event pulled off the |
| 3587 | // queue, and there may be several events stacked up on the queue unserviced. |
| 3588 | // So the PublicState may not reflect the last broadcasted event yet. |
| 3589 | // m_last_broadcast_state gets updated here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3590 | |
| 3591 | if (return_value) |
| 3592 | m_last_broadcast_state = state; |
| 3593 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3594 | LLDB_LOGF(log, |
| 3595 | "Process::ShouldBroadcastEvent (%p) => new state: %s, last " |
| 3596 | "broadcast state: %s - %s", |
| 3597 | static_cast<void *>(event_ptr), StateAsCString(state), |
| 3598 | StateAsCString(m_last_broadcast_state), |
| 3599 | return_value ? "YES" : "NO"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3600 | return return_value; |
| 3601 | } |
| 3602 | |
| 3603 | bool Process::StartPrivateStateThread(bool is_secondary_thread) { |
| 3604 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
| 3605 | |
| 3606 | bool already_running = PrivateStateThreadIsValid(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3607 | LLDB_LOGF(log, "Process::%s()%s ", __FUNCTION__, |
| 3608 | already_running ? " already running" |
| 3609 | : " starting private state thread"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3610 | |
| 3611 | if (!is_secondary_thread && already_running) |
| 3612 | return true; |
| 3613 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3614 | // Create a thread that watches our internal state and controls which events |
| 3615 | // make it to clients (into the DCProcess event queue). |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3616 | char thread_name[1024]; |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 3617 | uint32_t max_len = llvm::get_max_thread_name_length(); |
| 3618 | if (max_len > 0 && max_len <= 30) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3619 | // On platforms with abbreviated thread name lengths, choose thread names |
| 3620 | // that fit within the limit. |
| 3621 | if (already_running) |
| 3622 | snprintf(thread_name, sizeof(thread_name), "intern-state-OV"); |
| 3623 | else |
| 3624 | snprintf(thread_name, sizeof(thread_name), "intern-state"); |
| 3625 | } else { |
| 3626 | if (already_running) |
| 3627 | snprintf(thread_name, sizeof(thread_name), |
| 3628 | "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", |
| 3629 | GetID()); |
| 3630 | else |
| 3631 | snprintf(thread_name, sizeof(thread_name), |
| 3632 | "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID()); |
| 3633 | } |
| 3634 | |
| 3635 | // Create the private state thread, and start it running. |
| 3636 | PrivateStateThreadArgs *args_ptr = |
| 3637 | new PrivateStateThreadArgs(this, is_secondary_thread); |
Jonas Devlieghere | f39c2e1 | 2019-07-05 17:42:08 +0000 | [diff] [blame] | 3638 | llvm::Expected<HostThread> private_state_thread = |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3639 | ThreadLauncher::LaunchThread(thread_name, Process::PrivateStateThread, |
Jonas Devlieghere | f39c2e1 | 2019-07-05 17:42:08 +0000 | [diff] [blame] | 3640 | (void *)args_ptr, 8 * 1024 * 1024); |
| 3641 | if (!private_state_thread) { |
| 3642 | LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST), |
| 3643 | "failed to launch host thread: {}", |
| 3644 | llvm::toString(private_state_thread.takeError())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3645 | return false; |
Jonas Devlieghere | f39c2e1 | 2019-07-05 17:42:08 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | assert(private_state_thread->IsJoinable()); |
| 3649 | m_private_state_thread = *private_state_thread; |
| 3650 | ResumePrivateStateThread(); |
| 3651 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | void Process::PausePrivateStateThread() { |
| 3655 | ControlPrivateStateThread(eBroadcastInternalStateControlPause); |
| 3656 | } |
| 3657 | |
| 3658 | void Process::ResumePrivateStateThread() { |
| 3659 | ControlPrivateStateThread(eBroadcastInternalStateControlResume); |
| 3660 | } |
| 3661 | |
| 3662 | void Process::StopPrivateStateThread() { |
| 3663 | if (m_private_state_thread.IsJoinable()) |
| 3664 | ControlPrivateStateThread(eBroadcastInternalStateControlStop); |
| 3665 | else { |
| 3666 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3667 | LLDB_LOGF( |
| 3668 | log, |
| 3669 | "Went to stop the private state thread, but it was already invalid."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | void Process::ControlPrivateStateThread(uint32_t signal) { |
| 3674 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 3675 | |
| 3676 | assert(signal == eBroadcastInternalStateControlStop || |
| 3677 | signal == eBroadcastInternalStateControlPause || |
| 3678 | signal == eBroadcastInternalStateControlResume); |
| 3679 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3680 | LLDB_LOGF(log, "Process::%s (signal = %d)", __FUNCTION__, signal); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3681 | |
| 3682 | // Signal the private state thread |
| 3683 | if (m_private_state_thread.IsJoinable()) { |
| 3684 | // Broadcast the event. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3685 | // It is important to do this outside of the if below, because it's |
| 3686 | // possible that the thread state is invalid but that the thread is waiting |
| 3687 | // on a control event instead of simply being on its way out (this should |
| 3688 | // not happen, but it apparently can). |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3689 | LLDB_LOGF(log, "Sending control event of type: %d.", signal); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3690 | std::shared_ptr<EventDataReceipt> event_receipt_sp(new EventDataReceipt()); |
| 3691 | m_private_state_control_broadcaster.BroadcastEvent(signal, |
| 3692 | event_receipt_sp); |
| 3693 | |
| 3694 | // Wait for the event receipt or for the private state thread to exit |
| 3695 | bool receipt_received = false; |
| 3696 | if (PrivateStateThreadIsValid()) { |
| 3697 | while (!receipt_received) { |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 3698 | // Check for a receipt for n seconds and then check if the private |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3699 | // state thread is still around. |
Pavel Labath | 38d67db | 2018-05-03 15:33:41 +0000 | [diff] [blame] | 3700 | receipt_received = |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 3701 | event_receipt_sp->WaitForEventReceived(GetUtilityExpressionTimeout()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3702 | if (!receipt_received) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3703 | // Check if the private state thread is still around. If it isn't |
| 3704 | // then we are done waiting |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3705 | if (!PrivateStateThreadIsValid()) |
| 3706 | break; // Private state thread exited or is exiting, we are done |
| 3707 | } |
| 3708 | } |
| 3709 | } |
| 3710 | |
| 3711 | if (signal == eBroadcastInternalStateControlStop) { |
Jonas Devlieghere | edb52e2 | 2019-05-23 20:25:49 +0000 | [diff] [blame] | 3712 | thread_result_t result = {}; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3713 | m_private_state_thread.Join(&result); |
| 3714 | m_private_state_thread.Reset(); |
| 3715 | } |
| 3716 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3717 | LLDB_LOGF( |
| 3718 | log, |
| 3719 | "Private state thread already dead, no need to signal it to stop."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3720 | } |
| 3721 | } |
| 3722 | |
| 3723 | void Process::SendAsyncInterrupt() { |
| 3724 | if (PrivateStateThreadIsValid()) |
| 3725 | m_private_state_broadcaster.BroadcastEvent(Process::eBroadcastBitInterrupt, |
| 3726 | nullptr); |
| 3727 | else |
| 3728 | BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr); |
| 3729 | } |
| 3730 | |
| 3731 | void Process::HandlePrivateEvent(EventSP &event_sp) { |
| 3732 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 3733 | m_resume_requested = false; |
| 3734 | |
| 3735 | const StateType new_state = |
| 3736 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 3737 | |
| 3738 | // First check to see if anybody wants a shot at this event: |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 3739 | if (m_next_event_action_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3740 | NextEventAction::EventActionResult action_result = |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 3741 | m_next_event_action_up->PerformAction(event_sp); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3742 | LLDB_LOGF(log, "Ran next event action, result was %d.", action_result); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3743 | |
| 3744 | switch (action_result) { |
| 3745 | case NextEventAction::eEventActionSuccess: |
| 3746 | SetNextEventAction(nullptr); |
| 3747 | break; |
| 3748 | |
| 3749 | case NextEventAction::eEventActionRetry: |
| 3750 | break; |
| 3751 | |
| 3752 | case NextEventAction::eEventActionExit: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3753 | // Handle Exiting Here. If we already got an exited event, we should |
| 3754 | // just propagate it. Otherwise, swallow this event, and set our state |
| 3755 | // to exit so the next event will kill us. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3756 | if (new_state != eStateExited) { |
| 3757 | // FIXME: should cons up an exited event, and discard this one. |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 3758 | SetExitStatus(0, m_next_event_action_up->GetExitString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3759 | SetNextEventAction(nullptr); |
| 3760 | return; |
| 3761 | } |
| 3762 | SetNextEventAction(nullptr); |
| 3763 | break; |
| 3764 | } |
| 3765 | } |
| 3766 | |
| 3767 | // See if we should broadcast this state to external clients? |
| 3768 | const bool should_broadcast = ShouldBroadcastEvent(event_sp.get()); |
| 3769 | |
| 3770 | if (should_broadcast) { |
| 3771 | const bool is_hijacked = IsHijackedForEvent(eBroadcastBitStateChanged); |
| 3772 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3773 | LLDB_LOGF(log, |
| 3774 | "Process::%s (pid = %" PRIu64 |
| 3775 | ") broadcasting new state %s (old state %s) to %s", |
| 3776 | __FUNCTION__, GetID(), StateAsCString(new_state), |
| 3777 | StateAsCString(GetState()), |
| 3778 | is_hijacked ? "hijacked" : "public"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3779 | } |
| 3780 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
| 3781 | if (StateIsRunningState(new_state)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3782 | // Only push the input handler if we aren't fowarding events, as this |
| 3783 | // means the curses GUI is in use... Or don't push it if we are launching |
| 3784 | // since it will come up stopped. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3785 | if (!GetTarget().GetDebugger().IsForwardingEvents() && |
| 3786 | new_state != eStateLaunching && new_state != eStateAttaching) { |
| 3787 | PushProcessIOHandler(); |
| 3788 | m_iohandler_sync.SetValue(m_iohandler_sync.GetValue() + 1, |
| 3789 | eBroadcastAlways); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3790 | LLDB_LOGF(log, "Process::%s updated m_iohandler_sync to %d", |
| 3791 | __FUNCTION__, m_iohandler_sync.GetValue()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3792 | } |
| 3793 | } else if (StateIsStoppedState(new_state, false)) { |
| 3794 | if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3795 | // If the lldb_private::Debugger is handling the events, we don't want |
| 3796 | // to pop the process IOHandler here, we want to do it when we receive |
| 3797 | // the stopped event so we can carefully control when the process |
| 3798 | // IOHandler is popped because when we stop we want to display some |
| 3799 | // text stating how and why we stopped, then maybe some |
| 3800 | // process/thread/frame info, and then we want the "(lldb) " prompt to |
| 3801 | // show up. If we pop the process IOHandler here, then we will cause |
| 3802 | // the command interpreter to become the top IOHandler after the |
| 3803 | // process pops off and it will update its prompt right away... See the |
| 3804 | // Debugger.cpp file where it calls the function as |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3805 | // "process_sp->PopProcessIOHandler()" to see where I am talking about. |
| 3806 | // Otherwise we end up getting overlapping "(lldb) " prompts and |
| 3807 | // garbled output. |
| 3808 | // |
| 3809 | // 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] | 3810 | // by "m_target.GetDebugger().IsHandlingEvents()" returning false) or |
| 3811 | // we are hijacked, then we always pop the process IO handler manually. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3812 | // Hijacking happens when the internal process state thread is running |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3813 | // thread plans, or when commands want to run in synchronous mode and |
| 3814 | // they call "process->WaitForProcessToStop()". An example of something |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3815 | // that will hijack the events is a simple expression: |
| 3816 | // |
| 3817 | // (lldb) expr (int)puts("hello") |
| 3818 | // |
| 3819 | // This will cause the internal process state thread to resume and halt |
| 3820 | // the process (and _it_ will hijack the eBroadcastBitStateChanged |
| 3821 | // events) and we do need the IO handler to be pushed and popped |
| 3822 | // correctly. |
| 3823 | |
| 3824 | if (is_hijacked || !GetTarget().GetDebugger().IsHandlingEvents()) |
| 3825 | PopProcessIOHandler(); |
| 3826 | } |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | BroadcastEvent(event_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3830 | } else { |
| 3831 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3832 | LLDB_LOGF( |
| 3833 | log, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3834 | "Process::%s (pid = %" PRIu64 |
| 3835 | ") suppressing state %s (old state %s): should_broadcast == false", |
| 3836 | __FUNCTION__, GetID(), StateAsCString(new_state), |
| 3837 | StateAsCString(GetState())); |
| 3838 | } |
| 3839 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3842 | Status Process::HaltPrivate() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3843 | EventSP event_sp; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3844 | Status error(WillHalt()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3845 | if (error.Fail()) |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3846 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3847 | |
| 3848 | // Ask the process subclass to actually halt our process |
| 3849 | bool caused_stop; |
| 3850 | error = DoHalt(caused_stop); |
| 3851 | |
| 3852 | DidHalt(); |
| 3853 | return error; |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3856 | thread_result_t Process::PrivateStateThread(void *arg) { |
| 3857 | std::unique_ptr<PrivateStateThreadArgs> args_up( |
| 3858 | static_cast<PrivateStateThreadArgs *>(arg)); |
| 3859 | thread_result_t result = |
| 3860 | args_up->process->RunPrivateStateThread(args_up->is_secondary_thread); |
| 3861 | return result; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3864 | thread_result_t Process::RunPrivateStateThread(bool is_secondary_thread) { |
| 3865 | bool control_only = true; |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3866 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3867 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3868 | LLDB_LOGF(log, "Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...", |
| 3869 | __FUNCTION__, static_cast<void *>(this), GetID()); |
Jason Molenda | ede3193 | 2015-04-17 05:01:58 +0000 | [diff] [blame] | 3870 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3871 | bool exit_now = false; |
| 3872 | bool interrupt_requested = false; |
| 3873 | while (!exit_now) { |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3874 | EventSP event_sp; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3875 | GetEventsPrivate(event_sp, llvm::None, control_only); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3876 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3877 | LLDB_LOGF(log, |
| 3878 | "Process::%s (arg = %p, pid = %" PRIu64 |
| 3879 | ") got a control event: %d", |
| 3880 | __FUNCTION__, static_cast<void *>(this), GetID(), |
| 3881 | event_sp->GetType()); |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3882 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3883 | switch (event_sp->GetType()) { |
| 3884 | case eBroadcastInternalStateControlStop: |
| 3885 | exit_now = true; |
| 3886 | break; // doing any internal state management below |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3887 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3888 | case eBroadcastInternalStateControlPause: |
| 3889 | control_only = true; |
| 3890 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3891 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3892 | case eBroadcastInternalStateControlResume: |
| 3893 | control_only = false; |
| 3894 | break; |
| 3895 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3896 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3897 | continue; |
| 3898 | } else if (event_sp->GetType() == eBroadcastBitInterrupt) { |
| 3899 | if (m_public_state.GetValue() == eStateAttaching) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3900 | LLDB_LOGF(log, |
| 3901 | "Process::%s (arg = %p, pid = %" PRIu64 |
| 3902 | ") woke up with an interrupt while attaching - " |
| 3903 | "forwarding interrupt.", |
| 3904 | __FUNCTION__, static_cast<void *>(this), GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3905 | BroadcastEvent(eBroadcastBitInterrupt, nullptr); |
| 3906 | } else if (StateIsRunningState(m_last_broadcast_state)) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3907 | LLDB_LOGF(log, |
| 3908 | "Process::%s (arg = %p, pid = %" PRIu64 |
| 3909 | ") woke up with an interrupt - Halting.", |
| 3910 | __FUNCTION__, static_cast<void *>(this), GetID()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3911 | Status error = HaltPrivate(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3912 | if (error.Fail() && log) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3913 | LLDB_LOGF(log, |
| 3914 | "Process::%s (arg = %p, pid = %" PRIu64 |
| 3915 | ") failed to halt the process: %s", |
| 3916 | __FUNCTION__, static_cast<void *>(this), GetID(), |
| 3917 | error.AsCString()); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3918 | // Halt should generate a stopped event. Make a note of the fact that |
| 3919 | // we were doing the interrupt, so we can set the interrupted flag |
| 3920 | // after we receive the event. We deliberately set this to true even if |
| 3921 | // HaltPrivate failed, so that we can interrupt on the next natural |
| 3922 | // stop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3923 | interrupt_requested = true; |
| 3924 | } else { |
| 3925 | // 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] | 3926 | // running and sends an interrupt request, but the process actually |
| 3927 | // stops before we receive it. In that case, we can just ignore the |
| 3928 | // request. We use m_last_broadcast_state, because the Stopped event |
| 3929 | // may not have been popped of the event queue yet, which is when the |
| 3930 | // public state gets updated. |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3931 | LLDB_LOGF(log, |
| 3932 | "Process::%s ignoring interrupt as we have already stopped.", |
| 3933 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3934 | } |
| 3935 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3938 | const StateType internal_state = |
| 3939 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3940 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3941 | if (internal_state != eStateInvalid) { |
| 3942 | if (m_clear_thread_plans_on_stop && |
| 3943 | StateIsStoppedState(internal_state, true)) { |
| 3944 | m_clear_thread_plans_on_stop = false; |
| 3945 | m_thread_list.DiscardThreadPlans(); |
| 3946 | } |
| 3947 | |
| 3948 | if (interrupt_requested) { |
| 3949 | if (StateIsStoppedState(internal_state, true)) { |
| 3950 | // 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] | 3951 | // so clients can tell an interrupted process from a natural stop |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3952 | ProcessEventData::SetInterruptedInEvent(event_sp.get(), true); |
| 3953 | interrupt_requested = false; |
| 3954 | } else if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3955 | LLDB_LOGF(log, |
| 3956 | "Process::%s interrupt_requested, but a non-stopped " |
| 3957 | "state '%s' received.", |
| 3958 | __FUNCTION__, StateAsCString(internal_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3959 | } |
| 3960 | } |
| 3961 | |
| 3962 | HandlePrivateEvent(event_sp); |
| 3963 | } |
| 3964 | |
| 3965 | if (internal_state == eStateInvalid || internal_state == eStateExited || |
| 3966 | internal_state == eStateDetached) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3967 | LLDB_LOGF(log, |
| 3968 | "Process::%s (arg = %p, pid = %" PRIu64 |
| 3969 | ") about to exit with internal state %s...", |
| 3970 | __FUNCTION__, static_cast<void *>(this), GetID(), |
| 3971 | StateAsCString(internal_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3972 | |
| 3973 | break; |
| 3974 | } |
| 3975 | } |
| 3976 | |
| 3977 | // Verify log is still enabled before attempting to write to it... |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3978 | LLDB_LOGF(log, "Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", |
| 3979 | __FUNCTION__, static_cast<void *>(this), GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3980 | |
| 3981 | // 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] | 3982 | // will have already acquired the public_run_lock, and isn't done with what |
| 3983 | // 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] | 3984 | if (!is_secondary_thread) |
| 3985 | m_public_run_lock.SetStopped(); |
Jonas Devlieghere | edb52e2 | 2019-05-23 20:25:49 +0000 | [diff] [blame] | 3986 | return {}; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3989 | // Process Event Data |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3990 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3991 | Process::ProcessEventData::ProcessEventData() |
| 3992 | : EventData(), m_process_wp(), m_state(eStateInvalid), m_restarted(false), |
| 3993 | m_update_state(0), m_interrupted(false) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3994 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3995 | Process::ProcessEventData::ProcessEventData(const ProcessSP &process_sp, |
| 3996 | StateType state) |
| 3997 | : EventData(), m_process_wp(), m_state(state), m_restarted(false), |
| 3998 | m_update_state(0), m_interrupted(false) { |
| 3999 | if (process_sp) |
| 4000 | m_process_wp = process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 4003 | Process::ProcessEventData::~ProcessEventData() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4004 | |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 4005 | ConstString Process::ProcessEventData::GetFlavorString() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4006 | static ConstString g_flavor("Process::ProcessEventData"); |
| 4007 | return g_flavor; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 4010 | ConstString Process::ProcessEventData::GetFlavor() const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4011 | return ProcessEventData::GetFlavorString(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
Ilya Bukonkin | 3b43f00 | 2020-06-11 14:59:19 -0700 | [diff] [blame] | 4014 | bool Process::ProcessEventData::ShouldStop(Event *event_ptr, |
| 4015 | bool &found_valid_stopinfo) { |
| 4016 | found_valid_stopinfo = false; |
| 4017 | |
| 4018 | ProcessSP process_sp(m_process_wp.lock()); |
| 4019 | if (!process_sp) |
| 4020 | return false; |
| 4021 | |
| 4022 | ThreadList &curr_thread_list = process_sp->GetThreadList(); |
| 4023 | uint32_t num_threads = curr_thread_list.GetSize(); |
| 4024 | uint32_t idx; |
| 4025 | |
| 4026 | // The actions might change one of the thread's stop_info's opinions about |
| 4027 | // whether we should stop the process, so we need to query that as we go. |
| 4028 | |
| 4029 | // One other complication here, is that we try to catch any case where the |
| 4030 | // target has run (except for expressions) and immediately exit, but if we |
| 4031 | // get that wrong (which is possible) then the thread list might have |
| 4032 | // changed, and that would cause our iteration here to crash. We could |
| 4033 | // make a copy of the thread list, but we'd really like to also know if it |
| 4034 | // has changed at all, so we make up a vector of the thread ID's and check |
| 4035 | // what we get back against this list & bag out if anything differs. |
| 4036 | ThreadList not_suspended_thread_list(process_sp.get()); |
| 4037 | std::vector<uint32_t> thread_index_array(num_threads); |
| 4038 | uint32_t not_suspended_idx = 0; |
| 4039 | for (idx = 0; idx < num_threads; ++idx) { |
| 4040 | lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx); |
| 4041 | |
| 4042 | /* |
| 4043 | Filter out all suspended threads, they could not be the reason |
| 4044 | of stop and no need to perform any actions on them. |
| 4045 | */ |
| 4046 | if (thread_sp->GetResumeState() != eStateSuspended) { |
| 4047 | not_suspended_thread_list.AddThread(thread_sp); |
| 4048 | thread_index_array[not_suspended_idx] = thread_sp->GetIndexID(); |
| 4049 | not_suspended_idx++; |
| 4050 | } |
| 4051 | } |
| 4052 | |
| 4053 | // Use this to track whether we should continue from here. We will only |
| 4054 | // continue the target running if no thread says we should stop. Of course |
| 4055 | // if some thread's PerformAction actually sets the target running, then it |
| 4056 | // doesn't matter what the other threads say... |
| 4057 | |
| 4058 | bool still_should_stop = false; |
| 4059 | |
| 4060 | // Sometimes - for instance if we have a bug in the stub we are talking to, |
| 4061 | // we stop but no thread has a valid stop reason. In that case we should |
| 4062 | // just stop, because we have no way of telling what the right thing to do |
| 4063 | // is, and it's better to let the user decide than continue behind their |
| 4064 | // backs. |
| 4065 | |
| 4066 | for (idx = 0; idx < not_suspended_thread_list.GetSize(); ++idx) { |
| 4067 | curr_thread_list = process_sp->GetThreadList(); |
| 4068 | if (curr_thread_list.GetSize() != num_threads) { |
| 4069 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | |
| 4070 | LIBLLDB_LOG_PROCESS)); |
| 4071 | LLDB_LOGF( |
| 4072 | log, |
| 4073 | "Number of threads changed from %u to %u while processing event.", |
| 4074 | num_threads, curr_thread_list.GetSize()); |
| 4075 | break; |
| 4076 | } |
| 4077 | |
| 4078 | lldb::ThreadSP thread_sp = not_suspended_thread_list.GetThreadAtIndex(idx); |
| 4079 | |
| 4080 | if (thread_sp->GetIndexID() != thread_index_array[idx]) { |
| 4081 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | |
| 4082 | LIBLLDB_LOG_PROCESS)); |
| 4083 | LLDB_LOGF(log, |
| 4084 | "The thread at position %u changed from %u to %u while " |
| 4085 | "processing event.", |
| 4086 | idx, thread_index_array[idx], thread_sp->GetIndexID()); |
| 4087 | break; |
| 4088 | } |
| 4089 | |
| 4090 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo(); |
| 4091 | if (stop_info_sp && stop_info_sp->IsValid()) { |
| 4092 | found_valid_stopinfo = true; |
| 4093 | bool this_thread_wants_to_stop; |
| 4094 | if (stop_info_sp->GetOverrideShouldStop()) { |
| 4095 | this_thread_wants_to_stop = |
| 4096 | stop_info_sp->GetOverriddenShouldStopValue(); |
| 4097 | } else { |
| 4098 | stop_info_sp->PerformAction(event_ptr); |
| 4099 | // The stop action might restart the target. If it does, then we |
| 4100 | // want to mark that in the event so that whoever is receiving it |
| 4101 | // will know to wait for the running event and reflect that state |
| 4102 | // appropriately. We also need to stop processing actions, since they |
| 4103 | // aren't expecting the target to be running. |
| 4104 | |
| 4105 | // FIXME: we might have run. |
| 4106 | if (stop_info_sp->HasTargetRunSinceMe()) { |
| 4107 | SetRestarted(true); |
| 4108 | break; |
| 4109 | } |
| 4110 | |
| 4111 | this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr); |
| 4112 | } |
| 4113 | |
| 4114 | if (!still_should_stop) |
| 4115 | still_should_stop = this_thread_wants_to_stop; |
| 4116 | } |
| 4117 | } |
| 4118 | |
| 4119 | return still_should_stop; |
| 4120 | } |
| 4121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4122 | void Process::ProcessEventData::DoOnRemoval(Event *event_ptr) { |
| 4123 | ProcessSP process_sp(m_process_wp.lock()); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 4124 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4125 | if (!process_sp) |
| 4126 | return; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 4127 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4128 | // 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] | 4129 | // pulled off of the private process event queue, and then any number of |
| 4130 | // times, first when it gets pulled off of the public event queue, then other |
| 4131 | // times when we're pretending that this is where we stopped at the end of |
| 4132 | // expression evaluation. m_update_state is used to distinguish these three |
| 4133 | // cases; it is 0 when we're just pulling it off for private handling, and > |
| 4134 | // 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] | 4135 | // command handling then. |
| 4136 | if (m_update_state != 1) |
| 4137 | return; |
| 4138 | |
| 4139 | process_sp->SetPublicState( |
| 4140 | m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr)); |
| 4141 | |
Jason Molenda | b3a3cd1 | 2016-12-08 06:27:29 +0000 | [diff] [blame] | 4142 | if (m_state == eStateStopped && !m_restarted) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4143 | // Let process subclasses know we are about to do a public stop and do |
| 4144 | // anything they might need to in order to speed up register and memory |
| 4145 | // accesses. |
Jason Molenda | b3a3cd1 | 2016-12-08 06:27:29 +0000 | [diff] [blame] | 4146 | process_sp->WillPublicStop(); |
| 4147 | } |
| 4148 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4149 | // 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] | 4150 | // than a plain interrupt (e.g. we had already stopped for a breakpoint when |
| 4151 | // 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] | 4152 | // end up restarting the process. |
| 4153 | if (m_interrupted) |
| 4154 | return; |
| 4155 | |
Ilya Bukonkin | 3b43f00 | 2020-06-11 14:59:19 -0700 | [diff] [blame] | 4156 | // If we're not stopped or have restarted, then skip the StopInfo actions: |
| 4157 | if (m_state != eStateStopped || m_restarted) { |
| 4158 | return; |
| 4159 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4160 | |
Ilya Bukonkin | 3b43f00 | 2020-06-11 14:59:19 -0700 | [diff] [blame] | 4161 | bool does_anybody_have_an_opinion = false; |
| 4162 | bool still_should_stop = ShouldStop(event_ptr, does_anybody_have_an_opinion); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4163 | |
Ilya Bukonkin | 3b43f00 | 2020-06-11 14:59:19 -0700 | [diff] [blame] | 4164 | if (GetRestarted()) { |
| 4165 | return; |
| 4166 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4167 | |
Ilya Bukonkin | 3b43f00 | 2020-06-11 14:59:19 -0700 | [diff] [blame] | 4168 | if (!still_should_stop && does_anybody_have_an_opinion) { |
| 4169 | // We've been asked to continue, so do that here. |
| 4170 | SetRestarted(true); |
| 4171 | // Use the public resume method here, since this is just extending a |
| 4172 | // public resume. |
| 4173 | process_sp->PrivateResume(); |
| 4174 | } else { |
| 4175 | bool hijacked = process_sp->IsHijackedForEvent(eBroadcastBitStateChanged) && |
| 4176 | !process_sp->StateChangedIsHijackedForSynchronousResume(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4177 | |
Ilya Bukonkin | 3b43f00 | 2020-06-11 14:59:19 -0700 | [diff] [blame] | 4178 | if (!hijacked) { |
| 4179 | // If we didn't restart, run the Stop Hooks here. |
| 4180 | // Don't do that if state changed events aren't hooked up to the |
| 4181 | // public (or SyncResume) broadcasters. StopHooks are just for |
| 4182 | // real public stops. They might also restart the target, |
| 4183 | // so watch for that. |
Jim Ingham | be66987 | 2020-10-02 12:43:24 -0700 | [diff] [blame] | 4184 | if (process_sp->GetTarget().RunStopHooks()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4185 | SetRestarted(true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4186 | } |
| 4187 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4190 | void Process::ProcessEventData::Dump(Stream *s) const { |
| 4191 | ProcessSP process_sp(m_process_wp.lock()); |
Greg Clayton | aeb3b8b | 2015-05-29 03:20:37 +0000 | [diff] [blame] | 4192 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4193 | if (process_sp) |
| 4194 | s->Printf(" process = %p (pid = %" PRIu64 "), ", |
| 4195 | static_cast<void *>(process_sp.get()), process_sp->GetID()); |
| 4196 | else |
| 4197 | s->PutCString(" process = NULL, "); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4199 | s->Printf("state = %s", StateAsCString(GetState())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
| 4202 | const Process::ProcessEventData * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4203 | Process::ProcessEventData::GetEventDataFromEvent(const Event *event_ptr) { |
| 4204 | if (event_ptr) { |
| 4205 | const EventData *event_data = event_ptr->GetData(); |
| 4206 | if (event_data && |
| 4207 | event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 4208 | return static_cast<const ProcessEventData *>(event_ptr->GetData()); |
| 4209 | } |
| 4210 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | ProcessSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4214 | Process::ProcessEventData::GetProcessFromEvent(const Event *event_ptr) { |
| 4215 | ProcessSP process_sp; |
| 4216 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4217 | if (data) |
| 4218 | process_sp = data->GetProcessSP(); |
| 4219 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4222 | StateType Process::ProcessEventData::GetStateFromEvent(const Event *event_ptr) { |
| 4223 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4224 | if (data == nullptr) |
| 4225 | return eStateInvalid; |
| 4226 | else |
| 4227 | return data->GetState(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4230 | bool Process::ProcessEventData::GetRestartedFromEvent(const Event *event_ptr) { |
| 4231 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4232 | if (data == nullptr) |
| 4233 | return false; |
| 4234 | else |
| 4235 | return data->GetRestarted(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4238 | void Process::ProcessEventData::SetRestartedInEvent(Event *event_ptr, |
| 4239 | bool new_value) { |
| 4240 | ProcessEventData *data = |
| 4241 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4242 | if (data != nullptr) |
| 4243 | data->SetRestarted(new_value); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4246 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4247 | Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr) { |
| 4248 | ProcessEventData *data = |
| 4249 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4250 | if (data != nullptr) |
| 4251 | return data->GetNumRestartedReasons(); |
| 4252 | else |
| 4253 | return 0; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
| 4256 | const char * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4257 | Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, |
| 4258 | size_t idx) { |
| 4259 | ProcessEventData *data = |
| 4260 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4261 | if (data != nullptr) |
| 4262 | return data->GetRestartedReasonAtIndex(idx); |
| 4263 | else |
| 4264 | return nullptr; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4267 | void Process::ProcessEventData::AddRestartedReason(Event *event_ptr, |
| 4268 | const char *reason) { |
| 4269 | ProcessEventData *data = |
| 4270 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4271 | if (data != nullptr) |
| 4272 | data->AddRestartedReason(reason); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4275 | bool Process::ProcessEventData::GetInterruptedFromEvent( |
| 4276 | const Event *event_ptr) { |
| 4277 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4278 | if (data == nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4279 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4280 | else |
| 4281 | return data->GetInterrupted(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4284 | void Process::ProcessEventData::SetInterruptedInEvent(Event *event_ptr, |
| 4285 | bool new_value) { |
| 4286 | ProcessEventData *data = |
| 4287 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4288 | if (data != nullptr) |
| 4289 | data->SetInterrupted(new_value); |
Greg Clayton | d9e416c | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4292 | bool Process::ProcessEventData::SetUpdateStateOnRemoval(Event *event_ptr) { |
| 4293 | ProcessEventData *data = |
| 4294 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4295 | if (data) { |
| 4296 | data->SetUpdateStateOnRemoval(); |
| 4297 | return true; |
| 4298 | } |
| 4299 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
Jim Ingham | b87b9e6 | 2018-06-26 23:38:58 +0000 | [diff] [blame] | 4302 | lldb::TargetSP Process::CalculateTarget() { return m_target_wp.lock(); } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4303 | |
| 4304 | void Process::CalculateExecutionContext(ExecutionContext &exe_ctx) { |
| 4305 | exe_ctx.SetTargetPtr(&GetTarget()); |
| 4306 | exe_ctx.SetProcessPtr(this); |
| 4307 | exe_ctx.SetThreadPtr(nullptr); |
| 4308 | exe_ctx.SetFramePtr(nullptr); |
| 4309 | } |
| 4310 | |
| 4311 | // uint32_t |
| 4312 | // Process::ListProcessesMatchingName (const char *name, StringList &matches, |
| 4313 | // std::vector<lldb::pid_t> &pids) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4314 | //{ |
| 4315 | // return 0; |
| 4316 | //} |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4317 | // |
| 4318 | // ArchSpec |
| 4319 | // Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4320 | //{ |
| 4321 | // return Host::GetArchSpecForExistingProcess (pid); |
| 4322 | //} |
| 4323 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4324 | // ArchSpec |
| 4325 | // Process::GetArchSpecForExistingProcess (const char *process_name) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4326 | //{ |
| 4327 | // return Host::GetArchSpecForExistingProcess (process_name); |
| 4328 | //} |
Eugene Zelenko | da8cf8a | 2016-03-01 00:55:51 +0000 | [diff] [blame] | 4329 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4330 | void Process::AppendSTDOUT(const char *s, size_t len) { |
| 4331 | std::lock_guard<std::recursive_mutex> guard(m_stdio_communication_mutex); |
| 4332 | m_stdout_data.append(s, len); |
| 4333 | BroadcastEventIfUnique(eBroadcastBitSTDOUT, |
| 4334 | new ProcessEventData(shared_from_this(), GetState())); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4335 | } |
| 4336 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4337 | void Process::AppendSTDERR(const char *s, size_t len) { |
| 4338 | std::lock_guard<std::recursive_mutex> guard(m_stdio_communication_mutex); |
| 4339 | m_stderr_data.append(s, len); |
| 4340 | BroadcastEventIfUnique(eBroadcastBitSTDERR, |
| 4341 | new ProcessEventData(shared_from_this(), GetState())); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4344 | void Process::BroadcastAsyncProfileData(const std::string &one_profile_data) { |
| 4345 | std::lock_guard<std::recursive_mutex> guard(m_profile_data_comm_mutex); |
| 4346 | m_profile_data.push_back(one_profile_data); |
| 4347 | BroadcastEventIfUnique(eBroadcastBitProfileData, |
| 4348 | new ProcessEventData(shared_from_this(), GetState())); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4351 | void Process::BroadcastStructuredData(const StructuredData::ObjectSP &object_sp, |
| 4352 | const StructuredDataPluginSP &plugin_sp) { |
| 4353 | BroadcastEvent( |
| 4354 | eBroadcastBitStructuredData, |
| 4355 | new EventDataStructuredData(shared_from_this(), object_sp, plugin_sp)); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | StructuredDataPluginSP |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 4359 | Process::GetStructuredDataPlugin(ConstString type_name) const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4360 | auto find_it = m_structured_data_plugin_map.find(type_name); |
| 4361 | if (find_it != m_structured_data_plugin_map.end()) |
| 4362 | return find_it->second; |
| 4363 | else |
| 4364 | return StructuredDataPluginSP(); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4367 | size_t Process::GetAsyncProfileData(char *buf, size_t buf_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4368 | std::lock_guard<std::recursive_mutex> guard(m_profile_data_comm_mutex); |
| 4369 | if (m_profile_data.empty()) |
| 4370 | return 0; |
| 4371 | |
| 4372 | std::string &one_profile_data = m_profile_data.front(); |
| 4373 | size_t bytes_available = one_profile_data.size(); |
| 4374 | if (bytes_available > 0) { |
| 4375 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4376 | LLDB_LOGF(log, "Process::GetProfileData (buf = %p, size = %" PRIu64 ")", |
| 4377 | static_cast<void *>(buf), static_cast<uint64_t>(buf_size)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4378 | if (bytes_available > buf_size) { |
| 4379 | memcpy(buf, one_profile_data.c_str(), buf_size); |
| 4380 | one_profile_data.erase(0, buf_size); |
| 4381 | bytes_available = buf_size; |
| 4382 | } else { |
| 4383 | memcpy(buf, one_profile_data.c_str(), bytes_available); |
| 4384 | m_profile_data.erase(m_profile_data.begin()); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4385 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4386 | } |
| 4387 | return bytes_available; |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4390 | // Process STDIO |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4391 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4392 | size_t Process::GetSTDOUT(char *buf, size_t buf_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4393 | std::lock_guard<std::recursive_mutex> guard(m_stdio_communication_mutex); |
| 4394 | size_t bytes_available = m_stdout_data.size(); |
| 4395 | if (bytes_available > 0) { |
| 4396 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4397 | LLDB_LOGF(log, "Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")", |
| 4398 | static_cast<void *>(buf), static_cast<uint64_t>(buf_size)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4399 | if (bytes_available > buf_size) { |
| 4400 | memcpy(buf, m_stdout_data.c_str(), buf_size); |
| 4401 | m_stdout_data.erase(0, buf_size); |
| 4402 | bytes_available = buf_size; |
| 4403 | } else { |
| 4404 | memcpy(buf, m_stdout_data.c_str(), bytes_available); |
| 4405 | m_stdout_data.clear(); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4406 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4407 | } |
| 4408 | return bytes_available; |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4411 | size_t Process::GetSTDERR(char *buf, size_t buf_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4412 | std::lock_guard<std::recursive_mutex> gaurd(m_stdio_communication_mutex); |
| 4413 | size_t bytes_available = m_stderr_data.size(); |
| 4414 | if (bytes_available > 0) { |
| 4415 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4416 | LLDB_LOGF(log, "Process::GetSTDERR (buf = %p, size = %" PRIu64 ")", |
| 4417 | static_cast<void *>(buf), static_cast<uint64_t>(buf_size)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4418 | if (bytes_available > buf_size) { |
| 4419 | memcpy(buf, m_stderr_data.c_str(), buf_size); |
| 4420 | m_stderr_data.erase(0, buf_size); |
| 4421 | bytes_available = buf_size; |
| 4422 | } else { |
| 4423 | memcpy(buf, m_stderr_data.c_str(), bytes_available); |
| 4424 | m_stderr_data.clear(); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4425 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4426 | } |
| 4427 | return bytes_available; |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4428 | } |
| 4429 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4430 | void Process::STDIOReadThreadBytesReceived(void *baton, const void *src, |
| 4431 | size_t src_len) { |
| 4432 | Process *process = (Process *)baton; |
| 4433 | process->AppendSTDOUT(static_cast<const char *>(src), src_len); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4436 | class IOHandlerProcessSTDIO : public IOHandler { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4437 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4438 | IOHandlerProcessSTDIO(Process *process, int write_fd) |
| 4439 | : IOHandler(process->GetTarget().GetDebugger(), |
| 4440 | IOHandler::Type::ProcessIO), |
Jonas Devlieghere | 948786c | 2019-09-23 20:36:46 +0000 | [diff] [blame] | 4441 | m_process(process), |
Lawrence D'Anna | 7ca15ba | 2019-09-27 14:33:35 +0000 | [diff] [blame] | 4442 | m_read_file(GetInputFD(), File::eOpenOptionRead, false), |
Jonas Devlieghere | 948786c | 2019-09-23 20:36:46 +0000 | [diff] [blame] | 4443 | m_write_file(write_fd, File::eOpenOptionWrite, false) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4444 | m_pipe.CreateNew(false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | ~IOHandlerProcessSTDIO() override = default; |
| 4448 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4449 | // Each IOHandler gets to run until it is done. It should read data from the |
| 4450 | // "in" and place output into "out" and "err and return when done. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4451 | void Run() override { |
| 4452 | if (!m_read_file.IsValid() || !m_write_file.IsValid() || |
| 4453 | !m_pipe.CanRead() || !m_pipe.CanWrite()) { |
| 4454 | SetIsDone(true); |
| 4455 | return; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4458 | SetIsDone(false); |
| 4459 | const int read_fd = m_read_file.GetDescriptor(); |
| 4460 | TerminalState terminal_state; |
| 4461 | terminal_state.Save(read_fd, false); |
| 4462 | Terminal terminal(read_fd); |
| 4463 | terminal.SetCanonical(false); |
| 4464 | terminal.SetEcho(false); |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4465 | // FD_ZERO, FD_SET are not supported on windows |
Hafiz Abid Qadeer | 6eff101 | 2014-03-12 10:45:23 +0000 | [diff] [blame] | 4466 | #ifndef _WIN32 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4467 | const int pipe_read_fd = m_pipe.GetReadFileDescriptor(); |
| 4468 | m_is_running = true; |
| 4469 | while (!GetIsDone()) { |
| 4470 | SelectHelper select_helper; |
| 4471 | select_helper.FDSetRead(read_fd); |
| 4472 | select_helper.FDSetRead(pipe_read_fd); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4473 | Status error = select_helper.Select(); |
Greg Clayton | 860582f | 2016-02-26 17:36:44 +0000 | [diff] [blame] | 4474 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4475 | if (error.Fail()) { |
Greg Clayton | 860582f | 2016-02-26 17:36:44 +0000 | [diff] [blame] | 4476 | SetIsDone(true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4477 | } else { |
| 4478 | char ch = 0; |
| 4479 | size_t n; |
| 4480 | if (select_helper.FDIsSetRead(read_fd)) { |
| 4481 | n = 1; |
| 4482 | if (m_read_file.Read(&ch, n).Success() && n == 1) { |
| 4483 | if (m_write_file.Write(&ch, n).Fail() || n != 1) |
| 4484 | SetIsDone(true); |
| 4485 | } else |
| 4486 | SetIsDone(true); |
Greg Clayton | 860582f | 2016-02-26 17:36:44 +0000 | [diff] [blame] | 4487 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4488 | if (select_helper.FDIsSetRead(pipe_read_fd)) { |
| 4489 | size_t bytes_read; |
| 4490 | // Consume the interrupt byte |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4491 | Status error = m_pipe.Read(&ch, 1, bytes_read); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4492 | if (error.Success()) { |
| 4493 | switch (ch) { |
| 4494 | case 'q': |
| 4495 | SetIsDone(true); |
| 4496 | break; |
| 4497 | case 'i': |
| 4498 | if (StateIsRunningState(m_process->GetState())) |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 4499 | m_process->SendAsyncInterrupt(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4500 | break; |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 4501 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4502 | } |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 4503 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4504 | } |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4505 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4506 | m_is_running = false; |
| 4507 | #endif |
| 4508 | terminal_state.Restore(); |
| 4509 | } |
| 4510 | |
| 4511 | void Cancel() override { |
| 4512 | SetIsDone(true); |
| 4513 | // Only write to our pipe to cancel if we are in |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4514 | // IOHandlerProcessSTDIO::Run(). We can end up with a python command that |
| 4515 | // is being run from the command interpreter: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4516 | // |
| 4517 | // (lldb) step_process_thousands_of_times |
| 4518 | // |
| 4519 | // In this case the command interpreter will be in the middle of handling |
| 4520 | // the command and if the process pushes and pops the IOHandler thousands |
| 4521 | // of times, we can end up writing to m_pipe without ever consuming the |
| 4522 | // bytes from the pipe in IOHandlerProcessSTDIO::Run() and end up |
| 4523 | // deadlocking when the pipe gets fed up and blocks until data is consumed. |
| 4524 | if (m_is_running) { |
| 4525 | char ch = 'q'; // Send 'q' for quit |
| 4526 | size_t bytes_written = 0; |
| 4527 | m_pipe.Write(&ch, 1, bytes_written); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4528 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
| 4531 | bool Interrupt() override { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4532 | // Do only things that are safe to do in an interrupt context (like in a |
| 4533 | // SIGINT handler), like write 1 byte to a file descriptor. This will |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4534 | // interrupt the IOHandlerProcessSTDIO::Run() and we can look at the byte |
| 4535 | // that was written to the pipe and then call |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4536 | // m_process->SendAsyncInterrupt() from a much safer location in code. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4537 | if (m_active) { |
| 4538 | char ch = 'i'; // Send 'i' for interrupt |
| 4539 | size_t bytes_written = 0; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4540 | Status result = m_pipe.Write(&ch, 1, bytes_written); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4541 | return result.Success(); |
| 4542 | } else { |
| 4543 | // This IOHandler might be pushed on the stack, but not being run |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4544 | // currently so do the right thing if we aren't actively watching for |
| 4545 | // STDIN by sending the interrupt to the process. Otherwise the write to |
| 4546 | // the pipe above would do nothing. This can happen when the command |
| 4547 | // interpreter is running and gets a "expression ...". It will be on the |
| 4548 | // IOHandler thread and sending the input is complete to the delegate |
| 4549 | // which will cause the expression to run, which will push the process IO |
| 4550 | // handler, but not run it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4551 | |
| 4552 | if (StateIsRunningState(m_process->GetState())) { |
| 4553 | m_process->SendAsyncInterrupt(); |
| 4554 | return true; |
| 4555 | } |
| 4556 | } |
| 4557 | return false; |
| 4558 | } |
| 4559 | |
| 4560 | void GotEOF() override {} |
| 4561 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4562 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4563 | Process *m_process; |
Lawrence D'Anna | f913fd6 | 2019-10-03 04:31:46 +0000 | [diff] [blame] | 4564 | NativeFile m_read_file; // Read from this file (usually actual STDIN for LLDB |
| 4565 | NativeFile m_write_file; // Write to this file (usually the master pty for |
| 4566 | // getting io to debuggee) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4567 | Pipe m_pipe; |
Sam McCall | 6f43d9d | 2016-11-15 10:58:16 +0000 | [diff] [blame] | 4568 | std::atomic<bool> m_is_running{false}; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4569 | }; |
| 4570 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4571 | void Process::SetSTDIOFileDescriptor(int fd) { |
| 4572 | // First set up the Read Thread for reading/handling process I/O |
Pavel Labath | 451741a | 2020-04-02 14:40:59 +0200 | [diff] [blame] | 4573 | m_stdio_communication.SetConnection( |
| 4574 | std::make_unique<ConnectionFileDescriptor>(fd, true)); |
| 4575 | if (m_stdio_communication.IsConnected()) { |
| 4576 | m_stdio_communication.SetReadThreadBytesReceivedCallback( |
| 4577 | STDIOReadThreadBytesReceived, this); |
| 4578 | m_stdio_communication.StartReadThread(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4579 | |
Pavel Labath | 451741a | 2020-04-02 14:40:59 +0200 | [diff] [blame] | 4580 | // Now read thread is set up, set up input reader. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4581 | |
Pavel Labath | 451741a | 2020-04-02 14:40:59 +0200 | [diff] [blame] | 4582 | if (!m_process_input_reader) |
| 4583 | m_process_input_reader = |
| 4584 | std::make_shared<IOHandlerProcessSTDIO>(this, fd); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4585 | } |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4588 | bool Process::ProcessIOHandlerIsActive() { |
| 4589 | IOHandlerSP io_handler_sp(m_process_input_reader); |
| 4590 | if (io_handler_sp) |
| 4591 | return GetTarget().GetDebugger().IsTopIOHandler(io_handler_sp); |
| 4592 | return false; |
Greg Clayton | 6fea17e | 2014-03-03 19:15:20 +0000 | [diff] [blame] | 4593 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4594 | bool Process::PushProcessIOHandler() { |
| 4595 | IOHandlerSP io_handler_sp(m_process_input_reader); |
| 4596 | if (io_handler_sp) { |
| 4597 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4598 | LLDB_LOGF(log, "Process::%s pushing IO handler", __FUNCTION__); |
Pavel Labath | 4446487 | 2015-05-27 12:40:32 +0000 | [diff] [blame] | 4599 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4600 | io_handler_sp->SetIsDone(false); |
Raphael Isemann | c01783a | 2018-08-29 22:50:54 +0000 | [diff] [blame] | 4601 | // If we evaluate an utility function, then we don't cancel the current |
| 4602 | // IOHandler. Our IOHandler is non-interactive and shouldn't disturb the |
| 4603 | // existing IOHandler that potentially provides the user interface (e.g. |
| 4604 | // the IOHandler for Editline). |
| 4605 | bool cancel_top_handler = !m_mod_id.IsRunningUtilityFunction(); |
Jonas Devlieghere | 7ce2de2 | 2020-01-15 14:56:28 -0800 | [diff] [blame] | 4606 | GetTarget().GetDebugger().RunIOHandlerAsync(io_handler_sp, |
| 4607 | cancel_top_handler); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4608 | return true; |
| 4609 | } |
| 4610 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4613 | bool Process::PopProcessIOHandler() { |
| 4614 | IOHandlerSP io_handler_sp(m_process_input_reader); |
| 4615 | if (io_handler_sp) |
Jonas Devlieghere | 7ce2de2 | 2020-01-15 14:56:28 -0800 | [diff] [blame] | 4616 | return GetTarget().GetDebugger().RemoveIOHandler(io_handler_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4617 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 4620 | // The process needs to know about installed plug-ins |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4621 | void Process::SettingsInitialize() { Thread::SettingsInitialize(); } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4622 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4623 | void Process::SettingsTerminate() { Thread::SettingsTerminate(); } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4624 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4625 | namespace { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4626 | // RestorePlanState is used to record the "is private", "is master" and "okay |
| 4627 | // to discard" fields of the plan we are running, and reset it on Clean or on |
| 4628 | // destruction. It will only reset the state once, so you can call Clean and |
| 4629 | // 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] | 4630 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4631 | class RestorePlanState { |
| 4632 | public: |
| 4633 | RestorePlanState(lldb::ThreadPlanSP thread_plan_sp) |
| 4634 | : m_thread_plan_sp(thread_plan_sp), m_already_reset(false) { |
| 4635 | if (m_thread_plan_sp) { |
| 4636 | m_private = m_thread_plan_sp->GetPrivate(); |
| 4637 | m_is_master = m_thread_plan_sp->IsMasterPlan(); |
| 4638 | m_okay_to_discard = m_thread_plan_sp->OkayToDiscard(); |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | ~RestorePlanState() { Clean(); } |
| 4643 | |
| 4644 | void Clean() { |
| 4645 | if (!m_already_reset && m_thread_plan_sp) { |
| 4646 | m_already_reset = true; |
| 4647 | m_thread_plan_sp->SetPrivate(m_private); |
| 4648 | m_thread_plan_sp->SetIsMasterPlan(m_is_master); |
| 4649 | m_thread_plan_sp->SetOkayToDiscard(m_okay_to_discard); |
| 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | private: |
| 4654 | lldb::ThreadPlanSP m_thread_plan_sp; |
| 4655 | bool m_already_reset; |
| 4656 | bool m_private; |
| 4657 | bool m_is_master; |
| 4658 | bool m_okay_to_discard; |
| 4659 | }; |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 4660 | } // anonymous namespace |
Jim Ingham | c60963c | 2015-10-12 19:11:03 +0000 | [diff] [blame] | 4661 | |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4662 | static microseconds |
| 4663 | GetOneThreadExpressionTimeout(const EvaluateExpressionOptions &options) { |
| 4664 | const milliseconds default_one_thread_timeout(250); |
| 4665 | |
| 4666 | // 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] | 4667 | if (!options.GetTimeout()) { |
| 4668 | return options.GetOneThreadTimeout() ? *options.GetOneThreadTimeout() |
| 4669 | : default_one_thread_timeout; |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4670 | } |
| 4671 | |
| 4672 | // If the one thread timeout is set, use it. |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4673 | if (options.GetOneThreadTimeout()) |
| 4674 | return *options.GetOneThreadTimeout(); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4675 | |
| 4676 | // Otherwise use half the total timeout, bounded by the |
| 4677 | // default_one_thread_timeout. |
| 4678 | return std::min<microseconds>(default_one_thread_timeout, |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4679 | *options.GetTimeout() / 2); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | static Timeout<std::micro> |
| 4683 | GetExpressionTimeout(const EvaluateExpressionOptions &options, |
| 4684 | bool before_first_timeout) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4685 | // If we are going to run all threads the whole time, or if we are only going |
| 4686 | // to run one thread, we can just return the overall timeout. |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4687 | if (!options.GetStopOthers() || !options.GetTryAllThreads()) |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4688 | return options.GetTimeout(); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4689 | |
| 4690 | if (before_first_timeout) |
| 4691 | return GetOneThreadExpressionTimeout(options); |
| 4692 | |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4693 | if (!options.GetTimeout()) |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4694 | return llvm::None; |
| 4695 | else |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4696 | return *options.GetTimeout() - GetOneThreadExpressionTimeout(options); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4697 | } |
| 4698 | |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 4699 | static llvm::Optional<ExpressionResults> |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 4700 | HandleStoppedEvent(lldb::tid_t thread_id, const ThreadPlanSP &thread_plan_sp, |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 4701 | RestorePlanState &restorer, const EventSP &event_sp, |
| 4702 | EventSP &event_to_broadcast_sp, |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 4703 | const EvaluateExpressionOptions &options, |
| 4704 | bool handle_interrupts) { |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 4705 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS); |
| 4706 | |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 4707 | ThreadSP thread_sp = thread_plan_sp->GetTarget() |
| 4708 | .GetProcessSP() |
| 4709 | ->GetThreadList() |
| 4710 | .FindThreadByID(thread_id); |
| 4711 | if (!thread_sp) { |
| 4712 | LLDB_LOG(log, |
| 4713 | "The thread on which we were running the " |
| 4714 | "expression: tid = {0}, exited while " |
| 4715 | "the expression was running.", |
| 4716 | thread_id); |
| 4717 | return eExpressionThreadVanished; |
| 4718 | } |
| 4719 | |
| 4720 | ThreadPlanSP plan = thread_sp->GetCompletedPlan(); |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 4721 | if (plan == thread_plan_sp && plan->PlanSucceeded()) { |
| 4722 | LLDB_LOG(log, "execution completed successfully"); |
| 4723 | |
| 4724 | // Restore the plan state so it will get reported as intended when we are |
| 4725 | // done. |
| 4726 | restorer.Clean(); |
| 4727 | return eExpressionCompleted; |
| 4728 | } |
| 4729 | |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 4730 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo(); |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 4731 | if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint && |
| 4732 | stop_info_sp->ShouldNotify(event_sp.get())) { |
| 4733 | LLDB_LOG(log, "stopped for breakpoint: {0}.", stop_info_sp->GetDescription()); |
| 4734 | if (!options.DoesIgnoreBreakpoints()) { |
| 4735 | // Restore the plan state and then force Private to false. We are going |
| 4736 | // 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] | 4737 | // it won't report correctly when we continue to its termination later |
| 4738 | // on. |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 4739 | restorer.Clean(); |
| 4740 | thread_plan_sp->SetPrivate(false); |
| 4741 | event_to_broadcast_sp = event_sp; |
| 4742 | } |
| 4743 | return eExpressionHitBreakpoint; |
| 4744 | } |
| 4745 | |
| 4746 | if (!handle_interrupts && |
| 4747 | Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get())) |
| 4748 | return llvm::None; |
| 4749 | |
| 4750 | LLDB_LOG(log, "thread plan did not successfully complete"); |
| 4751 | if (!options.DoesUnwindOnError()) |
| 4752 | event_to_broadcast_sp = event_sp; |
| 4753 | return eExpressionInterrupted; |
| 4754 | } |
| 4755 | |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 4756 | ExpressionResults |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4757 | Process::RunThreadPlan(ExecutionContext &exe_ctx, |
| 4758 | lldb::ThreadPlanSP &thread_plan_sp, |
| 4759 | const EvaluateExpressionOptions &options, |
| 4760 | DiagnosticManager &diagnostic_manager) { |
| 4761 | ExpressionResults return_value = eExpressionSetupError; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4762 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4763 | std::lock_guard<std::mutex> run_thread_plan_locker(m_run_thread_plan_lock); |
| 4764 | |
| 4765 | if (!thread_plan_sp) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4766 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4767 | eDiagnosticSeverityError, |
| 4768 | "RunThreadPlan called with empty thread plan."); |
| 4769 | return eExpressionSetupError; |
| 4770 | } |
| 4771 | |
| 4772 | if (!thread_plan_sp->ValidatePlan(nullptr)) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4773 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4774 | eDiagnosticSeverityError, |
| 4775 | "RunThreadPlan called with an invalid thread plan."); |
| 4776 | return eExpressionSetupError; |
| 4777 | } |
| 4778 | |
| 4779 | if (exe_ctx.GetProcessPtr() != this) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4780 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 4781 | "RunThreadPlan called on wrong process."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4782 | return eExpressionSetupError; |
| 4783 | } |
| 4784 | |
| 4785 | Thread *thread = exe_ctx.GetThreadPtr(); |
| 4786 | if (thread == nullptr) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4787 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 4788 | "RunThreadPlan called with invalid thread."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4789 | return eExpressionSetupError; |
| 4790 | } |
| 4791 | |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 4792 | // Record the thread's id so we can tell when a thread we were using |
| 4793 | // to run the expression exits during the expression evaluation. |
| 4794 | lldb::tid_t expr_thread_id = thread->GetID(); |
| 4795 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4796 | // 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] | 4797 | // runner. This will restore them when we are done: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4798 | |
| 4799 | RestorePlanState thread_plan_restorer(thread_plan_sp); |
| 4800 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4801 | // We rely on the thread plan we are running returning "PlanCompleted" if |
| 4802 | // when it successfully completes. For that to be true the plan can't be |
| 4803 | // private - since private plans suppress themselves in the GetCompletedPlan |
| 4804 | // call. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4805 | |
| 4806 | thread_plan_sp->SetPrivate(false); |
| 4807 | |
| 4808 | // 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] | 4809 | // when they are done we will end up asking the plan above us whether we |
| 4810 | // should stop, which may give the wrong answer. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4811 | |
| 4812 | thread_plan_sp->SetIsMasterPlan(true); |
| 4813 | thread_plan_sp->SetOkayToDiscard(false); |
| 4814 | |
Raphael Isemann | c01783a | 2018-08-29 22:50:54 +0000 | [diff] [blame] | 4815 | // If we are running some utility expression for LLDB, we now have to mark |
| 4816 | // this in the ProcesModID of this process. This RAII takes care of marking |
| 4817 | // and reverting the mark it once we are done running the expression. |
| 4818 | UtilityFunctionScope util_scope(options.IsForUtilityExpr() ? this : nullptr); |
| 4819 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4820 | if (m_private_state.GetValue() != eStateStopped) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4821 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4822 | eDiagnosticSeverityError, |
| 4823 | "RunThreadPlan called while the private state was not stopped."); |
| 4824 | return eExpressionSetupError; |
| 4825 | } |
| 4826 | |
| 4827 | // Save the thread & frame from the exe_ctx for restoration after we run |
| 4828 | const uint32_t thread_idx_id = thread->GetIndexID(); |
| 4829 | StackFrameSP selected_frame_sp = thread->GetSelectedFrame(); |
| 4830 | if (!selected_frame_sp) { |
| 4831 | thread->SetSelectedFrame(nullptr); |
| 4832 | selected_frame_sp = thread->GetSelectedFrame(); |
| 4833 | if (!selected_frame_sp) { |
| 4834 | diagnostic_manager.Printf( |
| 4835 | eDiagnosticSeverityError, |
| 4836 | "RunThreadPlan called without a selected frame on thread %d", |
| 4837 | thread_idx_id); |
| 4838 | return eExpressionSetupError; |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 4839 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4840 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4841 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4842 | // Make sure the timeout values make sense. The one thread timeout needs to |
| 4843 | // be smaller than the overall timeout. |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4844 | if (options.GetOneThreadTimeout() && options.GetTimeout() && |
| 4845 | *options.GetTimeout() < *options.GetOneThreadTimeout()) { |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4846 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 4847 | "RunThreadPlan called with one thread " |
| 4848 | "timeout greater than total timeout"); |
| 4849 | return eExpressionSetupError; |
| 4850 | } |
| 4851 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4852 | StackID ctx_frame_id = selected_frame_sp->GetStackID(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4853 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4854 | // 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] | 4855 | // We don't want to do that either, so we should arrange to reset them as |
| 4856 | // well. |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4857 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4858 | lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4859 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4860 | uint32_t selected_tid; |
| 4861 | StackID selected_stack_id; |
| 4862 | if (selected_thread_sp) { |
| 4863 | selected_tid = selected_thread_sp->GetIndexID(); |
| 4864 | selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID(); |
| 4865 | } else { |
| 4866 | selected_tid = LLDB_INVALID_THREAD_ID; |
| 4867 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4868 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4869 | HostThread backup_private_state_thread; |
| 4870 | lldb::StateType old_state = eStateInvalid; |
| 4871 | lldb::ThreadPlanSP stopper_base_plan_sp; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4872 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4873 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | |
| 4874 | LIBLLDB_LOG_PROCESS)); |
| 4875 | if (m_private_state_thread.EqualsThread(Host::GetCurrentThread())) { |
| 4876 | // 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] | 4877 | // public events on this thread, since we are the thread that is generating |
| 4878 | // public events. The simplest thing to do is to spin up a temporary thread |
| 4879 | // to handle private state thread events while we are fielding public |
| 4880 | // events here. |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4881 | LLDB_LOGF(log, "Running thread plan on private state thread, spinning up " |
| 4882 | "another state thread to handle the events."); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 4883 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4884 | backup_private_state_thread = m_private_state_thread; |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 4885 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4886 | // 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] | 4887 | // anything it pushes, and then stop, returning control here. But in the |
| 4888 | // normal course of things, the plan above us on the stack would be given a |
| 4889 | // shot at the stop event before deciding to stop, and we don't want that. |
| 4890 | // So we insert a "stopper" base plan on the stack before the plan we want |
| 4891 | // to run. Since base plans always stop and return control to the user, |
| 4892 | // that will do just what we want. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4893 | stopper_base_plan_sp.reset(new ThreadPlanBase(*thread)); |
| 4894 | thread->QueueThreadPlan(stopper_base_plan_sp, false); |
| 4895 | // Have to make sure our public state is stopped, since otherwise the |
| 4896 | // reporting logic below doesn't work correctly. |
| 4897 | old_state = m_public_state.GetValue(); |
| 4898 | m_public_state.SetValueNoLock(eStateStopped); |
| 4899 | |
| 4900 | // Now spin up the private state thread: |
| 4901 | StartPrivateStateThread(true); |
| 4902 | } |
| 4903 | |
| 4904 | thread->QueueThreadPlan( |
| 4905 | thread_plan_sp, false); // This used to pass "true" does that make sense? |
| 4906 | |
| 4907 | if (options.GetDebug()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4908 | // In this case, we aren't actually going to run, we just want to stop |
| 4909 | // right away. Flush this thread so we will refetch the stacks and show the |
| 4910 | // correct backtrace. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4911 | // FIXME: To make this prettier we should invent some stop reason for this, |
| 4912 | // but that |
| 4913 | // is only cosmetic, and this functionality is only of use to lldb |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4914 | // developers who can live with not pretty... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4915 | thread->Flush(); |
| 4916 | return eExpressionStoppedForDebug; |
| 4917 | } |
| 4918 | |
| 4919 | ListenerSP listener_sp( |
| 4920 | Listener::MakeListener("lldb.process.listener.run-thread-plan")); |
| 4921 | |
| 4922 | lldb::EventSP event_to_broadcast_sp; |
| 4923 | |
| 4924 | { |
| 4925 | // This process event hijacker Hijacks the Public events and its destructor |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4926 | // 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] | 4927 | // |
| 4928 | // 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] | 4929 | // exits during execution), then the event is put into |
| 4930 | // event_to_broadcast_sp for rebroadcasting. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4931 | |
| 4932 | ProcessEventHijacker run_thread_plan_hijacker(*this, listener_sp); |
| 4933 | |
| 4934 | if (log) { |
| 4935 | StreamString s; |
| 4936 | thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4937 | LLDB_LOGF(log, |
| 4938 | "Process::RunThreadPlan(): Resuming thread %u - 0x%4.4" PRIx64 |
| 4939 | " to run thread plan \"%s\".", |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 4940 | thread_idx_id, expr_thread_id, s.GetData()); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 4941 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4942 | |
| 4943 | bool got_event; |
| 4944 | lldb::EventSP event_sp; |
| 4945 | lldb::StateType stop_state = lldb::eStateInvalid; |
| 4946 | |
| 4947 | bool before_first_timeout = true; // This is set to false the first time |
| 4948 | // that we have to halt the target. |
| 4949 | bool do_resume = true; |
| 4950 | bool handle_running_event = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4951 | |
| 4952 | // This is just for accounting: |
| 4953 | uint32_t num_resumes = 0; |
| 4954 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4955 | // 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] | 4956 | // going to run one thread, then we don't need the first timeout. So we |
| 4957 | // pretend we are after the first timeout already. |
| 4958 | if (!options.GetStopOthers() || !options.GetTryAllThreads()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4959 | before_first_timeout = false; |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 4960 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4961 | LLDB_LOGF(log, "Stop others: %u, try all: %u, before_first: %u.\n", |
| 4962 | options.GetStopOthers(), options.GetTryAllThreads(), |
| 4963 | before_first_timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4964 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4965 | // This isn't going to work if there are unfetched events on the queue. Are |
| 4966 | // there cases where we might want to run the remaining events here, and |
| 4967 | // then try to call the function? That's probably being too tricky for our |
| 4968 | // own good. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4969 | |
| 4970 | Event *other_events = listener_sp->PeekAtNextEvent(); |
| 4971 | if (other_events != nullptr) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4972 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4973 | eDiagnosticSeverityError, |
| 4974 | "RunThreadPlan called with pending events on the queue."); |
| 4975 | return eExpressionSetupError; |
Kuba Brecka | afdf842 | 2014-10-10 23:43:03 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4978 | // 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] | 4979 | // calling a function as part of a thread plan, in which case the last |
| 4980 | // delivered event could be the running event, and we don't want event |
| 4981 | // coalescing to cause us to lose OUR running event... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4982 | ForceNextEventDelivery(); |
Ryan Brown | 65d4d5c | 2015-09-16 21:20:44 +0000 | [diff] [blame] | 4983 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4984 | // 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] | 4985 | // when we are done. So don't call return anywhere within it. |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 4986 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4987 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4988 | // It's pretty much impossible to write test cases for things like: One |
| 4989 | // thread timeout expires, I go to halt, but the process already stopped on |
| 4990 | // the function call stop breakpoint. Turning on this define will make us |
| 4991 | // not fetch the first event till after the halt. So if you run a quick |
| 4992 | // function, it will have completed, and the completion event will be |
| 4993 | // waiting, when you interrupt for halt. The expression evaluation should |
| 4994 | // still succeed. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4995 | bool miss_first_event = true; |
| 4996 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4997 | while (true) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4998 | // We usually want to resume the process if we get to the top of the |
| 4999 | // loop. The only exception is if we get two running events with no |
| 5000 | // intervening stop, which can happen, we will just wait for then next |
| 5001 | // stop event. |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5002 | LLDB_LOGF(log, |
| 5003 | "Top of while loop: do_resume: %i handle_running_event: %i " |
| 5004 | "before_first_timeout: %i.", |
| 5005 | do_resume, handle_running_event, before_first_timeout); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5006 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5007 | if (do_resume || handle_running_event) { |
| 5008 | // Do the initial resume and wait for the running event before going |
| 5009 | // further. |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5010 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5011 | if (do_resume) { |
| 5012 | num_resumes++; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5013 | Status resume_error = PrivateResume(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5014 | if (!resume_error.Success()) { |
| 5015 | diagnostic_manager.Printf( |
| 5016 | eDiagnosticSeverityError, |
| 5017 | "couldn't resume inferior the %d time: \"%s\".", num_resumes, |
| 5018 | resume_error.AsCString()); |
| 5019 | return_value = eExpressionSetupError; |
| 5020 | break; |
| 5021 | } |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5022 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5023 | |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 5024 | got_event = |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 5025 | listener_sp->GetEvent(event_sp, GetUtilityExpressionTimeout()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5026 | if (!got_event) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5027 | LLDB_LOGF(log, |
| 5028 | "Process::RunThreadPlan(): didn't get any event after " |
| 5029 | "resume %" PRIu32 ", exiting.", |
| 5030 | num_resumes); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5031 | |
| 5032 | diagnostic_manager.Printf(eDiagnosticSeverityError, |
| 5033 | "didn't get any event after resume %" PRIu32 |
| 5034 | ", exiting.", |
| 5035 | num_resumes); |
| 5036 | return_value = eExpressionSetupError; |
| 5037 | break; |
| 5038 | } |
| 5039 | |
| 5040 | stop_state = |
| 5041 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5042 | |
| 5043 | if (stop_state != eStateRunning) { |
| 5044 | bool restarted = false; |
| 5045 | |
| 5046 | if (stop_state == eStateStopped) { |
| 5047 | restarted = Process::ProcessEventData::GetRestartedFromEvent( |
| 5048 | event_sp.get()); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5049 | LLDB_LOGF( |
| 5050 | log, |
| 5051 | "Process::RunThreadPlan(): didn't get running event after " |
| 5052 | "resume %d, got %s instead (restarted: %i, do_resume: %i, " |
| 5053 | "handle_running_event: %i).", |
| 5054 | num_resumes, StateAsCString(stop_state), restarted, do_resume, |
| 5055 | handle_running_event); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | if (restarted) { |
| 5059 | // This is probably an overabundance of caution, I don't think I |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5060 | // should ever get a stopped & restarted event here. But if I do, |
| 5061 | // the best thing is to Halt and then get out of here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5062 | const bool clear_thread_plans = false; |
| 5063 | const bool use_run_lock = false; |
| 5064 | Halt(clear_thread_plans, use_run_lock); |
| 5065 | } |
| 5066 | |
| 5067 | diagnostic_manager.Printf( |
| 5068 | eDiagnosticSeverityError, |
| 5069 | "didn't get running event after initial resume, got %s instead.", |
| 5070 | StateAsCString(stop_state)); |
| 5071 | return_value = eExpressionSetupError; |
| 5072 | break; |
| 5073 | } |
| 5074 | |
| 5075 | if (log) |
| 5076 | log->PutCString("Process::RunThreadPlan(): resuming succeeded."); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5077 | // We need to call the function synchronously, so spin waiting for it |
| 5078 | // to return. If we get interrupted while executing, we're going to |
| 5079 | // lose our context, and won't be able to gather the result at this |
| 5080 | // point. We set the timeout AFTER the resume, since the resume takes |
| 5081 | // 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] | 5082 | } else { |
| 5083 | if (log) |
| 5084 | log->PutCString("Process::RunThreadPlan(): waiting for next event."); |
| 5085 | } |
| 5086 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5087 | do_resume = true; |
| 5088 | handle_running_event = true; |
| 5089 | |
| 5090 | // Now wait for the process to stop again: |
| 5091 | event_sp.reset(); |
| 5092 | |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 5093 | Timeout<std::micro> timeout = |
| 5094 | GetExpressionTimeout(options, before_first_timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5095 | if (log) { |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 5096 | if (timeout) { |
| 5097 | auto now = system_clock::now(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5098 | LLDB_LOGF(log, |
| 5099 | "Process::RunThreadPlan(): about to wait - now is %s - " |
| 5100 | "endpoint is %s", |
| 5101 | llvm::to_string(now).c_str(), |
| 5102 | llvm::to_string(now + *timeout).c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5103 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5104 | LLDB_LOGF(log, "Process::RunThreadPlan(): about to wait forever."); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5105 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
| 5109 | // See comment above... |
| 5110 | if (miss_first_event) { |
Pavel Labath | 3d4f765 | 2019-08-05 08:23:25 +0000 | [diff] [blame] | 5111 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5112 | miss_first_event = false; |
| 5113 | got_event = false; |
| 5114 | } else |
| 5115 | #endif |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 5116 | got_event = listener_sp->GetEvent(event_sp, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5117 | |
| 5118 | if (got_event) { |
| 5119 | if (event_sp) { |
| 5120 | bool keep_going = false; |
| 5121 | if (event_sp->GetType() == eBroadcastBitInterrupt) { |
| 5122 | const bool clear_thread_plans = false; |
| 5123 | const bool use_run_lock = false; |
| 5124 | Halt(clear_thread_plans, use_run_lock); |
| 5125 | return_value = eExpressionInterrupted; |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 5126 | diagnostic_manager.PutString(eDiagnosticSeverityRemark, |
| 5127 | "execution halted by user interrupt."); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5128 | LLDB_LOGF(log, "Process::RunThreadPlan(): Got interrupted by " |
| 5129 | "eBroadcastBitInterrupted, exiting."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5130 | break; |
| 5131 | } else { |
| 5132 | stop_state = |
| 5133 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5134 | LLDB_LOGF(log, |
| 5135 | "Process::RunThreadPlan(): in while loop, got event: %s.", |
| 5136 | StateAsCString(stop_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5137 | |
| 5138 | switch (stop_state) { |
| 5139 | case lldb::eStateStopped: { |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 5140 | if (Process::ProcessEventData::GetRestartedFromEvent( |
| 5141 | event_sp.get())) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5142 | // If we were restarted, we just need to go back up to fetch |
| 5143 | // another event. |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 5144 | LLDB_LOGF(log, "Process::RunThreadPlan(): Got a stop and " |
| 5145 | "restart, so we'll continue waiting."); |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5146 | keep_going = true; |
| 5147 | do_resume = false; |
| 5148 | handle_running_event = true; |
| 5149 | } else { |
| 5150 | const bool handle_interrupts = true; |
| 5151 | return_value = *HandleStoppedEvent( |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 5152 | expr_thread_id, thread_plan_sp, thread_plan_restorer, |
| 5153 | event_sp, event_to_broadcast_sp, options, |
| 5154 | handle_interrupts); |
| 5155 | if (return_value == eExpressionThreadVanished) |
| 5156 | keep_going = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5157 | } |
| 5158 | } break; |
| 5159 | |
| 5160 | case lldb::eStateRunning: |
| 5161 | // This shouldn't really happen, but sometimes we do get two |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5162 | // running events without an intervening stop, and in that case |
| 5163 | // we should just go back to waiting for the stop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5164 | do_resume = false; |
| 5165 | keep_going = true; |
| 5166 | handle_running_event = false; |
| 5167 | break; |
| 5168 | |
| 5169 | default: |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5170 | LLDB_LOGF(log, |
| 5171 | "Process::RunThreadPlan(): execution stopped with " |
| 5172 | "unexpected state: %s.", |
| 5173 | StateAsCString(stop_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5174 | |
| 5175 | if (stop_state == eStateExited) |
| 5176 | event_to_broadcast_sp = event_sp; |
| 5177 | |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 5178 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5179 | eDiagnosticSeverityError, |
| 5180 | "execution stopped with unexpected state."); |
| 5181 | return_value = eExpressionInterrupted; |
| 5182 | break; |
| 5183 | } |
| 5184 | } |
| 5185 | |
| 5186 | if (keep_going) |
| 5187 | continue; |
| 5188 | else |
| 5189 | break; |
| 5190 | } else { |
| 5191 | if (log) |
| 5192 | log->PutCString("Process::RunThreadPlan(): got_event was true, but " |
| 5193 | "the event pointer was null. How odd..."); |
| 5194 | return_value = eExpressionInterrupted; |
| 5195 | break; |
| 5196 | } |
| 5197 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5198 | // If we didn't get an event that means we've timed out... We will |
| 5199 | // interrupt the process here. Depending on what we were asked to do |
| 5200 | // we will either exit, or try with all threads running for the same |
| 5201 | // timeout. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5202 | |
| 5203 | if (log) { |
| 5204 | if (options.GetTryAllThreads()) { |
| 5205 | if (before_first_timeout) { |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 5206 | LLDB_LOG(log, |
| 5207 | "Running function with one thread timeout timed out."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5208 | } else |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 5209 | LLDB_LOG(log, "Restarting function with all threads enabled and " |
| 5210 | "timeout: {0} timed out, abandoning execution.", |
| 5211 | timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5212 | } else |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 5213 | LLDB_LOG(log, "Running function with timeout: {0} timed out, " |
| 5214 | "abandoning execution.", |
| 5215 | timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5216 | } |
| 5217 | |
| 5218 | // 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] | 5219 | // around to calling Halt the target could have stopped. That's fine, |
| 5220 | // Halt will figure that out and send the appropriate Stopped event. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5221 | // BUT it is also possible that we stopped & restarted (e.g. hit a |
| 5222 | // signal with "stop" set to false.) In |
| 5223 | // 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] | 5224 | // back to waiting for the Halt's stopped event. That's what this |
| 5225 | // while loop does. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5226 | |
| 5227 | bool back_to_top = true; |
| 5228 | uint32_t try_halt_again = 0; |
| 5229 | bool do_halt = true; |
| 5230 | const uint32_t num_retries = 5; |
| 5231 | while (try_halt_again < num_retries) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5232 | Status halt_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5233 | if (do_halt) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5234 | LLDB_LOGF(log, "Process::RunThreadPlan(): Running Halt."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5235 | const bool clear_thread_plans = false; |
| 5236 | const bool use_run_lock = false; |
| 5237 | Halt(clear_thread_plans, use_run_lock); |
| 5238 | } |
| 5239 | if (halt_error.Success()) { |
| 5240 | if (log) |
| 5241 | log->PutCString("Process::RunThreadPlan(): Halt succeeded."); |
| 5242 | |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 5243 | got_event = |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 5244 | listener_sp->GetEvent(event_sp, GetUtilityExpressionTimeout()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5245 | |
| 5246 | if (got_event) { |
| 5247 | stop_state = |
| 5248 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5249 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5250 | LLDB_LOGF(log, |
| 5251 | "Process::RunThreadPlan(): Stopped with event: %s", |
| 5252 | StateAsCString(stop_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5253 | if (stop_state == lldb::eStateStopped && |
| 5254 | Process::ProcessEventData::GetInterruptedFromEvent( |
| 5255 | event_sp.get())) |
| 5256 | log->PutCString(" Event was the Halt interruption event."); |
| 5257 | } |
| 5258 | |
| 5259 | if (stop_state == lldb::eStateStopped) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5260 | if (Process::ProcessEventData::GetRestartedFromEvent( |
| 5261 | event_sp.get())) { |
| 5262 | if (log) |
| 5263 | log->PutCString("Process::RunThreadPlan(): Went to halt " |
| 5264 | "but got a restarted event, there must be " |
| 5265 | "an un-restarted stopped event so try " |
| 5266 | "again... " |
| 5267 | "Exiting wait loop."); |
| 5268 | try_halt_again++; |
| 5269 | do_halt = false; |
| 5270 | continue; |
| 5271 | } |
| 5272 | |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5273 | // Between the time we initiated the Halt and the time we |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5274 | // delivered it, the process could have already finished its |
| 5275 | // job. Check that here: |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5276 | const bool handle_interrupts = false; |
| 5277 | if (auto result = HandleStoppedEvent( |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 5278 | expr_thread_id, thread_plan_sp, thread_plan_restorer, |
| 5279 | event_sp, event_to_broadcast_sp, options, |
| 5280 | handle_interrupts)) { |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5281 | return_value = *result; |
| 5282 | back_to_top = false; |
| 5283 | break; |
| 5284 | } |
| 5285 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5286 | if (!options.GetTryAllThreads()) { |
| 5287 | if (log) |
| 5288 | log->PutCString("Process::RunThreadPlan(): try_all_threads " |
| 5289 | "was false, we stopped so now we're " |
| 5290 | "quitting."); |
| 5291 | return_value = eExpressionInterrupted; |
| 5292 | back_to_top = false; |
| 5293 | break; |
| 5294 | } |
| 5295 | |
| 5296 | if (before_first_timeout) { |
| 5297 | // Set all the other threads to run, and return to the top of |
| 5298 | // the loop, which will continue; |
| 5299 | before_first_timeout = false; |
| 5300 | thread_plan_sp->SetStopOthers(false); |
| 5301 | if (log) |
| 5302 | log->PutCString( |
| 5303 | "Process::RunThreadPlan(): about to resume."); |
| 5304 | |
| 5305 | back_to_top = true; |
| 5306 | break; |
| 5307 | } else { |
| 5308 | // Running all threads failed, so return Interrupted. |
| 5309 | if (log) |
| 5310 | log->PutCString("Process::RunThreadPlan(): running all " |
| 5311 | "threads timed out."); |
| 5312 | return_value = eExpressionInterrupted; |
| 5313 | back_to_top = false; |
| 5314 | break; |
| 5315 | } |
| 5316 | } |
| 5317 | } else { |
| 5318 | if (log) |
| 5319 | log->PutCString("Process::RunThreadPlan(): halt said it " |
| 5320 | "succeeded, but I got no event. " |
| 5321 | "I'm getting out of here passing Interrupted."); |
| 5322 | return_value = eExpressionInterrupted; |
| 5323 | back_to_top = false; |
| 5324 | break; |
| 5325 | } |
| 5326 | } else { |
| 5327 | try_halt_again++; |
| 5328 | continue; |
| 5329 | } |
| 5330 | } |
| 5331 | |
| 5332 | if (!back_to_top || try_halt_again > num_retries) |
| 5333 | break; |
| 5334 | else |
| 5335 | continue; |
| 5336 | } |
| 5337 | } // END WAIT LOOP |
| 5338 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5339 | // If we had to start up a temporary private state thread to run this |
| 5340 | // thread plan, shut it down now. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5341 | if (backup_private_state_thread.IsJoinable()) { |
| 5342 | StopPrivateStateThread(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5343 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5344 | m_private_state_thread = backup_private_state_thread; |
| 5345 | if (stopper_base_plan_sp) { |
| 5346 | thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp); |
| 5347 | } |
| 5348 | if (old_state != eStateInvalid) |
| 5349 | m_public_state.SetValueNoLock(old_state); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 5352 | // If our thread went away on us, we need to get out of here without |
| 5353 | // doing any more work. We don't have to clean up the thread plan, that |
| 5354 | // will have happened when the Thread was destroyed. |
| 5355 | if (return_value == eExpressionThreadVanished) { |
| 5356 | return return_value; |
| 5357 | } |
| 5358 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5359 | if (return_value != eExpressionCompleted && log) { |
| 5360 | // Print a backtrace into the log so we can figure out where we are: |
| 5361 | StreamString s; |
| 5362 | s.PutCString("Thread state after unsuccessful completion: \n"); |
| 5363 | thread->GetStackFrameStatus(s, 0, UINT32_MAX, true, UINT32_MAX); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 5364 | log->PutString(s.GetString()); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5365 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5366 | // 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] | 5367 | // There are three cases where this could happen: 1) The execution |
| 5368 | // successfully completed 2) We hit a breakpoint, and ignore_breakpoints |
| 5369 | // 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] | 5370 | bool should_unwind = (return_value == eExpressionInterrupted && |
| 5371 | options.DoesUnwindOnError()) || |
| 5372 | (return_value == eExpressionHitBreakpoint && |
| 5373 | options.DoesIgnoreBreakpoints()); |
| 5374 | |
| 5375 | if (return_value == eExpressionCompleted || should_unwind) { |
| 5376 | thread_plan_sp->RestoreThreadState(); |
| 5377 | } |
| 5378 | |
| 5379 | // Now do some processing on the results of the run: |
| 5380 | if (return_value == eExpressionInterrupted || |
| 5381 | return_value == eExpressionHitBreakpoint) { |
| 5382 | if (log) { |
| 5383 | StreamString s; |
| 5384 | if (event_sp) |
| 5385 | event_sp->Dump(&s); |
| 5386 | else { |
| 5387 | log->PutCString("Process::RunThreadPlan(): Stop event that " |
| 5388 | "interrupted us is NULL."); |
| 5389 | } |
| 5390 | |
| 5391 | StreamString ts; |
| 5392 | |
| 5393 | const char *event_explanation = nullptr; |
| 5394 | |
| 5395 | do { |
| 5396 | if (!event_sp) { |
| 5397 | event_explanation = "<no event>"; |
| 5398 | break; |
| 5399 | } else if (event_sp->GetType() == eBroadcastBitInterrupt) { |
| 5400 | event_explanation = "<user interrupt>"; |
| 5401 | break; |
| 5402 | } else { |
| 5403 | const Process::ProcessEventData *event_data = |
| 5404 | Process::ProcessEventData::GetEventDataFromEvent( |
| 5405 | event_sp.get()); |
| 5406 | |
| 5407 | if (!event_data) { |
| 5408 | event_explanation = "<no event data>"; |
| 5409 | break; |
| 5410 | } |
| 5411 | |
| 5412 | Process *process = event_data->GetProcessSP().get(); |
| 5413 | |
| 5414 | if (!process) { |
| 5415 | event_explanation = "<no process>"; |
| 5416 | break; |
| 5417 | } |
| 5418 | |
| 5419 | ThreadList &thread_list = process->GetThreadList(); |
| 5420 | |
| 5421 | uint32_t num_threads = thread_list.GetSize(); |
| 5422 | uint32_t thread_index; |
| 5423 | |
| 5424 | ts.Printf("<%u threads> ", num_threads); |
| 5425 | |
| 5426 | for (thread_index = 0; thread_index < num_threads; ++thread_index) { |
| 5427 | Thread *thread = thread_list.GetThreadAtIndex(thread_index).get(); |
| 5428 | |
| 5429 | if (!thread) { |
| 5430 | ts.Printf("<?> "); |
| 5431 | continue; |
| 5432 | } |
| 5433 | |
| 5434 | ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID()); |
| 5435 | RegisterContext *register_context = |
| 5436 | thread->GetRegisterContext().get(); |
| 5437 | |
| 5438 | if (register_context) |
| 5439 | ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC()); |
| 5440 | else |
| 5441 | ts.Printf("[ip unknown] "); |
| 5442 | |
| 5443 | // Show the private stop info here, the public stop info will be |
| 5444 | // from the last natural stop. |
| 5445 | lldb::StopInfoSP stop_info_sp = thread->GetPrivateStopInfo(); |
| 5446 | if (stop_info_sp) { |
| 5447 | const char *stop_desc = stop_info_sp->GetDescription(); |
| 5448 | if (stop_desc) |
| 5449 | ts.PutCString(stop_desc); |
| 5450 | } |
| 5451 | ts.Printf(">"); |
| 5452 | } |
| 5453 | |
| 5454 | event_explanation = ts.GetData(); |
| 5455 | } |
Jonas Devlieghere | 09ad8c8 | 2019-05-24 00:44:33 +0000 | [diff] [blame] | 5456 | } while (false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5457 | |
| 5458 | if (event_explanation) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5459 | LLDB_LOGF(log, |
| 5460 | "Process::RunThreadPlan(): execution interrupted: %s %s", |
| 5461 | s.GetData(), event_explanation); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5462 | else |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5463 | LLDB_LOGF(log, "Process::RunThreadPlan(): execution interrupted: %s", |
| 5464 | s.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5465 | } |
| 5466 | |
| 5467 | if (should_unwind) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5468 | LLDB_LOGF(log, |
| 5469 | "Process::RunThreadPlan: ExecutionInterrupted - " |
| 5470 | "discarding thread plans up to %p.", |
| 5471 | static_cast<void *>(thread_plan_sp.get())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5472 | thread->DiscardThreadPlansUpToPlan(thread_plan_sp); |
| 5473 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5474 | LLDB_LOGF(log, |
| 5475 | "Process::RunThreadPlan: ExecutionInterrupted - for " |
| 5476 | "plan: %p not discarding.", |
| 5477 | static_cast<void *>(thread_plan_sp.get())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5478 | } |
| 5479 | } else if (return_value == eExpressionSetupError) { |
| 5480 | if (log) |
| 5481 | log->PutCString("Process::RunThreadPlan(): execution set up error."); |
| 5482 | |
| 5483 | if (options.DoesUnwindOnError()) { |
| 5484 | thread->DiscardThreadPlansUpToPlan(thread_plan_sp); |
| 5485 | } |
| 5486 | } else { |
| 5487 | if (thread->IsThreadPlanDone(thread_plan_sp.get())) { |
| 5488 | if (log) |
| 5489 | log->PutCString("Process::RunThreadPlan(): thread plan is done"); |
| 5490 | return_value = eExpressionCompleted; |
| 5491 | } else if (thread->WasThreadPlanDiscarded(thread_plan_sp.get())) { |
| 5492 | if (log) |
| 5493 | log->PutCString( |
| 5494 | "Process::RunThreadPlan(): thread plan was discarded"); |
| 5495 | return_value = eExpressionDiscarded; |
| 5496 | } else { |
| 5497 | if (log) |
| 5498 | log->PutCString( |
| 5499 | "Process::RunThreadPlan(): thread plan stopped in mid course"); |
| 5500 | if (options.DoesUnwindOnError() && thread_plan_sp) { |
| 5501 | if (log) |
| 5502 | log->PutCString("Process::RunThreadPlan(): discarding thread plan " |
| 5503 | "'cause unwind_on_error is set."); |
| 5504 | thread->DiscardThreadPlansUpToPlan(thread_plan_sp); |
| 5505 | } |
| 5506 | } |
| 5507 | } |
| 5508 | |
| 5509 | // 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] | 5510 | // target Check that it's still there, and if it is put it back in the |
| 5511 | // 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] | 5512 | thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get(); |
| 5513 | if (thread) { |
| 5514 | exe_ctx.SetFrameSP(thread->GetFrameWithStackID(ctx_frame_id)); |
| 5515 | } |
| 5516 | |
| 5517 | // Also restore the current process'es selected frame & thread, since this |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5518 | // function calling may be done behind the user's back. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5519 | |
| 5520 | if (selected_tid != LLDB_INVALID_THREAD_ID) { |
| 5521 | if (GetThreadList().SetSelectedThreadByIndexID(selected_tid) && |
| 5522 | selected_stack_id.IsValid()) { |
| 5523 | // We were able to restore the selected thread, now restore the frame: |
| 5524 | std::lock_guard<std::recursive_mutex> guard(GetThreadList().GetMutex()); |
| 5525 | StackFrameSP old_frame_sp = |
| 5526 | GetThreadList().GetSelectedThread()->GetFrameWithStackID( |
| 5527 | selected_stack_id); |
| 5528 | if (old_frame_sp) |
| 5529 | GetThreadList().GetSelectedThread()->SetSelectedFrame( |
| 5530 | old_frame_sp.get()); |
| 5531 | } |
| 5532 | } |
| 5533 | } |
| 5534 | |
| 5535 | // If the process exited during the run of the thread plan, notify everyone. |
| 5536 | |
| 5537 | if (event_to_broadcast_sp) { |
| 5538 | if (log) |
| 5539 | log->PutCString("Process::RunThreadPlan(): rebroadcasting event."); |
| 5540 | BroadcastEvent(event_to_broadcast_sp); |
| 5541 | } |
| 5542 | |
| 5543 | return return_value; |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5546 | const char *Process::ExecutionResultAsCString(ExpressionResults result) { |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 5547 | const char *result_name = "<unknown>"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5548 | |
| 5549 | switch (result) { |
| 5550 | case eExpressionCompleted: |
| 5551 | result_name = "eExpressionCompleted"; |
| 5552 | break; |
| 5553 | case eExpressionDiscarded: |
| 5554 | result_name = "eExpressionDiscarded"; |
| 5555 | break; |
| 5556 | case eExpressionInterrupted: |
| 5557 | result_name = "eExpressionInterrupted"; |
| 5558 | break; |
| 5559 | case eExpressionHitBreakpoint: |
| 5560 | result_name = "eExpressionHitBreakpoint"; |
| 5561 | break; |
| 5562 | case eExpressionSetupError: |
| 5563 | result_name = "eExpressionSetupError"; |
| 5564 | break; |
| 5565 | case eExpressionParseError: |
| 5566 | result_name = "eExpressionParseError"; |
| 5567 | break; |
| 5568 | case eExpressionResultUnavailable: |
| 5569 | result_name = "eExpressionResultUnavailable"; |
| 5570 | break; |
| 5571 | case eExpressionTimedOut: |
| 5572 | result_name = "eExpressionTimedOut"; |
| 5573 | break; |
| 5574 | case eExpressionStoppedForDebug: |
| 5575 | result_name = "eExpressionStoppedForDebug"; |
| 5576 | break; |
Jim Ingham | dbbed97 | 2020-05-20 18:00:56 -0700 | [diff] [blame] | 5577 | case eExpressionThreadVanished: |
| 5578 | result_name = "eExpressionThreadVanished"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5579 | } |
| 5580 | return result_name; |
Jason Molenda | 484900b | 2015-08-10 07:55:25 +0000 | [diff] [blame] | 5581 | } |
| 5582 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5583 | void Process::GetStatus(Stream &strm) { |
| 5584 | const StateType state = GetState(); |
| 5585 | if (StateIsStoppedState(state, false)) { |
| 5586 | if (state == eStateExited) { |
| 5587 | int exit_status = GetExitStatus(); |
| 5588 | const char *exit_description = GetExitDescription(); |
| 5589 | strm.Printf("Process %" PRIu64 " exited with status = %i (0x%8.8x) %s\n", |
| 5590 | GetID(), exit_status, exit_status, |
| 5591 | exit_description ? exit_description : ""); |
| 5592 | } else { |
| 5593 | if (state == eStateConnected) |
| 5594 | strm.Printf("Connected to remote target.\n"); |
| 5595 | else |
| 5596 | strm.Printf("Process %" PRIu64 " %s\n", GetID(), StateAsCString(state)); |
| 5597 | } |
| 5598 | } else { |
| 5599 | strm.Printf("Process %" PRIu64 " is running.\n", GetID()); |
| 5600 | } |
Pavel Labath | a933d51 | 2016-04-05 13:07:16 +0000 | [diff] [blame] | 5601 | } |
| 5602 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5603 | size_t Process::GetThreadStatus(Stream &strm, |
| 5604 | bool only_threads_with_stop_reason, |
| 5605 | uint32_t start_frame, uint32_t num_frames, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 5606 | uint32_t num_frames_with_source, |
| 5607 | bool stop_format) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5608 | size_t num_thread_infos_dumped = 0; |
Greg Clayton | 35ca64b | 2015-04-16 17:13:34 +0000 | [diff] [blame] | 5609 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5610 | // 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] | 5611 | // very well might run code (e.g. if we need it to get return values or |
| 5612 | // arguments.) For that to work the process has to be able to acquire it. |
| 5613 | // 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] | 5614 | |
| 5615 | uint32_t num_threads; |
| 5616 | std::vector<lldb::tid_t> thread_id_array; |
| 5617 | // Scope for thread list locker; |
| 5618 | { |
| 5619 | std::lock_guard<std::recursive_mutex> guard(GetThreadList().GetMutex()); |
| 5620 | ThreadList &curr_thread_list = GetThreadList(); |
| 5621 | num_threads = curr_thread_list.GetSize(); |
| 5622 | uint32_t idx; |
| 5623 | thread_id_array.resize(num_threads); |
| 5624 | for (idx = 0; idx < num_threads; ++idx) |
| 5625 | thread_id_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetID(); |
| 5626 | } |
| 5627 | |
| 5628 | for (uint32_t i = 0; i < num_threads; i++) { |
| 5629 | ThreadSP thread_sp(GetThreadList().FindThreadByID(thread_id_array[i])); |
| 5630 | if (thread_sp) { |
| 5631 | if (only_threads_with_stop_reason) { |
| 5632 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo(); |
| 5633 | if (!stop_info_sp || !stop_info_sp->IsValid()) |
| 5634 | continue; |
| 5635 | } |
| 5636 | thread_sp->GetStatus(strm, start_frame, num_frames, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 5637 | num_frames_with_source, |
| 5638 | stop_format); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5639 | ++num_thread_infos_dumped; |
| 5640 | } else { |
| 5641 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5642 | LLDB_LOGF(log, "Process::GetThreadStatus - thread 0x" PRIu64 |
| 5643 | " vanished while running Thread::GetStatus."); |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 5644 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5645 | } |
| 5646 | return num_thread_infos_dumped; |
| 5647 | } |
| 5648 | |
| 5649 | void Process::AddInvalidMemoryRegion(const LoadRange ®ion) { |
| 5650 | m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 5651 | } |
| 5652 | |
| 5653 | bool Process::RemoveInvalidMemoryRange(const LoadRange ®ion) { |
| 5654 | return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), |
| 5655 | region.GetByteSize()); |
| 5656 | } |
| 5657 | |
| 5658 | void Process::AddPreResumeAction(PreResumeActionCallback callback, |
| 5659 | void *baton) { |
| 5660 | m_pre_resume_actions.push_back(PreResumeCallbackAndBaton(callback, baton)); |
| 5661 | } |
| 5662 | |
| 5663 | bool Process::RunPreResumeActions() { |
| 5664 | bool result = true; |
| 5665 | while (!m_pre_resume_actions.empty()) { |
| 5666 | struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back(); |
| 5667 | m_pre_resume_actions.pop_back(); |
| 5668 | bool this_result = action.callback(action.baton); |
| 5669 | if (result) |
| 5670 | result = this_result; |
| 5671 | } |
| 5672 | return result; |
| 5673 | } |
| 5674 | |
| 5675 | void Process::ClearPreResumeActions() { m_pre_resume_actions.clear(); } |
| 5676 | |
Jim Ingham | ffd9175 | 2016-10-20 22:50:00 +0000 | [diff] [blame] | 5677 | void Process::ClearPreResumeAction(PreResumeActionCallback callback, void *baton) |
| 5678 | { |
| 5679 | PreResumeCallbackAndBaton element(callback, baton); |
| 5680 | auto found_iter = std::find(m_pre_resume_actions.begin(), m_pre_resume_actions.end(), element); |
| 5681 | if (found_iter != m_pre_resume_actions.end()) |
| 5682 | { |
| 5683 | m_pre_resume_actions.erase(found_iter); |
| 5684 | } |
| 5685 | } |
| 5686 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5687 | ProcessRunLock &Process::GetRunLock() { |
| 5688 | if (m_private_state_thread.EqualsThread(Host::GetCurrentThread())) |
| 5689 | return m_private_run_lock; |
| 5690 | else |
| 5691 | return m_public_run_lock; |
| 5692 | } |
| 5693 | |
Jim Ingham | 58c3235e | 2019-10-01 00:47:25 +0000 | [diff] [blame] | 5694 | bool Process::CurrentThreadIsPrivateStateThread() |
| 5695 | { |
| 5696 | return m_private_state_thread.EqualsThread(Host::GetCurrentThread()); |
| 5697 | } |
| 5698 | |
| 5699 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5700 | void Process::Flush() { |
| 5701 | m_thread_list.Flush(); |
| 5702 | m_extended_thread_list.Flush(); |
| 5703 | m_extended_thread_stop_id = 0; |
| 5704 | m_queue_list.Clear(); |
| 5705 | m_queue_list_stop_id = 0; |
| 5706 | } |
| 5707 | |
| 5708 | void Process::DidExec() { |
| 5709 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5710 | LLDB_LOGF(log, "Process::%s()", __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5711 | |
| 5712 | Target &target = GetTarget(); |
| 5713 | target.CleanupProcess(); |
| 5714 | target.ClearModules(false); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 5715 | m_dynamic_checkers_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5716 | m_abi_sp.reset(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 5717 | m_system_runtime_up.reset(); |
| 5718 | m_os_up.reset(); |
| 5719 | m_dyld_up.reset(); |
| 5720 | m_jit_loaders_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5721 | m_image_tokens.clear(); |
| 5722 | m_allocated_memory_cache.Clear(); |
Alex Langford | 74eb76f | 2019-05-22 23:01:18 +0000 | [diff] [blame] | 5723 | { |
| 5724 | std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex); |
| 5725 | m_language_runtimes.clear(); |
| 5726 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5727 | m_instrumentation_runtimes.clear(); |
| 5728 | m_thread_list.DiscardThreadPlans(); |
| 5729 | m_memory_cache.Clear(true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5730 | DoDidExec(); |
| 5731 | CompleteAttach(); |
| 5732 | // Flush the process (threads and all stack frames) after running |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5733 | // CompleteAttach() in case the dynamic loader loaded things in new |
| 5734 | // locations. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5735 | Flush(); |
| 5736 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5737 | // After we figure out what was loaded/unloaded in CompleteAttach, we need to |
| 5738 | // 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] | 5739 | target.DidExec(); |
| 5740 | } |
| 5741 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5742 | addr_t Process::ResolveIndirectFunction(const Address *address, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5743 | if (address == nullptr) { |
| 5744 | error.SetErrorString("Invalid address argument"); |
| 5745 | return LLDB_INVALID_ADDRESS; |
| 5746 | } |
| 5747 | |
| 5748 | addr_t function_addr = LLDB_INVALID_ADDRESS; |
| 5749 | |
| 5750 | addr_t addr = address->GetLoadAddress(&GetTarget()); |
| 5751 | std::map<addr_t, addr_t>::const_iterator iter = |
| 5752 | m_resolved_indirect_addresses.find(addr); |
| 5753 | if (iter != m_resolved_indirect_addresses.end()) { |
| 5754 | function_addr = (*iter).second; |
| 5755 | } else { |
Alex Langford | 5b2b38e | 2019-09-13 00:02:05 +0000 | [diff] [blame] | 5756 | if (!CallVoidArgVoidPtrReturn(address, function_addr)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5757 | Symbol *symbol = address->CalculateSymbolContextSymbol(); |
| 5758 | error.SetErrorStringWithFormat( |
| 5759 | "Unable to call resolver for indirect function %s", |
| 5760 | symbol ? symbol->GetName().AsCString() : "<UNKNOWN>"); |
| 5761 | function_addr = LLDB_INVALID_ADDRESS; |
| 5762 | } else { |
| 5763 | m_resolved_indirect_addresses.insert( |
| 5764 | std::pair<addr_t, addr_t>(addr, function_addr)); |
| 5765 | } |
| 5766 | } |
| 5767 | return function_addr; |
| 5768 | } |
| 5769 | |
| 5770 | void Process::ModulesDidLoad(ModuleList &module_list) { |
Jonas Devlieghere | 0b339c0 | 2020-07-24 12:09:36 -0700 | [diff] [blame] | 5771 | // Inform the system runtime of the modified modules. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5772 | SystemRuntime *sys_runtime = GetSystemRuntime(); |
Jonas Devlieghere | 0b339c0 | 2020-07-24 12:09:36 -0700 | [diff] [blame] | 5773 | if (sys_runtime) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5774 | sys_runtime->ModulesDidLoad(module_list); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5775 | |
| 5776 | GetJITLoaders().ModulesDidLoad(module_list); |
| 5777 | |
Jonas Devlieghere | 0b339c0 | 2020-07-24 12:09:36 -0700 | [diff] [blame] | 5778 | // Give the instrumentation runtimes a chance to be created before informing |
| 5779 | // them of the modified modules. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5780 | InstrumentationRuntime::ModulesDidLoad(module_list, this, |
| 5781 | m_instrumentation_runtimes); |
Jonas Devlieghere | 0b339c0 | 2020-07-24 12:09:36 -0700 | [diff] [blame] | 5782 | for (auto &runtime : m_instrumentation_runtimes) |
| 5783 | runtime.second->ModulesDidLoad(module_list); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5784 | |
Jonas Devlieghere | 0b339c0 | 2020-07-24 12:09:36 -0700 | [diff] [blame] | 5785 | // Give the language runtimes a chance to be created before informing them of |
| 5786 | // the modified modules. |
| 5787 | for (const lldb::LanguageType lang_type : Language::GetSupportedLanguages()) { |
| 5788 | if (LanguageRuntime *runtime = GetLanguageRuntime(lang_type)) |
| 5789 | runtime->ModulesDidLoad(module_list); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
| 5792 | // If we don't have an operating system plug-in, try to load one since |
| 5793 | // loading shared libraries might cause a new one to try and load |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 5794 | if (!m_os_up) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5795 | LoadOperatingSystemPlugin(false); |
| 5796 | |
Jonas Devlieghere | 0b339c0 | 2020-07-24 12:09:36 -0700 | [diff] [blame] | 5797 | // Inform the structured-data plugins of the modified modules. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5798 | for (auto pair : m_structured_data_plugin_map) { |
| 5799 | if (pair.second) |
| 5800 | pair.second->ModulesDidLoad(*this, module_list); |
| 5801 | } |
| 5802 | } |
| 5803 | |
| 5804 | void Process::PrintWarning(uint64_t warning_type, const void *repeat_key, |
| 5805 | const char *fmt, ...) { |
| 5806 | bool print_warning = true; |
| 5807 | |
| 5808 | StreamSP stream_sp = GetTarget().GetDebugger().GetAsyncOutputStream(); |
| 5809 | if (!stream_sp) |
| 5810 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5811 | |
| 5812 | if (repeat_key != nullptr) { |
| 5813 | WarningsCollection::iterator it = m_warnings_issued.find(warning_type); |
| 5814 | if (it == m_warnings_issued.end()) { |
| 5815 | m_warnings_issued[warning_type] = WarningsPointerSet(); |
| 5816 | m_warnings_issued[warning_type].insert(repeat_key); |
| 5817 | } else { |
| 5818 | if (it->second.find(repeat_key) != it->second.end()) { |
| 5819 | print_warning = false; |
| 5820 | } else { |
| 5821 | it->second.insert(repeat_key); |
| 5822 | } |
| 5823 | } |
| 5824 | } |
| 5825 | |
| 5826 | if (print_warning) { |
| 5827 | va_list args; |
| 5828 | va_start(args, fmt); |
| 5829 | stream_sp->PrintfVarArg(fmt, args); |
| 5830 | va_end(args); |
| 5831 | } |
| 5832 | } |
| 5833 | |
| 5834 | void Process::PrintWarningOptimization(const SymbolContext &sc) { |
Adrian Prantl | 220c17f | 2020-05-21 17:42:24 -0700 | [diff] [blame] | 5835 | if (!GetWarningsOptimization()) |
| 5836 | return; |
| 5837 | if (!sc.module_sp) |
| 5838 | return; |
| 5839 | if (!sc.module_sp->GetFileSpec().GetFilename().IsEmpty() && sc.function && |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5840 | sc.function->GetIsOptimized()) { |
| 5841 | PrintWarning(Process::Warnings::eWarningsOptimization, sc.module_sp.get(), |
| 5842 | "%s was compiled with optimization - stepping may behave " |
| 5843 | "oddly; variables may not be available.\n", |
| 5844 | sc.module_sp->GetFileSpec().GetFilename().GetCString()); |
| 5845 | } |
| 5846 | } |
| 5847 | |
Adrian Prantl | 220c17f | 2020-05-21 17:42:24 -0700 | [diff] [blame] | 5848 | void Process::PrintWarningUnsupportedLanguage(const SymbolContext &sc) { |
| 5849 | if (!GetWarningsUnsupportedLanguage()) |
| 5850 | return; |
| 5851 | if (!sc.module_sp) |
| 5852 | return; |
| 5853 | LanguageType language = sc.GetLanguage(); |
| 5854 | if (language == eLanguageTypeUnknown) |
| 5855 | return; |
| 5856 | auto type_system_or_err = sc.module_sp->GetTypeSystemForLanguage(language); |
| 5857 | if (auto err = type_system_or_err.takeError()) { |
| 5858 | llvm::consumeError(std::move(err)); |
| 5859 | PrintWarning(Process::Warnings::eWarningsUnsupportedLanguage, |
| 5860 | sc.module_sp.get(), |
| 5861 | "This version of LLDB has no plugin for the %s language. " |
| 5862 | "Inspection of frame variables will be limited.\n", |
| 5863 | Language::GetNameForLanguageType(language)); |
| 5864 | } |
| 5865 | } |
| 5866 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5867 | bool Process::GetProcessInfo(ProcessInstanceInfo &info) { |
| 5868 | info.Clear(); |
| 5869 | |
| 5870 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
| 5871 | if (!platform_sp) |
| 5872 | return false; |
| 5873 | |
| 5874 | return platform_sp->GetProcessInfo(GetID(), info); |
| 5875 | } |
| 5876 | |
| 5877 | ThreadCollectionSP Process::GetHistoryThreads(lldb::addr_t addr) { |
| 5878 | ThreadCollectionSP threads; |
| 5879 | |
| 5880 | const MemoryHistorySP &memory_history = |
| 5881 | MemoryHistory::FindPlugin(shared_from_this()); |
| 5882 | |
| 5883 | if (!memory_history) { |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 5884 | return threads; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5885 | } |
| 5886 | |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 5887 | threads = std::make_shared<ThreadCollection>( |
| 5888 | memory_history->GetHistoryThreads(addr)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5889 | |
| 5890 | return threads; |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 5891 | } |
Kuba Brecka | 6392754 | 2014-10-11 01:59:32 +0000 | [diff] [blame] | 5892 | |
| 5893 | InstrumentationRuntimeSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5894 | Process::GetInstrumentationRuntime(lldb::InstrumentationRuntimeType type) { |
| 5895 | InstrumentationRuntimeCollection::iterator pos; |
| 5896 | pos = m_instrumentation_runtimes.find(type); |
| 5897 | if (pos == m_instrumentation_runtimes.end()) { |
| 5898 | return InstrumentationRuntimeSP(); |
| 5899 | } else |
| 5900 | return (*pos).second; |
Kuba Brecka | 6392754 | 2014-10-11 01:59:32 +0000 | [diff] [blame] | 5901 | } |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 5902 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5903 | bool Process::GetModuleSpec(const FileSpec &module_file_spec, |
| 5904 | const ArchSpec &arch, ModuleSpec &module_spec) { |
| 5905 | module_spec.Clear(); |
| 5906 | return false; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 5907 | } |
Tamas Berghammer | 3cb132a | 2015-12-02 11:58:51 +0000 | [diff] [blame] | 5908 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5909 | size_t Process::AddImageToken(lldb::addr_t image_ptr) { |
| 5910 | m_image_tokens.push_back(image_ptr); |
| 5911 | return m_image_tokens.size() - 1; |
Tamas Berghammer | 3cb132a | 2015-12-02 11:58:51 +0000 | [diff] [blame] | 5912 | } |
| 5913 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5914 | lldb::addr_t Process::GetImagePtrFromToken(size_t token) const { |
| 5915 | if (token < m_image_tokens.size()) |
| 5916 | return m_image_tokens[token]; |
Saleem Abdulrasool | 3775be2 | 2020-04-06 17:33:38 -0700 | [diff] [blame] | 5917 | return LLDB_INVALID_ADDRESS; |
Tamas Berghammer | 3cb132a | 2015-12-02 11:58:51 +0000 | [diff] [blame] | 5918 | } |
| 5919 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5920 | void Process::ResetImageToken(size_t token) { |
| 5921 | if (token < m_image_tokens.size()) |
Saleem Abdulrasool | 3775be2 | 2020-04-06 17:33:38 -0700 | [diff] [blame] | 5922 | m_image_tokens[token] = LLDB_INVALID_ADDRESS; |
Enrico Granata | f3129cb | 2015-12-03 23:53:45 +0000 | [diff] [blame] | 5923 | } |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5924 | |
| 5925 | Address |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5926 | Process::AdvanceAddressToNextBranchInstruction(Address default_stop_addr, |
| 5927 | AddressRange range_bounds) { |
| 5928 | Target &target = GetTarget(); |
| 5929 | DisassemblerSP disassembler_sp; |
| 5930 | InstructionList *insn_list = nullptr; |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5931 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5932 | Address retval = default_stop_addr; |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5933 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5934 | if (!target.GetUseFastStepping()) |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5935 | return retval; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5936 | if (!default_stop_addr.IsValid()) |
| 5937 | return retval; |
| 5938 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5939 | const char *plugin_name = nullptr; |
| 5940 | const char *flavor = nullptr; |
| 5941 | const bool prefer_file_cache = true; |
| 5942 | disassembler_sp = Disassembler::DisassembleRange( |
Pavel Labath | 04592d5 | 2020-03-05 13:03:26 +0100 | [diff] [blame] | 5943 | target.GetArchitecture(), plugin_name, flavor, GetTarget(), range_bounds, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5944 | prefer_file_cache); |
| 5945 | if (disassembler_sp) |
| 5946 | insn_list = &disassembler_sp->GetInstructionList(); |
| 5947 | |
| 5948 | if (insn_list == nullptr) { |
| 5949 | return retval; |
| 5950 | } |
| 5951 | |
| 5952 | size_t insn_offset = |
| 5953 | insn_list->GetIndexOfInstructionAtAddress(default_stop_addr); |
| 5954 | if (insn_offset == UINT32_MAX) { |
| 5955 | return retval; |
| 5956 | } |
| 5957 | |
Ted Woodward | 3169d92 | 2020-07-30 13:37:43 -0500 | [diff] [blame] | 5958 | uint32_t branch_index = insn_list->GetIndexOfNextBranchInstruction( |
| 5959 | insn_offset, false /* ignore_calls*/, nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5960 | if (branch_index == UINT32_MAX) { |
| 5961 | return retval; |
| 5962 | } |
| 5963 | |
| 5964 | if (branch_index > insn_offset) { |
| 5965 | Address next_branch_insn_address = |
| 5966 | insn_list->GetInstructionAtIndex(branch_index)->GetAddress(); |
| 5967 | if (next_branch_insn_address.IsValid() && |
| 5968 | range_bounds.ContainsFileAddress(next_branch_insn_address)) { |
| 5969 | retval = next_branch_insn_address; |
| 5970 | } |
| 5971 | } |
| 5972 | |
| 5973 | return retval; |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5974 | } |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 5975 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5976 | Status |
Tatyana Krasnukha | 36788bb | 2018-12-20 15:02:58 +0000 | [diff] [blame] | 5977 | Process::GetMemoryRegions(lldb_private::MemoryRegionInfos ®ion_list) { |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 5978 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5979 | Status error; |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 5980 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5981 | lldb::addr_t range_end = 0; |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 5982 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5983 | region_list.clear(); |
| 5984 | do { |
Tatyana Krasnukha | 36788bb | 2018-12-20 15:02:58 +0000 | [diff] [blame] | 5985 | lldb_private::MemoryRegionInfo region_info; |
| 5986 | error = GetMemoryRegionInfo(range_end, region_info); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5987 | // GetMemoryRegionInfo should only return an error if it is unimplemented. |
| 5988 | if (error.Fail()) { |
| 5989 | region_list.clear(); |
| 5990 | break; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5991 | } |
| 5992 | |
Tatyana Krasnukha | 36788bb | 2018-12-20 15:02:58 +0000 | [diff] [blame] | 5993 | range_end = region_info.GetRange().GetRangeEnd(); |
| 5994 | if (region_info.GetMapped() == MemoryRegionInfo::eYes) { |
| 5995 | region_list.push_back(std::move(region_info)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5996 | } |
| 5997 | } while (range_end != LLDB_INVALID_ADDRESS); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5998 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5999 | return error; |
| 6000 | } |
| 6001 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6002 | Status |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 6003 | Process::ConfigureStructuredData(ConstString type_name, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6004 | const StructuredData::ObjectSP &config_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6005 | // If you get this, the Process-derived class needs to implement a method to |
| 6006 | // enable an already-reported asynchronous structured data feature. See |
| 6007 | // ProcessGDBRemote for an example implementation over gdb-remote. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6008 | return Status("unimplemented"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6009 | } |
| 6010 | |
| 6011 | void Process::MapSupportedStructuredDataPlugins( |
| 6012 | const StructuredData::Array &supported_type_names) { |
| 6013 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 6014 | |
| 6015 | // Bail out early if there are no type names to map. |
| 6016 | if (supported_type_names.GetSize() == 0) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 6017 | LLDB_LOGF(log, "Process::%s(): no structured data types supported", |
| 6018 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6019 | return; |
| 6020 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6021 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6022 | // Convert StructuredData type names to ConstString instances. |
| 6023 | std::set<ConstString> const_type_names; |
| 6024 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 6025 | LLDB_LOGF(log, |
| 6026 | "Process::%s(): the process supports the following async " |
| 6027 | "structured data types:", |
| 6028 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6029 | |
| 6030 | supported_type_names.ForEach( |
| 6031 | [&const_type_names, &log](StructuredData::Object *object) { |
| 6032 | if (!object) { |
| 6033 | // Invalid - shouldn't be null objects in the array. |
| 6034 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6035 | } |
| 6036 | |
| 6037 | auto type_name = object->GetAsString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6038 | if (!type_name) { |
| 6039 | // Invalid format - all type names should be strings. |
| 6040 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6041 | } |
| 6042 | |
| 6043 | const_type_names.insert(ConstString(type_name->GetValue())); |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 6044 | LLDB_LOG(log, "- {0}", type_name->GetValue()); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6045 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6046 | }); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6047 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6048 | // For each StructuredDataPlugin, if the plugin handles any of the types in |
| 6049 | // the supported_type_names, map that type name to that plugin. Stop when |
| 6050 | // we've consumed all the type names. |
Leonard Mosescu | 9ba5157 | 2018-08-07 18:00:30 +0000 | [diff] [blame] | 6051 | // FIXME: should we return an error if there are type names nobody |
Jim Ingham | 93979f6 | 2018-04-27 01:57:40 +0000 | [diff] [blame] | 6052 | // supports? |
| 6053 | for (uint32_t plugin_index = 0; !const_type_names.empty(); plugin_index++) { |
| 6054 | auto create_instance = |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6055 | PluginManager::GetStructuredDataPluginCreateCallbackAtIndex( |
| 6056 | plugin_index); |
Jim Ingham | 93979f6 | 2018-04-27 01:57:40 +0000 | [diff] [blame] | 6057 | if (!create_instance) |
| 6058 | break; |
Leonard Mosescu | 9ba5157 | 2018-08-07 18:00:30 +0000 | [diff] [blame] | 6059 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6060 | // Create the plugin. |
| 6061 | StructuredDataPluginSP plugin_sp = (*create_instance)(*this); |
| 6062 | if (!plugin_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6063 | // This plugin doesn't think it can work with the process. Move on to the |
| 6064 | // next. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6065 | continue; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6066 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6067 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6068 | // 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] | 6069 | std::vector<ConstString> names_to_remove; |
| 6070 | for (auto &type_name : const_type_names) { |
| 6071 | if (plugin_sp->SupportsStructuredDataType(type_name)) { |
| 6072 | m_structured_data_plugin_map.insert( |
| 6073 | std::make_pair(type_name, plugin_sp)); |
| 6074 | names_to_remove.push_back(type_name); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 6075 | LLDB_LOGF(log, |
| 6076 | "Process::%s(): using plugin %s for type name " |
| 6077 | "%s", |
| 6078 | __FUNCTION__, plugin_sp->GetPluginName().GetCString(), |
| 6079 | type_name.GetCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6080 | } |
| 6081 | } |
| 6082 | |
| 6083 | // Remove the type names that were consumed by this plugin. |
| 6084 | for (auto &type_name : names_to_remove) |
| 6085 | const_type_names.erase(type_name); |
| 6086 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6089 | bool Process::RouteAsyncStructuredData( |
| 6090 | const StructuredData::ObjectSP object_sp) { |
| 6091 | // Nothing to do if there's no data. |
| 6092 | if (!object_sp) |
| 6093 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6094 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6095 | // The contract is this must be a dictionary, so we can look up the routing |
| 6096 | // key via the top-level 'type' string value within the dictionary. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6097 | StructuredData::Dictionary *dictionary = object_sp->GetAsDictionary(); |
| 6098 | if (!dictionary) |
| 6099 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6101 | // Grab the async structured type name (i.e. the feature/plugin name). |
| 6102 | ConstString type_name; |
| 6103 | if (!dictionary->GetValueForKeyAsString("type", type_name)) |
| 6104 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6106 | // Check if there's a plugin registered for this type name. |
| 6107 | auto find_it = m_structured_data_plugin_map.find(type_name); |
| 6108 | if (find_it == m_structured_data_plugin_map.end()) { |
| 6109 | // We don't have a mapping for this structured data type. |
| 6110 | return false; |
| 6111 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6113 | // Route the structured data to the plugin. |
| 6114 | find_it->second->HandleArrivalOfStructuredData(*this, type_name, object_sp); |
| 6115 | return true; |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 6116 | } |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 6117 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6118 | Status Process::UpdateAutomaticSignalFiltering() { |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 6119 | // Default implementation does nothign. |
| 6120 | // No automatic signal filtering to speak of. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6121 | return Status(); |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 6122 | } |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 6123 | |
Frederic Riss | d10d379 | 2018-05-11 18:21:11 +0000 | [diff] [blame] | 6124 | UtilityFunction *Process::GetLoadImageUtilityFunction( |
| 6125 | Platform *platform, |
| 6126 | llvm::function_ref<std::unique_ptr<UtilityFunction>()> factory) { |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 6127 | if (platform != GetTarget().GetPlatform().get()) |
| 6128 | return nullptr; |
Michal Gorny | 3276fff | 2019-09-03 12:31:24 +0000 | [diff] [blame] | 6129 | llvm::call_once(m_dlopen_utility_func_flag_once, |
| 6130 | [&] { m_dlopen_utility_func_up = factory(); }); |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 6131 | return m_dlopen_utility_func_up.get(); |
| 6132 | } |
Alex Langford | 5b2b38e | 2019-09-13 00:02:05 +0000 | [diff] [blame] | 6133 | |
Walter Erquinigo | fb19f11 | 2020-10-26 21:22:06 -0700 | [diff] [blame] | 6134 | llvm::Expected<TraceTypeInfo> Process::GetSupportedTraceType() { |
| 6135 | if (!IsLiveDebugSession()) |
| 6136 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
| 6137 | "Can't trace a non-live process."); |
| 6138 | return llvm::make_error<UnimplementedError>(); |
| 6139 | } |
| 6140 | |
Alex Langford | 5b2b38e | 2019-09-13 00:02:05 +0000 | [diff] [blame] | 6141 | bool Process::CallVoidArgVoidPtrReturn(const Address *address, |
| 6142 | addr_t &returned_func, |
| 6143 | bool trap_exceptions) { |
| 6144 | Thread *thread = GetThreadList().GetExpressionExecutionThread().get(); |
| 6145 | if (thread == nullptr || address == nullptr) |
| 6146 | return false; |
| 6147 | |
| 6148 | EvaluateExpressionOptions options; |
| 6149 | options.SetStopOthers(true); |
| 6150 | options.SetUnwindOnError(true); |
| 6151 | options.SetIgnoreBreakpoints(true); |
| 6152 | options.SetTryAllThreads(true); |
| 6153 | options.SetDebug(false); |
| 6154 | options.SetTimeout(GetUtilityExpressionTimeout()); |
| 6155 | options.SetTrapExceptions(trap_exceptions); |
| 6156 | |
| 6157 | auto type_system_or_err = |
| 6158 | GetTarget().GetScratchTypeSystemForLanguage(eLanguageTypeC); |
| 6159 | if (!type_system_or_err) { |
| 6160 | llvm::consumeError(type_system_or_err.takeError()); |
| 6161 | return false; |
| 6162 | } |
| 6163 | CompilerType void_ptr_type = |
| 6164 | type_system_or_err->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType(); |
| 6165 | lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallFunction( |
| 6166 | *thread, *address, void_ptr_type, llvm::ArrayRef<addr_t>(), options)); |
| 6167 | if (call_plan_sp) { |
| 6168 | DiagnosticManager diagnostics; |
| 6169 | |
| 6170 | StackFrame *frame = thread->GetStackFrameAtIndex(0).get(); |
| 6171 | if (frame) { |
| 6172 | ExecutionContext exe_ctx; |
| 6173 | frame->CalculateExecutionContext(exe_ctx); |
| 6174 | ExpressionResults result = |
| 6175 | RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics); |
| 6176 | if (result == eExpressionCompleted) { |
| 6177 | returned_func = |
| 6178 | call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned( |
| 6179 | LLDB_INVALID_ADDRESS); |
| 6180 | |
| 6181 | if (GetAddressByteSize() == 4) { |
| 6182 | if (returned_func == UINT32_MAX) |
| 6183 | return false; |
| 6184 | } else if (GetAddressByteSize() == 8) { |
| 6185 | if (returned_func == UINT64_MAX) |
| 6186 | return false; |
| 6187 | } |
| 6188 | return true; |
| 6189 | } |
| 6190 | } |
| 6191 | } |
| 6192 | |
| 6193 | return false; |
| 6194 | } |