1 //===-- PlatformNetBSD.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "PlatformNetBSD.h"
11 #include "lldb/Host/Config.h"
14 #ifndef LLDB_DISABLE_POSIX
15 #include <sys/utsname.h>
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Host/HostInfo.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Utility/FileSpec.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"
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
35 using namespace lldb_private;
36 using namespace lldb_private::platform_netbsd;
38 static uint32_t g_initialize_count = 0;
40 //------------------------------------------------------------------
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>");
49 if (!create && arch && arch->IsValid()) {
50 const llvm::Triple &triple = arch->GetTriple();
51 switch (triple.getOS()) {
52 case llvm::Triple::NetBSD:
61 LLDB_LOG(log, "create = {0}", create);
63 return PlatformSP(new PlatformNetBSD(false));
68 ConstString PlatformNetBSD::GetPluginNameStatic(bool is_host) {
70 static ConstString g_host_name(Platform::GetHostPlatformName());
73 static ConstString g_remote_name("remote-netbsd");
78 const char *PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) {
80 return "Local NetBSD user platform plug-in.";
82 return "Remote NetBSD user platform plug-in.";
85 ConstString PlatformNetBSD::GetPluginName() {
86 return GetPluginNameStatic(IsHost());
89 void PlatformNetBSD::Initialize() {
90 PlatformPOSIX::Initialize();
92 if (g_initialize_count++ == 0) {
93 #if defined(__NetBSD__)
94 PlatformSP default_platform_sp(new PlatformNetBSD(true));
95 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
96 Platform::SetHostPlatform(default_platform_sp);
98 PluginManager::RegisterPlugin(
99 PlatformNetBSD::GetPluginNameStatic(false),
100 PlatformNetBSD::GetPluginDescriptionStatic(false),
101 PlatformNetBSD::CreateInstance, nullptr);
105 void PlatformNetBSD::Terminate() {
106 if (g_initialize_count > 0) {
107 if (--g_initialize_count == 0) {
108 PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance);
112 PlatformPOSIX::Terminate();
115 //------------------------------------------------------------------
116 /// Default Constructor
117 //------------------------------------------------------------------
118 PlatformNetBSD::PlatformNetBSD(bool is_host)
119 : PlatformPOSIX(is_host) // This is the local host platform
122 PlatformNetBSD::~PlatformNetBSD() = default;
124 bool PlatformNetBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
127 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
128 if (hostArch.GetTriple().isOSNetBSD()) {
131 return arch.IsValid();
132 } else if (idx == 1) {
133 // If the default host architecture is 64-bit, look for a 32-bit
135 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
136 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
137 return arch.IsValid();
142 if (m_remote_platform_sp)
143 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
146 // Set the OS to NetBSD
147 triple.setOS(llvm::Triple::NetBSD);
148 // Set the architecture
151 triple.setArchName("x86_64");
154 triple.setArchName("i386");
159 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
160 // vendor by calling triple.SetVendorName("unknown") so that it is a
161 // "unspecified unknown". This means when someone calls
162 // triple.GetVendorName() it will return an empty string which indicates
163 // that the vendor can be set when two architectures are merged
165 // Now set the triple into "arch" and return true
166 arch.SetTriple(triple);
172 void PlatformNetBSD::GetStatus(Stream &strm) {
173 Platform::GetStatus(strm);
175 #ifndef LLDB_DISABLE_POSIX
176 // Display local kernel information only when we are running in host mode.
177 // Otherwise, we would end up printing non-NetBSD information (when running
178 // on Mac OS for example).
185 strm.Printf(" Kernel: %s\n", un.sysname);
186 strm.Printf(" Release: %s\n", un.release);
187 strm.Printf(" Version: %s\n", un.version);
193 PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
194 int32_t resume_count = 0;
196 // Always resume past the initial stop when we use eLaunchFlagDebug
197 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
198 // Resume past the stop for the final exec into the true inferior.
202 // If we're not launching a shell, we're done.
203 const FileSpec &shell = launch_info.GetShell();
207 std::string shell_string = shell.GetPath();
208 // We're in a shell, so for sure we have to resume past the shell exec.
211 // Figure out what shell we're planning on using.
212 const char *shell_name = strrchr(shell_string.c_str(), '/');
213 if (shell_name == NULL)
214 shell_name = shell_string.c_str();
218 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
219 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
220 // These shells seem to re-exec themselves. Add another resume.
227 bool PlatformNetBSD::CanDebugProcess() {
231 // If we're connected, we can debug.
232 return IsConnected();
236 // For local debugging, NetBSD will override the debug logic to use llgs-launch
237 // rather than lldb-launch, llgs-attach. This differs from current lldb-
238 // launch, debugserver-attach approach on MacOSX.
240 PlatformNetBSD::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
241 Target *target, // Can be NULL, if NULL create a new
242 // target, else use existing one
244 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
245 LLDB_LOG(log, "target {0}", target);
247 // If we're a remote host, use standard behavior from parent class.
249 return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error);
252 // For local debugging, we'll insist on having ProcessGDBRemote create the
256 ProcessSP process_sp;
258 // Make sure we stop at the entry point
259 launch_info.GetFlags().Set(eLaunchFlagDebug);
261 // We always launch the process we are going to debug in a separate process
262 // group, since then we can handle ^C interrupts ourselves w/o having to
263 // worry about the target getting them as well.
264 launch_info.SetLaunchInSeparateProcessGroup(true);
266 // Ensure we have a target.
267 if (target == nullptr) {
268 LLDB_LOG(log, "creating new target");
269 TargetSP new_target_sp;
270 error = debugger.GetTargetList().CreateTarget(
271 debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp);
273 LLDB_LOG(log, "failed to create new target: {0}", error);
277 target = new_target_sp.get();
279 error.SetErrorString("CreateTarget() returned nullptr");
280 LLDB_LOG(log, "error: {0}", error);
285 // Mark target as currently selected target.
286 debugger.GetTargetList().SetSelectedTarget(target);
288 // Now create the gdb-remote process.
289 LLDB_LOG(log, "having target create process with gdb-remote plugin");
291 target->CreateProcess(launch_info.GetListener(), "gdb-remote", nullptr);
294 error.SetErrorString("CreateProcess() failed for gdb-remote process");
295 LLDB_LOG(log, "error: {0}", error);
299 LLDB_LOG(log, "successfully created process");
300 // Adjust launch for a hijacker.
301 ListenerSP listener_sp;
302 if (!launch_info.GetHijackListener()) {
303 LLDB_LOG(log, "setting up hijacker");
305 Listener::MakeListener("lldb.PlatformNetBSD.DebugProcess.hijack");
306 launch_info.SetHijackListener(listener_sp);
307 process_sp->HijackProcessEvents(listener_sp);
312 LLDB_LOG(log, "launching process with the following file actions:");
315 const FileAction *file_action;
316 while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) {
317 file_action->Dump(stream);
318 LLDB_LOG(log, "{0}", stream.GetData());
324 error = process_sp->Launch(launch_info);
325 if (error.Success()) {
326 // Handle the hijacking of process events.
328 const StateType state = process_sp->WaitForProcessToStop(
329 llvm::None, NULL, false, listener_sp);
331 LLDB_LOG(log, "pid {0} state {0}", process_sp->GetID(), state);
334 // Hook up process PTY if we have one (which we should for local debugging
336 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
337 if (pty_fd != PseudoTerminal::invalid_fd) {
338 process_sp->SetSTDIOFileDescriptor(pty_fd);
339 LLDB_LOG(log, "hooked up STDIO pty to process");
341 LLDB_LOG(log, "not using process STDIO pty");
343 LLDB_LOG(log, "process launch failed: {0}", error);
344 // FIXME figure out appropriate cleanup here. Do we delete the target? Do
345 // we delete the process? Does our caller do that?
351 void PlatformNetBSD::CalculateTrapHandlerSymbolNames() {
352 m_trap_handlers.push_back(ConstString("_sigtramp"));
355 MmapArgList PlatformNetBSD::GetMmapArgumentList(const ArchSpec &arch,
356 addr_t addr, addr_t length,
357 unsigned prot, unsigned flags,
358 addr_t fd, addr_t offset) {
359 uint64_t flags_platform = 0;
361 if (flags & eMmapFlagsPrivate)
362 flags_platform |= MAP_PRIVATE;
363 if (flags & eMmapFlagsAnon)
364 flags_platform |= MAP_ANON;
366 MmapArgList args({addr, length, prot, flags_platform, fd, offset});