1 //===-- PlatformNetBSD.cpp -------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "PlatformNetBSD.h"
11 #include "lldb/Host/Config.h"
12
13 #include <stdio.h>
14 #ifndef LLDB_DISABLE_POSIX
15 #include <sys/utsname.h>
16 #endif
17
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Host/HostInfo.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Utility/FileSpec.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/State.h"
26 #include "lldb/Utility/Status.h"
27 #include "lldb/Utility/StreamString.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
38 static uint32_t g_initialize_count = 0;
39
40 //------------------------------------------------------------------
41
CreateInstance(bool force,const ArchSpec * arch)42 PlatformSP PlatformNetBSD::CreateInstance(bool force, const ArchSpec *arch) {
43 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
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
GetPluginNameStatic(bool is_host)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
GetPluginDescriptionStatic(bool is_host)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
GetPluginName()85 ConstString PlatformNetBSD::GetPluginName() {
86 return GetPluginNameStatic(IsHost());
87 }
88
Initialize()89 void PlatformNetBSD::Initialize() {
90 PlatformPOSIX::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
Terminate()105 void PlatformNetBSD::Terminate() {
106 if (g_initialize_count > 0) {
107 if (--g_initialize_count == 0) {
108 PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance);
109 }
110 }
111
112 PlatformPOSIX::Terminate();
113 }
114
115 //------------------------------------------------------------------
116 /// Default Constructor
117 //------------------------------------------------------------------
PlatformNetBSD(bool is_host)118 PlatformNetBSD::PlatformNetBSD(bool is_host)
119 : PlatformPOSIX(is_host) // This is the local host platform
120 {}
121
122 PlatformNetBSD::~PlatformNetBSD() = default;
123
GetSupportedArchitectureAtIndex(uint32_t idx,ArchSpec & arch)124 bool PlatformNetBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
125 ArchSpec &arch) {
126 if (IsHost()) {
127 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
128 if (hostArch.GetTriple().isOSNetBSD()) {
129 if (idx == 0) {
130 arch = hostArch;
131 return arch.IsValid();
132 } else if (idx == 1) {
133 // If the default host architecture is 64-bit, look for a 32-bit
134 // variant
135 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
136 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
137 return arch.IsValid();
138 }
139 }
140 }
141 } else {
142 if (m_remote_platform_sp)
143 return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
144
145 llvm::Triple triple;
146 // Set the OS to NetBSD
147 triple.setOS(llvm::Triple::NetBSD);
148 // Set the architecture
149 switch (idx) {
150 case 0:
151 triple.setArchName("x86_64");
152 break;
153 case 1:
154 triple.setArchName("i386");
155 break;
156 default:
157 return false;
158 }
159 // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
160 // vendor by calling triple.SetVendorName("unknown") so that it is a
161 // "unspecified unknown". This means when someone calls
162 // triple.GetVendorName() it will return an empty string which indicates
163 // that the vendor can be set when two architectures are merged
164
165 // Now set the triple into "arch" and return true
166 arch.SetTriple(triple);
167 return true;
168 }
169 return false;
170 }
171
GetStatus(Stream & strm)172 void PlatformNetBSD::GetStatus(Stream &strm) {
173 Platform::GetStatus(strm);
174
175 #ifndef LLDB_DISABLE_POSIX
176 // Display local kernel information only when we are running in host mode.
177 // Otherwise, we would end up printing non-NetBSD information (when running
178 // on Mac OS for example).
179 if (IsHost()) {
180 struct utsname un;
181
182 if (uname(&un))
183 return;
184
185 strm.Printf(" Kernel: %s\n", un.sysname);
186 strm.Printf(" Release: %s\n", un.release);
187 strm.Printf(" Version: %s\n", un.version);
188 }
189 #endif
190 }
191
192 int32_t
GetResumeCountForLaunchInfo(ProcessLaunchInfo & launch_info)193 PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
194 int32_t resume_count = 0;
195
196 // Always resume past the initial stop when we use eLaunchFlagDebug
197 if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
198 // Resume past the stop for the final exec into the true inferior.
199 ++resume_count;
200 }
201
202 // If we're not launching a shell, we're done.
203 const FileSpec &shell = launch_info.GetShell();
204 if (!shell)
205 return resume_count;
206
207 std::string shell_string = shell.GetPath();
208 // We're in a shell, so for sure we have to resume past the shell exec.
209 ++resume_count;
210
211 // Figure out what shell we're planning on using.
212 const char *shell_name = strrchr(shell_string.c_str(), '/');
213 if (shell_name == NULL)
214 shell_name = shell_string.c_str();
215 else
216 shell_name++;
217
218 if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
219 strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
220 // These shells seem to re-exec themselves. Add another resume.
221 ++resume_count;
222 }
223
224 return resume_count;
225 }
226
CanDebugProcess()227 bool PlatformNetBSD::CanDebugProcess() {
228 if (IsHost()) {
229 return true;
230 } else {
231 // If we're connected, we can debug.
232 return IsConnected();
233 }
234 }
235
236 // For local debugging, NetBSD will override the debug logic to use llgs-launch
237 // rather than lldb-launch, llgs-attach. This differs from current lldb-
238 // launch, debugserver-attach approach on MacOSX.
239 lldb::ProcessSP
DebugProcess(ProcessLaunchInfo & launch_info,Debugger & debugger,Target * target,Status & error)240 PlatformNetBSD::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
241 Target *target, // Can be NULL, if NULL create a new
242 // target, else use existing one
243 Status &error) {
244 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
245 LLDB_LOG(log, "target {0}", target);
246
247 // If we're a remote host, use standard behavior from parent class.
248 if (!IsHost())
249 return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error);
250
251 //
252 // For local debugging, we'll insist on having ProcessGDBRemote create the
253 // process.
254 //
255
256 ProcessSP process_sp;
257
258 // Make sure we stop at the entry point
259 launch_info.GetFlags().Set(eLaunchFlagDebug);
260
261 // We always launch the process we are going to debug in a separate process
262 // group, since then we can handle ^C interrupts ourselves w/o having to
263 // worry about the target getting them as well.
264 launch_info.SetLaunchInSeparateProcessGroup(true);
265
266 // Ensure we have a target.
267 if (target == nullptr) {
268 LLDB_LOG(log, "creating new target");
269 TargetSP new_target_sp;
270 error = debugger.GetTargetList().CreateTarget(
271 debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp);
272 if (error.Fail()) {
273 LLDB_LOG(log, "failed to create new target: {0}", error);
274 return process_sp;
275 }
276
277 target = new_target_sp.get();
278 if (!target) {
279 error.SetErrorString("CreateTarget() returned nullptr");
280 LLDB_LOG(log, "error: {0}", error);
281 return process_sp;
282 }
283 }
284
285 // Mark target as currently selected target.
286 debugger.GetTargetList().SetSelectedTarget(target);
287
288 // Now create the gdb-remote process.
289 LLDB_LOG(log, "having target create process with gdb-remote plugin");
290 process_sp =
291 target->CreateProcess(launch_info.GetListener(), "gdb-remote", nullptr);
292
293 if (!process_sp) {
294 error.SetErrorString("CreateProcess() failed for gdb-remote process");
295 LLDB_LOG(log, "error: {0}", error);
296 return process_sp;
297 }
298
299 LLDB_LOG(log, "successfully created process");
300 // Adjust launch for a hijacker.
301 ListenerSP listener_sp;
302 if (!launch_info.GetHijackListener()) {
303 LLDB_LOG(log, "setting up hijacker");
304 listener_sp =
305 Listener::MakeListener("lldb.PlatformNetBSD.DebugProcess.hijack");
306 launch_info.SetHijackListener(listener_sp);
307 process_sp->HijackProcessEvents(listener_sp);
308 }
309
310 // Log file actions.
311 if (log) {
312 LLDB_LOG(log, "launching process with the following file actions:");
313 StreamString stream;
314 size_t i = 0;
315 const FileAction *file_action;
316 while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) {
317 file_action->Dump(stream);
318 LLDB_LOG(log, "{0}", stream.GetData());
319 stream.Clear();
320 }
321 }
322
323 // Do the launch.
324 error = process_sp->Launch(launch_info);
325 if (error.Success()) {
326 // Handle the hijacking of process events.
327 if (listener_sp) {
328 const StateType state = process_sp->WaitForProcessToStop(
329 llvm::None, NULL, false, listener_sp);
330
331 LLDB_LOG(log, "pid {0} state {0}", process_sp->GetID(), state);
332 }
333
334 // Hook up process PTY if we have one (which we should for local debugging
335 // with llgs).
336 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
337 if (pty_fd != PseudoTerminal::invalid_fd) {
338 process_sp->SetSTDIOFileDescriptor(pty_fd);
339 LLDB_LOG(log, "hooked up STDIO pty to process");
340 } else
341 LLDB_LOG(log, "not using process STDIO pty");
342 } else {
343 LLDB_LOG(log, "process launch failed: {0}", error);
344 // FIXME figure out appropriate cleanup here. Do we delete the target? Do
345 // we delete the process? Does our caller do that?
346 }
347
348 return process_sp;
349 }
350
CalculateTrapHandlerSymbolNames()351 void PlatformNetBSD::CalculateTrapHandlerSymbolNames() {
352 m_trap_handlers.push_back(ConstString("_sigtramp"));
353 }
354
GetMmapArgumentList(const ArchSpec & arch,addr_t addr,addr_t length,unsigned prot,unsigned flags,addr_t fd,addr_t offset)355 MmapArgList PlatformNetBSD::GetMmapArgumentList(const ArchSpec &arch,
356 addr_t addr, addr_t length,
357 unsigned prot, unsigned flags,
358 addr_t fd, addr_t offset) {
359 uint64_t flags_platform = 0;
360
361 if (flags & eMmapFlagsPrivate)
362 flags_platform |= MAP_PRIVATE;
363 if (flags & eMmapFlagsAnon)
364 flags_platform |= MAP_ANON;
365
366 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
367 return args;
368 }
369