1 //===-- PlatformFreeBSD.cpp -------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "PlatformFreeBSD.h" 11 #include "lldb/Host/Config.h" 12 13 #include <stdio.h> 14 #ifndef LLDB_DISABLE_POSIX 15 #include <sys/utsname.h> 16 #endif 17 18 #include "lldb/Breakpoint/BreakpointLocation.h" 19 #include "lldb/Breakpoint/BreakpointSite.h" 20 #include "lldb/Core/Debugger.h" 21 #include "lldb/Core/PluginManager.h" 22 #include "lldb/Host/HostInfo.h" 23 #include "lldb/Target/Process.h" 24 #include "lldb/Target/Target.h" 25 #include "lldb/Utility/FileSpec.h" 26 #include "lldb/Utility/Log.h" 27 #include "lldb/Utility/State.h" 28 #include "lldb/Utility/Status.h" 29 #include "lldb/Utility/StreamString.h" 30 31 // Define these constants from FreeBSD mman.h for use when targeting remote 32 // FreeBSD systems even when host has different values. 33 #define MAP_PRIVATE 0x0002 34 #define MAP_ANON 0x1000 35 36 using namespace lldb; 37 using namespace lldb_private; 38 using namespace lldb_private::platform_freebsd; 39 40 static uint32_t g_initialize_count = 0; 41 42 //------------------------------------------------------------------ 43 44 PlatformSP PlatformFreeBSD::CreateInstance(bool force, const ArchSpec *arch) { 45 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); 46 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force, 47 arch ? arch->GetArchitectureName() : "<null>", 48 arch ? arch->GetTriple().getTriple() : "<null>"); 49 50 bool create = force; 51 if (!create && arch && arch->IsValid()) { 52 const llvm::Triple &triple = arch->GetTriple(); 53 switch (triple.getOS()) { 54 case llvm::Triple::FreeBSD: 55 create = true; 56 break; 57 58 #if defined(__FreeBSD__) 59 // Only accept "unknown" for the OS if the host is BSD and it "unknown" 60 // wasn't specified (it was just returned because it was NOT specified) 61 case llvm::Triple::OSType::UnknownOS: 62 create = !arch->TripleOSWasSpecified(); 63 break; 64 #endif 65 default: 66 break; 67 } 68 } 69 LLDB_LOG(log, "create = {0}", create); 70 if (create) { 71 return PlatformSP(new PlatformFreeBSD(false)); 72 } 73 return PlatformSP(); 74 } 75 76 ConstString PlatformFreeBSD::GetPluginNameStatic(bool is_host) { 77 if (is_host) { 78 static ConstString g_host_name(Platform::GetHostPlatformName()); 79 return g_host_name; 80 } else { 81 static ConstString g_remote_name("remote-freebsd"); 82 return g_remote_name; 83 } 84 } 85 86 const char *PlatformFreeBSD::GetPluginDescriptionStatic(bool is_host) { 87 if (is_host) 88 return "Local FreeBSD user platform plug-in."; 89 else 90 return "Remote FreeBSD user platform plug-in."; 91 } 92 93 ConstString PlatformFreeBSD::GetPluginName() { 94 return GetPluginNameStatic(IsHost()); 95 } 96 97 void PlatformFreeBSD::Initialize() { 98 Platform::Initialize(); 99 100 if (g_initialize_count++ == 0) { 101 #if defined(__FreeBSD__) 102 PlatformSP default_platform_sp(new PlatformFreeBSD(true)); 103 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 104 Platform::SetHostPlatform(default_platform_sp); 105 #endif 106 PluginManager::RegisterPlugin( 107 PlatformFreeBSD::GetPluginNameStatic(false), 108 PlatformFreeBSD::GetPluginDescriptionStatic(false), 109 PlatformFreeBSD::CreateInstance, nullptr); 110 } 111 } 112 113 void PlatformFreeBSD::Terminate() { 114 if (g_initialize_count > 0) { 115 if (--g_initialize_count == 0) { 116 PluginManager::UnregisterPlugin(PlatformFreeBSD::CreateInstance); 117 } 118 } 119 120 PlatformPOSIX::Terminate(); 121 } 122 123 //------------------------------------------------------------------ 124 /// Default Constructor 125 //------------------------------------------------------------------ 126 PlatformFreeBSD::PlatformFreeBSD(bool is_host) 127 : PlatformPOSIX(is_host) // This is the local host platform 128 {} 129 130 PlatformFreeBSD::~PlatformFreeBSD() = default; 131 132 bool PlatformFreeBSD::GetSupportedArchitectureAtIndex(uint32_t idx, 133 ArchSpec &arch) { 134 if (IsHost()) { 135 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 136 if (hostArch.GetTriple().isOSFreeBSD()) { 137 if (idx == 0) { 138 arch = hostArch; 139 return arch.IsValid(); 140 } else if (idx == 1) { 141 // If the default host architecture is 64-bit, look for a 32-bit 142 // variant 143 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) { 144 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 145 return arch.IsValid(); 146 } 147 } 148 } 149 } else { 150 if (m_remote_platform_sp) 151 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch); 152 153 llvm::Triple triple; 154 // Set the OS to FreeBSD 155 triple.setOS(llvm::Triple::FreeBSD); 156 // Set the architecture 157 switch (idx) { 158 case 0: 159 triple.setArchName("x86_64"); 160 break; 161 case 1: 162 triple.setArchName("i386"); 163 break; 164 case 2: 165 triple.setArchName("aarch64"); 166 break; 167 case 3: 168 triple.setArchName("arm"); 169 break; 170 case 4: 171 triple.setArchName("mips64"); 172 break; 173 case 5: 174 triple.setArchName("mips"); 175 break; 176 case 6: 177 triple.setArchName("ppc64"); 178 break; 179 case 7: 180 triple.setArchName("ppc"); 181 break; 182 default: 183 return false; 184 } 185 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the 186 // vendor by calling triple.SetVendorName("unknown") so that it is a 187 // "unspecified unknown". This means when someone calls 188 // triple.GetVendorName() it will return an empty string which indicates 189 // that the vendor can be set when two architectures are merged 190 191 // Now set the triple into "arch" and return true 192 arch.SetTriple(triple); 193 return true; 194 } 195 return false; 196 } 197 198 void PlatformFreeBSD::GetStatus(Stream &strm) { 199 Platform::GetStatus(strm); 200 201 #ifndef LLDB_DISABLE_POSIX 202 // Display local kernel information only when we are running in host mode. 203 // Otherwise, we would end up printing non-FreeBSD information (when running 204 // on Mac OS for example). 205 if (IsHost()) { 206 struct utsname un; 207 208 if (uname(&un)) 209 return; 210 211 strm.Printf(" Kernel: %s\n", un.sysname); 212 strm.Printf(" Release: %s\n", un.release); 213 strm.Printf(" Version: %s\n", un.version); 214 } 215 #endif 216 } 217 218 size_t 219 PlatformFreeBSD::GetSoftwareBreakpointTrapOpcode(Target &target, 220 BreakpointSite *bp_site) { 221 switch (target.GetArchitecture().GetMachine()) { 222 case llvm::Triple::arm: { 223 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0)); 224 AddressClass addr_class = AddressClass::eUnknown; 225 226 if (bp_loc_sp) { 227 addr_class = bp_loc_sp->GetAddress().GetAddressClass(); 228 if (addr_class == AddressClass::eUnknown && 229 (bp_loc_sp->GetAddress().GetFileAddress() & 1)) 230 addr_class = AddressClass::eCodeAlternateISA; 231 } 232 233 if (addr_class == AddressClass::eCodeAlternateISA) { 234 // TODO: Enable when FreeBSD supports thumb breakpoints. 235 // FreeBSD kernel as of 10.x, does not support thumb breakpoints 236 return 0; 237 } 238 239 static const uint8_t g_arm_breakpoint_opcode[] = {0xFE, 0xDE, 0xFF, 0xE7}; 240 size_t trap_opcode_size = sizeof(g_arm_breakpoint_opcode); 241 assert(bp_site); 242 if (bp_site->SetTrapOpcode(g_arm_breakpoint_opcode, trap_opcode_size)) 243 return trap_opcode_size; 244 } 245 LLVM_FALLTHROUGH; 246 default: 247 return Platform::GetSoftwareBreakpointTrapOpcode(target, bp_site); 248 } 249 } 250 251 Status PlatformFreeBSD::LaunchProcess(ProcessLaunchInfo &launch_info) { 252 Status error; 253 if (IsHost()) { 254 error = Platform::LaunchProcess(launch_info); 255 } else { 256 if (m_remote_platform_sp) 257 error = m_remote_platform_sp->LaunchProcess(launch_info); 258 else 259 error.SetErrorString("the platform is not currently connected"); 260 } 261 return error; 262 } 263 264 lldb::ProcessSP PlatformFreeBSD::Attach(ProcessAttachInfo &attach_info, 265 Debugger &debugger, Target *target, 266 Status &error) { 267 lldb::ProcessSP process_sp; 268 if (IsHost()) { 269 if (target == NULL) { 270 TargetSP new_target_sp; 271 ArchSpec emptyArchSpec; 272 273 error = debugger.GetTargetList().CreateTarget( 274 debugger, "", emptyArchSpec, eLoadDependentsNo, m_remote_platform_sp, 275 new_target_sp); 276 target = new_target_sp.get(); 277 } else 278 error.Clear(); 279 280 if (target && error.Success()) { 281 debugger.GetTargetList().SetSelectedTarget(target); 282 // The freebsd always currently uses the GDB remote debugger plug-in so 283 // even when debugging locally we are debugging remotely! Just like the 284 // darwin plugin. 285 process_sp = target->CreateProcess( 286 attach_info.GetListenerForProcess(debugger), "gdb-remote", NULL); 287 288 if (process_sp) 289 error = process_sp->Attach(attach_info); 290 } 291 } else { 292 if (m_remote_platform_sp) 293 process_sp = 294 m_remote_platform_sp->Attach(attach_info, debugger, target, error); 295 else 296 error.SetErrorString("the platform is not currently connected"); 297 } 298 return process_sp; 299 } 300 301 // FreeBSD processes cannot yet be launched by spawning and attaching. 302 bool PlatformFreeBSD::CanDebugProcess() { 303 return false; 304 } 305 306 void PlatformFreeBSD::CalculateTrapHandlerSymbolNames() { 307 m_trap_handlers.push_back(ConstString("_sigtramp")); 308 } 309 310 MmapArgList PlatformFreeBSD::GetMmapArgumentList(const ArchSpec &arch, 311 addr_t addr, addr_t length, 312 unsigned prot, unsigned flags, 313 addr_t fd, addr_t offset) { 314 uint64_t flags_platform = 0; 315 316 if (flags & eMmapFlagsPrivate) 317 flags_platform |= MAP_PRIVATE; 318 if (flags & eMmapFlagsAnon) 319 flags_platform |= MAP_ANON; 320 321 MmapArgList args({addr, length, prot, flags_platform, fd, offset}); 322 if (arch.GetTriple().getArch() == llvm::Triple::x86) 323 args.push_back(0); 324 return args; 325 } 326