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 #ifndef SRC_TRACE_PROCESSOR_SYSCALL_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_SYSCALL_TRACKER_H_
19 
20 #include <limits>
21 #include <tuple>
22 
23 #include "perfetto/base/string_view.h"
24 #include "src/trace_processor/slice_tracker.h"
25 #include "src/trace_processor/trace_processor_context.h"
26 #include "src/trace_processor/trace_storage.h"
27 
28 namespace perfetto {
29 namespace trace_processor {
30 
31 static constexpr size_t kMaxSyscalls = 550;
32 
33 enum Architecture {
34   kUnknown = 0,
35   kArmEabi,  // 32-bit kernel running a 32-bit process (most old devices).
36   kAarch32,  // 64-bit kernel running a 32-bit process (should be rare).
37   kAarch64,  // 64-bit kernel running a 64-bit process (most new devices).
38   kX86_64,
39 };
40 
41 class SyscallTracker {
42  public:
43   explicit SyscallTracker(TraceProcessorContext*);
44   SyscallTracker(const SyscallTracker&) = delete;
45   SyscallTracker& operator=(const SyscallTracker&) = delete;
46   virtual ~SyscallTracker();
47 
48   void SetArchitecture(Architecture architecture);
49 
Enter(int64_t ts,UniqueTid utid,uint32_t syscall_num)50   void Enter(int64_t ts, UniqueTid utid, uint32_t syscall_num) {
51     StringId name = SyscallNumberToStringId(syscall_num);
52     if (name)
53       context_->slice_tracker->Begin(ts, utid, 0 /* cat */, name);
54   }
55 
Exit(int64_t ts,UniqueTid utid,uint32_t syscall_num)56   void Exit(int64_t ts, UniqueTid utid, uint32_t syscall_num) {
57     StringId name = SyscallNumberToStringId(syscall_num);
58     if (name)
59       context_->slice_tracker->End(ts, utid, 0 /* cat */, name);
60   }
61 
62  private:
63   TraceProcessorContext* const context_;
64 
SyscallNumberToStringId(uint32_t syscall_num)65   inline StringId SyscallNumberToStringId(uint32_t syscall_num) {
66     if (syscall_num > kMaxSyscalls)
67       return 0;
68     // We see two write sys calls around each userspace slice that is going via
69     // trace_marker, this violates the assumption that userspace slices are
70     // perfectly nested. For the moment ignore all write sys calls.
71     // TODO(hjd): Remove this limitation.
72     StringId id = arch_syscall_to_string_id_[syscall_num];
73     if (id == sys_write_string_id_)
74       return 0;
75     return id;
76   }
77 
78   // This is table from platform specific syscall number directly to
79   // the relevent StringId (this avoids having to always do two conversions).
80   std::array<StringId, kMaxSyscalls> arch_syscall_to_string_id_{};
81   StringId sys_write_string_id_ = std::numeric_limits<StringId>::max();
82 };
83 
84 }  // namespace trace_processor
85 }  // namespace perfetto
86 
87 #endif  // SRC_TRACE_PROCESSOR_SYSCALL_TRACKER_H_
88