blob: ff0cec707b7af15350592d46593542694c8612aa [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)
Pavel Labath76ba4972018-06-18 15:44:36 +000029 return VersionTuple();
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 */
Pavel Labath76ba4972018-06-18 15:44:36 +000032 uint32_t major, minor, update;
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 int status = ::sscanf(un.release, "%" PRIu32 ".%" PRIu32 ".%" PRIu32, &major,
34 &minor, &update);
35 switch (status) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 case 1:
Pavel Labath2272c482018-06-18 15:02:23 +000037 return llvm::VersionTuple(major);
Kate Stoneb9c1b512016-09-06 20:57:50 +000038 case 2:
Pavel Labath2272c482018-06-18 15:02:23 +000039 return llvm::VersionTuple(major, minor);
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 case 3:
Pavel Labath2272c482018-06-18 15:02:23 +000041 return llvm::VersionTuple(major, minor, update);
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 }
Pavel Labath2272c482018-06-18 15:02:23 +000043 return llvm::VersionTuple();
Bruce Mitchener910af4d2015-10-13 05:04:13 +000044}
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046bool HostInfoNetBSD::GetOSBuildString(std::string &s) {
47 int mib[2] = {CTL_KERN, KERN_OSREV};
48 char osrev_str[12];
49 int osrev = 0;
50 size_t osrev_len = sizeof(osrev);
Bruce Mitchener910af4d2015-10-13 05:04:13 +000051
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0) {
53 ::snprintf(osrev_str, sizeof(osrev_str), "%-10.10d", osrev);
54 s.assign(osrev_str);
55 return true;
56 }
Bruce Mitchener910af4d2015-10-13 05:04:13 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 s.clear();
59 return false;
60}
61
62bool HostInfoNetBSD::GetOSKernelDescription(std::string &s) {
63 struct utsname un;
64
65 ::memset(&un, 0, sizeof(un));
66 s.clear();
67
68 if (::uname(&un) < 0)
69 return false;
70
71 s.assign(un.version);
72
73 return true;
74}
75
76FileSpec HostInfoNetBSD::GetProgramFileSpec() {
77 static FileSpec g_program_filespec;
78
79 if (!g_program_filespec) {
Kamil Rytarowski3caaaa92017-01-28 20:04:53 +000080 static const int name[] = {
81 CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME,
82 };
83 char path[MAXPATHLEN];
84 size_t len;
Kate Stoneb9c1b512016-09-06 20:57:50 +000085
Kamil Rytarowski3caaaa92017-01-28 20:04:53 +000086 len = sizeof(path);
87 if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1) {
Jonas Devliegheredd2f78e2018-06-13 22:23:48 +000088 g_program_filespec.SetFile(path, false, FileSpec::Style::native);
Bruce Mitchener910af4d2015-10-13 05:04:13 +000089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 }
91 return g_program_filespec;
Bruce Mitchener910af4d2015-10-13 05:04:13 +000092}