]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
MFV r316872: 7502 ztest should run zdb with -G (debug mode)
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / Platform / NetBSD / PlatformNetBSD.cpp
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 // C Includes
14 #include <stdio.h>
15 #ifndef LLDB_DISABLE_POSIX
16 #include <sys/utsname.h>
17 #endif
18
19 // C++ Includes
20 // Other libraries and framework includes
21 // Project 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"
32
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
37
38 using namespace lldb;
39 using namespace lldb_private;
40 using namespace lldb_private::platform_netbsd;
41
42 static uint32_t g_initialize_count = 0;
43
44 //------------------------------------------------------------------
45
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>");
51
52   bool create = force;
53   if (create == false && arch && arch->IsValid()) {
54     const llvm::Triple &triple = arch->GetTriple();
55     switch (triple.getOS()) {
56     case llvm::Triple::NetBSD:
57       create = true;
58       break;
59
60     default:
61       break;
62     }
63   }
64
65   LLDB_LOG(log, "create = {0}", create);
66   if (create) {
67     return PlatformSP(new PlatformNetBSD(false));
68   }
69   return PlatformSP();
70 }
71
72 ConstString PlatformNetBSD::GetPluginNameStatic(bool is_host) {
73   if (is_host) {
74     static ConstString g_host_name(Platform::GetHostPlatformName());
75     return g_host_name;
76   } else {
77     static ConstString g_remote_name("remote-netbsd");
78     return g_remote_name;
79   }
80 }
81
82 const char *PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) {
83   if (is_host)
84     return "Local NetBSD user platform plug-in.";
85   else
86     return "Remote NetBSD user platform plug-in.";
87 }
88
89 ConstString PlatformNetBSD::GetPluginName() {
90   return GetPluginNameStatic(IsHost());
91 }
92
93 void PlatformNetBSD::Initialize() {
94   PlatformPOSIX::Initialize();
95
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);
101 #endif
102     PluginManager::RegisterPlugin(
103         PlatformNetBSD::GetPluginNameStatic(false),
104         PlatformNetBSD::GetPluginDescriptionStatic(false),
105         PlatformNetBSD::CreateInstance, nullptr);
106   }
107 }
108
109 void PlatformNetBSD::Terminate() {
110   if (g_initialize_count > 0) {
111     if (--g_initialize_count == 0) {
112       PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance);
113     }
114   }
115
116   PlatformPOSIX::Terminate();
117 }
118
119 //------------------------------------------------------------------
120 /// Default Constructor
121 //------------------------------------------------------------------
122 PlatformNetBSD::PlatformNetBSD(bool is_host)
123     : PlatformPOSIX(is_host) // This is the local host platform
124 {}
125
126 PlatformNetBSD::~PlatformNetBSD() = default;
127
128 bool PlatformNetBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
129                                                      ArchSpec &arch) {
130   if (IsHost()) {
131     ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
132     if (hostArch.GetTriple().isOSNetBSD()) {
133       if (idx == 0) {
134         arch = hostArch;
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();
141         }
142       }
143     }
144   } else {
145     if (m_remote_platform_sp)
146       return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
147
148     llvm::Triple triple;
149     // Set the OS to NetBSD
150     triple.setOS(llvm::Triple::NetBSD);
151     // Set the architecture
152     switch (idx) {
153     case 0:
154       triple.setArchName("x86_64");
155       break;
156     case 1:
157       triple.setArchName("i386");
158       break;
159     default:
160       return false;
161     }
162     // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
163     // vendor by
164     // calling triple.SetVendorName("unknown") so that it is a "unspecified
165     // unknown".
166     // This means when someone calls triple.GetVendorName() it will return an
167     // empty string
168     // which indicates that the vendor can be set when two architectures are
169     // merged
170
171     // Now set the triple into "arch" and return true
172     arch.SetTriple(triple);
173     return true;
174   }
175   return false;
176 }
177
178 void PlatformNetBSD::GetStatus(Stream &strm) {
179   Platform::GetStatus(strm);
180
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).
185   if (IsHost()) {
186     struct utsname un;
187
188     if (uname(&un))
189       return;
190
191     strm.Printf("    Kernel: %s\n", un.sysname);
192     strm.Printf("   Release: %s\n", un.release);
193     strm.Printf("   Version: %s\n", un.version);
194   }
195 #endif
196 }
197
198 int32_t
199 PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
200   int32_t resume_count = 0;
201
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.
205     ++resume_count;
206   }
207
208   // If we're not launching a shell, we're done.
209   const FileSpec &shell = launch_info.GetShell();
210   if (!shell)
211     return resume_count;
212
213   std::string shell_string = shell.GetPath();
214   // We're in a shell, so for sure we have to resume past the shell exec.
215   ++resume_count;
216
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();
221   else
222     shell_name++;
223
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.
227     ++resume_count;
228   }
229
230   return resume_count;
231 }
232
233 bool PlatformNetBSD::CanDebugProcess() {
234   if (IsHost()) {
235     return true;
236   } else {
237     // If we're connected, we can debug.
238     return IsConnected();
239   }
240 }
241
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.
245 lldb::ProcessSP
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
249                              Status &error) {
250   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
251   LLDB_LOG(log, "target {0}", target);
252
253   // If we're a remote host, use standard behavior from parent class.
254   if (!IsHost())
255     return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error);
256
257   //
258   // For local debugging, we'll insist on having ProcessGDBRemote create the
259   // process.
260   //
261
262   ProcessSP process_sp;
263
264   // Make sure we stop at the entry point
265   launch_info.GetFlags().Set(eLaunchFlagDebug);
266
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);
271
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);
278     if (error.Fail()) {
279       LLDB_LOG(log, "failed to create new target: {0}", error);
280       return process_sp;
281     }
282
283     target = new_target_sp.get();
284     if (!target) {
285       error.SetErrorString("CreateTarget() returned nullptr");
286       LLDB_LOG(log, "error: {0}", error);
287       return process_sp;
288     }
289   }
290
291   // Mark target as currently selected target.
292   debugger.GetTargetList().SetSelectedTarget(target);
293
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);
298
299   if (!process_sp) {
300     error.SetErrorString("CreateProcess() failed for gdb-remote process");
301     LLDB_LOG(log, "error: {0}", error);
302     return process_sp;
303   }
304
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");
310     listener_sp =
311         Listener::MakeListener("lldb.PlatformNetBSD.DebugProcess.hijack");
312     launch_info.SetHijackListener(listener_sp);
313     process_sp->HijackProcessEvents(listener_sp);
314   }
315
316   // Log file actions.
317   if (log) {
318     LLDB_LOG(log, "launching process with the following file actions:");
319     StreamString stream;
320     size_t i = 0;
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());
325       stream.Clear();
326     }
327   }
328
329   // Do the launch.
330   error = process_sp->Launch(launch_info);
331   if (error.Success()) {
332     // Handle the hijacking of process events.
333     if (listener_sp) {
334       const StateType state = process_sp->WaitForProcessToStop(
335           llvm::None, NULL, false, listener_sp);
336
337       LLDB_LOG(log, "pid {0} state {0}", process_sp->GetID(), state);
338     }
339
340     // Hook up process PTY if we have one (which we should for local debugging
341     // with llgs).
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");
346     } else
347       LLDB_LOG(log, "not using process STDIO pty");
348   } else {
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?
352   }
353
354   return process_sp;
355 }
356
357 void PlatformNetBSD::CalculateTrapHandlerSymbolNames() {
358   m_trap_handlers.push_back(ConstString("_sigtramp"));
359 }
360
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;
366
367   if (flags & eMmapFlagsPrivate)
368     flags_platform |= MAP_PRIVATE;
369   if (flags & eMmapFlagsAnon)
370     flags_platform |= MAP_ANON;
371
372   MmapArgList args({addr, length, prot, flags_platform, fd, offset});
373   return args;
374 }