Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
Line
Count
Source (jump to first uncovered line)
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/LLDBLog.h"
26
#include "lldb/Utility/Log.h"
27
#include "lldb/Utility/State.h"
28
#include "lldb/Utility/Status.h"
29
#include "lldb/Utility/StreamString.h"
30
31
#include "llvm/TargetParser/Host.h"
32
#include "llvm/TargetParser/Triple.h"
33
34
// Define these constants from FreeBSD mman.h for use when targeting remote
35
// FreeBSD systems even when host has different values.
36
0
#define MAP_PRIVATE 0x0002
37
0
#define MAP_ANON 0x1000
38
39
using namespace lldb;
40
using namespace lldb_private;
41
using namespace lldb_private::platform_freebsd;
42
43
LLDB_PLUGIN_DEFINE(PlatformFreeBSD)
44
45
static uint32_t g_initialize_count = 0;
46
47
48
244
PlatformSP PlatformFreeBSD::CreateInstance(bool force, const ArchSpec *arch) {
49
244
  Log *log = GetLog(LLDBLog::Platform);
50
244
  LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
51
244
           arch ? arch->GetArchitectureName() : "<null>",
52
244
           arch ? arch->GetTriple().getTriple() : "<null>");
53
54
244
  bool create = force;
55
244
  if (!create && 
arch235
&&
arch->IsValid()235
) {
56
235
    const llvm::Triple &triple = arch->GetTriple();
57
235
    switch (triple.getOS()) {
58
2
    case llvm::Triple::FreeBSD:
59
2
      create = true;
60
2
      break;
61
62
#if defined(__FreeBSD__)
63
    // Only accept "unknown" for the OS if the host is BSD and it "unknown"
64
    // wasn't specified (it was just returned because it was NOT specified)
65
    case llvm::Triple::OSType::UnknownOS:
66
      create = !arch->TripleOSWasSpecified();
67
      break;
68
#endif
69
233
    default:
70
233
      break;
71
235
    }
72
235
  }
73
244
  LLDB_LOG(log, "create = {0}", create);
74
244
  if (create) {
75
11
    return PlatformSP(new PlatformFreeBSD(false));
76
11
  }
77
233
  return PlatformSP();
78
244
}
79
80
3.93k
llvm::StringRef PlatformFreeBSD::GetPluginDescriptionStatic(bool is_host) {
81
3.93k
  if (is_host)
82
0
    return "Local FreeBSD user platform plug-in.";
83
3.93k
  return "Remote FreeBSD user platform plug-in.";
84
3.93k
}
85
86
3.93k
void PlatformFreeBSD::Initialize() {
87
3.93k
  Platform::Initialize();
88
89
3.93k
  if (g_initialize_count++ == 0) {
90
#if defined(__FreeBSD__)
91
    PlatformSP default_platform_sp(new PlatformFreeBSD(true));
92
    default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
93
    Platform::SetHostPlatform(default_platform_sp);
94
#endif
95
3.93k
    PluginManager::RegisterPlugin(
96
3.93k
        PlatformFreeBSD::GetPluginNameStatic(false),
97
3.93k
        PlatformFreeBSD::GetPluginDescriptionStatic(false),
98
3.93k
        PlatformFreeBSD::CreateInstance, nullptr);
99
3.93k
  }
100
3.93k
}
101
102
3.92k
void PlatformFreeBSD::Terminate() {
103
3.92k
  if (g_initialize_count > 0) {
104
3.92k
    if (--g_initialize_count == 0) {
105
3.92k
      PluginManager::UnregisterPlugin(PlatformFreeBSD::CreateInstance);
106
3.92k
    }
107
3.92k
  }
108
109
3.92k
  PlatformPOSIX::Terminate();
110
3.92k
}
111
112
/// Default Constructor
113
PlatformFreeBSD::PlatformFreeBSD(bool is_host)
114
11
    : PlatformPOSIX(is_host) // This is the local host platform
115
11
{
116
11
  if (is_host) {
117
0
    ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
118
0
    m_supported_architectures.push_back(hostArch);
119
0
    if (hostArch.GetTriple().isArch64Bit()) {
120
0
      m_supported_architectures.push_back(
121
0
          HostInfo::GetArchitecture(HostInfo::eArchKind32));
122
0
    }
123
11
  } else {
124
11
    m_supported_architectures = CreateArchList(
125
11
        {llvm::Triple::x86_64, llvm::Triple::x86, llvm::Triple::aarch64,
126
11
         llvm::Triple::arm, llvm::Triple::mips64, llvm::Triple::ppc64,
127
11
         llvm::Triple::ppc},
128
11
        llvm::Triple::FreeBSD);
129
11
  }
130
11
}
131
132
std::vector<ArchSpec>
133
22
PlatformFreeBSD::GetSupportedArchitectures(const ArchSpec &process_host_arch) {
134
22
  if (m_remote_platform_sp)
135
0
    return m_remote_platform_sp->GetSupportedArchitectures(process_host_arch);
136
22
  return m_supported_architectures;
137
22
}
138
139
1
void PlatformFreeBSD::GetStatus(Stream &strm) {
140
1
  Platform::GetStatus(strm);
141
142
1
#if LLDB_ENABLE_POSIX
143
  // Display local kernel information only when we are running in host mode.
144
  // Otherwise, we would end up printing non-FreeBSD information (when running
145
  // on Mac OS for example).
146
1
  if (IsHost()) {
147
0
    struct utsname un;
148
149
0
    if (uname(&un))
150
0
      return;
151
152
0
    strm.Printf("    Kernel: %s\n", un.sysname);
153
0
    strm.Printf("   Release: %s\n", un.release);
154
0
    strm.Printf("   Version: %s\n", un.version);
155
0
  }
156
1
#endif
157
1
}
158
159
0
bool PlatformFreeBSD::CanDebugProcess() {
160
0
  if (IsHost()) {
161
0
    return true;
162
0
  } else {
163
    // If we're connected, we can debug.
164
0
    return IsConnected();
165
0
  }
166
0
}
167
168
0
void PlatformFreeBSD::CalculateTrapHandlerSymbolNames() {
169
0
  m_trap_handlers.push_back(ConstString("_sigtramp"));
170
0
}
171
172
MmapArgList PlatformFreeBSD::GetMmapArgumentList(const ArchSpec &arch,
173
                                                 addr_t addr, addr_t length,
174
                                                 unsigned prot, unsigned flags,
175
0
                                                 addr_t fd, addr_t offset) {
176
0
  uint64_t flags_platform = 0;
177
178
0
  if (flags & eMmapFlagsPrivate)
179
0
    flags_platform |= MAP_PRIVATE;
180
0
  if (flags & eMmapFlagsAnon)
181
0
    flags_platform |= MAP_ANON;
182
183
0
  MmapArgList args({addr, length, prot, flags_platform, fd, offset});
184
0
  if (arch.GetTriple().getArch() == llvm::Triple::x86)
185
0
    args.push_back(0);
186
0
  return args;
187
0
}
188
189
4
CompilerType PlatformFreeBSD::GetSiginfoType(const llvm::Triple &triple) {
190
4
  {
191
4
    std::lock_guard<std::mutex> guard(m_mutex);
192
4
    if (!m_type_system)
193
4
      m_type_system = std::make_shared<TypeSystemClang>("siginfo", triple);
194
4
  }
195
4
  TypeSystemClang *ast = m_type_system.get();
196
197
  // generic types
198
4
  CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
199
4
  CompilerType uint_type = ast->GetBasicType(eBasicTypeUnsignedInt);
200
4
  CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
201
4
  CompilerType voidp_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
202
203
  // platform-specific types
204
4
  CompilerType &pid_type = int_type;
205
4
  CompilerType &uid_type = uint_type;
206
207
4
  CompilerType sigval_type = ast->CreateRecordType(
208
4
      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
209
4
      clang::TTK_Union, lldb::eLanguageTypeC);
210
4
  ast->StartTagDeclarationDefinition(sigval_type);
211
4
  ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
212
4
                            lldb::eAccessPublic, 0);
213
4
  ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
214
4
                            lldb::eAccessPublic, 0);
215
4
  ast->CompleteTagDeclarationDefinition(sigval_type);
216
217
  // siginfo_t
218
4
  CompilerType siginfo_type = ast->CreateRecordType(
219
4
      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
220
4
      clang::TTK_Struct, lldb::eLanguageTypeC);
221
4
  ast->StartTagDeclarationDefinition(siginfo_type);
222
4
  ast->AddFieldToRecordType(siginfo_type, "si_signo", int_type,
223
4
                            lldb::eAccessPublic, 0);
224
4
  ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
225
4
                            lldb::eAccessPublic, 0);
226
4
  ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
227
4
                            lldb::eAccessPublic, 0);
228
4
  ast->AddFieldToRecordType(siginfo_type, "si_pid", pid_type,
229
4
                            lldb::eAccessPublic, 0);
230
4
  ast->AddFieldToRecordType(siginfo_type, "si_uid", uid_type,
231
4
                            lldb::eAccessPublic, 0);
232
4
  ast->AddFieldToRecordType(siginfo_type, "si_status", int_type,
233
4
                            lldb::eAccessPublic, 0);
234
4
  ast->AddFieldToRecordType(siginfo_type, "si_addr", voidp_type,
235
4
                            lldb::eAccessPublic, 0);
236
4
  ast->AddFieldToRecordType(siginfo_type, "si_value", sigval_type,
237
4
                            lldb::eAccessPublic, 0);
238
239
  // union used to hold the signal data
240
4
  CompilerType union_type = ast->CreateRecordType(
241
4
      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
242
4
      clang::TTK_Union, lldb::eLanguageTypeC);
243
4
  ast->StartTagDeclarationDefinition(union_type);
244
245
4
  ast->AddFieldToRecordType(
246
4
      union_type, "_fault",
247
4
      ast->CreateStructForIdentifier(llvm::StringRef(),
248
4
                                     {
249
4
                                         {"_trapno", int_type},
250
4
                                     }),
251
4
      lldb::eAccessPublic, 0);
252
253
4
  ast->AddFieldToRecordType(
254
4
      union_type, "_timer",
255
4
      ast->CreateStructForIdentifier(llvm::StringRef(),
256
4
                                     {
257
4
                                         {"_timerid", int_type},
258
4
                                         {"_overrun", int_type},
259
4
                                     }),
260
4
      lldb::eAccessPublic, 0);
261
262
4
  ast->AddFieldToRecordType(
263
4
      union_type, "_mesgq",
264
4
      ast->CreateStructForIdentifier(llvm::StringRef(),
265
4
                                     {
266
4
                                         {"_mqd", int_type},
267
4
                                     }),
268
4
      lldb::eAccessPublic, 0);
269
270
4
  ast->AddFieldToRecordType(
271
4
      union_type, "_poll",
272
4
      ast->CreateStructForIdentifier(llvm::StringRef(),
273
4
                                     {
274
4
                                         {"_band", long_type},
275
4
                                     }),
276
4
      lldb::eAccessPublic, 0);
277
278
4
  ast->CompleteTagDeclarationDefinition(union_type);
279
4
  ast->AddFieldToRecordType(siginfo_type, "_reason", union_type,
280
4
                            lldb::eAccessPublic, 0);
281
282
4
  ast->CompleteTagDeclarationDefinition(siginfo_type);
283
4
  return siginfo_type;
284
4
}