]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
Merge ^/head r319973 through 321382.
[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   if (log) {
49     const char *arch_name;
50     if (arch && arch->GetArchitectureName())
51       arch_name = arch->GetArchitectureName();
52     else
53       arch_name = "<null>";
54
55     const char *triple_cstr =
56         arch ? arch->GetTriple().getTriple().c_str() : "<null>";
57
58     log->Printf("PlatformNetBSD::%s(force=%s, arch={%s,%s})", __FUNCTION__,
59                 force ? "true" : "false", arch_name, triple_cstr);
60   }
61
62   bool create = force;
63   if (create == false && arch && arch->IsValid()) {
64     const llvm::Triple &triple = arch->GetTriple();
65     switch (triple.getOS()) {
66     case llvm::Triple::NetBSD:
67       create = true;
68       break;
69
70     default:
71       break;
72     }
73   }
74
75   if (create) {
76     if (log)
77       log->Printf("PlatformNetBSD::%s() creating remote-netbsd platform",
78                   __FUNCTION__);
79     return PlatformSP(new PlatformNetBSD(false));
80   }
81
82   if (log)
83     log->Printf(
84         "PlatformNetBSD::%s() aborting creation of remote-netbsd platform",
85         __FUNCTION__);
86
87   return PlatformSP();
88 }
89
90 ConstString PlatformNetBSD::GetPluginNameStatic(bool is_host) {
91   if (is_host) {
92     static ConstString g_host_name(Platform::GetHostPlatformName());
93     return g_host_name;
94   } else {
95     static ConstString g_remote_name("remote-netbsd");
96     return g_remote_name;
97   }
98 }
99
100 const char *PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) {
101   if (is_host)
102     return "Local NetBSD user platform plug-in.";
103   else
104     return "Remote NetBSD user platform plug-in.";
105 }
106
107 ConstString PlatformNetBSD::GetPluginName() {
108   return GetPluginNameStatic(IsHost());
109 }
110
111 void PlatformNetBSD::Initialize() {
112   PlatformPOSIX::Initialize();
113
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);
119 #endif
120     PluginManager::RegisterPlugin(
121         PlatformNetBSD::GetPluginNameStatic(false),
122         PlatformNetBSD::GetPluginDescriptionStatic(false),
123         PlatformNetBSD::CreateInstance, nullptr);
124   }
125 }
126
127 void PlatformNetBSD::Terminate() {
128   if (g_initialize_count > 0) {
129     if (--g_initialize_count == 0) {
130       PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance);
131     }
132   }
133
134   PlatformPOSIX::Terminate();
135 }
136
137 //------------------------------------------------------------------
138 /// Default Constructor
139 //------------------------------------------------------------------
140 PlatformNetBSD::PlatformNetBSD(bool is_host)
141     : PlatformPOSIX(is_host) // This is the local host platform
142 {}
143
144 PlatformNetBSD::~PlatformNetBSD() = default;
145
146 bool PlatformNetBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
147                                                      ArchSpec &arch) {
148   if (IsHost()) {
149     ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
150     if (hostArch.GetTriple().isOSNetBSD()) {
151       if (idx == 0) {
152         arch = hostArch;
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();
159         }
160       }
161     }
162   } else {
163     if (m_remote_platform_sp)
164       return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
165
166     llvm::Triple triple;
167     // Set the OS to NetBSD
168     triple.setOS(llvm::Triple::NetBSD);
169     // Set the architecture
170     switch (idx) {
171     case 0:
172       triple.setArchName("x86_64");
173       break;
174     case 1:
175       triple.setArchName("i386");
176       break;
177     default:
178       return false;
179     }
180     // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
181     // vendor by
182     // calling triple.SetVendorName("unknown") so that it is a "unspecified
183     // unknown".
184     // This means when someone calls triple.GetVendorName() it will return an
185     // empty string
186     // which indicates that the vendor can be set when two architectures are
187     // merged
188
189     // Now set the triple into "arch" and return true
190     arch.SetTriple(triple);
191     return true;
192   }
193   return false;
194 }
195
196 void PlatformNetBSD::GetStatus(Stream &strm) {
197   Platform::GetStatus(strm);
198
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).
203   if (IsHost()) {
204     struct utsname un;
205
206     if (uname(&un))
207       return;
208
209     strm.Printf("    Kernel: %s\n", un.sysname);
210     strm.Printf("   Release: %s\n", un.release);
211     strm.Printf("   Version: %s\n", un.version);
212   }
213 #endif
214 }
215
216 int32_t
217 PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
218   int32_t resume_count = 0;
219
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.
223     ++resume_count;
224   }
225
226   // If we're not launching a shell, we're done.
227   const FileSpec &shell = launch_info.GetShell();
228   if (!shell)
229     return resume_count;
230
231   std::string shell_string = shell.GetPath();
232   // We're in a shell, so for sure we have to resume past the shell exec.
233   ++resume_count;
234
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();
239   else
240     shell_name++;
241
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.
245     ++resume_count;
246   }
247
248   return resume_count;
249 }
250
251 bool PlatformNetBSD::CanDebugProcess() {
252   if (IsHost()) {
253     return true;
254   } else {
255     // If we're connected, we can debug.
256     return IsConnected();
257   }
258 }
259
260 // For local debugging, NetBSD will override the debug logic to use llgs-launch
261 // rather than
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
269     Status &error) {
270   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
271   if (log)
272     log->Printf("PlatformNetBSD::%s entered (target %p)", __FUNCTION__,
273                 static_cast<void *>(target));
274
275   // If we're a remote host, use standard behavior from parent class.
276   if (!IsHost())
277     return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error);
278
279   //
280   // For local debugging, we'll insist on having ProcessGDBRemote create the
281   // process.
282   //
283
284   ProcessSP process_sp;
285
286   // Make sure we stop at the entry point
287   launch_info.GetFlags().Set(eLaunchFlagDebug);
288
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);
293
294   // Ensure we have a target.
295   if (target == nullptr) {
296     if (log)
297       log->Printf("PlatformNetBSD::%s creating new target", __FUNCTION__);
298
299     TargetSP new_target_sp;
300     error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
301                                                   nullptr, new_target_sp);
302     if (error.Fail()) {
303       if (log)
304         log->Printf("PlatformNetBSD::%s failed to create new target: %s",
305                     __FUNCTION__, error.AsCString());
306       return process_sp;
307     }
308
309     target = new_target_sp.get();
310     if (!target) {
311       error.SetErrorString("CreateTarget() returned nullptr");
312       if (log)
313         log->Printf("PlatformNetBSD::%s failed: %s", __FUNCTION__,
314                     error.AsCString());
315       return process_sp;
316     }
317   } else {
318     if (log)
319       log->Printf("PlatformNetBSD::%s using provided target", __FUNCTION__);
320   }
321
322   // Mark target as currently selected target.
323   debugger.GetTargetList().SetSelectedTarget(target);
324
325   // Now create the gdb-remote process.
326   if (log)
327     log->Printf(
328         "PlatformNetBSD::%s having target create process with gdb-remote plugin",
329         __FUNCTION__);
330   process_sp = target->CreateProcess(
331       launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
332
333   if (!process_sp) {
334     error.SetErrorString("CreateProcess() failed for gdb-remote process");
335     if (log)
336       log->Printf("PlatformNetBSD::%s failed: %s", __FUNCTION__,
337                   error.AsCString());
338     return process_sp;
339   } else {
340     if (log)
341       log->Printf("PlatformNetBSD::%s successfully created process",
342                   __FUNCTION__);
343   }
344
345   // Adjust launch for a hijacker.
346   ListenerSP listener_sp;
347   if (!launch_info.GetHijackListener()) {
348     if (log)
349       log->Printf("PlatformNetBSD::%s setting up hijacker", __FUNCTION__);
350
351     listener_sp =
352         Listener::MakeListener("lldb.PlatformNetBSD.DebugProcess.hijack");
353     launch_info.SetHijackListener(listener_sp);
354     process_sp->HijackProcessEvents(listener_sp);
355   }
356
357   // Log file actions.
358   if (log) {
359     log->Printf(
360         "PlatformNetBSD::%s launching process with the following file actions:",
361         __FUNCTION__);
362
363     StreamString stream;
364     size_t i = 0;
365     const FileAction *file_action;
366     while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) {
367       file_action->Dump(stream);
368       log->PutCString(stream.GetData());
369       stream.Clear();
370     }
371   }
372
373   // Do the launch.
374   error = process_sp->Launch(launch_info);
375   if (error.Success()) {
376     // Handle the hijacking of process events.
377     if (listener_sp) {
378       const StateType state = process_sp->WaitForProcessToStop(
379           llvm::None, NULL, false, listener_sp);
380
381       if (state == eStateStopped) {
382         if (log)
383           log->Printf("PlatformNetBSD::%s pid %" PRIu64 " state %s\n",
384                       __FUNCTION__, process_sp->GetID(), StateAsCString(state));
385       } else {
386         if (log)
387           log->Printf("PlatformNetBSD::%s pid %" PRIu64
388                       " state is not stopped - %s\n",
389                       __FUNCTION__, process_sp->GetID(), StateAsCString(state));
390       }
391     }
392
393     // Hook up process PTY if we have one (which we should for local debugging
394     // with llgs).
395     int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
396     if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) {
397       process_sp->SetSTDIOFileDescriptor(pty_fd);
398       if (log)
399         log->Printf("PlatformNetBSD::%s pid %" PRIu64
400                     " hooked up STDIO pty to process",
401                     __FUNCTION__, process_sp->GetID());
402     } else {
403       if (log)
404         log->Printf("PlatformNetBSD::%s pid %" PRIu64
405                     " not using process STDIO pty",
406                     __FUNCTION__, process_sp->GetID());
407     }
408   } else {
409     if (log)
410       log->Printf("PlatformNetBSD::%s process launch failed: %s", __FUNCTION__,
411                   error.AsCString());
412     // FIXME figure out appropriate cleanup here.  Do we delete the target? Do
413     // we delete the process?  Does our caller do that?
414   }
415
416   return process_sp;
417 }
418
419 void PlatformNetBSD::CalculateTrapHandlerSymbolNames() {
420   m_trap_handlers.push_back(ConstString("_sigtramp"));
421 }
422
423 uint64_t PlatformNetBSD::ConvertMmapFlagsToPlatform(const ArchSpec &arch,
424                                                    unsigned flags) {
425   uint64_t flags_platform = 0;
426
427   if (flags & eMmapFlagsPrivate)
428     flags_platform |= MAP_PRIVATE;
429   if (flags & eMmapFlagsAnon)
430     flags_platform |= MAP_ANON;
431   return flags_platform;
432 }