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
34 // remote 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 variant
138 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
139 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
140 return arch.IsValid();
145 if (m_remote_platform_sp)
146 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
149 // Set the OS to NetBSD
150 triple.setOS(llvm::Triple::NetBSD);
151 // Set the architecture
154 triple.setArchName("x86_64");
157 triple.setArchName("i386");
162 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
164 // calling triple.SetVendorName("unknown") so that it is a "unspecified
166 // This means when someone calls triple.GetVendorName() it will return an
168 // which indicates that the vendor can be set when two architectures are
171 // Now set the triple into "arch" and return true
172 arch.SetTriple(triple);
178 void PlatformNetBSD::GetStatus(Stream &strm) {
179 Platform::GetStatus(strm);
181 #ifndef LLDB_DISABLE_POSIX
182 // Display local kernel information only when we are running in host mode.
183 // Otherwise, we would end up printing non-NetBSD information (when running
184 // on Mac OS for example).
191 strm.Printf(" Kernel: %s\n", un.sysname);
192 strm.Printf(" Release: %s\n", un.release);
193 strm.Printf(" Version: %s\n", un.version);
199 PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
200 int32_t resume_count = 0;
202 // Always resume past the initial stop when we use eLaunchFlagDebug
203 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
204 // Resume past the stop for the final exec into the true inferior.
208 // If we're not launching a shell, we're done.
209 const FileSpec &shell = launch_info.GetShell();
213 std::string shell_string = shell.GetPath();
214 // We're in a shell, so for sure we have to resume past the shell exec.
217 // Figure out what shell we're planning on using.
218 const char *shell_name = strrchr(shell_string.c_str(), '/');
219 if (shell_name == NULL)
220 shell_name = shell_string.c_str();
224 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
225 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
226 // These shells seem to re-exec themselves. Add another resume.
233 bool PlatformNetBSD::CanDebugProcess() {
237 // If we're connected, we can debug.
238 return IsConnected();
242 // For local debugging, NetBSD will override the debug logic to use llgs-launch
243 // rather than lldb-launch, llgs-attach. This differs from current lldb-launch,
244 // debugserver-attach approach on MacOSX.
246 PlatformNetBSD::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
247 Target *target, // Can be NULL, if NULL create a new
248 // target, else use existing one
250 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
251 LLDB_LOG(log, "target {0}", target);
253 // If we're a remote host, use standard behavior from parent class.
255 return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error);
258 // For local debugging, we'll insist on having ProcessGDBRemote create the
262 ProcessSP process_sp;
264 // Make sure we stop at the entry point
265 launch_info.GetFlags().Set(eLaunchFlagDebug);
267 // We always launch the process we are going to debug in a separate process
268 // group, since then we can handle ^C interrupts ourselves w/o having to worry
269 // about the target getting them as well.
270 launch_info.SetLaunchInSeparateProcessGroup(true);
272 // Ensure we have a target.
273 if (target == nullptr) {
274 LLDB_LOG(log, "creating new target");
275 TargetSP new_target_sp;
276 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
277 nullptr, new_target_sp);
279 LLDB_LOG(log, "failed to create new target: {0}", error);
283 target = new_target_sp.get();
285 error.SetErrorString("CreateTarget() returned nullptr");
286 LLDB_LOG(log, "error: {0}", error);
291 // Mark target as currently selected target.
292 debugger.GetTargetList().SetSelectedTarget(target);
294 // Now create the gdb-remote process.
295 LLDB_LOG(log, "having target create process with gdb-remote plugin");
296 process_sp = target->CreateProcess(
297 launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
300 error.SetErrorString("CreateProcess() failed for gdb-remote process");
301 LLDB_LOG(log, "error: {0}", error);
305 LLDB_LOG(log, "successfully created process");
306 // Adjust launch for a hijacker.
307 ListenerSP listener_sp;
308 if (!launch_info.GetHijackListener()) {
309 LLDB_LOG(log, "setting up hijacker");
311 Listener::MakeListener("lldb.PlatformNetBSD.DebugProcess.hijack");
312 launch_info.SetHijackListener(listener_sp);
313 process_sp->HijackProcessEvents(listener_sp);
318 LLDB_LOG(log, "launching process with the following file actions:");
321 const FileAction *file_action;
322 while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) {
323 file_action->Dump(stream);
324 LLDB_LOG(log, "{0}", stream.GetData());
330 error = process_sp->Launch(launch_info);
331 if (error.Success()) {
332 // Handle the hijacking of process events.
334 const StateType state = process_sp->WaitForProcessToStop(
335 llvm::None, NULL, false, listener_sp);
337 LLDB_LOG(log, "pid {0} state {0}", process_sp->GetID(), state);
340 // Hook up process PTY if we have one (which we should for local debugging
342 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
343 if (pty_fd != PseudoTerminal::invalid_fd) {
344 process_sp->SetSTDIOFileDescriptor(pty_fd);
345 LLDB_LOG(log, "hooked up STDIO pty to process");
347 LLDB_LOG(log, "not using process STDIO pty");
349 LLDB_LOG(log, "process launch failed: {0}", error);
350 // FIXME figure out appropriate cleanup here. Do we delete the target? Do
351 // we delete the process? Does our caller do that?
357 void PlatformNetBSD::CalculateTrapHandlerSymbolNames() {
358 m_trap_handlers.push_back(ConstString("_sigtramp"));
361 MmapArgList PlatformNetBSD::GetMmapArgumentList(const ArchSpec &arch,
362 addr_t addr, addr_t length,
363 unsigned prot, unsigned flags,
364 addr_t fd, addr_t offset) {
365 uint64_t flags_platform = 0;
367 if (flags & eMmapFlagsPrivate)
368 flags_platform |= MAP_PRIVATE;
369 if (flags & eMmapFlagsAnon)
370 flags_platform |= MAP_ANON;
372 MmapArgList args({addr, length, prot, flags_platform, fd, offset});