xref: /linux/tools/perf/trace/beauty/socket_type.c (revision b2128290c29902315e632ea59e0504d6bc9e9b42)
1 // SPDX-License-Identifier: LGPL-2.1
2 #include "trace/beauty/beauty.h"
3 
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 
7 #ifndef SOCK_DCCP
8 # define SOCK_DCCP		6
9 #endif
10 
11 #ifndef SOCK_CLOEXEC
12 # define SOCK_CLOEXEC		02000000
13 #endif
14 
15 #ifndef SOCK_NONBLOCK
16 # define SOCK_NONBLOCK		00004000
17 #endif
18 
19 #ifndef SOCK_TYPE_MASK
20 #define SOCK_TYPE_MASK 0xf
21 #endif
22 
23 size_t syscall_arg__scnprintf_socket_type(char *bf, size_t size, struct syscall_arg *arg)
24 {
25 	bool show_prefix = arg->show_string_prefix;
26 	const char *prefix = "SOCK_";
27 	size_t printed;
28 	int type = arg->val,
29 	    flags = type & ~SOCK_TYPE_MASK;
30 
31 	type &= SOCK_TYPE_MASK;
32 	/*
33 	 * Can't use a strarray, MIPS may override for ABI reasons.
34 	 */
35 	switch (type) {
36 #define	P_SK_TYPE(n) case SOCK_##n: printed = scnprintf(bf, size, "%s%s", show_prefix ? prefix : "", #n); break;
37 	P_SK_TYPE(STREAM);
38 	P_SK_TYPE(DGRAM);
39 	P_SK_TYPE(RAW);
40 	P_SK_TYPE(RDM);
41 	P_SK_TYPE(SEQPACKET);
42 	P_SK_TYPE(DCCP);
43 	P_SK_TYPE(PACKET);
44 #undef P_SK_TYPE
45 	default:
46 		printed = scnprintf(bf, size, "%#x", type);
47 	}
48 
49 #define	P_SK_FLAG(n) \
50 	if (flags & SOCK_##n) { \
51 		printed += scnprintf(bf + printed, size - printed, "|%s", #n); \
52 		flags &= ~SOCK_##n; \
53 	}
54 
55 	P_SK_FLAG(CLOEXEC);
56 	P_SK_FLAG(NONBLOCK);
57 #undef P_SK_FLAG
58 
59 	if (flags)
60 		printed += scnprintf(bf + printed, size - printed, "|%#x", flags);
61 
62 	return printed;
63 }
64