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));
49 const char *arch_name;
50 if (arch && arch->GetArchitectureName())
51 arch_name = arch->GetArchitectureName();
55 const char *triple_cstr =
56 arch ? arch->GetTriple().getTriple().c_str() : "<null>";
58 log->Printf("PlatformNetBSD::%s(force=%s, arch={%s,%s})", __FUNCTION__,
59 force ? "true" : "false", arch_name, triple_cstr);
63 if (create == false && arch && arch->IsValid()) {
64 const llvm::Triple &triple = arch->GetTriple();
65 switch (triple.getOS()) {
66 case llvm::Triple::NetBSD:
77 log->Printf("PlatformNetBSD::%s() creating remote-netbsd platform",
79 return PlatformSP(new PlatformNetBSD(false));
84 "PlatformNetBSD::%s() aborting creation of remote-netbsd platform",
90 ConstString PlatformNetBSD::GetPluginNameStatic(bool is_host) {
92 static ConstString g_host_name(Platform::GetHostPlatformName());
95 static ConstString g_remote_name("remote-netbsd");
100 const char *PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) {
102 return "Local NetBSD user platform plug-in.";
104 return "Remote NetBSD user platform plug-in.";
107 ConstString PlatformNetBSD::GetPluginName() {
108 return GetPluginNameStatic(IsHost());
111 void PlatformNetBSD::Initialize() {
112 PlatformPOSIX::Initialize();
114 if (g_initialize_count++ == 0) {
115 #if defined(__NetBSD__)
116 PlatformSP default_platform_sp(new PlatformNetBSD(true));
117 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
118 Platform::SetHostPlatform(default_platform_sp);
120 PluginManager::RegisterPlugin(
121 PlatformNetBSD::GetPluginNameStatic(false),
122 PlatformNetBSD::GetPluginDescriptionStatic(false),
123 PlatformNetBSD::CreateInstance, nullptr);
127 void PlatformNetBSD::Terminate() {
128 if (g_initialize_count > 0) {
129 if (--g_initialize_count == 0) {
130 PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance);
134 PlatformPOSIX::Terminate();
137 //------------------------------------------------------------------
138 /// Default Constructor
139 //------------------------------------------------------------------
140 PlatformNetBSD::PlatformNetBSD(bool is_host)
141 : PlatformPOSIX(is_host) // This is the local host platform
144 PlatformNetBSD::~PlatformNetBSD() = default;
146 bool PlatformNetBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
149 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
150 if (hostArch.GetTriple().isOSNetBSD()) {
153 return arch.IsValid();
154 } else if (idx == 1) {
155 // If the default host architecture is 64-bit, look for a 32-bit variant
156 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
157 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
158 return arch.IsValid();
163 if (m_remote_platform_sp)
164 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
167 // Set the OS to NetBSD
168 triple.setOS(llvm::Triple::NetBSD);
169 // Set the architecture
172 triple.setArchName("x86_64");
175 triple.setArchName("i386");
180 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
182 // calling triple.SetVendorName("unknown") so that it is a "unspecified
184 // This means when someone calls triple.GetVendorName() it will return an
186 // which indicates that the vendor can be set when two architectures are
189 // Now set the triple into "arch" and return true
190 arch.SetTriple(triple);
196 void PlatformNetBSD::GetStatus(Stream &strm) {
197 Platform::GetStatus(strm);
199 #ifndef LLDB_DISABLE_POSIX
200 // Display local kernel information only when we are running in host mode.
201 // Otherwise, we would end up printing non-NetBSD information (when running
202 // on Mac OS for example).
209 strm.Printf(" Kernel: %s\n", un.sysname);
210 strm.Printf(" Release: %s\n", un.release);
211 strm.Printf(" Version: %s\n", un.version);
217 PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
218 int32_t resume_count = 0;
220 // Always resume past the initial stop when we use eLaunchFlagDebug
221 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
222 // Resume past the stop for the final exec into the true inferior.
226 // If we're not launching a shell, we're done.
227 const FileSpec &shell = launch_info.GetShell();
231 std::string shell_string = shell.GetPath();
232 // We're in a shell, so for sure we have to resume past the shell exec.
235 // Figure out what shell we're planning on using.
236 const char *shell_name = strrchr(shell_string.c_str(), '/');
237 if (shell_name == NULL)
238 shell_name = shell_string.c_str();
242 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
243 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
244 // These shells seem to re-exec themselves. Add another resume.
251 bool PlatformNetBSD::CanDebugProcess() {
255 // If we're connected, we can debug.
256 return IsConnected();
260 // For local debugging, NetBSD will override the debug logic to use llgs-launch
262 // lldb-launch, llgs-attach. This differs from current lldb-launch,
263 // debugserver-attach
264 // approach on MacOSX.
265 lldb::ProcessSP PlatformNetBSD::DebugProcess(
266 ProcessLaunchInfo &launch_info, Debugger &debugger,
267 Target *target, // Can be NULL, if NULL create a new
268 // target, else use existing one
270 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
272 log->Printf("PlatformNetBSD::%s entered (target %p)", __FUNCTION__,
273 static_cast<void *>(target));
275 // If we're a remote host, use standard behavior from parent class.
277 return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error);
280 // For local debugging, we'll insist on having ProcessGDBRemote create the
284 ProcessSP process_sp;
286 // Make sure we stop at the entry point
287 launch_info.GetFlags().Set(eLaunchFlagDebug);
289 // We always launch the process we are going to debug in a separate process
290 // group, since then we can handle ^C interrupts ourselves w/o having to worry
291 // about the target getting them as well.
292 launch_info.SetLaunchInSeparateProcessGroup(true);
294 // Ensure we have a target.
295 if (target == nullptr) {
297 log->Printf("PlatformNetBSD::%s creating new target", __FUNCTION__);
299 TargetSP new_target_sp;
300 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
301 nullptr, new_target_sp);
304 log->Printf("PlatformNetBSD::%s failed to create new target: %s",
305 __FUNCTION__, error.AsCString());
309 target = new_target_sp.get();
311 error.SetErrorString("CreateTarget() returned nullptr");
313 log->Printf("PlatformNetBSD::%s failed: %s", __FUNCTION__,
319 log->Printf("PlatformNetBSD::%s using provided target", __FUNCTION__);
322 // Mark target as currently selected target.
323 debugger.GetTargetList().SetSelectedTarget(target);
325 // Now create the gdb-remote process.
328 "PlatformNetBSD::%s having target create process with gdb-remote plugin",
330 process_sp = target->CreateProcess(
331 launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
334 error.SetErrorString("CreateProcess() failed for gdb-remote process");
336 log->Printf("PlatformNetBSD::%s failed: %s", __FUNCTION__,
341 log->Printf("PlatformNetBSD::%s successfully created process",
345 // Adjust launch for a hijacker.
346 ListenerSP listener_sp;
347 if (!launch_info.GetHijackListener()) {
349 log->Printf("PlatformNetBSD::%s setting up hijacker", __FUNCTION__);
352 Listener::MakeListener("lldb.PlatformNetBSD.DebugProcess.hijack");
353 launch_info.SetHijackListener(listener_sp);
354 process_sp->HijackProcessEvents(listener_sp);
360 "PlatformNetBSD::%s launching process with the following file actions:",
365 const FileAction *file_action;
366 while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) {
367 file_action->Dump(stream);
368 log->PutCString(stream.GetData());
374 error = process_sp->Launch(launch_info);
375 if (error.Success()) {
376 // Handle the hijacking of process events.
378 const StateType state = process_sp->WaitForProcessToStop(
379 llvm::None, NULL, false, listener_sp);
381 if (state == eStateStopped) {
383 log->Printf("PlatformNetBSD::%s pid %" PRIu64 " state %s\n",
384 __FUNCTION__, process_sp->GetID(), StateAsCString(state));
387 log->Printf("PlatformNetBSD::%s pid %" PRIu64
388 " state is not stopped - %s\n",
389 __FUNCTION__, process_sp->GetID(), StateAsCString(state));
393 // Hook up process PTY if we have one (which we should for local debugging
395 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
396 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) {
397 process_sp->SetSTDIOFileDescriptor(pty_fd);
399 log->Printf("PlatformNetBSD::%s pid %" PRIu64
400 " hooked up STDIO pty to process",
401 __FUNCTION__, process_sp->GetID());
404 log->Printf("PlatformNetBSD::%s pid %" PRIu64
405 " not using process STDIO pty",
406 __FUNCTION__, process_sp->GetID());
410 log->Printf("PlatformNetBSD::%s process launch failed: %s", __FUNCTION__,
412 // FIXME figure out appropriate cleanup here. Do we delete the target? Do
413 // we delete the process? Does our caller do that?
419 void PlatformNetBSD::CalculateTrapHandlerSymbolNames() {
420 m_trap_handlers.push_back(ConstString("_sigtramp"));
423 MmapArgList PlatformNetBSD::GetMmapArgumentList(const ArchSpec &arch,
424 addr_t addr, addr_t length,
425 unsigned prot, unsigned flags,
426 addr_t fd, addr_t offset) {
427 uint64_t flags_platform = 0;
429 if (flags & eMmapFlagsPrivate)
430 flags_platform |= MAP_PRIVATE;
431 if (flags & eMmapFlagsAnon)
432 flags_platform |= MAP_ANON;
434 MmapArgList args({addr, length, prot, flags_platform, fd, offset});