Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 1 | //===-- 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 12 | #include <inttypes.h> |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 13 | #include <limits.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 14 | #include <pthread.h> |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <string.h> |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 17 | #include <sys/sysctl.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | #include <sys/types.h> |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 19 | #include <sys/utsname.h> |
| 20 | #include <unistd.h> |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace lldb_private; |
| 23 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | bool HostInfoNetBSD::GetOSVersion(uint32_t &major, uint32_t &minor, |
| 25 | uint32_t &update) { |
| 26 | struct utsname un; |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 27 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | ::memset(&un, 0, sizeof(un)); |
| 29 | if (::uname(&un) < 0) |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 30 | return false; |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 31 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | /* Accept versions like 7.99.21 and 6.1_STABLE */ |
| 33 | int status = ::sscanf(un.release, "%" PRIu32 ".%" PRIu32 ".%" PRIu32, &major, |
| 34 | &minor, &update); |
| 35 | switch (status) { |
| 36 | case 0: |
| 37 | return false; |
| 38 | case 1: |
| 39 | minor = 0; |
| 40 | /* FALLTHROUGH */ |
| 41 | case 2: |
| 42 | update = 0; |
| 43 | /* FALLTHROUGH */ |
| 44 | case 3: |
| 45 | default: |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 46 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | } |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | bool HostInfoNetBSD::GetOSBuildString(std::string &s) { |
| 51 | int mib[2] = {CTL_KERN, KERN_OSREV}; |
| 52 | char osrev_str[12]; |
| 53 | int osrev = 0; |
| 54 | size_t osrev_len = sizeof(osrev); |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0) { |
| 57 | ::snprintf(osrev_str, sizeof(osrev_str), "%-10.10d", osrev); |
| 58 | s.assign(osrev_str); |
| 59 | return true; |
| 60 | } |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 61 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | s.clear(); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | bool HostInfoNetBSD::GetOSKernelDescription(std::string &s) { |
| 67 | struct utsname un; |
| 68 | |
| 69 | ::memset(&un, 0, sizeof(un)); |
| 70 | s.clear(); |
| 71 | |
| 72 | if (::uname(&un) < 0) |
| 73 | return false; |
| 74 | |
| 75 | s.assign(un.version); |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | FileSpec HostInfoNetBSD::GetProgramFileSpec() { |
| 81 | static FileSpec g_program_filespec; |
| 82 | |
| 83 | if (!g_program_filespec) { |
Kamil Rytarowski | 3caaaa9 | 2017-01-28 20:04:53 +0000 | [diff] [blame] | 84 | static const int name[] = { |
| 85 | CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME, |
| 86 | }; |
| 87 | char path[MAXPATHLEN]; |
| 88 | size_t len; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | |
Kamil Rytarowski | 3caaaa9 | 2017-01-28 20:04:53 +0000 | [diff] [blame] | 90 | len = sizeof(path); |
| 91 | if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1) { |
Jonas Devlieghere | dd2f78e | 2018-06-13 22:23:48 +0000 | [diff] [blame] | 92 | g_program_filespec.SetFile(path, false, FileSpec::Style::native); |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 93 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | } |
| 95 | return g_program_filespec; |
Bruce Mitchener | 910af4d | 2015-10-13 05:04:13 +0000 | [diff] [blame] | 96 | } |