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-2017 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.
38 /* The libc headers do not define this constant since it should only be
39 used by the implementation. So we define it here. */
41 # ifdef ASM_SA_RESTORER
42 # define SA_RESTORER ASM_SA_RESTORER
47 * Some architectures define SA_RESTORER in their headers,
48 * but do not actually have sa_restorer.
50 * Some architectures, otherwise, do not define SA_RESTORER in their headers,
51 * but actually have sa_restorer.
54 # if defined HPPA || defined IA64
55 # define HAVE_SA_RESTORER 0
57 # define HAVE_SA_RESTORER 1
59 #else /* !SA_RESTORER */
60 # if defined SPARC || defined SPARC64 || defined M68K
61 # define HAVE_SA_RESTORER 1
63 # define HAVE_SA_RESTORER 0
67 #include "xlat/sa_handler_values.h"
68 #include "xlat/sigact_flags.h"
69 #include "xlat/sigprocmaskcmds.h"
71 /* Anonymous realtime signals. */
73 /* Linux kernel >= 3.18 defines SIGRTMIN to 32 on all architectures. */
74 # define ASM_SIGRTMIN 32
77 /* Under glibc 2.1, SIGRTMAX et al are functions, but __SIGRTMAX is a
78 constant. This is what we want. Otherwise, just use SIGRTMAX. */
81 # define __SIGRTMAX SIGRTMAX
85 # define ASM_SIGRTMAX __SIGRTMAX
89 /* Note on the size of sigset_t:
91 * In glibc, sigset_t is an array with space for 1024 bits (!),
92 * even though all arches supported by Linux have only 64 signals
93 * except MIPS, which has 128. IOW, it is 128 bytes long.
95 * In-kernel sigset_t is sized correctly (it is either 64 or 128 bit long).
96 * However, some old syscall return only 32 lower bits (one word).
97 * Example: sys_sigpending vs sys_rt_sigpending.
99 * Be aware of this fact when you try to
100 * memcpy(&tcp->u_arg[1], &something, sizeof(sigset_t))
101 * - sizeof(sigset_t) is much bigger than you think,
102 * it may overflow tcp->u_arg[] array, and it may try to copy more data
103 * than is really available in <something>.
105 * umoven(tcp, addr, sizeof(sigset_t), &sigset)
106 * may be a bad idea: it'll try to read much more data than needed
107 * to fetch a sigset_t.
108 * Use NSIG_BYTES as a size instead.
112 get_sa_handler_str(kernel_ulong_t handler)
114 return xlookup(sa_handler_values, handler);
118 print_sa_handler(kernel_ulong_t handler)
120 const char *sa_handler_str = get_sa_handler_str(handler);
123 tprints(sa_handler_str);
129 signame(const int sig)
131 static char buf[sizeof("SIGRT_%u") + sizeof(int)*3];
134 const unsigned int s = sig;
139 if (s >= ASM_SIGRTMIN && s <= (unsigned int) ASM_SIGRTMAX) {
140 sprintf(buf, "SIGRT_%u", s - ASM_SIGRTMIN);
145 sprintf(buf, "%d", sig);
150 popcount32(const uint32_t *a, unsigned int size)
152 unsigned int count = 0;
154 for (; size; ++a, --size) {
157 #ifdef HAVE___BUILTIN_POPCOUNT
158 count += __builtin_popcount(x);
169 sprintsigmask_n(const char *prefix, const void *sig_mask, unsigned int bytes)
172 * The maximum number of signal names to be printed
173 * is NSIG_BYTES * 8 * 2 / 3.
174 * Most of signal names have length 7,
175 * average length of signal names is less than 7.
176 * The length of prefix string does not exceed 16.
178 static char outstr[128 + 8 * (NSIG_BYTES * 8 * 2 / 3)];
181 const uint32_t *mask;
182 uint32_t inverted_mask[NSIG_BYTES / 4];
187 s = stpcpy(outstr, prefix);
190 /* length of signal mask in 4-byte words */
191 size = (bytes >= NSIG_BYTES) ? NSIG_BYTES / 4 : (bytes + 3) / 4;
193 /* check whether 2/3 or more bits are set */
194 if (popcount32(mask, size) >= size * (4 * 8) * 2 / 3) {
195 /* show those signals that are NOT in the mask */
197 for (j = 0; j < size; ++j)
198 inverted_mask[j] = ~mask[j];
199 mask = inverted_mask;
204 for (i = 0; (i = next_set_bit(mask, i, size * (4 * 8))) >= 0; ) {
207 if ((unsigned) i < nsignals) {
208 s = stpcpy(s, signalent[i] + 3);
211 else if (i >= ASM_SIGRTMIN && i <= ASM_SIGRTMAX) {
212 s += sprintf(s, "RT_%u", i - ASM_SIGRTMIN);
216 s += sprintf(s, "%u", i);
227 #define sprintsigmask_val(prefix, mask) \
228 sprintsigmask_n((prefix), &(mask), sizeof(mask))
230 #define tprintsigmask_val(prefix, mask) \
231 tprints(sprintsigmask_n((prefix), &(mask), sizeof(mask)))
236 tprints(signame(nr));
240 print_sigset_addr_len_limit(struct tcb *const tcp, const kernel_ulong_t addr,
241 const kernel_ulong_t len, const unsigned int min_len)
244 * Here len is usually equal to NSIG_BYTES or current_wordsize.
245 * But we code this defensively:
247 if (len < min_len || len > NSIG_BYTES) {
251 int mask[NSIG_BYTES / sizeof(int)] = {};
252 if (umoven_or_printaddr(tcp, addr, len, mask))
254 tprints(sprintsigmask_n("", mask, len));
258 print_sigset_addr_len(struct tcb *const tcp, const kernel_ulong_t addr,
259 const kernel_ulong_t len)
261 print_sigset_addr_len_limit(tcp, addr, len, current_wordsize);
265 print_sigset_addr(struct tcb *const tcp, const kernel_ulong_t addr)
267 print_sigset_addr_len_limit(tcp, addr, NSIG_BYTES, NSIG_BYTES);
273 tprintsigmask_val("", tcp->u_arg[0]);
275 else if (!syserror(tcp)) {
276 tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
277 return RVAL_HEX | RVAL_STR;
282 struct old_sigaction {
283 /* sa_handler may be a libc #define, need to use other name: */
285 unsigned int sa_flags;
286 unsigned long sa_handler__;
287 /* Kernel treats sa_mask as an array of longs. */
288 unsigned long sa_mask[NSIG / sizeof(long)];
290 unsigned long sa_handler__;
291 unsigned long sa_mask;
292 unsigned long sa_flags;
295 unsigned long sa_restorer;
299 struct old_sigaction32 {
300 /* sa_handler may be a libc #define, need to use other name: */
301 uint32_t sa_handler__;
305 uint32_t sa_restorer;
310 decode_old_sigaction(struct tcb *const tcp, const kernel_ulong_t addr)
312 struct old_sigaction sa;
314 #ifndef current_wordsize
315 if (current_wordsize < sizeof(sa.sa_handler__)) {
316 struct old_sigaction32 sa32;
318 if (umove_or_printaddr(tcp, addr, &sa32))
321 memset(&sa, 0, sizeof(sa));
322 sa.sa_handler__ = sa32.sa_handler__;
323 sa.sa_flags = sa32.sa_flags;
324 #if HAVE_SA_RESTORER && defined SA_RESTORER
325 sa.sa_restorer = sa32.sa_restorer;
327 sa.sa_mask = sa32.sa_mask;
330 if (umove_or_printaddr(tcp, addr, &sa))
333 tprints("{sa_handler=");
334 print_sa_handler(sa.sa_handler__);
335 tprints(", sa_mask=");
337 tprintsigmask_addr("", sa.sa_mask);
339 tprintsigmask_val("", sa.sa_mask);
341 tprints(", sa_flags=");
342 printflags(sigact_flags, sa.sa_flags, "SA_???");
343 #if HAVE_SA_RESTORER && defined SA_RESTORER
344 if (sa.sa_flags & SA_RESTORER) {
345 tprints(", sa_restorer=");
346 printaddr(sa.sa_restorer);
355 printsignal(tcp->u_arg[0]);
357 decode_old_sigaction(tcp, tcp->u_arg[1]);
360 decode_old_sigaction(tcp, tcp->u_arg[2]);
367 printsignal(tcp->u_arg[0]);
369 print_sa_handler(tcp->u_arg[1]);
371 } else if (!syserror(tcp)) {
372 tcp->auxstr = get_sa_handler_str(tcp->u_rval);
373 return RVAL_HEX | RVAL_STR;
381 tcp->auxstr = sprintsigmask_val("mask ", tcp->u_rval);
383 return RVAL_HEX | RVAL_STR;
388 tprintsigmask_val("", tcp->u_arg[2]);
393 /* "Old" sigprocmask, which operates with word-sized signal masks */
394 SYS_FUNC(sigprocmask)
399 * Alpha/OSF is different: it doesn't pass in two pointers,
400 * but rather passes in the new bitmask as an argument and
401 * then returns the old bitmask. This "works" because we
402 * only have 64 signals to worry about. If you want more,
403 * use of the rt_sigprocmask syscall is required.
405 * old = osf_sigprocmask(how, new);
407 * ret = sigprocmask(how, &new, &old, ...);
409 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
410 tprintsigmask_val(", ", tcp->u_arg[1]);
412 else if (!syserror(tcp)) {
413 tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
414 return RVAL_HEX | RVAL_STR;
418 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
420 print_sigset_addr_len(tcp, tcp->u_arg[1], current_wordsize);
424 print_sigset_addr_len(tcp, tcp->u_arg[2], current_wordsize);
434 signame(tcp->u_arg[1]));
441 tprintf("%d, %d, %s",
444 signame(tcp->u_arg[2]));
452 print_sigset_addr_len(tcp, tcp->u_arg[0], current_wordsize);
456 SYS_FUNC(rt_sigprocmask)
458 /* Note: arg[3] is the length of the sigset. Kernel requires NSIG_BYTES */
460 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
462 print_sigset_addr_len(tcp, tcp->u_arg[1], tcp->u_arg[3]);
466 print_sigset_addr_len(tcp, tcp->u_arg[2], tcp->u_arg[3]);
467 tprintf(", %" PRI_klu, tcp->u_arg[3]);
472 /* Structure describing the action to be taken when a signal arrives. */
475 /* sa_handler may be a libc #define, need to use other name: */
477 unsigned int sa_flags;
478 unsigned long sa_handler__;
480 unsigned long sa_handler__;
481 unsigned long sa_flags;
484 unsigned long sa_restorer;
486 /* Kernel treats sa_mask as an array of longs. */
487 unsigned long sa_mask[NSIG / sizeof(long)];
489 /* Same for i386-on-x86_64 and similar cases */
490 struct new_sigaction32
492 uint32_t sa_handler__;
495 uint32_t sa_restorer;
497 uint32_t sa_mask[2 * (NSIG / sizeof(long))];
501 decode_new_sigaction(struct tcb *const tcp, const kernel_ulong_t addr)
503 struct new_sigaction sa;
505 #ifndef current_wordsize
506 if (current_wordsize < sizeof(sa.sa_handler__)) {
507 struct new_sigaction32 sa32;
509 if (umove_or_printaddr(tcp, addr, &sa32))
512 memset(&sa, 0, sizeof(sa));
513 sa.sa_handler__ = sa32.sa_handler__;
514 sa.sa_flags = sa32.sa_flags;
515 #if HAVE_SA_RESTORER && defined SA_RESTORER
516 sa.sa_restorer = sa32.sa_restorer;
518 /* Kernel treats sa_mask as an array of longs.
519 * For 32-bit process, "long" is uint32_t, thus, for example,
520 * 32th bit in sa_mask will end up as bit 0 in sa_mask[1].
521 * But for (64-bit) kernel, 32th bit in sa_mask is
522 * 32th bit in 0th (64-bit) long!
523 * For little-endian, it's the same.
524 * For big-endian, we swap 32-bit words.
526 sa.sa_mask[0] = ULONG_LONG(sa32.sa_mask[0], sa32.sa_mask[1]);
529 if (umove_or_printaddr(tcp, addr, &sa))
532 tprints("{sa_handler=");
533 print_sa_handler(sa.sa_handler__);
534 tprints(", sa_mask=");
536 * Sigset size is in tcp->u_arg[4] (SPARC)
537 * or in tcp->u_arg[3] (all other),
538 * but kernel won't handle sys_rt_sigaction
539 * with wrong sigset size (just returns EINVAL instead).
540 * We just fetch the right size, which is NSIG_BYTES.
542 tprintsigmask_val("", sa.sa_mask);
543 tprints(", sa_flags=");
545 printflags(sigact_flags, sa.sa_flags, "SA_???");
546 #if HAVE_SA_RESTORER && defined SA_RESTORER
547 if (sa.sa_flags & SA_RESTORER) {
548 tprints(", sa_restorer=");
549 printaddr(sa.sa_restorer);
555 SYS_FUNC(rt_sigaction)
558 printsignal(tcp->u_arg[0]);
560 decode_new_sigaction(tcp, tcp->u_arg[1]);
563 decode_new_sigaction(tcp, tcp->u_arg[2]);
564 #if defined(SPARC) || defined(SPARC64)
565 tprintf(", %#" PRI_klx ", %" PRI_klu, tcp->u_arg[3], tcp->u_arg[4]);
567 tprintf(", %" PRI_klu ", %#" PRI_klx, tcp->u_arg[3], tcp->u_arg[4]);
569 tprintf(", %" PRI_klu, tcp->u_arg[3]);
575 SYS_FUNC(rt_sigpending)
579 * One of the few syscalls where sigset size (arg[1])
580 * is allowed to be <= NSIG_BYTES, not strictly ==.
581 * This allows non-rt sigpending() syscall
582 * to reuse rt_sigpending() code in kernel.
584 print_sigset_addr_len_limit(tcp, tcp->u_arg[0],
586 tprintf(", %" PRI_klu, tcp->u_arg[1]);
591 SYS_FUNC(rt_sigsuspend)
593 /* NB: kernel requires arg[1] == NSIG_BYTES */
594 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[1]);
595 tprintf(", %" PRI_klu, tcp->u_arg[1]);
601 print_sigqueueinfo(struct tcb *const tcp, const int sig,
602 const kernel_ulong_t addr)
606 printsiginfo_at(tcp, addr);
609 SYS_FUNC(rt_sigqueueinfo)
611 tprintf("%d, ", (int) tcp->u_arg[0]);
612 print_sigqueueinfo(tcp, tcp->u_arg[1], tcp->u_arg[2]);
617 SYS_FUNC(rt_tgsigqueueinfo)
619 tprintf("%d, %d, ", (int) tcp->u_arg[0], (int) tcp->u_arg[1]);
620 print_sigqueueinfo(tcp, tcp->u_arg[2], tcp->u_arg[3]);
625 SYS_FUNC(rt_sigtimedwait)
627 /* NB: kernel requires arg[3] == NSIG_BYTES */
629 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[3]);
631 if (!(tcp->u_arg[1] && verbose(tcp))) {
633 * This is the only "return" parameter,
634 * if we are not going to fetch it on exit,
635 * decode all parameters on entry.
637 printaddr(tcp->u_arg[1]);
639 print_timespec(tcp, tcp->u_arg[2]);
640 tprintf(", %" PRI_klu, tcp->u_arg[3]);
642 char *sts = xstrdup(sprint_timespec(tcp, tcp->u_arg[2]));
643 set_tcb_priv_data(tcp, sts, free);
646 if (tcp->u_arg[1] && verbose(tcp)) {
647 printsiginfo_at(tcp, tcp->u_arg[1]);
649 tprints(get_tcb_priv_data(tcp));
650 tprintf(", %" PRI_klu, tcp->u_arg[3]);
653 if (!syserror(tcp) && tcp->u_rval) {
654 tcp->auxstr = signame(tcp->u_rval);
661 SYS_FUNC(restart_syscall)
663 tprintf("<... resuming interrupted %s ...>",
664 tcp->s_prev_ent ? tcp->s_prev_ent->sys_name : "system call");