xref: /openbmc/linux/tools/perf/trace/beauty/clone.c (revision 81de3bf3)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  * trace/beauty/cone.c
4  *
5  *  Copyright (C) 2017, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
6  */
7 
8 #include "trace/beauty/beauty.h"
9 #include <linux/kernel.h>
10 #include <sys/types.h>
11 #include <uapi/linux/sched.h>
12 
13 static size_t clone__scnprintf_flags(unsigned long flags, char *bf, size_t size, bool show_prefix)
14 {
15 	const char *prefix = "CLONE_";
16 	int printed = 0;
17 
18 #define	P_FLAG(n) \
19 	if (flags & CLONE_##n) { \
20 		printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
21 		flags &= ~CLONE_##n; \
22 	}
23 
24 	P_FLAG(VM);
25 	P_FLAG(FS);
26 	P_FLAG(FILES);
27 	P_FLAG(SIGHAND);
28 	P_FLAG(PIDFD);
29 	P_FLAG(PTRACE);
30 	P_FLAG(VFORK);
31 	P_FLAG(PARENT);
32 	P_FLAG(THREAD);
33 	P_FLAG(NEWNS);
34 	P_FLAG(SYSVSEM);
35 	P_FLAG(SETTLS);
36 	P_FLAG(PARENT_SETTID);
37 	P_FLAG(CHILD_CLEARTID);
38 	P_FLAG(DETACHED);
39 	P_FLAG(UNTRACED);
40 	P_FLAG(CHILD_SETTID);
41 	P_FLAG(NEWCGROUP);
42 	P_FLAG(NEWUTS);
43 	P_FLAG(NEWIPC);
44 	P_FLAG(NEWUSER);
45 	P_FLAG(NEWPID);
46 	P_FLAG(NEWNET);
47 	P_FLAG(IO);
48 	P_FLAG(CLEAR_SIGHAND);
49 #undef P_FLAG
50 
51 	if (flags)
52 		printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
53 
54 	return printed;
55 }
56 
57 size_t syscall_arg__scnprintf_clone_flags(char *bf, size_t size, struct syscall_arg *arg)
58 {
59 	unsigned long flags = arg->val;
60 	enum syscall_clone_args {
61 		SCC_FLAGS	  = (1 << 0),
62 		SCC_CHILD_STACK	  = (1 << 1),
63 		SCC_PARENT_TIDPTR = (1 << 2),
64 		SCC_CHILD_TIDPTR  = (1 << 3),
65 		SCC_TLS		  = (1 << 4),
66 	};
67 	if (!(flags & CLONE_PARENT_SETTID))
68 		arg->mask |= SCC_PARENT_TIDPTR;
69 
70 	if (!(flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)))
71 		arg->mask |= SCC_CHILD_TIDPTR;
72 
73 	if (!(flags & CLONE_SETTLS))
74 		arg->mask |= SCC_TLS;
75 
76 	return clone__scnprintf_flags(flags, bf, size, arg->show_string_prefix);
77 }
78