| 1 |
//===-- HostTest.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/Host.h" |
| 10 |
#include "lldb/Host/FileSystem.h" |
| 11 |
#include "lldb/Host/HostInfo.h" |
| 12 |
#include "lldb/Utility/ProcessInfo.h" |
| 13 |
#include "gtest/gtest.h" |
| 14 |
|
| 15 |
#include <cerrno> |
| 16 |
#include <sys/resource.h> |
| 17 |
|
| 18 |
using namespace lldb_private; |
| 19 |
|
| 20 |
namespace { |
| 21 |
class HostTest : public testing::Test { |
| 22 |
public: |
| 23 |
static void SetUpTestCase() { |
| 24 |
FileSystem::Initialize(); |
| 25 |
HostInfo::Initialize(); |
| 26 |
} |
| 27 |
static void TearDownTestCase() { |
| 28 |
HostInfo::Terminate(); |
| 29 |
FileSystem::Terminate(); |
| 30 |
} |
| 31 |
}; |
| 32 |
} // namespace |
| 33 |
|
| 34 |
TEST_F(HostTest, GetProcessInfo) { |
| 35 |
ProcessInstanceInfo Info; |
| 36 |
|
| 37 |
ASSERT_FALSE(Host::GetProcessInfo(LLDB_INVALID_PROCESS_ID, Info)); |
| 38 |
|
| 39 |
ASSERT_TRUE(Host::GetProcessInfo(getpid(), Info)); |
| 40 |
|
| 41 |
EXPECT_TRUE(Info.ProcessIDIsValid()); |
| 42 |
EXPECT_EQ(lldb::pid_t(getpid()), Info.GetProcessID()); |
| 43 |
|
| 44 |
EXPECT_TRUE(Info.ParentProcessIDIsValid()); |
| 45 |
EXPECT_EQ(lldb::pid_t(getppid()), Info.GetParentProcessID()); |
| 46 |
|
| 47 |
// Not currently set on apple systems. |
| 48 |
#ifndef __APPLE__ |
| 49 |
EXPECT_TRUE(Info.ProcessGroupIDIsValid()); |
| 50 |
EXPECT_EQ(lldb::pid_t(getpgrp()), Info.GetProcessGroupID()); |
| 51 |
|
| 52 |
EXPECT_TRUE(Info.ProcessSessionIDIsValid()); |
| 53 |
EXPECT_EQ(lldb::pid_t(getsid(getpid())), Info.GetProcessSessionID()); |
| 54 |
#endif |
| 55 |
|
| 56 |
EXPECT_TRUE(Info.EffectiveUserIDIsValid()); |
| 57 |
EXPECT_EQ(geteuid(), Info.GetEffectiveUserID()); |
| 58 |
|
| 59 |
EXPECT_TRUE(Info.EffectiveGroupIDIsValid()); |
| 60 |
EXPECT_EQ(getegid(), Info.GetEffectiveGroupID()); |
| 61 |
|
| 62 |
EXPECT_TRUE(Info.UserIDIsValid()); |
| 63 |
EXPECT_EQ(geteuid(), Info.GetUserID()); |
| 64 |
|
| 65 |
EXPECT_TRUE(Info.GroupIDIsValid()); |
| 66 |
EXPECT_EQ(getegid(), Info.GetGroupID()); |
| 67 |
|
| 68 |
// Unexpected value on Apple x86_64 |
| 69 |
#ifndef __APPLE__ |
| 70 |
EXPECT_TRUE(Info.GetArchitecture().IsValid()); |
| 71 |
EXPECT_EQ(HostInfo::GetArchitecture(HostInfo::eArchKindDefault), |
| 72 |
Info.GetArchitecture()); |
| 73 |
#endif |
| 74 |
|
| 75 |
// Test timings |
| 76 |
// In some sense this is a pretty trivial test. What it is trying to |
| 77 |
// accomplish is just to validate that these values are never decreasing |
| 78 |
// which would be unambiguously wrong. We can not reliably show them |
| 79 |
// to be always increasing because the microsecond granularity means that, |
| 80 |
// with hardware variations the number of loop iterations need to always |
| 81 |
// be increasing for faster and faster machines. |
| 82 |
ASSERT_TRUE(Host::GetProcessInfo(getpid(), Info)); |
| 83 |
ProcessInstanceInfo::timespec user_time = Info.GetUserTime(); |
| 84 |
static volatile unsigned u = 0; |
| 85 |
for (unsigned i = 0; i < 10'000'000; i++) { |
| 86 |
u += i; |
| 87 |
} |
| 88 |
ASSERT_TRUE(u > 0); |
| 89 |
ASSERT_TRUE(Host::GetProcessInfo(getpid(), Info)); |
| 90 |
ProcessInstanceInfo::timespec next_user_time = Info.GetUserTime(); |
| 91 |
ASSERT_TRUE(user_time.tv_sec <= next_user_time.tv_sec || |
| 92 |
user_time.tv_usec <= next_user_time.tv_usec); |
| 93 |
} |
| 94 |
|
| 95 |
// Only linux currently sets these. |
| 96 |
#ifdef __linux__ |
| 97 |
TEST_F(HostTest, GetProcessInfoSetsPriority) { |
| 98 |
ProcessInstanceInfo Info; |
| 99 |
struct rlimit rlim; |
| 100 |
EXPECT_EQ(getrlimit(RLIMIT_NICE, &rlim), 0); |
| 101 |
// getpriority can return -1 so we zero errno first |
| 102 |
errno = 0; |
| 103 |
int prio = getpriority(PRIO_PROCESS, 0); |
| 104 |
ASSERT_TRUE((prio < 0 && errno == 0) || prio >= 0); |
| 105 |
ASSERT_TRUE(Host::GetProcessInfo(getpid(), Info)); |
| 106 |
ASSERT_EQ(Info.GetPriorityValue(), prio); |
| 107 |
// If we can't raise our nice level then this test can't be performed. |
| 108 |
int max_incr = PRIO_MAX - rlim.rlim_cur; |
| 109 |
if (max_incr < prio) { |
| 110 |
EXPECT_EQ(setpriority(PRIO_PROCESS, PRIO_PROCESS, prio - 1), 0); |
| 111 |
ASSERT_TRUE(Host::GetProcessInfo(getpid(), Info)); |
| 112 |
ASSERT_TRUE(Info.GetPriorityValue().has_value()); |
| 113 |
ASSERT_EQ(Info.GetPriorityValue().value(), prio - 1); |
| 114 |
EXPECT_EQ(setpriority(PRIO_PROCESS, PRIO_PROCESS, prio), 0); |
| 115 |
} |
| 116 |
ASSERT_TRUE(Info.IsZombie().has_value()); |
| 117 |
ASSERT_FALSE(Info.IsZombie().value()); |
| 118 |
|
| 119 |
ASSERT_TRUE(Info.IsCoreDumping().has_value()); |
| 120 |
ASSERT_FALSE(Info.IsCoreDumping().value()); |
| 121 |
} |
| 122 |
#endif |