Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- PlatformNetBSD.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 "PlatformNetBSD.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 NetBSD mman.h for use when targeting remote
29
// netbsd 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_netbsd;
36
37
LLDB_PLUGIN_DEFINE(PlatformNetBSD)
38
39
static uint32_t g_initialize_count = 0;
40
41
42
77
PlatformSP PlatformNetBSD::CreateInstance(bool force, const ArchSpec *arch) {
43
77
  Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
44
77
  LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force,
45
77
           arch ? arch->GetArchitectureName() : "<null>",
46
77
           arch ? arch->GetTriple().getTriple() : "<null>");
47
48
77
  bool create = force;
49
77
  if (!create && arch && arch->IsValid()) {
50
77
    const llvm::Triple &triple = arch->GetTriple();
51
77
    switch (triple.getOS()) {
52
14
    case llvm::Triple::NetBSD:
53
14
      create = true;
54
14
      break;
55
56
63
    default:
57
63
      break;
58
77
    }
59
77
  }
60
61
77
  LLDB_LOG(log, "create = {0}", create);
62
77
  if (create) {
63
14
    return PlatformSP(new PlatformNetBSD(false));
64
14
  }
65
63
  return PlatformSP();
66
77
}
67
68
3.44k
llvm::StringRef PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) {
69
3.44k
  if (is_host)
70
0
    return "Local NetBSD user platform plug-in.";
71
3.44k
  return "Remote NetBSD user platform plug-in.";
72
3.44k
}
73
74
3.44k
void PlatformNetBSD::Initialize() {
75
3.44k
  PlatformPOSIX::Initialize();
76
77
3.44k
  if (g_initialize_count++ == 0) {
78
#if defined(__NetBSD__)
79
    PlatformSP default_platform_sp(new PlatformNetBSD(true));
80
    default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
81
    Platform::SetHostPlatform(default_platform_sp);
82
#endif
83
3.44k
    PluginManager::RegisterPlugin(
84
3.44k
        PlatformNetBSD::GetPluginNameStatic(false),
85
3.44k
        PlatformNetBSD::GetPluginDescriptionStatic(false),
86
3.44k
        PlatformNetBSD::CreateInstance, nullptr);
87
3.44k
  }
88
3.44k
}
89
90
3.43k
void PlatformNetBSD::Terminate() {
91
3.43k
  if (g_initialize_count > 0) {
92
3.43k
    if (--g_initialize_count == 0) {
93
3.43k
      PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance);
94
3.43k
    }
95
3.43k
  }
96
97
3.43k
  PlatformPOSIX::Terminate();
98
3.43k
}
99
100
/// Default Constructor
101
PlatformNetBSD::PlatformNetBSD(bool is_host)
102
    : PlatformPOSIX(is_host) // This is the local host platform
103
14
{
104
14
  if (is_host) {
105
0
    ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
106
0
    m_supported_architectures.push_back(hostArch);
107
0
    if (hostArch.GetTriple().isArch64Bit()) {
108
0
      m_supported_architectures.push_back(
109
0
          HostInfo::GetArchitecture(HostInfo::eArchKind32));
110
0
    }
111
14
  } else {
112
14
    m_supported_architectures = CreateArchList(
113
14
        {llvm::Triple::x86_64, llvm::Triple::x86}, llvm::Triple::NetBSD);
114
14
  }
115
14
}
116
117
31
std::vector<ArchSpec> PlatformNetBSD::GetSupportedArchitectures() {
118
31
  if (m_remote_platform_sp)
119
0
    return m_remote_platform_sp->GetSupportedArchitectures();
120
31
  return m_supported_architectures;
121
31
}
122
123
0
void PlatformNetBSD::GetStatus(Stream &strm) {
124
0
  Platform::GetStatus(strm);
125
126
0
#if LLDB_ENABLE_POSIX
127
  // Display local kernel information only when we are running in host mode.
128
  // Otherwise, we would end up printing non-NetBSD information (when running
129
  // on Mac OS for example).
130
0
  if (IsHost()) {
131
0
    struct utsname un;
132
133
0
    if (uname(&un))
134
0
      return;
135
136
0
    strm.Printf("    Kernel: %s\n", un.sysname);
137
0
    strm.Printf("   Release: %s\n", un.release);
138
0
    strm.Printf("   Version: %s\n", un.version);
139
0
  }
140
0
#endif
141
0
}
142
143
uint32_t
144
0
PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
145
0
  uint32_t resume_count = 0;
146
147
  // Always resume past the initial stop when we use eLaunchFlagDebug
148
0
  if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
149
    // Resume past the stop for the final exec into the true inferior.
150
0
    ++resume_count;
151
0
  }
152
153
  // If we're not launching a shell, we're done.
154
0
  const FileSpec &shell = launch_info.GetShell();
155
0
  if (!shell)
156
0
    return resume_count;
157
158
0
  std::string shell_string = shell.GetPath();
159
  // We're in a shell, so for sure we have to resume past the shell exec.
160
0
  ++resume_count;
161
162
  // Figure out what shell we're planning on using.
163
0
  const char *shell_name = strrchr(shell_string.c_str(), '/');
164
0
  if (shell_name == nullptr)
165
0
    shell_name = shell_string.c_str();
166
0
  else
167
0
    shell_name++;
168
169
0
  if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
170
0
      strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
171
    // These shells seem to re-exec themselves.  Add another resume.
172
0
    ++resume_count;
173
0
  }
174
175
0
  return resume_count;
176
0
}
177
178
0
bool PlatformNetBSD::CanDebugProcess() {
179
0
  if (IsHost()) {
180
0
    return true;
181
0
  } else {
182
    // If we're connected, we can debug.
183
0
    return IsConnected();
184
0
  }
185
0
}
186
187
1
void PlatformNetBSD::CalculateTrapHandlerSymbolNames() {
188
1
  m_trap_handlers.push_back(ConstString("_sigtramp"));
189
1
}
190
191
MmapArgList PlatformNetBSD::GetMmapArgumentList(const ArchSpec &arch,
192
                                                addr_t addr, addr_t length,
193
                                                unsigned prot, unsigned flags,
194
0
                                                addr_t fd, addr_t offset) {
195
0
  uint64_t flags_platform = 0;
196
197
0
  if (flags & eMmapFlagsPrivate)
198
0
    flags_platform |= MAP_PRIVATE;
199
0
  if (flags & eMmapFlagsAnon)
200
0
    flags_platform |= MAP_ANON;
201
202
0
  MmapArgList args({addr, length, prot, flags_platform, fd, offset});
203
0
  return args;
204
0
}