blob: 99d4139223276405ec58152c38f52e807a4c2507 [file] [log] [blame]
Bruce Mitchener910af4d2015-10-13 05:04:13 +00001//===-- HostInfoNetBSD.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Bruce Mitchener910af4d2015-10-13 05:04:13 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Host/netbsd/HostInfoNetBSD.h"
10
Kate Stoneb9c1b512016-09-06 20:57:50 +000011#include <inttypes.h>
Bruce Mitchener910af4d2015-10-13 05:04:13 +000012#include <limits.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000013#include <pthread.h>
Bruce Mitchener910af4d2015-10-13 05:04:13 +000014#include <stdio.h>
15#include <string.h>
Bruce Mitchener910af4d2015-10-13 05:04:13 +000016#include <sys/sysctl.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include <sys/types.h>
Bruce Mitchener910af4d2015-10-13 05:04:13 +000018#include <sys/utsname.h>
19#include <unistd.h>
Bruce Mitchener910af4d2015-10-13 05:04:13 +000020
21using namespace lldb_private;
22
Pavel Labath2272c482018-06-18 15:02:23 +000023llvm::VersionTuple HostInfoNetBSD::GetOSVersion() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000024 struct utsname un;
Bruce Mitchener910af4d2015-10-13 05:04:13 +000025
Kate Stoneb9c1b512016-09-06 20:57:50 +000026 ::memset(&un, 0, sizeof(un));
27 if (::uname(&un) < 0)
Pavel Labath66829792018-06-18 16:10:20 +000028 return llvm::VersionTuple();
Bruce Mitchener910af4d2015-10-13 05:04:13 +000029
Kate Stoneb9c1b512016-09-06 20:57:50 +000030 /* Accept versions like 7.99.21 and 6.1_STABLE */
Pavel Labath76ba4972018-06-18 15:44:36 +000031 uint32_t major, minor, update;
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 int status = ::sscanf(un.release, "%" PRIu32 ".%" PRIu32 ".%" PRIu32, &major,
33 &minor, &update);
34 switch (status) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 case 1:
Pavel Labath2272c482018-06-18 15:02:23 +000036 return llvm::VersionTuple(major);
Kate Stoneb9c1b512016-09-06 20:57:50 +000037 case 2:
Pavel Labath2272c482018-06-18 15:02:23 +000038 return llvm::VersionTuple(major, minor);
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 case 3:
Pavel Labath2272c482018-06-18 15:02:23 +000040 return llvm::VersionTuple(major, minor, update);
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 }
Pavel Labath2272c482018-06-18 15:02:23 +000042 return llvm::VersionTuple();
Bruce Mitchener910af4d2015-10-13 05:04:13 +000043}
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045bool HostInfoNetBSD::GetOSBuildString(std::string &s) {
46 int mib[2] = {CTL_KERN, KERN_OSREV};
47 char osrev_str[12];
48 int osrev = 0;
49 size_t osrev_len = sizeof(osrev);
Bruce Mitchener910af4d2015-10-13 05:04:13 +000050
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0) {
52 ::snprintf(osrev_str, sizeof(osrev_str), "%-10.10d", osrev);
53 s.assign(osrev_str);
54 return true;
55 }
Bruce Mitchener910af4d2015-10-13 05:04:13 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 s.clear();
58 return false;
59}
60
61bool HostInfoNetBSD::GetOSKernelDescription(std::string &s) {
62 struct utsname un;
63
64 ::memset(&un, 0, sizeof(un));
65 s.clear();
66
67 if (::uname(&un) < 0)
68 return false;
69
70 s.assign(un.version);
71
72 return true;
73}
74
75FileSpec HostInfoNetBSD::GetProgramFileSpec() {
76 static FileSpec g_program_filespec;
77
78 if (!g_program_filespec) {
Kamil Rytarowski3caaaa92017-01-28 20:04:53 +000079 static const int name[] = {
80 CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME,
81 };
82 char path[MAXPATHLEN];
83 size_t len;
Kate Stoneb9c1b512016-09-06 20:57:50 +000084
Kamil Rytarowski3caaaa92017-01-28 20:04:53 +000085 len = sizeof(path);
86 if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1) {
Kamil Rytarowskia0a44e92018-11-04 16:53:16 +000087 g_program_filespec.SetFile(path, FileSpec::Style::native);
Bruce Mitchener910af4d2015-10-13 05:04:13 +000088 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 }
90 return g_program_filespec;
Bruce Mitchener910af4d2015-10-13 05:04:13 +000091}