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/LLDBLog.h" 24 #include "lldb/Utility/Log.h" 25 #include "lldb/Utility/State.h" 26 #include "lldb/Utility/Status.h" 27 #include "lldb/Utility/StreamString.h" 28 29 // Define these constants from NetBSD mman.h for use when targeting remote 30 // netbsd systems even when host has different values. 31 #define MAP_PRIVATE 0x0002 32 #define MAP_ANON 0x1000 33 34 using namespace lldb; 35 using namespace lldb_private; 36 using namespace lldb_private::platform_netbsd; 37 38 LLDB_PLUGIN_DEFINE(PlatformNetBSD) 39 40 static uint32_t g_initialize_count = 0; 41 42 43 PlatformSP PlatformNetBSD::CreateInstance(bool force, const ArchSpec *arch) { 44 Log *log = GetLog(LLDBLog::Platform); 45 LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force, 46 arch ? arch->GetArchitectureName() : "<null>", 47 arch ? arch->GetTriple().getTriple() : "<null>"); 48 49 bool create = force; 50 if (!create && arch && arch->IsValid()) { 51 const llvm::Triple &triple = arch->GetTriple(); 52 switch (triple.getOS()) { 53 case llvm::Triple::NetBSD: 54 create = true; 55 break; 56 57 default: 58 break; 59 } 60 } 61 62 LLDB_LOG(log, "create = {0}", create); 63 if (create) { 64 return PlatformSP(new PlatformNetBSD(false)); 65 } 66 return PlatformSP(); 67 } 68 69 llvm::StringRef PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) { 70 if (is_host) 71 return "Local NetBSD user platform plug-in."; 72 return "Remote NetBSD user platform plug-in."; 73 } 74 75 void PlatformNetBSD::Initialize() { 76 PlatformPOSIX::Initialize(); 77 78 if (g_initialize_count++ == 0) { 79 #if defined(__NetBSD__) 80 PlatformSP default_platform_sp(new PlatformNetBSD(true)); 81 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 82 Platform::SetHostPlatform(default_platform_sp); 83 #endif 84 PluginManager::RegisterPlugin( 85 PlatformNetBSD::GetPluginNameStatic(false), 86 PlatformNetBSD::GetPluginDescriptionStatic(false), 87 PlatformNetBSD::CreateInstance, nullptr); 88 } 89 } 90 91 void PlatformNetBSD::Terminate() { 92 if (g_initialize_count > 0) { 93 if (--g_initialize_count == 0) { 94 PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance); 95 } 96 } 97 98 PlatformPOSIX::Terminate(); 99 } 100 101 /// Default Constructor 102 PlatformNetBSD::PlatformNetBSD(bool is_host) 103 : PlatformPOSIX(is_host) // This is the local host platform 104 { 105 if (is_host) { 106 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 107 m_supported_architectures.push_back(hostArch); 108 if (hostArch.GetTriple().isArch64Bit()) { 109 m_supported_architectures.push_back( 110 HostInfo::GetArchitecture(HostInfo::eArchKind32)); 111 } 112 } else { 113 m_supported_architectures = CreateArchList( 114 {llvm::Triple::x86_64, llvm::Triple::x86}, llvm::Triple::NetBSD); 115 } 116 } 117 118 std::vector<ArchSpec> PlatformNetBSD::GetSupportedArchitectures() { 119 if (m_remote_platform_sp) 120 return m_remote_platform_sp->GetSupportedArchitectures(); 121 return m_supported_architectures; 122 } 123 124 void PlatformNetBSD::GetStatus(Stream &strm) { 125 Platform::GetStatus(strm); 126 127 #if LLDB_ENABLE_POSIX 128 // Display local kernel information only when we are running in host mode. 129 // Otherwise, we would end up printing non-NetBSD information (when running 130 // on Mac OS for example). 131 if (IsHost()) { 132 struct utsname un; 133 134 if (uname(&un)) 135 return; 136 137 strm.Printf(" Kernel: %s\n", un.sysname); 138 strm.Printf(" Release: %s\n", un.release); 139 strm.Printf(" Version: %s\n", un.version); 140 } 141 #endif 142 } 143 144 uint32_t 145 PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) { 146 uint32_t resume_count = 0; 147 148 // Always resume past the initial stop when we use eLaunchFlagDebug 149 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) { 150 // Resume past the stop for the final exec into the true inferior. 151 ++resume_count; 152 } 153 154 // If we're not launching a shell, we're done. 155 const FileSpec &shell = launch_info.GetShell(); 156 if (!shell) 157 return resume_count; 158 159 std::string shell_string = shell.GetPath(); 160 // We're in a shell, so for sure we have to resume past the shell exec. 161 ++resume_count; 162 163 // Figure out what shell we're planning on using. 164 const char *shell_name = strrchr(shell_string.c_str(), '/'); 165 if (shell_name == nullptr) 166 shell_name = shell_string.c_str(); 167 else 168 shell_name++; 169 170 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 || 171 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) { 172 // These shells seem to re-exec themselves. Add another resume. 173 ++resume_count; 174 } 175 176 return resume_count; 177 } 178 179 bool PlatformNetBSD::CanDebugProcess() { 180 if (IsHost()) { 181 return true; 182 } else { 183 // If we're connected, we can debug. 184 return IsConnected(); 185 } 186 } 187 188 void PlatformNetBSD::CalculateTrapHandlerSymbolNames() { 189 m_trap_handlers.push_back(ConstString("_sigtramp")); 190 } 191 192 MmapArgList PlatformNetBSD::GetMmapArgumentList(const ArchSpec &arch, 193 addr_t addr, addr_t length, 194 unsigned prot, unsigned flags, 195 addr_t fd, addr_t offset) { 196 uint64_t flags_platform = 0; 197 198 if (flags & eMmapFlagsPrivate) 199 flags_platform |= MAP_PRIVATE; 200 if (flags & eMmapFlagsAnon) 201 flags_platform |= MAP_ANON; 202 203 MmapArgList args({addr, length, prot, flags_platform, fd, offset}); 204 return args; 205 } 206 207 CompilerType PlatformNetBSD::GetSiginfoType(const llvm::Triple &triple) { 208 if (!m_type_system_up) 209 m_type_system_up.reset(new TypeSystemClang("siginfo", triple)); 210 TypeSystemClang *ast = m_type_system_up.get(); 211 212 // generic types 213 CompilerType int_type = ast->GetBasicType(eBasicTypeInt); 214 CompilerType uint_type = ast->GetBasicType(eBasicTypeUnsignedInt); 215 CompilerType long_type = ast->GetBasicType(eBasicTypeLong); 216 CompilerType long_long_type = ast->GetBasicType(eBasicTypeLongLong); 217 CompilerType voidp_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType(); 218 219 // platform-specific types 220 CompilerType &pid_type = int_type; 221 CompilerType &uid_type = uint_type; 222 CompilerType &clock_type = uint_type; 223 CompilerType &lwpid_type = int_type; 224 225 CompilerType sigval_type = ast->CreateRecordType( 226 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t", 227 clang::TTK_Union, lldb::eLanguageTypeC); 228 ast->StartTagDeclarationDefinition(sigval_type); 229 ast->AddFieldToRecordType(sigval_type, "sival_int", int_type, 230 lldb::eAccessPublic, 0); 231 ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type, 232 lldb::eAccessPublic, 0); 233 ast->CompleteTagDeclarationDefinition(sigval_type); 234 235 CompilerType ptrace_option_type = ast->CreateRecordType( 236 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "", 237 clang::TTK_Union, lldb::eLanguageTypeC); 238 ast->StartTagDeclarationDefinition(ptrace_option_type); 239 ast->AddFieldToRecordType(ptrace_option_type, "_pe_other_pid", pid_type, 240 lldb::eAccessPublic, 0); 241 ast->AddFieldToRecordType(ptrace_option_type, "_pe_lwp", lwpid_type, 242 lldb::eAccessPublic, 0); 243 ast->CompleteTagDeclarationDefinition(ptrace_option_type); 244 245 // siginfo_t 246 CompilerType siginfo_type = ast->CreateRecordType( 247 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t", 248 clang::TTK_Union, lldb::eLanguageTypeC); 249 ast->StartTagDeclarationDefinition(siginfo_type); 250 251 // struct _ksiginfo 252 CompilerType ksiginfo_type = ast->CreateRecordType( 253 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "", 254 clang::TTK_Struct, lldb::eLanguageTypeC); 255 ast->StartTagDeclarationDefinition(ksiginfo_type); 256 ast->AddFieldToRecordType(ksiginfo_type, "_signo", int_type, 257 lldb::eAccessPublic, 0); 258 ast->AddFieldToRecordType(ksiginfo_type, "_code", int_type, 259 lldb::eAccessPublic, 0); 260 ast->AddFieldToRecordType(ksiginfo_type, "_errno", int_type, 261 lldb::eAccessPublic, 0); 262 263 // the structure is padded on 64-bit arches to fix alignment 264 if (triple.isArch64Bit()) 265 ast->AddFieldToRecordType(ksiginfo_type, "__pad0", int_type, 266 lldb::eAccessPublic, 0); 267 268 // union used to hold the signal data 269 CompilerType union_type = ast->CreateRecordType( 270 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "", 271 clang::TTK_Union, lldb::eLanguageTypeC); 272 ast->StartTagDeclarationDefinition(union_type); 273 274 ast->AddFieldToRecordType( 275 union_type, "_rt", 276 ast->CreateStructForIdentifier(ConstString(), 277 { 278 {"_pid", pid_type}, 279 {"_uid", uid_type}, 280 {"_value", sigval_type}, 281 }), 282 lldb::eAccessPublic, 0); 283 284 ast->AddFieldToRecordType( 285 union_type, "_child", 286 ast->CreateStructForIdentifier(ConstString(), 287 { 288 {"_pid", pid_type}, 289 {"_uid", uid_type}, 290 {"_status", int_type}, 291 {"_utime", clock_type}, 292 {"_stime", clock_type}, 293 }), 294 lldb::eAccessPublic, 0); 295 296 ast->AddFieldToRecordType( 297 union_type, "_fault", 298 ast->CreateStructForIdentifier(ConstString(), 299 { 300 {"_addr", voidp_type}, 301 {"_trap", int_type}, 302 {"_trap2", int_type}, 303 {"_trap3", int_type}, 304 }), 305 lldb::eAccessPublic, 0); 306 307 ast->AddFieldToRecordType( 308 union_type, "_poll", 309 ast->CreateStructForIdentifier(ConstString(), 310 { 311 {"_band", long_type}, 312 {"_fd", int_type}, 313 }), 314 lldb::eAccessPublic, 0); 315 316 ast->AddFieldToRecordType(union_type, "_syscall", 317 ast->CreateStructForIdentifier( 318 ConstString(), 319 { 320 {"_sysnum", int_type}, 321 {"_retval", int_type.GetArrayType(2)}, 322 {"_error", int_type}, 323 {"_args", long_long_type.GetArrayType(8)}, 324 }), 325 lldb::eAccessPublic, 0); 326 327 ast->AddFieldToRecordType( 328 union_type, "_ptrace_state", 329 ast->CreateStructForIdentifier(ConstString(), 330 { 331 {"_pe_report_event", int_type}, 332 {"_option", ptrace_option_type}, 333 }), 334 lldb::eAccessPublic, 0); 335 336 ast->CompleteTagDeclarationDefinition(union_type); 337 ast->AddFieldToRecordType(ksiginfo_type, "_reason", union_type, 338 lldb::eAccessPublic, 0); 339 340 ast->CompleteTagDeclarationDefinition(ksiginfo_type); 341 ast->AddFieldToRecordType(siginfo_type, "_info", ksiginfo_type, 342 lldb::eAccessPublic, 0); 343 344 ast->CompleteTagDeclarationDefinition(siginfo_type); 345 return siginfo_type; 346 } 347