Home | History | Annotate | Download | only in trace_processor
      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/syscall_tracker.h"
     18 
     19 #include <type_traits>
     20 #include <utility>
     21 
     22 #include <inttypes.h>
     23 
     24 #include "src/trace_processor/stats.h"
     25 
     26 #include "src/trace_processor/syscalls_aarch32.h"
     27 #include "src/trace_processor/syscalls_aarch64.h"
     28 #include "src/trace_processor/syscalls_armeabi.h"
     29 #include "src/trace_processor/syscalls_x86_64.h"
     30 
     31 namespace perfetto {
     32 namespace trace_processor {
     33 namespace {
     34 
     35 template <typename 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.
     50 SyscallTracker::SyscallTracker(TraceProcessorContext* context)
     51     : context_(context) {
     52   SetArchitecture(kUnknown);
     53 }
     54 
     55 SyscallTracker::~SyscallTracker() = default;
     56 
     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 kUnknown:
     80       num_syscalls = 0;
     81       syscall_table = &kSyscalls_Unknown[0];
     82       break;
     83   }
     84 
     85   for (size_t i = 0; i < kMaxSyscalls; i++) {
     86     StringId id = 0;
     87     if (i < num_syscalls && syscall_table[i] && *syscall_table[i]) {
     88       const char* name = syscall_table[i];
     89       id = context_->storage->InternString(name);
     90       if (!strcmp(name, "sys_write"))
     91         sys_write_string_id_ = id;
     92     } else {
     93       char unknown_str[64];
     94       sprintf(unknown_str, "sys_%zu", i);
     95       id = context_->storage->InternString(unknown_str);
     96     }
     97     arch_syscall_to_string_id_[i] = id;
     98   }
     99 }
    100 
    101 }  // namespace trace_processor
    102 }  // namespace perfetto
    103