Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Process.cpp ---------------------------------------------*- C++ -*-===// |
| 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 | |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 16 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Breakpoint/BreakpointLocation.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 18 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Module.h" |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 21 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 23 | #include "lldb/Core/StreamFile.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 24 | #include "lldb/Expression/DiagnosticManager.h" |
Alex Langford | bab7e3d | 2019-07-12 00:58:02 +0000 | [diff] [blame] | 25 | #include "lldb/Expression/DynamicCheckerFunctions.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 26 | #include "lldb/Expression/UserExpression.h" |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 27 | #include "lldb/Expression/UtilityFunction.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 28 | #include "lldb/Host/ConnectionFileDescriptor.h" |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame] | 29 | #include "lldb/Host/FileSystem.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | #include "lldb/Host/Host.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 31 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | 3eb2b44 | 2017-03-22 23:33:16 +0000 | [diff] [blame] | 32 | #include "lldb/Host/OptionParser.h" |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 33 | #include "lldb/Host/Pipe.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 34 | #include "lldb/Host/Terminal.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 35 | #include "lldb/Host/ThreadLauncher.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 36 | #include "lldb/Interpreter/CommandInterpreter.h" |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 37 | #include "lldb/Interpreter/OptionArgParser.h" |
Zachary Turner | 633a29c | 2015-03-04 01:58:01 +0000 | [diff] [blame] | 38 | #include "lldb/Interpreter/OptionValueProperties.h" |
Jason Molenda | 484900b | 2015-08-10 07:55:25 +0000 | [diff] [blame] | 39 | #include "lldb/Symbol/Function.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 40 | #include "lldb/Symbol/Symbol.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | #include "lldb/Target/ABI.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" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 62 | #include "lldb/Target/UnixSignals.h" |
Pavel Labath | 181b823 | 2018-12-14 15:59:49 +0000 | [diff] [blame] | 63 | #include "lldb/Utility/Event.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 64 | #include "lldb/Utility/Log.h" |
Zachary Turner | 5023257 | 2015-03-18 21:31:45 +0000 | [diff] [blame] | 65 | #include "lldb/Utility/NameMatches.h" |
Zachary Turner | 805e710 | 2019-03-04 21:51:03 +0000 | [diff] [blame] | 66 | #include "lldb/Utility/ProcessInfo.h" |
Greg Clayton | ee1f578 | 2016-08-10 22:43:48 +0000 | [diff] [blame] | 67 | #include "lldb/Utility/SelectHelper.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 68 | #include "lldb/Utility/State.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | |
| 70 | using namespace lldb; |
| 71 | using namespace lldb_private; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 72 | using namespace std::chrono; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | // Comment out line below to disable memory caching, overriding the process |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 75 | // setting target.process.disable-memory-cache |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 76 | #define ENABLE_MEMORY_CACHING |
| 77 | |
| 78 | #ifdef ENABLE_MEMORY_CACHING |
| 79 | #define DISABLE_MEM_CACHE_DEFAULT false |
| 80 | #else |
| 81 | #define DISABLE_MEM_CACHE_DEFAULT true |
| 82 | #endif |
| 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | class ProcessOptionValueProperties : public OptionValueProperties { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 85 | public: |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 86 | ProcessOptionValueProperties(ConstString name) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | : OptionValueProperties(name) {} |
| 88 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 89 | // This constructor is used when creating ProcessOptionValueProperties when |
| 90 | // it is part of a new lldb_private::Process instance. It will copy all |
| 91 | // current global property values as needed |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | ProcessOptionValueProperties(ProcessProperties *global_properties) |
| 93 | : OptionValueProperties(*global_properties->GetValueProperties()) {} |
| 94 | |
| 95 | const Property *GetPropertyAtIndex(const ExecutionContext *exe_ctx, |
| 96 | bool will_modify, |
| 97 | uint32_t idx) const override { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 98 | // When getting the value for a key from the process options, we will |
| 99 | // always try and grab the setting from the current process if there is |
| 100 | // one. Else we just use the one from this instance. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | if (exe_ctx) { |
| 102 | Process *process = exe_ctx->GetProcessPtr(); |
| 103 | if (process) { |
| 104 | ProcessOptionValueProperties *instance_properties = |
| 105 | static_cast<ProcessOptionValueProperties *>( |
| 106 | process->GetValueProperties().get()); |
| 107 | if (this != instance_properties) |
| 108 | return instance_properties->ProtectedGetPropertyAtIndex(idx); |
| 109 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 110 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | return ProtectedGetPropertyAtIndex(idx); |
| 112 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Jonas Devlieghere | 971f9ca | 2019-07-25 21:36:37 +0000 | [diff] [blame] | 115 | #define LLDB_PROPERTIES_process |
Jordan Rupprecht | 6a253d3 | 2019-07-29 17:22:10 +0000 | [diff] [blame^] | 116 | #include "TargetProperties.inc" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 117 | |
| 118 | enum { |
Jonas Devlieghere | 971f9ca | 2019-07-25 21:36:37 +0000 | [diff] [blame] | 119 | #define LLDB_PROPERTIES_process |
Jordan Rupprecht | 6a253d3 | 2019-07-29 17:22:10 +0000 | [diff] [blame^] | 120 | #include "TargetPropertiesEnum.inc" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | ProcessProperties::ProcessProperties(lldb_private::Process *process) |
| 124 | : Properties(), |
| 125 | m_process(process) // Can be nullptr for global ProcessProperties |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 126 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | if (process == nullptr) { |
| 128 | // Global process properties, set them up one time |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 129 | m_collection_sp = |
| 130 | std::make_shared<ProcessOptionValueProperties>(ConstString("process")); |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 131 | m_collection_sp->Initialize(g_process_properties); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | m_collection_sp->AppendProperty( |
| 133 | ConstString("thread"), ConstString("Settings specific to threads."), |
| 134 | true, Thread::GetGlobalProperties()->GetValueProperties()); |
| 135 | } else { |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 136 | m_collection_sp = std::make_shared<ProcessOptionValueProperties>( |
| 137 | Process::GetGlobalProperties().get()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | m_collection_sp->SetValueChangedCallback( |
| 139 | ePropertyPythonOSPluginPath, |
| 140 | ProcessProperties::OptionValueChangedCallback, this); |
| 141 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 144 | ProcessProperties::~ProcessProperties() = default; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 145 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 146 | void ProcessProperties::OptionValueChangedCallback(void *baton, |
| 147 | OptionValue *option_value) { |
| 148 | ProcessProperties *properties = (ProcessProperties *)baton; |
| 149 | if (properties->m_process) |
| 150 | properties->m_process->LoadOperatingSystemPlugin(true); |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | bool ProcessProperties::GetDisableMemoryCache() const { |
| 154 | const uint32_t idx = ePropertyDisableMemCache; |
| 155 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 156 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 159 | uint64_t ProcessProperties::GetMemoryCacheLineSize() const { |
| 160 | const uint32_t idx = ePropertyMemCacheLineSize; |
| 161 | return m_collection_sp->GetPropertyAtIndexAsUInt64( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 162 | nullptr, idx, g_process_properties[idx].default_uint_value); |
Jason Molenda | f0340c9 | 2014-09-03 22:30:54 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | Args ProcessProperties::GetExtraStartupCommands() const { |
| 166 | Args args; |
| 167 | const uint32_t idx = ePropertyExtraStartCommand; |
| 168 | m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args); |
| 169 | return args; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | void ProcessProperties::SetExtraStartupCommands(const Args &args) { |
| 173 | const uint32_t idx = ePropertyExtraStartCommand; |
| 174 | m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | FileSpec ProcessProperties::GetPythonOSPluginPath() const { |
| 178 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 179 | return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx); |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | void ProcessProperties::SetPythonOSPluginPath(const FileSpec &file) { |
| 183 | const uint32_t idx = ePropertyPythonOSPluginPath; |
| 184 | m_collection_sp->SetPropertyAtIndexAsFileSpec(nullptr, idx, file); |
Greg Clayton | c9d645d | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | bool ProcessProperties::GetIgnoreBreakpointsInExpressions() const { |
| 188 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 189 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 190 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | void ProcessProperties::SetIgnoreBreakpointsInExpressions(bool ignore) { |
| 194 | const uint32_t idx = ePropertyIgnoreBreakpointsInExpressions; |
| 195 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, ignore); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | bool ProcessProperties::GetUnwindOnErrorInExpressions() const { |
| 199 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 200 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 201 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Jim Ingham | 2995077 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 204 | void ProcessProperties::SetUnwindOnErrorInExpressions(bool ignore) { |
| 205 | const uint32_t idx = ePropertyUnwindOnErrorInExpressions; |
| 206 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, ignore); |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | bool ProcessProperties::GetStopOnSharedLibraryEvents() const { |
| 210 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 211 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 212 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 215 | void ProcessProperties::SetStopOnSharedLibraryEvents(bool stop) { |
| 216 | const uint32_t idx = ePropertyStopOnSharedLibraryEvents; |
| 217 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, stop); |
| 218 | } |
| 219 | |
| 220 | bool ProcessProperties::GetDetachKeepsStopped() const { |
| 221 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 222 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 223 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void ProcessProperties::SetDetachKeepsStopped(bool stop) { |
| 227 | const uint32_t idx = ePropertyDetachKeepsStopped; |
| 228 | m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, stop); |
| 229 | } |
| 230 | |
| 231 | bool ProcessProperties::GetWarningsOptimization() const { |
| 232 | const uint32_t idx = ePropertyWarningOptimization; |
| 233 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 234 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Jim Ingham | ba205c1 | 2017-12-05 02:50:45 +0000 | [diff] [blame] | 237 | bool ProcessProperties::GetStopOnExec() const { |
| 238 | const uint32_t idx = ePropertyStopOnExec; |
| 239 | return m_collection_sp->GetPropertyAtIndexAsBoolean( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 240 | nullptr, idx, g_process_properties[idx].default_uint_value != 0); |
Jim Ingham | ba205c1 | 2017-12-05 02:50:45 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 243 | std::chrono::seconds ProcessProperties::GetUtilityExpressionTimeout() const { |
| 244 | const uint32_t idx = ePropertyUtilityExpressionTimeout; |
| 245 | uint64_t value = m_collection_sp->GetPropertyAtIndexAsUInt64( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 246 | nullptr, idx, g_process_properties[idx].default_uint_value); |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 247 | return std::chrono::seconds(value); |
| 248 | } |
| 249 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 250 | Status ProcessLaunchCommandOptions::SetOptionValue( |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 251 | uint32_t option_idx, llvm::StringRef option_arg, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 252 | ExecutionContext *execution_context) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 253 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 254 | const int short_option = m_getopt_table[option_idx].val; |
| 255 | |
| 256 | switch (short_option) { |
| 257 | case 's': // Stop at program entry point |
| 258 | launch_info.GetFlags().Set(eLaunchFlagStopAtEntry); |
| 259 | break; |
| 260 | |
| 261 | case 'i': // STDIN for read only |
| 262 | { |
| 263 | FileAction action; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 264 | if (action.Open(STDIN_FILENO, FileSpec(option_arg), true, false)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | launch_info.AppendFileAction(action); |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | case 'o': // Open STDOUT for write only |
| 270 | { |
| 271 | FileAction action; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 272 | if (action.Open(STDOUT_FILENO, FileSpec(option_arg), false, true)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | launch_info.AppendFileAction(action); |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | case 'e': // STDERR for write only |
| 278 | { |
| 279 | FileAction action; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 280 | if (action.Open(STDERR_FILENO, FileSpec(option_arg), false, true)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 281 | launch_info.AppendFileAction(action); |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | case 'p': // Process plug-in name |
| 286 | launch_info.SetProcessPluginName(option_arg); |
| 287 | break; |
| 288 | |
| 289 | case 'n': // Disable STDIO |
| 290 | { |
| 291 | FileAction action; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 292 | const FileSpec dev_null(FileSystem::DEV_NULL); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 293 | if (action.Open(STDIN_FILENO, dev_null, true, false)) |
| 294 | launch_info.AppendFileAction(action); |
| 295 | if (action.Open(STDOUT_FILENO, dev_null, false, true)) |
| 296 | launch_info.AppendFileAction(action); |
| 297 | if (action.Open(STDERR_FILENO, dev_null, false, true)) |
| 298 | launch_info.AppendFileAction(action); |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | case 'w': |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 303 | launch_info.SetWorkingDirectory(FileSpec(option_arg)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 304 | break; |
| 305 | |
| 306 | case 't': // Open process in new terminal window |
| 307 | launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY); |
| 308 | break; |
| 309 | |
| 310 | case 'a': { |
| 311 | TargetSP target_sp = |
| 312 | execution_context ? execution_context->GetTargetSP() : TargetSP(); |
| 313 | PlatformSP platform_sp = |
| 314 | target_sp ? target_sp->GetPlatform() : PlatformSP(); |
Pavel Labath | 7263f1b | 2017-10-31 10:56:03 +0000 | [diff] [blame] | 315 | launch_info.GetArchitecture() = |
| 316 | Platform::GetAugmentedArchSpec(platform_sp.get(), option_arg); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 317 | } break; |
| 318 | |
| 319 | case 'A': // Disable ASLR. |
| 320 | { |
| 321 | bool success; |
| 322 | const bool disable_aslr_arg = |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 323 | OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 324 | if (success) |
| 325 | disable_aslr = disable_aslr_arg ? eLazyBoolYes : eLazyBoolNo; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 326 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 327 | error.SetErrorStringWithFormat( |
| 328 | "Invalid boolean value for disable-aslr option: '%s'", |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 329 | option_arg.empty() ? "<null>" : option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 330 | break; |
| 331 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 332 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 333 | case 'X': // shell expand args. |
| 334 | { |
| 335 | bool success; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 336 | const bool expand_args = |
| 337 | OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 338 | if (success) |
| 339 | launch_info.SetShellExpandArguments(expand_args); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 340 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 341 | error.SetErrorStringWithFormat( |
| 342 | "Invalid boolean value for shell-expand-args option: '%s'", |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 343 | option_arg.empty() ? "<null>" : option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 344 | break; |
| 345 | } |
| 346 | |
| 347 | case 'c': |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 348 | if (!option_arg.empty()) |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 349 | launch_info.SetShell(FileSpec(option_arg)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 350 | else |
| 351 | launch_info.SetShell(HostInfo::GetDefaultShell()); |
| 352 | break; |
| 353 | |
| 354 | case 'v': |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 355 | launch_info.GetEnvironment().insert(option_arg); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 356 | break; |
| 357 | |
| 358 | default: |
| 359 | error.SetErrorStringWithFormat("unrecognized short option character '%c'", |
| 360 | short_option); |
| 361 | break; |
| 362 | } |
| 363 | return error; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 366 | static constexpr OptionDefinition g_process_launch_options[] = { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 367 | {LLDB_OPT_SET_ALL, false, "stop-at-entry", 's', OptionParser::eNoArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 368 | nullptr, {}, 0, eArgTypeNone, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 369 | "Stop at the entry point of the program when launching a process."}, |
| 370 | {LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 371 | OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 372 | "Set whether to disable address space layout randomization when launching " |
| 373 | "a process."}, |
| 374 | {LLDB_OPT_SET_ALL, false, "plugin", 'p', OptionParser::eRequiredArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 375 | nullptr, {}, 0, eArgTypePlugin, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 376 | "Name of the process plugin you want to use."}, |
| 377 | {LLDB_OPT_SET_ALL, false, "working-dir", 'w', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 378 | OptionParser::eRequiredArgument, nullptr, {}, 0, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 379 | eArgTypeDirectoryName, |
| 380 | "Set the current working directory to <path> when running the inferior."}, |
| 381 | {LLDB_OPT_SET_ALL, false, "arch", 'a', OptionParser::eRequiredArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 382 | nullptr, {}, 0, eArgTypeArchitecture, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 383 | "Set the architecture for the process to launch when ambiguous."}, |
| 384 | {LLDB_OPT_SET_ALL, false, "environment", 'v', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 385 | OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeNone, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 386 | "Specify an environment variable name/value string (--environment " |
| 387 | "NAME=VALUE). Can be specified multiple times for subsequent environment " |
| 388 | "entries."}, |
| 389 | {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] | 390 | OptionParser::eOptionalArgument, nullptr, {}, 0, eArgTypeFilename, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 391 | "Run the process in a shell (not supported on all platforms)."}, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 392 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 393 | {LLDB_OPT_SET_1, false, "stdin", 'i', OptionParser::eRequiredArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 394 | nullptr, {}, 0, eArgTypeFilename, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 395 | "Redirect stdin for the process to <filename>."}, |
| 396 | {LLDB_OPT_SET_1, false, "stdout", 'o', OptionParser::eRequiredArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 397 | nullptr, {}, 0, eArgTypeFilename, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 398 | "Redirect stdout for the process to <filename>."}, |
| 399 | {LLDB_OPT_SET_1, false, "stderr", 'e', OptionParser::eRequiredArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 400 | nullptr, {}, 0, eArgTypeFilename, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 401 | "Redirect stderr for the process to <filename>."}, |
Todd Fiala | 7df337f | 2015-10-13 23:41:19 +0000 | [diff] [blame] | 402 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 403 | {LLDB_OPT_SET_2, false, "tty", 't', OptionParser::eNoArgument, nullptr, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 404 | {}, 0, eArgTypeNone, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 405 | "Start the process in a terminal (not supported on all platforms)."}, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 406 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 407 | {LLDB_OPT_SET_3, false, "no-stdio", 'n', OptionParser::eNoArgument, nullptr, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 408 | {}, 0, eArgTypeNone, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 409 | "Do not set up for terminal I/O to go to running process."}, |
| 410 | {LLDB_OPT_SET_4, false, "shell-expand-args", 'X', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 411 | OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 412 | "Set whether to shell expand arguments to the process when launching."}, |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | llvm::ArrayRef<OptionDefinition> ProcessLaunchCommandOptions::GetDefinitions() { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 416 | return llvm::makeArrayRef(g_process_launch_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 417 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 418 | |
Zachary Turner | 3165945 | 2016-11-17 21:15:14 +0000 | [diff] [blame] | 419 | ProcessSP Process::FindPlugin(lldb::TargetSP target_sp, |
| 420 | llvm::StringRef plugin_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 421 | ListenerSP listener_sp, |
| 422 | const FileSpec *crash_file_path) { |
| 423 | static uint32_t g_process_unique_id = 0; |
| 424 | |
| 425 | ProcessSP process_sp; |
| 426 | ProcessCreateInstance create_callback = nullptr; |
Zachary Turner | 3165945 | 2016-11-17 21:15:14 +0000 | [diff] [blame] | 427 | if (!plugin_name.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 428 | ConstString const_plugin_name(plugin_name); |
| 429 | create_callback = |
| 430 | PluginManager::GetProcessCreateCallbackForPluginName(const_plugin_name); |
| 431 | if (create_callback) { |
| 432 | process_sp = create_callback(target_sp, listener_sp, crash_file_path); |
| 433 | if (process_sp) { |
| 434 | if (process_sp->CanDebug(target_sp, true)) { |
| 435 | process_sp->m_process_unique_id = ++g_process_unique_id; |
| 436 | } else |
| 437 | process_sp.reset(); |
| 438 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 439 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 440 | } else { |
| 441 | for (uint32_t idx = 0; |
| 442 | (create_callback = |
| 443 | PluginManager::GetProcessCreateCallbackAtIndex(idx)) != nullptr; |
| 444 | ++idx) { |
| 445 | process_sp = create_callback(target_sp, listener_sp, crash_file_path); |
| 446 | if (process_sp) { |
| 447 | if (process_sp->CanDebug(target_sp, false)) { |
| 448 | process_sp->m_process_unique_id = ++g_process_unique_id; |
| 449 | break; |
| 450 | } else |
| 451 | process_sp.reset(); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 458 | ConstString &Process::GetStaticBroadcasterClass() { |
| 459 | static ConstString class_name("lldb.process"); |
| 460 | return class_name; |
| 461 | } |
| 462 | |
| 463 | Process::Process(lldb::TargetSP target_sp, ListenerSP listener_sp) |
| 464 | : Process(target_sp, listener_sp, |
| 465 | UnixSignals::Create(HostInfo::GetArchitecture())) { |
| 466 | // This constructor just delegates to the full Process constructor, |
| 467 | // defaulting to using the Host's UnixSignals. |
| 468 | } |
| 469 | |
| 470 | Process::Process(lldb::TargetSP target_sp, ListenerSP listener_sp, |
| 471 | const UnixSignalsSP &unix_signals_sp) |
| 472 | : ProcessProperties(this), UserID(LLDB_INVALID_PROCESS_ID), |
| 473 | Broadcaster((target_sp->GetDebugger().GetBroadcasterManager()), |
| 474 | Process::GetStaticBroadcasterClass().AsCString()), |
Jim Ingham | b87b9e6 | 2018-06-26 23:38:58 +0000 | [diff] [blame] | 475 | m_target_wp(target_sp), m_public_state(eStateUnloaded), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 476 | m_private_state(eStateUnloaded), |
| 477 | m_private_state_broadcaster(nullptr, |
| 478 | "lldb.process.internal_state_broadcaster"), |
| 479 | m_private_state_control_broadcaster( |
| 480 | nullptr, "lldb.process.internal_state_control_broadcaster"), |
| 481 | m_private_state_listener_sp( |
| 482 | Listener::MakeListener("lldb.process.internal_state_listener")), |
| 483 | m_mod_id(), m_process_unique_id(0), m_thread_index_id(0), |
| 484 | m_thread_id_to_index_id_map(), m_exit_status(-1), m_exit_string(), |
| 485 | m_exit_status_mutex(), m_thread_mutex(), m_thread_list_real(this), |
| 486 | m_thread_list(this), m_extended_thread_list(this), |
| 487 | m_extended_thread_stop_id(0), m_queue_list(this), m_queue_list_stop_id(0), |
| 488 | m_notifications(), m_image_tokens(), m_listener_sp(listener_sp), |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 489 | m_breakpoint_site_list(), m_dynamic_checkers_up(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 490 | m_unix_signals_sp(unix_signals_sp), m_abi_sp(), m_process_input_reader(), |
| 491 | m_stdio_communication("process.stdio"), m_stdio_communication_mutex(), |
| 492 | m_stdin_forward(false), m_stdout_data(), m_stderr_data(), |
| 493 | m_profile_data_comm_mutex(), m_profile_data(), m_iohandler_sync(0), |
| 494 | m_memory_cache(*this), m_allocated_memory_cache(*this), |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 495 | 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] | 496 | m_private_run_lock(), m_finalizing(false), m_finalize_called(false), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 497 | m_clear_thread_plans_on_stop(false), m_force_next_event_delivery(false), |
| 498 | m_last_broadcast_state(eStateInvalid), m_destroy_in_process(false), |
| 499 | m_can_interpret_function_calls(false), m_warnings_issued(), |
| 500 | m_run_thread_plan_lock(), m_can_jit(eCanJITDontKnow) { |
| 501 | CheckInWithManager(); |
| 502 | |
| 503 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 504 | LLDB_LOGF(log, "%p Process::Process()", static_cast<void *>(this)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 505 | |
| 506 | if (!m_unix_signals_sp) |
| 507 | m_unix_signals_sp = std::make_shared<UnixSignals>(); |
| 508 | |
| 509 | SetEventName(eBroadcastBitStateChanged, "state-changed"); |
| 510 | SetEventName(eBroadcastBitInterrupt, "interrupt"); |
| 511 | SetEventName(eBroadcastBitSTDOUT, "stdout-available"); |
| 512 | SetEventName(eBroadcastBitSTDERR, "stderr-available"); |
| 513 | SetEventName(eBroadcastBitProfileData, "profile-data-available"); |
| 514 | SetEventName(eBroadcastBitStructuredData, "structured-data-available"); |
| 515 | |
| 516 | m_private_state_control_broadcaster.SetEventName( |
| 517 | eBroadcastInternalStateControlStop, "control-stop"); |
| 518 | m_private_state_control_broadcaster.SetEventName( |
| 519 | eBroadcastInternalStateControlPause, "control-pause"); |
| 520 | m_private_state_control_broadcaster.SetEventName( |
| 521 | eBroadcastInternalStateControlResume, "control-resume"); |
| 522 | |
| 523 | m_listener_sp->StartListeningForEvents( |
| 524 | this, eBroadcastBitStateChanged | eBroadcastBitInterrupt | |
| 525 | eBroadcastBitSTDOUT | eBroadcastBitSTDERR | |
| 526 | eBroadcastBitProfileData | eBroadcastBitStructuredData); |
| 527 | |
| 528 | m_private_state_listener_sp->StartListeningForEvents( |
| 529 | &m_private_state_broadcaster, |
| 530 | eBroadcastBitStateChanged | eBroadcastBitInterrupt); |
| 531 | |
| 532 | m_private_state_listener_sp->StartListeningForEvents( |
| 533 | &m_private_state_control_broadcaster, |
| 534 | eBroadcastInternalStateControlStop | eBroadcastInternalStateControlPause | |
| 535 | eBroadcastInternalStateControlResume); |
| 536 | // We need something valid here, even if just the default UnixSignalsSP. |
| 537 | assert(m_unix_signals_sp && "null m_unix_signals_sp after initialization"); |
| 538 | |
| 539 | // Allow the platform to override the default cache line size |
| 540 | OptionValueSP value_sp = |
| 541 | m_collection_sp |
| 542 | ->GetPropertyAtIndex(nullptr, true, ePropertyMemCacheLineSize) |
| 543 | ->GetValue(); |
| 544 | uint32_t platform_cache_line_size = |
| 545 | target_sp->GetPlatform()->GetDefaultMemoryCacheLineSize(); |
| 546 | if (!value_sp->OptionWasSet() && platform_cache_line_size != 0) |
| 547 | value_sp->SetUInt64Value(platform_cache_line_size); |
| 548 | } |
| 549 | |
| 550 | Process::~Process() { |
| 551 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 552 | LLDB_LOGF(log, "%p Process::~Process()", static_cast<void *>(this)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 553 | StopPrivateStateThread(); |
| 554 | |
| 555 | // ThreadList::Clear() will try to acquire this process's mutex, so |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 556 | // explicitly clear the thread list here to ensure that the mutex is not |
| 557 | // destroyed before the thread list. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 558 | m_thread_list.Clear(); |
| 559 | } |
| 560 | |
| 561 | const ProcessPropertiesSP &Process::GetGlobalProperties() { |
| 562 | // NOTE: intentional leak so we don't crash if global destructor chain gets |
| 563 | // called as other threads still use the result of this function |
Pavel Labath | 5f05ea8 | 2016-10-19 15:12:45 +0000 | [diff] [blame] | 564 | static ProcessPropertiesSP *g_settings_sp_ptr = |
| 565 | new ProcessPropertiesSP(new ProcessProperties(nullptr)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 566 | return *g_settings_sp_ptr; |
| 567 | } |
| 568 | |
| 569 | void Process::Finalize() { |
| 570 | m_finalizing = true; |
| 571 | |
| 572 | // Destroy this process if needed |
| 573 | switch (GetPrivateState()) { |
| 574 | case eStateConnected: |
| 575 | case eStateAttaching: |
| 576 | case eStateLaunching: |
| 577 | case eStateStopped: |
| 578 | case eStateRunning: |
| 579 | case eStateStepping: |
| 580 | case eStateCrashed: |
| 581 | case eStateSuspended: |
| 582 | Destroy(false); |
| 583 | break; |
| 584 | |
| 585 | case eStateInvalid: |
| 586 | case eStateUnloaded: |
| 587 | case eStateDetached: |
| 588 | case eStateExited: |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | // Clear our broadcaster before we proceed with destroying |
| 593 | Broadcaster::Clear(); |
| 594 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 595 | // Do any cleanup needed prior to being destructed... Subclasses that |
| 596 | // override this method should call this superclass method as well. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 597 | |
| 598 | // We need to destroy the loader before the derived Process class gets |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 599 | // destroyed since it is very likely that undoing the loader will require |
| 600 | // access to the real process. |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 601 | m_dynamic_checkers_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 602 | m_abi_sp.reset(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 603 | m_os_up.reset(); |
| 604 | m_system_runtime_up.reset(); |
| 605 | m_dyld_up.reset(); |
| 606 | m_jit_loaders_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 607 | m_thread_list_real.Destroy(); |
| 608 | m_thread_list.Destroy(); |
| 609 | m_extended_thread_list.Destroy(); |
| 610 | m_queue_list.Clear(); |
| 611 | m_queue_list_stop_id = 0; |
| 612 | std::vector<Notifications> empty_notifications; |
| 613 | m_notifications.swap(empty_notifications); |
| 614 | m_image_tokens.clear(); |
| 615 | m_memory_cache.Clear(); |
| 616 | m_allocated_memory_cache.Clear(); |
Alex Langford | 74eb76f | 2019-05-22 23:01:18 +0000 | [diff] [blame] | 617 | { |
| 618 | std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex); |
| 619 | m_language_runtimes.clear(); |
| 620 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 621 | m_instrumentation_runtimes.clear(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 622 | m_next_event_action_up.reset(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 623 | // Clear the last natural stop ID since it has a strong reference to this |
| 624 | // process |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 625 | m_mod_id.SetStopEventForLastNaturalStopID(EventSP()); |
| 626 | //#ifdef LLDB_CONFIGURATION_DEBUG |
| 627 | // StreamFile s(stdout, false); |
| 628 | // EventSP event_sp; |
| 629 | // while (m_private_state_listener_sp->GetNextEvent(event_sp)) |
| 630 | // { |
| 631 | // event_sp->Dump (&s); |
| 632 | // s.EOL(); |
| 633 | // } |
| 634 | //#endif |
| 635 | // We have to be very careful here as the m_private_state_listener might |
| 636 | // contain events that have ProcessSP values in them which can keep this |
| 637 | // process around forever. These events need to be cleared out. |
| 638 | m_private_state_listener_sp->Clear(); |
| 639 | m_public_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 640 | m_public_run_lock.SetStopped(); |
| 641 | m_private_run_lock.TrySetRunning(); // This will do nothing if already locked |
| 642 | m_private_run_lock.SetStopped(); |
Jason Molenda | 3826727 | 2016-12-16 02:48:39 +0000 | [diff] [blame] | 643 | m_structured_data_plugin_map.clear(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 644 | m_finalize_called = true; |
| 645 | } |
| 646 | |
| 647 | void Process::RegisterNotificationCallbacks(const Notifications &callbacks) { |
| 648 | m_notifications.push_back(callbacks); |
| 649 | if (callbacks.initialize != nullptr) |
| 650 | callbacks.initialize(callbacks.baton, this); |
| 651 | } |
| 652 | |
| 653 | bool Process::UnregisterNotificationCallbacks(const Notifications &callbacks) { |
| 654 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 655 | for (pos = m_notifications.begin(); pos != end; ++pos) { |
| 656 | if (pos->baton == callbacks.baton && |
| 657 | pos->initialize == callbacks.initialize && |
| 658 | pos->process_state_changed == callbacks.process_state_changed) { |
| 659 | m_notifications.erase(pos); |
| 660 | return true; |
| 661 | } |
| 662 | } |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | void Process::SynchronouslyNotifyStateChanged(StateType state) { |
| 667 | std::vector<Notifications>::iterator notification_pos, |
| 668 | notification_end = m_notifications.end(); |
| 669 | for (notification_pos = m_notifications.begin(); |
| 670 | notification_pos != notification_end; ++notification_pos) { |
| 671 | if (notification_pos->process_state_changed) |
| 672 | notification_pos->process_state_changed(notification_pos->baton, this, |
| 673 | state); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | // FIXME: We need to do some work on events before the general Listener sees |
| 678 | // them. |
| 679 | // 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] | 680 | // we do the little "insert real insn, step & stop" trick. But we can't do |
| 681 | // that when the event is delivered by the broadcaster - since that is done on |
| 682 | // the thread that is waiting for new events, so if we needed more than one |
| 683 | // event for our handling, we would stall. So instead we do it when we fetch |
| 684 | // the event off of the queue. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 685 | // |
| 686 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 687 | StateType Process::GetNextEvent(EventSP &event_sp) { |
| 688 | StateType state = eStateInvalid; |
| 689 | |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 690 | if (m_listener_sp->GetEventForBroadcaster(this, event_sp, |
| 691 | std::chrono::seconds(0)) && |
| 692 | event_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 693 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 694 | |
| 695 | return state; |
| 696 | } |
| 697 | |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 698 | void Process::SyncIOHandler(uint32_t iohandler_id, |
| 699 | const Timeout<std::micro> &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 700 | // don't sync (potentially context switch) in case where there is no process |
| 701 | // IO |
| 702 | if (!m_process_input_reader) |
| 703 | return; |
| 704 | |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 705 | auto Result = m_iohandler_sync.WaitForValueNotEqualTo(iohandler_id, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 706 | |
| 707 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | 3879fe0 | 2018-05-09 14:29:30 +0000 | [diff] [blame] | 708 | if (Result) { |
| 709 | LLDB_LOG( |
| 710 | log, |
| 711 | "waited from m_iohandler_sync to change from {0}. New value is {1}.", |
| 712 | iohandler_id, *Result); |
| 713 | } else { |
| 714 | LLDB_LOG(log, "timed out waiting for m_iohandler_sync to change from {0}.", |
| 715 | iohandler_id); |
| 716 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 719 | StateType Process::WaitForProcessToStop(const Timeout<std::micro> &timeout, |
| 720 | EventSP *event_sp_ptr, bool wait_always, |
| 721 | ListenerSP hijack_listener_sp, |
| 722 | Stream *stream, bool use_run_lock) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 723 | // 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] | 724 | // have restarted the target. We have to actually check each event, and in |
| 725 | // 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] | 726 | if (event_sp_ptr) |
| 727 | event_sp_ptr->reset(); |
| 728 | StateType state = GetState(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 729 | // If we are exited or detached, we won't ever get back to any other valid |
| 730 | // state... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 731 | if (state == eStateDetached || state == eStateExited) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 732 | return state; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 733 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 734 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 735 | LLDB_LOG(log, "timeout = {0}", timeout); |
Todd Fiala | a3b89e2 | 2014-08-12 14:33:19 +0000 | [diff] [blame] | 736 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 737 | if (!wait_always && StateIsStoppedState(state, true) && |
| 738 | StateIsStoppedState(GetPrivateState(), true)) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 739 | LLDB_LOGF(log, |
| 740 | "Process::%s returning without waiting for events; process " |
| 741 | "private and public states are already 'stopped'.", |
| 742 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 743 | // We need to toggle the run lock as this won't get done in |
| 744 | // SetPublicState() if the process is hijacked. |
| 745 | if (hijack_listener_sp && use_run_lock) |
| 746 | m_public_run_lock.SetStopped(); |
| 747 | return state; |
| 748 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 749 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 750 | while (state != eStateInvalid) { |
| 751 | EventSP event_sp; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 752 | state = GetStateChangedEvents(event_sp, timeout, hijack_listener_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 753 | if (event_sp_ptr && event_sp) |
| 754 | *event_sp_ptr = event_sp; |
Jim Ingham | 4b53618 | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 755 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 756 | bool pop_process_io_handler = (hijack_listener_sp.get() != nullptr); |
| 757 | Process::HandleProcessStateChangedEvent(event_sp, stream, |
| 758 | pop_process_io_handler); |
Daniel Malea | 9e9919f | 2013-10-09 16:56:28 +0000 | [diff] [blame] | 759 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 760 | switch (state) { |
| 761 | case eStateCrashed: |
| 762 | case eStateDetached: |
| 763 | case eStateExited: |
| 764 | case eStateUnloaded: |
| 765 | // We need to toggle the run lock as this won't get done in |
| 766 | // SetPublicState() if the process is hijacked. |
| 767 | if (hijack_listener_sp && use_run_lock) |
| 768 | m_public_run_lock.SetStopped(); |
| 769 | return state; |
| 770 | case eStateStopped: |
| 771 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 772 | continue; |
| 773 | else { |
Pavel Labath | 78521ef | 2015-03-06 10:52:47 +0000 | [diff] [blame] | 774 | // We need to toggle the run lock as this won't get done in |
| 775 | // SetPublicState() if the process is hijacked. |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 776 | if (hijack_listener_sp && use_run_lock) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 777 | m_public_run_lock.SetStopped(); |
| 778 | return state; |
| 779 | } |
| 780 | default: |
| 781 | continue; |
| 782 | } |
| 783 | } |
| 784 | return state; |
| 785 | } |
| 786 | |
| 787 | bool Process::HandleProcessStateChangedEvent(const EventSP &event_sp, |
| 788 | Stream *stream, |
| 789 | bool &pop_process_io_handler) { |
| 790 | const bool handle_pop = pop_process_io_handler; |
| 791 | |
| 792 | pop_process_io_handler = false; |
| 793 | ProcessSP process_sp = |
| 794 | Process::ProcessEventData::GetProcessFromEvent(event_sp.get()); |
| 795 | |
| 796 | if (!process_sp) |
| 797 | return false; |
| 798 | |
| 799 | StateType event_state = |
| 800 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 801 | if (event_state == eStateInvalid) |
| 802 | return false; |
| 803 | |
| 804 | switch (event_state) { |
| 805 | case eStateInvalid: |
| 806 | case eStateUnloaded: |
| 807 | case eStateAttaching: |
| 808 | case eStateLaunching: |
| 809 | case eStateStepping: |
| 810 | case eStateDetached: |
| 811 | if (stream) |
| 812 | stream->Printf("Process %" PRIu64 " %s\n", process_sp->GetID(), |
| 813 | StateAsCString(event_state)); |
| 814 | if (event_state == eStateDetached) |
| 815 | pop_process_io_handler = true; |
| 816 | break; |
| 817 | |
| 818 | case eStateConnected: |
| 819 | case eStateRunning: |
| 820 | // Don't be chatty when we run... |
| 821 | break; |
| 822 | |
| 823 | case eStateExited: |
| 824 | if (stream) |
| 825 | process_sp->GetStatus(*stream); |
| 826 | pop_process_io_handler = true; |
| 827 | break; |
| 828 | |
| 829 | case eStateStopped: |
| 830 | case eStateCrashed: |
| 831 | case eStateSuspended: |
| 832 | // Make sure the program hasn't been auto-restarted: |
| 833 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) { |
| 834 | if (stream) { |
| 835 | size_t num_reasons = |
| 836 | Process::ProcessEventData::GetNumRestartedReasons(event_sp.get()); |
| 837 | if (num_reasons > 0) { |
| 838 | // FIXME: Do we want to report this, or would that just be annoyingly |
| 839 | // chatty? |
| 840 | if (num_reasons == 1) { |
| 841 | const char *reason = |
| 842 | Process::ProcessEventData::GetRestartedReasonAtIndex( |
| 843 | event_sp.get(), 0); |
| 844 | stream->Printf("Process %" PRIu64 " stopped and restarted: %s\n", |
| 845 | process_sp->GetID(), |
| 846 | reason ? reason : "<UNKNOWN REASON>"); |
| 847 | } else { |
| 848 | stream->Printf("Process %" PRIu64 |
| 849 | " stopped and restarted, reasons:\n", |
| 850 | process_sp->GetID()); |
| 851 | |
| 852 | for (size_t i = 0; i < num_reasons; i++) { |
| 853 | const char *reason = |
| 854 | Process::ProcessEventData::GetRestartedReasonAtIndex( |
| 855 | event_sp.get(), i); |
| 856 | stream->Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>"); |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | } else { |
| 862 | StopInfoSP curr_thread_stop_info_sp; |
| 863 | // Lock the thread list so it doesn't change on us, this is the scope for |
| 864 | // the locker: |
| 865 | { |
| 866 | ThreadList &thread_list = process_sp->GetThreadList(); |
| 867 | std::lock_guard<std::recursive_mutex> guard(thread_list.GetMutex()); |
| 868 | |
| 869 | ThreadSP curr_thread(thread_list.GetSelectedThread()); |
| 870 | ThreadSP thread; |
| 871 | StopReason curr_thread_stop_reason = eStopReasonInvalid; |
| 872 | if (curr_thread) { |
| 873 | curr_thread_stop_reason = curr_thread->GetStopReason(); |
| 874 | curr_thread_stop_info_sp = curr_thread->GetStopInfo(); |
| 875 | } |
| 876 | if (!curr_thread || !curr_thread->IsValid() || |
| 877 | curr_thread_stop_reason == eStopReasonInvalid || |
| 878 | curr_thread_stop_reason == eStopReasonNone) { |
| 879 | // Prefer a thread that has just completed its plan over another |
| 880 | // thread as current thread. |
| 881 | ThreadSP plan_thread; |
| 882 | ThreadSP other_thread; |
| 883 | |
| 884 | const size_t num_threads = thread_list.GetSize(); |
| 885 | size_t i; |
| 886 | for (i = 0; i < num_threads; ++i) { |
| 887 | thread = thread_list.GetThreadAtIndex(i); |
| 888 | StopReason thread_stop_reason = thread->GetStopReason(); |
| 889 | switch (thread_stop_reason) { |
| 890 | case eStopReasonInvalid: |
| 891 | case eStopReasonNone: |
| 892 | break; |
| 893 | |
| 894 | case eStopReasonSignal: { |
| 895 | // 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] | 896 | // that signal. We have to have had another reason for stopping |
| 897 | // here, and the user doesn't want to see this thread. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 898 | uint64_t signo = thread->GetStopInfo()->GetValue(); |
| 899 | if (process_sp->GetUnixSignals()->GetShouldStop(signo)) { |
| 900 | if (!other_thread) |
| 901 | other_thread = thread; |
| 902 | } |
| 903 | break; |
| 904 | } |
| 905 | case eStopReasonTrace: |
| 906 | case eStopReasonBreakpoint: |
| 907 | case eStopReasonWatchpoint: |
| 908 | case eStopReasonException: |
| 909 | case eStopReasonExec: |
| 910 | case eStopReasonThreadExiting: |
| 911 | case eStopReasonInstrumentation: |
| 912 | if (!other_thread) |
| 913 | other_thread = thread; |
| 914 | break; |
| 915 | case eStopReasonPlanComplete: |
| 916 | if (!plan_thread) |
| 917 | plan_thread = thread; |
| 918 | break; |
| 919 | } |
| 920 | } |
| 921 | if (plan_thread) |
| 922 | thread_list.SetSelectedThreadByID(plan_thread->GetID()); |
| 923 | else if (other_thread) |
| 924 | thread_list.SetSelectedThreadByID(other_thread->GetID()); |
| 925 | else { |
| 926 | if (curr_thread && curr_thread->IsValid()) |
| 927 | thread = curr_thread; |
| 928 | else |
| 929 | thread = thread_list.GetThreadAtIndex(0); |
| 930 | |
| 931 | if (thread) |
| 932 | thread_list.SetSelectedThreadByID(thread->GetID()); |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | // Drop the ThreadList mutex by here, since GetThreadStatus below might |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 937 | // have to run code, e.g. for Data formatters, and if we hold the |
| 938 | // ThreadList mutex, then the process is going to have a hard time |
| 939 | // restarting the process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 940 | if (stream) { |
| 941 | Debugger &debugger = process_sp->GetTarget().GetDebugger(); |
| 942 | if (debugger.GetTargetList().GetSelectedTarget().get() == |
| 943 | &process_sp->GetTarget()) { |
| 944 | const bool only_threads_with_stop_reason = true; |
| 945 | const uint32_t start_frame = 0; |
| 946 | const uint32_t num_frames = 1; |
| 947 | const uint32_t num_frames_with_source = 1; |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 948 | const bool stop_format = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 949 | process_sp->GetStatus(*stream); |
| 950 | process_sp->GetThreadStatus(*stream, only_threads_with_stop_reason, |
| 951 | start_frame, num_frames, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 952 | num_frames_with_source, |
| 953 | stop_format); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 954 | if (curr_thread_stop_info_sp) { |
| 955 | lldb::addr_t crashing_address; |
| 956 | ValueObjectSP valobj_sp = StopInfo::GetCrashingDereference( |
| 957 | curr_thread_stop_info_sp, &crashing_address); |
| 958 | if (valobj_sp) { |
| 959 | const bool qualify_cxx_base_classes = false; |
| 960 | |
| 961 | const ValueObject::GetExpressionPathFormat format = |
| 962 | ValueObject::GetExpressionPathFormat:: |
| 963 | eGetExpressionPathFormatHonorPointers; |
| 964 | stream->PutCString("Likely cause: "); |
| 965 | valobj_sp->GetExpressionPath(*stream, qualify_cxx_base_classes, |
| 966 | format); |
| 967 | stream->Printf(" accessed 0x%" PRIx64 "\n", crashing_address); |
| 968 | } |
| 969 | } |
| 970 | } else { |
| 971 | uint32_t target_idx = debugger.GetTargetList().GetIndexOfTarget( |
| 972 | process_sp->GetTarget().shared_from_this()); |
| 973 | if (target_idx != UINT32_MAX) |
| 974 | stream->Printf("Target %d: (", target_idx); |
| 975 | else |
| 976 | stream->Printf("Target <unknown index>: ("); |
| 977 | process_sp->GetTarget().Dump(stream, eDescriptionLevelBrief); |
| 978 | stream->Printf(") stopped.\n"); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | // Pop the process IO handler |
| 983 | pop_process_io_handler = true; |
| 984 | } |
| 985 | break; |
| 986 | } |
| 987 | |
| 988 | if (handle_pop && pop_process_io_handler) |
| 989 | process_sp->PopProcessIOHandler(); |
| 990 | |
| 991 | return true; |
| 992 | } |
| 993 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 994 | bool Process::HijackProcessEvents(ListenerSP listener_sp) { |
| 995 | if (listener_sp) { |
| 996 | return HijackBroadcaster(listener_sp, eBroadcastBitStateChanged | |
| 997 | eBroadcastBitInterrupt); |
| 998 | } else |
| 999 | return false; |
| 1000 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1001 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1002 | void Process::RestoreProcessEvents() { RestoreBroadcaster(); } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1003 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1004 | StateType Process::GetStateChangedEvents(EventSP &event_sp, |
| 1005 | const Timeout<std::micro> &timeout, |
| 1006 | ListenerSP hijack_listener_sp) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1007 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1008 | LLDB_LOG(log, "timeout = {0}, event_sp)...", timeout); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1009 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1010 | ListenerSP listener_sp = hijack_listener_sp; |
| 1011 | if (!listener_sp) |
| 1012 | listener_sp = m_listener_sp; |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1013 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1014 | StateType state = eStateInvalid; |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 1015 | if (listener_sp->GetEventForBroadcasterWithType( |
| 1016 | this, eBroadcastBitStateChanged | eBroadcastBitInterrupt, event_sp, |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1017 | timeout)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1018 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1019 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1020 | else |
| 1021 | LLDB_LOG(log, "got no event or was interrupted."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1022 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1023 | |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1024 | LLDB_LOG(log, "timeout = {0}, event_sp) => {1}", timeout, state); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1025 | return state; |
| 1026 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1027 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1028 | Event *Process::PeekAtStateChangedEvents() { |
| 1029 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1030 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1031 | LLDB_LOGF(log, "Process::%s...", __FUNCTION__); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1032 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1033 | Event *event_ptr; |
| 1034 | event_ptr = m_listener_sp->PeekAtNextEventForBroadcasterWithType( |
| 1035 | this, eBroadcastBitStateChanged); |
| 1036 | if (log) { |
| 1037 | if (event_ptr) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1038 | LLDB_LOGF(log, "Process::%s (event_ptr) => %s", __FUNCTION__, |
| 1039 | StateAsCString(ProcessEventData::GetStateFromEvent(event_ptr))); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1040 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1041 | LLDB_LOGF(log, "Process::%s no events found", __FUNCTION__); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1042 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1043 | } |
| 1044 | return event_ptr; |
| 1045 | } |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1046 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1047 | StateType |
| 1048 | Process::GetStateChangedEventsPrivate(EventSP &event_sp, |
| 1049 | const Timeout<std::micro> &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1050 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1051 | LLDB_LOG(log, "timeout = {0}, event_sp)...", timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1052 | |
| 1053 | StateType state = eStateInvalid; |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 1054 | if (m_private_state_listener_sp->GetEventForBroadcasterWithType( |
| 1055 | &m_private_state_broadcaster, |
| 1056 | eBroadcastBitStateChanged | eBroadcastBitInterrupt, event_sp, |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1057 | timeout)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1058 | if (event_sp && event_sp->GetType() == eBroadcastBitStateChanged) |
| 1059 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1060 | |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1061 | LLDB_LOG(log, "timeout = {0}, event_sp) => {1}", timeout, |
| 1062 | state == eStateInvalid ? "TIMEOUT" : StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1063 | return state; |
| 1064 | } |
| 1065 | |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1066 | bool Process::GetEventsPrivate(EventSP &event_sp, |
| 1067 | const Timeout<std::micro> &timeout, |
| 1068 | bool control_only) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1069 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 1070 | LLDB_LOG(log, "timeout = {0}, event_sp)...", timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1071 | |
| 1072 | if (control_only) |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 1073 | return m_private_state_listener_sp->GetEventForBroadcaster( |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1074 | &m_private_state_control_broadcaster, event_sp, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1075 | else |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1076 | return m_private_state_listener_sp->GetEvent(event_sp, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | bool Process::IsRunning() const { |
| 1080 | return StateIsRunningState(m_public_state.GetValue()); |
| 1081 | } |
| 1082 | |
| 1083 | int Process::GetExitStatus() { |
| 1084 | std::lock_guard<std::mutex> guard(m_exit_status_mutex); |
| 1085 | |
| 1086 | if (m_public_state.GetValue() == eStateExited) |
| 1087 | return m_exit_status; |
| 1088 | return -1; |
| 1089 | } |
| 1090 | |
| 1091 | const char *Process::GetExitDescription() { |
| 1092 | std::lock_guard<std::mutex> guard(m_exit_status_mutex); |
| 1093 | |
| 1094 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 1095 | return m_exit_string.c_str(); |
| 1096 | return nullptr; |
| 1097 | } |
| 1098 | |
| 1099 | bool Process::SetExitStatus(int status, const char *cstr) { |
| 1100 | // Use a mutex to protect setting the exit status. |
| 1101 | std::lock_guard<std::mutex> guard(m_exit_status_mutex); |
| 1102 | |
| 1103 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1104 | LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1105 | LLDB_LOGF( |
| 1106 | log, "Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)", |
| 1107 | status, status, cstr ? "\"" : "", cstr ? cstr : "NULL", cstr ? "\"" : ""); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1108 | |
| 1109 | // We were already in the exited state |
| 1110 | if (m_private_state.GetValue() == eStateExited) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1111 | LLDB_LOGF(log, "Process::SetExitStatus () ignoring exit status because " |
| 1112 | "state was already set to eStateExited"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1113 | return false; |
| 1114 | } |
| 1115 | |
| 1116 | m_exit_status = status; |
| 1117 | if (cstr) |
| 1118 | m_exit_string = cstr; |
| 1119 | else |
| 1120 | m_exit_string.clear(); |
| 1121 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1122 | // Clear the last natural stop ID since it has a strong reference to this |
| 1123 | // process |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1124 | m_mod_id.SetStopEventForLastNaturalStopID(EventSP()); |
| 1125 | |
| 1126 | SetPrivateState(eStateExited); |
| 1127 | |
| 1128 | // Allow subclasses to do some cleanup |
| 1129 | DidExit(); |
| 1130 | |
| 1131 | return true; |
| 1132 | } |
| 1133 | |
| 1134 | bool Process::IsAlive() { |
| 1135 | switch (m_private_state.GetValue()) { |
| 1136 | case eStateConnected: |
| 1137 | case eStateAttaching: |
| 1138 | case eStateLaunching: |
| 1139 | case eStateStopped: |
| 1140 | case eStateRunning: |
| 1141 | case eStateStepping: |
| 1142 | case eStateCrashed: |
| 1143 | case eStateSuspended: |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1144 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1145 | default: |
| 1146 | return false; |
| 1147 | } |
Jason Molenda | a814f70 | 2015-11-05 23:03:44 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1150 | // This static callback can be used to watch for local child processes on the |
| 1151 | // current host. The child process exits, the process will be found in the |
| 1152 | // global target list (we want to be completely sure that the |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1153 | // 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] | 1154 | bool Process::SetProcessExitStatus( |
| 1155 | lldb::pid_t pid, bool exited, |
| 1156 | int signo, // Zero for no signal |
| 1157 | int exit_status // Exit value of process if signal is zero |
| 1158 | ) { |
| 1159 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1160 | LLDB_LOGF(log, |
| 1161 | "Process::SetProcessExitStatus (pid=%" PRIu64 |
| 1162 | ", exited=%i, signal=%i, exit_status=%i)\n", |
| 1163 | pid, exited, signo, exit_status); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1164 | |
| 1165 | if (exited) { |
| 1166 | TargetSP target_sp(Debugger::FindTargetWithProcessID(pid)); |
| 1167 | if (target_sp) { |
| 1168 | ProcessSP process_sp(target_sp->GetProcessSP()); |
| 1169 | if (process_sp) { |
| 1170 | const char *signal_cstr = nullptr; |
| 1171 | if (signo) |
| 1172 | signal_cstr = process_sp->GetUnixSignals()->GetSignalAsCString(signo); |
| 1173 | |
| 1174 | process_sp->SetExitStatus(exit_status, signal_cstr); |
| 1175 | } |
| 1176 | } |
| 1177 | return true; |
| 1178 | } |
| 1179 | return false; |
| 1180 | } |
| 1181 | |
| 1182 | void Process::UpdateThreadListIfNeeded() { |
| 1183 | const uint32_t stop_id = GetStopID(); |
| 1184 | if (m_thread_list.GetSize(false) == 0 || |
| 1185 | stop_id != m_thread_list.GetStopID()) { |
| 1186 | const StateType state = GetPrivateState(); |
| 1187 | if (StateIsStoppedState(state, true)) { |
| 1188 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1189 | // m_thread_list does have its own mutex, but we need to hold onto the |
| 1190 | // mutex between the call to UpdateThreadList(...) and the |
| 1191 | // os->UpdateThreadList(...) so it doesn't change on us |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1192 | ThreadList &old_thread_list = m_thread_list; |
| 1193 | ThreadList real_thread_list(this); |
| 1194 | ThreadList new_thread_list(this); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1195 | // Always update the thread list with the protocol specific thread list, |
| 1196 | // but only update if "true" is returned |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1197 | if (UpdateThreadList(m_thread_list_real, real_thread_list)) { |
| 1198 | // 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] | 1199 | // are shutting down, since that may call back into the SBAPI's, |
| 1200 | // requiring the API lock which is already held by whoever is shutting |
| 1201 | // us down, causing a deadlock. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1202 | OperatingSystem *os = GetOperatingSystem(); |
| 1203 | if (os && !m_destroy_in_process) { |
| 1204 | // Clear any old backing threads where memory threads might have been |
| 1205 | // backed by actual threads from the lldb_private::Process subclass |
| 1206 | size_t num_old_threads = old_thread_list.GetSize(false); |
| 1207 | for (size_t i = 0; i < num_old_threads; ++i) |
| 1208 | old_thread_list.GetThreadAtIndex(i, false)->ClearBackingThread(); |
| 1209 | |
| 1210 | // Turn off dynamic types to ensure we don't run any expressions. |
Adrian Prantl | 4e8be2c | 2018-06-13 16:21:24 +0000 | [diff] [blame] | 1211 | // 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] | 1212 | // dynamic type or not and we need to avoid this. OperatingSystem |
| 1213 | // plug-ins can't run expressions that require running code... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1214 | |
| 1215 | Target &target = GetTarget(); |
| 1216 | const lldb::DynamicValueType saved_prefer_dynamic = |
| 1217 | target.GetPreferDynamicValue(); |
| 1218 | if (saved_prefer_dynamic != lldb::eNoDynamicValues) |
| 1219 | target.SetPreferDynamicValue(lldb::eNoDynamicValues); |
| 1220 | |
| 1221 | // Now let the OperatingSystem plug-in update the thread list |
| 1222 | |
| 1223 | os->UpdateThreadList( |
| 1224 | old_thread_list, // Old list full of threads created by OS plug-in |
| 1225 | real_thread_list, // The actual thread list full of threads |
| 1226 | // created by each lldb_private::Process |
| 1227 | // subclass |
| 1228 | new_thread_list); // The new thread list that we will show to the |
| 1229 | // user that gets filled in |
| 1230 | |
| 1231 | if (saved_prefer_dynamic != lldb::eNoDynamicValues) |
| 1232 | target.SetPreferDynamicValue(saved_prefer_dynamic); |
| 1233 | } else { |
| 1234 | // No OS plug-in, the new thread list is the same as the real thread |
| 1235 | // list |
| 1236 | new_thread_list = real_thread_list; |
| 1237 | } |
| 1238 | |
| 1239 | m_thread_list_real.Update(real_thread_list); |
| 1240 | m_thread_list.Update(new_thread_list); |
| 1241 | m_thread_list.SetStopID(stop_id); |
| 1242 | |
| 1243 | if (GetLastNaturalStopID() != m_extended_thread_stop_id) { |
| 1244 | // Clear any extended threads that we may have accumulated previously |
| 1245 | m_extended_thread_list.Clear(); |
| 1246 | m_extended_thread_stop_id = GetLastNaturalStopID(); |
| 1247 | |
| 1248 | m_queue_list.Clear(); |
| 1249 | m_queue_list_stop_id = GetLastNaturalStopID(); |
| 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | void Process::UpdateQueueListIfNeeded() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1257 | if (m_system_runtime_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1258 | if (m_queue_list.GetSize() == 0 || |
| 1259 | m_queue_list_stop_id != GetLastNaturalStopID()) { |
| 1260 | const StateType state = GetPrivateState(); |
| 1261 | if (StateIsStoppedState(state, true)) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1262 | m_system_runtime_up->PopulateQueueList(m_queue_list); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1263 | m_queue_list_stop_id = GetLastNaturalStopID(); |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | ThreadSP Process::CreateOSPluginThread(lldb::tid_t tid, lldb::addr_t context) { |
| 1270 | OperatingSystem *os = GetOperatingSystem(); |
| 1271 | if (os) |
| 1272 | return os->CreateThread(tid, context); |
| 1273 | return ThreadSP(); |
| 1274 | } |
| 1275 | |
| 1276 | uint32_t Process::GetNextThreadIndexID(uint64_t thread_id) { |
| 1277 | return AssignIndexIDToThread(thread_id); |
| 1278 | } |
| 1279 | |
| 1280 | bool Process::HasAssignedIndexIDToThread(uint64_t thread_id) { |
| 1281 | return (m_thread_id_to_index_id_map.find(thread_id) != |
| 1282 | m_thread_id_to_index_id_map.end()); |
| 1283 | } |
| 1284 | |
| 1285 | uint32_t Process::AssignIndexIDToThread(uint64_t thread_id) { |
| 1286 | uint32_t result = 0; |
| 1287 | std::map<uint64_t, uint32_t>::iterator iterator = |
| 1288 | m_thread_id_to_index_id_map.find(thread_id); |
| 1289 | if (iterator == m_thread_id_to_index_id_map.end()) { |
| 1290 | result = ++m_thread_index_id; |
| 1291 | m_thread_id_to_index_id_map[thread_id] = result; |
| 1292 | } else { |
| 1293 | result = iterator->second; |
| 1294 | } |
| 1295 | |
| 1296 | return result; |
| 1297 | } |
| 1298 | |
| 1299 | StateType Process::GetState() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1300 | return m_public_state.GetValue(); |
| 1301 | } |
| 1302 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1303 | void Process::SetPublicState(StateType new_state, bool restarted) { |
| 1304 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1305 | LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1306 | LLDB_LOGF(log, "Process::SetPublicState (state = %s, restarted = %i)", |
| 1307 | StateAsCString(new_state), restarted); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1308 | const StateType old_state = m_public_state.GetValue(); |
| 1309 | m_public_state.SetValue(new_state); |
| 1310 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1311 | // On the transition from Run to Stopped, we unlock the writer end of the run |
| 1312 | // lock. The lock gets locked in Resume, which is the public API to tell the |
| 1313 | // program to run. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1314 | if (!StateChangedIsExternallyHijacked()) { |
| 1315 | if (new_state == eStateDetached) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1316 | LLDB_LOGF(log, |
| 1317 | "Process::SetPublicState (%s) -- unlocking run lock for detach", |
| 1318 | StateAsCString(new_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1319 | m_public_run_lock.SetStopped(); |
| 1320 | } else { |
| 1321 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1322 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1323 | if ((old_state_is_stopped != new_state_is_stopped)) { |
| 1324 | if (new_state_is_stopped && !restarted) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1325 | LLDB_LOGF(log, "Process::SetPublicState (%s) -- unlocking run lock", |
| 1326 | StateAsCString(new_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1327 | m_public_run_lock.SetStopped(); |
| 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1334 | Status Process::Resume() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1335 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1336 | LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1337 | LLDB_LOGF(log, "Process::Resume -- locking run lock"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1338 | if (!m_public_run_lock.TrySetRunning()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1339 | Status error("Resume request failed - process still running."); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1340 | LLDB_LOGF(log, "Process::Resume: -- TrySetRunning failed, not resuming."); |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1341 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1342 | } |
Adrian McCarthy | 3887ba8 | 2017-09-19 18:07:33 +0000 | [diff] [blame] | 1343 | Status error = PrivateResume(); |
| 1344 | if (!error.Success()) { |
| 1345 | // Undo running state change |
| 1346 | m_public_run_lock.SetStopped(); |
| 1347 | } |
| 1348 | return error; |
Greg Clayton | dc6224e | 2014-10-21 01:00:42 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Jim Ingham | 3139fc9 | 2019-03-01 18:13:38 +0000 | [diff] [blame] | 1351 | static const char *g_resume_sync_name = "lldb.Process.ResumeSynchronous.hijack"; |
| 1352 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1353 | Status Process::ResumeSynchronous(Stream *stream) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1354 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1355 | LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1356 | LLDB_LOGF(log, "Process::ResumeSynchronous -- locking run lock"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1357 | if (!m_public_run_lock.TrySetRunning()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1358 | Status error("Resume request failed - process still running."); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1359 | LLDB_LOGF(log, "Process::Resume: -- TrySetRunning failed, not resuming."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1360 | return error; |
| 1361 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1362 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1363 | ListenerSP listener_sp( |
Jim Ingham | 3139fc9 | 2019-03-01 18:13:38 +0000 | [diff] [blame] | 1364 | Listener::MakeListener(g_resume_sync_name)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1365 | HijackProcessEvents(listener_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1366 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1367 | Status error = PrivateResume(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1368 | if (error.Success()) { |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 1369 | StateType state = |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 1370 | WaitForProcessToStop(llvm::None, nullptr, true, listener_sp, stream); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1371 | const bool must_be_alive = |
| 1372 | false; // eStateExited is ok, so this must be false |
| 1373 | if (!StateIsStoppedState(state, must_be_alive)) |
| 1374 | error.SetErrorStringWithFormat( |
| 1375 | "process not in stopped state after synchronous resume: %s", |
| 1376 | StateAsCString(state)); |
Adrian McCarthy | 3887ba8 | 2017-09-19 18:07:33 +0000 | [diff] [blame] | 1377 | } else { |
| 1378 | // Undo running state change |
| 1379 | m_public_run_lock.SetStopped(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1380 | } |
Ed Maste | c29693f | 2013-07-02 16:35:47 +0000 | [diff] [blame] | 1381 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1382 | // Undo the hijacking of process events... |
| 1383 | RestoreProcessEvents(); |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 1384 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1385 | return error; |
| 1386 | } |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 1387 | |
Jim Ingham | 3139fc9 | 2019-03-01 18:13:38 +0000 | [diff] [blame] | 1388 | bool Process::StateChangedIsExternallyHijacked() { |
| 1389 | if (IsHijackedForEvent(eBroadcastBitStateChanged)) { |
| 1390 | const char *hijacking_name = GetHijackingListenerName(); |
| 1391 | if (hijacking_name && |
| 1392 | strcmp(hijacking_name, g_resume_sync_name)) |
| 1393 | return true; |
| 1394 | } |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
| 1398 | bool Process::StateChangedIsHijackedForSynchronousResume() { |
| 1399 | if (IsHijackedForEvent(eBroadcastBitStateChanged)) { |
| 1400 | const char *hijacking_name = GetHijackingListenerName(); |
| 1401 | if (hijacking_name && |
| 1402 | strcmp(hijacking_name, g_resume_sync_name) == 0) |
| 1403 | return true; |
| 1404 | } |
| 1405 | return false; |
| 1406 | } |
| 1407 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1408 | StateType Process::GetPrivateState() { return m_private_state.GetValue(); } |
Ilia K | 38810f4 | 2015-05-20 10:15:47 +0000 | [diff] [blame] | 1409 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1410 | void Process::SetPrivateState(StateType new_state) { |
| 1411 | if (m_finalize_called) |
| 1412 | return; |
| 1413 | |
| 1414 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STATE | |
| 1415 | LIBLLDB_LOG_PROCESS)); |
| 1416 | bool state_changed = false; |
| 1417 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1418 | LLDB_LOGF(log, "Process::SetPrivateState (%s)", StateAsCString(new_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1419 | |
| 1420 | std::lock_guard<std::recursive_mutex> thread_guard(m_thread_list.GetMutex()); |
| 1421 | std::lock_guard<std::recursive_mutex> guard(m_private_state.GetMutex()); |
| 1422 | |
| 1423 | const StateType old_state = m_private_state.GetValueNoLock(); |
| 1424 | state_changed = old_state != new_state; |
| 1425 | |
| 1426 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1427 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1428 | if (old_state_is_stopped != new_state_is_stopped) { |
| 1429 | if (new_state_is_stopped) |
| 1430 | m_private_run_lock.SetStopped(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1431 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1432 | m_private_run_lock.SetRunning(); |
| 1433 | } |
| 1434 | |
| 1435 | if (state_changed) { |
| 1436 | m_private_state.SetValueNoLock(new_state); |
| 1437 | EventSP event_sp( |
| 1438 | new Event(eBroadcastBitStateChanged, |
| 1439 | new ProcessEventData(shared_from_this(), new_state))); |
| 1440 | if (StateIsStoppedState(new_state, false)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1441 | // Note, this currently assumes that all threads in the list stop when |
| 1442 | // the process stops. In the future we will want to support a debugging |
| 1443 | // model where some threads continue to run while others are stopped. |
| 1444 | // When that happens we will either need a way for the thread list to |
| 1445 | // identify which threads are stopping or create a special thread list |
| 1446 | // containing only threads which actually stopped. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1447 | // |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1448 | // The process plugin is responsible for managing the actual behavior of |
| 1449 | // the threads and should have stopped any threads that are going to stop |
| 1450 | // before we get here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1451 | m_thread_list.DidStop(); |
| 1452 | |
| 1453 | m_mod_id.BumpStopID(); |
| 1454 | if (!m_mod_id.IsLastResumeForUserExpression()) |
| 1455 | m_mod_id.SetStopEventForLastNaturalStopID(event_sp); |
| 1456 | m_memory_cache.Clear(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1457 | LLDB_LOGF(log, "Process::SetPrivateState (%s) stop_id = %u", |
| 1458 | StateAsCString(new_state), m_mod_id.GetStopID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1459 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1460 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1461 | // Use our target to get a shared pointer to ourselves... |
| 1462 | if (m_finalize_called && !PrivateStateThreadIsValid()) |
| 1463 | BroadcastEvent(event_sp); |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1464 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1465 | m_private_state_broadcaster.BroadcastEvent(event_sp); |
| 1466 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1467 | LLDB_LOGF(log, |
| 1468 | "Process::SetPrivateState (%s) state didn't change. Ignoring...", |
| 1469 | StateAsCString(new_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1470 | } |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1473 | void Process::SetRunningUserExpression(bool on) { |
| 1474 | m_mod_id.SetRunningUserExpression(on); |
| 1475 | } |
| 1476 | |
Raphael Isemann | c01783a | 2018-08-29 22:50:54 +0000 | [diff] [blame] | 1477 | void Process::SetRunningUtilityFunction(bool on) { |
| 1478 | m_mod_id.SetRunningUtilityFunction(on); |
| 1479 | } |
| 1480 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1481 | addr_t Process::GetImageInfoAddress() { return LLDB_INVALID_ADDRESS; } |
| 1482 | |
| 1483 | const lldb::ABISP &Process::GetABI() { |
| 1484 | if (!m_abi_sp) |
Jason Molenda | 43294c9 | 2017-06-29 02:57:03 +0000 | [diff] [blame] | 1485 | m_abi_sp = ABI::FindPlugin(shared_from_this(), GetTarget().GetArchitecture()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1486 | return m_abi_sp; |
| 1487 | } |
| 1488 | |
Alex Langford | 03e1a82 | 2019-05-29 18:08:22 +0000 | [diff] [blame] | 1489 | std::vector<LanguageRuntime *> |
| 1490 | Process::GetLanguageRuntimes(bool retry_if_null) { |
| 1491 | std::vector<LanguageRuntime *> language_runtimes; |
| 1492 | |
| 1493 | if (m_finalizing) |
| 1494 | return language_runtimes; |
| 1495 | |
| 1496 | std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex); |
| 1497 | // Before we pass off a copy of the language runtimes, we must make sure that |
| 1498 | // our collection is properly populated. It's possible that some of the |
| 1499 | // language runtimes were not loaded yet, either because nobody requested it |
| 1500 | // yet or the proper condition for loading wasn't yet met (e.g. libc++.so |
| 1501 | // hadn't been loaded). |
| 1502 | for (const lldb::LanguageType lang_type : Language::GetSupportedLanguages()) { |
| 1503 | if (LanguageRuntime *runtime = GetLanguageRuntime(lang_type, retry_if_null)) |
| 1504 | language_runtimes.emplace_back(runtime); |
| 1505 | } |
| 1506 | |
| 1507 | return language_runtimes; |
| 1508 | } |
| 1509 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1510 | LanguageRuntime *Process::GetLanguageRuntime(lldb::LanguageType language, |
| 1511 | bool retry_if_null) { |
| 1512 | if (m_finalizing) |
Eugene Zelenko | da8cf8a | 2016-03-01 00:55:51 +0000 | [diff] [blame] | 1513 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1514 | |
Alex Langford | 7d3e97f | 2019-05-29 21:07:53 +0000 | [diff] [blame] | 1515 | LanguageRuntime *runtime = nullptr; |
| 1516 | |
Alex Langford | 74eb76f | 2019-05-22 23:01:18 +0000 | [diff] [blame] | 1517 | std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1518 | LanguageRuntimeCollection::iterator pos; |
| 1519 | pos = m_language_runtimes.find(language); |
Alex Langford | 7d3e97f | 2019-05-29 21:07:53 +0000 | [diff] [blame] | 1520 | if (pos == m_language_runtimes.end() || (retry_if_null && !pos->second)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1521 | lldb::LanguageRuntimeSP runtime_sp( |
| 1522 | LanguageRuntime::FindPlugin(this, language)); |
| 1523 | |
| 1524 | m_language_runtimes[language] = runtime_sp; |
Alex Langford | 7d3e97f | 2019-05-29 21:07:53 +0000 | [diff] [blame] | 1525 | runtime = runtime_sp.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1526 | } else |
Alex Langford | 7d3e97f | 2019-05-29 21:07:53 +0000 | [diff] [blame] | 1527 | runtime = pos->second.get(); |
| 1528 | |
| 1529 | if (runtime) |
| 1530 | // It's possible that a language runtime can support multiple LanguageTypes, |
| 1531 | // for example, CPPLanguageRuntime will support eLanguageTypeC_plus_plus, |
| 1532 | // eLanguageTypeC_plus_plus_03, etc. Because of this, we should get the |
| 1533 | // primary language type and make sure that our runtime supports it. |
| 1534 | assert(runtime->GetLanguageType() == Language::GetPrimaryLanguage(language)); |
| 1535 | |
| 1536 | return runtime; |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1539 | bool Process::IsPossibleDynamicValue(ValueObject &in_value) { |
| 1540 | if (m_finalizing) |
| 1541 | return false; |
| 1542 | |
| 1543 | if (in_value.IsDynamic()) |
| 1544 | return false; |
| 1545 | LanguageType known_type = in_value.GetObjectRuntimeLanguage(); |
| 1546 | |
| 1547 | if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC) { |
| 1548 | LanguageRuntime *runtime = GetLanguageRuntime(known_type); |
| 1549 | return runtime ? runtime->CouldHaveDynamicValue(in_value) : false; |
| 1550 | } |
| 1551 | |
Alex Langford | 41dc552 | 2019-05-30 21:03:53 +0000 | [diff] [blame] | 1552 | for (LanguageRuntime *runtime : GetLanguageRuntimes()) { |
| 1553 | if (runtime->CouldHaveDynamicValue(in_value)) |
| 1554 | return true; |
| 1555 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1556 | |
Alex Langford | 41dc552 | 2019-05-30 21:03:53 +0000 | [diff] [blame] | 1557 | return false; |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1560 | void Process::SetDynamicCheckers(DynamicCheckerFunctions *dynamic_checkers) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1561 | m_dynamic_checkers_up.reset(dynamic_checkers); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1564 | BreakpointSiteList &Process::GetBreakpointSiteList() { |
| 1565 | return m_breakpoint_site_list; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1568 | const BreakpointSiteList &Process::GetBreakpointSiteList() const { |
| 1569 | return m_breakpoint_site_list; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1572 | void Process::DisableAllBreakpointSites() { |
| 1573 | m_breakpoint_site_list.ForEach([this](BreakpointSite *bp_site) -> void { |
| 1574 | // bp_site->SetEnabled(true); |
| 1575 | DisableBreakpointSite(bp_site); |
| 1576 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1579 | Status Process::ClearBreakpointSiteByID(lldb::user_id_t break_id) { |
| 1580 | Status error(DisableBreakpointSiteByID(break_id)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1581 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1582 | if (error.Success()) |
| 1583 | m_breakpoint_site_list.Remove(break_id); |
| 1584 | |
| 1585 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1588 | Status Process::DisableBreakpointSiteByID(lldb::user_id_t break_id) { |
| 1589 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1590 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID(break_id); |
| 1591 | if (bp_site_sp) { |
| 1592 | if (bp_site_sp->IsEnabled()) |
| 1593 | error = DisableBreakpointSite(bp_site_sp.get()); |
| 1594 | } else { |
| 1595 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, |
| 1596 | break_id); |
| 1597 | } |
| 1598 | |
| 1599 | return error; |
| 1600 | } |
| 1601 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1602 | Status Process::EnableBreakpointSiteByID(lldb::user_id_t break_id) { |
| 1603 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1604 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID(break_id); |
| 1605 | if (bp_site_sp) { |
| 1606 | if (!bp_site_sp->IsEnabled()) |
| 1607 | error = EnableBreakpointSite(bp_site_sp.get()); |
| 1608 | } else { |
| 1609 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %" PRIu64, |
| 1610 | break_id); |
| 1611 | } |
| 1612 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Stephen Wilson | 50bd94f | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 1615 | lldb::break_id_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1616 | Process::CreateBreakpointSite(const BreakpointLocationSP &owner, |
| 1617 | bool use_hardware) { |
| 1618 | addr_t load_addr = LLDB_INVALID_ADDRESS; |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1619 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1620 | bool show_error = true; |
| 1621 | switch (GetState()) { |
| 1622 | case eStateInvalid: |
| 1623 | case eStateUnloaded: |
| 1624 | case eStateConnected: |
| 1625 | case eStateAttaching: |
| 1626 | case eStateLaunching: |
| 1627 | case eStateDetached: |
| 1628 | case eStateExited: |
| 1629 | show_error = false; |
| 1630 | break; |
| 1631 | |
| 1632 | case eStateStopped: |
| 1633 | case eStateRunning: |
| 1634 | case eStateStepping: |
| 1635 | case eStateCrashed: |
| 1636 | case eStateSuspended: |
| 1637 | show_error = IsAlive(); |
| 1638 | break; |
| 1639 | } |
| 1640 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1641 | // Reset the IsIndirect flag here, in case the location changes from pointing |
| 1642 | // to a indirect symbol to a regular symbol. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1643 | owner->SetIsIndirect(false); |
| 1644 | |
| 1645 | if (owner->ShouldResolveIndirectFunctions()) { |
| 1646 | Symbol *symbol = owner->GetAddress().CalculateSymbolContextSymbol(); |
| 1647 | if (symbol && symbol->IsIndirect()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1648 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1649 | Address symbol_address = symbol->GetAddress(); |
| 1650 | load_addr = ResolveIndirectFunction(&symbol_address, error); |
| 1651 | if (!error.Success() && show_error) { |
| 1652 | GetTarget().GetDebugger().GetErrorFile()->Printf( |
| 1653 | "warning: failed to resolve indirect function at 0x%" PRIx64 |
| 1654 | " for breakpoint %i.%i: %s\n", |
| 1655 | symbol->GetLoadAddress(&GetTarget()), |
| 1656 | owner->GetBreakpoint().GetID(), owner->GetID(), |
| 1657 | error.AsCString() ? error.AsCString() : "unknown error"); |
| 1658 | return LLDB_INVALID_BREAK_ID; |
| 1659 | } |
| 1660 | Address resolved_address(load_addr); |
| 1661 | load_addr = resolved_address.GetOpcodeLoadAddress(&GetTarget()); |
| 1662 | owner->SetIsIndirect(true); |
| 1663 | } else |
| 1664 | load_addr = owner->GetAddress().GetOpcodeLoadAddress(&GetTarget()); |
| 1665 | } else |
| 1666 | load_addr = owner->GetAddress().GetOpcodeLoadAddress(&GetTarget()); |
| 1667 | |
| 1668 | if (load_addr != LLDB_INVALID_ADDRESS) { |
| 1669 | BreakpointSiteSP bp_site_sp; |
| 1670 | |
| 1671 | // 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] | 1672 | // otherwise create a new breakpoint site and add it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1673 | |
| 1674 | bp_site_sp = m_breakpoint_site_list.FindByAddress(load_addr); |
| 1675 | |
| 1676 | if (bp_site_sp) { |
| 1677 | bp_site_sp->AddOwner(owner); |
| 1678 | owner->SetBreakpointSite(bp_site_sp); |
| 1679 | return bp_site_sp->GetID(); |
| 1680 | } else { |
| 1681 | bp_site_sp.reset(new BreakpointSite(&m_breakpoint_site_list, owner, |
| 1682 | load_addr, use_hardware)); |
| 1683 | if (bp_site_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1684 | Status error = EnableBreakpointSite(bp_site_sp.get()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1685 | if (error.Success()) { |
| 1686 | owner->SetBreakpointSite(bp_site_sp); |
| 1687 | return m_breakpoint_site_list.Add(bp_site_sp); |
| 1688 | } else { |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 +0000 | [diff] [blame] | 1689 | if (show_error || use_hardware) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1690 | // Report error for setting breakpoint... |
| 1691 | GetTarget().GetDebugger().GetErrorFile()->Printf( |
| 1692 | "warning: failed to set breakpoint site at 0x%" PRIx64 |
| 1693 | " for breakpoint %i.%i: %s\n", |
| 1694 | load_addr, owner->GetBreakpoint().GetID(), owner->GetID(), |
| 1695 | error.AsCString() ? error.AsCString() : "unknown error"); |
| 1696 | } |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1697 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1698 | } |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 1699 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1700 | } |
| 1701 | // We failed to enable the breakpoint |
| 1702 | return LLDB_INVALID_BREAK_ID; |
| 1703 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1704 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1705 | void Process::RemoveOwnerFromBreakpointSite(lldb::user_id_t owner_id, |
| 1706 | lldb::user_id_t owner_loc_id, |
| 1707 | BreakpointSiteSP &bp_site_sp) { |
| 1708 | uint32_t num_owners = bp_site_sp->RemoveOwner(owner_id, owner_loc_id); |
| 1709 | if (num_owners == 0) { |
| 1710 | // Don't try to disable the site if we don't have a live process anymore. |
| 1711 | if (IsAlive()) |
| 1712 | DisableBreakpointSite(bp_site_sp.get()); |
| 1713 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 1714 | } |
| 1715 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1716 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1717 | size_t Process::RemoveBreakpointOpcodesFromBuffer(addr_t bp_addr, size_t size, |
| 1718 | uint8_t *buf) const { |
| 1719 | size_t bytes_removed = 0; |
| 1720 | BreakpointSiteList bp_sites_in_range; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1721 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1722 | if (m_breakpoint_site_list.FindInRange(bp_addr, bp_addr + size, |
| 1723 | bp_sites_in_range)) { |
Zachary Turner | 3bc714b | 2017-03-02 00:05:25 +0000 | [diff] [blame] | 1724 | bp_sites_in_range.ForEach([bp_addr, size, |
| 1725 | buf](BreakpointSite *bp_site) -> void { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1726 | if (bp_site->GetType() == BreakpointSite::eSoftware) { |
| 1727 | addr_t intersect_addr; |
| 1728 | size_t intersect_size; |
| 1729 | size_t opcode_offset; |
| 1730 | if (bp_site->IntersectsRange(bp_addr, size, &intersect_addr, |
| 1731 | &intersect_size, &opcode_offset)) { |
| 1732 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 1733 | assert(bp_addr < intersect_addr + intersect_size && |
| 1734 | intersect_addr + intersect_size <= bp_addr + size); |
| 1735 | assert(opcode_offset + intersect_size <= bp_site->GetByteSize()); |
| 1736 | size_t buf_offset = intersect_addr - bp_addr; |
| 1737 | ::memcpy(buf + buf_offset, |
| 1738 | bp_site->GetSavedOpcodeBytes() + opcode_offset, |
| 1739 | intersect_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1740 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1741 | } |
| 1742 | }); |
| 1743 | } |
| 1744 | return bytes_removed; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1747 | size_t Process::GetSoftwareBreakpointTrapOpcode(BreakpointSite *bp_site) { |
| 1748 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 1749 | if (platform_sp) |
| 1750 | return platform_sp->GetSoftwareBreakpointTrapOpcode(GetTarget(), bp_site); |
| 1751 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1754 | Status Process::EnableSoftwareBreakpoint(BreakpointSite *bp_site) { |
| 1755 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1756 | assert(bp_site != nullptr); |
| 1757 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
| 1758 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1759 | LLDB_LOGF( |
| 1760 | log, "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64, |
| 1761 | bp_site->GetID(), (uint64_t)bp_addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1762 | if (bp_site->IsEnabled()) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1763 | LLDB_LOGF( |
| 1764 | log, |
| 1765 | "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 1766 | " -- already enabled", |
| 1767 | bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1768 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | if (bp_addr == LLDB_INVALID_ADDRESS) { |
| 1772 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 1773 | return error; |
| 1774 | } |
| 1775 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 1776 | // trap for the breakpoint site |
| 1777 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 1778 | |
| 1779 | if (bp_opcode_size == 0) { |
| 1780 | error.SetErrorStringWithFormat("Process::GetSoftwareBreakpointTrapOpcode() " |
| 1781 | "returned zero, unable to get breakpoint " |
| 1782 | "trap for address 0x%" PRIx64, |
| 1783 | bp_addr); |
| 1784 | } else { |
| 1785 | const uint8_t *const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 1786 | |
| 1787 | if (bp_opcode_bytes == nullptr) { |
| 1788 | error.SetErrorString( |
| 1789 | "BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 1790 | return error; |
| 1791 | } |
| 1792 | |
| 1793 | // Save the original opcode by reading it |
| 1794 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, |
| 1795 | error) == bp_opcode_size) { |
| 1796 | // Write a software breakpoint in place of the original opcode |
| 1797 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == |
| 1798 | bp_opcode_size) { |
| 1799 | uint8_t verify_bp_opcode_bytes[64]; |
| 1800 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, |
| 1801 | error) == bp_opcode_size) { |
| 1802 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, |
| 1803 | bp_opcode_size) == 0) { |
| 1804 | bp_site->SetEnabled(true); |
| 1805 | bp_site->SetType(BreakpointSite::eSoftware); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1806 | LLDB_LOGF(log, |
| 1807 | "Process::EnableSoftwareBreakpoint (site_id = %d) " |
| 1808 | "addr = 0x%" PRIx64 " -- SUCCESS", |
| 1809 | bp_site->GetID(), (uint64_t)bp_addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1810 | } else |
| 1811 | error.SetErrorString( |
| 1812 | "failed to verify the breakpoint trap in memory."); |
| 1813 | } else |
| 1814 | error.SetErrorString( |
| 1815 | "Unable to read memory to verify breakpoint trap."); |
| 1816 | } else |
| 1817 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 1818 | } else |
| 1819 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 1820 | } |
| 1821 | if (log && error.Fail()) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1822 | LLDB_LOGF( |
| 1823 | log, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1824 | "Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 1825 | " -- FAILED: %s", |
| 1826 | bp_site->GetID(), (uint64_t)bp_addr, error.AsCString()); |
| 1827 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1830 | Status Process::DisableSoftwareBreakpoint(BreakpointSite *bp_site) { |
| 1831 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1832 | assert(bp_site != nullptr); |
| 1833 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
| 1834 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 1835 | lldb::user_id_t breakID = bp_site->GetID(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1836 | LLDB_LOGF(log, |
| 1837 | "Process::DisableSoftwareBreakpoint (breakID = %" PRIu64 |
| 1838 | ") addr = 0x%" PRIx64, |
| 1839 | breakID, (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1840 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1841 | if (bp_site->IsHardware()) { |
| 1842 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 1843 | } else if (bp_site->IsEnabled()) { |
| 1844 | const size_t break_op_size = bp_site->GetByteSize(); |
| 1845 | const uint8_t *const break_op = bp_site->GetTrapOpcodeBytes(); |
| 1846 | if (break_op_size > 0) { |
| 1847 | // Clear a software breakpoint instruction |
| 1848 | uint8_t curr_break_op[8]; |
| 1849 | assert(break_op_size <= sizeof(curr_break_op)); |
| 1850 | bool break_op_found = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1851 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1852 | // Read the breakpoint opcode |
| 1853 | if (DoReadMemory(bp_addr, curr_break_op, break_op_size, error) == |
| 1854 | break_op_size) { |
| 1855 | bool verify = false; |
| 1856 | // Make sure the breakpoint opcode exists at this address |
| 1857 | if (::memcmp(curr_break_op, break_op, break_op_size) == 0) { |
| 1858 | break_op_found = true; |
| 1859 | // We found a valid breakpoint opcode at this address, now restore |
| 1860 | // the saved opcode. |
| 1861 | if (DoWriteMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), |
| 1862 | break_op_size, error) == break_op_size) { |
| 1863 | verify = true; |
| 1864 | } else |
| 1865 | error.SetErrorString( |
| 1866 | "Memory write failed when restoring original opcode."); |
| 1867 | } else { |
| 1868 | error.SetErrorString( |
| 1869 | "Original breakpoint trap is no longer in memory."); |
| 1870 | // Set verify to true and so we can check if the original opcode has |
| 1871 | // already been restored |
| 1872 | verify = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1873 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1874 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1875 | if (verify) { |
| 1876 | uint8_t verify_opcode[8]; |
| 1877 | assert(break_op_size < sizeof(verify_opcode)); |
| 1878 | // Verify that our original opcode made it back to the inferior |
| 1879 | if (DoReadMemory(bp_addr, verify_opcode, break_op_size, error) == |
| 1880 | break_op_size) { |
| 1881 | // compare the memory we just read with the original opcode |
| 1882 | if (::memcmp(bp_site->GetSavedOpcodeBytes(), verify_opcode, |
| 1883 | break_op_size) == 0) { |
| 1884 | // SUCCESS |
| 1885 | bp_site->SetEnabled(false); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1886 | LLDB_LOGF(log, |
| 1887 | "Process::DisableSoftwareBreakpoint (site_id = %d) " |
| 1888 | "addr = 0x%" PRIx64 " -- SUCCESS", |
| 1889 | bp_site->GetID(), (uint64_t)bp_addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1890 | return error; |
| 1891 | } else { |
| 1892 | if (break_op_found) |
| 1893 | error.SetErrorString("Failed to restore original opcode."); |
| 1894 | } |
| 1895 | } else |
| 1896 | error.SetErrorString("Failed to read memory to verify that " |
| 1897 | "breakpoint trap was restored."); |
| 1898 | } |
| 1899 | } else |
| 1900 | error.SetErrorString( |
| 1901 | "Unable to read memory that should contain the breakpoint trap."); |
| 1902 | } |
| 1903 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1904 | LLDB_LOGF( |
| 1905 | log, |
| 1906 | "Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 1907 | " -- already disabled", |
| 1908 | bp_site->GetID(), (uint64_t)bp_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1909 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1912 | LLDB_LOGF( |
| 1913 | log, |
| 1914 | "Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%" PRIx64 |
| 1915 | " -- FAILED: %s", |
| 1916 | bp_site->GetID(), (uint64_t)bp_addr, error.AsCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1917 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1920 | // Uncomment to verify memory caching works after making changes to caching |
| 1921 | // code |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1922 | //#define VERIFY_MEMORY_READS |
| 1923 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1924 | 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] | 1925 | error.Clear(); |
| 1926 | if (!GetDisableMemoryCache()) { |
| 1927 | #if defined(VERIFY_MEMORY_READS) |
| 1928 | // Memory caching is enabled, with debug verification |
| 1929 | |
| 1930 | if (buf && size) { |
| 1931 | // Uncomment the line below to make sure memory caching is working. |
| 1932 | // I ran this through the test suite and got no assertions, so I am |
| 1933 | // pretty confident this is working well. If any changes are made to |
| 1934 | // memory caching, uncomment the line below and test your changes! |
| 1935 | |
| 1936 | // Verify all memory reads by using the cache first, then redundantly |
| 1937 | // reading the same memory from the inferior and comparing to make sure |
| 1938 | // everything is exactly the same. |
| 1939 | std::string verify_buf(size, '\0'); |
| 1940 | assert(verify_buf.size() == size); |
| 1941 | const size_t cache_bytes_read = |
| 1942 | m_memory_cache.Read(this, addr, buf, size, error); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1943 | Status verify_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1944 | const size_t verify_bytes_read = |
| 1945 | ReadMemoryFromInferior(addr, const_cast<char *>(verify_buf.data()), |
| 1946 | verify_buf.size(), verify_error); |
| 1947 | assert(cache_bytes_read == verify_bytes_read); |
| 1948 | assert(memcmp(buf, verify_buf.data(), verify_buf.size()) == 0); |
| 1949 | assert(verify_error.Success() == error.Success()); |
| 1950 | return cache_bytes_read; |
| 1951 | } |
| 1952 | return 0; |
| 1953 | #else // !defined(VERIFY_MEMORY_READS) |
| 1954 | // Memory caching is enabled, without debug verification |
| 1955 | |
| 1956 | return m_memory_cache.Read(addr, buf, size, error); |
Sean Callanan | 64c0cf2 | 2012-06-07 22:26:42 +0000 | [diff] [blame] | 1957 | #endif // defined (VERIFY_MEMORY_READS) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1958 | } else { |
| 1959 | // Memory caching is disabled |
| 1960 | |
| 1961 | return ReadMemoryFromInferior(addr, buf, size, error); |
| 1962 | } |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1963 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1964 | |
| 1965 | size_t Process::ReadCStringFromMemory(addr_t addr, std::string &out_str, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1966 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1967 | char buf[256]; |
| 1968 | out_str.clear(); |
| 1969 | addr_t curr_addr = addr; |
| 1970 | while (true) { |
| 1971 | size_t length = ReadCStringFromMemory(curr_addr, buf, sizeof(buf), error); |
| 1972 | if (length == 0) |
| 1973 | break; |
| 1974 | out_str.append(buf, length); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1975 | // If we got "length - 1" bytes, we didn't get the whole C string, we need |
| 1976 | // to read some more characters |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1977 | if (length == sizeof(buf) - 1) |
| 1978 | curr_addr += length; |
| 1979 | else |
| 1980 | break; |
| 1981 | } |
| 1982 | return out_str.size(); |
| 1983 | } |
| 1984 | |
| 1985 | 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] | 1986 | Status &error, size_t type_width) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1987 | size_t total_bytes_read = 0; |
| 1988 | if (dst && max_bytes && type_width && max_bytes >= type_width) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1989 | // Ensure a null terminator independent of the number of bytes that is |
| 1990 | // read. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1991 | memset(dst, 0, max_bytes); |
| 1992 | size_t bytes_left = max_bytes - type_width; |
| 1993 | |
| 1994 | const char terminator[4] = {'\0', '\0', '\0', '\0'}; |
| 1995 | assert(sizeof(terminator) >= type_width && "Attempting to validate a " |
| 1996 | "string with more than 4 bytes " |
| 1997 | "per character!"); |
| 1998 | |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1999 | addr_t curr_addr = addr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2000 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2001 | char *curr_dst = dst; |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2002 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2003 | error.Clear(); |
| 2004 | while (bytes_left > 0 && error.Success()) { |
| 2005 | addr_t cache_line_bytes_left = |
| 2006 | cache_line_size - (curr_addr % cache_line_size); |
| 2007 | addr_t bytes_to_read = |
| 2008 | std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2009 | 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] | 2010 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2011 | if (bytes_read == 0) |
| 2012 | break; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2013 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2014 | // Search for a null terminator of correct size and alignment in |
| 2015 | // bytes_read |
| 2016 | size_t aligned_start = total_bytes_read - total_bytes_read % type_width; |
| 2017 | for (size_t i = aligned_start; |
| 2018 | i + type_width <= total_bytes_read + bytes_read; i += type_width) |
| 2019 | if (::memcmp(&dst[i], terminator, type_width) == 0) { |
| 2020 | error.Clear(); |
| 2021 | return i; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2022 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2023 | |
| 2024 | total_bytes_read += bytes_read; |
| 2025 | curr_dst += bytes_read; |
| 2026 | curr_addr += bytes_read; |
| 2027 | bytes_left -= bytes_read; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2028 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2029 | } else { |
| 2030 | if (max_bytes) |
| 2031 | error.SetErrorString("invalid arguments"); |
| 2032 | } |
| 2033 | return total_bytes_read; |
Ashok Thirumurthi | 6ac9d13 | 2013-04-19 15:58:38 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2036 | // Deprecated in favor of ReadStringFromMemory which has wchar support and |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2037 | // correct code to find null terminators. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2038 | size_t Process::ReadCStringFromMemory(addr_t addr, char *dst, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2039 | size_t dst_max_len, |
| 2040 | Status &result_error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2041 | size_t total_cstr_len = 0; |
| 2042 | if (dst && dst_max_len) { |
| 2043 | result_error.Clear(); |
| 2044 | // NULL out everything just to be safe |
| 2045 | memset(dst, 0, dst_max_len); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2046 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2047 | addr_t curr_addr = addr; |
| 2048 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 2049 | size_t bytes_left = dst_max_len - 1; |
| 2050 | char *curr_dst = dst; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2051 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2052 | while (bytes_left > 0) { |
| 2053 | addr_t cache_line_bytes_left = |
| 2054 | cache_line_size - (curr_addr % cache_line_size); |
| 2055 | addr_t bytes_to_read = |
| 2056 | std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 2057 | 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] | 2058 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2059 | if (bytes_read == 0) { |
| 2060 | result_error = error; |
| 2061 | dst[total_cstr_len] = '\0'; |
| 2062 | break; |
| 2063 | } |
| 2064 | const size_t len = strlen(curr_dst); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2065 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2066 | total_cstr_len += len; |
| 2067 | |
| 2068 | if (len < bytes_to_read) |
| 2069 | break; |
| 2070 | |
| 2071 | curr_dst += bytes_read; |
| 2072 | curr_addr += bytes_read; |
| 2073 | bytes_left -= bytes_read; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2074 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2075 | } else { |
| 2076 | if (dst == nullptr) |
| 2077 | result_error.SetErrorString("invalid arguments"); |
Greg Clayton | e91b795 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 2078 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2079 | result_error.Clear(); |
| 2080 | } |
| 2081 | return total_cstr_len; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2084 | size_t Process::ReadMemoryFromInferior(addr_t addr, void *buf, size_t size, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2085 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2086 | if (buf == nullptr || size == 0) |
| 2087 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2088 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2089 | size_t bytes_read = 0; |
| 2090 | uint8_t *bytes = (uint8_t *)buf; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2091 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2092 | while (bytes_read < size) { |
| 2093 | const size_t curr_size = size - bytes_read; |
| 2094 | const size_t curr_bytes_read = |
| 2095 | DoReadMemory(addr + bytes_read, bytes + bytes_read, curr_size, error); |
| 2096 | bytes_read += curr_bytes_read; |
| 2097 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 2098 | break; |
| 2099 | } |
| 2100 | |
| 2101 | // Replace any software breakpoint opcodes that fall into this range back |
| 2102 | // into "buf" before we return |
| 2103 | if (bytes_read > 0) |
| 2104 | RemoveBreakpointOpcodesFromBuffer(addr, bytes_read, (uint8_t *)buf); |
| 2105 | return bytes_read; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2108 | uint64_t Process::ReadUnsignedIntegerFromMemory(lldb::addr_t vm_addr, |
| 2109 | size_t integer_byte_size, |
| 2110 | uint64_t fail_value, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2111 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2112 | Scalar scalar; |
| 2113 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, |
| 2114 | error)) |
| 2115 | return scalar.ULongLong(fail_value); |
| 2116 | return fail_value; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2119 | int64_t Process::ReadSignedIntegerFromMemory(lldb::addr_t vm_addr, |
| 2120 | size_t integer_byte_size, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2121 | int64_t fail_value, |
| 2122 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2123 | Scalar scalar; |
| 2124 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, true, scalar, |
| 2125 | error)) |
| 2126 | return scalar.SLongLong(fail_value); |
| 2127 | return fail_value; |
Greg Clayton | c226778 | 2016-05-23 20:37:24 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2130 | addr_t Process::ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2131 | Scalar scalar; |
| 2132 | if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, |
| 2133 | error)) |
| 2134 | return scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 2135 | return LLDB_INVALID_ADDRESS; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2138 | 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] | 2139 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2140 | Scalar scalar; |
| 2141 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2142 | if (addr_byte_size <= 4) |
| 2143 | scalar = (uint32_t)ptr_value; |
| 2144 | else |
| 2145 | scalar = ptr_value; |
| 2146 | return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == |
| 2147 | addr_byte_size; |
Greg Clayton | 58a4c46 | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2150 | 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] | 2151 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2152 | size_t bytes_written = 0; |
| 2153 | const uint8_t *bytes = (const uint8_t *)buf; |
| 2154 | |
| 2155 | while (bytes_written < size) { |
| 2156 | const size_t curr_size = size - bytes_written; |
| 2157 | const size_t curr_bytes_written = DoWriteMemory( |
| 2158 | addr + bytes_written, bytes + bytes_written, curr_size, error); |
| 2159 | bytes_written += curr_bytes_written; |
| 2160 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 2161 | break; |
| 2162 | } |
| 2163 | return bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2166 | 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] | 2167 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2168 | #if defined(ENABLE_MEMORY_CACHING) |
| 2169 | m_memory_cache.Flush(addr, size); |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2170 | #endif |
| 2171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2172 | if (buf == nullptr || size == 0) |
| 2173 | return 0; |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2175 | m_mod_id.BumpMemoryID(); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2176 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2177 | // We need to write any data that would go where any current software traps |
| 2178 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 2179 | // may have placed in our tasks memory. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2180 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2181 | BreakpointSiteList bp_sites_in_range; |
Jonas Devlieghere | 533fe61 | 2019-04-01 20:39:03 +0000 | [diff] [blame] | 2182 | 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] | 2183 | return WriteMemoryPrivate(addr, buf, size, error); |
Jonas Devlieghere | 533fe61 | 2019-04-01 20:39:03 +0000 | [diff] [blame] | 2184 | |
| 2185 | // No breakpoint sites overlap |
| 2186 | if (bp_sites_in_range.IsEmpty()) |
| 2187 | return WriteMemoryPrivate(addr, buf, size, error); |
| 2188 | |
| 2189 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 2190 | uint64_t bytes_written = 0; |
| 2191 | |
| 2192 | bp_sites_in_range.ForEach([this, addr, size, &bytes_written, &ubuf, |
| 2193 | &error](BreakpointSite *bp) -> void { |
| 2194 | if (error.Fail()) |
| 2195 | return; |
| 2196 | |
| 2197 | addr_t intersect_addr; |
| 2198 | size_t intersect_size; |
| 2199 | size_t opcode_offset; |
| 2200 | const bool intersects = bp->IntersectsRange( |
| 2201 | addr, size, &intersect_addr, &intersect_size, &opcode_offset); |
| 2202 | UNUSED_IF_ASSERT_DISABLED(intersects); |
| 2203 | assert(intersects); |
| 2204 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 2205 | assert(addr < intersect_addr + intersect_size && |
| 2206 | intersect_addr + intersect_size <= addr + size); |
| 2207 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 2208 | |
| 2209 | // Check for bytes before this breakpoint |
| 2210 | const addr_t curr_addr = addr + bytes_written; |
| 2211 | if (intersect_addr > curr_addr) { |
| 2212 | // There are some bytes before this breakpoint that we need to just |
| 2213 | // write to memory |
| 2214 | size_t curr_size = intersect_addr - curr_addr; |
| 2215 | size_t curr_bytes_written = |
| 2216 | WriteMemoryPrivate(curr_addr, ubuf + bytes_written, curr_size, error); |
| 2217 | bytes_written += curr_bytes_written; |
| 2218 | if (curr_bytes_written != curr_size) { |
| 2219 | // We weren't able to write all of the requested bytes, we are |
| 2220 | // done looping and will return the number of bytes that we have |
| 2221 | // written so far. |
| 2222 | if (error.Success()) |
| 2223 | error.SetErrorToGenericError(); |
| 2224 | } |
| 2225 | } |
| 2226 | // Now write any bytes that would cover up any software breakpoints |
| 2227 | // directly into the breakpoint opcode buffer |
| 2228 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, |
| 2229 | intersect_size); |
| 2230 | bytes_written += intersect_size; |
| 2231 | }); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2232 | |
| 2233 | // 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] | 2234 | if (bytes_written < size) |
| 2235 | bytes_written += |
| 2236 | WriteMemoryPrivate(addr + bytes_written, ubuf + bytes_written, |
| 2237 | size - bytes_written, error); |
| 2238 | |
| 2239 | return bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2240 | } |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2241 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2242 | size_t Process::WriteScalarToMemory(addr_t addr, const Scalar &scalar, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2243 | size_t byte_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2244 | if (byte_size == UINT32_MAX) |
| 2245 | byte_size = scalar.GetByteSize(); |
| 2246 | if (byte_size > 0) { |
| 2247 | uint8_t buf[32]; |
| 2248 | const size_t mem_size = |
| 2249 | scalar.GetAsMemoryData(buf, byte_size, GetByteOrder(), error); |
| 2250 | if (mem_size > 0) |
| 2251 | return WriteMemory(addr, buf, mem_size, error); |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2252 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2253 | error.SetErrorString("failed to get scalar as memory data"); |
| 2254 | } else { |
| 2255 | error.SetErrorString("invalid scalar value"); |
| 2256 | } |
| 2257 | return 0; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2260 | size_t Process::ReadScalarIntegerFromMemory(addr_t addr, uint32_t byte_size, |
| 2261 | bool is_signed, Scalar &scalar, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2262 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2263 | uint64_t uval = 0; |
| 2264 | if (byte_size == 0) { |
| 2265 | error.SetErrorString("byte size is zero"); |
| 2266 | } else if (byte_size & (byte_size - 1)) { |
| 2267 | error.SetErrorStringWithFormat("byte size %u is not a power of 2", |
| 2268 | byte_size); |
| 2269 | } else if (byte_size <= sizeof(uval)) { |
| 2270 | const size_t bytes_read = ReadMemory(addr, &uval, byte_size, error); |
| 2271 | if (bytes_read == byte_size) { |
| 2272 | DataExtractor data(&uval, sizeof(uval), GetByteOrder(), |
| 2273 | GetAddressByteSize()); |
| 2274 | lldb::offset_t offset = 0; |
| 2275 | if (byte_size <= 4) |
| 2276 | scalar = data.GetMaxU32(&offset, byte_size); |
| 2277 | else |
| 2278 | scalar = data.GetMaxU64(&offset, byte_size); |
| 2279 | if (is_signed) |
| 2280 | scalar.SignExtend(byte_size * 8); |
| 2281 | return bytes_read; |
Greg Clayton | 7060f89 | 2013-05-01 23:41:30 +0000 | [diff] [blame] | 2282 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2283 | } else { |
| 2284 | error.SetErrorStringWithFormat( |
| 2285 | "byte size of %u is too large for integer scalar type", byte_size); |
| 2286 | } |
| 2287 | return 0; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 2290 | Status Process::WriteObjectFile(std::vector<ObjectFile::LoadableData> entries) { |
| 2291 | Status error; |
| 2292 | for (const auto &Entry : entries) { |
| 2293 | WriteMemory(Entry.Dest, Entry.Contents.data(), Entry.Contents.size(), |
| 2294 | error); |
| 2295 | if (!error.Success()) |
| 2296 | break; |
| 2297 | } |
| 2298 | return error; |
| 2299 | } |
| 2300 | |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2301 | #define USE_ALLOCATE_MEMORY_CACHE 1 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2302 | addr_t Process::AllocateMemory(size_t size, uint32_t permissions, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2303 | Status &error) { |
Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 2304 | if (GetPrivateState() != eStateStopped) { |
| 2305 | error.SetErrorToGenericError(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2306 | return LLDB_INVALID_ADDRESS; |
Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 2307 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2308 | |
| 2309 | #if defined(USE_ALLOCATE_MEMORY_CACHE) |
| 2310 | return m_allocated_memory_cache.AllocateMemory(size, permissions, error); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2311 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2312 | addr_t allocated_addr = DoAllocateMemory(size, permissions, error); |
| 2313 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2314 | LLDB_LOGF(log, |
| 2315 | "Process::AllocateMemory(size=%" PRIu64 |
| 2316 | ", permissions=%s) => 0x%16.16" PRIx64 |
| 2317 | " (m_stop_id = %u m_memory_id = %u)", |
| 2318 | (uint64_t)size, GetPermissionsAsCString(permissions), |
| 2319 | (uint64_t)allocated_addr, m_mod_id.GetStopID(), |
| 2320 | m_mod_id.GetMemoryID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2321 | return allocated_addr; |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2322 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2325 | addr_t Process::CallocateMemory(size_t size, uint32_t permissions, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2326 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2327 | addr_t return_addr = AllocateMemory(size, permissions, error); |
| 2328 | if (error.Success()) { |
| 2329 | std::string buffer(size, 0); |
| 2330 | WriteMemory(return_addr, buffer.c_str(), size, error); |
| 2331 | } |
| 2332 | return return_addr; |
| 2333 | } |
| 2334 | |
| 2335 | bool Process::CanJIT() { |
| 2336 | if (m_can_jit == eCanJITDontKnow) { |
| 2337 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2338 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2339 | |
| 2340 | uint64_t allocated_memory = AllocateMemory( |
| 2341 | 8, ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable, |
| 2342 | err); |
| 2343 | |
| 2344 | if (err.Success()) { |
| 2345 | m_can_jit = eCanJITYes; |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2346 | LLDB_LOGF(log, |
| 2347 | "Process::%s pid %" PRIu64 |
| 2348 | " allocation test passed, CanJIT () is true", |
| 2349 | __FUNCTION__, GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2350 | } else { |
| 2351 | m_can_jit = eCanJITNo; |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2352 | LLDB_LOGF(log, |
| 2353 | "Process::%s pid %" PRIu64 |
| 2354 | " allocation test failed, CanJIT () is false: %s", |
| 2355 | __FUNCTION__, GetID(), err.AsCString()); |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame] | 2356 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2357 | |
| 2358 | DeallocateMemory(allocated_memory); |
| 2359 | } |
| 2360 | |
| 2361 | return m_can_jit == eCanJITYes; |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2364 | void Process::SetCanJIT(bool can_jit) { |
| 2365 | m_can_jit = (can_jit ? eCanJITYes : eCanJITNo); |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2368 | void Process::SetCanRunCode(bool can_run_code) { |
| 2369 | SetCanJIT(can_run_code); |
| 2370 | m_can_interpret_function_calls = can_run_code; |
Sean Callanan | 9053945 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2373 | Status Process::DeallocateMemory(addr_t ptr) { |
| 2374 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2375 | #if defined(USE_ALLOCATE_MEMORY_CACHE) |
| 2376 | if (!m_allocated_memory_cache.DeallocateMemory(ptr)) { |
| 2377 | error.SetErrorStringWithFormat( |
| 2378 | "deallocation of memory at 0x%" PRIx64 " failed.", (uint64_t)ptr); |
| 2379 | } |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2380 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2381 | error = DoDeallocateMemory(ptr); |
| 2382 | |
| 2383 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2384 | LLDB_LOGF(log, |
| 2385 | "Process::DeallocateMemory(addr=0x%16.16" PRIx64 |
| 2386 | ") => err = %s (m_stop_id = %u, m_memory_id = %u)", |
| 2387 | ptr, error.AsCString("SUCCESS"), m_mod_id.GetStopID(), |
| 2388 | m_mod_id.GetMemoryID()); |
Greg Clayton | d495c53 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2389 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2390 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2393 | ModuleSP Process::ReadModuleFromMemory(const FileSpec &file_spec, |
| 2394 | lldb::addr_t header_addr, |
| 2395 | size_t size_to_read) { |
| 2396 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); |
| 2397 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2398 | LLDB_LOGF(log, |
| 2399 | "Process::ReadModuleFromMemory reading %s binary from memory", |
| 2400 | file_spec.GetPath().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2401 | } |
| 2402 | ModuleSP module_sp(new Module(file_spec, ArchSpec())); |
| 2403 | if (module_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2404 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2405 | ObjectFile *objfile = module_sp->GetMemoryObjectFile( |
| 2406 | shared_from_this(), header_addr, error, size_to_read); |
| 2407 | if (objfile) |
| 2408 | return module_sp; |
| 2409 | } |
| 2410 | return ModuleSP(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2413 | bool Process::GetLoadAddressPermissions(lldb::addr_t load_addr, |
| 2414 | uint32_t &permissions) { |
| 2415 | MemoryRegionInfo range_info; |
| 2416 | permissions = 0; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2417 | Status error(GetMemoryRegionInfo(load_addr, range_info)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2418 | if (!error.Success()) |
| 2419 | return false; |
| 2420 | if (range_info.GetReadable() == MemoryRegionInfo::eDontKnow || |
| 2421 | range_info.GetWritable() == MemoryRegionInfo::eDontKnow || |
| 2422 | range_info.GetExecutable() == MemoryRegionInfo::eDontKnow) { |
| 2423 | return false; |
| 2424 | } |
| 2425 | |
| 2426 | if (range_info.GetReadable() == MemoryRegionInfo::eYes) |
| 2427 | permissions |= lldb::ePermissionsReadable; |
| 2428 | |
| 2429 | if (range_info.GetWritable() == MemoryRegionInfo::eYes) |
| 2430 | permissions |= lldb::ePermissionsWritable; |
| 2431 | |
| 2432 | if (range_info.GetExecutable() == MemoryRegionInfo::eYes) |
| 2433 | permissions |= lldb::ePermissionsExecutable; |
| 2434 | |
| 2435 | return true; |
| 2436 | } |
| 2437 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2438 | Status Process::EnableWatchpoint(Watchpoint *watchpoint, bool notify) { |
| 2439 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2440 | error.SetErrorString("watchpoints are not supported"); |
| 2441 | return error; |
| 2442 | } |
| 2443 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2444 | Status Process::DisableWatchpoint(Watchpoint *watchpoint, bool notify) { |
| 2445 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2446 | error.SetErrorString("watchpoints are not supported"); |
| 2447 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | StateType |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 2451 | Process::WaitForProcessStopPrivate(EventSP &event_sp, |
| 2452 | const Timeout<std::micro> &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2453 | StateType state; |
Stella Stamenova | 36d457c | 2018-06-01 19:14:53 +0000 | [diff] [blame] | 2454 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2455 | while (true) { |
| 2456 | event_sp.reset(); |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 2457 | state = GetStateChangedEventsPrivate(event_sp, timeout); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2458 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2459 | if (StateIsStoppedState(state, false)) |
| 2460 | break; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2461 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2462 | // If state is invalid, then we timed out |
| 2463 | if (state == eStateInvalid) |
| 2464 | break; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2465 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2466 | if (event_sp) |
| 2467 | HandlePrivateEvent(event_sp); |
| 2468 | } |
| 2469 | return state; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2472 | void Process::LoadOperatingSystemPlugin(bool flush) { |
| 2473 | if (flush) |
| 2474 | m_thread_list.Clear(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2475 | m_os_up.reset(OperatingSystem::FindPlugin(this, nullptr)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2476 | if (flush) |
| 2477 | Flush(); |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2480 | Status Process::Launch(ProcessLaunchInfo &launch_info) { |
| 2481 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2482 | m_abi_sp.reset(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2483 | m_dyld_up.reset(); |
| 2484 | m_jit_loaders_up.reset(); |
| 2485 | m_system_runtime_up.reset(); |
| 2486 | m_os_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2487 | m_process_input_reader.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2488 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2489 | Module *exe_module = GetTarget().GetExecutableModulePointer(); |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2490 | if (!exe_module) { |
| 2491 | error.SetErrorString("executable module does not exist"); |
| 2492 | return error; |
| 2493 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2494 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2495 | char local_exec_file_path[PATH_MAX]; |
| 2496 | char platform_exec_file_path[PATH_MAX]; |
| 2497 | exe_module->GetFileSpec().GetPath(local_exec_file_path, |
| 2498 | sizeof(local_exec_file_path)); |
| 2499 | exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, |
| 2500 | sizeof(platform_exec_file_path)); |
| 2501 | if (FileSystem::Instance().Exists(exe_module->GetFileSpec())) { |
| 2502 | // Install anything that might need to be installed prior to launching. |
| 2503 | // For host systems, this will do nothing, but if we are connected to a |
| 2504 | // remote platform it will install any needed binaries |
| 2505 | error = GetTarget().Install(&launch_info); |
| 2506 | if (error.Fail()) |
| 2507 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2508 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2509 | if (PrivateStateThreadIsValid()) |
| 2510 | PausePrivateStateThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2511 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2512 | error = WillLaunch(exe_module); |
| 2513 | if (error.Success()) { |
| 2514 | const bool restarted = false; |
| 2515 | SetPublicState(eStateLaunching, restarted); |
| 2516 | m_should_detach = false; |
| 2517 | |
| 2518 | if (m_public_run_lock.TrySetRunning()) { |
| 2519 | // Now launch using these arguments. |
| 2520 | error = DoLaunch(exe_module, launch_info); |
| 2521 | } else { |
| 2522 | // This shouldn't happen |
| 2523 | error.SetErrorString("failed to acquire process run lock"); |
| 2524 | } |
| 2525 | |
| 2526 | if (error.Fail()) { |
| 2527 | if (GetID() != LLDB_INVALID_PROCESS_ID) { |
| 2528 | SetID(LLDB_INVALID_PROCESS_ID); |
| 2529 | const char *error_string = error.AsCString(); |
| 2530 | if (error_string == nullptr) |
| 2531 | error_string = "launch failed"; |
| 2532 | SetExitStatus(-1, error_string); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2533 | } |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2534 | } else { |
| 2535 | EventSP event_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2536 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2537 | // Now wait for the process to launch and return control to us, and then |
| 2538 | // call DidLaunch: |
| 2539 | StateType state = WaitForProcessStopPrivate(event_sp, seconds(10)); |
Stella Stamenova | 36d457c | 2018-06-01 19:14:53 +0000 | [diff] [blame] | 2540 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2541 | if (state == eStateInvalid || !event_sp) { |
| 2542 | // We were able to launch the process, but we failed to catch the |
| 2543 | // initial stop. |
| 2544 | error.SetErrorString("failed to catch stop after launch"); |
| 2545 | SetExitStatus(0, "failed to catch stop after launch"); |
| 2546 | Destroy(false); |
| 2547 | } else if (state == eStateStopped || state == eStateCrashed) { |
| 2548 | DidLaunch(); |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 2549 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2550 | DynamicLoader *dyld = GetDynamicLoader(); |
| 2551 | if (dyld) |
| 2552 | dyld->DidLaunch(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2553 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2554 | GetJITLoaders().DidLaunch(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2555 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2556 | SystemRuntime *system_runtime = GetSystemRuntime(); |
| 2557 | if (system_runtime) |
| 2558 | system_runtime->DidLaunch(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2559 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2560 | if (!m_os_up) |
| 2561 | LoadOperatingSystemPlugin(false); |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 2562 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2563 | // We successfully launched the process and stopped, now it the |
| 2564 | // right time to set up signal filters before resuming. |
| 2565 | UpdateAutomaticSignalFiltering(); |
Greg Clayton | 35824e3 | 2015-02-20 20:59:47 +0000 | [diff] [blame] | 2566 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2567 | // Note, the stop event was consumed above, but not handled. This |
| 2568 | // was done to give DidLaunch a chance to run. The target is either |
| 2569 | // stopped or crashed. Directly set the state. This is done to |
| 2570 | // prevent a stop message with a bunch of spurious output on thread |
| 2571 | // status, as well as not pop a ProcessIOHandler. |
| 2572 | SetPublicState(state, false); |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 2573 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2574 | if (PrivateStateThreadIsValid()) |
| 2575 | ResumePrivateStateThread(); |
| 2576 | else |
| 2577 | StartPrivateStateThread(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2578 | |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2579 | // Target was stopped at entry as was intended. Need to notify the |
| 2580 | // listeners about it. |
| 2581 | if (state == eStateStopped && |
| 2582 | launch_info.GetFlags().Test(eLaunchFlagStopAtEntry)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2583 | HandlePrivateEvent(event_sp); |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2584 | } else if (state == eStateExited) { |
| 2585 | // We exited while trying to launch somehow. Don't call DidLaunch |
| 2586 | // as that's not likely to work, and return an invalid pid. |
| 2587 | HandlePrivateEvent(event_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2588 | } |
| 2589 | } |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2590 | } |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2591 | } else { |
| 2592 | error.SetErrorStringWithFormat("file doesn't exist: '%s'", |
| 2593 | local_exec_file_path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2594 | } |
Zachary Turner | 7e89b3c | 2019-03-08 00:11:27 +0000 | [diff] [blame] | 2595 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2596 | return error; |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2597 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2598 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2599 | Status Process::LoadCore() { |
| 2600 | Status error = DoLoadCore(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2601 | if (error.Success()) { |
| 2602 | ListenerSP listener_sp( |
| 2603 | Listener::MakeListener("lldb.process.load_core_listener")); |
| 2604 | HijackProcessEvents(listener_sp); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2605 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2606 | if (PrivateStateThreadIsValid()) |
| 2607 | ResumePrivateStateThread(); |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 2608 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2609 | StartPrivateStateThread(); |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 2610 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2611 | DynamicLoader *dyld = GetDynamicLoader(); |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2612 | if (dyld) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2613 | dyld->DidAttach(); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2614 | |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 2615 | GetJITLoaders().DidAttach(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 2616 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2617 | SystemRuntime *system_runtime = GetSystemRuntime(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2618 | if (system_runtime) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2619 | system_runtime->DidAttach(); |
Jason Molenda | eef5106 | 2013-11-05 03:57:19 +0000 | [diff] [blame] | 2620 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2621 | if (!m_os_up) |
Greg Clayton | 29eeea0 | 2017-02-09 18:55:41 +0000 | [diff] [blame] | 2622 | LoadOperatingSystemPlugin(false); |
| 2623 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2624 | // 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] | 2625 | // 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] | 2626 | SetPrivateState(eStateStopped); |
Greg Clayton | a97c4d2 | 2014-12-09 23:31:02 +0000 | [diff] [blame] | 2627 | |
Jonas Devlieghere | 25486b7 | 2018-03-14 11:50:10 +0000 | [diff] [blame] | 2628 | // Wait for a stopped event since we just posted one above... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2629 | lldb::EventSP event_sp; |
Jonas Devlieghere | 25486b7 | 2018-03-14 11:50:10 +0000 | [diff] [blame] | 2630 | StateType state = |
Pavel Labath | 7ada1c5 | 2019-06-25 07:14:29 +0000 | [diff] [blame] | 2631 | WaitForProcessToStop(llvm::None, &event_sp, true, listener_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2632 | |
| 2633 | if (!StateIsStoppedState(state, false)) { |
| 2634 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2635 | LLDB_LOGF(log, "Process::Halt() failed to stop, state is: %s", |
| 2636 | StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2637 | error.SetErrorString( |
| 2638 | "Did not get stopped event after loading the core file."); |
| 2639 | } |
| 2640 | RestoreProcessEvents(); |
| 2641 | } |
| 2642 | return error; |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2645 | DynamicLoader *Process::GetDynamicLoader() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2646 | if (!m_dyld_up) |
| 2647 | m_dyld_up.reset(DynamicLoader::FindPlugin(this, nullptr)); |
| 2648 | return m_dyld_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Antonio Afonso | 943faef | 2019-06-11 20:16:13 +0000 | [diff] [blame] | 2651 | DataExtractor Process::GetAuxvData() { return DataExtractor(); } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2652 | |
| 2653 | JITLoaderList &Process::GetJITLoaders() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2654 | if (!m_jit_loaders_up) { |
| 2655 | m_jit_loaders_up.reset(new JITLoaderList()); |
| 2656 | JITLoader::LoadPlugins(this, *m_jit_loaders_up); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2657 | } |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2658 | return *m_jit_loaders_up; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | SystemRuntime *Process::GetSystemRuntime() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2662 | if (!m_system_runtime_up) |
| 2663 | m_system_runtime_up.reset(SystemRuntime::FindPlugin(this)); |
| 2664 | return m_system_runtime_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | Process::AttachCompletionHandler::AttachCompletionHandler(Process *process, |
| 2668 | uint32_t exec_count) |
| 2669 | : NextEventAction(process), m_exec_count(exec_count) { |
| 2670 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2671 | LLDB_LOGF( |
| 2672 | log, |
| 2673 | "Process::AttachCompletionHandler::%s process=%p, exec_count=%" PRIu32, |
| 2674 | __FUNCTION__, static_cast<void *>(process), exec_count); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | Process::NextEventAction::EventActionResult |
| 2678 | Process::AttachCompletionHandler::PerformAction(lldb::EventSP &event_sp) { |
| 2679 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2680 | |
| 2681 | StateType state = ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2682 | LLDB_LOGF(log, |
| 2683 | "Process::AttachCompletionHandler::%s called with state %s (%d)", |
| 2684 | __FUNCTION__, StateAsCString(state), static_cast<int>(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2685 | |
| 2686 | switch (state) { |
| 2687 | case eStateAttaching: |
| 2688 | return eEventActionSuccess; |
| 2689 | |
| 2690 | case eStateRunning: |
| 2691 | case eStateConnected: |
| 2692 | return eEventActionRetry; |
| 2693 | |
| 2694 | case eStateStopped: |
| 2695 | case eStateCrashed: |
| 2696 | // During attach, prior to sending the eStateStopped event, |
| 2697 | // lldb_private::Process subclasses must set the new process ID. |
| 2698 | assert(m_process->GetID() != LLDB_INVALID_PROCESS_ID); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2699 | // We don't want these events to be reported, so go set the |
| 2700 | // ShouldReportStop here: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2701 | m_process->GetThreadList().SetShouldReportStop(eVoteNo); |
| 2702 | |
| 2703 | if (m_exec_count > 0) { |
| 2704 | --m_exec_count; |
| 2705 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2706 | LLDB_LOGF(log, |
| 2707 | "Process::AttachCompletionHandler::%s state %s: reduced " |
| 2708 | "remaining exec count to %" PRIu32 ", requesting resume", |
| 2709 | __FUNCTION__, StateAsCString(state), m_exec_count); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2710 | |
| 2711 | RequestResume(); |
| 2712 | return eEventActionRetry; |
| 2713 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2714 | LLDB_LOGF(log, |
| 2715 | "Process::AttachCompletionHandler::%s state %s: no more " |
| 2716 | "execs expected to start, continuing with attach", |
| 2717 | __FUNCTION__, StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2718 | |
| 2719 | m_process->CompleteAttach(); |
| 2720 | return eEventActionSuccess; |
| 2721 | } |
| 2722 | break; |
| 2723 | |
| 2724 | default: |
| 2725 | case eStateExited: |
| 2726 | case eStateInvalid: |
| 2727 | break; |
| 2728 | } |
| 2729 | |
| 2730 | m_exit_string.assign("No valid Process"); |
| 2731 | return eEventActionExit; |
| 2732 | } |
| 2733 | |
| 2734 | Process::NextEventAction::EventActionResult |
| 2735 | Process::AttachCompletionHandler::HandleBeingInterrupted() { |
| 2736 | return eEventActionSuccess; |
| 2737 | } |
| 2738 | |
| 2739 | const char *Process::AttachCompletionHandler::GetExitString() { |
| 2740 | return m_exit_string.c_str(); |
| 2741 | } |
| 2742 | |
| 2743 | ListenerSP ProcessAttachInfo::GetListenerForProcess(Debugger &debugger) { |
| 2744 | if (m_listener_sp) |
| 2745 | return m_listener_sp; |
| 2746 | else |
| 2747 | return debugger.GetListener(); |
| 2748 | } |
| 2749 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2750 | Status Process::Attach(ProcessAttachInfo &attach_info) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2751 | m_abi_sp.reset(); |
| 2752 | m_process_input_reader.reset(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2753 | m_dyld_up.reset(); |
| 2754 | m_jit_loaders_up.reset(); |
| 2755 | m_system_runtime_up.reset(); |
| 2756 | m_os_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2757 | |
| 2758 | lldb::pid_t attach_pid = attach_info.GetProcessID(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 2759 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2760 | if (attach_pid == LLDB_INVALID_PROCESS_ID) { |
| 2761 | char process_name[PATH_MAX]; |
| 2762 | |
| 2763 | if (attach_info.GetExecutableFile().GetPath(process_name, |
| 2764 | sizeof(process_name))) { |
| 2765 | const bool wait_for_launch = attach_info.GetWaitForLaunch(); |
| 2766 | |
| 2767 | if (wait_for_launch) { |
| 2768 | error = WillAttachToProcessWithName(process_name, wait_for_launch); |
| 2769 | if (error.Success()) { |
| 2770 | if (m_public_run_lock.TrySetRunning()) { |
| 2771 | m_should_detach = true; |
| 2772 | const bool restarted = false; |
| 2773 | SetPublicState(eStateAttaching, restarted); |
| 2774 | // Now attach using these arguments. |
| 2775 | error = DoAttachToProcessWithName(process_name, attach_info); |
| 2776 | } else { |
| 2777 | // This shouldn't happen |
| 2778 | error.SetErrorString("failed to acquire process run lock"); |
| 2779 | } |
| 2780 | |
| 2781 | if (error.Fail()) { |
| 2782 | if (GetID() != LLDB_INVALID_PROCESS_ID) { |
| 2783 | SetID(LLDB_INVALID_PROCESS_ID); |
| 2784 | if (error.AsCString() == nullptr) |
| 2785 | error.SetErrorString("attach failed"); |
| 2786 | |
| 2787 | SetExitStatus(-1, error.AsCString()); |
| 2788 | } |
| 2789 | } else { |
| 2790 | SetNextEventAction(new Process::AttachCompletionHandler( |
| 2791 | this, attach_info.GetResumeCount())); |
| 2792 | StartPrivateStateThread(); |
| 2793 | } |
| 2794 | return error; |
| 2795 | } |
| 2796 | } else { |
| 2797 | ProcessInstanceInfoList process_infos; |
| 2798 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 2799 | |
| 2800 | if (platform_sp) { |
| 2801 | ProcessInstanceInfoMatch match_info; |
| 2802 | match_info.GetProcessInfo() = attach_info; |
Pavel Labath | c4a3395 | 2017-02-20 11:35:33 +0000 | [diff] [blame] | 2803 | match_info.SetNameMatchType(NameMatch::Equals); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2804 | platform_sp->FindProcesses(match_info, process_infos); |
| 2805 | const uint32_t num_matches = process_infos.GetSize(); |
| 2806 | if (num_matches == 1) { |
| 2807 | attach_pid = process_infos.GetProcessIDAtIndex(0); |
| 2808 | // Fall through and attach using the above process ID |
| 2809 | } else { |
| 2810 | match_info.GetProcessInfo().GetExecutableFile().GetPath( |
| 2811 | process_name, sizeof(process_name)); |
| 2812 | if (num_matches > 1) { |
| 2813 | StreamString s; |
Pavel Labath | aa51e6a | 2019-03-04 18:48:00 +0000 | [diff] [blame] | 2814 | ProcessInstanceInfo::DumpTableHeader(s, true, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2815 | for (size_t i = 0; i < num_matches; i++) { |
| 2816 | process_infos.GetProcessInfoAtIndex(i).DumpAsTableRow( |
Pavel Labath | aa51e6a | 2019-03-04 18:48:00 +0000 | [diff] [blame] | 2817 | s, platform_sp->GetUserIDResolver(), true, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2818 | } |
| 2819 | error.SetErrorStringWithFormat( |
| 2820 | "more than one process named %s:\n%s", process_name, |
| 2821 | s.GetData()); |
| 2822 | } else |
| 2823 | error.SetErrorStringWithFormat( |
| 2824 | "could not find a process named %s", process_name); |
| 2825 | } |
| 2826 | } else { |
| 2827 | error.SetErrorString( |
| 2828 | "invalid platform, can't find processes by name"); |
| 2829 | return error; |
| 2830 | } |
| 2831 | } |
| 2832 | } else { |
| 2833 | error.SetErrorString("invalid process name"); |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | if (attach_pid != LLDB_INVALID_PROCESS_ID) { |
| 2838 | error = WillAttachToProcessWithID(attach_pid); |
| 2839 | if (error.Success()) { |
| 2840 | |
| 2841 | if (m_public_run_lock.TrySetRunning()) { |
| 2842 | // Now attach using these arguments. |
| 2843 | m_should_detach = true; |
| 2844 | const bool restarted = false; |
| 2845 | SetPublicState(eStateAttaching, restarted); |
| 2846 | error = DoAttachToProcessWithID(attach_pid, attach_info); |
| 2847 | } else { |
| 2848 | // This shouldn't happen |
| 2849 | error.SetErrorString("failed to acquire process run lock"); |
| 2850 | } |
| 2851 | |
| 2852 | if (error.Success()) { |
| 2853 | SetNextEventAction(new Process::AttachCompletionHandler( |
| 2854 | this, attach_info.GetResumeCount())); |
| 2855 | StartPrivateStateThread(); |
| 2856 | } else { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2857 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2858 | SetID(LLDB_INVALID_PROCESS_ID); |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 2859 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2860 | const char *error_string = error.AsCString(); |
| 2861 | if (error_string == nullptr) |
| 2862 | error_string = "attach failed"; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2863 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2864 | SetExitStatus(-1, error_string); |
| 2865 | } |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 2866 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2867 | } |
| 2868 | return error; |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2871 | void Process::CompleteAttach() { |
| 2872 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | |
| 2873 | LIBLLDB_LOG_TARGET)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2874 | LLDB_LOGF(log, "Process::%s()", __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2875 | |
| 2876 | // Let the process subclass figure out at much as it can about the process |
| 2877 | // before we go looking for a dynamic loader plug-in. |
| 2878 | ArchSpec process_arch; |
| 2879 | DidAttach(process_arch); |
| 2880 | |
| 2881 | if (process_arch.IsValid()) { |
| 2882 | GetTarget().SetArchitecture(process_arch); |
| 2883 | if (log) { |
| 2884 | const char *triple_str = process_arch.GetTriple().getTriple().c_str(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2885 | LLDB_LOGF(log, |
| 2886 | "Process::%s replacing process architecture with DidAttach() " |
| 2887 | "architecture: %s", |
| 2888 | __FUNCTION__, triple_str ? triple_str : "<null>"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | // 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] | 2893 | // architecture, and if it isn't the same as the one we've already set, |
| 2894 | // switch architectures. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2895 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 2896 | assert(platform_sp); |
| 2897 | if (platform_sp) { |
| 2898 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
| 2899 | if (target_arch.IsValid() && |
| 2900 | !platform_sp->IsCompatibleArchitecture(target_arch, false, nullptr)) { |
| 2901 | ArchSpec platform_arch; |
| 2902 | platform_sp = |
| 2903 | platform_sp->GetPlatformForArchitecture(target_arch, &platform_arch); |
| 2904 | if (platform_sp) { |
| 2905 | GetTarget().SetPlatform(platform_sp); |
| 2906 | GetTarget().SetArchitecture(platform_arch); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2907 | LLDB_LOGF(log, |
| 2908 | "Process::%s switching platform to %s and architecture " |
| 2909 | "to %s based on info from attach", |
| 2910 | __FUNCTION__, platform_sp->GetName().AsCString(""), |
| 2911 | platform_arch.GetTriple().getTriple().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2912 | } |
| 2913 | } else if (!process_arch.IsValid()) { |
| 2914 | ProcessInstanceInfo process_info; |
| 2915 | GetProcessInfo(process_info); |
| 2916 | const ArchSpec &process_arch = process_info.GetArchitecture(); |
| 2917 | if (process_arch.IsValid() && |
| 2918 | !GetTarget().GetArchitecture().IsExactMatch(process_arch)) { |
| 2919 | GetTarget().SetArchitecture(process_arch); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2920 | LLDB_LOGF(log, |
| 2921 | "Process::%s switching architecture to %s based on info " |
| 2922 | "the platform retrieved for pid %" PRIu64, |
| 2923 | __FUNCTION__, process_arch.GetTriple().getTriple().c_str(), |
| 2924 | GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2925 | } |
| 2926 | } |
| 2927 | } |
| 2928 | |
| 2929 | // We have completed the attach, now it is time to find the dynamic loader |
| 2930 | // plug-in |
| 2931 | DynamicLoader *dyld = GetDynamicLoader(); |
| 2932 | if (dyld) { |
| 2933 | dyld->DidAttach(); |
| 2934 | if (log) { |
| 2935 | ModuleSP exe_module_sp = GetTarget().GetExecutableModule(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2936 | LLDB_LOGF(log, |
| 2937 | "Process::%s after DynamicLoader::DidAttach(), target " |
| 2938 | "executable is %s (using %s plugin)", |
| 2939 | __FUNCTION__, |
| 2940 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str() |
| 2941 | : "<none>", |
| 2942 | dyld->GetPluginName().AsCString("<unnamed>")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | GetJITLoaders().DidAttach(); |
| 2947 | |
| 2948 | SystemRuntime *system_runtime = GetSystemRuntime(); |
| 2949 | if (system_runtime) { |
| 2950 | system_runtime->DidAttach(); |
| 2951 | if (log) { |
| 2952 | ModuleSP exe_module_sp = GetTarget().GetExecutableModule(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2953 | LLDB_LOGF(log, |
| 2954 | "Process::%s after SystemRuntime::DidAttach(), target " |
| 2955 | "executable is %s (using %s plugin)", |
| 2956 | __FUNCTION__, |
| 2957 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str() |
| 2958 | : "<none>", |
| 2959 | system_runtime->GetPluginName().AsCString("<unnamed>")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2960 | } |
| 2961 | } |
| 2962 | |
Jim Ingham | 8864b43 | 2019-06-28 17:57:19 +0000 | [diff] [blame] | 2963 | if (!m_os_up) { |
Greg Clayton | 29eeea0 | 2017-02-09 18:55:41 +0000 | [diff] [blame] | 2964 | LoadOperatingSystemPlugin(false); |
Jim Ingham | 8864b43 | 2019-06-28 17:57:19 +0000 | [diff] [blame] | 2965 | if (m_os_up) { |
| 2966 | // Somebody might have gotten threads before now, but we need to force the |
| 2967 | // update after we've loaded the OperatingSystem plugin or it won't get a |
| 2968 | // chance to process the threads. |
| 2969 | m_thread_list.Clear(); |
| 2970 | UpdateThreadListIfNeeded(); |
| 2971 | } |
| 2972 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2973 | // Figure out which one is the executable, and set that in our target: |
| 2974 | const ModuleList &target_modules = GetTarget().GetImages(); |
| 2975 | std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); |
| 2976 | size_t num_modules = target_modules.GetSize(); |
| 2977 | ModuleSP new_executable_module_sp; |
| 2978 | |
| 2979 | for (size_t i = 0; i < num_modules; i++) { |
| 2980 | ModuleSP module_sp(target_modules.GetModuleAtIndexUnlocked(i)); |
| 2981 | if (module_sp && module_sp->IsExecutable()) { |
| 2982 | if (GetTarget().GetExecutableModulePointer() != module_sp.get()) |
| 2983 | new_executable_module_sp = module_sp; |
| 2984 | break; |
| 2985 | } |
| 2986 | } |
| 2987 | if (new_executable_module_sp) { |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 2988 | GetTarget().SetExecutableModule(new_executable_module_sp, |
| 2989 | eLoadDependentsNo); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2990 | if (log) { |
| 2991 | ModuleSP exe_module_sp = GetTarget().GetExecutableModule(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2992 | LLDB_LOGF( |
| 2993 | log, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2994 | "Process::%s after looping through modules, target executable is %s", |
| 2995 | __FUNCTION__, |
| 2996 | exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str() |
| 2997 | : "<none>"); |
| 2998 | } |
| 2999 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3002 | Status Process::ConnectRemote(Stream *strm, llvm::StringRef remote_url) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3003 | m_abi_sp.reset(); |
| 3004 | m_process_input_reader.reset(); |
| 3005 | |
| 3006 | // Find the process and its architecture. Make sure it matches the |
Zachary Turner | 3165945 | 2016-11-17 21:15:14 +0000 | [diff] [blame] | 3007 | // architecture of the current Target, and if not adjust it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3008 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3009 | Status error(DoConnectRemote(strm, remote_url)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3010 | if (error.Success()) { |
| 3011 | if (GetID() != LLDB_INVALID_PROCESS_ID) { |
| 3012 | EventSP event_sp; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3013 | StateType state = WaitForProcessStopPrivate(event_sp, llvm::None); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3014 | |
| 3015 | if (state == eStateStopped || state == eStateCrashed) { |
| 3016 | // If we attached and actually have a process on the other end, then |
| 3017 | // this ended up being the equivalent of an attach. |
| 3018 | CompleteAttach(); |
| 3019 | |
| 3020 | // This delays passing the stopped event to listeners till |
| 3021 | // CompleteAttach gets a chance to complete... |
| 3022 | HandlePrivateEvent(event_sp); |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | if (PrivateStateThreadIsValid()) |
| 3027 | ResumePrivateStateThread(); |
| 3028 | else |
| 3029 | StartPrivateStateThread(); |
| 3030 | } |
| 3031 | return error; |
| 3032 | } |
| 3033 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3034 | Status Process::PrivateResume() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3035 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | |
| 3036 | LIBLLDB_LOG_STEP)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3037 | LLDB_LOGF(log, |
| 3038 | "Process::PrivateResume() m_stop_id = %u, public state: %s " |
| 3039 | "private state: %s", |
| 3040 | m_mod_id.GetStopID(), StateAsCString(m_public_state.GetValue()), |
| 3041 | StateAsCString(m_private_state.GetValue())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3042 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3043 | // If signals handing status changed we might want to update our signal |
| 3044 | // filters before resuming. |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 3045 | UpdateAutomaticSignalFiltering(); |
| 3046 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3047 | Status error(WillResume()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3048 | // Tell the process it is about to resume before the thread list |
| 3049 | if (error.Success()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3050 | // Now let the thread list know we are about to resume so it can let all of |
| 3051 | // our threads know that they are about to be resumed. Threads will each be |
| 3052 | // called with Thread::WillResume(StateType) where StateType contains the |
| 3053 | // state that they are supposed to have when the process is resumed |
| 3054 | // (suspended/running/stepping). Threads should also check their resume |
| 3055 | // signal in lldb::Thread::GetResumeSignal() to see if they are supposed to |
| 3056 | // start back up with a signal. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3057 | if (m_thread_list.WillResume()) { |
| 3058 | // Last thing, do the PreResumeActions. |
| 3059 | if (!RunPreResumeActions()) { |
| 3060 | error.SetErrorStringWithFormat( |
| 3061 | "Process::PrivateResume PreResumeActions failed, not resuming."); |
| 3062 | } else { |
| 3063 | m_mod_id.BumpResumeID(); |
| 3064 | error = DoResume(); |
| 3065 | if (error.Success()) { |
| 3066 | DidResume(); |
| 3067 | m_thread_list.DidResume(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3068 | LLDB_LOGF(log, "Process thinks the process has resumed."); |
Jason Molenda | f47c734 | 2018-12-17 23:33:40 +0000 | [diff] [blame] | 3069 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3070 | LLDB_LOGF(log, "Process::PrivateResume() DoResume failed."); |
Jason Molenda | f47c734 | 2018-12-17 23:33:40 +0000 | [diff] [blame] | 3071 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3072 | } |
| 3073 | } |
| 3074 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3075 | // Somebody wanted to run without running (e.g. we were faking a step |
| 3076 | // from one frame of a set of inlined frames that share the same PC to |
| 3077 | // another.) So generate a continue & a stopped event, and let the world |
| 3078 | // handle them. |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3079 | LLDB_LOGF(log, |
| 3080 | "Process::PrivateResume() asked to simulate a start & stop."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3081 | |
| 3082 | SetPrivateState(eStateRunning); |
| 3083 | SetPrivateState(eStateStopped); |
| 3084 | } |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3085 | } else |
| 3086 | LLDB_LOGF(log, "Process::PrivateResume() got an error \"%s\".", |
| 3087 | error.AsCString("<unknown error>")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3088 | return error; |
| 3089 | } |
| 3090 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3091 | Status Process::Halt(bool clear_thread_plans, bool use_run_lock) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3092 | if (!StateIsRunningState(m_public_state.GetValue())) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3093 | return Status("Process is not running."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3094 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3095 | // Don't clear the m_clear_thread_plans_on_stop, only set it to true if in |
| 3096 | // 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] | 3097 | m_clear_thread_plans_on_stop |= clear_thread_plans; |
| 3098 | |
| 3099 | ListenerSP halt_listener_sp( |
| 3100 | Listener::MakeListener("lldb.process.halt_listener")); |
| 3101 | HijackProcessEvents(halt_listener_sp); |
| 3102 | |
| 3103 | EventSP event_sp; |
| 3104 | |
| 3105 | SendAsyncInterrupt(); |
| 3106 | |
| 3107 | if (m_public_state.GetValue() == eStateAttaching) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3108 | // Don't hijack and eat the eStateExited as the code that was doing the |
| 3109 | // attach will be waiting for this event... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3110 | RestoreProcessEvents(); |
| 3111 | SetExitStatus(SIGKILL, "Cancelled async attach."); |
| 3112 | Destroy(false); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3113 | return Status(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | // Wait for 10 second for the process to stop. |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3117 | StateType state = WaitForProcessToStop( |
| 3118 | seconds(10), &event_sp, true, halt_listener_sp, nullptr, use_run_lock); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3119 | RestoreProcessEvents(); |
| 3120 | |
| 3121 | if (state == eStateInvalid || !event_sp) { |
| 3122 | // We timed out and didn't get a stop event... |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3123 | return Status("Halt timed out. State = %s", StateAsCString(GetState())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | BroadcastEvent(event_sp); |
| 3127 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3128 | return Status(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3131 | Status Process::StopForDestroyOrDetach(lldb::EventSP &exit_event_sp) { |
| 3132 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3133 | |
| 3134 | // 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] | 3135 | // expression, for instance, then the public state will be stopped, but we |
| 3136 | // still need to interrupt. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3137 | if (m_public_state.GetValue() == eStateRunning || |
| 3138 | m_private_state.GetValue() == eStateRunning) { |
| 3139 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3140 | LLDB_LOGF(log, "Process::%s() About to stop.", __FUNCTION__); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3142 | ListenerSP listener_sp( |
| 3143 | Listener::MakeListener("lldb.Process.StopForDestroyOrDetach.hijack")); |
| 3144 | HijackProcessEvents(listener_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3145 | |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3146 | SendAsyncInterrupt(); |
| 3147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3148 | // Consume the interrupt event. |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3149 | StateType state = |
| 3150 | WaitForProcessToStop(seconds(10), &exit_event_sp, true, listener_sp); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3151 | |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3152 | RestoreProcessEvents(); |
Jim Ingham | bb3a283 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3153 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3154 | // 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] | 3155 | // exited event into the shared pointer passed in and return. Our caller |
| 3156 | // doesn't need to do anything else, since they don't have a process |
| 3157 | // anymore... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3158 | |
| 3159 | if (state == eStateExited || m_private_state.GetValue() == eStateExited) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3160 | LLDB_LOGF(log, "Process::%s() Process exited while waiting to stop.", |
| 3161 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3162 | return error; |
| 3163 | } else |
| 3164 | exit_event_sp.reset(); // It is ok to consume any non-exit stop events |
| 3165 | |
| 3166 | if (state != eStateStopped) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3167 | LLDB_LOGF(log, "Process::%s() failed to stop, state is: %s", __FUNCTION__, |
| 3168 | StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3169 | // 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] | 3170 | // here, but if the lower levels just bobbled sending the event and we |
| 3171 | // really are stopped, then continue on. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3172 | StateType private_state = m_private_state.GetValue(); |
| 3173 | if (private_state != eStateStopped) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3174 | return Status( |
| 3175 | "Attempt to stop the target in order to detach timed out. " |
| 3176 | "State = %s", |
| 3177 | StateAsCString(GetState())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3178 | } |
| 3179 | } |
| 3180 | } |
| 3181 | return error; |
| 3182 | } |
| 3183 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3184 | Status Process::Detach(bool keep_stopped) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3185 | EventSP exit_event_sp; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3186 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3187 | m_destroy_in_process = true; |
| 3188 | |
| 3189 | error = WillDetach(); |
| 3190 | |
| 3191 | if (error.Success()) { |
| 3192 | if (DetachRequiresHalt()) { |
| 3193 | error = StopForDestroyOrDetach(exit_event_sp); |
| 3194 | if (!error.Success()) { |
| 3195 | m_destroy_in_process = false; |
| 3196 | return error; |
| 3197 | } else if (exit_event_sp) { |
| 3198 | // We shouldn't need to do anything else here. There's no process left |
| 3199 | // to detach from... |
| 3200 | StopPrivateStateThread(); |
| 3201 | m_destroy_in_process = false; |
| 3202 | return error; |
| 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | m_thread_list.DiscardThreadPlans(); |
| 3207 | DisableAllBreakpointSites(); |
| 3208 | |
| 3209 | error = DoDetach(keep_stopped); |
| 3210 | if (error.Success()) { |
| 3211 | DidDetach(); |
| 3212 | StopPrivateStateThread(); |
| 3213 | } else { |
| 3214 | return error; |
| 3215 | } |
| 3216 | } |
| 3217 | m_destroy_in_process = false; |
| 3218 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3219 | // If we exited when we were waiting for a process to stop, then forward the |
| 3220 | // event here so we don't lose the event |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3221 | if (exit_event_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3222 | // Directly broadcast our exited event because we shut down our private |
| 3223 | // state thread above |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3224 | BroadcastEvent(exit_event_sp); |
| 3225 | } |
| 3226 | |
| 3227 | // 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] | 3228 | // not end up propagating the last events through the event system, in which |
| 3229 | // case we might strand the write lock. Unlock it here so when we do to tear |
| 3230 | // down the process we don't get an error destroying the lock. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3231 | |
| 3232 | m_public_run_lock.SetStopped(); |
| 3233 | return error; |
| 3234 | } |
| 3235 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3236 | Status Process::Destroy(bool force_kill) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3237 | |
| 3238 | // 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] | 3239 | // don't do any unnecessary work that might hinder the destruction. Remember |
| 3240 | // 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] | 3241 | // failed and the process stays around for some reason it won't be in a |
| 3242 | // confused state. |
| 3243 | |
| 3244 | if (force_kill) |
| 3245 | m_should_detach = false; |
| 3246 | |
| 3247 | if (GetShouldDetach()) { |
| 3248 | // FIXME: This will have to be a process setting: |
| 3249 | bool keep_stopped = false; |
| 3250 | Detach(keep_stopped); |
| 3251 | } |
| 3252 | |
| 3253 | m_destroy_in_process = true; |
| 3254 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3255 | Status error(WillDestroy()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3256 | if (error.Success()) { |
| 3257 | EventSP exit_event_sp; |
| 3258 | if (DestroyRequiresHalt()) { |
| 3259 | error = StopForDestroyOrDetach(exit_event_sp); |
| 3260 | } |
| 3261 | |
| 3262 | if (m_public_state.GetValue() != eStateRunning) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3263 | // Ditch all thread plans, and remove all our breakpoints: in case we |
| 3264 | // have to restart the target to kill it, we don't want it hitting a |
| 3265 | // breakpoint... Only do this if we've stopped, however, since if we |
| 3266 | // didn't manage to halt it above, then we're not going to have much luck |
| 3267 | // doing this now. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3268 | m_thread_list.DiscardThreadPlans(); |
| 3269 | DisableAllBreakpointSites(); |
| 3270 | } |
| 3271 | |
| 3272 | error = DoDestroy(); |
| 3273 | if (error.Success()) { |
| 3274 | DidDestroy(); |
| 3275 | StopPrivateStateThread(); |
| 3276 | } |
| 3277 | m_stdio_communication.Disconnect(); |
| 3278 | m_stdio_communication.StopReadThread(); |
| 3279 | m_stdin_forward = false; |
| 3280 | |
| 3281 | if (m_process_input_reader) { |
| 3282 | m_process_input_reader->SetIsDone(true); |
| 3283 | m_process_input_reader->Cancel(); |
| 3284 | m_process_input_reader.reset(); |
| 3285 | } |
| 3286 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3287 | // If we exited when we were waiting for a process to stop, then forward |
| 3288 | // the event here so we don't lose the event |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3289 | if (exit_event_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3290 | // Directly broadcast our exited event because we shut down our private |
| 3291 | // state thread above |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3292 | BroadcastEvent(exit_event_sp); |
| 3293 | } |
| 3294 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3295 | // If we have been interrupted (to kill us) in the middle of running, we |
| 3296 | // may not end up propagating the last events through the event system, in |
| 3297 | // which case we might strand the write lock. Unlock it here so when we do |
| 3298 | // 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] | 3299 | m_public_run_lock.SetStopped(); |
| 3300 | } |
| 3301 | |
| 3302 | m_destroy_in_process = false; |
| 3303 | |
| 3304 | return error; |
| 3305 | } |
| 3306 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3307 | Status Process::Signal(int signal) { |
| 3308 | Status error(WillSignal()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3309 | if (error.Success()) { |
| 3310 | error = DoSignal(signal); |
| 3311 | if (error.Success()) |
| 3312 | DidSignal(); |
| 3313 | } |
| 3314 | return error; |
| 3315 | } |
| 3316 | |
| 3317 | void Process::SetUnixSignals(UnixSignalsSP &&signals_sp) { |
| 3318 | assert(signals_sp && "null signals_sp"); |
| 3319 | m_unix_signals_sp = signals_sp; |
| 3320 | } |
| 3321 | |
| 3322 | const lldb::UnixSignalsSP &Process::GetUnixSignals() { |
| 3323 | assert(m_unix_signals_sp && "null m_unix_signals_sp"); |
| 3324 | return m_unix_signals_sp; |
| 3325 | } |
| 3326 | |
| 3327 | lldb::ByteOrder Process::GetByteOrder() const { |
| 3328 | return GetTarget().GetArchitecture().GetByteOrder(); |
| 3329 | } |
| 3330 | |
| 3331 | uint32_t Process::GetAddressByteSize() const { |
| 3332 | return GetTarget().GetArchitecture().GetAddressByteSize(); |
| 3333 | } |
| 3334 | |
| 3335 | bool Process::ShouldBroadcastEvent(Event *event_ptr) { |
| 3336 | const StateType state = |
| 3337 | Process::ProcessEventData::GetStateFromEvent(event_ptr); |
| 3338 | bool return_value = true; |
| 3339 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS | |
| 3340 | LIBLLDB_LOG_PROCESS)); |
| 3341 | |
| 3342 | switch (state) { |
| 3343 | case eStateDetached: |
| 3344 | case eStateExited: |
| 3345 | case eStateUnloaded: |
| 3346 | m_stdio_communication.SynchronizeWithReadThread(); |
| 3347 | m_stdio_communication.Disconnect(); |
| 3348 | m_stdio_communication.StopReadThread(); |
| 3349 | m_stdin_forward = false; |
| 3350 | |
| 3351 | LLVM_FALLTHROUGH; |
| 3352 | case eStateConnected: |
| 3353 | case eStateAttaching: |
| 3354 | case eStateLaunching: |
| 3355 | // These events indicate changes in the state of the debugging session, |
| 3356 | // always report them. |
| 3357 | return_value = true; |
| 3358 | break; |
| 3359 | case eStateInvalid: |
| 3360 | // We stopped for no apparent reason, don't report it. |
| 3361 | return_value = false; |
| 3362 | break; |
| 3363 | case eStateRunning: |
| 3364 | case eStateStepping: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3365 | // If we've started the target running, we handle the cases where we are |
| 3366 | // already running and where there is a transition from stopped to running |
| 3367 | // differently. running -> running: Automatically suppress extra running |
| 3368 | // events stopped -> running: Report except when there is one or more no |
| 3369 | // votes |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3370 | // and no yes votes. |
| 3371 | SynchronouslyNotifyStateChanged(state); |
| 3372 | if (m_force_next_event_delivery) |
| 3373 | return_value = true; |
| 3374 | else { |
| 3375 | switch (m_last_broadcast_state) { |
| 3376 | case eStateRunning: |
| 3377 | case eStateStepping: |
| 3378 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 3379 | return_value = false; |
| 3380 | break; |
| 3381 | default: |
| 3382 | // TODO: make this work correctly. For now always report |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3383 | // run if we aren't running so we don't miss any running events. If I |
| 3384 | // run the lldb/test/thread/a.out file and break at main.cpp:58, run |
| 3385 | // and hit the breakpoints on multiple threads, then somehow during the |
| 3386 | // stepping over of all breakpoints no run gets reported. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3387 | |
| 3388 | // This is a transition from stop to run. |
| 3389 | switch (m_thread_list.ShouldReportRun(event_ptr)) { |
| 3390 | case eVoteYes: |
| 3391 | case eVoteNoOpinion: |
| 3392 | return_value = true; |
| 3393 | break; |
| 3394 | case eVoteNo: |
| 3395 | return_value = false; |
| 3396 | break; |
| 3397 | } |
| 3398 | break; |
| 3399 | } |
| 3400 | } |
| 3401 | break; |
| 3402 | case eStateStopped: |
| 3403 | case eStateCrashed: |
| 3404 | case eStateSuspended: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3405 | // We've stopped. First see if we're going to restart the target. If we |
| 3406 | // are going to stop, then we always broadcast the event. If we aren't |
| 3407 | // going to stop, let the thread plans decide if we're going to report this |
| 3408 | // event. If no thread has an opinion, we don't report it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3409 | |
| 3410 | m_stdio_communication.SynchronizeWithReadThread(); |
| 3411 | RefreshStateAfterStop(); |
| 3412 | if (ProcessEventData::GetInterruptedFromEvent(event_ptr)) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3413 | LLDB_LOGF(log, |
| 3414 | "Process::ShouldBroadcastEvent (%p) stopped due to an " |
| 3415 | "interrupt, state: %s", |
| 3416 | static_cast<void *>(event_ptr), StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3417 | // 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] | 3418 | // 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] | 3419 | m_thread_list.ShouldStop(event_ptr); |
| 3420 | return_value = true; |
| 3421 | } else { |
| 3422 | bool was_restarted = ProcessEventData::GetRestartedFromEvent(event_ptr); |
| 3423 | bool should_resume = false; |
| 3424 | |
| 3425 | // It makes no sense to ask "ShouldStop" if we've already been |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3426 | // restarted... Asking the thread list is also not likely to go well, |
| 3427 | // 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] | 3428 | |
| 3429 | if (!was_restarted) |
| 3430 | should_resume = !m_thread_list.ShouldStop(event_ptr); |
| 3431 | |
| 3432 | if (was_restarted || should_resume || m_resume_requested) { |
| 3433 | Vote stop_vote = m_thread_list.ShouldReportStop(event_ptr); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3434 | LLDB_LOGF(log, |
| 3435 | "Process::ShouldBroadcastEvent: should_resume: %i state: " |
| 3436 | "%s was_restarted: %i stop_vote: %d.", |
| 3437 | should_resume, StateAsCString(state), was_restarted, |
| 3438 | stop_vote); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3439 | |
| 3440 | switch (stop_vote) { |
| 3441 | case eVoteYes: |
| 3442 | return_value = true; |
| 3443 | break; |
| 3444 | case eVoteNoOpinion: |
| 3445 | case eVoteNo: |
| 3446 | return_value = false; |
| 3447 | break; |
| 3448 | } |
| 3449 | |
| 3450 | if (!was_restarted) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3451 | LLDB_LOGF(log, |
| 3452 | "Process::ShouldBroadcastEvent (%p) Restarting process " |
| 3453 | "from state: %s", |
| 3454 | static_cast<void *>(event_ptr), StateAsCString(state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3455 | ProcessEventData::SetRestartedInEvent(event_ptr, true); |
| 3456 | PrivateResume(); |
| 3457 | } |
| 3458 | } else { |
| 3459 | return_value = true; |
| 3460 | SynchronouslyNotifyStateChanged(state); |
| 3461 | } |
| 3462 | } |
| 3463 | break; |
| 3464 | } |
| 3465 | |
| 3466 | // Forcing the next event delivery is a one shot deal. So reset it here. |
| 3467 | m_force_next_event_delivery = false; |
| 3468 | |
| 3469 | // We do some coalescing of events (for instance two consecutive running |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3470 | // events get coalesced.) But we only coalesce against events we actually |
| 3471 | // broadcast. So we use m_last_broadcast_state to track that. NB - you |
| 3472 | // can't use "m_public_state.GetValue()" for that purpose, as was originally |
| 3473 | // done, because the PublicState reflects the last event pulled off the |
| 3474 | // queue, and there may be several events stacked up on the queue unserviced. |
| 3475 | // So the PublicState may not reflect the last broadcasted event yet. |
| 3476 | // m_last_broadcast_state gets updated here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3477 | |
| 3478 | if (return_value) |
| 3479 | m_last_broadcast_state = state; |
| 3480 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3481 | LLDB_LOGF(log, |
| 3482 | "Process::ShouldBroadcastEvent (%p) => new state: %s, last " |
| 3483 | "broadcast state: %s - %s", |
| 3484 | static_cast<void *>(event_ptr), StateAsCString(state), |
| 3485 | StateAsCString(m_last_broadcast_state), |
| 3486 | return_value ? "YES" : "NO"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3487 | return return_value; |
| 3488 | } |
| 3489 | |
| 3490 | bool Process::StartPrivateStateThread(bool is_secondary_thread) { |
| 3491 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
| 3492 | |
| 3493 | bool already_running = PrivateStateThreadIsValid(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3494 | LLDB_LOGF(log, "Process::%s()%s ", __FUNCTION__, |
| 3495 | already_running ? " already running" |
| 3496 | : " starting private state thread"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3497 | |
| 3498 | if (!is_secondary_thread && already_running) |
| 3499 | return true; |
| 3500 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3501 | // Create a thread that watches our internal state and controls which events |
| 3502 | // make it to clients (into the DCProcess event queue). |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3503 | char thread_name[1024]; |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 3504 | uint32_t max_len = llvm::get_max_thread_name_length(); |
| 3505 | if (max_len > 0 && max_len <= 30) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3506 | // On platforms with abbreviated thread name lengths, choose thread names |
| 3507 | // that fit within the limit. |
| 3508 | if (already_running) |
| 3509 | snprintf(thread_name, sizeof(thread_name), "intern-state-OV"); |
| 3510 | else |
| 3511 | snprintf(thread_name, sizeof(thread_name), "intern-state"); |
| 3512 | } else { |
| 3513 | if (already_running) |
| 3514 | snprintf(thread_name, sizeof(thread_name), |
| 3515 | "<lldb.process.internal-state-override(pid=%" PRIu64 ")>", |
| 3516 | GetID()); |
| 3517 | else |
| 3518 | snprintf(thread_name, sizeof(thread_name), |
| 3519 | "<lldb.process.internal-state(pid=%" PRIu64 ")>", GetID()); |
| 3520 | } |
| 3521 | |
| 3522 | // Create the private state thread, and start it running. |
| 3523 | PrivateStateThreadArgs *args_ptr = |
| 3524 | new PrivateStateThreadArgs(this, is_secondary_thread); |
Jonas Devlieghere | f39c2e1 | 2019-07-05 17:42:08 +0000 | [diff] [blame] | 3525 | llvm::Expected<HostThread> private_state_thread = |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3526 | ThreadLauncher::LaunchThread(thread_name, Process::PrivateStateThread, |
Jonas Devlieghere | f39c2e1 | 2019-07-05 17:42:08 +0000 | [diff] [blame] | 3527 | (void *)args_ptr, 8 * 1024 * 1024); |
| 3528 | if (!private_state_thread) { |
| 3529 | LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST), |
| 3530 | "failed to launch host thread: {}", |
| 3531 | llvm::toString(private_state_thread.takeError())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3532 | return false; |
Jonas Devlieghere | f39c2e1 | 2019-07-05 17:42:08 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | assert(private_state_thread->IsJoinable()); |
| 3536 | m_private_state_thread = *private_state_thread; |
| 3537 | ResumePrivateStateThread(); |
| 3538 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | void Process::PausePrivateStateThread() { |
| 3542 | ControlPrivateStateThread(eBroadcastInternalStateControlPause); |
| 3543 | } |
| 3544 | |
| 3545 | void Process::ResumePrivateStateThread() { |
| 3546 | ControlPrivateStateThread(eBroadcastInternalStateControlResume); |
| 3547 | } |
| 3548 | |
| 3549 | void Process::StopPrivateStateThread() { |
| 3550 | if (m_private_state_thread.IsJoinable()) |
| 3551 | ControlPrivateStateThread(eBroadcastInternalStateControlStop); |
| 3552 | else { |
| 3553 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3554 | LLDB_LOGF( |
| 3555 | log, |
| 3556 | "Went to stop the private state thread, but it was already invalid."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3557 | } |
| 3558 | } |
| 3559 | |
| 3560 | void Process::ControlPrivateStateThread(uint32_t signal) { |
| 3561 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 3562 | |
| 3563 | assert(signal == eBroadcastInternalStateControlStop || |
| 3564 | signal == eBroadcastInternalStateControlPause || |
| 3565 | signal == eBroadcastInternalStateControlResume); |
| 3566 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3567 | LLDB_LOGF(log, "Process::%s (signal = %d)", __FUNCTION__, signal); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3568 | |
| 3569 | // Signal the private state thread |
| 3570 | if (m_private_state_thread.IsJoinable()) { |
| 3571 | // Broadcast the event. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3572 | // It is important to do this outside of the if below, because it's |
| 3573 | // possible that the thread state is invalid but that the thread is waiting |
| 3574 | // on a control event instead of simply being on its way out (this should |
| 3575 | // not happen, but it apparently can). |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3576 | LLDB_LOGF(log, "Sending control event of type: %d.", signal); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3577 | std::shared_ptr<EventDataReceipt> event_receipt_sp(new EventDataReceipt()); |
| 3578 | m_private_state_control_broadcaster.BroadcastEvent(signal, |
| 3579 | event_receipt_sp); |
| 3580 | |
| 3581 | // Wait for the event receipt or for the private state thread to exit |
| 3582 | bool receipt_received = false; |
| 3583 | if (PrivateStateThreadIsValid()) { |
| 3584 | while (!receipt_received) { |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 3585 | // 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] | 3586 | // state thread is still around. |
Pavel Labath | 38d67db | 2018-05-03 15:33:41 +0000 | [diff] [blame] | 3587 | receipt_received = |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 3588 | event_receipt_sp->WaitForEventReceived(GetUtilityExpressionTimeout()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3589 | if (!receipt_received) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3590 | // Check if the private state thread is still around. If it isn't |
| 3591 | // then we are done waiting |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3592 | if (!PrivateStateThreadIsValid()) |
| 3593 | break; // Private state thread exited or is exiting, we are done |
| 3594 | } |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | if (signal == eBroadcastInternalStateControlStop) { |
Jonas Devlieghere | edb52e2 | 2019-05-23 20:25:49 +0000 | [diff] [blame] | 3599 | thread_result_t result = {}; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3600 | m_private_state_thread.Join(&result); |
| 3601 | m_private_state_thread.Reset(); |
| 3602 | } |
| 3603 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3604 | LLDB_LOGF( |
| 3605 | log, |
| 3606 | "Private state thread already dead, no need to signal it to stop."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3607 | } |
| 3608 | } |
| 3609 | |
| 3610 | void Process::SendAsyncInterrupt() { |
| 3611 | if (PrivateStateThreadIsValid()) |
| 3612 | m_private_state_broadcaster.BroadcastEvent(Process::eBroadcastBitInterrupt, |
| 3613 | nullptr); |
| 3614 | else |
| 3615 | BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr); |
| 3616 | } |
| 3617 | |
| 3618 | void Process::HandlePrivateEvent(EventSP &event_sp) { |
| 3619 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 3620 | m_resume_requested = false; |
| 3621 | |
| 3622 | const StateType new_state = |
| 3623 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 3624 | |
| 3625 | // First check to see if anybody wants a shot at this event: |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 3626 | if (m_next_event_action_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3627 | NextEventAction::EventActionResult action_result = |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 3628 | m_next_event_action_up->PerformAction(event_sp); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3629 | LLDB_LOGF(log, "Ran next event action, result was %d.", action_result); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3630 | |
| 3631 | switch (action_result) { |
| 3632 | case NextEventAction::eEventActionSuccess: |
| 3633 | SetNextEventAction(nullptr); |
| 3634 | break; |
| 3635 | |
| 3636 | case NextEventAction::eEventActionRetry: |
| 3637 | break; |
| 3638 | |
| 3639 | case NextEventAction::eEventActionExit: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3640 | // Handle Exiting Here. If we already got an exited event, we should |
| 3641 | // just propagate it. Otherwise, swallow this event, and set our state |
| 3642 | // to exit so the next event will kill us. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3643 | if (new_state != eStateExited) { |
| 3644 | // FIXME: should cons up an exited event, and discard this one. |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 3645 | SetExitStatus(0, m_next_event_action_up->GetExitString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3646 | SetNextEventAction(nullptr); |
| 3647 | return; |
| 3648 | } |
| 3649 | SetNextEventAction(nullptr); |
| 3650 | break; |
| 3651 | } |
| 3652 | } |
| 3653 | |
| 3654 | // See if we should broadcast this state to external clients? |
| 3655 | const bool should_broadcast = ShouldBroadcastEvent(event_sp.get()); |
| 3656 | |
| 3657 | if (should_broadcast) { |
| 3658 | const bool is_hijacked = IsHijackedForEvent(eBroadcastBitStateChanged); |
| 3659 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3660 | LLDB_LOGF(log, |
| 3661 | "Process::%s (pid = %" PRIu64 |
| 3662 | ") broadcasting new state %s (old state %s) to %s", |
| 3663 | __FUNCTION__, GetID(), StateAsCString(new_state), |
| 3664 | StateAsCString(GetState()), |
| 3665 | is_hijacked ? "hijacked" : "public"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3666 | } |
| 3667 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
| 3668 | if (StateIsRunningState(new_state)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3669 | // Only push the input handler if we aren't fowarding events, as this |
| 3670 | // means the curses GUI is in use... Or don't push it if we are launching |
| 3671 | // since it will come up stopped. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3672 | if (!GetTarget().GetDebugger().IsForwardingEvents() && |
| 3673 | new_state != eStateLaunching && new_state != eStateAttaching) { |
| 3674 | PushProcessIOHandler(); |
| 3675 | m_iohandler_sync.SetValue(m_iohandler_sync.GetValue() + 1, |
| 3676 | eBroadcastAlways); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3677 | LLDB_LOGF(log, "Process::%s updated m_iohandler_sync to %d", |
| 3678 | __FUNCTION__, m_iohandler_sync.GetValue()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3679 | } |
| 3680 | } else if (StateIsStoppedState(new_state, false)) { |
| 3681 | if (!Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3682 | // If the lldb_private::Debugger is handling the events, we don't want |
| 3683 | // to pop the process IOHandler here, we want to do it when we receive |
| 3684 | // the stopped event so we can carefully control when the process |
| 3685 | // IOHandler is popped because when we stop we want to display some |
| 3686 | // text stating how and why we stopped, then maybe some |
| 3687 | // process/thread/frame info, and then we want the "(lldb) " prompt to |
| 3688 | // show up. If we pop the process IOHandler here, then we will cause |
| 3689 | // the command interpreter to become the top IOHandler after the |
| 3690 | // process pops off and it will update its prompt right away... See the |
| 3691 | // Debugger.cpp file where it calls the function as |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3692 | // "process_sp->PopProcessIOHandler()" to see where I am talking about. |
| 3693 | // Otherwise we end up getting overlapping "(lldb) " prompts and |
| 3694 | // garbled output. |
| 3695 | // |
| 3696 | // 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] | 3697 | // by "m_target.GetDebugger().IsHandlingEvents()" returning false) or |
| 3698 | // we are hijacked, then we always pop the process IO handler manually. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3699 | // Hijacking happens when the internal process state thread is running |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3700 | // thread plans, or when commands want to run in synchronous mode and |
| 3701 | // they call "process->WaitForProcessToStop()". An example of something |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3702 | // that will hijack the events is a simple expression: |
| 3703 | // |
| 3704 | // (lldb) expr (int)puts("hello") |
| 3705 | // |
| 3706 | // This will cause the internal process state thread to resume and halt |
| 3707 | // the process (and _it_ will hijack the eBroadcastBitStateChanged |
| 3708 | // events) and we do need the IO handler to be pushed and popped |
| 3709 | // correctly. |
| 3710 | |
| 3711 | if (is_hijacked || !GetTarget().GetDebugger().IsHandlingEvents()) |
| 3712 | PopProcessIOHandler(); |
| 3713 | } |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
| 3716 | BroadcastEvent(event_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3717 | } else { |
| 3718 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3719 | LLDB_LOGF( |
| 3720 | log, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3721 | "Process::%s (pid = %" PRIu64 |
| 3722 | ") suppressing state %s (old state %s): should_broadcast == false", |
| 3723 | __FUNCTION__, GetID(), StateAsCString(new_state), |
| 3724 | StateAsCString(GetState())); |
| 3725 | } |
| 3726 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3729 | Status Process::HaltPrivate() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3730 | EventSP event_sp; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3731 | Status error(WillHalt()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3732 | if (error.Fail()) |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3733 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3734 | |
| 3735 | // Ask the process subclass to actually halt our process |
| 3736 | bool caused_stop; |
| 3737 | error = DoHalt(caused_stop); |
| 3738 | |
| 3739 | DidHalt(); |
| 3740 | return error; |
Jim Ingham | 8af3b9c | 2013-03-29 01:18:12 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3743 | thread_result_t Process::PrivateStateThread(void *arg) { |
| 3744 | std::unique_ptr<PrivateStateThreadArgs> args_up( |
| 3745 | static_cast<PrivateStateThreadArgs *>(arg)); |
| 3746 | thread_result_t result = |
| 3747 | args_up->process->RunPrivateStateThread(args_up->is_secondary_thread); |
| 3748 | return result; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3751 | thread_result_t Process::RunPrivateStateThread(bool is_secondary_thread) { |
| 3752 | bool control_only = true; |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3753 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3754 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3755 | LLDB_LOGF(log, "Process::%s (arg = %p, pid = %" PRIu64 ") thread starting...", |
| 3756 | __FUNCTION__, static_cast<void *>(this), GetID()); |
Jason Molenda | ede3193 | 2015-04-17 05:01:58 +0000 | [diff] [blame] | 3757 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3758 | bool exit_now = false; |
| 3759 | bool interrupt_requested = false; |
| 3760 | while (!exit_now) { |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3761 | EventSP event_sp; |
Pavel Labath | e3e21cf | 2016-11-30 11:56:32 +0000 | [diff] [blame] | 3762 | GetEventsPrivate(event_sp, llvm::None, control_only); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3763 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3764 | LLDB_LOGF(log, |
| 3765 | "Process::%s (arg = %p, pid = %" PRIu64 |
| 3766 | ") got a control event: %d", |
| 3767 | __FUNCTION__, static_cast<void *>(this), GetID(), |
| 3768 | event_sp->GetType()); |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3769 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3770 | switch (event_sp->GetType()) { |
| 3771 | case eBroadcastInternalStateControlStop: |
| 3772 | exit_now = true; |
| 3773 | break; // doing any internal state management below |
Pavel Labath | 19da1f1 | 2015-12-07 12:36:52 +0000 | [diff] [blame] | 3774 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3775 | case eBroadcastInternalStateControlPause: |
| 3776 | control_only = true; |
| 3777 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3778 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3779 | case eBroadcastInternalStateControlResume: |
| 3780 | control_only = false; |
| 3781 | break; |
| 3782 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3783 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3784 | continue; |
| 3785 | } else if (event_sp->GetType() == eBroadcastBitInterrupt) { |
| 3786 | if (m_public_state.GetValue() == eStateAttaching) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3787 | LLDB_LOGF(log, |
| 3788 | "Process::%s (arg = %p, pid = %" PRIu64 |
| 3789 | ") woke up with an interrupt while attaching - " |
| 3790 | "forwarding interrupt.", |
| 3791 | __FUNCTION__, static_cast<void *>(this), GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3792 | BroadcastEvent(eBroadcastBitInterrupt, nullptr); |
| 3793 | } else if (StateIsRunningState(m_last_broadcast_state)) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3794 | LLDB_LOGF(log, |
| 3795 | "Process::%s (arg = %p, pid = %" PRIu64 |
| 3796 | ") woke up with an interrupt - Halting.", |
| 3797 | __FUNCTION__, static_cast<void *>(this), GetID()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3798 | Status error = HaltPrivate(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3799 | if (error.Fail() && log) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3800 | LLDB_LOGF(log, |
| 3801 | "Process::%s (arg = %p, pid = %" PRIu64 |
| 3802 | ") failed to halt the process: %s", |
| 3803 | __FUNCTION__, static_cast<void *>(this), GetID(), |
| 3804 | error.AsCString()); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3805 | // Halt should generate a stopped event. Make a note of the fact that |
| 3806 | // we were doing the interrupt, so we can set the interrupted flag |
| 3807 | // after we receive the event. We deliberately set this to true even if |
| 3808 | // HaltPrivate failed, so that we can interrupt on the next natural |
| 3809 | // stop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3810 | interrupt_requested = true; |
| 3811 | } else { |
| 3812 | // 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] | 3813 | // running and sends an interrupt request, but the process actually |
| 3814 | // stops before we receive it. In that case, we can just ignore the |
| 3815 | // request. We use m_last_broadcast_state, because the Stopped event |
| 3816 | // may not have been popped of the event queue yet, which is when the |
| 3817 | // public state gets updated. |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3818 | LLDB_LOGF(log, |
| 3819 | "Process::%s ignoring interrupt as we have already stopped.", |
| 3820 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3821 | } |
| 3822 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3825 | const StateType internal_state = |
| 3826 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3827 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3828 | if (internal_state != eStateInvalid) { |
| 3829 | if (m_clear_thread_plans_on_stop && |
| 3830 | StateIsStoppedState(internal_state, true)) { |
| 3831 | m_clear_thread_plans_on_stop = false; |
| 3832 | m_thread_list.DiscardThreadPlans(); |
| 3833 | } |
| 3834 | |
| 3835 | if (interrupt_requested) { |
| 3836 | if (StateIsStoppedState(internal_state, true)) { |
| 3837 | // 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] | 3838 | // so clients can tell an interrupted process from a natural stop |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3839 | ProcessEventData::SetInterruptedInEvent(event_sp.get(), true); |
| 3840 | interrupt_requested = false; |
| 3841 | } else if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3842 | LLDB_LOGF(log, |
| 3843 | "Process::%s interrupt_requested, but a non-stopped " |
| 3844 | "state '%s' received.", |
| 3845 | __FUNCTION__, StateAsCString(internal_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3846 | } |
| 3847 | } |
| 3848 | |
| 3849 | HandlePrivateEvent(event_sp); |
| 3850 | } |
| 3851 | |
| 3852 | if (internal_state == eStateInvalid || internal_state == eStateExited || |
| 3853 | internal_state == eStateDetached) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3854 | LLDB_LOGF(log, |
| 3855 | "Process::%s (arg = %p, pid = %" PRIu64 |
| 3856 | ") about to exit with internal state %s...", |
| 3857 | __FUNCTION__, static_cast<void *>(this), GetID(), |
| 3858 | StateAsCString(internal_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3859 | |
| 3860 | break; |
| 3861 | } |
| 3862 | } |
| 3863 | |
| 3864 | // Verify log is still enabled before attempting to write to it... |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3865 | LLDB_LOGF(log, "Process::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", |
| 3866 | __FUNCTION__, static_cast<void *>(this), GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3867 | |
| 3868 | // 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] | 3869 | // will have already acquired the public_run_lock, and isn't done with what |
| 3870 | // 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] | 3871 | if (!is_secondary_thread) |
| 3872 | m_public_run_lock.SetStopped(); |
Jonas Devlieghere | edb52e2 | 2019-05-23 20:25:49 +0000 | [diff] [blame] | 3873 | return {}; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3876 | // Process Event Data |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3877 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3878 | Process::ProcessEventData::ProcessEventData() |
| 3879 | : EventData(), m_process_wp(), m_state(eStateInvalid), m_restarted(false), |
| 3880 | m_update_state(0), m_interrupted(false) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3881 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3882 | Process::ProcessEventData::ProcessEventData(const ProcessSP &process_sp, |
| 3883 | StateType state) |
| 3884 | : EventData(), m_process_wp(), m_state(state), m_restarted(false), |
| 3885 | m_update_state(0), m_interrupted(false) { |
| 3886 | if (process_sp) |
| 3887 | m_process_wp = process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 3890 | Process::ProcessEventData::~ProcessEventData() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3891 | |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 3892 | ConstString Process::ProcessEventData::GetFlavorString() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3893 | static ConstString g_flavor("Process::ProcessEventData"); |
| 3894 | return g_flavor; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 3897 | ConstString Process::ProcessEventData::GetFlavor() const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3898 | return ProcessEventData::GetFlavorString(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3901 | void Process::ProcessEventData::DoOnRemoval(Event *event_ptr) { |
| 3902 | ProcessSP process_sp(m_process_wp.lock()); |
Greg Clayton | 2e30907 | 2015-07-17 23:42:28 +0000 | [diff] [blame] | 3903 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3904 | if (!process_sp) |
| 3905 | return; |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 3906 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3907 | // 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] | 3908 | // pulled off of the private process event queue, and then any number of |
| 3909 | // times, first when it gets pulled off of the public event queue, then other |
| 3910 | // times when we're pretending that this is where we stopped at the end of |
| 3911 | // expression evaluation. m_update_state is used to distinguish these three |
| 3912 | // cases; it is 0 when we're just pulling it off for private handling, and > |
| 3913 | // 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] | 3914 | // command handling then. |
| 3915 | if (m_update_state != 1) |
| 3916 | return; |
| 3917 | |
| 3918 | process_sp->SetPublicState( |
| 3919 | m_state, Process::ProcessEventData::GetRestartedFromEvent(event_ptr)); |
| 3920 | |
Jason Molenda | b3a3cd1 | 2016-12-08 06:27:29 +0000 | [diff] [blame] | 3921 | if (m_state == eStateStopped && !m_restarted) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3922 | // Let process subclasses know we are about to do a public stop and do |
| 3923 | // anything they might need to in order to speed up register and memory |
| 3924 | // accesses. |
Jason Molenda | b3a3cd1 | 2016-12-08 06:27:29 +0000 | [diff] [blame] | 3925 | process_sp->WillPublicStop(); |
| 3926 | } |
| 3927 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3928 | // 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] | 3929 | // than a plain interrupt (e.g. we had already stopped for a breakpoint when |
| 3930 | // 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] | 3931 | // end up restarting the process. |
| 3932 | if (m_interrupted) |
| 3933 | return; |
| 3934 | |
| 3935 | // If we're stopped and haven't restarted, then do the StopInfo actions here: |
| 3936 | if (m_state == eStateStopped && !m_restarted) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3937 | ThreadList &curr_thread_list = process_sp->GetThreadList(); |
| 3938 | uint32_t num_threads = curr_thread_list.GetSize(); |
| 3939 | uint32_t idx; |
| 3940 | |
| 3941 | // The actions might change one of the thread's stop_info's opinions about |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3942 | // whether we should stop the process, so we need to query that as we go. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3943 | |
| 3944 | // One other complication here, is that we try to catch any case where the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3945 | // target has run (except for expressions) and immediately exit, but if we |
| 3946 | // get that wrong (which is possible) then the thread list might have |
| 3947 | // changed, and that would cause our iteration here to crash. We could |
| 3948 | // make a copy of the thread list, but we'd really like to also know if it |
| 3949 | // has changed at all, so we make up a vector of the thread ID's and check |
| 3950 | // what we get back against this list & bag out if anything differs. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3951 | std::vector<uint32_t> thread_index_array(num_threads); |
| 3952 | for (idx = 0; idx < num_threads; ++idx) |
| 3953 | thread_index_array[idx] = |
| 3954 | curr_thread_list.GetThreadAtIndex(idx)->GetIndexID(); |
| 3955 | |
| 3956 | // Use this to track whether we should continue from here. We will only |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3957 | // continue the target running if no thread says we should stop. Of course |
| 3958 | // if some thread's PerformAction actually sets the target running, then it |
| 3959 | // doesn't matter what the other threads say... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3960 | |
| 3961 | bool still_should_stop = false; |
| 3962 | |
| 3963 | // Sometimes - for instance if we have a bug in the stub we are talking to, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3964 | // we stop but no thread has a valid stop reason. In that case we should |
| 3965 | // just stop, because we have no way of telling what the right thing to do |
| 3966 | // is, and it's better to let the user decide than continue behind their |
| 3967 | // backs. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3968 | |
| 3969 | bool does_anybody_have_an_opinion = false; |
| 3970 | |
| 3971 | for (idx = 0; idx < num_threads; ++idx) { |
| 3972 | curr_thread_list = process_sp->GetThreadList(); |
| 3973 | if (curr_thread_list.GetSize() != num_threads) { |
| 3974 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | |
| 3975 | LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3976 | LLDB_LOGF( |
| 3977 | log, |
| 3978 | "Number of threads changed from %u to %u while processing event.", |
| 3979 | num_threads, curr_thread_list.GetSize()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3980 | break; |
| 3981 | } |
| 3982 | |
| 3983 | lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx); |
| 3984 | |
| 3985 | if (thread_sp->GetIndexID() != thread_index_array[idx]) { |
| 3986 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | |
| 3987 | LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 3988 | LLDB_LOGF(log, |
| 3989 | "The thread at position %u changed from %u to %u while " |
| 3990 | "processing event.", |
| 3991 | idx, thread_index_array[idx], thread_sp->GetIndexID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3992 | break; |
| 3993 | } |
| 3994 | |
| 3995 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo(); |
| 3996 | if (stop_info_sp && stop_info_sp->IsValid()) { |
| 3997 | does_anybody_have_an_opinion = true; |
| 3998 | bool this_thread_wants_to_stop; |
| 3999 | if (stop_info_sp->GetOverrideShouldStop()) { |
| 4000 | this_thread_wants_to_stop = |
| 4001 | stop_info_sp->GetOverriddenShouldStopValue(); |
| 4002 | } else { |
| 4003 | stop_info_sp->PerformAction(event_ptr); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4004 | // The stop action might restart the target. If it does, then we |
| 4005 | // want to mark that in the event so that whoever is receiving it |
| 4006 | // will know to wait for the running event and reflect that state |
| 4007 | // appropriately. We also need to stop processing actions, since they |
| 4008 | // aren't expecting the target to be running. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4009 | |
| 4010 | // FIXME: we might have run. |
| 4011 | if (stop_info_sp->HasTargetRunSinceMe()) { |
| 4012 | SetRestarted(true); |
| 4013 | break; |
| 4014 | } |
| 4015 | |
| 4016 | this_thread_wants_to_stop = stop_info_sp->ShouldStop(event_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4017 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4018 | |
| 4019 | if (!still_should_stop) |
| 4020 | still_should_stop = this_thread_wants_to_stop; |
| 4021 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4022 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4023 | |
| 4024 | if (!GetRestarted()) { |
| 4025 | if (!still_should_stop && does_anybody_have_an_opinion) { |
| 4026 | // We've been asked to continue, so do that here. |
| 4027 | SetRestarted(true); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4028 | // Use the public resume method here, since this is just extending a |
| 4029 | // public resume. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4030 | process_sp->PrivateResume(); |
| 4031 | } else { |
Jonas Devlieghere | edb52e2 | 2019-05-23 20:25:49 +0000 | [diff] [blame] | 4032 | bool hijacked = |
| 4033 | process_sp->IsHijackedForEvent(eBroadcastBitStateChanged) && |
| 4034 | !process_sp->StateChangedIsHijackedForSynchronousResume(); |
Jim Ingham | 3139fc9 | 2019-03-01 18:13:38 +0000 | [diff] [blame] | 4035 | |
| 4036 | if (!hijacked) { |
| 4037 | // If we didn't restart, run the Stop Hooks here. |
| 4038 | // Don't do that if state changed events aren't hooked up to the |
Jonas Devlieghere | edb52e2 | 2019-05-23 20:25:49 +0000 | [diff] [blame] | 4039 | // public (or SyncResume) broadcasters. StopHooks are just for |
| 4040 | // real public stops. They might also restart the target, |
Jim Ingham | 3139fc9 | 2019-03-01 18:13:38 +0000 | [diff] [blame] | 4041 | // so watch for that. |
| 4042 | process_sp->GetTarget().RunStopHooks(); |
| 4043 | if (process_sp->GetPrivateState() == eStateRunning) |
| 4044 | SetRestarted(true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4045 | } |
| 4046 | } |
| 4047 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4048 | } |
Zachary Turner | 805e710 | 2019-03-04 21:51:03 +0000 | [diff] [blame] | 4049 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4050 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4051 | void Process::ProcessEventData::Dump(Stream *s) const { |
| 4052 | ProcessSP process_sp(m_process_wp.lock()); |
Greg Clayton | aeb3b8b | 2015-05-29 03:20:37 +0000 | [diff] [blame] | 4053 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4054 | if (process_sp) |
| 4055 | s->Printf(" process = %p (pid = %" PRIu64 "), ", |
| 4056 | static_cast<void *>(process_sp.get()), process_sp->GetID()); |
| 4057 | else |
| 4058 | s->PutCString(" process = NULL, "); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4059 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4060 | s->Printf("state = %s", StateAsCString(GetState())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4061 | } |
| 4062 | |
| 4063 | const Process::ProcessEventData * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4064 | Process::ProcessEventData::GetEventDataFromEvent(const Event *event_ptr) { |
| 4065 | if (event_ptr) { |
| 4066 | const EventData *event_data = event_ptr->GetData(); |
| 4067 | if (event_data && |
| 4068 | event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 4069 | return static_cast<const ProcessEventData *>(event_ptr->GetData()); |
| 4070 | } |
| 4071 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | ProcessSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4075 | Process::ProcessEventData::GetProcessFromEvent(const Event *event_ptr) { |
| 4076 | ProcessSP process_sp; |
| 4077 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4078 | if (data) |
| 4079 | process_sp = data->GetProcessSP(); |
| 4080 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4083 | StateType Process::ProcessEventData::GetStateFromEvent(const Event *event_ptr) { |
| 4084 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4085 | if (data == nullptr) |
| 4086 | return eStateInvalid; |
| 4087 | else |
| 4088 | return data->GetState(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4091 | bool Process::ProcessEventData::GetRestartedFromEvent(const Event *event_ptr) { |
| 4092 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4093 | if (data == nullptr) |
| 4094 | return false; |
| 4095 | else |
| 4096 | return data->GetRestarted(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4099 | void Process::ProcessEventData::SetRestartedInEvent(Event *event_ptr, |
| 4100 | bool new_value) { |
| 4101 | ProcessEventData *data = |
| 4102 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4103 | if (data != nullptr) |
| 4104 | data->SetRestarted(new_value); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4107 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4108 | Process::ProcessEventData::GetNumRestartedReasons(const Event *event_ptr) { |
| 4109 | ProcessEventData *data = |
| 4110 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4111 | if (data != nullptr) |
| 4112 | return data->GetNumRestartedReasons(); |
| 4113 | else |
| 4114 | return 0; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | const char * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4118 | Process::ProcessEventData::GetRestartedReasonAtIndex(const Event *event_ptr, |
| 4119 | size_t idx) { |
| 4120 | ProcessEventData *data = |
| 4121 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4122 | if (data != nullptr) |
| 4123 | return data->GetRestartedReasonAtIndex(idx); |
| 4124 | else |
| 4125 | return nullptr; |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4128 | void Process::ProcessEventData::AddRestartedReason(Event *event_ptr, |
| 4129 | const char *reason) { |
| 4130 | ProcessEventData *data = |
| 4131 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4132 | if (data != nullptr) |
| 4133 | data->AddRestartedReason(reason); |
Jim Ingham | 0161b49 | 2013-02-09 01:29:05 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4136 | bool Process::ProcessEventData::GetInterruptedFromEvent( |
| 4137 | const Event *event_ptr) { |
| 4138 | const ProcessEventData *data = GetEventDataFromEvent(event_ptr); |
| 4139 | if (data == nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4140 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4141 | else |
| 4142 | return data->GetInterrupted(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4145 | void Process::ProcessEventData::SetInterruptedInEvent(Event *event_ptr, |
| 4146 | bool new_value) { |
| 4147 | ProcessEventData *data = |
| 4148 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4149 | if (data != nullptr) |
| 4150 | data->SetInterrupted(new_value); |
Greg Clayton | d9e416c | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4153 | bool Process::ProcessEventData::SetUpdateStateOnRemoval(Event *event_ptr) { |
| 4154 | ProcessEventData *data = |
| 4155 | const_cast<ProcessEventData *>(GetEventDataFromEvent(event_ptr)); |
| 4156 | if (data) { |
| 4157 | data->SetUpdateStateOnRemoval(); |
| 4158 | return true; |
| 4159 | } |
| 4160 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
Jim Ingham | b87b9e6 | 2018-06-26 23:38:58 +0000 | [diff] [blame] | 4163 | lldb::TargetSP Process::CalculateTarget() { return m_target_wp.lock(); } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4164 | |
| 4165 | void Process::CalculateExecutionContext(ExecutionContext &exe_ctx) { |
| 4166 | exe_ctx.SetTargetPtr(&GetTarget()); |
| 4167 | exe_ctx.SetProcessPtr(this); |
| 4168 | exe_ctx.SetThreadPtr(nullptr); |
| 4169 | exe_ctx.SetFramePtr(nullptr); |
| 4170 | } |
| 4171 | |
| 4172 | // uint32_t |
| 4173 | // Process::ListProcessesMatchingName (const char *name, StringList &matches, |
| 4174 | // std::vector<lldb::pid_t> &pids) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4175 | //{ |
| 4176 | // return 0; |
| 4177 | //} |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4178 | // |
| 4179 | // ArchSpec |
| 4180 | // Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4181 | //{ |
| 4182 | // return Host::GetArchSpecForExistingProcess (pid); |
| 4183 | //} |
| 4184 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4185 | // ArchSpec |
| 4186 | // Process::GetArchSpecForExistingProcess (const char *process_name) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 4187 | //{ |
| 4188 | // return Host::GetArchSpecForExistingProcess (process_name); |
| 4189 | //} |
Eugene Zelenko | da8cf8a | 2016-03-01 00:55:51 +0000 | [diff] [blame] | 4190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4191 | void Process::AppendSTDOUT(const char *s, size_t len) { |
| 4192 | std::lock_guard<std::recursive_mutex> guard(m_stdio_communication_mutex); |
| 4193 | m_stdout_data.append(s, len); |
| 4194 | BroadcastEventIfUnique(eBroadcastBitSTDOUT, |
| 4195 | new ProcessEventData(shared_from_this(), GetState())); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4198 | void Process::AppendSTDERR(const char *s, size_t len) { |
| 4199 | std::lock_guard<std::recursive_mutex> guard(m_stdio_communication_mutex); |
| 4200 | m_stderr_data.append(s, len); |
| 4201 | BroadcastEventIfUnique(eBroadcastBitSTDERR, |
| 4202 | new ProcessEventData(shared_from_this(), GetState())); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4205 | void Process::BroadcastAsyncProfileData(const std::string &one_profile_data) { |
| 4206 | std::lock_guard<std::recursive_mutex> guard(m_profile_data_comm_mutex); |
| 4207 | m_profile_data.push_back(one_profile_data); |
| 4208 | BroadcastEventIfUnique(eBroadcastBitProfileData, |
| 4209 | new ProcessEventData(shared_from_this(), GetState())); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4210 | } |
| 4211 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4212 | void Process::BroadcastStructuredData(const StructuredData::ObjectSP &object_sp, |
| 4213 | const StructuredDataPluginSP &plugin_sp) { |
| 4214 | BroadcastEvent( |
| 4215 | eBroadcastBitStructuredData, |
| 4216 | new EventDataStructuredData(shared_from_this(), object_sp, plugin_sp)); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
| 4219 | StructuredDataPluginSP |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 4220 | Process::GetStructuredDataPlugin(ConstString type_name) const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4221 | auto find_it = m_structured_data_plugin_map.find(type_name); |
| 4222 | if (find_it != m_structured_data_plugin_map.end()) |
| 4223 | return find_it->second; |
| 4224 | else |
| 4225 | return StructuredDataPluginSP(); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 4226 | } |
| 4227 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4228 | size_t Process::GetAsyncProfileData(char *buf, size_t buf_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4229 | std::lock_guard<std::recursive_mutex> guard(m_profile_data_comm_mutex); |
| 4230 | if (m_profile_data.empty()) |
| 4231 | return 0; |
| 4232 | |
| 4233 | std::string &one_profile_data = m_profile_data.front(); |
| 4234 | size_t bytes_available = one_profile_data.size(); |
| 4235 | if (bytes_available > 0) { |
| 4236 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4237 | LLDB_LOGF(log, "Process::GetProfileData (buf = %p, size = %" PRIu64 ")", |
| 4238 | static_cast<void *>(buf), static_cast<uint64_t>(buf_size)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4239 | if (bytes_available > buf_size) { |
| 4240 | memcpy(buf, one_profile_data.c_str(), buf_size); |
| 4241 | one_profile_data.erase(0, buf_size); |
| 4242 | bytes_available = buf_size; |
| 4243 | } else { |
| 4244 | memcpy(buf, one_profile_data.c_str(), bytes_available); |
| 4245 | m_profile_data.erase(m_profile_data.begin()); |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4246 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4247 | } |
| 4248 | return bytes_available; |
Han Ming Ong | ab3b8b2 | 2012-11-17 00:21:04 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4251 | // Process STDIO |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4252 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4253 | size_t Process::GetSTDOUT(char *buf, size_t buf_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4254 | std::lock_guard<std::recursive_mutex> guard(m_stdio_communication_mutex); |
| 4255 | size_t bytes_available = m_stdout_data.size(); |
| 4256 | if (bytes_available > 0) { |
| 4257 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4258 | LLDB_LOGF(log, "Process::GetSTDOUT (buf = %p, size = %" PRIu64 ")", |
| 4259 | static_cast<void *>(buf), static_cast<uint64_t>(buf_size)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4260 | if (bytes_available > buf_size) { |
| 4261 | memcpy(buf, m_stdout_data.c_str(), buf_size); |
| 4262 | m_stdout_data.erase(0, buf_size); |
| 4263 | bytes_available = buf_size; |
| 4264 | } else { |
| 4265 | memcpy(buf, m_stdout_data.c_str(), bytes_available); |
| 4266 | m_stdout_data.clear(); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4267 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4268 | } |
| 4269 | return bytes_available; |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4272 | size_t Process::GetSTDERR(char *buf, size_t buf_size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4273 | std::lock_guard<std::recursive_mutex> gaurd(m_stdio_communication_mutex); |
| 4274 | size_t bytes_available = m_stderr_data.size(); |
| 4275 | if (bytes_available > 0) { |
| 4276 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4277 | LLDB_LOGF(log, "Process::GetSTDERR (buf = %p, size = %" PRIu64 ")", |
| 4278 | static_cast<void *>(buf), static_cast<uint64_t>(buf_size)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4279 | if (bytes_available > buf_size) { |
| 4280 | memcpy(buf, m_stderr_data.c_str(), buf_size); |
| 4281 | m_stderr_data.erase(0, buf_size); |
| 4282 | bytes_available = buf_size; |
| 4283 | } else { |
| 4284 | memcpy(buf, m_stderr_data.c_str(), bytes_available); |
| 4285 | m_stderr_data.clear(); |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4286 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4287 | } |
| 4288 | return bytes_available; |
Greg Clayton | 93e8619 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4291 | void Process::STDIOReadThreadBytesReceived(void *baton, const void *src, |
| 4292 | size_t src_len) { |
| 4293 | Process *process = (Process *)baton; |
| 4294 | process->AppendSTDOUT(static_cast<const char *>(src), src_len); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4297 | class IOHandlerProcessSTDIO : public IOHandler { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4298 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4299 | IOHandlerProcessSTDIO(Process *process, int write_fd) |
| 4300 | : IOHandler(process->GetTarget().GetDebugger(), |
| 4301 | IOHandler::Type::ProcessIO), |
Sam McCall | 6f43d9d | 2016-11-15 10:58:16 +0000 | [diff] [blame] | 4302 | m_process(process), m_write_file(write_fd, false) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4303 | m_pipe.CreateNew(false); |
| 4304 | m_read_file.SetDescriptor(GetInputFD(), false); |
| 4305 | } |
| 4306 | |
| 4307 | ~IOHandlerProcessSTDIO() override = default; |
| 4308 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4309 | // Each IOHandler gets to run until it is done. It should read data from the |
| 4310 | // "in" and place output into "out" and "err and return when done. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4311 | void Run() override { |
| 4312 | if (!m_read_file.IsValid() || !m_write_file.IsValid() || |
| 4313 | !m_pipe.CanRead() || !m_pipe.CanWrite()) { |
| 4314 | SetIsDone(true); |
| 4315 | return; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4318 | SetIsDone(false); |
| 4319 | const int read_fd = m_read_file.GetDescriptor(); |
| 4320 | TerminalState terminal_state; |
| 4321 | terminal_state.Save(read_fd, false); |
| 4322 | Terminal terminal(read_fd); |
| 4323 | terminal.SetCanonical(false); |
| 4324 | terminal.SetEcho(false); |
Deepak Panickal | 914b8d9 | 2014-01-31 18:48:46 +0000 | [diff] [blame] | 4325 | // FD_ZERO, FD_SET are not supported on windows |
Hafiz Abid Qadeer | 6eff101 | 2014-03-12 10:45:23 +0000 | [diff] [blame] | 4326 | #ifndef _WIN32 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4327 | const int pipe_read_fd = m_pipe.GetReadFileDescriptor(); |
| 4328 | m_is_running = true; |
| 4329 | while (!GetIsDone()) { |
| 4330 | SelectHelper select_helper; |
| 4331 | select_helper.FDSetRead(read_fd); |
| 4332 | select_helper.FDSetRead(pipe_read_fd); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4333 | Status error = select_helper.Select(); |
Greg Clayton | 860582f | 2016-02-26 17:36:44 +0000 | [diff] [blame] | 4334 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4335 | if (error.Fail()) { |
Greg Clayton | 860582f | 2016-02-26 17:36:44 +0000 | [diff] [blame] | 4336 | SetIsDone(true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4337 | } else { |
| 4338 | char ch = 0; |
| 4339 | size_t n; |
| 4340 | if (select_helper.FDIsSetRead(read_fd)) { |
| 4341 | n = 1; |
| 4342 | if (m_read_file.Read(&ch, n).Success() && n == 1) { |
| 4343 | if (m_write_file.Write(&ch, n).Fail() || n != 1) |
| 4344 | SetIsDone(true); |
| 4345 | } else |
| 4346 | SetIsDone(true); |
Greg Clayton | 860582f | 2016-02-26 17:36:44 +0000 | [diff] [blame] | 4347 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4348 | if (select_helper.FDIsSetRead(pipe_read_fd)) { |
| 4349 | size_t bytes_read; |
| 4350 | // Consume the interrupt byte |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4351 | Status error = m_pipe.Read(&ch, 1, bytes_read); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4352 | if (error.Success()) { |
| 4353 | switch (ch) { |
| 4354 | case 'q': |
| 4355 | SetIsDone(true); |
| 4356 | break; |
| 4357 | case 'i': |
| 4358 | if (StateIsRunningState(m_process->GetState())) |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 4359 | m_process->SendAsyncInterrupt(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4360 | break; |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 4361 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4362 | } |
Greg Clayton | 0fdd3ae | 2014-07-16 21:05:41 +0000 | [diff] [blame] | 4363 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4364 | } |
Greg Clayton | e68f5d6 | 2014-02-24 22:50:57 +0000 | [diff] [blame] | 4365 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4366 | m_is_running = false; |
| 4367 | #endif |
| 4368 | terminal_state.Restore(); |
| 4369 | } |
| 4370 | |
| 4371 | void Cancel() override { |
| 4372 | SetIsDone(true); |
| 4373 | // Only write to our pipe to cancel if we are in |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4374 | // IOHandlerProcessSTDIO::Run(). We can end up with a python command that |
| 4375 | // is being run from the command interpreter: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4376 | // |
| 4377 | // (lldb) step_process_thousands_of_times |
| 4378 | // |
| 4379 | // In this case the command interpreter will be in the middle of handling |
| 4380 | // the command and if the process pushes and pops the IOHandler thousands |
| 4381 | // of times, we can end up writing to m_pipe without ever consuming the |
| 4382 | // bytes from the pipe in IOHandlerProcessSTDIO::Run() and end up |
| 4383 | // deadlocking when the pipe gets fed up and blocks until data is consumed. |
| 4384 | if (m_is_running) { |
| 4385 | char ch = 'q'; // Send 'q' for quit |
| 4386 | size_t bytes_written = 0; |
| 4387 | m_pipe.Write(&ch, 1, bytes_written); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4388 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
| 4391 | bool Interrupt() override { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4392 | // Do only things that are safe to do in an interrupt context (like in a |
| 4393 | // SIGINT handler), like write 1 byte to a file descriptor. This will |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4394 | // interrupt the IOHandlerProcessSTDIO::Run() and we can look at the byte |
| 4395 | // that was written to the pipe and then call |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4396 | // m_process->SendAsyncInterrupt() from a much safer location in code. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4397 | if (m_active) { |
| 4398 | char ch = 'i'; // Send 'i' for interrupt |
| 4399 | size_t bytes_written = 0; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4400 | Status result = m_pipe.Write(&ch, 1, bytes_written); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4401 | return result.Success(); |
| 4402 | } else { |
| 4403 | // This IOHandler might be pushed on the stack, but not being run |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4404 | // currently so do the right thing if we aren't actively watching for |
| 4405 | // STDIN by sending the interrupt to the process. Otherwise the write to |
| 4406 | // the pipe above would do nothing. This can happen when the command |
| 4407 | // interpreter is running and gets a "expression ...". It will be on the |
| 4408 | // IOHandler thread and sending the input is complete to the delegate |
| 4409 | // which will cause the expression to run, which will push the process IO |
| 4410 | // handler, but not run it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4411 | |
| 4412 | if (StateIsRunningState(m_process->GetState())) { |
| 4413 | m_process->SendAsyncInterrupt(); |
| 4414 | return true; |
| 4415 | } |
| 4416 | } |
| 4417 | return false; |
| 4418 | } |
| 4419 | |
| 4420 | void GotEOF() override {} |
| 4421 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4422 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4423 | Process *m_process; |
| 4424 | File m_read_file; // Read from this file (usually actual STDIN for LLDB |
| 4425 | File m_write_file; // Write to this file (usually the master pty for getting |
| 4426 | // io to debuggee) |
| 4427 | Pipe m_pipe; |
Sam McCall | 6f43d9d | 2016-11-15 10:58:16 +0000 | [diff] [blame] | 4428 | std::atomic<bool> m_is_running{false}; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 4429 | }; |
| 4430 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4431 | void Process::SetSTDIOFileDescriptor(int fd) { |
| 4432 | // First set up the Read Thread for reading/handling process I/O |
| 4433 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 4434 | std::unique_ptr<ConnectionFileDescriptor> conn_up( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4435 | new ConnectionFileDescriptor(fd, true)); |
| 4436 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 4437 | if (conn_up) { |
| 4438 | m_stdio_communication.SetConnection(conn_up.release()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4439 | if (m_stdio_communication.IsConnected()) { |
| 4440 | m_stdio_communication.SetReadThreadBytesReceivedCallback( |
| 4441 | STDIOReadThreadBytesReceived, this); |
| 4442 | m_stdio_communication.StartReadThread(); |
| 4443 | |
| 4444 | // Now read thread is set up, set up input reader. |
| 4445 | |
| 4446 | if (!m_process_input_reader) |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 4447 | m_process_input_reader = |
| 4448 | std::make_shared<IOHandlerProcessSTDIO>(this, fd); |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4449 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4450 | } |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4453 | bool Process::ProcessIOHandlerIsActive() { |
| 4454 | IOHandlerSP io_handler_sp(m_process_input_reader); |
| 4455 | if (io_handler_sp) |
| 4456 | return GetTarget().GetDebugger().IsTopIOHandler(io_handler_sp); |
| 4457 | return false; |
Greg Clayton | 6fea17e | 2014-03-03 19:15:20 +0000 | [diff] [blame] | 4458 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4459 | bool Process::PushProcessIOHandler() { |
| 4460 | IOHandlerSP io_handler_sp(m_process_input_reader); |
| 4461 | if (io_handler_sp) { |
| 4462 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4463 | LLDB_LOGF(log, "Process::%s pushing IO handler", __FUNCTION__); |
Pavel Labath | 4446487 | 2015-05-27 12:40:32 +0000 | [diff] [blame] | 4464 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4465 | io_handler_sp->SetIsDone(false); |
Raphael Isemann | c01783a | 2018-08-29 22:50:54 +0000 | [diff] [blame] | 4466 | // If we evaluate an utility function, then we don't cancel the current |
| 4467 | // IOHandler. Our IOHandler is non-interactive and shouldn't disturb the |
| 4468 | // existing IOHandler that potentially provides the user interface (e.g. |
| 4469 | // the IOHandler for Editline). |
| 4470 | bool cancel_top_handler = !m_mod_id.IsRunningUtilityFunction(); |
| 4471 | GetTarget().GetDebugger().PushIOHandler(io_handler_sp, cancel_top_handler); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4472 | return true; |
| 4473 | } |
| 4474 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4475 | } |
| 4476 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4477 | bool Process::PopProcessIOHandler() { |
| 4478 | IOHandlerSP io_handler_sp(m_process_input_reader); |
| 4479 | if (io_handler_sp) |
| 4480 | return GetTarget().GetDebugger().PopIOHandler(io_handler_sp); |
| 4481 | return false; |
Caroline Tice | ef5c6d0 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 4484 | // The process needs to know about installed plug-ins |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4485 | void Process::SettingsInitialize() { Thread::SettingsInitialize(); } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4486 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4487 | void Process::SettingsTerminate() { Thread::SettingsTerminate(); } |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4488 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4489 | namespace { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4490 | // RestorePlanState is used to record the "is private", "is master" and "okay |
| 4491 | // to discard" fields of the plan we are running, and reset it on Clean or on |
| 4492 | // destruction. It will only reset the state once, so you can call Clean and |
| 4493 | // 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] | 4494 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4495 | class RestorePlanState { |
| 4496 | public: |
| 4497 | RestorePlanState(lldb::ThreadPlanSP thread_plan_sp) |
| 4498 | : m_thread_plan_sp(thread_plan_sp), m_already_reset(false) { |
| 4499 | if (m_thread_plan_sp) { |
| 4500 | m_private = m_thread_plan_sp->GetPrivate(); |
| 4501 | m_is_master = m_thread_plan_sp->IsMasterPlan(); |
| 4502 | m_okay_to_discard = m_thread_plan_sp->OkayToDiscard(); |
| 4503 | } |
| 4504 | } |
| 4505 | |
| 4506 | ~RestorePlanState() { Clean(); } |
| 4507 | |
| 4508 | void Clean() { |
| 4509 | if (!m_already_reset && m_thread_plan_sp) { |
| 4510 | m_already_reset = true; |
| 4511 | m_thread_plan_sp->SetPrivate(m_private); |
| 4512 | m_thread_plan_sp->SetIsMasterPlan(m_is_master); |
| 4513 | m_thread_plan_sp->SetOkayToDiscard(m_okay_to_discard); |
| 4514 | } |
| 4515 | } |
| 4516 | |
| 4517 | private: |
| 4518 | lldb::ThreadPlanSP m_thread_plan_sp; |
| 4519 | bool m_already_reset; |
| 4520 | bool m_private; |
| 4521 | bool m_is_master; |
| 4522 | bool m_okay_to_discard; |
| 4523 | }; |
Eugene Zelenko | 8f30a65 | 2015-10-23 18:39:37 +0000 | [diff] [blame] | 4524 | } // anonymous namespace |
Jim Ingham | c60963c | 2015-10-12 19:11:03 +0000 | [diff] [blame] | 4525 | |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4526 | static microseconds |
| 4527 | GetOneThreadExpressionTimeout(const EvaluateExpressionOptions &options) { |
| 4528 | const milliseconds default_one_thread_timeout(250); |
| 4529 | |
| 4530 | // 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] | 4531 | if (!options.GetTimeout()) { |
| 4532 | return options.GetOneThreadTimeout() ? *options.GetOneThreadTimeout() |
| 4533 | : default_one_thread_timeout; |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | // If the one thread timeout is set, use it. |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4537 | if (options.GetOneThreadTimeout()) |
| 4538 | return *options.GetOneThreadTimeout(); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4539 | |
| 4540 | // Otherwise use half the total timeout, bounded by the |
| 4541 | // default_one_thread_timeout. |
| 4542 | return std::min<microseconds>(default_one_thread_timeout, |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4543 | *options.GetTimeout() / 2); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | static Timeout<std::micro> |
| 4547 | GetExpressionTimeout(const EvaluateExpressionOptions &options, |
| 4548 | bool before_first_timeout) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4549 | // If we are going to run all threads the whole time, or if we are only going |
| 4550 | // to run one thread, we can just return the overall timeout. |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4551 | if (!options.GetStopOthers() || !options.GetTryAllThreads()) |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4552 | return options.GetTimeout(); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4553 | |
| 4554 | if (before_first_timeout) |
| 4555 | return GetOneThreadExpressionTimeout(options); |
| 4556 | |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4557 | if (!options.GetTimeout()) |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4558 | return llvm::None; |
| 4559 | else |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4560 | return *options.GetTimeout() - GetOneThreadExpressionTimeout(options); |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 4563 | static llvm::Optional<ExpressionResults> |
| 4564 | HandleStoppedEvent(Thread &thread, const ThreadPlanSP &thread_plan_sp, |
| 4565 | RestorePlanState &restorer, const EventSP &event_sp, |
| 4566 | EventSP &event_to_broadcast_sp, |
| 4567 | const EvaluateExpressionOptions &options, bool handle_interrupts) { |
| 4568 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS); |
| 4569 | |
| 4570 | ThreadPlanSP plan = thread.GetCompletedPlan(); |
| 4571 | if (plan == thread_plan_sp && plan->PlanSucceeded()) { |
| 4572 | LLDB_LOG(log, "execution completed successfully"); |
| 4573 | |
| 4574 | // Restore the plan state so it will get reported as intended when we are |
| 4575 | // done. |
| 4576 | restorer.Clean(); |
| 4577 | return eExpressionCompleted; |
| 4578 | } |
| 4579 | |
| 4580 | StopInfoSP stop_info_sp = thread.GetStopInfo(); |
| 4581 | if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint && |
| 4582 | stop_info_sp->ShouldNotify(event_sp.get())) { |
| 4583 | LLDB_LOG(log, "stopped for breakpoint: {0}.", stop_info_sp->GetDescription()); |
| 4584 | if (!options.DoesIgnoreBreakpoints()) { |
| 4585 | // Restore the plan state and then force Private to false. We are going |
| 4586 | // 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] | 4587 | // it won't report correctly when we continue to its termination later |
| 4588 | // on. |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 4589 | restorer.Clean(); |
| 4590 | thread_plan_sp->SetPrivate(false); |
| 4591 | event_to_broadcast_sp = event_sp; |
| 4592 | } |
| 4593 | return eExpressionHitBreakpoint; |
| 4594 | } |
| 4595 | |
| 4596 | if (!handle_interrupts && |
| 4597 | Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get())) |
| 4598 | return llvm::None; |
| 4599 | |
| 4600 | LLDB_LOG(log, "thread plan did not successfully complete"); |
| 4601 | if (!options.DoesUnwindOnError()) |
| 4602 | event_to_broadcast_sp = event_sp; |
| 4603 | return eExpressionInterrupted; |
| 4604 | } |
| 4605 | |
Jim Ingham | 1624a2d | 2014-05-05 02:26:40 +0000 | [diff] [blame] | 4606 | ExpressionResults |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4607 | Process::RunThreadPlan(ExecutionContext &exe_ctx, |
| 4608 | lldb::ThreadPlanSP &thread_plan_sp, |
| 4609 | const EvaluateExpressionOptions &options, |
| 4610 | DiagnosticManager &diagnostic_manager) { |
| 4611 | ExpressionResults return_value = eExpressionSetupError; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4612 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4613 | std::lock_guard<std::mutex> run_thread_plan_locker(m_run_thread_plan_lock); |
| 4614 | |
| 4615 | if (!thread_plan_sp) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4616 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4617 | eDiagnosticSeverityError, |
| 4618 | "RunThreadPlan called with empty thread plan."); |
| 4619 | return eExpressionSetupError; |
| 4620 | } |
| 4621 | |
| 4622 | if (!thread_plan_sp->ValidatePlan(nullptr)) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4623 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4624 | eDiagnosticSeverityError, |
| 4625 | "RunThreadPlan called with an invalid thread plan."); |
| 4626 | return eExpressionSetupError; |
| 4627 | } |
| 4628 | |
| 4629 | if (exe_ctx.GetProcessPtr() != this) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4630 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 4631 | "RunThreadPlan called on wrong process."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4632 | return eExpressionSetupError; |
| 4633 | } |
| 4634 | |
| 4635 | Thread *thread = exe_ctx.GetThreadPtr(); |
| 4636 | if (thread == nullptr) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4637 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 4638 | "RunThreadPlan called with invalid thread."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4639 | return eExpressionSetupError; |
| 4640 | } |
| 4641 | |
| 4642 | // 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] | 4643 | // runner. This will restore them when we are done: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4644 | |
| 4645 | RestorePlanState thread_plan_restorer(thread_plan_sp); |
| 4646 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4647 | // We rely on the thread plan we are running returning "PlanCompleted" if |
| 4648 | // when it successfully completes. For that to be true the plan can't be |
| 4649 | // private - since private plans suppress themselves in the GetCompletedPlan |
| 4650 | // call. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4651 | |
| 4652 | thread_plan_sp->SetPrivate(false); |
| 4653 | |
| 4654 | // 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] | 4655 | // when they are done we will end up asking the plan above us whether we |
| 4656 | // should stop, which may give the wrong answer. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4657 | |
| 4658 | thread_plan_sp->SetIsMasterPlan(true); |
| 4659 | thread_plan_sp->SetOkayToDiscard(false); |
| 4660 | |
Raphael Isemann | c01783a | 2018-08-29 22:50:54 +0000 | [diff] [blame] | 4661 | // If we are running some utility expression for LLDB, we now have to mark |
| 4662 | // this in the ProcesModID of this process. This RAII takes care of marking |
| 4663 | // and reverting the mark it once we are done running the expression. |
| 4664 | UtilityFunctionScope util_scope(options.IsForUtilityExpr() ? this : nullptr); |
| 4665 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4666 | if (m_private_state.GetValue() != eStateStopped) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4667 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4668 | eDiagnosticSeverityError, |
| 4669 | "RunThreadPlan called while the private state was not stopped."); |
| 4670 | return eExpressionSetupError; |
| 4671 | } |
| 4672 | |
| 4673 | // Save the thread & frame from the exe_ctx for restoration after we run |
| 4674 | const uint32_t thread_idx_id = thread->GetIndexID(); |
| 4675 | StackFrameSP selected_frame_sp = thread->GetSelectedFrame(); |
| 4676 | if (!selected_frame_sp) { |
| 4677 | thread->SetSelectedFrame(nullptr); |
| 4678 | selected_frame_sp = thread->GetSelectedFrame(); |
| 4679 | if (!selected_frame_sp) { |
| 4680 | diagnostic_manager.Printf( |
| 4681 | eDiagnosticSeverityError, |
| 4682 | "RunThreadPlan called without a selected frame on thread %d", |
| 4683 | thread_idx_id); |
| 4684 | return eExpressionSetupError; |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 4685 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4686 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4687 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4688 | // Make sure the timeout values make sense. The one thread timeout needs to |
| 4689 | // be smaller than the overall timeout. |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 4690 | if (options.GetOneThreadTimeout() && options.GetTimeout() && |
| 4691 | *options.GetTimeout() < *options.GetOneThreadTimeout()) { |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4692 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 4693 | "RunThreadPlan called with one thread " |
| 4694 | "timeout greater than total timeout"); |
| 4695 | return eExpressionSetupError; |
| 4696 | } |
| 4697 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4698 | StackID ctx_frame_id = selected_frame_sp->GetStackID(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4699 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4700 | // 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] | 4701 | // We don't want to do that either, so we should arrange to reset them as |
| 4702 | // well. |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4703 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4704 | lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4705 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4706 | uint32_t selected_tid; |
| 4707 | StackID selected_stack_id; |
| 4708 | if (selected_thread_sp) { |
| 4709 | selected_tid = selected_thread_sp->GetIndexID(); |
| 4710 | selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID(); |
| 4711 | } else { |
| 4712 | selected_tid = LLDB_INVALID_THREAD_ID; |
| 4713 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4714 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4715 | HostThread backup_private_state_thread; |
| 4716 | lldb::StateType old_state = eStateInvalid; |
| 4717 | lldb::ThreadPlanSP stopper_base_plan_sp; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 4718 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4719 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP | |
| 4720 | LIBLLDB_LOG_PROCESS)); |
| 4721 | if (m_private_state_thread.EqualsThread(Host::GetCurrentThread())) { |
| 4722 | // 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] | 4723 | // public events on this thread, since we are the thread that is generating |
| 4724 | // public events. The simplest thing to do is to spin up a temporary thread |
| 4725 | // to handle private state thread events while we are fielding public |
| 4726 | // events here. |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4727 | LLDB_LOGF(log, "Running thread plan on private state thread, spinning up " |
| 4728 | "another state thread to handle the events."); |
Todd Fiala | 76e0fc9 | 2014-08-27 22:58:26 +0000 | [diff] [blame] | 4729 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4730 | backup_private_state_thread = m_private_state_thread; |
Greg Clayton | 095eeaa | 2013-11-05 23:28:00 +0000 | [diff] [blame] | 4731 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4732 | // 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] | 4733 | // anything it pushes, and then stop, returning control here. But in the |
| 4734 | // normal course of things, the plan above us on the stack would be given a |
| 4735 | // shot at the stop event before deciding to stop, and we don't want that. |
| 4736 | // So we insert a "stopper" base plan on the stack before the plan we want |
| 4737 | // to run. Since base plans always stop and return control to the user, |
| 4738 | // that will do just what we want. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4739 | stopper_base_plan_sp.reset(new ThreadPlanBase(*thread)); |
| 4740 | thread->QueueThreadPlan(stopper_base_plan_sp, false); |
| 4741 | // Have to make sure our public state is stopped, since otherwise the |
| 4742 | // reporting logic below doesn't work correctly. |
| 4743 | old_state = m_public_state.GetValue(); |
| 4744 | m_public_state.SetValueNoLock(eStateStopped); |
| 4745 | |
| 4746 | // Now spin up the private state thread: |
| 4747 | StartPrivateStateThread(true); |
| 4748 | } |
| 4749 | |
| 4750 | thread->QueueThreadPlan( |
| 4751 | thread_plan_sp, false); // This used to pass "true" does that make sense? |
| 4752 | |
| 4753 | if (options.GetDebug()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4754 | // In this case, we aren't actually going to run, we just want to stop |
| 4755 | // right away. Flush this thread so we will refetch the stacks and show the |
| 4756 | // correct backtrace. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4757 | // FIXME: To make this prettier we should invent some stop reason for this, |
| 4758 | // but that |
| 4759 | // is only cosmetic, and this functionality is only of use to lldb |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4760 | // developers who can live with not pretty... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4761 | thread->Flush(); |
| 4762 | return eExpressionStoppedForDebug; |
| 4763 | } |
| 4764 | |
| 4765 | ListenerSP listener_sp( |
| 4766 | Listener::MakeListener("lldb.process.listener.run-thread-plan")); |
| 4767 | |
| 4768 | lldb::EventSP event_to_broadcast_sp; |
| 4769 | |
| 4770 | { |
| 4771 | // This process event hijacker Hijacks the Public events and its destructor |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4772 | // 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] | 4773 | // |
| 4774 | // 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] | 4775 | // exits during execution), then the event is put into |
| 4776 | // event_to_broadcast_sp for rebroadcasting. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4777 | |
| 4778 | ProcessEventHijacker run_thread_plan_hijacker(*this, listener_sp); |
| 4779 | |
| 4780 | if (log) { |
| 4781 | StreamString s; |
| 4782 | thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4783 | LLDB_LOGF(log, |
| 4784 | "Process::RunThreadPlan(): Resuming thread %u - 0x%4.4" PRIx64 |
| 4785 | " to run thread plan \"%s\".", |
| 4786 | thread->GetIndexID(), thread->GetID(), s.GetData()); |
Jim Ingham | 1460e4b | 2014-01-10 23:46:59 +0000 | [diff] [blame] | 4787 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4788 | |
| 4789 | bool got_event; |
| 4790 | lldb::EventSP event_sp; |
| 4791 | lldb::StateType stop_state = lldb::eStateInvalid; |
| 4792 | |
| 4793 | bool before_first_timeout = true; // This is set to false the first time |
| 4794 | // that we have to halt the target. |
| 4795 | bool do_resume = true; |
| 4796 | bool handle_running_event = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4797 | |
| 4798 | // This is just for accounting: |
| 4799 | uint32_t num_resumes = 0; |
| 4800 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4801 | // 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] | 4802 | // going to run one thread, then we don't need the first timeout. So we |
| 4803 | // pretend we are after the first timeout already. |
| 4804 | if (!options.GetStopOthers() || !options.GetTryAllThreads()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4805 | before_first_timeout = false; |
Andrew MacPherson | eb4d060 | 2014-03-13 09:37:02 +0000 | [diff] [blame] | 4806 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4807 | LLDB_LOGF(log, "Stop others: %u, try all: %u, before_first: %u.\n", |
| 4808 | options.GetStopOthers(), options.GetTryAllThreads(), |
| 4809 | before_first_timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4810 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4811 | // This isn't going to work if there are unfetched events on the queue. Are |
| 4812 | // there cases where we might want to run the remaining events here, and |
| 4813 | // then try to call the function? That's probably being too tricky for our |
| 4814 | // own good. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4815 | |
| 4816 | Event *other_events = listener_sp->PeekAtNextEvent(); |
| 4817 | if (other_events != nullptr) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4818 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4819 | eDiagnosticSeverityError, |
| 4820 | "RunThreadPlan called with pending events on the queue."); |
| 4821 | return eExpressionSetupError; |
Kuba Brecka | afdf842 | 2014-10-10 23:43:03 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4824 | // 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] | 4825 | // calling a function as part of a thread plan, in which case the last |
| 4826 | // delivered event could be the running event, and we don't want event |
| 4827 | // coalescing to cause us to lose OUR running event... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4828 | ForceNextEventDelivery(); |
Ryan Brown | 65d4d5c | 2015-09-16 21:20:44 +0000 | [diff] [blame] | 4829 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4830 | // 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] | 4831 | // when we are done. So don't call return anywhere within it. |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 4832 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4833 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4834 | // It's pretty much impossible to write test cases for things like: One |
| 4835 | // thread timeout expires, I go to halt, but the process already stopped on |
| 4836 | // the function call stop breakpoint. Turning on this define will make us |
| 4837 | // not fetch the first event till after the halt. So if you run a quick |
| 4838 | // function, it will have completed, and the completion event will be |
| 4839 | // waiting, when you interrupt for halt. The expression evaluation should |
| 4840 | // still succeed. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4841 | bool miss_first_event = true; |
| 4842 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4843 | while (true) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4844 | // We usually want to resume the process if we get to the top of the |
| 4845 | // loop. The only exception is if we get two running events with no |
| 4846 | // intervening stop, which can happen, we will just wait for then next |
| 4847 | // stop event. |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4848 | LLDB_LOGF(log, |
| 4849 | "Top of while loop: do_resume: %i handle_running_event: %i " |
| 4850 | "before_first_timeout: %i.", |
| 4851 | do_resume, handle_running_event, before_first_timeout); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 4852 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4853 | if (do_resume || handle_running_event) { |
| 4854 | // Do the initial resume and wait for the running event before going |
| 4855 | // further. |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 4856 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4857 | if (do_resume) { |
| 4858 | num_resumes++; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 4859 | Status resume_error = PrivateResume(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4860 | if (!resume_error.Success()) { |
| 4861 | diagnostic_manager.Printf( |
| 4862 | eDiagnosticSeverityError, |
| 4863 | "couldn't resume inferior the %d time: \"%s\".", num_resumes, |
| 4864 | resume_error.AsCString()); |
| 4865 | return_value = eExpressionSetupError; |
| 4866 | break; |
| 4867 | } |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 4868 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4869 | |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 4870 | got_event = |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 4871 | listener_sp->GetEvent(event_sp, GetUtilityExpressionTimeout()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4872 | if (!got_event) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4873 | LLDB_LOGF(log, |
| 4874 | "Process::RunThreadPlan(): didn't get any event after " |
| 4875 | "resume %" PRIu32 ", exiting.", |
| 4876 | num_resumes); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4877 | |
| 4878 | diagnostic_manager.Printf(eDiagnosticSeverityError, |
| 4879 | "didn't get any event after resume %" PRIu32 |
| 4880 | ", exiting.", |
| 4881 | num_resumes); |
| 4882 | return_value = eExpressionSetupError; |
| 4883 | break; |
| 4884 | } |
| 4885 | |
| 4886 | stop_state = |
| 4887 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 4888 | |
| 4889 | if (stop_state != eStateRunning) { |
| 4890 | bool restarted = false; |
| 4891 | |
| 4892 | if (stop_state == eStateStopped) { |
| 4893 | restarted = Process::ProcessEventData::GetRestartedFromEvent( |
| 4894 | event_sp.get()); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4895 | LLDB_LOGF( |
| 4896 | log, |
| 4897 | "Process::RunThreadPlan(): didn't get running event after " |
| 4898 | "resume %d, got %s instead (restarted: %i, do_resume: %i, " |
| 4899 | "handle_running_event: %i).", |
| 4900 | num_resumes, StateAsCString(stop_state), restarted, do_resume, |
| 4901 | handle_running_event); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | if (restarted) { |
| 4905 | // This is probably an overabundance of caution, I don't think I |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4906 | // should ever get a stopped & restarted event here. But if I do, |
| 4907 | // the best thing is to Halt and then get out of here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4908 | const bool clear_thread_plans = false; |
| 4909 | const bool use_run_lock = false; |
| 4910 | Halt(clear_thread_plans, use_run_lock); |
| 4911 | } |
| 4912 | |
| 4913 | diagnostic_manager.Printf( |
| 4914 | eDiagnosticSeverityError, |
| 4915 | "didn't get running event after initial resume, got %s instead.", |
| 4916 | StateAsCString(stop_state)); |
| 4917 | return_value = eExpressionSetupError; |
| 4918 | break; |
| 4919 | } |
| 4920 | |
| 4921 | if (log) |
| 4922 | log->PutCString("Process::RunThreadPlan(): resuming succeeded."); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4923 | // We need to call the function synchronously, so spin waiting for it |
| 4924 | // to return. If we get interrupted while executing, we're going to |
| 4925 | // lose our context, and won't be able to gather the result at this |
| 4926 | // point. We set the timeout AFTER the resume, since the resume takes |
| 4927 | // 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] | 4928 | } else { |
| 4929 | if (log) |
| 4930 | log->PutCString("Process::RunThreadPlan(): waiting for next event."); |
| 4931 | } |
| 4932 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4933 | do_resume = true; |
| 4934 | handle_running_event = true; |
| 4935 | |
| 4936 | // Now wait for the process to stop again: |
| 4937 | event_sp.reset(); |
| 4938 | |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4939 | Timeout<std::micro> timeout = |
| 4940 | GetExpressionTimeout(options, before_first_timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4941 | if (log) { |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4942 | if (timeout) { |
| 4943 | auto now = system_clock::now(); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4944 | LLDB_LOGF(log, |
| 4945 | "Process::RunThreadPlan(): about to wait - now is %s - " |
| 4946 | "endpoint is %s", |
| 4947 | llvm::to_string(now).c_str(), |
| 4948 | llvm::to_string(now + *timeout).c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4949 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4950 | LLDB_LOGF(log, "Process::RunThreadPlan(): about to wait forever."); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 4951 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | #ifdef LLDB_RUN_THREAD_HALT_WITH_EVENT |
| 4955 | // See comment above... |
| 4956 | if (miss_first_event) { |
| 4957 | usleep(1000); |
| 4958 | miss_first_event = false; |
| 4959 | got_event = false; |
| 4960 | } else |
| 4961 | #endif |
Pavel Labath | 2ce2216 | 2016-12-01 10:57:30 +0000 | [diff] [blame] | 4962 | got_event = listener_sp->GetEvent(event_sp, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4963 | |
| 4964 | if (got_event) { |
| 4965 | if (event_sp) { |
| 4966 | bool keep_going = false; |
| 4967 | if (event_sp->GetType() == eBroadcastBitInterrupt) { |
| 4968 | const bool clear_thread_plans = false; |
| 4969 | const bool use_run_lock = false; |
| 4970 | Halt(clear_thread_plans, use_run_lock); |
| 4971 | return_value = eExpressionInterrupted; |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 4972 | diagnostic_manager.PutString(eDiagnosticSeverityRemark, |
| 4973 | "execution halted by user interrupt."); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4974 | LLDB_LOGF(log, "Process::RunThreadPlan(): Got interrupted by " |
| 4975 | "eBroadcastBitInterrupted, exiting."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4976 | break; |
| 4977 | } else { |
| 4978 | stop_state = |
| 4979 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4980 | LLDB_LOGF(log, |
| 4981 | "Process::RunThreadPlan(): in while loop, got event: %s.", |
| 4982 | StateAsCString(stop_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4983 | |
| 4984 | switch (stop_state) { |
| 4985 | case lldb::eStateStopped: { |
| 4986 | // We stopped, figure out what we are going to do now. |
| 4987 | ThreadSP thread_sp = |
| 4988 | GetThreadList().FindThreadByIndexID(thread_idx_id); |
| 4989 | if (!thread_sp) { |
| 4990 | // Ooh, our thread has vanished. Unlikely that this was |
| 4991 | // successful execution... |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 4992 | LLDB_LOGF(log, |
| 4993 | "Process::RunThreadPlan(): execution completed " |
| 4994 | "but our thread (index-id=%u) has vanished.", |
| 4995 | thread_idx_id); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4996 | return_value = eExpressionInterrupted; |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 4997 | } else if (Process::ProcessEventData::GetRestartedFromEvent( |
| 4998 | event_sp.get())) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4999 | // If we were restarted, we just need to go back up to fetch |
| 5000 | // another event. |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5001 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5002 | LLDB_LOGF(log, "Process::RunThreadPlan(): Got a stop and " |
| 5003 | "restart, so we'll continue waiting."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5004 | } |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5005 | keep_going = true; |
| 5006 | do_resume = false; |
| 5007 | handle_running_event = true; |
| 5008 | } else { |
| 5009 | const bool handle_interrupts = true; |
| 5010 | return_value = *HandleStoppedEvent( |
| 5011 | *thread, thread_plan_sp, thread_plan_restorer, event_sp, |
| 5012 | event_to_broadcast_sp, options, handle_interrupts); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5013 | } |
| 5014 | } break; |
| 5015 | |
| 5016 | case lldb::eStateRunning: |
| 5017 | // This shouldn't really happen, but sometimes we do get two |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5018 | // running events without an intervening stop, and in that case |
| 5019 | // we should just go back to waiting for the stop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5020 | do_resume = false; |
| 5021 | keep_going = true; |
| 5022 | handle_running_event = false; |
| 5023 | break; |
| 5024 | |
| 5025 | default: |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5026 | LLDB_LOGF(log, |
| 5027 | "Process::RunThreadPlan(): execution stopped with " |
| 5028 | "unexpected state: %s.", |
| 5029 | StateAsCString(stop_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5030 | |
| 5031 | if (stop_state == eStateExited) |
| 5032 | event_to_broadcast_sp = event_sp; |
| 5033 | |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 5034 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5035 | eDiagnosticSeverityError, |
| 5036 | "execution stopped with unexpected state."); |
| 5037 | return_value = eExpressionInterrupted; |
| 5038 | break; |
| 5039 | } |
| 5040 | } |
| 5041 | |
| 5042 | if (keep_going) |
| 5043 | continue; |
| 5044 | else |
| 5045 | break; |
| 5046 | } else { |
| 5047 | if (log) |
| 5048 | log->PutCString("Process::RunThreadPlan(): got_event was true, but " |
| 5049 | "the event pointer was null. How odd..."); |
| 5050 | return_value = eExpressionInterrupted; |
| 5051 | break; |
| 5052 | } |
| 5053 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5054 | // If we didn't get an event that means we've timed out... We will |
| 5055 | // interrupt the process here. Depending on what we were asked to do |
| 5056 | // we will either exit, or try with all threads running for the same |
| 5057 | // timeout. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5058 | |
| 5059 | if (log) { |
| 5060 | if (options.GetTryAllThreads()) { |
| 5061 | if (before_first_timeout) { |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 5062 | LLDB_LOG(log, |
| 5063 | "Running function with one thread timeout timed out."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5064 | } else |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 5065 | LLDB_LOG(log, "Restarting function with all threads enabled and " |
| 5066 | "timeout: {0} timed out, abandoning execution.", |
| 5067 | timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5068 | } else |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 5069 | LLDB_LOG(log, "Running function with timeout: {0} timed out, " |
| 5070 | "abandoning execution.", |
| 5071 | timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5072 | } |
| 5073 | |
| 5074 | // 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] | 5075 | // around to calling Halt the target could have stopped. That's fine, |
| 5076 | // Halt will figure that out and send the appropriate Stopped event. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5077 | // BUT it is also possible that we stopped & restarted (e.g. hit a |
| 5078 | // signal with "stop" set to false.) In |
| 5079 | // 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] | 5080 | // back to waiting for the Halt's stopped event. That's what this |
| 5081 | // while loop does. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5082 | |
| 5083 | bool back_to_top = true; |
| 5084 | uint32_t try_halt_again = 0; |
| 5085 | bool do_halt = true; |
| 5086 | const uint32_t num_retries = 5; |
| 5087 | while (try_halt_again < num_retries) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5088 | Status halt_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5089 | if (do_halt) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5090 | LLDB_LOGF(log, "Process::RunThreadPlan(): Running Halt."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5091 | const bool clear_thread_plans = false; |
| 5092 | const bool use_run_lock = false; |
| 5093 | Halt(clear_thread_plans, use_run_lock); |
| 5094 | } |
| 5095 | if (halt_error.Success()) { |
| 5096 | if (log) |
| 5097 | log->PutCString("Process::RunThreadPlan(): Halt succeeded."); |
| 5098 | |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 5099 | got_event = |
Adrian Prantl | 4c03ea1 | 2019-04-05 22:43:42 +0000 | [diff] [blame] | 5100 | listener_sp->GetEvent(event_sp, GetUtilityExpressionTimeout()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5101 | |
| 5102 | if (got_event) { |
| 5103 | stop_state = |
| 5104 | Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 5105 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5106 | LLDB_LOGF(log, |
| 5107 | "Process::RunThreadPlan(): Stopped with event: %s", |
| 5108 | StateAsCString(stop_state)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5109 | if (stop_state == lldb::eStateStopped && |
| 5110 | Process::ProcessEventData::GetInterruptedFromEvent( |
| 5111 | event_sp.get())) |
| 5112 | log->PutCString(" Event was the Halt interruption event."); |
| 5113 | } |
| 5114 | |
| 5115 | if (stop_state == lldb::eStateStopped) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5116 | if (Process::ProcessEventData::GetRestartedFromEvent( |
| 5117 | event_sp.get())) { |
| 5118 | if (log) |
| 5119 | log->PutCString("Process::RunThreadPlan(): Went to halt " |
| 5120 | "but got a restarted event, there must be " |
| 5121 | "an un-restarted stopped event so try " |
| 5122 | "again... " |
| 5123 | "Exiting wait loop."); |
| 5124 | try_halt_again++; |
| 5125 | do_halt = false; |
| 5126 | continue; |
| 5127 | } |
| 5128 | |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5129 | // Between the time we initiated the Halt and the time we |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5130 | // delivered it, the process could have already finished its |
| 5131 | // job. Check that here: |
Pavel Labath | 45dde23 | 2017-05-25 10:50:06 +0000 | [diff] [blame] | 5132 | const bool handle_interrupts = false; |
| 5133 | if (auto result = HandleStoppedEvent( |
| 5134 | *thread, thread_plan_sp, thread_plan_restorer, event_sp, |
| 5135 | event_to_broadcast_sp, options, handle_interrupts)) { |
| 5136 | return_value = *result; |
| 5137 | back_to_top = false; |
| 5138 | break; |
| 5139 | } |
| 5140 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5141 | if (!options.GetTryAllThreads()) { |
| 5142 | if (log) |
| 5143 | log->PutCString("Process::RunThreadPlan(): try_all_threads " |
| 5144 | "was false, we stopped so now we're " |
| 5145 | "quitting."); |
| 5146 | return_value = eExpressionInterrupted; |
| 5147 | back_to_top = false; |
| 5148 | break; |
| 5149 | } |
| 5150 | |
| 5151 | if (before_first_timeout) { |
| 5152 | // Set all the other threads to run, and return to the top of |
| 5153 | // the loop, which will continue; |
| 5154 | before_first_timeout = false; |
| 5155 | thread_plan_sp->SetStopOthers(false); |
| 5156 | if (log) |
| 5157 | log->PutCString( |
| 5158 | "Process::RunThreadPlan(): about to resume."); |
| 5159 | |
| 5160 | back_to_top = true; |
| 5161 | break; |
| 5162 | } else { |
| 5163 | // Running all threads failed, so return Interrupted. |
| 5164 | if (log) |
| 5165 | log->PutCString("Process::RunThreadPlan(): running all " |
| 5166 | "threads timed out."); |
| 5167 | return_value = eExpressionInterrupted; |
| 5168 | back_to_top = false; |
| 5169 | break; |
| 5170 | } |
| 5171 | } |
| 5172 | } else { |
| 5173 | if (log) |
| 5174 | log->PutCString("Process::RunThreadPlan(): halt said it " |
| 5175 | "succeeded, but I got no event. " |
| 5176 | "I'm getting out of here passing Interrupted."); |
| 5177 | return_value = eExpressionInterrupted; |
| 5178 | back_to_top = false; |
| 5179 | break; |
| 5180 | } |
| 5181 | } else { |
| 5182 | try_halt_again++; |
| 5183 | continue; |
| 5184 | } |
| 5185 | } |
| 5186 | |
| 5187 | if (!back_to_top || try_halt_again > num_retries) |
| 5188 | break; |
| 5189 | else |
| 5190 | continue; |
| 5191 | } |
| 5192 | } // END WAIT LOOP |
| 5193 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5194 | // If we had to start up a temporary private state thread to run this |
| 5195 | // thread plan, shut it down now. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5196 | if (backup_private_state_thread.IsJoinable()) { |
| 5197 | StopPrivateStateThread(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5198 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5199 | m_private_state_thread = backup_private_state_thread; |
| 5200 | if (stopper_base_plan_sp) { |
| 5201 | thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp); |
| 5202 | } |
| 5203 | if (old_state != eStateInvalid) |
| 5204 | m_public_state.SetValueNoLock(old_state); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5205 | } |
| 5206 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5207 | if (return_value != eExpressionCompleted && log) { |
| 5208 | // Print a backtrace into the log so we can figure out where we are: |
| 5209 | StreamString s; |
| 5210 | s.PutCString("Thread state after unsuccessful completion: \n"); |
| 5211 | thread->GetStackFrameStatus(s, 0, UINT32_MAX, true, UINT32_MAX); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 5212 | log->PutString(s.GetString()); |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5213 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5214 | // 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] | 5215 | // There are three cases where this could happen: 1) The execution |
| 5216 | // successfully completed 2) We hit a breakpoint, and ignore_breakpoints |
| 5217 | // 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] | 5218 | bool should_unwind = (return_value == eExpressionInterrupted && |
| 5219 | options.DoesUnwindOnError()) || |
| 5220 | (return_value == eExpressionHitBreakpoint && |
| 5221 | options.DoesIgnoreBreakpoints()); |
| 5222 | |
| 5223 | if (return_value == eExpressionCompleted || should_unwind) { |
| 5224 | thread_plan_sp->RestoreThreadState(); |
| 5225 | } |
| 5226 | |
| 5227 | // Now do some processing on the results of the run: |
| 5228 | if (return_value == eExpressionInterrupted || |
| 5229 | return_value == eExpressionHitBreakpoint) { |
| 5230 | if (log) { |
| 5231 | StreamString s; |
| 5232 | if (event_sp) |
| 5233 | event_sp->Dump(&s); |
| 5234 | else { |
| 5235 | log->PutCString("Process::RunThreadPlan(): Stop event that " |
| 5236 | "interrupted us is NULL."); |
| 5237 | } |
| 5238 | |
| 5239 | StreamString ts; |
| 5240 | |
| 5241 | const char *event_explanation = nullptr; |
| 5242 | |
| 5243 | do { |
| 5244 | if (!event_sp) { |
| 5245 | event_explanation = "<no event>"; |
| 5246 | break; |
| 5247 | } else if (event_sp->GetType() == eBroadcastBitInterrupt) { |
| 5248 | event_explanation = "<user interrupt>"; |
| 5249 | break; |
| 5250 | } else { |
| 5251 | const Process::ProcessEventData *event_data = |
| 5252 | Process::ProcessEventData::GetEventDataFromEvent( |
| 5253 | event_sp.get()); |
| 5254 | |
| 5255 | if (!event_data) { |
| 5256 | event_explanation = "<no event data>"; |
| 5257 | break; |
| 5258 | } |
| 5259 | |
| 5260 | Process *process = event_data->GetProcessSP().get(); |
| 5261 | |
| 5262 | if (!process) { |
| 5263 | event_explanation = "<no process>"; |
| 5264 | break; |
| 5265 | } |
| 5266 | |
| 5267 | ThreadList &thread_list = process->GetThreadList(); |
| 5268 | |
| 5269 | uint32_t num_threads = thread_list.GetSize(); |
| 5270 | uint32_t thread_index; |
| 5271 | |
| 5272 | ts.Printf("<%u threads> ", num_threads); |
| 5273 | |
| 5274 | for (thread_index = 0; thread_index < num_threads; ++thread_index) { |
| 5275 | Thread *thread = thread_list.GetThreadAtIndex(thread_index).get(); |
| 5276 | |
| 5277 | if (!thread) { |
| 5278 | ts.Printf("<?> "); |
| 5279 | continue; |
| 5280 | } |
| 5281 | |
| 5282 | ts.Printf("<0x%4.4" PRIx64 " ", thread->GetID()); |
| 5283 | RegisterContext *register_context = |
| 5284 | thread->GetRegisterContext().get(); |
| 5285 | |
| 5286 | if (register_context) |
| 5287 | ts.Printf("[ip 0x%" PRIx64 "] ", register_context->GetPC()); |
| 5288 | else |
| 5289 | ts.Printf("[ip unknown] "); |
| 5290 | |
| 5291 | // Show the private stop info here, the public stop info will be |
| 5292 | // from the last natural stop. |
| 5293 | lldb::StopInfoSP stop_info_sp = thread->GetPrivateStopInfo(); |
| 5294 | if (stop_info_sp) { |
| 5295 | const char *stop_desc = stop_info_sp->GetDescription(); |
| 5296 | if (stop_desc) |
| 5297 | ts.PutCString(stop_desc); |
| 5298 | } |
| 5299 | ts.Printf(">"); |
| 5300 | } |
| 5301 | |
| 5302 | event_explanation = ts.GetData(); |
| 5303 | } |
Jonas Devlieghere | 09ad8c8 | 2019-05-24 00:44:33 +0000 | [diff] [blame] | 5304 | } while (false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5305 | |
| 5306 | if (event_explanation) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5307 | LLDB_LOGF(log, |
| 5308 | "Process::RunThreadPlan(): execution interrupted: %s %s", |
| 5309 | s.GetData(), event_explanation); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5310 | else |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5311 | LLDB_LOGF(log, "Process::RunThreadPlan(): execution interrupted: %s", |
| 5312 | s.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5313 | } |
| 5314 | |
| 5315 | if (should_unwind) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5316 | LLDB_LOGF(log, |
| 5317 | "Process::RunThreadPlan: ExecutionInterrupted - " |
| 5318 | "discarding thread plans up to %p.", |
| 5319 | static_cast<void *>(thread_plan_sp.get())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5320 | thread->DiscardThreadPlansUpToPlan(thread_plan_sp); |
| 5321 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5322 | LLDB_LOGF(log, |
| 5323 | "Process::RunThreadPlan: ExecutionInterrupted - for " |
| 5324 | "plan: %p not discarding.", |
| 5325 | static_cast<void *>(thread_plan_sp.get())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5326 | } |
| 5327 | } else if (return_value == eExpressionSetupError) { |
| 5328 | if (log) |
| 5329 | log->PutCString("Process::RunThreadPlan(): execution set up error."); |
| 5330 | |
| 5331 | if (options.DoesUnwindOnError()) { |
| 5332 | thread->DiscardThreadPlansUpToPlan(thread_plan_sp); |
| 5333 | } |
| 5334 | } else { |
| 5335 | if (thread->IsThreadPlanDone(thread_plan_sp.get())) { |
| 5336 | if (log) |
| 5337 | log->PutCString("Process::RunThreadPlan(): thread plan is done"); |
| 5338 | return_value = eExpressionCompleted; |
| 5339 | } else if (thread->WasThreadPlanDiscarded(thread_plan_sp.get())) { |
| 5340 | if (log) |
| 5341 | log->PutCString( |
| 5342 | "Process::RunThreadPlan(): thread plan was discarded"); |
| 5343 | return_value = eExpressionDiscarded; |
| 5344 | } else { |
| 5345 | if (log) |
| 5346 | log->PutCString( |
| 5347 | "Process::RunThreadPlan(): thread plan stopped in mid course"); |
| 5348 | if (options.DoesUnwindOnError() && thread_plan_sp) { |
| 5349 | if (log) |
| 5350 | log->PutCString("Process::RunThreadPlan(): discarding thread plan " |
| 5351 | "'cause unwind_on_error is set."); |
| 5352 | thread->DiscardThreadPlansUpToPlan(thread_plan_sp); |
| 5353 | } |
| 5354 | } |
| 5355 | } |
| 5356 | |
| 5357 | // 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] | 5358 | // target Check that it's still there, and if it is put it back in the |
| 5359 | // 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] | 5360 | thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get(); |
| 5361 | if (thread) { |
| 5362 | exe_ctx.SetFrameSP(thread->GetFrameWithStackID(ctx_frame_id)); |
| 5363 | } |
| 5364 | |
| 5365 | // Also restore the current process'es selected frame & thread, since this |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5366 | // function calling may be done behind the user's back. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5367 | |
| 5368 | if (selected_tid != LLDB_INVALID_THREAD_ID) { |
| 5369 | if (GetThreadList().SetSelectedThreadByIndexID(selected_tid) && |
| 5370 | selected_stack_id.IsValid()) { |
| 5371 | // We were able to restore the selected thread, now restore the frame: |
| 5372 | std::lock_guard<std::recursive_mutex> guard(GetThreadList().GetMutex()); |
| 5373 | StackFrameSP old_frame_sp = |
| 5374 | GetThreadList().GetSelectedThread()->GetFrameWithStackID( |
| 5375 | selected_stack_id); |
| 5376 | if (old_frame_sp) |
| 5377 | GetThreadList().GetSelectedThread()->SetSelectedFrame( |
| 5378 | old_frame_sp.get()); |
| 5379 | } |
| 5380 | } |
| 5381 | } |
| 5382 | |
| 5383 | // If the process exited during the run of the thread plan, notify everyone. |
| 5384 | |
| 5385 | if (event_to_broadcast_sp) { |
| 5386 | if (log) |
| 5387 | log->PutCString("Process::RunThreadPlan(): rebroadcasting event."); |
| 5388 | BroadcastEvent(event_to_broadcast_sp); |
| 5389 | } |
| 5390 | |
| 5391 | return return_value; |
Jason Molenda | ef7d641 | 2015-08-06 03:27:10 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5394 | const char *Process::ExecutionResultAsCString(ExpressionResults result) { |
| 5395 | const char *result_name; |
| 5396 | |
| 5397 | switch (result) { |
| 5398 | case eExpressionCompleted: |
| 5399 | result_name = "eExpressionCompleted"; |
| 5400 | break; |
| 5401 | case eExpressionDiscarded: |
| 5402 | result_name = "eExpressionDiscarded"; |
| 5403 | break; |
| 5404 | case eExpressionInterrupted: |
| 5405 | result_name = "eExpressionInterrupted"; |
| 5406 | break; |
| 5407 | case eExpressionHitBreakpoint: |
| 5408 | result_name = "eExpressionHitBreakpoint"; |
| 5409 | break; |
| 5410 | case eExpressionSetupError: |
| 5411 | result_name = "eExpressionSetupError"; |
| 5412 | break; |
| 5413 | case eExpressionParseError: |
| 5414 | result_name = "eExpressionParseError"; |
| 5415 | break; |
| 5416 | case eExpressionResultUnavailable: |
| 5417 | result_name = "eExpressionResultUnavailable"; |
| 5418 | break; |
| 5419 | case eExpressionTimedOut: |
| 5420 | result_name = "eExpressionTimedOut"; |
| 5421 | break; |
| 5422 | case eExpressionStoppedForDebug: |
| 5423 | result_name = "eExpressionStoppedForDebug"; |
| 5424 | break; |
| 5425 | } |
| 5426 | return result_name; |
Jason Molenda | 484900b | 2015-08-10 07:55:25 +0000 | [diff] [blame] | 5427 | } |
| 5428 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5429 | void Process::GetStatus(Stream &strm) { |
| 5430 | const StateType state = GetState(); |
| 5431 | if (StateIsStoppedState(state, false)) { |
| 5432 | if (state == eStateExited) { |
| 5433 | int exit_status = GetExitStatus(); |
| 5434 | const char *exit_description = GetExitDescription(); |
| 5435 | strm.Printf("Process %" PRIu64 " exited with status = %i (0x%8.8x) %s\n", |
| 5436 | GetID(), exit_status, exit_status, |
| 5437 | exit_description ? exit_description : ""); |
| 5438 | } else { |
| 5439 | if (state == eStateConnected) |
| 5440 | strm.Printf("Connected to remote target.\n"); |
| 5441 | else |
| 5442 | strm.Printf("Process %" PRIu64 " %s\n", GetID(), StateAsCString(state)); |
| 5443 | } |
| 5444 | } else { |
| 5445 | strm.Printf("Process %" PRIu64 " is running.\n", GetID()); |
| 5446 | } |
Pavel Labath | a933d51 | 2016-04-05 13:07:16 +0000 | [diff] [blame] | 5447 | } |
| 5448 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5449 | size_t Process::GetThreadStatus(Stream &strm, |
| 5450 | bool only_threads_with_stop_reason, |
| 5451 | uint32_t start_frame, uint32_t num_frames, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 5452 | uint32_t num_frames_with_source, |
| 5453 | bool stop_format) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5454 | size_t num_thread_infos_dumped = 0; |
Greg Clayton | 35ca64b | 2015-04-16 17:13:34 +0000 | [diff] [blame] | 5455 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5456 | // 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] | 5457 | // very well might run code (e.g. if we need it to get return values or |
| 5458 | // arguments.) For that to work the process has to be able to acquire it. |
| 5459 | // 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] | 5460 | |
| 5461 | uint32_t num_threads; |
| 5462 | std::vector<lldb::tid_t> thread_id_array; |
| 5463 | // Scope for thread list locker; |
| 5464 | { |
| 5465 | std::lock_guard<std::recursive_mutex> guard(GetThreadList().GetMutex()); |
| 5466 | ThreadList &curr_thread_list = GetThreadList(); |
| 5467 | num_threads = curr_thread_list.GetSize(); |
| 5468 | uint32_t idx; |
| 5469 | thread_id_array.resize(num_threads); |
| 5470 | for (idx = 0; idx < num_threads; ++idx) |
| 5471 | thread_id_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetID(); |
| 5472 | } |
| 5473 | |
| 5474 | for (uint32_t i = 0; i < num_threads; i++) { |
| 5475 | ThreadSP thread_sp(GetThreadList().FindThreadByID(thread_id_array[i])); |
| 5476 | if (thread_sp) { |
| 5477 | if (only_threads_with_stop_reason) { |
| 5478 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo(); |
| 5479 | if (!stop_info_sp || !stop_info_sp->IsValid()) |
| 5480 | continue; |
| 5481 | } |
| 5482 | thread_sp->GetStatus(strm, start_frame, num_frames, |
Jim Ingham | 6a9767c | 2016-11-08 20:36:40 +0000 | [diff] [blame] | 5483 | num_frames_with_source, |
| 5484 | stop_format); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5485 | ++num_thread_infos_dumped; |
| 5486 | } else { |
| 5487 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5488 | LLDB_LOGF(log, "Process::GetThreadStatus - thread 0x" PRIu64 |
| 5489 | " vanished while running Thread::GetStatus."); |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 5490 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5491 | } |
| 5492 | return num_thread_infos_dumped; |
| 5493 | } |
| 5494 | |
| 5495 | void Process::AddInvalidMemoryRegion(const LoadRange ®ion) { |
| 5496 | m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 5497 | } |
| 5498 | |
| 5499 | bool Process::RemoveInvalidMemoryRange(const LoadRange ®ion) { |
| 5500 | return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), |
| 5501 | region.GetByteSize()); |
| 5502 | } |
| 5503 | |
| 5504 | void Process::AddPreResumeAction(PreResumeActionCallback callback, |
| 5505 | void *baton) { |
| 5506 | m_pre_resume_actions.push_back(PreResumeCallbackAndBaton(callback, baton)); |
| 5507 | } |
| 5508 | |
| 5509 | bool Process::RunPreResumeActions() { |
| 5510 | bool result = true; |
| 5511 | while (!m_pre_resume_actions.empty()) { |
| 5512 | struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back(); |
| 5513 | m_pre_resume_actions.pop_back(); |
| 5514 | bool this_result = action.callback(action.baton); |
| 5515 | if (result) |
| 5516 | result = this_result; |
| 5517 | } |
| 5518 | return result; |
| 5519 | } |
| 5520 | |
| 5521 | void Process::ClearPreResumeActions() { m_pre_resume_actions.clear(); } |
| 5522 | |
Jim Ingham | ffd9175 | 2016-10-20 22:50:00 +0000 | [diff] [blame] | 5523 | void Process::ClearPreResumeAction(PreResumeActionCallback callback, void *baton) |
| 5524 | { |
| 5525 | PreResumeCallbackAndBaton element(callback, baton); |
| 5526 | auto found_iter = std::find(m_pre_resume_actions.begin(), m_pre_resume_actions.end(), element); |
| 5527 | if (found_iter != m_pre_resume_actions.end()) |
| 5528 | { |
| 5529 | m_pre_resume_actions.erase(found_iter); |
| 5530 | } |
| 5531 | } |
| 5532 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5533 | ProcessRunLock &Process::GetRunLock() { |
| 5534 | if (m_private_state_thread.EqualsThread(Host::GetCurrentThread())) |
| 5535 | return m_private_run_lock; |
| 5536 | else |
| 5537 | return m_public_run_lock; |
| 5538 | } |
| 5539 | |
| 5540 | void Process::Flush() { |
| 5541 | m_thread_list.Flush(); |
| 5542 | m_extended_thread_list.Flush(); |
| 5543 | m_extended_thread_stop_id = 0; |
| 5544 | m_queue_list.Clear(); |
| 5545 | m_queue_list_stop_id = 0; |
| 5546 | } |
| 5547 | |
| 5548 | void Process::DidExec() { |
| 5549 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5550 | LLDB_LOGF(log, "Process::%s()", __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5551 | |
| 5552 | Target &target = GetTarget(); |
| 5553 | target.CleanupProcess(); |
| 5554 | target.ClearModules(false); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 5555 | m_dynamic_checkers_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5556 | m_abi_sp.reset(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 5557 | m_system_runtime_up.reset(); |
| 5558 | m_os_up.reset(); |
| 5559 | m_dyld_up.reset(); |
| 5560 | m_jit_loaders_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5561 | m_image_tokens.clear(); |
| 5562 | m_allocated_memory_cache.Clear(); |
Alex Langford | 74eb76f | 2019-05-22 23:01:18 +0000 | [diff] [blame] | 5563 | { |
| 5564 | std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex); |
| 5565 | m_language_runtimes.clear(); |
| 5566 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5567 | m_instrumentation_runtimes.clear(); |
| 5568 | m_thread_list.DiscardThreadPlans(); |
| 5569 | m_memory_cache.Clear(true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5570 | DoDidExec(); |
| 5571 | CompleteAttach(); |
| 5572 | // Flush the process (threads and all stack frames) after running |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5573 | // CompleteAttach() in case the dynamic loader loaded things in new |
| 5574 | // locations. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5575 | Flush(); |
| 5576 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5577 | // After we figure out what was loaded/unloaded in CompleteAttach, we need to |
| 5578 | // 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] | 5579 | target.DidExec(); |
| 5580 | } |
| 5581 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5582 | addr_t Process::ResolveIndirectFunction(const Address *address, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5583 | if (address == nullptr) { |
| 5584 | error.SetErrorString("Invalid address argument"); |
| 5585 | return LLDB_INVALID_ADDRESS; |
| 5586 | } |
| 5587 | |
| 5588 | addr_t function_addr = LLDB_INVALID_ADDRESS; |
| 5589 | |
| 5590 | addr_t addr = address->GetLoadAddress(&GetTarget()); |
| 5591 | std::map<addr_t, addr_t>::const_iterator iter = |
| 5592 | m_resolved_indirect_addresses.find(addr); |
| 5593 | if (iter != m_resolved_indirect_addresses.end()) { |
| 5594 | function_addr = (*iter).second; |
| 5595 | } else { |
| 5596 | if (!InferiorCall(this, address, function_addr)) { |
| 5597 | Symbol *symbol = address->CalculateSymbolContextSymbol(); |
| 5598 | error.SetErrorStringWithFormat( |
| 5599 | "Unable to call resolver for indirect function %s", |
| 5600 | symbol ? symbol->GetName().AsCString() : "<UNKNOWN>"); |
| 5601 | function_addr = LLDB_INVALID_ADDRESS; |
| 5602 | } else { |
| 5603 | m_resolved_indirect_addresses.insert( |
| 5604 | std::pair<addr_t, addr_t>(addr, function_addr)); |
| 5605 | } |
| 5606 | } |
| 5607 | return function_addr; |
| 5608 | } |
| 5609 | |
| 5610 | void Process::ModulesDidLoad(ModuleList &module_list) { |
| 5611 | SystemRuntime *sys_runtime = GetSystemRuntime(); |
| 5612 | if (sys_runtime) { |
| 5613 | sys_runtime->ModulesDidLoad(module_list); |
| 5614 | } |
| 5615 | |
| 5616 | GetJITLoaders().ModulesDidLoad(module_list); |
| 5617 | |
| 5618 | // Give runtimes a chance to be created. |
| 5619 | InstrumentationRuntime::ModulesDidLoad(module_list, this, |
| 5620 | m_instrumentation_runtimes); |
| 5621 | |
| 5622 | // Tell runtimes about new modules. |
| 5623 | for (auto pos = m_instrumentation_runtimes.begin(); |
| 5624 | pos != m_instrumentation_runtimes.end(); ++pos) { |
| 5625 | InstrumentationRuntimeSP runtime = pos->second; |
| 5626 | runtime->ModulesDidLoad(module_list); |
| 5627 | } |
| 5628 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5629 | // Let any language runtimes we have already created know about the modules |
| 5630 | // that loaded. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5631 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5632 | // Iterate over a copy of this language runtime list in case the language |
Leonard Mosescu | 9ba5157 | 2018-08-07 18:00:30 +0000 | [diff] [blame] | 5633 | // runtime ModulesDidLoad somehow causes the language runtime to be |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5634 | // unloaded. |
Alex Langford | 74eb76f | 2019-05-22 23:01:18 +0000 | [diff] [blame] | 5635 | { |
| 5636 | std::lock_guard<std::recursive_mutex> guard(m_language_runtimes_mutex); |
| 5637 | LanguageRuntimeCollection language_runtimes(m_language_runtimes); |
| 5638 | for (const auto &pair : language_runtimes) { |
| 5639 | // We must check language_runtime_sp to make sure it is not nullptr as we |
| 5640 | // might cache the fact that we didn't have a language runtime for a |
| 5641 | // language. |
| 5642 | LanguageRuntimeSP language_runtime_sp = pair.second; |
| 5643 | if (language_runtime_sp) |
| 5644 | language_runtime_sp->ModulesDidLoad(module_list); |
| 5645 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | // If we don't have an operating system plug-in, try to load one since |
| 5649 | // loading shared libraries might cause a new one to try and load |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 5650 | if (!m_os_up) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5651 | LoadOperatingSystemPlugin(false); |
| 5652 | |
| 5653 | // Give structured-data plugins a chance to see the modified modules. |
| 5654 | for (auto pair : m_structured_data_plugin_map) { |
| 5655 | if (pair.second) |
| 5656 | pair.second->ModulesDidLoad(*this, module_list); |
| 5657 | } |
| 5658 | } |
| 5659 | |
| 5660 | void Process::PrintWarning(uint64_t warning_type, const void *repeat_key, |
| 5661 | const char *fmt, ...) { |
| 5662 | bool print_warning = true; |
| 5663 | |
| 5664 | StreamSP stream_sp = GetTarget().GetDebugger().GetAsyncOutputStream(); |
| 5665 | if (!stream_sp) |
| 5666 | return; |
| 5667 | if (warning_type == eWarningsOptimization && !GetWarningsOptimization()) { |
| 5668 | return; |
| 5669 | } |
| 5670 | |
| 5671 | if (repeat_key != nullptr) { |
| 5672 | WarningsCollection::iterator it = m_warnings_issued.find(warning_type); |
| 5673 | if (it == m_warnings_issued.end()) { |
| 5674 | m_warnings_issued[warning_type] = WarningsPointerSet(); |
| 5675 | m_warnings_issued[warning_type].insert(repeat_key); |
| 5676 | } else { |
| 5677 | if (it->second.find(repeat_key) != it->second.end()) { |
| 5678 | print_warning = false; |
| 5679 | } else { |
| 5680 | it->second.insert(repeat_key); |
| 5681 | } |
| 5682 | } |
| 5683 | } |
| 5684 | |
| 5685 | if (print_warning) { |
| 5686 | va_list args; |
| 5687 | va_start(args, fmt); |
| 5688 | stream_sp->PrintfVarArg(fmt, args); |
| 5689 | va_end(args); |
| 5690 | } |
| 5691 | } |
| 5692 | |
| 5693 | void Process::PrintWarningOptimization(const SymbolContext &sc) { |
| 5694 | if (GetWarningsOptimization() && sc.module_sp && |
| 5695 | !sc.module_sp->GetFileSpec().GetFilename().IsEmpty() && sc.function && |
| 5696 | sc.function->GetIsOptimized()) { |
| 5697 | PrintWarning(Process::Warnings::eWarningsOptimization, sc.module_sp.get(), |
| 5698 | "%s was compiled with optimization - stepping may behave " |
| 5699 | "oddly; variables may not be available.\n", |
| 5700 | sc.module_sp->GetFileSpec().GetFilename().GetCString()); |
| 5701 | } |
| 5702 | } |
| 5703 | |
| 5704 | bool Process::GetProcessInfo(ProcessInstanceInfo &info) { |
| 5705 | info.Clear(); |
| 5706 | |
| 5707 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
| 5708 | if (!platform_sp) |
| 5709 | return false; |
| 5710 | |
| 5711 | return platform_sp->GetProcessInfo(GetID(), info); |
| 5712 | } |
| 5713 | |
| 5714 | ThreadCollectionSP Process::GetHistoryThreads(lldb::addr_t addr) { |
| 5715 | ThreadCollectionSP threads; |
| 5716 | |
| 5717 | const MemoryHistorySP &memory_history = |
| 5718 | MemoryHistory::FindPlugin(shared_from_this()); |
| 5719 | |
| 5720 | if (!memory_history) { |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 5721 | return threads; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5722 | } |
| 5723 | |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 5724 | threads = std::make_shared<ThreadCollection>( |
| 5725 | memory_history->GetHistoryThreads(addr)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5726 | |
| 5727 | return threads; |
Kuba Brecka | a51ea38 | 2014-09-06 01:33:13 +0000 | [diff] [blame] | 5728 | } |
Kuba Brecka | 6392754 | 2014-10-11 01:59:32 +0000 | [diff] [blame] | 5729 | |
| 5730 | InstrumentationRuntimeSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5731 | Process::GetInstrumentationRuntime(lldb::InstrumentationRuntimeType type) { |
| 5732 | InstrumentationRuntimeCollection::iterator pos; |
| 5733 | pos = m_instrumentation_runtimes.find(type); |
| 5734 | if (pos == m_instrumentation_runtimes.end()) { |
| 5735 | return InstrumentationRuntimeSP(); |
| 5736 | } else |
| 5737 | return (*pos).second; |
Kuba Brecka | 6392754 | 2014-10-11 01:59:32 +0000 | [diff] [blame] | 5738 | } |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 5739 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5740 | bool Process::GetModuleSpec(const FileSpec &module_file_spec, |
| 5741 | const ArchSpec &arch, ModuleSpec &module_spec) { |
| 5742 | module_spec.Clear(); |
| 5743 | return false; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 5744 | } |
Tamas Berghammer | 3cb132a | 2015-12-02 11:58:51 +0000 | [diff] [blame] | 5745 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5746 | size_t Process::AddImageToken(lldb::addr_t image_ptr) { |
| 5747 | m_image_tokens.push_back(image_ptr); |
| 5748 | return m_image_tokens.size() - 1; |
Tamas Berghammer | 3cb132a | 2015-12-02 11:58:51 +0000 | [diff] [blame] | 5749 | } |
| 5750 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5751 | lldb::addr_t Process::GetImagePtrFromToken(size_t token) const { |
| 5752 | if (token < m_image_tokens.size()) |
| 5753 | return m_image_tokens[token]; |
| 5754 | return LLDB_INVALID_IMAGE_TOKEN; |
Tamas Berghammer | 3cb132a | 2015-12-02 11:58:51 +0000 | [diff] [blame] | 5755 | } |
| 5756 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5757 | void Process::ResetImageToken(size_t token) { |
| 5758 | if (token < m_image_tokens.size()) |
| 5759 | m_image_tokens[token] = LLDB_INVALID_IMAGE_TOKEN; |
Enrico Granata | f3129cb | 2015-12-03 23:53:45 +0000 | [diff] [blame] | 5760 | } |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5761 | |
| 5762 | Address |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5763 | Process::AdvanceAddressToNextBranchInstruction(Address default_stop_addr, |
| 5764 | AddressRange range_bounds) { |
| 5765 | Target &target = GetTarget(); |
| 5766 | DisassemblerSP disassembler_sp; |
| 5767 | InstructionList *insn_list = nullptr; |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5768 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5769 | Address retval = default_stop_addr; |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5770 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5771 | if (!target.GetUseFastStepping()) |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5772 | return retval; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5773 | if (!default_stop_addr.IsValid()) |
| 5774 | return retval; |
| 5775 | |
| 5776 | ExecutionContext exe_ctx(this); |
| 5777 | const char *plugin_name = nullptr; |
| 5778 | const char *flavor = nullptr; |
| 5779 | const bool prefer_file_cache = true; |
| 5780 | disassembler_sp = Disassembler::DisassembleRange( |
| 5781 | target.GetArchitecture(), plugin_name, flavor, exe_ctx, range_bounds, |
| 5782 | prefer_file_cache); |
| 5783 | if (disassembler_sp) |
| 5784 | insn_list = &disassembler_sp->GetInstructionList(); |
| 5785 | |
| 5786 | if (insn_list == nullptr) { |
| 5787 | return retval; |
| 5788 | } |
| 5789 | |
| 5790 | size_t insn_offset = |
| 5791 | insn_list->GetIndexOfInstructionAtAddress(default_stop_addr); |
| 5792 | if (insn_offset == UINT32_MAX) { |
| 5793 | return retval; |
| 5794 | } |
| 5795 | |
| 5796 | uint32_t branch_index = |
Greg Clayton | df22576 | 2019-05-09 20:39:34 +0000 | [diff] [blame] | 5797 | insn_list->GetIndexOfNextBranchInstruction(insn_offset, target, |
| 5798 | false /* ignore_calls*/); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5799 | if (branch_index == UINT32_MAX) { |
| 5800 | return retval; |
| 5801 | } |
| 5802 | |
| 5803 | if (branch_index > insn_offset) { |
| 5804 | Address next_branch_insn_address = |
| 5805 | insn_list->GetInstructionAtIndex(branch_index)->GetAddress(); |
| 5806 | if (next_branch_insn_address.IsValid() && |
| 5807 | range_bounds.ContainsFileAddress(next_branch_insn_address)) { |
| 5808 | retval = next_branch_insn_address; |
| 5809 | } |
| 5810 | } |
| 5811 | |
| 5812 | return retval; |
Jason Molenda | fd4cea5 | 2016-01-08 21:40:11 +0000 | [diff] [blame] | 5813 | } |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 5814 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5815 | Status |
Tatyana Krasnukha | 36788bb | 2018-12-20 15:02:58 +0000 | [diff] [blame] | 5816 | Process::GetMemoryRegions(lldb_private::MemoryRegionInfos ®ion_list) { |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 5817 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5818 | Status error; |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 5819 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5820 | lldb::addr_t range_end = 0; |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 5821 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5822 | region_list.clear(); |
| 5823 | do { |
Tatyana Krasnukha | 36788bb | 2018-12-20 15:02:58 +0000 | [diff] [blame] | 5824 | lldb_private::MemoryRegionInfo region_info; |
| 5825 | error = GetMemoryRegionInfo(range_end, region_info); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5826 | // GetMemoryRegionInfo should only return an error if it is unimplemented. |
| 5827 | if (error.Fail()) { |
| 5828 | region_list.clear(); |
| 5829 | break; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5830 | } |
| 5831 | |
Tatyana Krasnukha | 36788bb | 2018-12-20 15:02:58 +0000 | [diff] [blame] | 5832 | range_end = region_info.GetRange().GetRangeEnd(); |
| 5833 | if (region_info.GetMapped() == MemoryRegionInfo::eYes) { |
| 5834 | region_list.push_back(std::move(region_info)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5835 | } |
| 5836 | } while (range_end != LLDB_INVALID_ADDRESS); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5837 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5838 | return error; |
| 5839 | } |
| 5840 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5841 | Status |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 5842 | Process::ConfigureStructuredData(ConstString type_name, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5843 | const StructuredData::ObjectSP &config_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5844 | // If you get this, the Process-derived class needs to implement a method to |
| 5845 | // enable an already-reported asynchronous structured data feature. See |
| 5846 | // ProcessGDBRemote for an example implementation over gdb-remote. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5847 | return Status("unimplemented"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5848 | } |
| 5849 | |
| 5850 | void Process::MapSupportedStructuredDataPlugins( |
| 5851 | const StructuredData::Array &supported_type_names) { |
| 5852 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 5853 | |
| 5854 | // Bail out early if there are no type names to map. |
| 5855 | if (supported_type_names.GetSize() == 0) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5856 | LLDB_LOGF(log, "Process::%s(): no structured data types supported", |
| 5857 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5858 | return; |
| 5859 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5860 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5861 | // Convert StructuredData type names to ConstString instances. |
| 5862 | std::set<ConstString> const_type_names; |
| 5863 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5864 | LLDB_LOGF(log, |
| 5865 | "Process::%s(): the process supports the following async " |
| 5866 | "structured data types:", |
| 5867 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5868 | |
| 5869 | supported_type_names.ForEach( |
| 5870 | [&const_type_names, &log](StructuredData::Object *object) { |
| 5871 | if (!object) { |
| 5872 | // Invalid - shouldn't be null objects in the array. |
| 5873 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5874 | } |
| 5875 | |
| 5876 | auto type_name = object->GetAsString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5877 | if (!type_name) { |
| 5878 | // Invalid format - all type names should be strings. |
| 5879 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | const_type_names.insert(ConstString(type_name->GetValue())); |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 5883 | LLDB_LOG(log, "- {0}", type_name->GetValue()); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5884 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5885 | }); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5886 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5887 | // For each StructuredDataPlugin, if the plugin handles any of the types in |
| 5888 | // the supported_type_names, map that type name to that plugin. Stop when |
| 5889 | // we've consumed all the type names. |
Leonard Mosescu | 9ba5157 | 2018-08-07 18:00:30 +0000 | [diff] [blame] | 5890 | // FIXME: should we return an error if there are type names nobody |
Jim Ingham | 93979f6 | 2018-04-27 01:57:40 +0000 | [diff] [blame] | 5891 | // supports? |
| 5892 | for (uint32_t plugin_index = 0; !const_type_names.empty(); plugin_index++) { |
| 5893 | auto create_instance = |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5894 | PluginManager::GetStructuredDataPluginCreateCallbackAtIndex( |
| 5895 | plugin_index); |
Jim Ingham | 93979f6 | 2018-04-27 01:57:40 +0000 | [diff] [blame] | 5896 | if (!create_instance) |
| 5897 | break; |
Leonard Mosescu | 9ba5157 | 2018-08-07 18:00:30 +0000 | [diff] [blame] | 5898 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5899 | // Create the plugin. |
| 5900 | StructuredDataPluginSP plugin_sp = (*create_instance)(*this); |
| 5901 | if (!plugin_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5902 | // This plugin doesn't think it can work with the process. Move on to the |
| 5903 | // next. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5904 | continue; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5905 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5906 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5907 | // 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] | 5908 | std::vector<ConstString> names_to_remove; |
| 5909 | for (auto &type_name : const_type_names) { |
| 5910 | if (plugin_sp->SupportsStructuredDataType(type_name)) { |
| 5911 | m_structured_data_plugin_map.insert( |
| 5912 | std::make_pair(type_name, plugin_sp)); |
| 5913 | names_to_remove.push_back(type_name); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 5914 | LLDB_LOGF(log, |
| 5915 | "Process::%s(): using plugin %s for type name " |
| 5916 | "%s", |
| 5917 | __FUNCTION__, plugin_sp->GetPluginName().GetCString(), |
| 5918 | type_name.GetCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5919 | } |
| 5920 | } |
| 5921 | |
| 5922 | // Remove the type names that were consumed by this plugin. |
| 5923 | for (auto &type_name : names_to_remove) |
| 5924 | const_type_names.erase(type_name); |
| 5925 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5926 | } |
| 5927 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5928 | bool Process::RouteAsyncStructuredData( |
| 5929 | const StructuredData::ObjectSP object_sp) { |
| 5930 | // Nothing to do if there's no data. |
| 5931 | if (!object_sp) |
| 5932 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5933 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5934 | // The contract is this must be a dictionary, so we can look up the routing |
| 5935 | // key via the top-level 'type' string value within the dictionary. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5936 | StructuredData::Dictionary *dictionary = object_sp->GetAsDictionary(); |
| 5937 | if (!dictionary) |
| 5938 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5939 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5940 | // Grab the async structured type name (i.e. the feature/plugin name). |
| 5941 | ConstString type_name; |
| 5942 | if (!dictionary->GetValueForKeyAsString("type", type_name)) |
| 5943 | return false; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5944 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5945 | // Check if there's a plugin registered for this type name. |
| 5946 | auto find_it = m_structured_data_plugin_map.find(type_name); |
| 5947 | if (find_it == m_structured_data_plugin_map.end()) { |
| 5948 | // We don't have a mapping for this structured data type. |
| 5949 | return false; |
| 5950 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 5951 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5952 | // Route the structured data to the plugin. |
| 5953 | find_it->second->HandleArrivalOfStructuredData(*this, type_name, object_sp); |
| 5954 | return true; |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 5955 | } |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 5956 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5957 | Status Process::UpdateAutomaticSignalFiltering() { |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 5958 | // Default implementation does nothign. |
| 5959 | // No automatic signal filtering to speak of. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 5960 | return Status(); |
Eugene Zemtsov | 7993cc5 | 2017-03-07 21:34:40 +0000 | [diff] [blame] | 5961 | } |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 5962 | |
Frederic Riss | d10d379 | 2018-05-11 18:21:11 +0000 | [diff] [blame] | 5963 | UtilityFunction *Process::GetLoadImageUtilityFunction( |
| 5964 | Platform *platform, |
| 5965 | llvm::function_ref<std::unique_ptr<UtilityFunction>()> factory) { |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 5966 | if (platform != GetTarget().GetPlatform().get()) |
| 5967 | return nullptr; |
Frederic Riss | d10d379 | 2018-05-11 18:21:11 +0000 | [diff] [blame] | 5968 | std::call_once(m_dlopen_utility_func_flag_once, |
| 5969 | [&] { m_dlopen_utility_func_up = factory(); }); |
Jim Ingham | 1ecb34f | 2018-04-17 20:44:47 +0000 | [diff] [blame] | 5970 | return m_dlopen_utility_func_up.get(); |
| 5971 | } |