]> granicus.if.org Git - strace/blob - affinity.c
evdev: decode struct input_absinfo regardless of in-kernel definitions
[strace] / affinity.c
1 /*
2  * Copyright (c) 2002-2004 Roland McGrath <roland@redhat.com>
3  * Copyright (c) 2009-2018 Dmitry V. Levin <ldv@altlinux.org>
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: LGPL-2.1-or-later
7  */
8
9 #include "defs.h"
10 #include <sched.h>
11
12 static unsigned int
13 get_cpuset_size(void)
14 {
15         static unsigned int cpuset_size;
16
17         if (!cpuset_size) {
18                 /*
19                  * If the cpuset size passed to sched_getaffinity is less
20                  * than necessary to store the bitmask, the kernel does not
21                  * look at the mask pointer and fails with EINVAL.
22                  *
23                  * If the cpuset size is large enough, the kernel fails with
24                  * EFAULT on inaccessible mask pointers.
25                  *
26                  * This undocumented kernel feature can be used to probe
27                  * the kernel and find out the minimal valid cpuset size
28                  * without allocating any memory for the CPU affinity mask.
29                  */
30                 cpuset_size = 128;
31                 while (cpuset_size &&
32                        sched_getaffinity(0, cpuset_size, NULL) == -1 &&
33                        EINVAL == errno) {
34                         cpuset_size <<= 1;
35                 }
36                 if (!cpuset_size)
37                         cpuset_size = 128;
38         }
39
40         return cpuset_size;
41 }
42
43 static void
44 print_affinitylist(struct tcb *const tcp, const kernel_ulong_t addr,
45                    const unsigned int len)
46 {
47         const unsigned int max_size = get_cpuset_size();
48         const unsigned int umove_size = len < max_size ? len : max_size;
49         const unsigned int size =
50                 (umove_size + current_wordsize - 1) & -current_wordsize;
51         const unsigned int ncpu = size * 8;
52         void *cpu;
53
54         if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
55             !addr || !len || !(cpu = calloc(size, 1))) {
56                 printaddr(addr);
57                 return;
58         }
59
60         if (!umoven_or_printaddr(tcp, addr, umove_size, cpu)) {
61                 int i = 0;
62                 const char *sep = "";
63
64                 tprints("[");
65                 for (;; i++) {
66                         i = next_set_bit(cpu, i, ncpu);
67                         if (i < 0)
68                                 break;
69                         tprintf("%s%d", sep, i);
70                         sep = ", ";
71                 }
72                 if (size < len)
73                         tprintf("%s...", sep);
74                 tprints("]");
75         }
76
77         free(cpu);
78 }
79
80 SYS_FUNC(sched_setaffinity)
81 {
82         const int pid = tcp->u_arg[0];
83         const unsigned int len = tcp->u_arg[1];
84
85         tprintf("%d, %u, ", pid, len);
86         print_affinitylist(tcp, tcp->u_arg[2], len);
87
88         return RVAL_DECODED;
89 }
90
91 SYS_FUNC(sched_getaffinity)
92 {
93         const int pid = tcp->u_arg[0];
94         const unsigned int len = tcp->u_arg[1];
95
96         if (entering(tcp)) {
97                 tprintf("%d, %u, ", pid, len);
98         } else {
99                 print_affinitylist(tcp, tcp->u_arg[2], tcp->u_rval);
100         }
101         return 0;
102 }