LLDB  mainline
PlatformNetBSD.cpp
Go to the documentation of this file.
1 //===-- PlatformNetBSD.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "PlatformNetBSD.h"
10 #include "lldb/Host/Config.h"
11 
12 #include <cstdio>
13 #if LLDB_ENABLE_POSIX
14 #include <sys/utsname.h>
15 #endif
16 
17 #include "lldb/Core/Debugger.h"
19 #include "lldb/Host/HostInfo.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/FileSpec.h"
23 #include "lldb/Utility/Log.h"
24 #include "lldb/Utility/State.h"
25 #include "lldb/Utility/Status.h"
27 
28 // Define these constants from NetBSD mman.h for use when targeting remote
29 // netbsd systems even when host has different values.
30 #define MAP_PRIVATE 0x0002
31 #define MAP_ANON 0x1000
32 
33 using namespace lldb;
34 using namespace lldb_private;
35 using namespace lldb_private::platform_netbsd;
36 
38 
40 
41 
42 PlatformSP PlatformNetBSD::CreateInstance(bool force, const ArchSpec *arch) {
44  LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
45  arch ? arch->GetArchitectureName() : "<null>",
46  arch ? arch->GetTriple().getTriple() : "<null>");
47 
48  bool create = force;
49  if (!create && arch && arch->IsValid()) {
50  const llvm::Triple &triple = arch->GetTriple();
51  switch (triple.getOS()) {
52  case llvm::Triple::NetBSD:
53  create = true;
54  break;
55 
56  default:
57  break;
58  }
59  }
60 
61  LLDB_LOG(log, "create = {0}", create);
62  if (create) {
63  return PlatformSP(new PlatformNetBSD(false));
64  }
65  return PlatformSP();
66 }
67 
68 ConstString PlatformNetBSD::GetPluginNameStatic(bool is_host) {
69  if (is_host) {
70  static ConstString g_host_name(Platform::GetHostPlatformName());
71  return g_host_name;
72  } else {
73  static ConstString g_remote_name("remote-netbsd");
74  return g_remote_name;
75  }
76 }
77 
78 const char *PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) {
79  if (is_host)
80  return "Local NetBSD user platform plug-in.";
81  else
82  return "Remote NetBSD user platform plug-in.";
83 }
84 
85 ConstString PlatformNetBSD::GetPluginName() {
86  return GetPluginNameStatic(IsHost());
87 }
88 
89 void PlatformNetBSD::Initialize() {
91 
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);
97 #endif
98  PluginManager::RegisterPlugin(
99  PlatformNetBSD::GetPluginNameStatic(false),
100  PlatformNetBSD::GetPluginDescriptionStatic(false),
101  PlatformNetBSD::CreateInstance, nullptr);
102  }
103 }
104 
105 void PlatformNetBSD::Terminate() {
106  if (g_initialize_count > 0) {
107  if (--g_initialize_count == 0) {
108  PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance);
109  }
110  }
111 
113 }
114 
115 /// Default Constructor
116 PlatformNetBSD::PlatformNetBSD(bool is_host)
117  : PlatformPOSIX(is_host) // This is the local host platform
118 {}
119 
121  ArchSpec &arch) {
122  if (IsHost()) {
123  ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
124  if (hostArch.GetTriple().isOSNetBSD()) {
125  if (idx == 0) {
126  arch = hostArch;
127  return arch.IsValid();
128  } else if (idx == 1) {
129  // If the default host architecture is 64-bit, look for a 32-bit
130  // variant
131  if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
132  arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
133  return arch.IsValid();
134  }
135  }
136  }
137  } else {
139  return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
140 
141  llvm::Triple triple;
142  // Set the OS to NetBSD
143  triple.setOS(llvm::Triple::NetBSD);
144  // Set the architecture
145  switch (idx) {
146  case 0:
147  triple.setArchName("x86_64");
148  break;
149  case 1:
150  triple.setArchName("i386");
151  break;
152  default:
153  return false;
154  }
155  // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
156  // vendor by calling triple.SetVendorName("unknown") so that it is a
157  // "unspecified unknown". This means when someone calls
158  // triple.GetVendorName() it will return an empty string which indicates
159  // that the vendor can be set when two architectures are merged
160 
161  // Now set the triple into "arch" and return true
162  arch.SetTriple(triple);
163  return true;
164  }
165  return false;
166 }
167 
169  Platform::GetStatus(strm);
170 
171 #if LLDB_ENABLE_POSIX
172  // Display local kernel information only when we are running in host mode.
173  // Otherwise, we would end up printing non-NetBSD information (when running
174  // on Mac OS for example).
175  if (IsHost()) {
176  struct utsname un;
177 
178  if (uname(&un))
179  return;
180 
181  strm.Printf(" Kernel: %s\n", un.sysname);
182  strm.Printf(" Release: %s\n", un.release);
183  strm.Printf(" Version: %s\n", un.version);
184  }
185 #endif
186 }
187 
188 uint32_t
190  uint32_t resume_count = 0;
191 
192  // Always resume past the initial stop when we use eLaunchFlagDebug
193  if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
194  // Resume past the stop for the final exec into the true inferior.
195  ++resume_count;
196  }
197 
198  // If we're not launching a shell, we're done.
199  const FileSpec &shell = launch_info.GetShell();
200  if (!shell)
201  return resume_count;
202 
203  std::string shell_string = shell.GetPath();
204  // We're in a shell, so for sure we have to resume past the shell exec.
205  ++resume_count;
206 
207  // Figure out what shell we're planning on using.
208  const char *shell_name = strrchr(shell_string.c_str(), '/');
209  if (shell_name == nullptr)
210  shell_name = shell_string.c_str();
211  else
212  shell_name++;
213 
214  if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
215  strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
216  // These shells seem to re-exec themselves. Add another resume.
217  ++resume_count;
218  }
219 
220  return resume_count;
221 }
222 
224  if (IsHost()) {
225  return true;
226  } else {
227  // If we're connected, we can debug.
228  return IsConnected();
229  }
230 }
231 
233  m_trap_handlers.push_back(ConstString("_sigtramp"));
234 }
235 
237  addr_t addr, addr_t length,
238  unsigned prot, unsigned flags,
239  addr_t fd, addr_t offset) {
240  uint64_t flags_platform = 0;
241 
242  if (flags & eMmapFlagsPrivate)
243  flags_platform |= MAP_PRIVATE;
244  if (flags & eMmapFlagsAnon)
245  flags_platform |= MAP_ANON;
246 
247  MmapArgList args({addr, length, prot, flags_platform, fd, offset});
248  return args;
249 }
lldb_private::RemoteAwarePlatform::IsConnected
bool IsConnected() const override
Definition: RemoteAwarePlatform.cpp:380
lldb_private::platform_netbsd::PlatformNetBSD::CanDebugProcess
bool CanDebugProcess() override
Not all platforms will support debugging a process by spawning somehow halted for a debugger (specifi...
Definition: PlatformNetBSD.cpp:223
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::MmapArgList
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition: Platform.h:59
lldb_private::platform_netbsd
Definition: PlatformNetBSD.h:15
LIBLLDB_LOG_PLATFORM
#define LIBLLDB_LOG_PLATFORM
Definition: Logging.h:39
g_initialize_count
static uint32_t g_initialize_count
Definition: PlatformNetBSD.cpp:39
PlatformNetBSD.h
lldb_private::Platform::m_trap_handlers
std::vector< ConstString > m_trap_handlers
Definition: Platform.h:903
lldb_private::Flags::Test
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:96
lldb_private::ProcessLaunchInfo::GetFlags
Flags & GetFlags()
Definition: ProcessLaunchInfo.h:64
lldb_private::Stream
Definition: Stream.h:28
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:444
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
MAP_ANON
#define MAP_ANON
Definition: PlatformNetBSD.cpp:31
Debugger.h
Process.h
lldb_private::platform_netbsd::PlatformNetBSD
Definition: PlatformNetBSD.h:17
Target.h
lldb_private::FileSpec
Definition: FileSpec.h:56
PlatformPOSIX
Definition: PlatformPOSIX.h:18
MAP_PRIVATE
#define MAP_PRIVATE
Definition: PlatformNetBSD.cpp:30
lldb_private::ProcessLaunchInfo
Definition: ProcessLaunchInfo.h:31
Log.h
lldb_private::platform_netbsd::PlatformNetBSD::GetSupportedArchitectureAtIndex
bool GetSupportedArchitectureAtIndex(uint32_t idx, ArchSpec &arch) override
Get the platform's supported architectures in the order in which they should be searched.
Definition: PlatformNetBSD.cpp:120
lldb_private::ArchSpec::SetTriple
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition: ArchSpec.cpp:739
lldb_private::RemoteAwarePlatform::m_remote_platform_sp
lldb::PlatformSP m_remote_platform_sp
Definition: RemoteAwarePlatform.h:104
StreamString.h
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::Platform::Terminate
static void Terminate()
Definition: Platform.cpp:149
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
HostInfo.h
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:39
lldb_private::ArchSpec::IsValid
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:342
lldb_private::Platform::Initialize
static void Initialize()
Definition: Platform.cpp:147
lldb_private::platform_netbsd::PlatformNetBSD::CalculateTrapHandlerSymbolNames
void CalculateTrapHandlerSymbolNames() override
Ask the Platform subclass to fill in the list of trap handler names.
Definition: PlatformNetBSD.cpp:232
uint32_t
lldb_private::ProcessLaunchInfo::GetShell
const FileSpec & GetShell() const
Definition: ProcessLaunchInfo.cpp:140
PluginManager.h
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:242
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
Status.h
lldb_private::platform_netbsd::PlatformNetBSD::GetStatus
void GetStatus(Stream &strm) override
Report the current status for this platform.
Definition: PlatformNetBSD.cpp:168
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::eMmapFlagsAnon
@ eMmapFlagsAnon
Definition: Platform.h:40
lldb_private::platform_netbsd::PlatformNetBSD::GetMmapArgumentList
MmapArgList GetMmapArgumentList(const ArchSpec &arch, lldb::addr_t addr, lldb::addr_t length, unsigned prot, unsigned flags, lldb::addr_t fd, lldb::addr_t offset) override
Definition: PlatformNetBSD.cpp:236
FileSpec.h
State.h
lldb_private::Platform::GetStatus
virtual void GetStatus(Stream &strm)
Report the current status for this platform.
Definition: Platform.cpp:407
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Log
Definition: Log.h:49
lldb_private::platform_netbsd::PlatformNetBSD::GetResumeCountForLaunchInfo
uint32_t GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) override
Definition: PlatformNetBSD.cpp:189
lldb_private::Platform::IsHost
bool IsHost() const
Definition: Platform.h:440
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:348
lldb_private::eMmapFlagsPrivate
@ eMmapFlagsPrivate
Definition: Platform.h:40
lldb
Definition: SBAddress.h:15