xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
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   if (is_host) {
116     ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
117     m_supported_architectures.push_back(hostArch);
118     if (hostArch.GetTriple().isArch64Bit()) {
119       m_supported_architectures.push_back(
120           HostInfo::GetArchitecture(HostInfo::eArchKind32));
121     }
122   } else {
123     m_supported_architectures = CreateArchList(
124         {llvm::Triple::x86_64, llvm::Triple::x86, llvm::Triple::aarch64,
125          llvm::Triple::arm, llvm::Triple::mips64, llvm::Triple::ppc64,
126          llvm::Triple::ppc},
127         llvm::Triple::FreeBSD);
128   }
129 }
130 
131 std::vector<ArchSpec> PlatformFreeBSD::GetSupportedArchitectures() {
132   if (m_remote_platform_sp)
133     return m_remote_platform_sp->GetSupportedArchitectures();
134   return m_supported_architectures;
135 }
136 
137 void PlatformFreeBSD::GetStatus(Stream &strm) {
138   Platform::GetStatus(strm);
139 
140 #if LLDB_ENABLE_POSIX
141   // Display local kernel information only when we are running in host mode.
142   // Otherwise, we would end up printing non-FreeBSD information (when running
143   // on Mac OS for example).
144   if (IsHost()) {
145     struct utsname un;
146 
147     if (uname(&un))
148       return;
149 
150     strm.Printf("    Kernel: %s\n", un.sysname);
151     strm.Printf("   Release: %s\n", un.release);
152     strm.Printf("   Version: %s\n", un.version);
153   }
154 #endif
155 }
156 
157 bool PlatformFreeBSD::CanDebugProcess() {
158   if (IsHost()) {
159     return true;
160   } else {
161     // If we're connected, we can debug.
162     return IsConnected();
163   }
164 }
165 
166 void PlatformFreeBSD::CalculateTrapHandlerSymbolNames() {
167   m_trap_handlers.push_back(ConstString("_sigtramp"));
168 }
169 
170 MmapArgList PlatformFreeBSD::GetMmapArgumentList(const ArchSpec &arch,
171                                                  addr_t addr, addr_t length,
172                                                  unsigned prot, unsigned flags,
173                                                  addr_t fd, addr_t offset) {
174   uint64_t flags_platform = 0;
175 
176   if (flags & eMmapFlagsPrivate)
177     flags_platform |= MAP_PRIVATE;
178   if (flags & eMmapFlagsAnon)
179     flags_platform |= MAP_ANON;
180 
181   MmapArgList args({addr, length, prot, flags_platform, fd, offset});
182   if (arch.GetTriple().getArch() == llvm::Triple::x86)
183     args.push_back(0);
184   return args;
185 }
186 
187 CompilerType PlatformFreeBSD::GetSiginfoType(const llvm::Triple &triple) {
188   if (!m_type_system_up)
189     m_type_system_up.reset(new TypeSystemClang("siginfo", triple));
190   TypeSystemClang *ast = m_type_system_up.get();
191 
192   // generic types
193   CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
194   CompilerType uint_type = ast->GetBasicType(eBasicTypeUnsignedInt);
195   CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
196   CompilerType voidp_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
197 
198   // platform-specific types
199   CompilerType &pid_type = int_type;
200   CompilerType &uid_type = uint_type;
201 
202   CompilerType sigval_type = ast->CreateRecordType(
203       nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
204       clang::TTK_Union, lldb::eLanguageTypeC);
205   ast->StartTagDeclarationDefinition(sigval_type);
206   ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
207                             lldb::eAccessPublic, 0);
208   ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
209                             lldb::eAccessPublic, 0);
210   ast->CompleteTagDeclarationDefinition(sigval_type);
211 
212   // siginfo_t
213   CompilerType siginfo_type = ast->CreateRecordType(
214       nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
215       clang::TTK_Struct, lldb::eLanguageTypeC);
216   ast->StartTagDeclarationDefinition(siginfo_type);
217   ast->AddFieldToRecordType(siginfo_type, "si_signo", int_type,
218                             lldb::eAccessPublic, 0);
219   ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
220                             lldb::eAccessPublic, 0);
221   ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
222                             lldb::eAccessPublic, 0);
223   ast->AddFieldToRecordType(siginfo_type, "si_pid", pid_type,
224                             lldb::eAccessPublic, 0);
225   ast->AddFieldToRecordType(siginfo_type, "si_uid", uid_type,
226                             lldb::eAccessPublic, 0);
227   ast->AddFieldToRecordType(siginfo_type, "si_status", int_type,
228                             lldb::eAccessPublic, 0);
229   ast->AddFieldToRecordType(siginfo_type, "si_addr", voidp_type,
230                             lldb::eAccessPublic, 0);
231   ast->AddFieldToRecordType(siginfo_type, "si_value", sigval_type,
232                             lldb::eAccessPublic, 0);
233 
234   // union used to hold the signal data
235   CompilerType union_type = ast->CreateRecordType(
236       nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
237       clang::TTK_Union, lldb::eLanguageTypeC);
238   ast->StartTagDeclarationDefinition(union_type);
239 
240   ast->AddFieldToRecordType(
241       union_type, "_fault",
242       ast->CreateStructForIdentifier(ConstString(),
243                                      {
244                                          {"_trapno", int_type},
245                                      }),
246       lldb::eAccessPublic, 0);
247 
248   ast->AddFieldToRecordType(
249       union_type, "_timer",
250       ast->CreateStructForIdentifier(ConstString(),
251                                      {
252                                          {"_timerid", int_type},
253                                          {"_overrun", int_type},
254                                      }),
255       lldb::eAccessPublic, 0);
256 
257   ast->AddFieldToRecordType(
258       union_type, "_mesgq",
259       ast->CreateStructForIdentifier(ConstString(),
260                                      {
261                                          {"_mqd", int_type},
262                                      }),
263       lldb::eAccessPublic, 0);
264 
265   ast->AddFieldToRecordType(
266       union_type, "_poll",
267       ast->CreateStructForIdentifier(ConstString(),
268                                      {
269                                          {"_band", long_type},
270                                      }),
271       lldb::eAccessPublic, 0);
272 
273   ast->CompleteTagDeclarationDefinition(union_type);
274   ast->AddFieldToRecordType(siginfo_type, "_reason", union_type,
275                             lldb::eAccessPublic, 0);
276 
277   ast->CompleteTagDeclarationDefinition(siginfo_type);
278   return siginfo_type;
279 }
280