1 //===-- NativeThreadFreeBSD.cpp -------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "NativeThreadFreeBSD.h" 10 #include "NativeRegisterContextFreeBSD.h" 11 12 #include "NativeProcessFreeBSD.h" 13 14 #include "Plugins/Process/POSIX/CrashReason.h" 15 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 16 #include "lldb/Utility/LLDBAssert.h" 17 #include "lldb/Utility/RegisterValue.h" 18 #include "lldb/Utility/State.h" 19 #include "llvm/Support/Errno.h" 20 21 // clang-format off 22 #include <sys/types.h> 23 #include <sys/ptrace.h> 24 #include <sys/sysctl.h> 25 #include <sys/user.h> 26 // clang-format on 27 28 #include <sstream> 29 #include <vector> 30 31 using namespace lldb; 32 using namespace lldb_private; 33 using namespace lldb_private::process_freebsd; 34 35 NativeThreadFreeBSD::NativeThreadFreeBSD(NativeProcessFreeBSD &process, 36 lldb::tid_t tid) 37 : NativeThreadProtocol(process, tid), m_state(StateType::eStateInvalid), 38 m_stop_info(), 39 m_reg_context_up( 40 NativeRegisterContextFreeBSD::CreateHostNativeRegisterContextFreeBSD( 41 process.GetArchitecture(), *this)), 42 m_stop_description() {} 43 44 Status NativeThreadFreeBSD::Resume() { 45 Status ret = NativeProcessFreeBSD::PtraceWrapper(PT_RESUME, GetID()); 46 if (!ret.Success()) 47 return ret; 48 ret = NativeProcessFreeBSD::PtraceWrapper(PT_CLEARSTEP, GetID()); 49 // we can get EINVAL if the architecture in question does not support 50 // hardware single-stepping -- that's fine, we have nothing to clear 51 // then 52 if (ret.GetError() == EINVAL) 53 ret.Clear(); 54 if (ret.Success()) 55 SetRunning(); 56 return ret; 57 } 58 59 Status NativeThreadFreeBSD::SingleStep() { 60 Status ret = NativeProcessFreeBSD::PtraceWrapper(PT_RESUME, GetID()); 61 if (!ret.Success()) 62 return ret; 63 ret = NativeProcessFreeBSD::PtraceWrapper(PT_SETSTEP, GetID()); 64 if (ret.Success()) 65 SetStepping(); 66 return ret; 67 } 68 69 Status NativeThreadFreeBSD::Suspend() { 70 Status ret = NativeProcessFreeBSD::PtraceWrapper(PT_SUSPEND, GetID()); 71 if (ret.Success()) 72 SetStopped(); 73 return ret; 74 } 75 76 void NativeThreadFreeBSD::SetStoppedBySignal(uint32_t signo, 77 const siginfo_t *info) { 78 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 79 LLDB_LOG(log, "tid = {0} in called with signal {1}", GetID(), signo); 80 81 SetStopped(); 82 83 m_stop_info.reason = StopReason::eStopReasonSignal; 84 m_stop_info.details.signal.signo = signo; 85 86 m_stop_description.clear(); 87 if (info) { 88 switch (signo) { 89 case SIGSEGV: 90 case SIGBUS: 91 case SIGFPE: 92 case SIGILL: 93 const auto reason = GetCrashReason(*info); 94 m_stop_description = GetCrashReasonString(reason, *info); 95 break; 96 } 97 } 98 } 99 100 void NativeThreadFreeBSD::SetStoppedByBreakpoint() { 101 SetStopped(); 102 m_stop_info.reason = StopReason::eStopReasonBreakpoint; 103 m_stop_info.details.signal.signo = SIGTRAP; 104 } 105 106 void NativeThreadFreeBSD::SetStoppedByTrace() { 107 SetStopped(); 108 m_stop_info.reason = StopReason::eStopReasonTrace; 109 m_stop_info.details.signal.signo = SIGTRAP; 110 } 111 112 void NativeThreadFreeBSD::SetStoppedByExec() { 113 SetStopped(); 114 m_stop_info.reason = StopReason::eStopReasonExec; 115 m_stop_info.details.signal.signo = SIGTRAP; 116 } 117 118 void NativeThreadFreeBSD::SetStoppedByWatchpoint(uint32_t wp_index) { 119 lldbassert(wp_index != LLDB_INVALID_INDEX32 && "wp_index cannot be invalid"); 120 121 std::ostringstream ostr; 122 ostr << GetRegisterContext().GetWatchpointAddress(wp_index) << " "; 123 ostr << wp_index; 124 125 ostr << " " << GetRegisterContext().GetWatchpointHitAddress(wp_index); 126 127 SetStopped(); 128 m_stop_description = ostr.str(); 129 m_stop_info.reason = StopReason::eStopReasonWatchpoint; 130 m_stop_info.details.signal.signo = SIGTRAP; 131 } 132 133 void NativeThreadFreeBSD::SetStoppedWithNoReason() { 134 SetStopped(); 135 136 m_stop_info.reason = StopReason::eStopReasonNone; 137 m_stop_info.details.signal.signo = 0; 138 } 139 140 void NativeThreadFreeBSD::SetStopped() { 141 const StateType new_state = StateType::eStateStopped; 142 m_state = new_state; 143 m_stop_description.clear(); 144 } 145 146 void NativeThreadFreeBSD::SetRunning() { 147 m_state = StateType::eStateRunning; 148 m_stop_info.reason = StopReason::eStopReasonNone; 149 } 150 151 void NativeThreadFreeBSD::SetStepping() { 152 m_state = StateType::eStateStepping; 153 m_stop_info.reason = StopReason::eStopReasonNone; 154 } 155 156 std::string NativeThreadFreeBSD::GetName() { 157 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 158 159 std::vector<struct kinfo_proc> kp; 160 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, 161 static_cast<int>(GetProcess().GetID())}; 162 163 while (1) { 164 size_t len = kp.size() * sizeof(struct kinfo_proc); 165 void *ptr = len == 0 ? nullptr : kp.data(); 166 int error = ::sysctl(mib, 4, ptr, &len, nullptr, 0); 167 if (ptr == nullptr || (error != 0 && errno == ENOMEM)) { 168 kp.resize(len / sizeof(struct kinfo_proc)); 169 continue; 170 } 171 if (error != 0) { 172 len = 0; 173 LLDB_LOG(log, "tid = {0} in state {1} failed to get thread name: {2}", 174 GetID(), m_state, strerror(errno)); 175 } 176 kp.resize(len / sizeof(struct kinfo_proc)); 177 break; 178 } 179 180 for (auto &procinfo : kp) { 181 if (procinfo.ki_tid == static_cast<lwpid_t>(GetID())) 182 return procinfo.ki_tdname; 183 } 184 185 return ""; 186 } 187 188 lldb::StateType NativeThreadFreeBSD::GetState() { return m_state; } 189 190 bool NativeThreadFreeBSD::GetStopReason(ThreadStopInfo &stop_info, 191 std::string &description) { 192 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 193 description.clear(); 194 195 switch (m_state) { 196 case eStateStopped: 197 case eStateCrashed: 198 case eStateExited: 199 case eStateSuspended: 200 case eStateUnloaded: 201 stop_info = m_stop_info; 202 description = m_stop_description; 203 204 return true; 205 206 case eStateInvalid: 207 case eStateConnected: 208 case eStateAttaching: 209 case eStateLaunching: 210 case eStateRunning: 211 case eStateStepping: 212 case eStateDetached: 213 LLDB_LOG(log, "tid = {0} in state {1} cannot answer stop reason", GetID(), 214 StateAsCString(m_state)); 215 return false; 216 } 217 llvm_unreachable("unhandled StateType!"); 218 } 219 220 NativeRegisterContextFreeBSD &NativeThreadFreeBSD::GetRegisterContext() { 221 assert(m_reg_context_up); 222 return *m_reg_context_up; 223 } 224 225 Status NativeThreadFreeBSD::SetWatchpoint(lldb::addr_t addr, size_t size, 226 uint32_t watch_flags, bool hardware) { 227 assert(m_state == eStateStopped); 228 if (!hardware) 229 return Status("not implemented"); 230 Status error = RemoveWatchpoint(addr); 231 if (error.Fail()) 232 return error; 233 uint32_t wp_index = 234 GetRegisterContext().SetHardwareWatchpoint(addr, size, watch_flags); 235 if (wp_index == LLDB_INVALID_INDEX32) 236 return Status("Setting hardware watchpoint failed."); 237 m_watchpoint_index_map.insert({addr, wp_index}); 238 return Status(); 239 } 240 241 Status NativeThreadFreeBSD::RemoveWatchpoint(lldb::addr_t addr) { 242 auto wp = m_watchpoint_index_map.find(addr); 243 if (wp == m_watchpoint_index_map.end()) 244 return Status(); 245 uint32_t wp_index = wp->second; 246 m_watchpoint_index_map.erase(wp); 247 if (GetRegisterContext().ClearHardwareWatchpoint(wp_index)) 248 return Status(); 249 return Status("Clearing hardware watchpoint failed."); 250 } 251 252 Status NativeThreadFreeBSD::SetHardwareBreakpoint(lldb::addr_t addr, 253 size_t size) { 254 assert(m_state == eStateStopped); 255 Status error = RemoveHardwareBreakpoint(addr); 256 if (error.Fail()) 257 return error; 258 259 uint32_t bp_index = GetRegisterContext().SetHardwareBreakpoint(addr, size); 260 261 if (bp_index == LLDB_INVALID_INDEX32) 262 return Status("Setting hardware breakpoint failed."); 263 264 m_hw_break_index_map.insert({addr, bp_index}); 265 return Status(); 266 } 267 268 Status NativeThreadFreeBSD::RemoveHardwareBreakpoint(lldb::addr_t addr) { 269 auto bp = m_hw_break_index_map.find(addr); 270 if (bp == m_hw_break_index_map.end()) 271 return Status(); 272 273 uint32_t bp_index = bp->second; 274 if (GetRegisterContext().ClearHardwareBreakpoint(bp_index)) { 275 m_hw_break_index_map.erase(bp); 276 return Status(); 277 } 278 279 return Status("Clearing hardware breakpoint failed."); 280 } 281 282 llvm::Error 283 NativeThreadFreeBSD::CopyWatchpointsFrom(NativeThreadFreeBSD &source) { 284 llvm::Error s = GetRegisterContext().CopyHardwareWatchpointsFrom( 285 source.GetRegisterContext()); 286 if (!s) { 287 m_watchpoint_index_map = source.m_watchpoint_index_map; 288 m_hw_break_index_map = source.m_hw_break_index_map; 289 } 290 return s; 291 } 292