blob: 19afae3f698d76f07067422cfdbb73d557859293 [file] [log] [blame]
Bruce Mitchener910af4d2015-10-13 05:04:13 +00001//===-- HostInfoNetBSD.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Host/netbsd/HostInfoNetBSD.h"
11
Kate Stoneb9c1b512016-09-06 20:57:50 +000012#include <inttypes.h>
Bruce Mitchener910af4d2015-10-13 05:04:13 +000013#include <limits.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000014#include <pthread.h>
Bruce Mitchener910af4d2015-10-13 05:04:13 +000015#include <stdio.h>
16#include <string.h>
Bruce Mitchener910af4d2015-10-13 05:04:13 +000017#include <sys/sysctl.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include <sys/types.h>
Bruce Mitchener910af4d2015-10-13 05:04:13 +000019#include <sys/utsname.h>
20#include <unistd.h>
Bruce Mitchener910af4d2015-10-13 05:04:13 +000021
22using namespace lldb_private;
23
Pavel Labath2272c482018-06-18 15:02:23 +000024llvm::VersionTuple HostInfoNetBSD::GetOSVersion() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000025 struct utsname un;
Bruce Mitchener910af4d2015-10-13 05:04:13 +000026
Kate Stoneb9c1b512016-09-06 20:57:50 +000027 ::memset(&un, 0, sizeof(un));
28 if (::uname(&un) < 0)
Bruce Mitchener910af4d2015-10-13 05:04:13 +000029 return false;
Bruce Mitchener910af4d2015-10-13 05:04:13 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 /* Accept versions like 7.99.21 and 6.1_STABLE */
32 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) {
Jonas Devliegheredd2f78e2018-06-13 22:23:48 +000087 g_program_filespec.SetFile(path, false, 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}