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/sysctl.h> 15 #include <sys/types.h> 16 #include <sys/utsname.h> 17 #include <unistd.h> 18 19 using namespace lldb_private; 20 21 bool HostInfoFreeBSD::GetOSVersion(uint32_t &major, uint32_t &minor, 22 uint32_t &update) { 23 struct utsname un; 24 25 ::memset(&un, 0, sizeof(utsname)); 26 if (uname(&un) < 0) 27 return false; 28 29 int status = sscanf(un.release, "%u.%u", &major, &minor); 30 return status == 2; 31 } 32 33 bool HostInfoFreeBSD::GetOSBuildString(std::string &s) { 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 ::snprintf(osrev_str, sizeof(osrev_str), "%-8.8u", osrev); 41 s.assign(osrev_str); 42 return true; 43 } 44 45 s.clear(); 46 return false; 47 } 48 49 bool HostInfoFreeBSD::GetOSKernelDescription(std::string &s) { 50 struct utsname un; 51 52 ::memset(&un, 0, sizeof(utsname)); 53 s.clear(); 54 55 if (uname(&un) < 0) 56 return false; 57 58 s.assign(un.version); 59 60 return true; 61 } 62 63 FileSpec HostInfoFreeBSD::GetProgramFileSpec() { 64 static FileSpec g_program_filespec; 65 if (!g_program_filespec) { 66 int exe_path_mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid()}; 67 size_t exe_path_size; 68 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0) { 69 char *exe_path = new char[exe_path_size]; 70 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0) 71 g_program_filespec.SetFile(exe_path, false); 72 delete[] exe_path; 73 } 74 } 75 return g_program_filespec; 76 }