xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/syscalls/syscall_tracker.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 "perfetto/ext/base/string_utils.h"
24 #include "src/kernel_utils/syscall_table.h"
25 #include "src/trace_processor/storage/stats.h"
26 
27 namespace perfetto::trace_processor {
28 
29 // TODO(primiano): The current design is broken in case of 32-bit processes
30 // running on 64-bit kernel. At least on ARM, the syscal numbers don't match
31 // and we should use the kSyscalls_Aarch32 table for those processes. But this
32 // means that the architecture is not a global property but is per-process.
33 // Which in turn means that somehow we need to figure out what is the bitness
34 // of each process from the trace.
SyscallTracker(TraceProcessorContext * context)35 SyscallTracker::SyscallTracker(TraceProcessorContext* context)
36     : context_(context) {
37   SetArchitecture(Architecture::kUnknown);
38 }
39 
40 SyscallTracker::~SyscallTracker() = default;
41 
SetArchitecture(Architecture arch)42 void SyscallTracker::SetArchitecture(Architecture arch) {
43   SyscallTable syscalls(arch);
44 
45   for (size_t i = 0; i < kMaxSyscalls; i++) {
46     StringId id = kNullStringId;
47     const char* name = syscalls.GetById(i);
48     if (name && *name) {
49       id = context_->storage->InternString(name);
50       if (!strcmp(name, "sys_write")) {
51         sys_write_string_id_ = id;
52       } else if (!strcmp(name, "sys_rt_sigreturn")) {
53         sys_rt_sigreturn_string_id_ = id;
54       }
55     } else {
56       base::StackString<64> unknown_str("sys_%zu", i);
57       id = context_->storage->InternString(unknown_str.string_view());
58     }
59     arch_syscall_to_string_id_[i] = id;
60   }
61 }
62 
63 }  // namespace perfetto::trace_processor
64