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