1 //===-- PlatformFreeBSD.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 "PlatformFreeBSD.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/Breakpoint/BreakpointLocation.h"
18 #include "lldb/Breakpoint/BreakpointSite.h"
19 #include "lldb/Core/Debugger.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Host/HostInfo.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Utility/FileSpec.h"
25 #include "lldb/Utility/Log.h"
26 #include "lldb/Utility/State.h"
27 #include "lldb/Utility/Status.h"
28 #include "lldb/Utility/StreamString.h"
29 
30 #include "llvm/ADT/Triple.h"
31 #include "llvm/Support/Host.h"
32 
33 // Define these constants from FreeBSD mman.h for use when targeting remote
34 // FreeBSD 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_freebsd;
41 
42 LLDB_PLUGIN_DEFINE(PlatformFreeBSD)
43 
44 static uint32_t g_initialize_count = 0;
45 
46 
47 PlatformSP PlatformFreeBSD::CreateInstance(bool force, const ArchSpec *arch) {
48   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
49   LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
50            arch ? arch->GetArchitectureName() : "<null>",
51            arch ? arch->GetTriple().getTriple() : "<null>");
52 
53   bool create = force;
54   if (!create && arch && arch->IsValid()) {
55     const llvm::Triple &triple = arch->GetTriple();
56     switch (triple.getOS()) {
57     case llvm::Triple::FreeBSD:
58       create = true;
59       break;
60 
61 #if defined(__FreeBSD__)
62     // Only accept "unknown" for the OS if the host is BSD and it "unknown"
63     // wasn't specified (it was just returned because it was NOT specified)
64     case llvm::Triple::OSType::UnknownOS:
65       create = !arch->TripleOSWasSpecified();
66       break;
67 #endif
68     default:
69       break;
70     }
71   }
72   LLDB_LOG(log, "create = {0}", create);
73   if (create) {
74     return PlatformSP(new PlatformFreeBSD(false));
75   }
76   return PlatformSP();
77 }
78 
79 llvm::StringRef PlatformFreeBSD::GetPluginDescriptionStatic(bool is_host) {
80   if (is_host)
81     return "Local FreeBSD user platform plug-in.";
82   return "Remote FreeBSD user platform plug-in.";
83 }
84 
85 void PlatformFreeBSD::Initialize() {
86   Platform::Initialize();
87 
88   if (g_initialize_count++ == 0) {
89 #if defined(__FreeBSD__)
90     PlatformSP default_platform_sp(new PlatformFreeBSD(true));
91     default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
92     Platform::SetHostPlatform(default_platform_sp);
93 #endif
94     PluginManager::RegisterPlugin(
95         PlatformFreeBSD::GetPluginNameStatic(false),
96         PlatformFreeBSD::GetPluginDescriptionStatic(false),
97         PlatformFreeBSD::CreateInstance, nullptr);
98   }
99 }
100 
101 void PlatformFreeBSD::Terminate() {
102   if (g_initialize_count > 0) {
103     if (--g_initialize_count == 0) {
104       PluginManager::UnregisterPlugin(PlatformFreeBSD::CreateInstance);
105     }
106   }
107 
108   PlatformPOSIX::Terminate();
109 }
110 
111 /// Default Constructor
112 PlatformFreeBSD::PlatformFreeBSD(bool is_host)
113     : PlatformPOSIX(is_host) // This is the local host platform
114 {}
115 
116 bool PlatformFreeBSD::GetSupportedArchitectureAtIndex(uint32_t idx,
117                                                       ArchSpec &arch) {
118   if (IsHost()) {
119     ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
120     if (hostArch.GetTriple().isOSFreeBSD()) {
121       if (idx == 0) {
122         arch = hostArch;
123         return arch.IsValid();
124       } else if (idx == 1) {
125         // If the default host architecture is 64-bit, look for a 32-bit
126         // variant
127         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
128           arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
129           return arch.IsValid();
130         }
131       }
132     }
133   } else {
134     if (m_remote_platform_sp)
135       return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
136 
137     llvm::Triple triple;
138     // Set the OS to FreeBSD
139     triple.setOS(llvm::Triple::FreeBSD);
140     // Set the architecture
141     switch (idx) {
142     case 0:
143       triple.setArchName("x86_64");
144       break;
145     case 1:
146       triple.setArchName("i386");
147       break;
148     case 2:
149       triple.setArchName("aarch64");
150       break;
151     case 3:
152       triple.setArchName("arm");
153       break;
154     case 4:
155       triple.setArchName("mips64");
156       break;
157     case 5:
158       triple.setArchName("mips");
159       break;
160     case 6:
161       triple.setArchName("ppc64");
162       break;
163     case 7:
164       triple.setArchName("ppc");
165       break;
166     default:
167       return false;
168     }
169     // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
170     // vendor by calling triple.SetVendorName("unknown") so that it is a
171     // "unspecified unknown". This means when someone calls
172     // triple.GetVendorName() it will return an empty string which indicates
173     // that the vendor can be set when two architectures are merged
174 
175     // Now set the triple into "arch" and return true
176     arch.SetTriple(triple);
177     return true;
178   }
179   return false;
180 }
181 
182 void PlatformFreeBSD::GetStatus(Stream &strm) {
183   Platform::GetStatus(strm);
184 
185 #if LLDB_ENABLE_POSIX
186   // Display local kernel information only when we are running in host mode.
187   // Otherwise, we would end up printing non-FreeBSD information (when running
188   // on Mac OS for example).
189   if (IsHost()) {
190     struct utsname un;
191 
192     if (uname(&un))
193       return;
194 
195     strm.Printf("    Kernel: %s\n", un.sysname);
196     strm.Printf("   Release: %s\n", un.release);
197     strm.Printf("   Version: %s\n", un.version);
198   }
199 #endif
200 }
201 
202 bool PlatformFreeBSD::CanDebugProcess() {
203   if (IsHost()) {
204     return true;
205   } else {
206     // If we're connected, we can debug.
207     return IsConnected();
208   }
209 }
210 
211 void PlatformFreeBSD::CalculateTrapHandlerSymbolNames() {
212   m_trap_handlers.push_back(ConstString("_sigtramp"));
213 }
214 
215 MmapArgList PlatformFreeBSD::GetMmapArgumentList(const ArchSpec &arch,
216                                                  addr_t addr, addr_t length,
217                                                  unsigned prot, unsigned flags,
218                                                  addr_t fd, addr_t offset) {
219   uint64_t flags_platform = 0;
220 
221   if (flags & eMmapFlagsPrivate)
222     flags_platform |= MAP_PRIVATE;
223   if (flags & eMmapFlagsAnon)
224     flags_platform |= MAP_ANON;
225 
226   MmapArgList args({addr, length, prot, flags_platform, fd, offset});
227   if (arch.GetTriple().getArch() == llvm::Triple::x86)
228     args.push_back(0);
229   return args;
230 }
231