xref: /aosp_15_r20/external/strace/futex.c (revision cf84ac9a129d8ea9952db616b4e9b904c4bdde56)
1 /*
2  * Copyright (c) 2002-2003 Roland McGrath  <[email protected]>
3  * Copyright (c) 2007-2008 Ulrich Drepper <[email protected]>
4  * Copyright (c) 2009 Andreas Schwab <[email protected]>
5  * Copyright (c) 2014-2015 Dmitry V. Levin <[email protected]>
6  * Copyright (c) 2014-2018 The strace developers.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "defs.h"
33 
34 #ifndef FUTEX_PRIVATE_FLAG
35 # define FUTEX_PRIVATE_FLAG 128
36 #endif
37 #ifndef FUTEX_CLOCK_REALTIME
38 # define FUTEX_CLOCK_REALTIME 256
39 #endif
40 #ifndef FUTEX_OP_OPARG_SHIFT
41 # define FUTEX_OP_OPARG_SHIFT 8
42 #endif
43 
44 #include "xlat/futexbitset.h"
45 #include "xlat/futexops.h"
46 #include "xlat/futexwakeops.h"
47 #include "xlat/futexwakecmps.h"
48 
SYS_FUNC(futex)49 SYS_FUNC(futex)
50 {
51 	const kernel_ulong_t uaddr = tcp->u_arg[0];
52 	const int op = tcp->u_arg[1];
53 	const int cmd = op & 127;
54 	const kernel_ulong_t timeout = tcp->u_arg[3];
55 	const kernel_ulong_t uaddr2 = tcp->u_arg[4];
56 	const unsigned int val = tcp->u_arg[2];
57 	const unsigned int val2 = tcp->u_arg[3];
58 	const unsigned int val3 = tcp->u_arg[5];
59 	const char *comment;
60 
61 	printaddr(uaddr);
62 	tprints(", ");
63 	printxval(futexops, op, "FUTEX_???");
64 	switch (cmd) {
65 	case FUTEX_WAIT:
66 		tprintf(", %u", val);
67 		tprints(", ");
68 		print_timespec(tcp, timeout);
69 		break;
70 	case FUTEX_LOCK_PI:
71 		tprints(", ");
72 		print_timespec(tcp, timeout);
73 		break;
74 	case FUTEX_WAIT_BITSET:
75 		tprintf(", %u", val);
76 		tprints(", ");
77 		print_timespec(tcp, timeout);
78 		tprints(", ");
79 		printxval(futexbitset, val3, NULL);
80 		break;
81 	case FUTEX_WAKE_BITSET:
82 		tprintf(", %u", val);
83 		tprints(", ");
84 		printxval(futexbitset, val3, NULL);
85 		break;
86 	case FUTEX_REQUEUE:
87 		tprintf(", %u", val);
88 		tprintf(", %u, ", val2);
89 		printaddr(uaddr2);
90 		break;
91 	case FUTEX_CMP_REQUEUE:
92 	case FUTEX_CMP_REQUEUE_PI:
93 		tprintf(", %u", val);
94 		tprintf(", %u, ", val2);
95 		printaddr(uaddr2);
96 		tprintf(", %u", val3);
97 		break;
98 	case FUTEX_WAKE_OP:
99 		tprintf(", %u", val);
100 		tprintf(", %u, ", val2);
101 		printaddr(uaddr2);
102 		tprints(", ");
103 		if ((val3 >> 28) & FUTEX_OP_OPARG_SHIFT) {
104 			print_xlat(FUTEX_OP_OPARG_SHIFT);
105 			tprints("<<28|");
106 		}
107 		comment = printxval(futexwakeops, (val3 >> 28) & 0x7, NULL)
108 			? NULL : "FUTEX_OP_???";
109 		tprints("<<28");
110 		tprints_comment(comment);
111 		tprintf("|%#x<<12|", (val3 >> 12) & 0xfff);
112 		comment = printxval(futexwakecmps, (val3 >> 24) & 0xf, NULL)
113 			? NULL : "FUTEX_OP_CMP_???";
114 		tprints("<<24");
115 		tprints_comment(comment);
116 		tprintf("|%#x", val3 & 0xfff);
117 		break;
118 	case FUTEX_WAIT_REQUEUE_PI:
119 		tprintf(", %u", val);
120 		tprints(", ");
121 		print_timespec(tcp, timeout);
122 		tprints(", ");
123 		printaddr(uaddr2);
124 		break;
125 	case FUTEX_FD:
126 	case FUTEX_WAKE:
127 		tprintf(", %u", val);
128 		break;
129 	case FUTEX_UNLOCK_PI:
130 	case FUTEX_TRYLOCK_PI:
131 		break;
132 	default:
133 		tprintf(", %u", val);
134 		tprints(", ");
135 		printaddr(timeout);
136 		tprints(", ");
137 		printaddr(uaddr2);
138 		tprintf(", %#x", val3);
139 		break;
140 	}
141 
142 	return RVAL_DECODED;
143 }
144