2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
9 * Copyright (c) 2001-2018 The strace developers.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 /* The libc headers do not define this constant since it should only be
40 used by the implementation. So we define it here. */
42 # ifdef ASM_SA_RESTORER
43 # define SA_RESTORER ASM_SA_RESTORER
48 * Some architectures define SA_RESTORER in their headers,
49 * but do not actually have sa_restorer.
51 * Some architectures, otherwise, do not define SA_RESTORER in their headers,
52 * but actually have sa_restorer.
54 #ifdef HAVE_ARCH_SA_RESTORER
55 # define HAVE_SA_RESTORER HAVE_ARCH_SA_RESTORER
56 #else /* !HAVE_ARCH_SA_RESTORER */
58 # define HAVE_SA_RESTORER 1
60 # define HAVE_SA_RESTORER 0
62 #endif /* HAVE_ARCH_SA_RESTORER */
64 #include "xlat/sa_handler_values.h"
65 #include "xlat/sigact_flags.h"
66 #include "xlat/sigprocmaskcmds.h"
68 /* Anonymous realtime signals. */
70 /* Linux kernel >= 3.18 defines SIGRTMIN to 32 on all architectures. */
71 # define ASM_SIGRTMIN 32
74 /* Under glibc 2.1, SIGRTMAX et al are functions, but __SIGRTMAX is a
75 constant. This is what we want. Otherwise, just use SIGRTMAX. */
78 # define __SIGRTMAX SIGRTMAX
82 # define ASM_SIGRTMAX __SIGRTMAX
86 /* Note on the size of sigset_t:
88 * In glibc, sigset_t is an array with space for 1024 bits (!),
89 * even though all arches supported by Linux have only 64 signals
90 * except MIPS, which has 128. IOW, it is 128 bytes long.
92 * In-kernel sigset_t is sized correctly (it is either 64 or 128 bit long).
93 * However, some old syscall return only 32 lower bits (one word).
94 * Example: sys_sigpending vs sys_rt_sigpending.
96 * Be aware of this fact when you try to
97 * memcpy(&tcp->u_arg[1], &something, sizeof(sigset_t))
98 * - sizeof(sigset_t) is much bigger than you think,
99 * it may overflow tcp->u_arg[] array, and it may try to copy more data
100 * than is really available in <something>.
102 * umoven(tcp, addr, sizeof(sigset_t), &sigset)
103 * may be a bad idea: it'll try to read much more data than needed
104 * to fetch a sigset_t.
105 * Use NSIG_BYTES as a size instead.
109 get_sa_handler_str(kernel_ulong_t handler)
111 return xlookup(sa_handler_values, handler);
115 print_sa_handler(kernel_ulong_t handler)
117 const char *sa_handler_str = get_sa_handler_str(handler);
120 tprints(sa_handler_str);
126 signame(const int sig)
128 static char buf[sizeof("SIGRT_%u") + sizeof(int)*3];
131 const unsigned int s = sig;
136 if (s >= ASM_SIGRTMIN && s <= (unsigned int) ASM_SIGRTMAX) {
137 xsprintf(buf, "SIGRT_%u", s - ASM_SIGRTMIN);
142 xsprintf(buf, "%d", sig);
147 popcount32(const uint32_t *a, unsigned int size)
149 unsigned int count = 0;
151 for (; size; ++a, --size) {
154 #ifdef HAVE___BUILTIN_POPCOUNT
155 count += __builtin_popcount(x);
166 sprintsigmask_n(const char *prefix, const void *sig_mask, unsigned int bytes)
169 * The maximum number of signal names to be printed
170 * is NSIG_BYTES * 8 * 2 / 3.
171 * Most of signal names have length 7,
172 * average length of signal names is less than 7.
173 * The length of prefix string does not exceed 16.
175 static char outstr[128 + 8 * (NSIG_BYTES * 8 * 2 / 3)];
178 const uint32_t *mask;
179 uint32_t inverted_mask[NSIG_BYTES / 4];
184 s = stpcpy(outstr, prefix);
187 /* length of signal mask in 4-byte words */
188 size = (bytes >= NSIG_BYTES) ? NSIG_BYTES / 4 : (bytes + 3) / 4;
190 /* check whether 2/3 or more bits are set */
191 if (popcount32(mask, size) >= size * (4 * 8) * 2 / 3) {
192 /* show those signals that are NOT in the mask */
194 for (j = 0; j < size; ++j)
195 inverted_mask[j] = ~mask[j];
196 mask = inverted_mask;
201 for (i = 0; (i = next_set_bit(mask, i, size * (4 * 8))) >= 0; ) {
204 if ((unsigned) i < nsignals) {
205 s = stpcpy(s, signalent[i] + 3);
208 else if (i >= ASM_SIGRTMIN && i <= ASM_SIGRTMAX) {
209 s = xappendstr(outstr, s, "RT_%u", i - ASM_SIGRTMIN);
213 s = xappendstr(outstr, s, "%u", i);
224 #define sprintsigmask_val(prefix, mask) \
225 sprintsigmask_n((prefix), &(mask), sizeof(mask))
227 #define tprintsigmask_val(prefix, mask) \
228 tprints(sprintsigmask_n((prefix), &(mask), sizeof(mask)))
231 sprint_old_sigmask_val(const char *const prefix, const unsigned long mask)
233 #if defined(current_wordsize) || !defined(WORDS_BIGENDIAN)
234 return sprintsigmask_n(prefix, &mask, current_wordsize);
235 #else /* !current_wordsize && WORDS_BIGENDIAN */
236 if (current_wordsize == sizeof(mask)) {
237 return sprintsigmask_val(prefix, mask);
239 uint32_t mask32 = mask;
240 return sprintsigmask_val(prefix, mask32);
245 #define tprint_old_sigmask_val(prefix, mask) \
246 tprints(sprint_old_sigmask_val((prefix), (mask)))
251 tprints(signame(nr));
255 print_sigset_addr_len_limit(struct tcb *const tcp, const kernel_ulong_t addr,
256 const kernel_ulong_t len, const unsigned int min_len)
259 * Here len is usually equal to NSIG_BYTES or current_wordsize.
260 * But we code this defensively:
262 if (len < min_len || len > NSIG_BYTES) {
266 int mask[NSIG_BYTES / sizeof(int)] = {};
267 if (umoven_or_printaddr(tcp, addr, len, mask))
269 tprints(sprintsigmask_n("", mask, len));
273 print_sigset_addr_len(struct tcb *const tcp, const kernel_ulong_t addr,
274 const kernel_ulong_t len)
276 print_sigset_addr_len_limit(tcp, addr, len, current_wordsize);
280 print_sigset_addr(struct tcb *const tcp, const kernel_ulong_t addr)
282 print_sigset_addr_len_limit(tcp, addr, NSIG_BYTES, NSIG_BYTES);
288 tprint_old_sigmask_val("", (unsigned) tcp->u_arg[0]);
289 } else if (!syserror(tcp)) {
290 tcp->auxstr = sprint_old_sigmask_val("old mask ",
291 (unsigned) tcp->u_rval);
292 return RVAL_HEX | RVAL_STR;
297 struct old_sigaction {
298 /* sa_handler may be a libc #define, need to use other name: */
300 unsigned int sa_flags;
301 unsigned long sa_handler__;
302 unsigned long sa_mask;
304 unsigned long sa_handler__;
305 unsigned long sa_mask;
306 unsigned int sa_flags;
308 unsigned long sa_handler__;
309 unsigned long sa_mask;
310 unsigned long sa_flags;
311 unsigned long sa_restorer;
320 decode_old_sigaction(struct tcb *const tcp, const kernel_ulong_t addr)
322 struct old_sigaction sa;
324 #ifndef current_wordsize
325 if (current_wordsize < sizeof(sa.sa_handler__)) {
326 struct old_sigaction32 {
327 uint32_t sa_handler__;
330 uint32_t sa_restorer;
333 if (umove_or_printaddr(tcp, addr, &sa32))
336 memset(&sa, 0, sizeof(sa));
337 sa.sa_handler__ = sa32.sa_handler__;
338 sa.sa_flags = sa32.sa_flags;
339 sa.sa_restorer = sa32.sa_restorer;
340 sa.sa_mask = sa32.sa_mask;
343 if (umove_or_printaddr(tcp, addr, &sa))
346 tprints("{sa_handler=");
347 print_sa_handler(sa.sa_handler__);
348 tprints(", sa_mask=");
349 tprint_old_sigmask_val("", sa.sa_mask);
350 tprints(", sa_flags=");
351 printflags(sigact_flags, sa.sa_flags, "SA_???");
352 #if !(defined ALPHA || defined MIPS)
353 if (sa.sa_flags & 0x04000000U) {
354 tprints(", sa_restorer=");
355 printaddr(sa.sa_restorer);
364 int signo = tcp->u_arg[0];
365 #if defined SPARC || defined SPARC64
373 decode_old_sigaction(tcp, tcp->u_arg[1]);
376 decode_old_sigaction(tcp, tcp->u_arg[2]);
383 printsignal(tcp->u_arg[0]);
385 print_sa_handler(tcp->u_arg[1]);
387 } else if (!syserror(tcp)) {
388 tcp->auxstr = get_sa_handler_str(tcp->u_rval);
389 return RVAL_HEX | RVAL_STR;
396 if (exiting(tcp) && !syserror(tcp)) {
397 tcp->auxstr = sprint_old_sigmask_val("mask ", tcp->u_rval);
398 return RVAL_HEX | RVAL_STR;
406 print_sigset_addr_len(tcp, tcp->u_arg[tcp->s_ent->nargs - 1],
409 tprint_old_sigmask_val("", tcp->u_arg[tcp->s_ent->nargs - 1]);
417 * The OSF/1 sigprocmask is different: it doesn't pass in two pointers,
418 * but rather passes in the new bitmask as an argument and then returns
419 * the old bitmask. This "works" because we only have 64 signals to worry
420 * about. If you want more, use of the rt_sigprocmask syscall is required.
423 * old = osf_sigprocmask(how, new);
425 * ret = sigprocmask(how, &new, &old, ...);
427 SYS_FUNC(osf_sigprocmask)
430 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
431 tprintsigmask_val(", ", tcp->u_arg[1]);
432 } else if (!syserror(tcp)) {
433 tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
434 return RVAL_HEX | RVAL_STR;
441 /* "Old" sigprocmask, which operates with word-sized signal masks */
442 SYS_FUNC(sigprocmask)
445 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
447 print_sigset_addr_len(tcp, tcp->u_arg[1], current_wordsize);
450 print_sigset_addr_len(tcp, tcp->u_arg[2], current_wordsize);
460 signame(tcp->u_arg[1]));
467 tprintf("%d, %d, %s",
470 signame(tcp->u_arg[2]));
478 print_sigset_addr_len(tcp, tcp->u_arg[0], current_wordsize);
482 SYS_FUNC(rt_sigprocmask)
484 /* Note: arg[3] is the length of the sigset. Kernel requires NSIG_BYTES */
486 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
488 print_sigset_addr_len(tcp, tcp->u_arg[1], tcp->u_arg[3]);
491 print_sigset_addr_len(tcp, tcp->u_arg[2], tcp->u_arg[3]);
492 tprintf(", %" PRI_klu, tcp->u_arg[3]);
497 /* Structure describing the action to be taken when a signal arrives. */
498 struct new_sigaction {
499 /* sa_handler may be a libc #define, need to use other name: */
501 unsigned int sa_flags;
502 unsigned long sa_handler__;
504 unsigned long sa_handler__;
505 unsigned long sa_flags;
508 unsigned long sa_restorer;
510 /* Kernel treats sa_mask as an array of longs. */
511 unsigned long sa_mask[NSIG / sizeof(long)];
513 /* Same for i386-on-x86_64 and similar cases */
514 struct new_sigaction32 {
515 uint32_t sa_handler__;
518 uint32_t sa_restorer;
520 uint32_t sa_mask[2 * (NSIG / sizeof(long))];
524 decode_new_sigaction(struct tcb *const tcp, const kernel_ulong_t addr)
526 struct new_sigaction sa;
528 #ifndef current_wordsize
529 if (current_wordsize < sizeof(sa.sa_handler__)) {
530 struct new_sigaction32 sa32;
532 if (umove_or_printaddr(tcp, addr, &sa32))
535 memset(&sa, 0, sizeof(sa));
536 sa.sa_handler__ = sa32.sa_handler__;
537 sa.sa_flags = sa32.sa_flags;
538 #if HAVE_SA_RESTORER && defined SA_RESTORER
539 sa.sa_restorer = sa32.sa_restorer;
541 /* Kernel treats sa_mask as an array of longs.
542 * For 32-bit process, "long" is uint32_t, thus, for example,
543 * 32th bit in sa_mask will end up as bit 0 in sa_mask[1].
544 * But for (64-bit) kernel, 32th bit in sa_mask is
545 * 32th bit in 0th (64-bit) long!
546 * For little-endian, it's the same.
547 * For big-endian, we swap 32-bit words.
549 sa.sa_mask[0] = ULONG_LONG(sa32.sa_mask[0], sa32.sa_mask[1]);
552 if (umove_or_printaddr(tcp, addr, &sa))
555 tprints("{sa_handler=");
556 print_sa_handler(sa.sa_handler__);
557 tprints(", sa_mask=");
559 * Sigset size is in tcp->u_arg[4] (SPARC)
560 * or in tcp->u_arg[3] (all other),
561 * but kernel won't handle sys_rt_sigaction
562 * with wrong sigset size (just returns EINVAL instead).
563 * We just fetch the right size, which is NSIG_BYTES.
565 tprintsigmask_val("", sa.sa_mask);
566 tprints(", sa_flags=");
568 printflags(sigact_flags, sa.sa_flags, "SA_???");
569 #if HAVE_SA_RESTORER && defined SA_RESTORER
570 if (sa.sa_flags & SA_RESTORER) {
571 tprints(", sa_restorer=");
572 printaddr(sa.sa_restorer);
578 SYS_FUNC(rt_sigaction)
581 printsignal(tcp->u_arg[0]);
583 decode_new_sigaction(tcp, tcp->u_arg[1]);
586 decode_new_sigaction(tcp, tcp->u_arg[2]);
587 #if defined(SPARC) || defined(SPARC64)
588 tprintf(", %#" PRI_klx ", %" PRI_klu, tcp->u_arg[3], tcp->u_arg[4]);
590 tprintf(", %" PRI_klu ", %#" PRI_klx, tcp->u_arg[3], tcp->u_arg[4]);
592 tprintf(", %" PRI_klu, tcp->u_arg[3]);
598 SYS_FUNC(rt_sigpending)
602 * One of the few syscalls where sigset size (arg[1])
603 * is allowed to be <= NSIG_BYTES, not strictly ==.
604 * This allows non-rt sigpending() syscall
605 * to reuse rt_sigpending() code in kernel.
607 print_sigset_addr_len_limit(tcp, tcp->u_arg[0],
609 tprintf(", %" PRI_klu, tcp->u_arg[1]);
614 SYS_FUNC(rt_sigsuspend)
616 /* NB: kernel requires arg[1] == NSIG_BYTES */
617 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[1]);
618 tprintf(", %" PRI_klu, tcp->u_arg[1]);
624 print_sigqueueinfo(struct tcb *const tcp, const int sig,
625 const kernel_ulong_t addr)
629 printsiginfo_at(tcp, addr);
632 SYS_FUNC(rt_sigqueueinfo)
634 tprintf("%d, ", (int) tcp->u_arg[0]);
635 print_sigqueueinfo(tcp, tcp->u_arg[1], tcp->u_arg[2]);
640 SYS_FUNC(rt_tgsigqueueinfo)
642 tprintf("%d, %d, ", (int) tcp->u_arg[0], (int) tcp->u_arg[1]);
643 print_sigqueueinfo(tcp, tcp->u_arg[2], tcp->u_arg[3]);
648 SYS_FUNC(rt_sigtimedwait)
650 /* NB: kernel requires arg[3] == NSIG_BYTES */
652 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[3]);
654 if (!(tcp->u_arg[1] && verbose(tcp))) {
656 * This is the only "return" parameter,
657 * if we are not going to fetch it on exit,
658 * decode all parameters on entry.
660 printaddr(tcp->u_arg[1]);
662 print_timespec(tcp, tcp->u_arg[2]);
663 tprintf(", %" PRI_klu, tcp->u_arg[3]);
665 char *sts = xstrdup(sprint_timespec(tcp, tcp->u_arg[2]));
666 set_tcb_priv_data(tcp, sts, free);
669 if (tcp->u_arg[1] && verbose(tcp)) {
670 printsiginfo_at(tcp, tcp->u_arg[1]);
672 tprints(get_tcb_priv_data(tcp));
673 tprintf(", %" PRI_klu, tcp->u_arg[3]);
676 if (!syserror(tcp) && tcp->u_rval) {
677 tcp->auxstr = signame(tcp->u_rval);
684 SYS_FUNC(restart_syscall)
686 tprintf("<... resuming interrupted %s ...>",
687 tcp->s_prev_ent ? tcp->s_prev_ent->sys_name : "system call");