xref: /openbmc/linux/tools/perf/trace/beauty/mmap.c (revision 547840bd)
1 // SPDX-License-Identifier: LGPL-2.1
2 #include <uapi/linux/mman.h>
3 #include <linux/log2.h>
4 
5 static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size,
6 					       struct syscall_arg *arg)
7 {
8 	const char *prot_prefix = "PROT_";
9 	int printed = 0, prot = arg->val;
10 	bool show_prefix = arg->show_string_prefix;
11 
12 	if (prot == PROT_NONE)
13 		return scnprintf(bf, size, "%sNONE", show_prefix ? prot_prefix : "");
14 #define	P_MMAP_PROT(n) \
15 	if (prot & PROT_##n) { \
16 		printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prot_prefix :"", #n); \
17 		prot &= ~PROT_##n; \
18 	}
19 
20 	P_MMAP_PROT(READ);
21 	P_MMAP_PROT(WRITE);
22 	P_MMAP_PROT(EXEC);
23 	P_MMAP_PROT(SEM);
24 	P_MMAP_PROT(GROWSDOWN);
25 	P_MMAP_PROT(GROWSUP);
26 #undef P_MMAP_PROT
27 
28 	if (prot)
29 		printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
30 
31 	return printed;
32 }
33 
34 #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
35 
36 #include "trace/beauty/generated/mmap_flags_array.c"
37        static DEFINE_STRARRAY(mmap_flags, "MAP_");
38 
39 static size_t mmap__scnprintf_flags(unsigned long flags, char *bf, size_t size, bool show_prefix)
40 {
41        return strarray__scnprintf_flags(&strarray__mmap_flags, bf, size, show_prefix, flags);
42 }
43 
44 static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size,
45 						struct syscall_arg *arg)
46 {
47 	unsigned long flags = arg->val;
48 
49 	if (flags & MAP_ANONYMOUS)
50 		arg->mask |= (1 << 4) | (1 << 5); /* Mask 4th ('fd') and 5th ('offset') args, ignored */
51 
52 	return mmap__scnprintf_flags(flags, bf, size, arg->show_string_prefix);
53 }
54 
55 #define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
56 
57 static size_t syscall_arg__scnprintf_mremap_flags(char *bf, size_t size,
58 						  struct syscall_arg *arg)
59 {
60 	const char *flags_prefix = "MREMAP_";
61 	bool show_prefix = arg->show_string_prefix;
62 	int printed = 0, flags = arg->val;
63 
64 #define P_MREMAP_FLAG(n) \
65 	if (flags & MREMAP_##n) { \
66 		printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? flags_prefix : "", #n); \
67 		flags &= ~MREMAP_##n; \
68 	}
69 
70 	P_MREMAP_FLAG(MAYMOVE);
71 	P_MREMAP_FLAG(FIXED);
72 	P_MREMAP_FLAG(DONTUNMAP);
73 #undef P_MREMAP_FLAG
74 
75 	if (flags)
76 		printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
77 
78 	return printed;
79 }
80 
81 #define SCA_MREMAP_FLAGS syscall_arg__scnprintf_mremap_flags
82 
83 static size_t madvise__scnprintf_behavior(int behavior, char *bf, size_t size)
84 {
85 #include "trace/beauty/generated/madvise_behavior_array.c"
86        static DEFINE_STRARRAY(madvise_advices, "MADV_");
87 
88        if (behavior < strarray__madvise_advices.nr_entries && strarray__madvise_advices.entries[behavior] != NULL)
89                return scnprintf(bf, size, "MADV_%s", strarray__madvise_advices.entries[behavior]);
90 
91        return scnprintf(bf, size, "%#", behavior);
92 }
93 
94 static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size,
95 						      struct syscall_arg *arg)
96 {
97 	return madvise__scnprintf_behavior(arg->val, bf, size);
98 }
99 
100 #define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior
101