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-2019 The strace developers.
10 * All rights reserved.
12 * SPDX-License-Identifier: LGPL-2.1-or-later
19 /* The libc headers do not define this constant since it should only be
20 used by the implementation. So we define it here. */
22 # ifdef ASM_SA_RESTORER
23 # define SA_RESTORER ASM_SA_RESTORER
28 * Some architectures define SA_RESTORER in their headers,
29 * but do not actually have sa_restorer.
31 * Some architectures, otherwise, do not define SA_RESTORER in their headers,
32 * but actually have sa_restorer.
34 #ifdef HAVE_ARCH_SA_RESTORER
35 # define HAVE_SA_RESTORER HAVE_ARCH_SA_RESTORER
36 #else /* !HAVE_ARCH_SA_RESTORER */
38 # define HAVE_SA_RESTORER 1
40 # define HAVE_SA_RESTORER 0
42 #endif /* HAVE_ARCH_SA_RESTORER */
44 #include "xlat/sa_handler_values.h"
45 #include "xlat/sigact_flags.h"
46 #include "xlat/sigprocmaskcmds.h"
48 /* Anonymous realtime signals. */
50 /* Linux kernel >= 3.18 defines SIGRTMIN to 32 on all architectures. */
51 # define ASM_SIGRTMIN 32
54 /* Under glibc 2.1, SIGRTMAX et al are functions, but __SIGRTMAX is a
55 constant. This is what we want. Otherwise, just use SIGRTMAX. */
58 # define __SIGRTMAX SIGRTMAX
62 # define ASM_SIGRTMAX __SIGRTMAX
66 /* Note on the size of sigset_t:
68 * In glibc, sigset_t is an array with space for 1024 bits (!),
69 * even though all arches supported by Linux have only 64 signals
70 * except MIPS, which has 128. IOW, it is 128 bytes long.
72 * In-kernel sigset_t is sized correctly (it is either 64 or 128 bit long).
73 * However, some old syscall return only 32 lower bits (one word).
74 * Example: sys_sigpending vs sys_rt_sigpending.
76 * Be aware of this fact when you try to
77 * memcpy(&tcp->u_arg[1], &something, sizeof(sigset_t))
78 * - sizeof(sigset_t) is much bigger than you think,
79 * it may overflow tcp->u_arg[] array, and it may try to copy more data
80 * than is really available in <something>.
82 * umoven(tcp, addr, sizeof(sigset_t), &sigset)
83 * may be a bad idea: it'll try to read much more data than needed
84 * to fetch a sigset_t.
85 * Use NSIG_BYTES as a size instead.
89 get_sa_handler_str(kernel_ulong_t handler)
91 return xlookup(sa_handler_values, handler);
95 print_sa_handler(kernel_ulong_t handler)
97 const char *sa_handler_str = get_sa_handler_str(handler);
100 print_xlat_ex(handler, sa_handler_str, XLAT_STYLE_DEFAULT);
106 signame(const int sig)
109 const unsigned int s = sig;
114 if (s >= ASM_SIGRTMIN && s <= (unsigned int) ASM_SIGRTMAX) {
115 static char buf[sizeof("SIGRT_%u") + sizeof(s) * 3];
117 xsprintf(buf, "SIGRT_%u", s - ASM_SIGRTMIN);
127 sprintsigname(const int sig)
129 const char *str = signame(sig);
134 static char buf[sizeof(sig) * 3 + 2];
136 xsprintf(buf, "%d", sig);
142 sprintsigmask_n(const char *prefix, const void *sig_mask, unsigned int bytes)
145 * The maximum number of signal names to be printed
146 * is NSIG_BYTES * 8 * 2 / 3.
147 * Most of signal names have length 7,
148 * average length of signal names is less than 7.
149 * The length of prefix string does not exceed 16.
151 static char outstr[128 + 8 * (NSIG_BYTES * 8 * 2 / 3)];
154 const uint32_t *mask;
155 uint32_t inverted_mask[NSIG_BYTES / 4];
160 s = stpcpy(outstr, prefix);
163 /* length of signal mask in 4-byte words */
164 size = (bytes >= NSIG_BYTES) ? NSIG_BYTES / 4 : (bytes + 3) / 4;
166 /* check whether 2/3 or more bits are set */
167 if (popcount32(mask, size) >= size * (4 * 8) * 2 / 3) {
168 /* show those signals that are NOT in the mask */
170 for (j = 0; j < size; ++j)
171 inverted_mask[j] = ~mask[j];
172 mask = inverted_mask;
177 for (i = 0; (i = next_set_bit(mask, i, size * (4 * 8))) >= 0; ) {
180 if ((unsigned) i < nsignals) {
181 s = stpcpy(s, signalent[i] + 3);
184 else if (i >= ASM_SIGRTMIN && i <= ASM_SIGRTMAX) {
185 s = xappendstr(outstr, s, "RT_%u", i - ASM_SIGRTMIN);
189 s = xappendstr(outstr, s, "%u", i);
200 #define sprintsigmask_val(prefix, mask) \
201 sprintsigmask_n((prefix), &(mask), sizeof(mask))
203 #define tprintsigmask_val(prefix, mask) \
204 tprints(sprintsigmask_n((prefix), &(mask), sizeof(mask)))
207 sprint_old_sigmask_val(const char *const prefix, const unsigned long mask)
209 #if defined(current_wordsize) || !defined(WORDS_BIGENDIAN)
210 return sprintsigmask_n(prefix, &mask, current_wordsize);
211 #else /* !current_wordsize && WORDS_BIGENDIAN */
212 if (current_wordsize == sizeof(mask)) {
213 return sprintsigmask_val(prefix, mask);
215 uint32_t mask32 = mask;
216 return sprintsigmask_val(prefix, mask32);
221 #define tprint_old_sigmask_val(prefix, mask) \
222 tprints(sprint_old_sigmask_val((prefix), (mask)))
227 const char *str = signame(nr);
229 if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
231 if (!str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
233 (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
234 ? tprints_comment : tprints)(str);
238 print_sigset_addr_len_limit(struct tcb *const tcp, const kernel_ulong_t addr,
239 const kernel_ulong_t len, const unsigned int min_len)
242 * Here len is usually equal to NSIG_BYTES or current_wordsize.
243 * But we code this defensively:
245 if (len < min_len || len > NSIG_BYTES) {
249 int mask[NSIG_BYTES / sizeof(int)] = {};
250 if (umoven_or_printaddr(tcp, addr, len, mask))
252 tprints(sprintsigmask_n("", mask, len));
256 print_sigset_addr_len(struct tcb *const tcp, const kernel_ulong_t addr,
257 const kernel_ulong_t len)
259 print_sigset_addr_len_limit(tcp, addr, len, current_wordsize);
263 print_sigset_addr(struct tcb *const tcp, const kernel_ulong_t addr)
265 print_sigset_addr_len_limit(tcp, addr, NSIG_BYTES, NSIG_BYTES);
271 tprint_old_sigmask_val("", (unsigned) tcp->u_arg[0]);
272 } else if (!syserror(tcp)) {
273 tcp->auxstr = sprint_old_sigmask_val("old mask ",
274 (unsigned) tcp->u_rval);
275 return RVAL_HEX | RVAL_STR;
280 struct old_sigaction {
281 /* sa_handler may be a libc #define, need to use other name: */
283 unsigned int sa_flags;
284 unsigned long sa_handler__;
285 unsigned long sa_mask;
287 unsigned long sa_handler__;
288 unsigned long sa_mask;
289 unsigned int sa_flags;
291 unsigned long sa_handler__;
292 unsigned long sa_mask;
293 unsigned long sa_flags;
294 unsigned long sa_restorer;
303 decode_old_sigaction(struct tcb *const tcp, const kernel_ulong_t addr)
305 struct old_sigaction sa;
307 #ifndef current_wordsize
308 if (current_wordsize < sizeof(sa.sa_handler__)) {
309 struct old_sigaction32 {
310 uint32_t sa_handler__;
313 uint32_t sa_restorer;
316 if (umove_or_printaddr(tcp, addr, &sa32))
319 memset(&sa, 0, sizeof(sa));
320 sa.sa_handler__ = sa32.sa_handler__;
321 sa.sa_flags = sa32.sa_flags;
322 sa.sa_restorer = sa32.sa_restorer;
323 sa.sa_mask = sa32.sa_mask;
326 if (umove_or_printaddr(tcp, addr, &sa))
329 tprints("{sa_handler=");
330 print_sa_handler(sa.sa_handler__);
331 tprints(", sa_mask=");
332 tprint_old_sigmask_val("", sa.sa_mask);
333 tprints(", sa_flags=");
334 printflags(sigact_flags, sa.sa_flags, "SA_???");
335 #if !(defined ALPHA || defined MIPS)
336 if (sa.sa_flags & 0x04000000U) {
337 tprints(", sa_restorer=");
338 printaddr(sa.sa_restorer);
347 int signo = tcp->u_arg[0];
348 #if defined SPARC || defined SPARC64
356 decode_old_sigaction(tcp, tcp->u_arg[1]);
359 decode_old_sigaction(tcp, tcp->u_arg[2]);
366 printsignal(tcp->u_arg[0]);
368 print_sa_handler(tcp->u_arg[1]);
370 } else if (!syserror(tcp)) {
371 tcp->auxstr = get_sa_handler_str(tcp->u_rval);
372 return RVAL_HEX | RVAL_STR;
379 if (exiting(tcp) && !syserror(tcp)) {
380 tcp->auxstr = sprint_old_sigmask_val("mask ", tcp->u_rval);
381 return RVAL_HEX | RVAL_STR;
389 print_sigset_addr_len(tcp, tcp->u_arg[n_args(tcp) - 1],
392 tprint_old_sigmask_val("", tcp->u_arg[n_args(tcp) - 1]);
400 * The OSF/1 sigprocmask is different: it doesn't pass in two pointers,
401 * but rather passes in the new bitmask as an argument and then returns
402 * the old bitmask. This "works" because we only have 64 signals to worry
403 * about. If you want more, use of the rt_sigprocmask syscall is required.
406 * old = osf_sigprocmask(how, new);
408 * ret = sigprocmask(how, &new, &old, ...);
410 SYS_FUNC(osf_sigprocmask)
413 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
414 tprintsigmask_val(", ", tcp->u_arg[1]);
415 } else if (!syserror(tcp)) {
416 tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
417 return RVAL_HEX | RVAL_STR;
424 /* "Old" sigprocmask, which operates with word-sized signal masks */
425 SYS_FUNC(sigprocmask)
428 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
430 print_sigset_addr_len(tcp, tcp->u_arg[1], current_wordsize);
433 print_sigset_addr_len(tcp, tcp->u_arg[2], current_wordsize);
442 tprintf("%d, ", (int) tcp->u_arg[0]);
444 printsignal(tcp->u_arg[1]);
452 tprintf("%d, %d, ", (int) tcp->u_arg[0], (int) tcp->u_arg[1]);
454 printsignal(tcp->u_arg[2]);
462 print_sigset_addr_len(tcp, tcp->u_arg[0], current_wordsize);
466 SYS_FUNC(rt_sigprocmask)
468 /* Note: arg[3] is the length of the sigset. Kernel requires NSIG_BYTES */
470 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
472 print_sigset_addr_len(tcp, tcp->u_arg[1], tcp->u_arg[3]);
475 print_sigset_addr_len(tcp, tcp->u_arg[2], tcp->u_arg[3]);
476 tprintf(", %" PRI_klu, tcp->u_arg[3]);
481 /* Structure describing the action to be taken when a signal arrives. */
482 struct new_sigaction {
483 /* sa_handler may be a libc #define, need to use other name: */
485 unsigned int sa_flags;
486 unsigned long sa_handler__;
488 unsigned long sa_handler__;
489 unsigned long sa_flags;
492 unsigned long sa_restorer;
494 /* Kernel treats sa_mask as an array of longs. */
495 unsigned long sa_mask[NSIG / sizeof(long)];
497 /* Same for i386-on-x86_64 and similar cases */
498 struct new_sigaction32 {
499 uint32_t sa_handler__;
502 uint32_t sa_restorer;
504 uint32_t sa_mask[2 * (NSIG / sizeof(long))];
508 decode_new_sigaction(struct tcb *const tcp, const kernel_ulong_t addr)
510 struct new_sigaction sa;
512 #ifndef current_wordsize
513 if (current_wordsize < sizeof(sa.sa_handler__)) {
514 struct new_sigaction32 sa32;
516 if (umove_or_printaddr(tcp, addr, &sa32))
519 memset(&sa, 0, sizeof(sa));
520 sa.sa_handler__ = sa32.sa_handler__;
521 sa.sa_flags = sa32.sa_flags;
522 # if HAVE_SA_RESTORER && defined SA_RESTORER
523 sa.sa_restorer = sa32.sa_restorer;
525 /* Kernel treats sa_mask as an array of longs.
526 * For 32-bit process, "long" is uint32_t, thus, for example,
527 * 32th bit in sa_mask will end up as bit 0 in sa_mask[1].
528 * But for (64-bit) kernel, 32th bit in sa_mask is
529 * 32th bit in 0th (64-bit) long!
530 * For little-endian, it's the same.
531 * For big-endian, we swap 32-bit words.
533 sa.sa_mask[0] = ULONG_LONG(sa32.sa_mask[0], sa32.sa_mask[1]);
536 if (umove_or_printaddr(tcp, addr, &sa))
539 tprints("{sa_handler=");
540 print_sa_handler(sa.sa_handler__);
541 tprints(", sa_mask=");
543 * Sigset size is in tcp->u_arg[4] (SPARC)
544 * or in tcp->u_arg[3] (all other),
545 * but kernel won't handle sys_rt_sigaction
546 * with wrong sigset size (just returns EINVAL instead).
547 * We just fetch the right size, which is NSIG_BYTES.
549 tprintsigmask_val("", sa.sa_mask);
550 tprints(", sa_flags=");
552 printflags(sigact_flags, sa.sa_flags, "SA_???");
553 #if HAVE_SA_RESTORER && defined SA_RESTORER
554 if (sa.sa_flags & SA_RESTORER) {
555 tprints(", sa_restorer=");
556 printaddr(sa.sa_restorer);
562 SYS_FUNC(rt_sigaction)
565 printsignal(tcp->u_arg[0]);
567 decode_new_sigaction(tcp, tcp->u_arg[1]);
570 decode_new_sigaction(tcp, tcp->u_arg[2]);
571 #if defined(SPARC) || defined(SPARC64)
572 tprintf(", %#" PRI_klx ", %" PRI_klu, tcp->u_arg[3], tcp->u_arg[4]);
574 tprintf(", %" PRI_klu ", %#" PRI_klx, tcp->u_arg[3], tcp->u_arg[4]);
576 tprintf(", %" PRI_klu, tcp->u_arg[3]);
582 SYS_FUNC(rt_sigpending)
586 * One of the few syscalls where sigset size (arg[1])
587 * is allowed to be <= NSIG_BYTES, not strictly ==.
588 * This allows non-rt sigpending() syscall
589 * to reuse rt_sigpending() code in kernel.
591 print_sigset_addr_len_limit(tcp, tcp->u_arg[0],
593 tprintf(", %" PRI_klu, tcp->u_arg[1]);
598 SYS_FUNC(rt_sigsuspend)
600 /* NB: kernel requires arg[1] == NSIG_BYTES */
601 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[1]);
602 tprintf(", %" PRI_klu, tcp->u_arg[1]);
608 print_sigqueueinfo(struct tcb *const tcp, const int sig,
609 const kernel_ulong_t addr)
613 printsiginfo_at(tcp, addr);
616 SYS_FUNC(rt_sigqueueinfo)
618 tprintf("%d, ", (int) tcp->u_arg[0]);
619 print_sigqueueinfo(tcp, tcp->u_arg[1], tcp->u_arg[2]);
624 SYS_FUNC(rt_tgsigqueueinfo)
626 tprintf("%d, %d, ", (int) tcp->u_arg[0], (int) tcp->u_arg[1]);
627 print_sigqueueinfo(tcp, tcp->u_arg[2], tcp->u_arg[3]);
632 SYS_FUNC(pidfd_send_signal)
635 printfd(tcp, tcp->u_arg[0]);
636 /* int sig, siginfo_t *info */
638 print_sigqueueinfo(tcp, tcp->u_arg[1], tcp->u_arg[2]);
639 /* unsigned int flags */
640 tprintf(", %#x", (unsigned int) tcp->u_arg[3]);
646 do_rt_sigtimedwait(struct tcb *const tcp, const print_obj_by_addr_fn print_ts,
647 const sprint_obj_by_addr_fn sprint_ts)
649 /* NB: kernel requires arg[3] == NSIG_BYTES */
651 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[3]);
653 if (!(tcp->u_arg[1] && verbose(tcp))) {
655 * This is the only "return" parameter,
656 * if we are not going to fetch it on exit,
657 * decode all parameters on entry.
659 printaddr(tcp->u_arg[1]);
661 print_ts(tcp, tcp->u_arg[2]);
662 tprintf(", %" PRI_klu, tcp->u_arg[3]);
664 char *sts = xstrdup(sprint_ts(tcp, tcp->u_arg[2]));
665 set_tcb_priv_data(tcp, sts, free);
668 if (tcp->u_arg[1] && verbose(tcp)) {
669 printsiginfo_at(tcp, tcp->u_arg[1]);
671 tprints(get_tcb_priv_data(tcp));
672 tprintf(", %" PRI_klu, tcp->u_arg[3]);
675 if (!syserror(tcp) && tcp->u_rval) {
676 tcp->auxstr = signame(tcp->u_rval);
683 #if HAVE_ARCH_TIME32_SYSCALLS
684 SYS_FUNC(rt_sigtimedwait_time32)
686 return do_rt_sigtimedwait(tcp, print_timespec32, sprint_timespec32);
690 SYS_FUNC(rt_sigtimedwait_time64)
692 return do_rt_sigtimedwait(tcp, print_timespec64, sprint_timespec64);
695 SYS_FUNC(restart_syscall)
697 tprintf("<... resuming interrupted %s ...>",
698 tcp->s_prev_ent ? tcp->s_prev_ent->sys_name : "system call");