• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cinttypes>
20 #include <type_traits>
21 #include <utility>
22 
23 #include "src/trace_processor/storage/stats.h"
24 
25 #include "src/trace_processor/importers/syscalls/syscalls_aarch32.h"
26 #include "src/trace_processor/importers/syscalls/syscalls_aarch64.h"
27 #include "src/trace_processor/importers/syscalls/syscalls_armeabi.h"
28 #include "src/trace_processor/importers/syscalls/syscalls_x86.h"
29 #include "src/trace_processor/importers/syscalls/syscalls_x86_64.h"
30 
31 namespace perfetto {
32 namespace trace_processor {
33 namespace {
34 
35 template <typename T>
GetSyscalls(const T &)36 constexpr size_t GetSyscalls(const T&) {
37   static_assert(std::extent<T>::value <= kMaxSyscalls,
38                 "kMaxSyscalls too small");
39   return std::extent<T>::value;
40 }
41 
42 }  // namespace
43 
44 // TODO(primiano): The current design is broken in case of 32-bit processes
45 // running on 64-bit kernel. At least on ARM, the syscal numbers don't match
46 // and we should use the kSyscalls_Aarch32 table for those processes. But this
47 // means that the architecture is not a global property but is per-process.
48 // Which in turn means that somehow we need to figure out what is the bitness
49 // of each process from the trace.
SyscallTracker(TraceProcessorContext * context)50 SyscallTracker::SyscallTracker(TraceProcessorContext* context)
51     : context_(context) {
52   SetArchitecture(kUnknown);
53 }
54 
55 SyscallTracker::~SyscallTracker() = default;
56 
SetArchitecture(Architecture arch)57 void SyscallTracker::SetArchitecture(Architecture arch) {
58   const char* kSyscalls_Unknown[] = {nullptr};
59   size_t num_syscalls = 0;
60   const char* const* syscall_table = nullptr;
61 
62   switch (arch) {
63     case kArmEabi:
64       num_syscalls = GetSyscalls(kSyscalls_ArmEabi);
65       syscall_table = &kSyscalls_ArmEabi[0];
66       break;
67     case kAarch32:
68       num_syscalls = GetSyscalls(kSyscalls_Aarch32);
69       syscall_table = &kSyscalls_Aarch32[0];
70       break;
71     case kAarch64:
72       num_syscalls = GetSyscalls(kSyscalls_Aarch64);
73       syscall_table = &kSyscalls_Aarch64[0];
74       break;
75     case kX86_64:
76       num_syscalls = GetSyscalls(kSyscalls_x86_64);
77       syscall_table = &kSyscalls_x86_64[0];
78       break;
79     case kX86:
80       num_syscalls = GetSyscalls(kSyscalls_x86);
81       syscall_table = &kSyscalls_x86[0];
82       break;
83     case kUnknown:
84       num_syscalls = 0;
85       syscall_table = &kSyscalls_Unknown[0];
86       break;
87   }
88 
89   for (size_t i = 0; i < kMaxSyscalls; i++) {
90     StringId id = kNullStringId;
91     if (i < num_syscalls && syscall_table[i] && *syscall_table[i]) {
92       const char* name = syscall_table[i];
93       id = context_->storage->InternString(name);
94       if (!strcmp(name, "sys_write"))
95         sys_write_string_id_ = id;
96     } else {
97       char unknown_str[64];
98       sprintf(unknown_str, "sys_%zu", i);
99       id = context_->storage->InternString(unknown_str);
100     }
101     arch_syscall_to_string_id_[i] = id;
102   }
103 }
104 
105 }  // namespace trace_processor
106 }  // namespace perfetto
107