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