blob: f28c8d16250de116403b54c2daba062e476cf30c [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 +000024uint32_t HostInfoNetBSD::GetMaxThreadNameLength() {
25 return PTHREAD_MAX_NAMELEN_NP;
Bruce Mitchener910af4d2015-10-13 05:04:13 +000026}
27
Kate Stoneb9c1b512016-09-06 20:57:50 +000028bool HostInfoNetBSD::GetOSVersion(uint32_t &major, uint32_t &minor,
29 uint32_t &update) {
30 struct utsname un;
Bruce Mitchener910af4d2015-10-13 05:04:13 +000031
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 ::memset(&un, 0, sizeof(un));
33 if (::uname(&un) < 0)
Bruce Mitchener910af4d2015-10-13 05:04:13 +000034 return false;
Bruce Mitchener910af4d2015-10-13 05:04:13 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 /* Accept versions like 7.99.21 and 6.1_STABLE */
37 int status = ::sscanf(un.release, "%" PRIu32 ".%" PRIu32 ".%" PRIu32, &major,
38 &minor, &update);
39 switch (status) {
40 case 0:
41 return false;
42 case 1:
43 minor = 0;
44 /* FALLTHROUGH */
45 case 2:
46 update = 0;
47 /* FALLTHROUGH */
48 case 3:
49 default:
Bruce Mitchener910af4d2015-10-13 05:04:13 +000050 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 }
Bruce Mitchener910af4d2015-10-13 05:04:13 +000052}
53
Kate Stoneb9c1b512016-09-06 20:57:50 +000054bool HostInfoNetBSD::GetOSBuildString(std::string &s) {
55 int mib[2] = {CTL_KERN, KERN_OSREV};
56 char osrev_str[12];
57 int osrev = 0;
58 size_t osrev_len = sizeof(osrev);
Bruce Mitchener910af4d2015-10-13 05:04:13 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0) {
61 ::snprintf(osrev_str, sizeof(osrev_str), "%-10.10d", osrev);
62 s.assign(osrev_str);
63 return true;
64 }
Bruce Mitchener910af4d2015-10-13 05:04:13 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 s.clear();
67 return false;
68}
69
70bool HostInfoNetBSD::GetOSKernelDescription(std::string &s) {
71 struct utsname un;
72
73 ::memset(&un, 0, sizeof(un));
74 s.clear();
75
76 if (::uname(&un) < 0)
77 return false;
78
79 s.assign(un.version);
80
81 return true;
82}
83
84FileSpec HostInfoNetBSD::GetProgramFileSpec() {
85 static FileSpec g_program_filespec;
86
87 if (!g_program_filespec) {
Kamil Rytarowski3caaaa92017-01-28 20:04:53 +000088 static const int name[] = {
89 CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME,
90 };
91 char path[MAXPATHLEN];
92 size_t len;
Kate Stoneb9c1b512016-09-06 20:57:50 +000093
Kamil Rytarowski3caaaa92017-01-28 20:04:53 +000094 len = sizeof(path);
95 if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1) {
96 g_program_filespec.SetFile(path, false);
Bruce Mitchener910af4d2015-10-13 05:04:13 +000097 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 }
99 return g_program_filespec;
Bruce Mitchener910af4d2015-10-13 05:04:13 +0000100}