Coverage Report

Created: 2022-01-22 13:19

/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/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
0
#define MAP_PRIVATE 0x0002
36
0
#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
93
PlatformSP PlatformFreeBSD::CreateInstance(bool force, const ArchSpec *arch) {
48
93
  Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
49
93
  LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
50
93
           arch ? arch->GetArchitectureName() : "<null>",
51
93
           arch ? arch->GetTriple().getTriple() : "<null>");
52
53
93
  bool create = force;
54
93
  if (!create && arch && arch->IsValid()) {
55
93
    const llvm::Triple &triple = arch->GetTriple();
56
93
    switch (triple.getOS()) {
57
1
    case llvm::Triple::FreeBSD:
58
1
      create = true;
59
1
      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
92
    default:
69
92
      break;
70
93
    }
71
93
  }
72
93
  LLDB_LOG(log, "create = {0}", create);
73
93
  if (create) {
74
1
    return PlatformSP(new PlatformFreeBSD(false));
75
1
  }
76
92
  return PlatformSP();
77
93
}
78
79
3.44k
llvm::StringRef PlatformFreeBSD::GetPluginDescriptionStatic(bool is_host) {
80
3.44k
  if (is_host)
81
0
    return "Local FreeBSD user platform plug-in.";
82
3.44k
  return "Remote FreeBSD user platform plug-in.";
83
3.44k
}
84
85
3.44k
void PlatformFreeBSD::Initialize() {
86
3.44k
  Platform::Initialize();
87
88
3.44k
  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
3.44k
    PluginManager::RegisterPlugin(
95
3.44k
        PlatformFreeBSD::GetPluginNameStatic(false),
96
3.44k
        PlatformFreeBSD::GetPluginDescriptionStatic(false),
97
3.44k
        PlatformFreeBSD::CreateInstance, nullptr);
98
3.44k
  }
99
3.44k
}
100
101
3.43k
void PlatformFreeBSD::Terminate() {
102
3.43k
  if (g_initialize_count > 0) {
103
3.43k
    if (--g_initialize_count == 0) {
104
3.43k
      PluginManager::UnregisterPlugin(PlatformFreeBSD::CreateInstance);
105
3.43k
    }
106
3.43k
  }
107
108
3.43k
  PlatformPOSIX::Terminate();
109
3.43k
}
110
111
/// Default Constructor
112
PlatformFreeBSD::PlatformFreeBSD(bool is_host)
113
    : PlatformPOSIX(is_host) // This is the local host platform
114
1
{
115
1
  if (is_host) {
116
0
    ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
117
0
    m_supported_architectures.push_back(hostArch);
118
0
    if (hostArch.GetTriple().isArch64Bit()) {
119
0
      m_supported_architectures.push_back(
120
0
          HostInfo::GetArchitecture(HostInfo::eArchKind32));
121
0
    }
122
1
  } else {
123
1
    m_supported_architectures = CreateArchList(
124
1
        {llvm::Triple::x86_64, llvm::Triple::x86, llvm::Triple::aarch64,
125
1
         llvm::Triple::arm, llvm::Triple::mips64, llvm::Triple::ppc64,
126
1
         llvm::Triple::ppc},
127
1
        llvm::Triple::FreeBSD);
128
1
  }
129
1
}
130
131
9
std::vector<ArchSpec> PlatformFreeBSD::GetSupportedArchitectures() {
132
9
  if (m_remote_platform_sp)
133
0
    return m_remote_platform_sp->GetSupportedArchitectures();
134
9
  return m_supported_architectures;
135
9
}
136
137
0
void PlatformFreeBSD::GetStatus(Stream &strm) {
138
0
  Platform::GetStatus(strm);
139
140
0
#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
0
  if (IsHost()) {
145
0
    struct utsname un;
146
147
0
    if (uname(&un))
148
0
      return;
149
150
0
    strm.Printf("    Kernel: %s\n", un.sysname);
151
0
    strm.Printf("   Release: %s\n", un.release);
152
0
    strm.Printf("   Version: %s\n", un.version);
153
0
  }
154
0
#endif
155
0
}
156
157
0
bool PlatformFreeBSD::CanDebugProcess() {
158
0
  if (IsHost()) {
159
0
    return true;
160
0
  } else {
161
    // If we're connected, we can debug.
162
0
    return IsConnected();
163
0
  }
164
0
}
165
166
0
void PlatformFreeBSD::CalculateTrapHandlerSymbolNames() {
167
0
  m_trap_handlers.push_back(ConstString("_sigtramp"));
168
0
}
169
170
MmapArgList PlatformFreeBSD::GetMmapArgumentList(const ArchSpec &arch,
171
                                                 addr_t addr, addr_t length,
172
                                                 unsigned prot, unsigned flags,
173
0
                                                 addr_t fd, addr_t offset) {
174
0
  uint64_t flags_platform = 0;
175
176
0
  if (flags & eMmapFlagsPrivate)
177
0
    flags_platform |= MAP_PRIVATE;
178
0
  if (flags & eMmapFlagsAnon)
179
0
    flags_platform |= MAP_ANON;
180
181
0
  MmapArgList args({addr, length, prot, flags_platform, fd, offset});
182
0
  if (arch.GetTriple().getArch() == llvm::Triple::x86)
183
0
    args.push_back(0);
184
0
  return args;
185
0
}