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"
15 #ifndef LLDB_DISABLE_POSIX
16 #include <sys/utsname.h>
20 // Other libraries and framework includes
22 #include "lldb/Core/Debugger.h"
23 #include "lldb/Core/PluginManager.h"
24 #include "lldb/Core/State.h"
25 #include "lldb/Host/HostInfo.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Utility/FileSpec.h"
29 #include "lldb/Utility/Log.h"
30 #include "lldb/Utility/Status.h"
31 #include "lldb/Utility/StreamString.h"
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
39 using namespace lldb_private;
40 using namespace lldb_private::platform_netbsd;
42 static uint32_t g_initialize_count = 0;
44 //------------------------------------------------------------------
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>");
53 if (create == false && arch && arch->IsValid()) {
54 const llvm::Triple &triple = arch->GetTriple();
55 switch (triple.getOS()) {
56 case llvm::Triple::NetBSD:
65 LLDB_LOG(log, "create = {0}", create);
67 return PlatformSP(new PlatformNetBSD(false));
72 ConstString PlatformNetBSD::GetPluginNameStatic(bool is_host) {
74 static ConstString g_host_name(Platform::GetHostPlatformName());
77 static ConstString g_remote_name("remote-netbsd");
82 const char *PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) {
84 return "Local NetBSD user platform plug-in.";
86 return "Remote NetBSD user platform plug-in.";
89 ConstString PlatformNetBSD::GetPluginName() {
90 return GetPluginNameStatic(IsHost());
93 void PlatformNetBSD::Initialize() {
94 PlatformPOSIX::Initialize();
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);
102 PluginManager::RegisterPlugin(
103 PlatformNetBSD::GetPluginNameStatic(false),
104 PlatformNetBSD::GetPluginDescriptionStatic(false),
105 PlatformNetBSD::CreateInstance, nullptr);
109 void PlatformNetBSD::Terminate() {
110 if (g_initialize_count > 0) {
111 if (--g_initialize_count == 0) {
112 PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance);
116 PlatformPOSIX::Terminate();
119 //------------------------------------------------------------------
120 /// Default Constructor
121 //------------------------------------------------------------------
122 PlatformNetBSD::PlatformNetBSD(bool is_host)
123 : PlatformPOSIX(is_host) // This is the local host platform
126 PlatformNetBSD::~PlatformNetBSD() = default;
128 bool PlatformNetBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
131 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
132 if (hostArch.GetTriple().isOSNetBSD()) {
135 return arch.IsValid();
136 } else if (idx == 1) {
137 // If the default host architecture is 64-bit, look for a 32-bit
139 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
140 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
141 return arch.IsValid();
146 if (m_remote_platform_sp)
147 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
150 // Set the OS to NetBSD
151 triple.setOS(llvm::Triple::NetBSD);
152 // Set the architecture
155 triple.setArchName("x86_64");
158 triple.setArchName("i386");
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
169 // Now set the triple into "arch" and return true
170 arch.SetTriple(triple);
176 void PlatformNetBSD::GetStatus(Stream &strm) {
177 Platform::GetStatus(strm);
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).
189 strm.Printf(" Kernel: %s\n", un.sysname);
190 strm.Printf(" Release: %s\n", un.release);
191 strm.Printf(" Version: %s\n", un.version);
197 PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
198 int32_t resume_count = 0;
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.
206 // If we're not launching a shell, we're done.
207 const FileSpec &shell = launch_info.GetShell();
211 std::string shell_string = shell.GetPath();
212 // We're in a shell, so for sure we have to resume past the shell exec.
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();
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.
231 bool PlatformNetBSD::CanDebugProcess() {
235 // If we're connected, we can debug.
236 return IsConnected();
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.
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
248 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
249 LLDB_LOG(log, "target {0}", target);
251 // If we're a remote host, use standard behavior from parent class.
253 return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error);
256 // For local debugging, we'll insist on having ProcessGDBRemote create the
260 ProcessSP process_sp;
262 // Make sure we stop at the entry point
263 launch_info.GetFlags().Set(eLaunchFlagDebug);
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);
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(debugger, "", "", false,
275 nullptr, new_target_sp);
277 LLDB_LOG(log, "failed to create new target: {0}", error);
281 target = new_target_sp.get();
283 error.SetErrorString("CreateTarget() returned nullptr");
284 LLDB_LOG(log, "error: {0}", error);
289 // Mark target as currently selected target.
290 debugger.GetTargetList().SetSelectedTarget(target);
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);
298 error.SetErrorString("CreateProcess() failed for gdb-remote process");
299 LLDB_LOG(log, "error: {0}", error);
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");
309 Listener::MakeListener("lldb.PlatformNetBSD.DebugProcess.hijack");
310 launch_info.SetHijackListener(listener_sp);
311 process_sp->HijackProcessEvents(listener_sp);
316 LLDB_LOG(log, "launching process with the following file actions:");
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());
328 error = process_sp->Launch(launch_info);
329 if (error.Success()) {
330 // Handle the hijacking of process events.
332 const StateType state = process_sp->WaitForProcessToStop(
333 llvm::None, NULL, false, listener_sp);
335 LLDB_LOG(log, "pid {0} state {0}", process_sp->GetID(), state);
338 // Hook up process PTY if we have one (which we should for local debugging
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");
345 LLDB_LOG(log, "not using process STDIO pty");
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?
355 void PlatformNetBSD::CalculateTrapHandlerSymbolNames() {
356 m_trap_handlers.push_back(ConstString("_sigtramp"));
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;
365 if (flags & eMmapFlagsPrivate)
366 flags_platform |= MAP_PRIVATE;
367 if (flags & eMmapFlagsAnon)
368 flags_platform |= MAP_ANON;
370 MmapArgList args({addr, length, prot, flags_platform, fd, offset});