Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1 | //===-- ProcessFreeBSD.cpp ----------------------------------------*- C++ |
| 2 | //-*-===// |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 3 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 10 | #include <errno.h> |
Pavel Labath | b7f0f45 | 2017-03-17 11:08:40 +0000 | [diff] [blame] | 11 | #include <pthread.h> |
| 12 | #include <pthread_np.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <sys/sysctl.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/user.h> |
Pavel Labath | 5b11623 | 2017-03-17 11:33:57 +0000 | [diff] [blame] | 17 | #include <machine/elf.h> |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 18 | |
Benjamin Kramer | 3f69fa6 | 2015-04-03 10:55:00 +0000 | [diff] [blame] | 19 | #include <mutex> |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 20 | #include <unordered_map> |
Benjamin Kramer | 3f69fa6 | 2015-04-03 10:55:00 +0000 | [diff] [blame] | 21 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 22 | #include "lldb/Core/PluginManager.h" |
Jonas Devlieghere | 60cf3f8 | 2018-11-01 17:35:31 +0000 | [diff] [blame] | 23 | #include "lldb/Host/FileSystem.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 24 | #include "lldb/Host/Host.h" |
| 25 | #include "lldb/Symbol/ObjectFile.h" |
| 26 | #include "lldb/Target/DynamicLoader.h" |
| 27 | #include "lldb/Target/Target.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/RegisterValue.h" |
| 29 | #include "lldb/Utility/State.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 30 | |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 31 | #include "FreeBSDThread.h" |
Pavel Labath | f0a6d8a | 2017-04-11 12:26:25 +0000 | [diff] [blame] | 32 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | #include "Plugins/Process/Utility/FreeBSDSignals.h" |
| 34 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
| 35 | #include "ProcessFreeBSD.h" |
| 36 | #include "ProcessMonitor.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 37 | |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 38 | #include "lldb/Breakpoint/BreakpointLocation.h" |
| 39 | #include "lldb/Breakpoint/Watchpoint.h" |
| 40 | #include "lldb/Core/Module.h" |
| 41 | #include "lldb/Core/ModuleSpec.h" |
| 42 | #include "lldb/Core/PluginManager.h" |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 43 | #include "lldb/Host/Host.h" |
| 44 | #include "lldb/Symbol/ObjectFile.h" |
| 45 | #include "lldb/Target/DynamicLoader.h" |
| 46 | #include "lldb/Target/Platform.h" |
| 47 | #include "lldb/Target/Target.h" |
Pavel Labath | 5b11623 | 2017-03-17 11:33:57 +0000 | [diff] [blame] | 48 | #include "lldb/Utility/DataBufferHeap.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 49 | #include "lldb/Utility/FileSpec.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 50 | #include "lldb/Utility/State.h" |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 51 | |
| 52 | #include "lldb/Host/posix/Fcntl.h" |
| 53 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 54 | #include "llvm/Support/FileSystem.h" |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Threading.h" |
| 56 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 57 | using namespace lldb; |
| 58 | using namespace lldb_private; |
| 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | namespace { |
| 61 | UnixSignalsSP &GetFreeBSDSignals() { |
| 62 | static UnixSignalsSP s_freebsd_signals_sp(new FreeBSDSignals()); |
| 63 | return s_freebsd_signals_sp; |
| 64 | } |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 67 | //------------------------------------------------------------------------------ |
| 68 | // Static functions. |
| 69 | |
Greg Clayton | 29d1930 | 2012-02-27 18:40:48 +0000 | [diff] [blame] | 70 | lldb::ProcessSP |
Zachary Turner | 12004eb | 2015-09-02 16:47:47 +0000 | [diff] [blame] | 71 | ProcessFreeBSD::CreateInstance(lldb::TargetSP target_sp, |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 72 | lldb::ListenerSP listener_sp, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | const FileSpec *crash_file_path) { |
| 74 | lldb::ProcessSP process_sp; |
| 75 | if (crash_file_path == NULL) |
| 76 | process_sp.reset( |
| 77 | new ProcessFreeBSD(target_sp, listener_sp, GetFreeBSDSignals())); |
| 78 | return process_sp; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | void ProcessFreeBSD::Initialize() { |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 82 | static llvm::once_flag g_once_flag; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 83 | |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 84 | llvm::call_once(g_once_flag, []() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 86 | GetPluginDescriptionStatic(), CreateInstance); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | }); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | lldb_private::ConstString ProcessFreeBSD::GetPluginNameStatic() { |
| 91 | static ConstString g_name("freebsd"); |
| 92 | return g_name; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | const char *ProcessFreeBSD::GetPluginDescriptionStatic() { |
| 96 | return "Process plugin for FreeBSD"; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | //------------------------------------------------------------------------------ |
| 100 | // ProcessInterface protocol. |
| 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | lldb_private::ConstString ProcessFreeBSD::GetPluginName() { |
| 103 | return GetPluginNameStatic(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | uint32_t ProcessFreeBSD::GetPluginVersion() { return 1; } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | void ProcessFreeBSD::Terminate() {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 109 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 110 | Status ProcessFreeBSD::DoDetach(bool keep_stopped) { |
| 111 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | if (keep_stopped) { |
| 113 | error.SetErrorString("Detaching with keep_stopped true is not currently " |
| 114 | "supported on FreeBSD."); |
Ed Maste | 7dcb77d | 2013-08-30 13:11:30 +0000 | [diff] [blame] | 115 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | error = m_monitor->Detach(GetID()); |
| 119 | |
| 120 | if (error.Success()) |
| 121 | SetPrivateState(eStateDetached); |
| 122 | |
| 123 | return error; |
Ed Maste | 7dcb77d | 2013-08-30 13:11:30 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 126 | Status ProcessFreeBSD::DoResume() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | SetPrivateState(eStateRunning); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 132 | bool do_step = false; |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 133 | bool software_single_step = !SupportHardwareSingleStepping(); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | for (tid_collection::const_iterator t_pos = m_run_tids.begin(), |
| 136 | t_end = m_run_tids.end(); |
| 137 | t_pos != t_end; ++t_pos) { |
| 138 | m_monitor->ThreadSuspend(*t_pos, false); |
| 139 | } |
| 140 | for (tid_collection::const_iterator t_pos = m_step_tids.begin(), |
| 141 | t_end = m_step_tids.end(); |
| 142 | t_pos != t_end; ++t_pos) { |
| 143 | m_monitor->ThreadSuspend(*t_pos, false); |
| 144 | do_step = true; |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 145 | if (software_single_step) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 146 | Status error = SetupSoftwareSingleStepping(*t_pos); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 147 | if (error.Fail()) |
| 148 | return error; |
| 149 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | } |
| 151 | for (tid_collection::const_iterator t_pos = m_suspend_tids.begin(), |
| 152 | t_end = m_suspend_tids.end(); |
| 153 | t_pos != t_end; ++t_pos) { |
| 154 | m_monitor->ThreadSuspend(*t_pos, true); |
| 155 | // XXX Cannot PT_CONTINUE properly with suspended threads. |
| 156 | do_step = true; |
| 157 | } |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 158 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 159 | if (log) |
| 160 | log->Printf("process %" PRIu64 " resuming (%s)", GetID(), |
| 161 | do_step ? "step" : "continue"); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 162 | if (do_step && !software_single_step) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | m_monitor->SingleStep(GetID(), m_resume_signo); |
| 164 | else |
| 165 | m_monitor->Resume(GetID(), m_resume_signo); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 166 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 167 | return Status(); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | bool ProcessFreeBSD::UpdateThreadList(ThreadList &old_thread_list, |
| 171 | ThreadList &new_thread_list) { |
| 172 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 173 | if (log) |
| 174 | log->Printf("ProcessFreeBSD::%s (pid = %" PRIu64 ")", __FUNCTION__, |
| 175 | GetID()); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 176 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | std::vector<lldb::pid_t> tds; |
| 178 | if (!GetMonitor().GetCurrentThreadIDs(tds)) { |
| 179 | return false; |
| 180 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 181 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | ThreadList old_thread_list_copy(old_thread_list); |
| 183 | for (size_t i = 0; i < tds.size(); ++i) { |
| 184 | tid_t tid = tds[i]; |
| 185 | ThreadSP thread_sp(old_thread_list_copy.RemoveThreadByID(tid, false)); |
| 186 | if (!thread_sp) { |
| 187 | thread_sp.reset(new FreeBSDThread(*this, tid)); |
| 188 | if (log) |
| 189 | log->Printf("ProcessFreeBSD::%s new tid = %" PRIu64, __FUNCTION__, tid); |
| 190 | } else { |
| 191 | if (log) |
| 192 | log->Printf("ProcessFreeBSD::%s existing tid = %" PRIu64, __FUNCTION__, |
| 193 | tid); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 194 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 195 | new_thread_list.AddThread(thread_sp); |
| 196 | } |
| 197 | for (size_t i = 0; i < old_thread_list_copy.GetSize(false); ++i) { |
| 198 | ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex(i, false)); |
| 199 | if (old_thread_sp) { |
| 200 | if (log) |
| 201 | log->Printf("ProcessFreeBSD::%s remove tid", __FUNCTION__); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 202 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 203 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | return true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 206 | } |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 207 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 208 | Status ProcessFreeBSD::WillResume() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | m_resume_signo = 0; |
| 210 | m_suspend_tids.clear(); |
| 211 | m_run_tids.clear(); |
| 212 | m_step_tids.clear(); |
| 213 | return Process::WillResume(); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | void ProcessFreeBSD::SendMessage(const ProcessMessage &message) { |
| 217 | std::lock_guard<std::recursive_mutex> guard(m_message_mutex); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 218 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 219 | switch (message.GetKind()) { |
| 220 | case ProcessMessage::eInvalidMessage: |
| 221 | return; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 222 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | case ProcessMessage::eAttachMessage: |
| 224 | SetPrivateState(eStateStopped); |
| 225 | return; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 226 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | case ProcessMessage::eLimboMessage: |
| 228 | case ProcessMessage::eExitMessage: |
| 229 | SetExitStatus(message.GetExitStatus(), NULL); |
| 230 | break; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 231 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 232 | case ProcessMessage::eSignalMessage: |
| 233 | case ProcessMessage::eSignalDeliveredMessage: |
| 234 | case ProcessMessage::eBreakpointMessage: |
| 235 | case ProcessMessage::eTraceMessage: |
| 236 | case ProcessMessage::eWatchpointMessage: |
| 237 | case ProcessMessage::eCrashMessage: |
| 238 | SetPrivateState(eStateStopped); |
| 239 | break; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | case ProcessMessage::eNewThreadMessage: |
| 242 | llvm_unreachable("eNewThreadMessage unexpected on FreeBSD"); |
| 243 | break; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 244 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 245 | case ProcessMessage::eExecMessage: |
| 246 | SetPrivateState(eStateStopped); |
| 247 | break; |
| 248 | } |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 249 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 250 | m_message_queue.push(message); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 251 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 252 | |
| 253 | //------------------------------------------------------------------------------ |
| 254 | // Constructors and destructors. |
| 255 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | ProcessFreeBSD::ProcessFreeBSD(lldb::TargetSP target_sp, |
| 257 | lldb::ListenerSP listener_sp, |
| 258 | UnixSignalsSP &unix_signals_sp) |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 259 | : Process(target_sp, listener_sp, unix_signals_sp), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 260 | m_byte_order(endian::InlHostByteOrder()), m_monitor(NULL), m_module(NULL), |
| 261 | m_message_mutex(), m_exit_now(false), m_seen_initial_stop(), |
| 262 | m_resume_signo(0) { |
| 263 | // FIXME: Putting this code in the ctor and saving the byte order in a |
| 264 | // member variable is a hack to avoid const qual issues in GetByteOrder. |
| 265 | lldb::ModuleSP module = GetTarget().GetExecutableModule(); |
| 266 | if (module && module->GetObjectFile()) |
| 267 | m_byte_order = module->GetObjectFile()->GetByteOrder(); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | ProcessFreeBSD::~ProcessFreeBSD() { delete m_monitor; } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 271 | |
| 272 | //------------------------------------------------------------------------------ |
| 273 | // Process protocol. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | void ProcessFreeBSD::Finalize() { |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 275 | Process::Finalize(); |
| 276 | |
| 277 | if (m_monitor) |
| 278 | m_monitor->StopMonitor(); |
| 279 | } |
| 280 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 281 | bool ProcessFreeBSD::CanDebug(lldb::TargetSP target_sp, |
| 282 | bool plugin_specified_by_name) { |
| 283 | // For now we are just making sure the file exists for a given module |
| 284 | ModuleSP exe_module_sp(target_sp->GetExecutableModule()); |
| 285 | if (exe_module_sp.get()) |
Jonas Devlieghere | 49996c1 | 2018-11-01 17:46:31 +0000 | [diff] [blame] | 286 | return FileSystem::Instance().Exists(exe_module_sp->GetFileSpec()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 287 | // If there is no executable module, we return true since we might be |
| 288 | // preparing to attach. |
| 289 | return true; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 292 | Status |
| 293 | ProcessFreeBSD::DoAttachToProcessWithID(lldb::pid_t pid, |
| 294 | const ProcessAttachInfo &attach_info) { |
| 295 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 296 | assert(m_monitor == NULL); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 297 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 298 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 299 | LLDB_LOGV(log, "pid = {0}", GetID()); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 300 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 301 | m_monitor = new ProcessMonitor(this, pid, error); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 302 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 303 | if (!error.Success()) |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 304 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 305 | |
| 306 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 307 | assert(platform_sp.get()); |
| 308 | if (!platform_sp) |
| 309 | return error; // FIXME: Detatch? |
| 310 | |
| 311 | // Find out what we can about this process |
| 312 | ProcessInstanceInfo process_info; |
| 313 | platform_sp->GetProcessInfo(pid, process_info); |
| 314 | |
| 315 | // Resolve the executable module |
| 316 | ModuleSP exe_module_sp; |
| 317 | FileSpecList executable_search_paths( |
| 318 | Target::GetDefaultExecutableSearchPaths()); |
| 319 | ModuleSpec exe_module_spec(process_info.GetExecutableFile(), |
| 320 | GetTarget().GetArchitecture()); |
| 321 | error = platform_sp->ResolveExecutable( |
| 322 | exe_module_spec, exe_module_sp, |
| 323 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
| 324 | if (!error.Success()) |
| 325 | return error; |
| 326 | |
| 327 | // Fix the target architecture if necessary |
| 328 | const ArchSpec &module_arch = exe_module_sp->GetArchitecture(); |
| 329 | if (module_arch.IsValid() && |
| 330 | !GetTarget().GetArchitecture().IsExactMatch(module_arch)) |
| 331 | GetTarget().SetArchitecture(module_arch); |
| 332 | |
| 333 | // Initialize the target module list |
Tatyana Krasnukha | 891d750 | 2018-09-25 17:59:44 +0000 | [diff] [blame] | 334 | GetTarget().SetExecutableModule(exe_module_sp, eLoadDependentsYes); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 335 | |
| 336 | SetSTDIOFileDescriptor(m_monitor->GetTerminalFD()); |
| 337 | |
| 338 | SetID(pid); |
| 339 | |
| 340 | return error; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 343 | Status ProcessFreeBSD::WillLaunch(Module *module) { |
| 344 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 345 | return error; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | FileSpec |
| 349 | ProcessFreeBSD::GetFileSpec(const lldb_private::FileAction *file_action, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 350 | const FileSpec &default_file_spec, |
| 351 | const FileSpec &dbg_pts_file_spec) { |
| 352 | FileSpec file_spec{}; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 353 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 354 | if (file_action && file_action->GetAction() == FileAction::eFileActionOpen) { |
| 355 | file_spec = file_action->GetFileSpec(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 356 | // By default the stdio paths passed in will be pseudo-terminal (/dev/pts). |
| 357 | // If so, convert to using a different default path instead to redirect I/O |
| 358 | // to the debugger console. This should also handle user overrides to |
| 359 | // /dev/null or a different file. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 360 | if (!file_spec || file_spec == dbg_pts_file_spec) |
| 361 | file_spec = default_file_spec; |
| 362 | } |
| 363 | return file_spec; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 366 | Status ProcessFreeBSD::DoLaunch(Module *module, |
| 367 | ProcessLaunchInfo &launch_info) { |
| 368 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 369 | assert(m_monitor == NULL); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 370 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 371 | FileSpec working_dir = launch_info.GetWorkingDirectory(); |
David Carlier | 511e1cf | 2018-11-04 23:19:25 +0000 | [diff] [blame] | 372 | if (working_dir) { |
| 373 | FileSystem::Instance().Resolve(working_dir); |
Jonas Devlieghere | 3a58d89 | 2018-11-08 00:14:50 +0000 | [diff] [blame] | 374 | if (!FileSystem::Instance().IsDirectory(working_dir.GetPath())) { |
David Carlier | 511e1cf | 2018-11-04 23:19:25 +0000 | [diff] [blame] | 375 | error.SetErrorStringWithFormat("No such file or directory: %s", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 376 | working_dir.GetCString()); |
David Carlier | 511e1cf | 2018-11-04 23:19:25 +0000 | [diff] [blame] | 377 | return error; |
| 378 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 379 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 380 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 381 | SetPrivateState(eStateLaunching); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 382 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 383 | const lldb_private::FileAction *file_action; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 384 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 385 | // Default of empty will mean to use existing open file descriptors |
| 386 | FileSpec stdin_file_spec{}; |
| 387 | FileSpec stdout_file_spec{}; |
| 388 | FileSpec stderr_file_spec{}; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 389 | |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 390 | const FileSpec dbg_pts_file_spec{launch_info.GetPTY().GetSlaveName(NULL, 0)}; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 391 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | file_action = launch_info.GetFileActionForFD(STDIN_FILENO); |
| 393 | stdin_file_spec = |
| 394 | GetFileSpec(file_action, stdin_file_spec, dbg_pts_file_spec); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 395 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 396 | file_action = launch_info.GetFileActionForFD(STDOUT_FILENO); |
| 397 | stdout_file_spec = |
| 398 | GetFileSpec(file_action, stdout_file_spec, dbg_pts_file_spec); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 399 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 400 | file_action = launch_info.GetFileActionForFD(STDERR_FILENO); |
| 401 | stderr_file_spec = |
| 402 | GetFileSpec(file_action, stderr_file_spec, dbg_pts_file_spec); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 403 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 404 | m_monitor = new ProcessMonitor( |
| 405 | this, module, launch_info.GetArguments().GetConstArgumentVector(), |
Pavel Labath | 75c6de0 | 2018-01-10 13:53:40 +0000 | [diff] [blame] | 406 | launch_info.GetEnvironment(), stdin_file_spec, stdout_file_spec, |
Pavel Labath | 3ff377a | 2018-01-10 12:25:48 +0000 | [diff] [blame] | 407 | stderr_file_spec, working_dir, launch_info, error); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 408 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 409 | m_module = module; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 410 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 411 | if (!error.Success()) |
| 412 | return error; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 413 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 414 | int terminal = m_monitor->GetTerminalFD(); |
| 415 | if (terminal >= 0) { |
| 416 | // The reader thread will close the file descriptor when done, so we pass it a |
| 417 | // copy. |
Sylvestre Ledru | 79cb009 | 2015-08-28 12:24:07 +0000 | [diff] [blame] | 418 | #ifdef F_DUPFD_CLOEXEC |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 419 | int stdio = fcntl(terminal, F_DUPFD_CLOEXEC, 0); |
| 420 | if (stdio == -1) { |
| 421 | error.SetErrorToErrno(); |
| 422 | return error; |
| 423 | } |
Sylvestre Ledru | 79cb009 | 2015-08-28 12:24:07 +0000 | [diff] [blame] | 424 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | // Special case when F_DUPFD_CLOEXEC does not exist (Debian kFreeBSD) |
| 426 | int stdio = fcntl(terminal, F_DUPFD, 0); |
| 427 | if (stdio == -1) { |
| 428 | error.SetErrorToErrno(); |
| 429 | return error; |
| 430 | } |
| 431 | stdio = fcntl(terminal, F_SETFD, FD_CLOEXEC); |
| 432 | if (stdio == -1) { |
| 433 | error.SetErrorToErrno(); |
| 434 | return error; |
| 435 | } |
Sylvestre Ledru | 79cb009 | 2015-08-28 12:24:07 +0000 | [diff] [blame] | 436 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 437 | SetSTDIOFileDescriptor(stdio); |
| 438 | } |
| 439 | |
| 440 | SetID(m_monitor->GetPID()); |
| 441 | return error; |
| 442 | } |
| 443 | |
| 444 | void ProcessFreeBSD::DidLaunch() {} |
| 445 | |
| 446 | addr_t ProcessFreeBSD::GetImageInfoAddress() { |
| 447 | Target *target = &GetTarget(); |
| 448 | ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); |
| 449 | Address addr = obj_file->GetImageInfoAddress(target); |
| 450 | |
| 451 | if (addr.IsValid()) |
| 452 | return addr.GetLoadAddress(target); |
| 453 | return LLDB_INVALID_ADDRESS; |
| 454 | } |
| 455 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 456 | Status ProcessFreeBSD::DoHalt(bool &caused_stop) { |
| 457 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 458 | |
| 459 | if (IsStopped()) { |
| 460 | caused_stop = false; |
| 461 | } else if (kill(GetID(), SIGSTOP)) { |
| 462 | caused_stop = false; |
| 463 | error.SetErrorToErrno(); |
| 464 | } else { |
| 465 | caused_stop = true; |
| 466 | } |
| 467 | return error; |
| 468 | } |
| 469 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 470 | Status ProcessFreeBSD::DoSignal(int signal) { |
| 471 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 472 | |
| 473 | if (kill(GetID(), signal)) |
| 474 | error.SetErrorToErrno(); |
| 475 | |
| 476 | return error; |
| 477 | } |
| 478 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 479 | Status ProcessFreeBSD::DoDestroy() { |
| 480 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 481 | |
| 482 | if (!HasExited()) { |
| 483 | assert(m_monitor); |
| 484 | m_exit_now = true; |
| 485 | if (GetID() == LLDB_INVALID_PROCESS_ID) { |
| 486 | error.SetErrorString("invalid process id"); |
| 487 | return error; |
| 488 | } |
| 489 | if (!m_monitor->Kill()) { |
| 490 | error.SetErrorToErrno(); |
| 491 | return error; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 494 | SetPrivateState(eStateExited); |
| 495 | } |
| 496 | |
| 497 | return error; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 500 | void ProcessFreeBSD::DoDidExec() { |
| 501 | Target *target = &GetTarget(); |
| 502 | if (target) { |
| 503 | PlatformSP platform_sp(target->GetPlatform()); |
| 504 | assert(platform_sp.get()); |
| 505 | if (platform_sp) { |
| 506 | ProcessInstanceInfo process_info; |
| 507 | platform_sp->GetProcessInfo(GetID(), process_info); |
| 508 | ModuleSP exe_module_sp; |
| 509 | ModuleSpec exe_module_spec(process_info.GetExecutableFile(), |
| 510 | target->GetArchitecture()); |
| 511 | FileSpecList executable_search_paths( |
| 512 | Target::GetDefaultExecutableSearchPaths()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 513 | Status error = platform_sp->ResolveExecutable( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 514 | exe_module_spec, exe_module_sp, |
| 515 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
| 516 | if (!error.Success()) |
| 517 | return; |
Tatyana Krasnukha | 891d750 | 2018-09-25 17:59:44 +0000 | [diff] [blame] | 518 | target->SetExecutableModule(exe_module_sp, eLoadDependentsYes); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 519 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 520 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 523 | bool ProcessFreeBSD::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid) { |
| 524 | bool added_to_set = false; |
| 525 | ThreadStopSet::iterator it = m_seen_initial_stop.find(stop_tid); |
| 526 | if (it == m_seen_initial_stop.end()) { |
| 527 | m_seen_initial_stop.insert(stop_tid); |
| 528 | added_to_set = true; |
| 529 | } |
| 530 | return added_to_set; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 533 | bool ProcessFreeBSD::WaitingForInitialStop(lldb::tid_t stop_tid) { |
| 534 | return (m_seen_initial_stop.find(stop_tid) == m_seen_initial_stop.end()); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | FreeBSDThread * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 538 | ProcessFreeBSD::CreateNewFreeBSDThread(lldb_private::Process &process, |
| 539 | lldb::tid_t tid) { |
| 540 | return new FreeBSDThread(process, tid); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 543 | void ProcessFreeBSD::RefreshStateAfterStop() { |
| 544 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 545 | LLDB_LOGV(log, "message_queue size = {0}", m_message_queue.size()); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 546 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 547 | std::lock_guard<std::recursive_mutex> guard(m_message_mutex); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 548 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 549 | // This method used to only handle one message. Changing it to loop allows |
| 550 | // it to handle the case where we hit a breakpoint while handling a different |
| 551 | // breakpoint. |
| 552 | while (!m_message_queue.empty()) { |
| 553 | ProcessMessage &message = m_message_queue.front(); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 554 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 555 | // Resolve the thread this message corresponds to and pass it along. |
| 556 | lldb::tid_t tid = message.GetTID(); |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 557 | LLDB_LOGV(log, " message_queue size = {0}, pid = {1}", |
| 558 | m_message_queue.size(), tid); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 559 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 560 | m_thread_list.RefreshStateAfterStop(); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 561 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 562 | FreeBSDThread *thread = static_cast<FreeBSDThread *>( |
| 563 | GetThreadList().FindThreadByID(tid, false).get()); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 564 | if (thread) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 565 | thread->Notify(message); |
| 566 | |
| 567 | if (message.GetKind() == ProcessMessage::eExitMessage) { |
| 568 | // FIXME: We should tell the user about this, but the limbo message is |
| 569 | // probably better for that. |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 570 | LLDB_LOG(log, "removing thread, tid = {0}", tid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 571 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 572 | |
| 573 | ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false); |
| 574 | thread_sp.reset(); |
| 575 | m_seen_initial_stop.erase(tid); |
| 576 | } |
| 577 | |
| 578 | m_message_queue.pop(); |
| 579 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 582 | bool ProcessFreeBSD::IsAlive() { |
| 583 | StateType state = GetPrivateState(); |
| 584 | return state != eStateDetached && state != eStateExited && |
| 585 | state != eStateInvalid && state != eStateUnloaded; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 588 | size_t ProcessFreeBSD::DoReadMemory(addr_t vm_addr, void *buf, size_t size, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 589 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 590 | assert(m_monitor); |
| 591 | return m_monitor->ReadMemory(vm_addr, buf, size, error); |
| 592 | } |
| 593 | |
| 594 | size_t ProcessFreeBSD::DoWriteMemory(addr_t vm_addr, const void *buf, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 595 | size_t size, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 596 | assert(m_monitor); |
| 597 | return m_monitor->WriteMemory(vm_addr, buf, size, error); |
| 598 | } |
| 599 | |
| 600 | addr_t ProcessFreeBSD::DoAllocateMemory(size_t size, uint32_t permissions, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 601 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 602 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
| 603 | |
| 604 | unsigned prot = 0; |
| 605 | if (permissions & lldb::ePermissionsReadable) |
| 606 | prot |= eMmapProtRead; |
| 607 | if (permissions & lldb::ePermissionsWritable) |
| 608 | prot |= eMmapProtWrite; |
| 609 | if (permissions & lldb::ePermissionsExecutable) |
| 610 | prot |= eMmapProtExec; |
| 611 | |
| 612 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 613 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { |
| 614 | m_addr_to_mmap_size[allocated_addr] = size; |
| 615 | error.Clear(); |
| 616 | } else { |
| 617 | allocated_addr = LLDB_INVALID_ADDRESS; |
| 618 | error.SetErrorStringWithFormat( |
| 619 | "unable to allocate %zu bytes of memory with permissions %s", size, |
| 620 | GetPermissionsAsCString(permissions)); |
| 621 | } |
| 622 | |
| 623 | return allocated_addr; |
| 624 | } |
| 625 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 626 | Status ProcessFreeBSD::DoDeallocateMemory(lldb::addr_t addr) { |
| 627 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 628 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
| 629 | if (pos != m_addr_to_mmap_size.end() && |
| 630 | InferiorCallMunmap(this, addr, pos->second)) |
| 631 | m_addr_to_mmap_size.erase(pos); |
| 632 | else |
| 633 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, |
| 634 | addr); |
| 635 | |
| 636 | return error; |
| 637 | } |
| 638 | |
| 639 | size_t |
| 640 | ProcessFreeBSD::GetSoftwareBreakpointTrapOpcode(BreakpointSite *bp_site) { |
| 641 | static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xD4}; |
| 642 | static const uint8_t g_i386_opcode[] = {0xCC}; |
| 643 | |
| 644 | ArchSpec arch = GetTarget().GetArchitecture(); |
| 645 | const uint8_t *opcode = NULL; |
| 646 | size_t opcode_size = 0; |
| 647 | |
| 648 | switch (arch.GetMachine()) { |
| 649 | default: |
| 650 | assert(false && "CPU type not supported!"); |
| 651 | break; |
| 652 | |
| 653 | case llvm::Triple::arm: { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 654 | // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the |
| 655 | // linux kernel does otherwise. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 656 | static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; |
| 657 | static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; |
| 658 | |
| 659 | lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0)); |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 660 | AddressClass addr_class = AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 661 | |
| 662 | if (bp_loc_sp) |
| 663 | addr_class = bp_loc_sp->GetAddress().GetAddressClass(); |
| 664 | |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 665 | if (addr_class == AddressClass::eCodeAlternateISA || |
| 666 | (addr_class == AddressClass::eUnknown && |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 667 | bp_loc_sp->GetAddress().GetOffset() & 1)) { |
| 668 | opcode = g_thumb_breakpoint_opcode; |
| 669 | opcode_size = sizeof(g_thumb_breakpoint_opcode); |
| 670 | } else { |
| 671 | opcode = g_arm_breakpoint_opcode; |
| 672 | opcode_size = sizeof(g_arm_breakpoint_opcode); |
| 673 | } |
| 674 | } break; |
| 675 | case llvm::Triple::aarch64: |
| 676 | opcode = g_aarch64_opcode; |
| 677 | opcode_size = sizeof(g_aarch64_opcode); |
| 678 | break; |
| 679 | |
| 680 | case llvm::Triple::x86: |
| 681 | case llvm::Triple::x86_64: |
| 682 | opcode = g_i386_opcode; |
| 683 | opcode_size = sizeof(g_i386_opcode); |
| 684 | break; |
| 685 | } |
| 686 | |
| 687 | bp_site->SetTrapOpcode(opcode, opcode_size); |
| 688 | return opcode_size; |
| 689 | } |
| 690 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 691 | Status ProcessFreeBSD::EnableBreakpointSite(BreakpointSite *bp_site) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 692 | return EnableSoftwareBreakpoint(bp_site); |
| 693 | } |
| 694 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 695 | Status ProcessFreeBSD::DisableBreakpointSite(BreakpointSite *bp_site) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 696 | return DisableSoftwareBreakpoint(bp_site); |
| 697 | } |
| 698 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 699 | Status ProcessFreeBSD::EnableWatchpoint(Watchpoint *wp, bool notify) { |
| 700 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 701 | if (wp) { |
| 702 | user_id_t watchID = wp->GetID(); |
| 703 | addr_t addr = wp->GetLoadAddress(); |
| 704 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); |
| 705 | if (log) |
| 706 | log->Printf("ProcessFreeBSD::EnableWatchpoint(watchID = %" PRIu64 ")", |
| 707 | watchID); |
| 708 | if (wp->IsEnabled()) { |
| 709 | if (log) |
| 710 | log->Printf("ProcessFreeBSD::EnableWatchpoint(watchID = %" PRIu64 |
| 711 | ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", |
| 712 | watchID, (uint64_t)addr); |
| 713 | return error; |
| 714 | } |
| 715 | |
| 716 | // Try to find a vacant watchpoint slot in the inferiors' main thread |
| 717 | uint32_t wp_hw_index = LLDB_INVALID_INDEX32; |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 718 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 719 | FreeBSDThread *thread = static_cast<FreeBSDThread *>( |
| 720 | m_thread_list.GetThreadAtIndex(0, false).get()); |
| 721 | |
| 722 | if (thread) |
| 723 | wp_hw_index = thread->FindVacantWatchpointIndex(); |
| 724 | |
| 725 | if (wp_hw_index == LLDB_INVALID_INDEX32) { |
| 726 | error.SetErrorString("Setting hardware watchpoint failed."); |
| 727 | } else { |
| 728 | wp->SetHardwareIndex(wp_hw_index); |
| 729 | bool wp_enabled = true; |
| 730 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 731 | for (uint32_t i = 0; i < thread_count; ++i) { |
| 732 | thread = static_cast<FreeBSDThread *>( |
| 733 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 734 | if (thread) |
| 735 | wp_enabled &= thread->EnableHardwareWatchpoint(wp); |
| 736 | else |
| 737 | wp_enabled = false; |
| 738 | } |
| 739 | if (wp_enabled) { |
| 740 | wp->SetEnabled(true, notify); |
| 741 | return error; |
| 742 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 743 | // Watchpoint enabling failed on at least one of the threads so roll |
| 744 | // back all of them |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 745 | DisableWatchpoint(wp, false); |
| 746 | error.SetErrorString("Setting hardware watchpoint failed"); |
| 747 | } |
| 748 | } |
| 749 | } else |
| 750 | error.SetErrorString("Watchpoint argument was NULL."); |
| 751 | return error; |
| 752 | } |
| 753 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 754 | Status ProcessFreeBSD::DisableWatchpoint(Watchpoint *wp, bool notify) { |
| 755 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 756 | if (wp) { |
| 757 | user_id_t watchID = wp->GetID(); |
| 758 | addr_t addr = wp->GetLoadAddress(); |
| 759 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); |
| 760 | if (log) |
| 761 | log->Printf("ProcessFreeBSD::DisableWatchpoint(watchID = %" PRIu64 ")", |
| 762 | watchID); |
| 763 | if (!wp->IsEnabled()) { |
| 764 | if (log) |
| 765 | log->Printf("ProcessFreeBSD::DisableWatchpoint(watchID = %" PRIu64 |
| 766 | ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.", |
| 767 | watchID, (uint64_t)addr); |
| 768 | // This is needed (for now) to keep watchpoints disabled correctly |
| 769 | wp->SetEnabled(false, notify); |
| 770 | return error; |
| 771 | } |
| 772 | |
| 773 | if (wp->IsHardware()) { |
| 774 | bool wp_disabled = true; |
| 775 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 776 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 777 | for (uint32_t i = 0; i < thread_count; ++i) { |
| 778 | FreeBSDThread *thread = static_cast<FreeBSDThread *>( |
| 779 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 780 | if (thread) |
| 781 | wp_disabled &= thread->DisableHardwareWatchpoint(wp); |
| 782 | else |
| 783 | wp_disabled = false; |
| 784 | } |
| 785 | if (wp_disabled) { |
| 786 | wp->SetHardwareIndex(LLDB_INVALID_INDEX32); |
| 787 | wp->SetEnabled(false, notify); |
| 788 | return error; |
| 789 | } else |
| 790 | error.SetErrorString("Disabling hardware watchpoint failed"); |
| 791 | } |
| 792 | } else |
| 793 | error.SetErrorString("Watchpoint argument was NULL."); |
| 794 | return error; |
| 795 | } |
| 796 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 797 | Status ProcessFreeBSD::GetWatchpointSupportInfo(uint32_t &num) { |
| 798 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 799 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 800 | FreeBSDThread *thread = static_cast<FreeBSDThread *>( |
| 801 | m_thread_list.GetThreadAtIndex(0, false).get()); |
| 802 | if (thread) |
| 803 | num = thread->NumSupportedHardwareWatchpoints(); |
| 804 | else |
| 805 | error.SetErrorString("Process does not exist."); |
| 806 | return error; |
| 807 | } |
| 808 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 809 | Status ProcessFreeBSD::GetWatchpointSupportInfo(uint32_t &num, bool &after) { |
| 810 | Status error = GetWatchpointSupportInfo(num); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 811 | // Watchpoints trigger and halt the inferior after the corresponding |
| 812 | // instruction has been executed. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 813 | after = true; |
| 814 | return error; |
| 815 | } |
| 816 | |
| 817 | uint32_t ProcessFreeBSD::UpdateThreadListIfNeeded() { |
| 818 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 819 | // Do not allow recursive updates. |
| 820 | return m_thread_list.GetSize(false); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 823 | ByteOrder ProcessFreeBSD::GetByteOrder() const { |
| 824 | // FIXME: We should be able to extract this value directly. See comment in |
| 825 | // ProcessFreeBSD(). |
| 826 | return m_byte_order; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 829 | size_t ProcessFreeBSD::PutSTDIN(const char *buf, size_t len, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 830 | ssize_t status; |
| 831 | if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0) { |
| 832 | error.SetErrorToErrno(); |
| 833 | return 0; |
| 834 | } |
| 835 | return status; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | //------------------------------------------------------------------------------ |
| 839 | // Utility functions. |
| 840 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 841 | bool ProcessFreeBSD::HasExited() { |
| 842 | switch (GetPrivateState()) { |
| 843 | default: |
| 844 | break; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 845 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 846 | case eStateDetached: |
| 847 | case eStateExited: |
| 848 | return true; |
| 849 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 850 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 851 | return false; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 854 | bool ProcessFreeBSD::IsStopped() { |
| 855 | switch (GetPrivateState()) { |
| 856 | default: |
| 857 | break; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 858 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 859 | case eStateStopped: |
| 860 | case eStateCrashed: |
| 861 | case eStateSuspended: |
| 862 | return true; |
| 863 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 864 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 865 | return false; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 868 | bool ProcessFreeBSD::IsAThreadRunning() { |
| 869 | bool is_running = false; |
| 870 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 871 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 872 | for (uint32_t i = 0; i < thread_count; ++i) { |
| 873 | FreeBSDThread *thread = static_cast<FreeBSDThread *>( |
| 874 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 875 | StateType thread_state = thread->GetState(); |
| 876 | if (thread_state == eStateRunning || thread_state == eStateStepping) { |
| 877 | is_running = true; |
| 878 | break; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 879 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 880 | } |
| 881 | return is_running; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 884 | const DataBufferSP ProcessFreeBSD::GetAuxvData() { |
| 885 | // If we're the local platform, we can ask the host for auxv data. |
| 886 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
Pavel Labath | b7f0f45 | 2017-03-17 11:08:40 +0000 | [diff] [blame] | 887 | assert(platform_sp && platform_sp->IsHost()); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 888 | |
Pavel Labath | 5b11623 | 2017-03-17 11:33:57 +0000 | [diff] [blame] | 889 | int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_AUXV, (int)m_process->GetID()}; |
Pavel Labath | b7f0f45 | 2017-03-17 11:08:40 +0000 | [diff] [blame] | 890 | size_t auxv_size = AT_COUNT * sizeof(Elf_Auxinfo); |
| 891 | DataBufferSP buf_sp(new DataBufferHeap(auxv_size, 0)); |
| 892 | |
Pavel Labath | 5b11623 | 2017-03-17 11:33:57 +0000 | [diff] [blame] | 893 | if (::sysctl(mib, 4, buf_sp->GetBytes(), &auxv_size, NULL, 0) != 0) { |
Pavel Labath | b7f0f45 | 2017-03-17 11:08:40 +0000 | [diff] [blame] | 894 | perror("sysctl failed on auxv"); |
| 895 | buf_sp.reset(); |
| 896 | } |
| 897 | |
| 898 | return buf_sp; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 899 | } |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 900 | |
| 901 | struct EmulatorBaton { |
| 902 | ProcessFreeBSD *m_process; |
| 903 | RegisterContext *m_reg_context; |
| 904 | |
| 905 | // eRegisterKindDWARF -> RegisterValue |
| 906 | std::unordered_map<uint32_t, RegisterValue> m_register_values; |
| 907 | |
| 908 | EmulatorBaton(ProcessFreeBSD *process, RegisterContext *reg_context) |
| 909 | : m_process(process), m_reg_context(reg_context) {} |
| 910 | }; |
| 911 | |
| 912 | static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton, |
| 913 | const EmulateInstruction::Context &context, |
| 914 | lldb::addr_t addr, void *dst, size_t length) { |
| 915 | EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); |
| 916 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 917 | Status error; |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 918 | size_t bytes_read = |
| 919 | emulator_baton->m_process->DoReadMemory(addr, dst, length, error); |
| 920 | if (!error.Success()) |
| 921 | bytes_read = 0; |
| 922 | return bytes_read; |
| 923 | } |
| 924 | |
| 925 | static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton, |
| 926 | const RegisterInfo *reg_info, |
| 927 | RegisterValue ®_value) { |
| 928 | EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); |
| 929 | |
| 930 | auto it = emulator_baton->m_register_values.find( |
| 931 | reg_info->kinds[eRegisterKindDWARF]); |
| 932 | if (it != emulator_baton->m_register_values.end()) { |
| 933 | reg_value = it->second; |
| 934 | return true; |
| 935 | } |
| 936 | |
| 937 | // The emulator only fills in the dwarf register numbers (and in some cases |
| 938 | // the generic register numbers). Get the full register info from the |
| 939 | // register context based on the dwarf register numbers. |
| 940 | const RegisterInfo *full_reg_info = |
| 941 | emulator_baton->m_reg_context->GetRegisterInfo( |
| 942 | eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); |
| 943 | |
| 944 | bool error = |
| 945 | emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); |
| 946 | return error; |
| 947 | } |
| 948 | |
| 949 | static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton, |
| 950 | const EmulateInstruction::Context &context, |
| 951 | const RegisterInfo *reg_info, |
| 952 | const RegisterValue ®_value) { |
| 953 | EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); |
| 954 | emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = |
| 955 | reg_value; |
| 956 | return true; |
| 957 | } |
| 958 | |
| 959 | static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton, |
| 960 | const EmulateInstruction::Context &context, |
| 961 | lldb::addr_t addr, const void *dst, |
| 962 | size_t length) { |
| 963 | return length; |
| 964 | } |
| 965 | |
| 966 | bool ProcessFreeBSD::SingleStepBreakpointHit( |
| 967 | void *baton, lldb_private::StoppointCallbackContext *context, |
| 968 | lldb::user_id_t break_id, lldb::user_id_t break_loc_id) { |
| 969 | return false; |
| 970 | } |
| 971 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 972 | Status ProcessFreeBSD::SetSoftwareSingleStepBreakpoint(lldb::tid_t tid, |
| 973 | lldb::addr_t addr) { |
| 974 | Status error; |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 975 | |
| 976 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 977 | if (log) { |
| 978 | log->Printf("ProcessFreeBSD::%s addr = 0x%" PRIx64, __FUNCTION__, addr); |
| 979 | log->Printf("SoftwareBreakpoint::%s addr = 0x%" PRIx64, __FUNCTION__, addr); |
| 980 | } |
| 981 | |
| 982 | // Validate the address. |
| 983 | if (addr == LLDB_INVALID_ADDRESS) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 984 | return Status("ProcessFreeBSD::%s invalid load address specified.", |
| 985 | __FUNCTION__); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 986 | |
| 987 | Breakpoint *const sw_step_break = |
| 988 | m_process->GetTarget().CreateBreakpoint(addr, true, false).get(); |
| 989 | sw_step_break->SetCallback(SingleStepBreakpointHit, this, true); |
| 990 | sw_step_break->SetBreakpointKind("software-signle-step"); |
| 991 | |
| 992 | if (log) |
| 993 | log->Printf("ProcessFreeBSD::%s addr = 0x%" PRIx64 " -- SUCCESS", |
| 994 | __FUNCTION__, addr); |
| 995 | |
| 996 | m_threads_stepping_with_breakpoint.insert({tid, sw_step_break->GetID()}); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 997 | return Status(); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | bool ProcessFreeBSD::IsSoftwareStepBreakpoint(lldb::tid_t tid) { |
| 1001 | ThreadSP thread = GetThreadList().FindThreadByID(tid); |
| 1002 | if (!thread) |
| 1003 | return false; |
| 1004 | |
| 1005 | assert(thread->GetRegisterContext()); |
| 1006 | lldb::addr_t stop_pc = thread->GetRegisterContext()->GetPC(); |
| 1007 | |
| 1008 | const auto &iter = m_threads_stepping_with_breakpoint.find(tid); |
| 1009 | if (iter == m_threads_stepping_with_breakpoint.end()) |
| 1010 | return false; |
| 1011 | |
| 1012 | lldb::break_id_t bp_id = iter->second; |
| 1013 | BreakpointSP bp = GetTarget().GetBreakpointByID(bp_id); |
| 1014 | if (!bp) |
| 1015 | return false; |
| 1016 | |
| 1017 | BreakpointLocationSP bp_loc = bp->FindLocationByAddress(stop_pc); |
| 1018 | if (!bp_loc) |
| 1019 | return false; |
| 1020 | |
| 1021 | GetTarget().RemoveBreakpointByID(bp_id); |
| 1022 | m_threads_stepping_with_breakpoint.erase(tid); |
| 1023 | return true; |
| 1024 | } |
| 1025 | |
| 1026 | bool ProcessFreeBSD::SupportHardwareSingleStepping() const { |
| 1027 | lldb_private::ArchSpec arch = GetTarget().GetArchitecture(); |
| 1028 | if (arch.GetMachine() == llvm::Triple::arm || |
| 1029 | arch.GetMachine() == llvm::Triple::mips64 || |
| 1030 | arch.GetMachine() == llvm::Triple::mips64el || |
| 1031 | arch.GetMachine() == llvm::Triple::mips || |
| 1032 | arch.GetMachine() == llvm::Triple::mipsel) |
| 1033 | return false; |
| 1034 | return true; |
| 1035 | } |
| 1036 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1037 | Status ProcessFreeBSD::SetupSoftwareSingleStepping(lldb::tid_t tid) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 1038 | std::unique_ptr<EmulateInstruction> emulator_up( |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 1039 | EmulateInstruction::FindPlugin(GetTarget().GetArchitecture(), |
| 1040 | eInstructionTypePCModifying, nullptr)); |
| 1041 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 1042 | if (emulator_up == nullptr) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1043 | return Status("Instruction emulator not found!"); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 1044 | |
| 1045 | FreeBSDThread *thread = static_cast<FreeBSDThread *>( |
| 1046 | m_thread_list.FindThreadByID(tid, false).get()); |
| 1047 | if (thread == NULL) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1048 | return Status("Thread not found not found!"); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 1049 | |
| 1050 | lldb::RegisterContextSP register_context_sp = thread->GetRegisterContext(); |
| 1051 | |
| 1052 | EmulatorBaton baton(this, register_context_sp.get()); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 1053 | emulator_up->SetBaton(&baton); |
| 1054 | emulator_up->SetReadMemCallback(&ReadMemoryCallback); |
| 1055 | emulator_up->SetReadRegCallback(&ReadRegisterCallback); |
| 1056 | emulator_up->SetWriteMemCallback(&WriteMemoryCallback); |
| 1057 | emulator_up->SetWriteRegCallback(&WriteRegisterCallback); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 1058 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 1059 | if (!emulator_up->ReadInstruction()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1060 | return Status("Read instruction failed!"); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 1061 | |
| 1062 | bool emulation_result = |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 1063 | emulator_up->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 1064 | const RegisterInfo *reg_info_pc = register_context_sp->GetRegisterInfo( |
| 1065 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 1066 | auto pc_it = |
| 1067 | baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); |
| 1068 | |
| 1069 | lldb::addr_t next_pc; |
| 1070 | if (emulation_result) { |
| 1071 | assert(pc_it != baton.m_register_values.end() && |
| 1072 | "Emulation was successful but PC wasn't updated"); |
| 1073 | next_pc = pc_it->second.GetAsUInt64(); |
| 1074 | } else if (pc_it == baton.m_register_values.end()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1075 | // Emulate instruction failed and it haven't changed PC. Advance PC with |
| 1076 | // the size of the current opcode because the emulation of all |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 1077 | // PC modifying instruction should be successful. The failure most |
| 1078 | // likely caused by a not supported instruction which don't modify PC. |
| 1079 | next_pc = |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame^] | 1080 | register_context_sp->GetPC() + emulator_up->GetOpcode().GetByteSize(); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 1081 | } else { |
| 1082 | // The instruction emulation failed after it modified the PC. It is an |
| 1083 | // unknown error where we can't continue because the next instruction is |
| 1084 | // modifying the PC but we don't know how. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1085 | return Status("Instruction emulation failed unexpectedly"); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | SetSoftwareSingleStepBreakpoint(tid, next_pc); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1089 | return Status(); |
Ed Maste | 31f0180 | 2017-01-24 14:34:49 +0000 | [diff] [blame] | 1090 | } |