Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Platform/OpenBSD/PlatformOpenBSD.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- PlatformOpenBSD.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 "PlatformOpenBSD.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/Core/Debugger.h"
18
#include "lldb/Core/PluginManager.h"
19
#include "lldb/Host/HostInfo.h"
20
#include "lldb/Target/Process.h"
21
#include "lldb/Target/Target.h"
22
#include "lldb/Utility/FileSpec.h"
23
#include "lldb/Utility/Log.h"
24
#include "lldb/Utility/State.h"
25
#include "lldb/Utility/Status.h"
26
#include "lldb/Utility/StreamString.h"
27
28
// Define these constants from OpenBSD mman.h for use when targeting remote
29
// openbsd systems even when host has different values.
30
0
#define MAP_PRIVATE 0x0002
31
0
#define MAP_ANON 0x1000
32
33
using namespace lldb;
34
using namespace lldb_private;
35
using namespace lldb_private::platform_openbsd;
36
37
LLDB_PLUGIN_DEFINE(PlatformOpenBSD)
38
39
static uint32_t g_initialize_count = 0;
40
41
42
75
PlatformSP PlatformOpenBSD::CreateInstance(bool force, const ArchSpec *arch) {
43
75
  Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
44
75
  LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
45
75
           arch ? arch->GetArchitectureName() : "<null>",
46
75
           arch ? arch->GetTriple().getTriple() : "<null>");
47
48
75
  bool create = force;
49
75
  if (!create && arch && arch->IsValid()) {
50
75
    const llvm::Triple &triple = arch->GetTriple();
51
75
    switch (triple.getOS()) {
52
0
    case llvm::Triple::OpenBSD:
53
0
      create = true;
54
0
      break;
55
56
#if defined(__OpenBSD__)
57
    // Only accept "unknown" for the OS if the host is BSD and it "unknown"
58
    // wasn't specified (it was just returned because it was NOT specified)
59
    case llvm::Triple::OSType::UnknownOS:
60
      create = !arch->TripleOSWasSpecified();
61
      break;
62
#endif
63
75
    default:
64
75
      break;
65
75
    }
66
75
  }
67
75
  LLDB_LOG(log, "create = {0}", create);
68
75
  if (create) {
69
0
    return PlatformSP(new PlatformOpenBSD(false));
70
0
  }
71
75
  return PlatformSP();
72
75
}
73
74
3.44k
llvm::StringRef PlatformOpenBSD::GetPluginDescriptionStatic(bool is_host) {
75
3.44k
  if (is_host)
76
0
    return "Local OpenBSD user platform plug-in.";
77
3.44k
  return "Remote OpenBSD user platform plug-in.";
78
3.44k
}
79
80
3.44k
void PlatformOpenBSD::Initialize() {
81
3.44k
  Platform::Initialize();
82
83
3.44k
  if (g_initialize_count++ == 0) {
84
#if defined(__OpenBSD__)
85
    PlatformSP default_platform_sp(new PlatformOpenBSD(true));
86
    default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
87
    Platform::SetHostPlatform(default_platform_sp);
88
#endif
89
3.44k
    PluginManager::RegisterPlugin(
90
3.44k
        PlatformOpenBSD::GetPluginNameStatic(false),
91
3.44k
        PlatformOpenBSD::GetPluginDescriptionStatic(false),
92
3.44k
        PlatformOpenBSD::CreateInstance, nullptr);
93
3.44k
  }
94
3.44k
}
95
96
3.43k
void PlatformOpenBSD::Terminate() {
97
3.43k
  if (g_initialize_count > 0) {
98
3.43k
    if (--g_initialize_count == 0) {
99
3.43k
      PluginManager::UnregisterPlugin(PlatformOpenBSD::CreateInstance);
100
3.43k
    }
101
3.43k
  }
102
103
3.43k
  PlatformPOSIX::Terminate();
104
3.43k
}
105
106
/// Default Constructor
107
PlatformOpenBSD::PlatformOpenBSD(bool is_host)
108
    : PlatformPOSIX(is_host) // This is the local host platform
109
0
{
110
0
  if (is_host) {
111
0
    m_supported_architectures.push_back(HostInfo::GetArchitecture());
112
0
  } else {
113
0
    m_supported_architectures =
114
0
        CreateArchList({llvm::Triple::x86_64, llvm::Triple::x86,
115
0
                        llvm::Triple::aarch64, llvm::Triple::arm},
116
0
                       llvm::Triple::OpenBSD);
117
0
  }
118
0
}
119
120
0
std::vector<ArchSpec> PlatformOpenBSD::GetSupportedArchitectures() {
121
0
  if (m_remote_platform_sp)
122
0
    return m_remote_platform_sp->GetSupportedArchitectures();
123
0
  return m_supported_architectures;
124
0
}
125
126
0
void PlatformOpenBSD::GetStatus(Stream &strm) {
127
0
  Platform::GetStatus(strm);
128
129
0
#if LLDB_ENABLE_POSIX
130
  // Display local kernel information only when we are running in host mode.
131
  // Otherwise, we would end up printing non-OpenBSD information (when running
132
  // on Mac OS for example).
133
0
  if (IsHost()) {
134
0
    struct utsname un;
135
136
0
    if (uname(&un))
137
0
      return;
138
139
0
    strm.Printf("    Kernel: %s\n", un.sysname);
140
0
    strm.Printf("   Release: %s\n", un.release);
141
0
    strm.Printf("   Version: %s\n", un.version);
142
0
  }
143
0
#endif
144
0
}
145
146
// OpenBSD processes cannot yet be launched by spawning and attaching.
147
0
bool PlatformOpenBSD::CanDebugProcess() {
148
0
  return false;
149
0
}
150
151
0
void PlatformOpenBSD::CalculateTrapHandlerSymbolNames() {
152
0
  m_trap_handlers.push_back(ConstString("_sigtramp"));
153
0
}
154
155
MmapArgList PlatformOpenBSD::GetMmapArgumentList(const ArchSpec &arch,
156
                                                 addr_t addr, addr_t length,
157
                                                 unsigned prot, unsigned flags,
158
0
                                                 addr_t fd, addr_t offset) {
159
0
  uint64_t flags_platform = 0;
160
161
0
  if (flags & eMmapFlagsPrivate)
162
0
    flags_platform |= MAP_PRIVATE;
163
0
  if (flags & eMmapFlagsAnon)
164
0
    flags_platform |= MAP_ANON;
165
166
0
  MmapArgList args({addr, length, prot, flags_platform, fd, offset});
167
0
  return args;
168
0
}