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/LLDBLog.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/State.h"
26 #include "lldb/Utility/Status.h"
28 
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
33 
34 using namespace lldb;
35 using namespace lldb_private;
36 using namespace lldb_private::platform_netbsd;
37 
39 
41 
42 
43 PlatformSP PlatformNetBSD::CreateInstance(bool force, const ArchSpec *arch) {
44  Log *log = GetLog(LLDBLog::Platform);
45  LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
46  arch ? arch->GetArchitectureName() : "<null>",
47  arch ? arch->GetTriple().getTriple() : "<null>");
48 
49  bool create = force;
50  if (!create && arch && arch->IsValid()) {
51  const llvm::Triple &triple = arch->GetTriple();
52  switch (triple.getOS()) {
53  case llvm::Triple::NetBSD:
54  create = true;
55  break;
56 
57  default:
58  break;
59  }
60  }
61 
62  LLDB_LOG(log, "create = {0}", create);
63  if (create) {
64  return PlatformSP(new PlatformNetBSD(false));
65  }
66  return PlatformSP();
67 }
68 
69 llvm::StringRef PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) {
70  if (is_host)
71  return "Local NetBSD user platform plug-in.";
72  return "Remote NetBSD user platform plug-in.";
73 }
74 
75 void PlatformNetBSD::Initialize() {
77 
78  if (g_initialize_count++ == 0) {
79 #if defined(__NetBSD__)
80  PlatformSP default_platform_sp(new PlatformNetBSD(true));
81  default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
82  Platform::SetHostPlatform(default_platform_sp);
83 #endif
84  PluginManager::RegisterPlugin(
85  PlatformNetBSD::GetPluginNameStatic(false),
86  PlatformNetBSD::GetPluginDescriptionStatic(false),
87  PlatformNetBSD::CreateInstance, nullptr);
88  }
89 }
90 
91 void PlatformNetBSD::Terminate() {
92  if (g_initialize_count > 0) {
93  if (--g_initialize_count == 0) {
94  PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance);
95  }
96  }
97 
99 }
100 
101 /// Default Constructor
102 PlatformNetBSD::PlatformNetBSD(bool is_host)
103  : PlatformPOSIX(is_host) // This is the local host platform
104 {
105  if (is_host) {
106  ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
107  m_supported_architectures.push_back(hostArch);
108  if (hostArch.GetTriple().isArch64Bit()) {
109  m_supported_architectures.push_back(
110  HostInfo::GetArchitecture(HostInfo::eArchKind32));
111  }
112  } else {
114  {llvm::Triple::x86_64, llvm::Triple::x86}, llvm::Triple::NetBSD);
115  }
116 }
117 
118 std::vector<ArchSpec>
121  return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
123 }
124 
126  Platform::GetStatus(strm);
127 
128 #if LLDB_ENABLE_POSIX
129  // Display local kernel information only when we are running in host mode.
130  // Otherwise, we would end up printing non-NetBSD information (when running
131  // on Mac OS for example).
132  if (IsHost()) {
133  struct utsname un;
134 
135  if (uname(&un))
136  return;
137 
138  strm.Printf(" Kernel: %s\n", un.sysname);
139  strm.Printf(" Release: %s\n", un.release);
140  strm.Printf(" Version: %s\n", un.version);
141  }
142 #endif
143 }
144 
145 uint32_t
147  uint32_t resume_count = 0;
148 
149  // Always resume past the initial stop when we use eLaunchFlagDebug
150  if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
151  // Resume past the stop for the final exec into the true inferior.
152  ++resume_count;
153  }
154 
155  // If we're not launching a shell, we're done.
156  const FileSpec &shell = launch_info.GetShell();
157  if (!shell)
158  return resume_count;
159 
160  std::string shell_string = shell.GetPath();
161  // We're in a shell, so for sure we have to resume past the shell exec.
162  ++resume_count;
163 
164  // Figure out what shell we're planning on using.
165  const char *shell_name = strrchr(shell_string.c_str(), '/');
166  if (shell_name == nullptr)
167  shell_name = shell_string.c_str();
168  else
169  shell_name++;
170 
171  if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
172  strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
173  // These shells seem to re-exec themselves. Add another resume.
174  ++resume_count;
175  }
176 
177  return resume_count;
178 }
179 
181  if (IsHost()) {
182  return true;
183  } else {
184  // If we're connected, we can debug.
185  return IsConnected();
186  }
187 }
188 
190  m_trap_handlers.push_back(ConstString("_sigtramp"));
191 }
192 
194  addr_t addr, addr_t length,
195  unsigned prot, unsigned flags,
196  addr_t fd, addr_t offset) {
197  uint64_t flags_platform = 0;
198 
199  if (flags & eMmapFlagsPrivate)
200  flags_platform |= MAP_PRIVATE;
201  if (flags & eMmapFlagsAnon)
202  flags_platform |= MAP_ANON;
203 
204  MmapArgList args({addr, length, prot, flags_platform, fd, offset});
205  return args;
206 }
207 
208 CompilerType PlatformNetBSD::GetSiginfoType(const llvm::Triple &triple) {
209  if (!m_type_system_up)
210  m_type_system_up.reset(new TypeSystemClang("siginfo", triple));
211  TypeSystemClang *ast = m_type_system_up.get();
212 
213  // generic types
214  CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
216  CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
217  CompilerType long_long_type = ast->GetBasicType(eBasicTypeLongLong);
219 
220  // platform-specific types
221  CompilerType &pid_type = int_type;
222  CompilerType &uid_type = uint_type;
223  CompilerType &clock_type = uint_type;
224  CompilerType &lwpid_type = int_type;
225 
226  CompilerType sigval_type = ast->CreateRecordType(
227  nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
228  clang::TTK_Union, lldb::eLanguageTypeC);
229  ast->StartTagDeclarationDefinition(sigval_type);
230  ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
232  ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
234  ast->CompleteTagDeclarationDefinition(sigval_type);
235 
236  CompilerType ptrace_option_type = ast->CreateRecordType(
238  clang::TTK_Union, lldb::eLanguageTypeC);
239  ast->StartTagDeclarationDefinition(ptrace_option_type);
240  ast->AddFieldToRecordType(ptrace_option_type, "_pe_other_pid", pid_type,
242  ast->AddFieldToRecordType(ptrace_option_type, "_pe_lwp", lwpid_type,
244  ast->CompleteTagDeclarationDefinition(ptrace_option_type);
245 
246  // siginfo_t
247  CompilerType siginfo_type = ast->CreateRecordType(
248  nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
249  clang::TTK_Union, lldb::eLanguageTypeC);
250  ast->StartTagDeclarationDefinition(siginfo_type);
251 
252  // struct _ksiginfo
253  CompilerType ksiginfo_type = ast->CreateRecordType(
255  clang::TTK_Struct, lldb::eLanguageTypeC);
256  ast->StartTagDeclarationDefinition(ksiginfo_type);
257  ast->AddFieldToRecordType(ksiginfo_type, "_signo", int_type,
259  ast->AddFieldToRecordType(ksiginfo_type, "_code", int_type,
261  ast->AddFieldToRecordType(ksiginfo_type, "_errno", int_type,
263 
264  // the structure is padded on 64-bit arches to fix alignment
265  if (triple.isArch64Bit())
266  ast->AddFieldToRecordType(ksiginfo_type, "__pad0", int_type,
268 
269  // union used to hold the signal data
270  CompilerType union_type = ast->CreateRecordType(
272  clang::TTK_Union, lldb::eLanguageTypeC);
273  ast->StartTagDeclarationDefinition(union_type);
274 
276  union_type, "_rt",
278  {
279  {"_pid", pid_type},
280  {"_uid", uid_type},
281  {"_value", sigval_type},
282  }),
284 
286  union_type, "_child",
288  {
289  {"_pid", pid_type},
290  {"_uid", uid_type},
291  {"_status", int_type},
292  {"_utime", clock_type},
293  {"_stime", clock_type},
294  }),
296 
298  union_type, "_fault",
300  {
301  {"_addr", voidp_type},
302  {"_trap", int_type},
303  {"_trap2", int_type},
304  {"_trap3", int_type},
305  }),
307 
309  union_type, "_poll",
311  {
312  {"_band", long_type},
313  {"_fd", int_type},
314  }),
316 
317  ast->AddFieldToRecordType(union_type, "_syscall",
319  ConstString(),
320  {
321  {"_sysnum", int_type},
322  {"_retval", int_type.GetArrayType(2)},
323  {"_error", int_type},
324  {"_args", long_long_type.GetArrayType(8)},
325  }),
327 
329  union_type, "_ptrace_state",
331  {
332  {"_pe_report_event", int_type},
333  {"_option", ptrace_option_type},
334  }),
336 
337  ast->CompleteTagDeclarationDefinition(union_type);
338  ast->AddFieldToRecordType(ksiginfo_type, "_reason", union_type,
340 
341  ast->CompleteTagDeclarationDefinition(ksiginfo_type);
342  ast->AddFieldToRecordType(siginfo_type, "_info", ksiginfo_type,
344 
345  ast->CompleteTagDeclarationDefinition(siginfo_type);
346  return siginfo_type;
347 }
lldb_private::RemoteAwarePlatform::IsConnected
bool IsConnected() const override
Definition: RemoteAwarePlatform.cpp:344
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:180
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::MmapArgList
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition: Platform.h:58
lldb_private::platform_netbsd::PlatformNetBSD::m_supported_architectures
std::vector< ArchSpec > m_supported_architectures
Definition: PlatformNetBSD.h:62
lldb_private::platform_netbsd
Definition: PlatformNetBSD.h:16
g_initialize_count
static uint32_t g_initialize_count
Definition: PlatformNetBSD.cpp:40
PlatformNetBSD.h
lldb_private::Platform::m_trap_handlers
std::vector< ConstString > m_trap_handlers
Definition: Platform.h:897
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:455
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
MAP_ANON
#define MAP_ANON
Definition: PlatformNetBSD.cpp:32
Debugger.h
Process.h
lldb_private::platform_netbsd::PlatformNetBSD
Definition: PlatformNetBSD.h:18
Target.h
lldb_private::TypeSystemClang::CreateStructForIdentifier
CompilerType CreateStructForIdentifier(ConstString type_name, const std::initializer_list< std::pair< const char *, CompilerType >> &type_fields, bool packed=false)
Definition: TypeSystemClang.cpp:2269
lldb_private::FileSpec
Definition: FileSpec.h:56
PlatformPOSIX
Definition: PlatformPOSIX.h:18
MAP_PRIVATE
#define MAP_PRIVATE
Definition: PlatformNetBSD.cpp:31
lldb_private::ProcessLaunchInfo
Definition: ProcessLaunchInfo.h:31
Log.h
lldb_private::TypeSystemClang::AddFieldToRecordType
static clang::FieldDecl * AddFieldToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &field_type, lldb::AccessType access, uint32_t bitfield_bit_size)
Definition: TypeSystemClang.cpp:7276
lldb_private::platform_netbsd::PlatformNetBSD::m_type_system_up
std::unique_ptr< TypeSystemClang > m_type_system_up
Definition: PlatformNetBSD.h:65
lldb_private::RemoteAwarePlatform::m_remote_platform_sp
lldb::PlatformSP m_remote_platform_sp
Definition: RemoteAwarePlatform.h:104
StreamString.h
lldb::eBasicTypeInt
@ eBasicTypeInt
Definition: lldb-enumerations.h:763
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::Platform::Terminate
static void Terminate()
Definition: Platform.cpp:140
lldb::eBasicTypeVoid
@ eBasicTypeVoid
Definition: lldb-enumerations.h:751
HostInfo.h
lldb_private::platform_netbsd::PlatformNetBSD::GetSupportedArchitectures
std::vector< ArchSpec > GetSupportedArchitectures(const ArchSpec &process_host_arch) override
Get the platform's supported architectures in the order in which they should be searched.
Definition: PlatformNetBSD.cpp:119
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb::eBasicTypeLong
@ eBasicTypeLong
Definition: lldb-enumerations.h:765
lldb_private::Platform::Initialize
static void Initialize()
Definition: Platform.cpp:138
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:189
lldb::eBasicTypeUnsignedInt
@ eBasicTypeUnsignedInt
Definition: lldb-enumerations.h:764
lldb::eAccessPublic
@ eAccessPublic
Definition: lldb-enumerations.h:509
lldb_private::CompilerType::GetPointerType
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
Definition: CompilerType.cpp:407
lldb_private::OptionalClangModuleID
A Clang module ID.
Definition: TypeSystemClang.h:56
uint32_t
lldb_private::ProcessLaunchInfo::GetShell
const FileSpec & GetShell() const
Definition: ProcessLaunchInfo.cpp:139
PluginManager.h
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:336
lldb_private::TypeSystemClang::CreateRecordType
CompilerType CreateRecordType(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, lldb::AccessType access_type, llvm::StringRef name, int kind, lldb::LanguageType language, ClangASTMetadata *metadata=nullptr, bool exports_symbols=false)
Definition: TypeSystemClang.cpp:1270
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
lldb::eBasicTypeLongLong
@ eBasicTypeLongLong
Definition: lldb-enumerations.h:767
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::TypeSystemClang::StartTagDeclarationDefinition
static bool StartTagDeclarationDefinition(const CompilerType &type)
Definition: TypeSystemClang.cpp:8223
lldb_private::platform_netbsd::PlatformNetBSD::GetStatus
void GetStatus(Stream &strm) override
Report the current status for this platform.
Definition: PlatformNetBSD.cpp:125
lldb::eLanguageTypeC
@ eLanguageTypeC
Non-standardized C, such as K&R.
Definition: lldb-enumerations.h:439
lldb_private::Platform::CreateArchList
static std::vector< ArchSpec > CreateArchList(llvm::ArrayRef< llvm::Triple::ArchType > archs, llvm::Triple::OSType os)
Create a list of ArchSpecs with the given OS and a architectures.
Definition: Platform.cpp:1144
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:193
lldb_private::TypeSystemClang::GetBasicType
CompilerType GetBasicType(lldb::BasicType type)
Definition: TypeSystemClang.cpp:930
FileSpec.h
lldb_private::TypeSystemClang
A TypeSystem implementation based on Clang.
Definition: TypeSystemClang.h:105
State.h
lldb_private::Platform::GetStatus
virtual void GetStatus(Stream &strm)
Report the current status for this platform.
Definition: Platform.cpp:285
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Log
Definition: Log.h:115
lldb_private::platform_netbsd::PlatformNetBSD::GetSiginfoType
CompilerType GetSiginfoType(const llvm::Triple &triple) override
Definition: PlatformNetBSD.cpp:208
lldb_private::platform_netbsd::PlatformNetBSD::GetResumeCountForLaunchInfo
uint32_t GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) override
Definition: PlatformNetBSD.cpp:146
lldb_private::Platform::IsHost
bool IsHost() const
Definition: Platform.h:420
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:308
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:364
lldb_private::eMmapFlagsPrivate
@ eMmapFlagsPrivate
Definition: Platform.h:40
lldb_private::TypeSystemClang::CompleteTagDeclarationDefinition
static bool CompleteTagDeclarationDefinition(const CompilerType &type)
Definition: TypeSystemClang.cpp:8248
lldb
Definition: SBAddress.h:15
LLDBLog.h
lldb_private::CompilerType::GetArrayType
CompilerType GetArrayType(uint64_t size) const
Definition: CompilerType.cpp:334