xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
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