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