blob: 11ac11551064215aa97cb3242765568f1e6358ee [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
Kate Stoneb9c1b512016-09-06 20:57:50 +000024bool HostInfoNetBSD::GetOSVersion(uint32_t &major, uint32_t &minor,
25 uint32_t &update) {
26 struct utsname un;
Bruce Mitchener910af4d2015-10-13 05:04:13 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028 ::memset(&un, 0, sizeof(un));
29 if (::uname(&un) < 0)
Bruce Mitchener910af4d2015-10-13 05:04:13 +000030 return false;
Bruce Mitchener910af4d2015-10-13 05:04:13 +000031
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 /* Accept versions like 7.99.21 and 6.1_STABLE */
33 int status = ::sscanf(un.release, "%" PRIu32 ".%" PRIu32 ".%" PRIu32, &major,
34 &minor, &update);
35 switch (status) {
36 case 0:
37 return false;
38 case 1:
39 minor = 0;
40 /* FALLTHROUGH */
41 case 2:
42 update = 0;
43 /* FALLTHROUGH */
44 case 3:
45 default:
Bruce Mitchener910af4d2015-10-13 05:04:13 +000046 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 }
Bruce Mitchener910af4d2015-10-13 05:04:13 +000048}
49
Kate Stoneb9c1b512016-09-06 20:57:50 +000050bool HostInfoNetBSD::GetOSBuildString(std::string &s) {
51 int mib[2] = {CTL_KERN, KERN_OSREV};
52 char osrev_str[12];
53 int osrev = 0;
54 size_t osrev_len = sizeof(osrev);
Bruce Mitchener910af4d2015-10-13 05:04:13 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0) {
57 ::snprintf(osrev_str, sizeof(osrev_str), "%-10.10d", osrev);
58 s.assign(osrev_str);
59 return true;
60 }
Bruce Mitchener910af4d2015-10-13 05:04:13 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 s.clear();
63 return false;
64}
65
66bool HostInfoNetBSD::GetOSKernelDescription(std::string &s) {
67 struct utsname un;
68
69 ::memset(&un, 0, sizeof(un));
70 s.clear();
71
72 if (::uname(&un) < 0)
73 return false;
74
75 s.assign(un.version);
76
77 return true;
78}
79
80FileSpec HostInfoNetBSD::GetProgramFileSpec() {
81 static FileSpec g_program_filespec;
82
83 if (!g_program_filespec) {
Kamil Rytarowski3caaaa92017-01-28 20:04:53 +000084 static const int name[] = {
85 CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME,
86 };
87 char path[MAXPATHLEN];
88 size_t len;
Kate Stoneb9c1b512016-09-06 20:57:50 +000089
Kamil Rytarowski3caaaa92017-01-28 20:04:53 +000090 len = sizeof(path);
91 if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1) {
Jonas Devliegheredd2f78e2018-06-13 22:23:48 +000092 g_program_filespec.SetFile(path, false, FileSpec::Style::native);
Bruce Mitchener910af4d2015-10-13 05:04:13 +000093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 }
95 return g_program_filespec;
Bruce Mitchener910af4d2015-10-13 05:04:13 +000096}