1 //===-- PlatformOpenBSD.cpp -------------------------------------*- C++ -*-===// 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 "PlatformOpenBSD.h" 10 #include "lldb/Host/Config.h" 11 12 #include <stdio.h> 13 #if LLDB_ENABLE_POSIX 14 #include <sys/utsname.h> 15 #endif 16 17 #include "lldb/Core/Debugger.h" 18 #include "lldb/Core/PluginManager.h" 19 #include "lldb/Host/HostInfo.h" 20 #include "lldb/Target/Process.h" 21 #include "lldb/Target/Target.h" 22 #include "lldb/Utility/FileSpec.h" 23 #include "lldb/Utility/Log.h" 24 #include "lldb/Utility/State.h" 25 #include "lldb/Utility/Status.h" 26 #include "lldb/Utility/StreamString.h" 27 28 // Define these constants from OpenBSD mman.h for use when targeting remote 29 // openbsd systems even when host has different values. 30 #define MAP_PRIVATE 0x0002 31 #define MAP_ANON 0x1000 32 33 using namespace lldb; 34 using namespace lldb_private; 35 using namespace lldb_private::platform_openbsd; 36 37 static uint32_t g_initialize_count = 0; 38 39 40 PlatformSP PlatformOpenBSD::CreateInstance(bool force, const ArchSpec *arch) { 41 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); 42 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force, 43 arch ? arch->GetArchitectureName() : "<null>", 44 arch ? arch->GetTriple().getTriple() : "<null>"); 45 46 bool create = force; 47 if (!create && arch && arch->IsValid()) { 48 const llvm::Triple &triple = arch->GetTriple(); 49 switch (triple.getOS()) { 50 case llvm::Triple::OpenBSD: 51 create = true; 52 break; 53 54 #if defined(__OpenBSD__) 55 // Only accept "unknown" for the OS if the host is BSD and it "unknown" 56 // wasn't specified (it was just returned because it was NOT specified) 57 case llvm::Triple::OSType::UnknownOS: 58 create = !arch->TripleOSWasSpecified(); 59 break; 60 #endif 61 default: 62 break; 63 } 64 } 65 LLDB_LOG(log, "create = {0}", create); 66 if (create) { 67 return PlatformSP(new PlatformOpenBSD(false)); 68 } 69 return PlatformSP(); 70 } 71 72 ConstString PlatformOpenBSD::GetPluginNameStatic(bool is_host) { 73 if (is_host) { 74 static ConstString g_host_name(Platform::GetHostPlatformName()); 75 return g_host_name; 76 } else { 77 static ConstString g_remote_name("remote-openbsd"); 78 return g_remote_name; 79 } 80 } 81 82 const char *PlatformOpenBSD::GetPluginDescriptionStatic(bool is_host) { 83 if (is_host) 84 return "Local OpenBSD user platform plug-in."; 85 else 86 return "Remote OpenBSD user platform plug-in."; 87 } 88 89 ConstString PlatformOpenBSD::GetPluginName() { 90 return GetPluginNameStatic(IsHost()); 91 } 92 93 void PlatformOpenBSD::Initialize() { 94 PlatformPOSIX::Initialize(); 95 96 if (g_initialize_count++ == 0) { 97 #if defined(__OpenBSD__) 98 PlatformSP default_platform_sp(new PlatformOpenBSD(true)); 99 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 100 Platform::SetHostPlatform(default_platform_sp); 101 #endif 102 PluginManager::RegisterPlugin( 103 PlatformOpenBSD::GetPluginNameStatic(false), 104 PlatformOpenBSD::GetPluginDescriptionStatic(false), 105 PlatformOpenBSD::CreateInstance, nullptr); 106 } 107 } 108 109 void PlatformOpenBSD::Terminate() { 110 if (g_initialize_count > 0) { 111 if (--g_initialize_count == 0) { 112 PluginManager::UnregisterPlugin(PlatformOpenBSD::CreateInstance); 113 } 114 } 115 116 PlatformPOSIX::Terminate(); 117 } 118 119 /// Default Constructor 120 PlatformOpenBSD::PlatformOpenBSD(bool is_host) 121 : PlatformPOSIX(is_host) // This is the local host platform 122 {} 123 124 PlatformOpenBSD::~PlatformOpenBSD() = default; 125 126 bool PlatformOpenBSD::GetSupportedArchitectureAtIndex(uint32_t idx, 127 ArchSpec &arch) { 128 if (IsHost()) { 129 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 130 if (hostArch.GetTriple().isOSOpenBSD()) { 131 if (idx == 0) { 132 arch = hostArch; 133 return arch.IsValid(); 134 } 135 } 136 } else { 137 if (m_remote_platform_sp) 138 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch); 139 140 llvm::Triple triple; 141 // Set the OS to OpenBSD 142 triple.setOS(llvm::Triple::OpenBSD); 143 // Set the architecture 144 switch (idx) { 145 case 0: 146 triple.setArchName("x86_64"); 147 break; 148 case 1: 149 triple.setArchName("i386"); 150 break; 151 case 2: 152 triple.setArchName("aarch64"); 153 break; 154 case 3: 155 triple.setArchName("arm"); 156 break; 157 default: 158 return false; 159 } 160 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the 161 // vendor by calling triple.SetVendorName("unknown") so that it is a 162 // "unspecified unknown". This means when someone calls 163 // triple.GetVendorName() it will return an empty string which indicates 164 // that the vendor can be set when two architectures are merged 165 166 // Now set the triple into "arch" and return true 167 arch.SetTriple(triple); 168 return true; 169 } 170 return false; 171 } 172 173 void PlatformOpenBSD::GetStatus(Stream &strm) { 174 Platform::GetStatus(strm); 175 176 #if LLDB_ENABLE_POSIX 177 // Display local kernel information only when we are running in host mode. 178 // Otherwise, we would end up printing non-OpenBSD information (when running 179 // on Mac OS for example). 180 if (IsHost()) { 181 struct utsname un; 182 183 if (uname(&un)) 184 return; 185 186 strm.Printf(" Kernel: %s\n", un.sysname); 187 strm.Printf(" Release: %s\n", un.release); 188 strm.Printf(" Version: %s\n", un.version); 189 } 190 #endif 191 } 192 193 bool PlatformOpenBSD::CanDebugProcess() { 194 if (IsHost()) { 195 return true; 196 } else { 197 // If we're connected, we can debug. 198 return IsConnected(); 199 } 200 } 201 202 // For local debugging, OpenBSD will override the debug logic to use llgs-launch 203 // rather than lldb-launch, llgs-attach. This differs from current lldb- 204 // launch, debugserver-attach approach on MacOSX. 205 lldb::ProcessSP 206 PlatformOpenBSD::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger, 207 Target *target, // Can be NULL, if NULL create a new 208 // target, else use existing one 209 Status &error) { 210 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); 211 LLDB_LOG(log, "target {0}", target); 212 213 // If we're a remote host, use standard behavior from parent class. 214 if (!IsHost()) 215 return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error); 216 217 // 218 // For local debugging, we'll insist on having ProcessGDBRemote create the 219 // process. 220 // 221 222 ProcessSP process_sp; 223 224 // Make sure we stop at the entry point 225 launch_info.GetFlags().Set(eLaunchFlagDebug); 226 227 // We always launch the process we are going to debug in a separate process 228 // group, since then we can handle ^C interrupts ourselves w/o having to 229 // worry about the target getting them as well. 230 launch_info.SetLaunchInSeparateProcessGroup(true); 231 232 // Ensure we have a target. 233 if (target == nullptr) { 234 LLDB_LOG(log, "creating new target"); 235 TargetSP new_target_sp; 236 error = debugger.GetTargetList().CreateTarget(debugger, "", "", eLoadDependentsNo, 237 nullptr, new_target_sp); 238 if (error.Fail()) { 239 LLDB_LOG(log, "failed to create new target: {0}", error); 240 return process_sp; 241 } 242 243 target = new_target_sp.get(); 244 if (!target) { 245 error.SetErrorString("CreateTarget() returned nullptr"); 246 LLDB_LOG(log, "error: {0}", error); 247 return process_sp; 248 } 249 } 250 251 // Mark target as currently selected target. 252 debugger.GetTargetList().SetSelectedTarget(target); 253 254 // Now create the gdb-remote process. 255 LLDB_LOG(log, "having target create process with gdb-remote plugin"); 256 process_sp = target->CreateProcess( 257 launch_info.GetListener(), "gdb-remote", nullptr); 258 259 if (!process_sp) { 260 error.SetErrorString("CreateProcess() failed for gdb-remote process"); 261 LLDB_LOG(log, "error: {0}", error); 262 return process_sp; 263 } 264 265 LLDB_LOG(log, "successfully created process"); 266 // Adjust launch for a hijacker. 267 ListenerSP listener_sp; 268 if (!launch_info.GetHijackListener()) { 269 LLDB_LOG(log, "setting up hijacker"); 270 listener_sp = 271 Listener::MakeListener("lldb.PlatformOpenBSD.DebugProcess.hijack"); 272 launch_info.SetHijackListener(listener_sp); 273 process_sp->HijackProcessEvents(listener_sp); 274 } 275 276 // Log file actions. 277 if (log) { 278 LLDB_LOG(log, "launching process with the following file actions:"); 279 StreamString stream; 280 size_t i = 0; 281 const FileAction *file_action; 282 while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) { 283 file_action->Dump(stream); 284 LLDB_LOG(log, "{0}", stream.GetData()); 285 stream.Clear(); 286 } 287 } 288 289 // Do the launch. 290 error = process_sp->Launch(launch_info); 291 if (error.Success()) { 292 // Handle the hijacking of process events. 293 if (listener_sp) { 294 const StateType state = process_sp->WaitForProcessToStop( 295 llvm::None, NULL, false, listener_sp); 296 297 LLDB_LOG(log, "pid {0} state {0}", process_sp->GetID(), state); 298 } 299 300 // Hook up process PTY if we have one (which we should for local debugging 301 // with llgs). 302 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 303 if (pty_fd != PseudoTerminal::invalid_fd) { 304 process_sp->SetSTDIOFileDescriptor(pty_fd); 305 LLDB_LOG(log, "hooked up STDIO pty to process"); 306 } else 307 LLDB_LOG(log, "not using process STDIO pty"); 308 } else { 309 LLDB_LOG(log, "process launch failed: {0}", error); 310 // FIXME figure out appropriate cleanup here. Do we delete the target? Do 311 // we delete the process? Does our caller do that? 312 } 313 314 return process_sp; 315 } 316 317 void PlatformOpenBSD::CalculateTrapHandlerSymbolNames() { 318 m_trap_handlers.push_back(ConstString("_sigtramp")); 319 } 320 321 MmapArgList PlatformOpenBSD::GetMmapArgumentList(const ArchSpec &arch, 322 addr_t addr, addr_t length, 323 unsigned prot, unsigned flags, 324 addr_t fd, addr_t offset) { 325 uint64_t flags_platform = 0; 326 327 if (flags & eMmapFlagsPrivate) 328 flags_platform |= MAP_PRIVATE; 329 if (flags & eMmapFlagsAnon) 330 flags_platform |= MAP_ANON; 331 332 MmapArgList args({addr, length, prot, flags_platform, fd, offset}); 333 return args; 334 } 335 336 FileSpec PlatformOpenBSD::LocateExecutable(const char *basename) { 337 338 std::string check = std::string("/usr/bin/") + basename; 339 if (access(check.c_str(), X_OK) == 0) { 340 return FileSpec(check); 341 } 342 343 return FileSpec(); 344 } 345