xref: /NextBSD/contrib/llvm/tools/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1 //===-- HostInfoFreeBSD.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/freebsd/HostInfoFreeBSD.h"
11 
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/sysctl.h>
16 #include <sys/utsname.h>
17 
18 using namespace lldb_private;
19 
20 uint32_t
GetMaxThreadNameLength()21 HostInfoFreeBSD::GetMaxThreadNameLength()
22 {
23     return 16;
24 }
25 
26 bool
GetOSVersion(uint32_t & major,uint32_t & minor,uint32_t & update)27 HostInfoFreeBSD::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
28 {
29     struct utsname un;
30 
31     ::memset(&un, 0, sizeof(utsname));
32     if (uname(&un) < 0)
33         return false;
34 
35     int status = sscanf(un.release, "%u.%u", &major, &minor);
36     return status == 2;
37 }
38 
39 bool
GetOSBuildString(std::string & s)40 HostInfoFreeBSD::GetOSBuildString(std::string &s)
41 {
42     int mib[2] = {CTL_KERN, KERN_OSREV};
43     char osrev_str[12];
44     uint32_t osrev = 0;
45     size_t osrev_len = sizeof(osrev);
46 
47     if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0)
48     {
49         ::snprintf(osrev_str, sizeof(osrev_str), "%-8.8u", osrev);
50         s.assign(osrev_str);
51         return true;
52     }
53 
54     s.clear();
55     return false;
56 }
57 
58 bool
GetOSKernelDescription(std::string & s)59 HostInfoFreeBSD::GetOSKernelDescription(std::string &s)
60 {
61     struct utsname un;
62 
63     ::memset(&un, 0, sizeof(utsname));
64     s.clear();
65 
66     if (uname(&un) < 0)
67         return false;
68 
69     s.assign(un.version);
70 
71     return true;
72 }
73 
74 FileSpec
GetProgramFileSpec()75 HostInfoFreeBSD::GetProgramFileSpec()
76 {
77     static FileSpec g_program_filespec;
78     if (!g_program_filespec)
79     {
80         int exe_path_mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid()};
81         size_t exe_path_size;
82         if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
83         {
84             char *exe_path = new char[exe_path_size];
85             if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
86                 g_program_filespec.SetFile(exe_path, false);
87             delete[] exe_path;
88         }
89     }
90     return g_program_filespec;
91 }