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