1 //===-- PlatformNetBSD.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 "PlatformNetBSD.h" 10 #include "lldb/Host/Config.h" 11 12 #include <cstdio> 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 NetBSD mman.h for use when targeting remote 29 // netbsd 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_netbsd; 36 37 LLDB_PLUGIN_DEFINE(PlatformNetBSD) 38 39 static uint32_t g_initialize_count = 0; 40 41 42 PlatformSP PlatformNetBSD::CreateInstance(bool force, const ArchSpec *arch) { 43 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); 44 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force, 45 arch ? arch->GetArchitectureName() : "<null>", 46 arch ? arch->GetTriple().getTriple() : "<null>"); 47 48 bool create = force; 49 if (!create && arch && arch->IsValid()) { 50 const llvm::Triple &triple = arch->GetTriple(); 51 switch (triple.getOS()) { 52 case llvm::Triple::NetBSD: 53 create = true; 54 break; 55 56 default: 57 break; 58 } 59 } 60 61 LLDB_LOG(log, "create = {0}", create); 62 if (create) { 63 return PlatformSP(new PlatformNetBSD(false)); 64 } 65 return PlatformSP(); 66 } 67 68 ConstString PlatformNetBSD::GetPluginNameStatic(bool is_host) { 69 if (is_host) { 70 static ConstString g_host_name(Platform::GetHostPlatformName()); 71 return g_host_name; 72 } else { 73 static ConstString g_remote_name("remote-netbsd"); 74 return g_remote_name; 75 } 76 } 77 78 const char *PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) { 79 if (is_host) 80 return "Local NetBSD user platform plug-in."; 81 else 82 return "Remote NetBSD user platform plug-in."; 83 } 84 85 void PlatformNetBSD::Initialize() { 86 PlatformPOSIX::Initialize(); 87 88 if (g_initialize_count++ == 0) { 89 #if defined(__NetBSD__) 90 PlatformSP default_platform_sp(new PlatformNetBSD(true)); 91 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 92 Platform::SetHostPlatform(default_platform_sp); 93 #endif 94 PluginManager::RegisterPlugin( 95 PlatformNetBSD::GetPluginNameStatic(false), 96 PlatformNetBSD::GetPluginDescriptionStatic(false), 97 PlatformNetBSD::CreateInstance, nullptr); 98 } 99 } 100 101 void PlatformNetBSD::Terminate() { 102 if (g_initialize_count > 0) { 103 if (--g_initialize_count == 0) { 104 PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance); 105 } 106 } 107 108 PlatformPOSIX::Terminate(); 109 } 110 111 /// Default Constructor 112 PlatformNetBSD::PlatformNetBSD(bool is_host) 113 : PlatformPOSIX(is_host) // This is the local host platform 114 {} 115 116 bool PlatformNetBSD::GetSupportedArchitectureAtIndex(uint32_t idx, 117 ArchSpec &arch) { 118 if (IsHost()) { 119 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 120 if (hostArch.GetTriple().isOSNetBSD()) { 121 if (idx == 0) { 122 arch = hostArch; 123 return arch.IsValid(); 124 } else if (idx == 1) { 125 // If the default host architecture is 64-bit, look for a 32-bit 126 // variant 127 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) { 128 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 129 return arch.IsValid(); 130 } 131 } 132 } 133 } else { 134 if (m_remote_platform_sp) 135 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch); 136 137 llvm::Triple triple; 138 // Set the OS to NetBSD 139 triple.setOS(llvm::Triple::NetBSD); 140 // Set the architecture 141 switch (idx) { 142 case 0: 143 triple.setArchName("x86_64"); 144 break; 145 case 1: 146 triple.setArchName("i386"); 147 break; 148 default: 149 return false; 150 } 151 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the 152 // vendor by calling triple.SetVendorName("unknown") so that it is a 153 // "unspecified unknown". This means when someone calls 154 // triple.GetVendorName() it will return an empty string which indicates 155 // that the vendor can be set when two architectures are merged 156 157 // Now set the triple into "arch" and return true 158 arch.SetTriple(triple); 159 return true; 160 } 161 return false; 162 } 163 164 void PlatformNetBSD::GetStatus(Stream &strm) { 165 Platform::GetStatus(strm); 166 167 #if LLDB_ENABLE_POSIX 168 // Display local kernel information only when we are running in host mode. 169 // Otherwise, we would end up printing non-NetBSD information (when running 170 // on Mac OS for example). 171 if (IsHost()) { 172 struct utsname un; 173 174 if (uname(&un)) 175 return; 176 177 strm.Printf(" Kernel: %s\n", un.sysname); 178 strm.Printf(" Release: %s\n", un.release); 179 strm.Printf(" Version: %s\n", un.version); 180 } 181 #endif 182 } 183 184 uint32_t 185 PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) { 186 uint32_t resume_count = 0; 187 188 // Always resume past the initial stop when we use eLaunchFlagDebug 189 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) { 190 // Resume past the stop for the final exec into the true inferior. 191 ++resume_count; 192 } 193 194 // If we're not launching a shell, we're done. 195 const FileSpec &shell = launch_info.GetShell(); 196 if (!shell) 197 return resume_count; 198 199 std::string shell_string = shell.GetPath(); 200 // We're in a shell, so for sure we have to resume past the shell exec. 201 ++resume_count; 202 203 // Figure out what shell we're planning on using. 204 const char *shell_name = strrchr(shell_string.c_str(), '/'); 205 if (shell_name == nullptr) 206 shell_name = shell_string.c_str(); 207 else 208 shell_name++; 209 210 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 || 211 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) { 212 // These shells seem to re-exec themselves. Add another resume. 213 ++resume_count; 214 } 215 216 return resume_count; 217 } 218 219 bool PlatformNetBSD::CanDebugProcess() { 220 if (IsHost()) { 221 return true; 222 } else { 223 // If we're connected, we can debug. 224 return IsConnected(); 225 } 226 } 227 228 void PlatformNetBSD::CalculateTrapHandlerSymbolNames() { 229 m_trap_handlers.push_back(ConstString("_sigtramp")); 230 } 231 232 MmapArgList PlatformNetBSD::GetMmapArgumentList(const ArchSpec &arch, 233 addr_t addr, addr_t length, 234 unsigned prot, unsigned flags, 235 addr_t fd, addr_t offset) { 236 uint64_t flags_platform = 0; 237 238 if (flags & eMmapFlagsPrivate) 239 flags_platform |= MAP_PRIVATE; 240 if (flags & eMmapFlagsAnon) 241 flags_platform |= MAP_ANON; 242 243 MmapArgList args({addr, length, prot, flags_platform, fd, offset}); 244 return args; 245 } 246