1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_processor/importers/syscalls/syscall_tracker.h"
18
19 #include "src/trace_processor/importers/common/slice_tracker.h"
20 #include "test/gtest_and_gmock.h"
21
22 namespace perfetto {
23 namespace trace_processor {
24 namespace {
25
26 using ::testing::_;
27 using ::testing::DoAll;
28 using ::testing::Return;
29 using ::testing::SaveArg;
30
31 class MockSliceTracker : public SliceTracker {
32 public:
MockSliceTracker(TraceProcessorContext * context)33 MockSliceTracker(TraceProcessorContext* context) : SliceTracker(context) {}
34 virtual ~MockSliceTracker() = default;
35
36 MOCK_METHOD5(Begin,
37 base::Optional<SliceId>(int64_t timestamp,
38 TrackId track_id,
39 StringId cat,
40 StringId name,
41 SetArgsCallback args_callback));
42 MOCK_METHOD5(End,
43 base::Optional<SliceId>(int64_t timestamp,
44 TrackId track_id,
45 StringId cat,
46 StringId name,
47 SetArgsCallback args_callback));
48 };
49
50 class SyscallTrackerTest : public ::testing::Test {
51 public:
SyscallTrackerTest()52 SyscallTrackerTest() {
53 context.storage.reset(new TraceStorage());
54 track_tracker = new TrackTracker(&context);
55 context.track_tracker.reset(track_tracker);
56 slice_tracker = new MockSliceTracker(&context);
57 context.slice_tracker.reset(slice_tracker);
58 }
59
60 protected:
61 TraceProcessorContext context;
62 MockSliceTracker* slice_tracker;
63 TrackTracker* track_tracker;
64 };
65
TEST_F(SyscallTrackerTest,ReportUnknownSyscalls)66 TEST_F(SyscallTrackerTest, ReportUnknownSyscalls) {
67 constexpr TrackId track{0u};
68 StringId begin_name = kNullStringId;
69 StringId end_name = kNullStringId;
70 EXPECT_CALL(*slice_tracker, Begin(100, track, kNullStringId, _, _))
71 .WillOnce(DoAll(SaveArg<3>(&begin_name), Return(base::nullopt)));
72 EXPECT_CALL(*slice_tracker, End(110, track, kNullStringId, _, _))
73 .WillOnce(DoAll(SaveArg<3>(&end_name), Return(base::nullopt)));
74
75 SyscallTracker* syscall_tracker = SyscallTracker::GetOrCreate(&context);
76 syscall_tracker->Enter(100 /*ts*/, 42 /*utid*/, 57 /*sys_read*/);
77 syscall_tracker->Exit(110 /*ts*/, 42 /*utid*/, 57 /*sys_read*/);
78 EXPECT_EQ(context.storage->GetString(begin_name), "sys_57");
79 EXPECT_EQ(context.storage->GetString(end_name), "sys_57");
80 }
81
TEST_F(SyscallTrackerTest,IgnoreWriteSyscalls)82 TEST_F(SyscallTrackerTest, IgnoreWriteSyscalls) {
83 SyscallTracker* syscall_tracker = SyscallTracker::GetOrCreate(&context);
84 syscall_tracker->SetArchitecture(kAarch64);
85 EXPECT_CALL(*slice_tracker, Begin(_, _, _, _, _)).Times(0);
86 EXPECT_CALL(*slice_tracker, End(_, _, _, _, _)).Times(0);
87
88 syscall_tracker->Enter(100 /*ts*/, 42 /*utid*/, 64 /*sys_write*/);
89 syscall_tracker->Exit(110 /*ts*/, 42 /*utid*/, 64 /*sys_write*/);
90 }
91
TEST_F(SyscallTrackerTest,Aarch64)92 TEST_F(SyscallTrackerTest, Aarch64) {
93 constexpr TrackId track{0u};
94 StringId begin_name = kNullStringId;
95 StringId end_name = kNullStringId;
96 EXPECT_CALL(*slice_tracker, Begin(100, track, kNullStringId, _, _))
97 .WillOnce(DoAll(SaveArg<3>(&begin_name), Return(base::nullopt)));
98 EXPECT_CALL(*slice_tracker, End(110, track, kNullStringId, _, _))
99 .WillOnce(DoAll(SaveArg<3>(&end_name), Return(base::nullopt)));
100
101 SyscallTracker* syscall_tracker = SyscallTracker::GetOrCreate(&context);
102 syscall_tracker->SetArchitecture(kAarch64);
103 syscall_tracker->Enter(100 /*ts*/, 42 /*utid*/, 63 /*sys_read*/);
104 syscall_tracker->Exit(110 /*ts*/, 42 /*utid*/, 63 /*sys_read*/);
105 EXPECT_EQ(context.storage->GetString(begin_name), "sys_read");
106 EXPECT_EQ(context.storage->GetString(end_name), "sys_read");
107 }
108
TEST_F(SyscallTrackerTest,x8664)109 TEST_F(SyscallTrackerTest, x8664) {
110 constexpr TrackId track{0u};
111 StringId begin_name = kNullStringId;
112 StringId end_name = kNullStringId;
113 EXPECT_CALL(*slice_tracker, Begin(100, track, kNullStringId, _, _))
114 .WillOnce(DoAll(SaveArg<3>(&begin_name), Return(base::nullopt)));
115 EXPECT_CALL(*slice_tracker, End(110, track, kNullStringId, _, _))
116 .WillOnce(DoAll(SaveArg<3>(&end_name), Return(base::nullopt)));
117
118 SyscallTracker* syscall_tracker = SyscallTracker::GetOrCreate(&context);
119 syscall_tracker->SetArchitecture(kX86_64);
120 syscall_tracker->Enter(100 /*ts*/, 42 /*utid*/, 0 /*sys_read*/);
121 syscall_tracker->Exit(110 /*ts*/, 42 /*utid*/, 0 /*sys_read*/);
122 EXPECT_EQ(context.storage->GetString(begin_name), "sys_read");
123 EXPECT_EQ(context.storage->GetString(end_name), "sys_read");
124 }
125
TEST_F(SyscallTrackerTest,SyscallNumberTooLarge)126 TEST_F(SyscallTrackerTest, SyscallNumberTooLarge) {
127 EXPECT_CALL(*slice_tracker, Begin(_, _, _, _, _)).Times(0);
128 EXPECT_CALL(*slice_tracker, End(_, _, _, _, _)).Times(0);
129
130 SyscallTracker* syscall_tracker = SyscallTracker::GetOrCreate(&context);
131 syscall_tracker->SetArchitecture(kAarch64);
132 syscall_tracker->Enter(100 /*ts*/, 42 /*utid*/, 9999);
133 syscall_tracker->Exit(110 /*ts*/, 42 /*utid*/, 9999);
134 }
135
136 } // namespace
137 } // namespace trace_processor
138 } // namespace perfetto
139