1 //===-- HostInfoFreeBSD.cpp -----------------------------------------------===//
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/freebsd/HostInfoFreeBSD.h"
10 #include "llvm/Support/FormatVariadic.h"
11 #include <cstdio>
12 #include <cstring>
13 #include <sys/sysctl.h>
14 #include <sys/types.h>
15 #include <sys/utsname.h>
16 #include <unistd.h>
17 
18 using namespace lldb_private;
19 
20 llvm::VersionTuple HostInfoFreeBSD::GetOSVersion() {
21   struct utsname un;
22 
23   ::memset(&un, 0, sizeof(utsname));
24   if (uname(&un) < 0)
25     return llvm::VersionTuple();
26 
27   unsigned major, minor;
28   if (2 == sscanf(un.release, "%u.%u", &major, &minor))
29     return llvm::VersionTuple(major, minor);
30   return llvm::VersionTuple();
31 }
32 
33 llvm::Optional<std::string> HostInfoFreeBSD::GetOSBuildString() {
34   int mib[2] = {CTL_KERN, KERN_OSREV};
35   char osrev_str[12];
36   uint32_t osrev = 0;
37   size_t osrev_len = sizeof(osrev);
38 
39   if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0)
40     return llvm::formatv("{0,8:8}", osrev).str();
41 
42   return llvm::None;
43 }
44 
45 bool HostInfoFreeBSD::GetOSKernelDescription(std::string &s) {
46   struct utsname un;
47 
48   ::memset(&un, 0, sizeof(utsname));
49   s.clear();
50 
51   if (uname(&un) < 0)
52     return false;
53 
54   s.assign(un.version);
55 
56   return true;
57 }
58 
59 FileSpec HostInfoFreeBSD::GetProgramFileSpec() {
60   static FileSpec g_program_filespec;
61   if (!g_program_filespec) {
62     int exe_path_mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid()};
63     char exe_path[PATH_MAX];
64     size_t exe_path_size = sizeof(exe_path);
65     if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
66       g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
67   }
68   return g_program_filespec;
69 }
70