Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf syscalltbl: Use lookup table containing multiple architectures

Switch to use the lookup table containing all architectures rather
than tables matching the perf binary.

This fixes perf trace when executed on a 32-bit i386 binary on an
x86-64 machine. Note in the following the system call names of the
32-bit i386 binary as seen by an x86-64 perf.

Before:
```
? ( ): a.out/447296 ... [continued]: munmap()) = 0
0.024 ( 0.001 ms): a.out/447296 recvfrom(ubuf: 0x2, size: 4160585708, flags: DONTROUTE|CTRUNC|TRUNC|DONTWAIT|EOR|WAITALL|FIN|SYN|CONFIRM|RST|ERRQUEUE|NOSIGNAL|WAITFORONE|BATCH|SOCK_DEVMEM|ZEROCOPY|FASTOPEN|CMSG_CLOEXEC|0x91f80000, addr: 0xe30, addr_len: 0xffce438c) = 1475198976
0.042 ( 0.003 ms): a.out/447296 lgetxattr(name: "", value: 0x3, size: 34) = 4160344064
0.054 ( 0.003 ms): a.out/447296 dup2(oldfd: -134422744, newfd: 4) = -1 ENOENT (No such file or directory)
0.060 ( 0.009 ms): a.out/447296 preadv(fd: 4294967196, vec: (struct iovec){.iov_base = (void *)0x2e646c2f6374652f,.iov_len = (__kernel_size_t)7307199665335594867,}, vlen: 557056, pos_h: 4160585708) = 3
0.074 ( 0.004 ms): a.out/447296 lgetxattr(name: "", value: 0x1, size: 2) = 4160237568
0.080 ( 0.001 ms): a.out/447296 lstat(filename: "", statbuf: 0x193f6) = 0
0.089 ( 0.007 ms): a.out/447296 preadv(fd: 4294967196, vec: (struct iovec){.iov_base = (void *)0x3833692f62696c2f,.iov_len = (__kernel_size_t)3276497845987585334,}, vlen: 557056, pos_h: 4160585708) = 3
0.097 ( 0.002 ms): a.out/447296 close(fd: 3</proc/447296/status>) = 512
0.103 ( 0.002 ms): a.out/447296 lgetxattr(name: "", value: 0x1, size: 2050) = 4157935616
0.107 ( 0.007 ms): a.out/447296 lgetxattr(pathname: "", name: "", value: 0x5, size: 2066) = 4158078976
0.116 ( 0.003 ms): a.out/447296 lgetxattr(pathname: "", name: "", value: 0x1, size: 2066) = 4159639552
0.121 ( 0.003 ms): a.out/447296 lgetxattr(pathname: "", name: "", value: 0x3, size: 2066) = 4160184320
0.129 ( 0.002 ms): a.out/447296 lgetxattr(pathname: "", name: "", value: 0x3, size: 50) = 4160196608
0.138 ( 0.001 ms): a.out/447296 lstat(filename: "") = 0
0.145 ( 0.002 ms): a.out/447296 mq_timedreceive(mqdes: 4291706800, u_msg_ptr: 0xf7f9ea48, msg_len: 134616640, u_msg_prio: 0xf7fd7fec, u_abs_timeout: (struct __kernel_timespec){.tv_sec = (__kernel_time64_t)-578174027777317696,.tv_nsec = (long long int)4160349376,}) = 0
0.148 ( 0.001 ms): a.out/447296 mkdirat(dfd: -134617816, pathname: " ��� ���▒���▒���", mode: IFREG|ISUID|IRUSR|IWGRP|0xf7fd0000) = 447296
0.150 ( 0.001 ms): a.out/447296 process_vm_writev(pid: -134617812, lvec: (struct iovec){.iov_base = (void *)0xf7f9e9c8f7f9e4c0,.iov_len = (__kernel_size_t)4160349376,}, liovcnt: 4160588048, rvec: (struct iovec){}, riovcnt: 4160585708, flags: 4291707352) = 0
0.197 ( 0.004 ms): a.out/447296 capget(header: 4160184320, dataptr: 8192) = 0
0.202 ( 0.002 ms): a.out/447296 capget(header: 1448669184, dataptr: 4096) = 0
0.208 ( 0.002 ms): a.out/447296 capget(header: 4160577536, dataptr: 8192) = 0
0.220 ( 0.001 ms): a.out/447296 getxattr(pathname: "", name: "c������", value: 0xf7f77e34, size: 1) = 0
0.228 ( 0.005 ms): a.out/447296 fchmod(fd: -134729728, mode: IRUGO|IWUGO|IFREG|IFIFO|ISVTX|IXUSR|0x10000) = 0
0.240 ( 0.009 ms): a.out/447296 preadv(fd: 4294967196, vec: 0x5658e008, pos_h: 4160192052) = 3
0.250 ( 0.008 ms): a.out/447296 close(fd: 3</proc/447296/status>) = 1436
0.260 ( 0.018 ms): a.out/447296 stat(filename: "", statbuf: 0xffce32ac) = 1436
0.288 (1000.213 ms): a.out/447296 readlinkat(buf: 0xffce31d4, bufsiz: 4291703244) = 0
```

After:
```
? ( ): a.out/442930 ... [continued]: execve()) = 0
0.023 ( 0.002 ms): a.out/442930 brk() = 0x57760000
0.052 ( 0.003 ms): a.out/442930 access(filename: 0xf7f5af28, mode: R) = -1 ENOENT (No such file or directory)
0.059 ( 0.009 ms): a.out/442930 openat(dfd: CWD, filename: "/etc/ld.so.cache", flags: RDONLY|CLOEXEC|LARGEFILE) = 3
0.078 ( 0.001 ms): a.out/442930 close(fd: 3</proc/442930/status>) = 0
0.087 ( 0.007 ms): a.out/442930 openat(dfd: CWD, filename: "/lib/i386-linux-", flags: RDONLY|CLOEXEC|LARGEFILE) = 3
0.095 ( 0.002 ms): a.out/442930 read(fd: 3</proc/442930/status>, buf: 0xffbdbb70, count: 512) = 512
0.135 ( 0.001 ms): a.out/442930 close(fd: 3</proc/442930/status>) = 0
0.148 ( 0.001 ms): a.out/442930 set_tid_address(tidptr: 0xf7f2b528) = 442930 (a.out)
0.150 ( 0.001 ms): a.out/442930 set_robust_list(head: 0xf7f2b52c, len: 12) =
0.196 ( 0.004 ms): a.out/442930 mprotect(start: 0xf7f03000, len: 8192, prot: READ) = 0
0.202 ( 0.002 ms): a.out/442930 mprotect(start: 0x5658e000, len: 4096, prot: READ) = 0
0.207 ( 0.002 ms): a.out/442930 mprotect(start: 0xf7f63000, len: 8192, prot: READ) = 0
0.230 ( 0.005 ms): a.out/442930 munmap(addr: 0xf7f10000, len: 103414) = 0
0.244 ( 0.010 ms): a.out/442930 openat(dfd: CWD, filename: 0x5658d008) = 3
0.255 ( 0.007 ms): a.out/442930 read(fd: 3</proc/442930/status>, buf: 0xffbdb67c, count: 4096) = 1436
0.264 ( 0.018 ms): a.out/442930 write(fd: 1</dev/pts/4>, buf: , count: 1436) = 1436
0.292 (1000.173 ms): a.out/442930 clock_nanosleep(rqtp: { .tv_sec: 17866546940376776704, .tv_nsec: 4159878336 }, rmtp: 0xffbdb59c) = 0
1000.478 ( ): a.out/442930 exit_group() = ?
```

Signed-off-by: Ian Rogers <irogers@google.com>
Reviewed-by: Howard Chu <howardchu95@gmail.com>
Reviewed-by: Charlie Jenkins <charlie@rivosinc.com>
Reviewed-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Arnaldo Carvalho de Melo <acme@kernel.org>
Link: https://lore.kernel.org/r/20250319050741.269828-10-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Ian Rogers and committed by
Namhyung Kim
1470eaa5 0fb641f0

+64 -25
+64 -25
tools/perf/util/syscalltbl.c
··· 15 15 #include <string.h> 16 16 #include "string2.h" 17 17 18 - #if __BITS_PER_LONG == 64 19 - #include <asm/syscalls_64.h> 20 - #else 21 - #include <asm/syscalls_32.h> 22 - #endif 18 + #include "trace/beauty/generated/syscalltbl.c" 23 19 24 - const char *syscalltbl__name(int e_machine __maybe_unused, int id) 20 + static const struct syscalltbl *find_table(int e_machine) 25 21 { 26 - if (id >= 0 && id <= (int)ARRAY_SIZE(syscall_num_to_name)) 27 - return syscall_num_to_name[id]; 22 + static const struct syscalltbl *last_table; 23 + static int last_table_machine = EM_NONE; 24 + 25 + /* Tables only exist for EM_SPARC. */ 26 + if (e_machine == EM_SPARCV9) 27 + e_machine = EM_SPARC; 28 + 29 + if (last_table_machine == e_machine && last_table != NULL) 30 + return last_table; 31 + 32 + for (size_t i = 0; i < ARRAY_SIZE(syscalltbls); i++) { 33 + const struct syscalltbl *entry = &syscalltbls[i]; 34 + 35 + if (entry->e_machine != e_machine && entry->e_machine != EM_NONE) 36 + continue; 37 + 38 + last_table = entry; 39 + last_table_machine = e_machine; 40 + return entry; 41 + } 42 + return NULL; 43 + } 44 + 45 + const char *syscalltbl__name(int e_machine, int id) 46 + { 47 + const struct syscalltbl *table = find_table(e_machine); 48 + 49 + if (table && id >= 0 && id < table->num_to_name_len) 50 + return table->num_to_name[id]; 28 51 return NULL; 29 52 } 30 53 ··· 64 41 return strcmp(key->name, key->tbl[*entry]); 65 42 } 66 43 67 - int syscalltbl__id(int e_machine __maybe_unused, const char *name) 44 + int syscalltbl__id(int e_machine, const char *name) 68 45 { 69 - struct syscall_cmp_key key = { 70 - .name = name, 71 - .tbl = syscall_num_to_name, 72 - }; 73 - const int *id = bsearch(&key, syscall_sorted_names, 74 - ARRAY_SIZE(syscall_sorted_names), 75 - sizeof(syscall_sorted_names[0]), 76 - syscallcmpname); 46 + const struct syscalltbl *table = find_table(e_machine); 47 + struct syscall_cmp_key key; 48 + const uint16_t *id; 49 + 50 + if (!table) 51 + return -1; 52 + 53 + key.name = name; 54 + key.tbl = table->num_to_name; 55 + id = bsearch(&key, table->sorted_names, table->sorted_names_len, 56 + sizeof(table->sorted_names[0]), syscallcmpname); 77 57 78 58 return id ? *id : -1; 79 59 } 80 60 81 - int syscalltbl__num_idx(int e_machine __maybe_unused) 61 + int syscalltbl__num_idx(int e_machine) 82 62 { 83 - return ARRAY_SIZE(syscall_sorted_names); 63 + const struct syscalltbl *table = find_table(e_machine); 64 + 65 + if (!table) 66 + return 0; 67 + 68 + return table->sorted_names_len; 84 69 } 85 70 86 - int syscalltbl__id_at_idx(int e_machine __maybe_unused, int idx) 71 + int syscalltbl__id_at_idx(int e_machine, int idx) 87 72 { 88 - return syscall_sorted_names[idx]; 73 + const struct syscalltbl *table = find_table(e_machine); 74 + 75 + if (!table) 76 + return -1; 77 + 78 + assert(idx >= 0 && idx < table->sorted_names_len); 79 + return table->sorted_names[idx]; 89 80 } 90 81 91 - int syscalltbl__strglobmatch_next(int e_machine __maybe_unused, const char *syscall_glob, int *idx) 82 + int syscalltbl__strglobmatch_next(int e_machine, const char *syscall_glob, int *idx) 92 83 { 93 - for (int i = *idx + 1; i < (int)ARRAY_SIZE(syscall_sorted_names); ++i) { 94 - const char *name = syscall_num_to_name[syscall_sorted_names[i]]; 84 + const struct syscalltbl *table = find_table(e_machine); 85 + 86 + for (int i = *idx + 1; table && i < table->sorted_names_len; ++i) { 87 + const char *name = table->num_to_name[table->sorted_names[i]]; 95 88 96 89 if (strglobmatch(name, syscall_glob)) { 97 90 *idx = i; 98 - return syscall_sorted_names[i]; 91 + return table->sorted_names[i]; 99 92 } 100 93 } 101 94