]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/netbsd/HostInfoNetBSD.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / netbsd / HostInfoNetBSD.cpp
1 //===-- HostInfoNetBSD.cpp -------------------------------------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Host/netbsd/HostInfoNetBSD.h"
10
11 #include <inttypes.h>
12 #include <limits.h>
13 #include <pthread.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/sysctl.h>
17 #include <sys/types.h>
18 #include <sys/utsname.h>
19 #include <unistd.h>
20
21 using namespace lldb_private;
22
23 llvm::VersionTuple HostInfoNetBSD::GetOSVersion() {
24   struct utsname un;
25
26   ::memset(&un, 0, sizeof(un));
27   if (::uname(&un) < 0)
28     return llvm::VersionTuple();
29
30   /* Accept versions like 7.99.21 and 6.1_STABLE */
31   uint32_t major, minor, update;
32   int status = ::sscanf(un.release, "%" PRIu32 ".%" PRIu32 ".%" PRIu32, &major,
33                         &minor, &update);
34   switch (status) {
35   case 1:
36     return llvm::VersionTuple(major);
37   case 2:
38     return llvm::VersionTuple(major, minor);
39   case 3:
40     return llvm::VersionTuple(major, minor, update);
41   }
42   return llvm::VersionTuple();
43 }
44
45 bool 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);
50
51   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   }
56
57   s.clear();
58   return false;
59 }
60
61 bool 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
75 FileSpec HostInfoNetBSD::GetProgramFileSpec() {
76   static FileSpec g_program_filespec;
77
78   if (!g_program_filespec) {
79     static const int name[] = {
80         CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME,
81     };
82     char path[MAXPATHLEN];
83     size_t len;
84
85     len = sizeof(path);
86     if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1) {
87       g_program_filespec.SetFile(path, FileSpec::Style::native);
88     }
89   }
90   return g_program_filespec;
91 }