]> granicus.if.org Git - strace/blob - signal.c
Improve SI_TIMER decoding
[strace] / signal.c
1 /*
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  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "defs.h"
35 #include <sys/user.h>
36 #include <fcntl.h>
37
38 #ifdef HAVE_SYS_REG_H
39 # include <sys/reg.h>
40 #elif defined(HAVE_LINUX_PTRACE_H)
41 # undef PTRACE_SYSCALL
42 # ifdef HAVE_STRUCT_IA64_FPREG
43 #  define ia64_fpreg XXX_ia64_fpreg
44 # endif
45 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
46 #  define pt_all_user_regs XXX_pt_all_user_regs
47 # endif
48 # ifdef HAVE_STRUCT_PTRACE_PEEKSIGINFO_ARGS
49 #  define ptrace_peeksiginfo_args XXX_ptrace_peeksiginfo_args
50 # endif
51 # include <linux/ptrace.h>
52 # undef ptrace_peeksiginfo_args
53 # undef ia64_fpreg
54 # undef pt_all_user_regs
55 #endif
56
57 #ifdef IA64
58 # include <asm/ptrace_offsets.h>
59 #endif
60
61 #if defined(SPARC) || defined(SPARC64) || defined(MIPS)
62 typedef struct {
63         struct pt_regs          si_regs;
64         int                     si_mask;
65 } m_siginfo_t;
66 #elif defined HAVE_ASM_SIGCONTEXT_H
67 # if !defined(IA64) && !defined(X86_64) && !defined(X32)
68 #  include <asm/sigcontext.h>
69 # endif
70 #else /* !HAVE_ASM_SIGCONTEXT_H */
71 # if defined M68K && !defined HAVE_STRUCT_SIGCONTEXT
72 struct sigcontext {
73         unsigned long sc_mask;
74         unsigned long sc_usp;
75         unsigned long sc_d0;
76         unsigned long sc_d1;
77         unsigned long sc_a0;
78         unsigned long sc_a1;
79         unsigned short sc_sr;
80         unsigned long sc_pc;
81         unsigned short sc_formatvec;
82 };
83 # endif /* M68K */
84 #endif /* !HAVE_ASM_SIGCONTEXT_H */
85
86 #ifndef NSIG
87 # warning: NSIG is not defined, using 32
88 # define NSIG 32
89 #elif NSIG < 32
90 # error: NSIG < 32
91 #endif
92
93 #ifdef HAVE_SIGACTION
94
95 /* The libc headers do not define this constant since it should only be
96    used by the implementation.  So we define it here.  */
97 #ifndef SA_RESTORER
98 # ifdef ASM_SA_RESTORER
99 #  define SA_RESTORER ASM_SA_RESTORER
100 # endif
101 #endif
102
103 static const struct xlat sigact_flags[] = {
104 #ifdef SA_RESTORER
105         XLAT(SA_RESTORER),
106 #endif
107 #ifdef SA_STACK
108         XLAT(SA_STACK),
109 #endif
110 #ifdef SA_RESTART
111         XLAT(SA_RESTART),
112 #endif
113 #ifdef SA_INTERRUPT
114         XLAT(SA_INTERRUPT),
115 #endif
116 #ifdef SA_NODEFER
117         XLAT(SA_NODEFER),
118 #endif
119 #if defined SA_NOMASK && SA_NODEFER != SA_NOMASK
120         XLAT(SA_NOMASK),
121 #endif
122 #ifdef SA_RESETHAND
123         XLAT(SA_RESETHAND),
124 #endif
125 #if defined SA_ONESHOT && SA_ONESHOT != SA_RESETHAND
126         XLAT(SA_ONESHOT),
127 #endif
128 #ifdef SA_SIGINFO
129         XLAT(SA_SIGINFO),
130 #endif
131 #ifdef SA_RESETHAND
132         XLAT(SA_RESETHAND),
133 #endif
134 #ifdef SA_ONSTACK
135         XLAT(SA_ONSTACK),
136 #endif
137 #ifdef SA_NODEFER
138         XLAT(SA_NODEFER),
139 #endif
140 #ifdef SA_NOCLDSTOP
141         XLAT(SA_NOCLDSTOP),
142 #endif
143 #ifdef SA_NOCLDWAIT
144         XLAT(SA_NOCLDWAIT),
145 #endif
146 #ifdef _SA_BSDCALL
147         XLAT(_SA_BSDCALL),
148 #endif
149 #ifdef SA_NOPTRACE
150         XLAT(SA_NOPTRACE),
151 #endif
152         XLAT_END
153 };
154
155 static const struct xlat sigprocmaskcmds[] = {
156         XLAT(SIG_BLOCK),
157         XLAT(SIG_UNBLOCK),
158         XLAT(SIG_SETMASK),
159 #ifdef SIG_SETMASK32
160         XLAT(SIG_SETMASK32),
161 #endif
162         XLAT_END
163 };
164
165 #endif /* HAVE_SIGACTION */
166
167 /* Anonymous realtime signals. */
168 /* Under glibc 2.1, SIGRTMIN et al are functions, but __SIGRTMIN is a
169    constant.  This is what we want.  Otherwise, just use SIGRTMIN. */
170 #ifdef SIGRTMIN
171 #ifndef __SIGRTMIN
172 #define __SIGRTMIN SIGRTMIN
173 #define __SIGRTMAX SIGRTMAX /* likewise */
174 #endif
175 #endif
176
177 /* Note on the size of sigset_t:
178  *
179  * In glibc, sigset_t is an array with space for 1024 bits (!),
180  * even though all arches supported by Linux have only 64 signals
181  * except MIPS, which has 128. IOW, it is 128 bytes long.
182  *
183  * In-kernel sigset_t is sized correctly (it is either 64 or 128 bit long).
184  * However, some old syscall return only 32 lower bits (one word).
185  * Example: sys_sigpending vs sys_rt_sigpending.
186  *
187  * Be aware of this fact when you try to
188  *     memcpy(&tcp->u_arg[1], &something, sizeof(sigset_t))
189  * - sizeof(sigset_t) is much bigger than you think,
190  * it may overflow tcp->u_arg[] array, and it may try to copy more data
191  * than is really available in <something>.
192  * Similarly,
193  *     umoven(tcp, addr, sizeof(sigset_t), &sigset)
194  * may be a bad idea: it'll try to read much more data than needed
195  * to fetch a sigset_t.
196  * Use (NSIG / 8) as a size instead.
197  */
198
199 const char *
200 signame(int sig)
201 {
202         static char buf[sizeof("SIGRT_%d") + sizeof(int)*3];
203
204         if (sig >= 0 && sig < nsignals)
205                 return signalent[sig];
206 #ifdef SIGRTMIN
207         if (sig >= __SIGRTMIN && sig <= __SIGRTMAX) {
208                 sprintf(buf, "SIGRT_%d", (int)(sig - __SIGRTMIN));
209                 return buf;
210         }
211 #endif
212         sprintf(buf, "%d", sig);
213         return buf;
214 }
215
216 static unsigned int
217 popcount32(const uint32_t *a, unsigned int size)
218 {
219         unsigned int count = 0;
220
221         for (; size; ++a, --size) {
222                 uint32_t x = *a;
223
224 #ifdef HAVE___BUILTIN_POPCOUNT
225                 count += __builtin_popcount(x);
226 #else
227                 for (; x; ++count)
228                         x &= x - 1;
229 #endif
230         }
231
232         return count;
233 }
234
235 static const char *
236 sprintsigmask_n(const char *prefix, const void *sig_mask, unsigned int bytes)
237 {
238         /*
239          * The maximum number of signal names to be printed is NSIG * 2 / 3.
240          * Most of signal names have length 7,
241          * average length of signal names is less than 7.
242          * The length of prefix string does not exceed 16.
243          */
244         static char outstr[128 + 8 * (NSIG * 2 / 3)];
245
246         char *s;
247         const uint32_t *mask;
248         uint32_t inverted_mask[NSIG / 32];
249         unsigned int size;
250         int i;
251         char sep;
252
253         s = stpcpy(outstr, prefix);
254
255         mask = sig_mask;
256         /* length of signal mask in 4-byte words */
257         size = (bytes >= NSIG / 8) ? NSIG / 32 : (bytes + 3) / 4;
258
259         /* check whether 2/3 or more bits are set */
260         if (popcount32(mask, size) >= size * 32 * 2 / 3) {
261                 /* show those signals that are NOT in the mask */
262                 unsigned int j;
263                 for (j = 0; j < size; ++j)
264                         inverted_mask[j] = ~mask[j];
265                 mask = inverted_mask;
266                 *s++ = '~';
267         }
268
269         sep = '[';
270         for (i = 0; (i = next_set_bit(mask, i, size * 32)) >= 0; ) {
271                 ++i;
272                 *s++ = sep;
273                 if (i < nsignals) {
274                         s = stpcpy(s, signalent[i] + 3);
275                 }
276 #ifdef SIGRTMIN
277                 else if (i >= __SIGRTMIN && i <= __SIGRTMAX) {
278                         s += sprintf(s, "RT_%u", i - __SIGRTMIN);
279                 }
280 #endif
281                 else {
282                         s += sprintf(s, "%u", i);
283                 }
284                 sep = ' ';
285         }
286         if (sep == '[')
287                 *s++ = sep;
288         *s++ = ']';
289         *s = '\0';
290         return outstr;
291 }
292
293 #define tprintsigmask_addr(prefix, mask) \
294         tprints(sprintsigmask_n((prefix), (mask), sizeof(mask)))
295
296 #define sprintsigmask_val(prefix, mask) \
297         sprintsigmask_n((prefix), &(mask), sizeof(mask))
298
299 #define tprintsigmask_val(prefix, mask) \
300         tprints(sprintsigmask_n((prefix), &(mask), sizeof(mask)))
301
302 void
303 printsignal(int nr)
304 {
305         tprints(signame(nr));
306 }
307
308 void
309 print_sigset_addr_len(struct tcb *tcp, long addr, long len)
310 {
311         char mask[NSIG / 8];
312
313         if (!addr) {
314                 tprints("NULL");
315                 return;
316         }
317         /* Here len is usually equals NSIG / 8 or current_wordsize.
318          * But we code this defensively:
319          */
320         if (len < 0) {
321  bad:
322                 tprintf("%#lx", addr);
323                 return;
324         }
325         if (len >= NSIG / 8)
326                 len = NSIG / 8;
327         else
328                 len = (len + 3) & ~3;
329
330         if (umoven(tcp, addr, len, mask) < 0)
331                 goto bad;
332         tprints(sprintsigmask_n("", mask, len));
333 }
334
335 #ifndef ILL_ILLOPC
336 #define ILL_ILLOPC      1       /* illegal opcode */
337 #define ILL_ILLOPN      2       /* illegal operand */
338 #define ILL_ILLADR      3       /* illegal addressing mode */
339 #define ILL_ILLTRP      4       /* illegal trap */
340 #define ILL_PRVOPC      5       /* privileged opcode */
341 #define ILL_PRVREG      6       /* privileged register */
342 #define ILL_COPROC      7       /* coprocessor error */
343 #define ILL_BADSTK      8       /* internal stack error */
344 #define FPE_INTDIV      1       /* integer divide by zero */
345 #define FPE_INTOVF      2       /* integer overflow */
346 #define FPE_FLTDIV      3       /* floating point divide by zero */
347 #define FPE_FLTOVF      4       /* floating point overflow */
348 #define FPE_FLTUND      5       /* floating point underflow */
349 #define FPE_FLTRES      6       /* floating point inexact result */
350 #define FPE_FLTINV      7       /* floating point invalid operation */
351 #define FPE_FLTSUB      8       /* subscript out of range */
352 #define SEGV_MAPERR     1       /* address not mapped to object */
353 #define SEGV_ACCERR     2       /* invalid permissions for mapped object */
354 #define BUS_ADRALN      1       /* invalid address alignment */
355 #define BUS_ADRERR      2       /* non-existant physical address */
356 #define BUS_OBJERR      3       /* object specific hardware error */
357 #define TRAP_BRKPT      1       /* process breakpoint */
358 #define TRAP_TRACE      2       /* process trace trap */
359 #define CLD_EXITED      1       /* child has exited */
360 #define CLD_KILLED      2       /* child was killed */
361 #define CLD_DUMPED      3       /* child terminated abnormally */
362 #define CLD_TRAPPED     4       /* traced child has trapped */
363 #define CLD_STOPPED     5       /* child has stopped */
364 #define CLD_CONTINUED   6       /* stopped child has continued */
365 #define POLL_IN         1       /* data input available */
366 #define POLL_OUT        2       /* output buffers available */
367 #define POLL_MSG        3       /* input message available */
368 #define POLL_ERR        4       /* i/o error */
369 #define POLL_PRI        5       /* high priority input available */
370 #define POLL_HUP        6       /* device disconnected */
371 #define SI_KERNEL       0x80    /* sent by kernel */
372 #define SI_USER         0       /* sent by kill, sigsend, raise */
373 #define SI_QUEUE        -1      /* sent by sigqueue */
374 #define SI_TIMER        -2      /* sent by timer expiration */
375 #define SI_MESGQ        -3      /* sent by real time mesq state change */
376 #define SI_ASYNCIO      -4      /* sent by AIO completion */
377 #define SI_SIGIO        -5      /* sent by SIGIO */
378 #define SI_TKILL        -6      /* sent by tkill */
379 #define SI_ASYNCNL      -60     /* sent by asynch name lookup completion */
380 #endif
381
382 #ifndef SI_FROMUSER
383 # define SI_FROMUSER(sip)       ((sip)->si_code <= 0)
384 #endif
385
386 static const struct xlat siginfo_codes[] = {
387 #ifdef SI_KERNEL
388         XLAT(SI_KERNEL),
389 #endif
390 #ifdef SI_USER
391         XLAT(SI_USER),
392 #endif
393 #ifdef SI_QUEUE
394         XLAT(SI_QUEUE),
395 #endif
396 #ifdef SI_TIMER
397         XLAT(SI_TIMER),
398 #endif
399 #ifdef SI_MESGQ
400         XLAT(SI_MESGQ),
401 #endif
402 #ifdef SI_ASYNCIO
403         XLAT(SI_ASYNCIO),
404 #endif
405 #ifdef SI_SIGIO
406         XLAT(SI_SIGIO),
407 #endif
408 #ifdef SI_TKILL
409         XLAT(SI_TKILL),
410 #endif
411 #ifdef SI_ASYNCNL
412         XLAT(SI_ASYNCNL),
413 #endif
414 #ifdef SI_NOINFO
415         XLAT(SI_NOINFO),
416 #endif
417 #ifdef SI_LWP
418         XLAT(SI_LWP),
419 #endif
420         XLAT_END
421 };
422
423 static const struct xlat sigill_codes[] = {
424         XLAT(ILL_ILLOPC),
425         XLAT(ILL_ILLOPN),
426         XLAT(ILL_ILLADR),
427         XLAT(ILL_ILLTRP),
428         XLAT(ILL_PRVOPC),
429         XLAT(ILL_PRVREG),
430         XLAT(ILL_COPROC),
431         XLAT(ILL_BADSTK),
432         XLAT_END
433 };
434
435 static const struct xlat sigfpe_codes[] = {
436         XLAT(FPE_INTDIV),
437         XLAT(FPE_INTOVF),
438         XLAT(FPE_FLTDIV),
439         XLAT(FPE_FLTOVF),
440         XLAT(FPE_FLTUND),
441         XLAT(FPE_FLTRES),
442         XLAT(FPE_FLTINV),
443         XLAT(FPE_FLTSUB),
444         XLAT_END
445 };
446
447 static const struct xlat sigtrap_codes[] = {
448         XLAT(TRAP_BRKPT),
449         XLAT(TRAP_TRACE),
450         XLAT_END
451 };
452
453 static const struct xlat sigchld_codes[] = {
454         XLAT(CLD_EXITED),
455         XLAT(CLD_KILLED),
456         XLAT(CLD_DUMPED),
457         XLAT(CLD_TRAPPED),
458         XLAT(CLD_STOPPED),
459         XLAT(CLD_CONTINUED),
460         XLAT_END
461 };
462
463 static const struct xlat sigpoll_codes[] = {
464         XLAT(POLL_IN),
465         XLAT(POLL_OUT),
466         XLAT(POLL_MSG),
467         XLAT(POLL_ERR),
468         XLAT(POLL_PRI),
469         XLAT(POLL_HUP),
470         XLAT_END
471 };
472
473 static const struct xlat sigprof_codes[] = {
474 #ifdef PROF_SIG
475         XLAT(PROF_SIG),
476 #endif
477         XLAT_END
478 };
479
480 #ifdef SIGEMT
481 static const struct xlat sigemt_codes[] = {
482 #ifdef EMT_TAGOVF
483         XLAT(EMT_TAGOVF),
484 #endif
485         XLAT_END
486 };
487 #endif
488
489 static const struct xlat sigsegv_codes[] = {
490         XLAT(SEGV_MAPERR),
491         XLAT(SEGV_ACCERR),
492         XLAT_END
493 };
494
495 static const struct xlat sigbus_codes[] = {
496         XLAT(BUS_ADRALN),
497         XLAT(BUS_ADRERR),
498         XLAT(BUS_OBJERR),
499         XLAT_END
500 };
501
502 static void
503 printsigval(siginfo_t *sip, int verbose)
504 {
505         if (!verbose)
506                 tprints(", ...");
507         else
508                 tprintf(", si_value={int=%u, ptr=%#lx}",
509                         sip->si_int,
510                         (unsigned long) sip->si_ptr);
511 }
512
513 void
514 printsiginfo(siginfo_t *sip, int verbose)
515 {
516         const char *code;
517
518         if (sip->si_signo == 0) {
519                 tprints("{}");
520                 return;
521         }
522         tprints("{si_signo=");
523         printsignal(sip->si_signo);
524         code = xlookup(siginfo_codes, sip->si_code);
525         if (!code) {
526                 switch (sip->si_signo) {
527                 case SIGTRAP:
528                         code = xlookup(sigtrap_codes, sip->si_code);
529                         break;
530                 case SIGCHLD:
531                         code = xlookup(sigchld_codes, sip->si_code);
532                         break;
533                 case SIGPOLL:
534                         code = xlookup(sigpoll_codes, sip->si_code);
535                         break;
536                 case SIGPROF:
537                         code = xlookup(sigprof_codes, sip->si_code);
538                         break;
539                 case SIGILL:
540                         code = xlookup(sigill_codes, sip->si_code);
541                         break;
542 #ifdef SIGEMT
543                 case SIGEMT:
544                         code = xlookup(sigemt_codes, sip->si_code);
545                         break;
546 #endif
547                 case SIGFPE:
548                         code = xlookup(sigfpe_codes, sip->si_code);
549                         break;
550                 case SIGSEGV:
551                         code = xlookup(sigsegv_codes, sip->si_code);
552                         break;
553                 case SIGBUS:
554                         code = xlookup(sigbus_codes, sip->si_code);
555                         break;
556                 }
557         }
558         if (code)
559                 tprintf(", si_code=%s", code);
560         else
561                 tprintf(", si_code=%#x", sip->si_code);
562 #ifdef SI_NOINFO
563         if (sip->si_code != SI_NOINFO)
564 #endif
565         {
566                 if (sip->si_errno) {
567                         if (sip->si_errno < 0 || sip->si_errno >= nerrnos)
568                                 tprintf(", si_errno=%d", sip->si_errno);
569                         else
570                                 tprintf(", si_errno=%s",
571                                         errnoent[sip->si_errno]);
572                 }
573 #ifdef SI_FROMUSER
574                 if (SI_FROMUSER(sip)) {
575                         switch (sip->si_code) {
576 #ifdef SI_USER
577                         case SI_USER:
578                                 tprintf(", si_pid=%lu, si_uid=%lu",
579                                         (unsigned long) sip->si_pid,
580                                         (unsigned long) sip->si_uid);
581                                 break;
582 #endif
583 #ifdef SI_TKILL
584                         case SI_TKILL:
585                                 tprintf(", si_pid=%lu, si_uid=%lu",
586                                         (unsigned long) sip->si_pid,
587                                         (unsigned long) sip->si_uid);
588                                 break;
589 #endif
590 #ifdef SI_TIMER
591                         case SI_TIMER:
592                                 tprintf(", si_timerid=%#x, si_overrun=%d",
593                                         sip->si_timerid, sip->si_overrun);
594                                 printsigval(sip, verbose);
595                                 break;
596 #endif
597                         default:
598                                 tprintf(", si_pid=%lu, si_uid=%lu",
599                                         (unsigned long) sip->si_pid,
600                                         (unsigned long) sip->si_uid);
601                                 if (!sip->si_ptr)
602                                         break;
603                                 printsigval(sip, verbose);
604                                 break;
605                         }
606                 }
607                 else
608 #endif /* SI_FROMUSER */
609                 {
610                         switch (sip->si_signo) {
611                         case SIGCHLD:
612                                 tprintf(", si_pid=%ld, si_status=",
613                                         (long) sip->si_pid);
614                                 if (sip->si_code == CLD_EXITED)
615                                         tprintf("%d", sip->si_status);
616                                 else
617                                         printsignal(sip->si_status);
618                                 if (!verbose)
619                                         tprints(", ...");
620                                 else
621                                         tprintf(", si_utime=%llu, si_stime=%llu",
622                                                 (unsigned long long) sip->si_utime,
623                                                 (unsigned long long) sip->si_stime);
624                                 break;
625                         case SIGILL: case SIGFPE:
626                         case SIGSEGV: case SIGBUS:
627                                 tprintf(", si_addr=%#lx",
628                                         (unsigned long) sip->si_addr);
629                                 break;
630                         case SIGPOLL:
631                                 switch (sip->si_code) {
632                                 case POLL_IN: case POLL_OUT: case POLL_MSG:
633                                         tprintf(", si_band=%ld",
634                                                 (long) sip->si_band);
635                                         break;
636                                 }
637                                 break;
638                         default:
639                                 if (sip->si_pid || sip->si_uid)
640                                         tprintf(", si_pid=%lu, si_uid=%lu",
641                                                 (unsigned long) sip->si_pid,
642                                                 (unsigned long) sip->si_uid);
643                                 if (!sip->si_ptr)
644                                         break;
645                                 printsigval(sip, verbose);
646
647                         }
648                 }
649         }
650         tprints("}");
651 }
652
653 void
654 printsiginfo_at(struct tcb *tcp, long addr)
655 {
656         siginfo_t si;
657         if (!addr) {
658                 tprints("NULL");
659                 return;
660         }
661         if (syserror(tcp)) {
662                 tprintf("%#lx", addr);
663                 return;
664         }
665         if (umove(tcp, addr, &si) < 0) {
666                 tprints("{???}");
667                 return;
668         }
669         printsiginfo(&si, verbose(tcp));
670 }
671
672 int
673 sys_sigsetmask(struct tcb *tcp)
674 {
675         if (entering(tcp)) {
676                 tprintsigmask_val("", tcp->u_arg[0]);
677         }
678         else if (!syserror(tcp)) {
679                 tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
680                 return RVAL_HEX | RVAL_STR;
681         }
682         return 0;
683 }
684
685 #ifdef HAVE_SIGACTION
686
687 struct old_sigaction {
688         /* sa_handler may be a libc #define, need to use other name: */
689 #ifdef MIPS
690         unsigned int sa_flags;
691         void (*__sa_handler)(int);
692         /* Kernel treats sa_mask as an array of longs. */
693         unsigned long sa_mask[NSIG / sizeof(long) ? NSIG / sizeof(long) : 1];
694 #else
695         void (*__sa_handler)(int);
696         unsigned long sa_mask;
697         unsigned long sa_flags;
698         void (*sa_restorer)(void);
699 #endif /* !MIPS */
700 };
701
702 struct old_sigaction32 {
703         /* sa_handler may be a libc #define, need to use other name: */
704         uint32_t __sa_handler;
705         uint32_t sa_mask;
706         uint32_t sa_flags;
707         uint32_t sa_restorer;
708 };
709
710 static void
711 decode_old_sigaction(struct tcb *tcp, long addr)
712 {
713         struct old_sigaction sa;
714         int r;
715
716         if (!addr) {
717                 tprints("NULL");
718                 return;
719         }
720         if (!verbose(tcp) || (exiting(tcp) && syserror(tcp))) {
721                 tprintf("%#lx", addr);
722                 return;
723         }
724
725 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
726         if (current_wordsize != sizeof(sa.__sa_handler) && current_wordsize == 4) {
727                 struct old_sigaction32 sa32;
728                 r = umove(tcp, addr, &sa32);
729                 if (r >= 0) {
730                         memset(&sa, 0, sizeof(sa));
731                         sa.__sa_handler = (void*)(uintptr_t)sa32.__sa_handler;
732                         sa.sa_flags = sa32.sa_flags;
733                         sa.sa_restorer = (void*)(uintptr_t)sa32.sa_restorer;
734                         sa.sa_mask = sa32.sa_mask;
735                 }
736         } else
737 #endif
738         {
739                 r = umove(tcp, addr, &sa);
740         }
741         if (r < 0) {
742                 tprints("{...}");
743                 return;
744         }
745
746         /* Architectures using function pointers, like
747          * hppa, may need to manipulate the function pointer
748          * to compute the result of a comparison. However,
749          * the __sa_handler function pointer exists only in
750          * the address space of the traced process, and can't
751          * be manipulated by strace. In order to prevent the
752          * compiler from generating code to manipulate
753          * __sa_handler we cast the function pointers to long. */
754         if ((long)sa.__sa_handler == (long)SIG_ERR)
755                 tprints("{SIG_ERR, ");
756         else if ((long)sa.__sa_handler == (long)SIG_DFL)
757                 tprints("{SIG_DFL, ");
758         else if ((long)sa.__sa_handler == (long)SIG_IGN)
759                 tprints("{SIG_IGN, ");
760         else
761                 tprintf("{%#lx, ", (long) sa.__sa_handler);
762 #ifdef MIPS
763         tprintsigmask_addr("", sa.sa_mask);
764 #else
765         tprintsigmask_val("", sa.sa_mask);
766 #endif
767         tprints(", ");
768         printflags(sigact_flags, sa.sa_flags, "SA_???");
769 #ifdef SA_RESTORER
770         if (sa.sa_flags & SA_RESTORER)
771                 tprintf(", %p", sa.sa_restorer);
772 #endif
773         tprints("}");
774 }
775
776 int
777 sys_sigaction(struct tcb *tcp)
778 {
779         if (entering(tcp)) {
780                 printsignal(tcp->u_arg[0]);
781                 tprints(", ");
782                 decode_old_sigaction(tcp, tcp->u_arg[1]);
783                 tprints(", ");
784         } else
785                 decode_old_sigaction(tcp, tcp->u_arg[2]);
786         return 0;
787 }
788
789 int
790 sys_signal(struct tcb *tcp)
791 {
792         if (entering(tcp)) {
793                 printsignal(tcp->u_arg[0]);
794                 tprints(", ");
795                 switch (tcp->u_arg[1]) {
796                 case (long) SIG_ERR:
797                         tprints("SIG_ERR");
798                         break;
799                 case (long) SIG_DFL:
800                         tprints("SIG_DFL");
801                         break;
802                 case (long) SIG_IGN:
803                         tprints("SIG_IGN");
804                         break;
805                 default:
806                         tprintf("%#lx", tcp->u_arg[1]);
807                 }
808                 return 0;
809         }
810         else if (!syserror(tcp)) {
811                 switch (tcp->u_rval) {
812                 case (long) SIG_ERR:
813                         tcp->auxstr = "SIG_ERR"; break;
814                 case (long) SIG_DFL:
815                         tcp->auxstr = "SIG_DFL"; break;
816                 case (long) SIG_IGN:
817                         tcp->auxstr = "SIG_IGN"; break;
818                 default:
819                         tcp->auxstr = NULL;
820                 }
821                 return RVAL_HEX | RVAL_STR;
822         }
823         return 0;
824 }
825
826 #endif /* HAVE_SIGACTION */
827
828 int
829 sys_sigreturn(struct tcb *tcp)
830 {
831 #if defined(ARM)
832         if (entering(tcp)) {
833                 struct arm_sigcontext {
834                         unsigned long trap_no;
835                         unsigned long error_code;
836                         unsigned long oldmask;
837                         unsigned long arm_r0;
838                         unsigned long arm_r1;
839                         unsigned long arm_r2;
840                         unsigned long arm_r3;
841                         unsigned long arm_r4;
842                         unsigned long arm_r5;
843                         unsigned long arm_r6;
844                         unsigned long arm_r7;
845                         unsigned long arm_r8;
846                         unsigned long arm_r9;
847                         unsigned long arm_r10;
848                         unsigned long arm_fp;
849                         unsigned long arm_ip;
850                         unsigned long arm_sp;
851                         unsigned long arm_lr;
852                         unsigned long arm_pc;
853                         unsigned long arm_cpsr;
854                         unsigned long fault_address;
855                 };
856                 struct arm_ucontext {
857                         unsigned long uc_flags;
858                         unsigned long uc_link;  /* struct ucontext* */
859                         /* The next three members comprise stack_t struct: */
860                         unsigned long ss_sp;    /* void*   */
861                         unsigned long ss_flags; /* int     */
862                         unsigned long ss_size;  /* size_t  */
863                         struct arm_sigcontext sc;
864                         /* These two members are sigset_t: */
865                         unsigned long uc_sigmask[2];
866                         /* more fields follow, which we aren't interested in */
867                 };
868                 struct arm_ucontext uc;
869                 if (umove(tcp, arm_regs.ARM_sp, &uc) < 0)
870                         return 0;
871                 /*
872                  * Kernel fills out uc.sc.oldmask too when it sets up signal stack,
873                  * but for sigmask restore, sigreturn syscall uses uc.uc_sigmask instead.
874                  */
875                 tprintsigmask_addr(") (mask ", uc.uc_sigmask);
876         }
877 #elif defined(S390) || defined(S390X)
878         if (entering(tcp)) {
879                 long usp;
880                 struct sigcontext sc;
881                 if (upeek(tcp->pid, PT_GPR15, &usp) < 0)
882                         return 0;
883                 if (umove(tcp, usp + __SIGNAL_FRAMESIZE, &sc) < 0)
884                         return 0;
885                 tprintsigmask_addr(") (mask ", sc.oldmask);
886         }
887 #elif defined(I386) || defined(X86_64)
888 # if defined(X86_64)
889         if (current_personality == 0) /* 64-bit */
890                 return 0;
891 # endif
892         if (entering(tcp)) {
893                 struct i386_sigcontext_struct {
894                         uint16_t gs, __gsh;
895                         uint16_t fs, __fsh;
896                         uint16_t es, __esh;
897                         uint16_t ds, __dsh;
898                         uint32_t edi;
899                         uint32_t esi;
900                         uint32_t ebp;
901                         uint32_t esp;
902                         uint32_t ebx;
903                         uint32_t edx;
904                         uint32_t ecx;
905                         uint32_t eax;
906                         uint32_t trapno;
907                         uint32_t err;
908                         uint32_t eip;
909                         uint16_t cs, __csh;
910                         uint32_t eflags;
911                         uint32_t esp_at_signal;
912                         uint16_t ss, __ssh;
913                         uint32_t i387;
914                         uint32_t oldmask;
915                         uint32_t cr2;
916                 };
917                 struct i386_fpstate {
918                         uint32_t cw;
919                         uint32_t sw;
920                         uint32_t tag;
921                         uint32_t ipoff;
922                         uint32_t cssel;
923                         uint32_t dataoff;
924                         uint32_t datasel;
925                         uint8_t  st[8][10]; /* 8*10 bytes: FP regs */
926                         uint16_t status;
927                         uint16_t magic;
928                         uint32_t fxsr_env[6];
929                         uint32_t mxcsr;
930                         uint32_t reserved;
931                         uint8_t  stx[8][16]; /* 8*16 bytes: FP regs, each padded to 16 bytes */
932                         uint8_t  xmm[8][16]; /* 8 XMM regs */
933                         uint32_t padding1[44];
934                         uint32_t padding2[12]; /* union with struct _fpx_sw_bytes */
935                 };
936                 struct {
937                         struct i386_sigcontext_struct sc;
938                         struct i386_fpstate fp;
939                         uint32_t extramask[1];
940                 } signal_stack;
941                 /* On i386, sc is followed on stack by struct fpstate
942                  * and after it an additional u32 extramask[1] which holds
943                  * upper half of the mask.
944                  */
945                 uint32_t sigmask[2];
946                 if (umove(tcp, *i386_esp_ptr, &signal_stack) < 0)
947                         return 0;
948                 sigmask[0] = signal_stack.sc.oldmask;
949                 sigmask[1] = signal_stack.extramask[0];
950                 tprintsigmask_addr(") (mask ", sigmask);
951         }
952 #elif defined(IA64)
953         if (entering(tcp)) {
954                 struct sigcontext sc;
955                 long sp;
956                 /* offset of sigcontext in the kernel's sigframe structure: */
957 #               define SIGFRAME_SC_OFFSET       0x90
958                 if (upeek(tcp->pid, PT_R12, &sp) < 0)
959                         return 0;
960                 if (umove(tcp, sp + 16 + SIGFRAME_SC_OFFSET, &sc) < 0)
961                         return 0;
962                 tprintsigmask_val(") (mask ", sc.sc_mask);
963         }
964 #elif defined(POWERPC)
965         if (entering(tcp)) {
966                 long esp;
967                 struct sigcontext sc;
968
969                 esp = ppc_regs.gpr[1];
970
971                 /* Skip dummy stack frame. */
972 #ifdef POWERPC64
973                 if (current_personality == 0)
974                         esp += 128;
975                 else
976                         esp += 64;
977 #else
978                 esp += 64;
979 #endif
980                 if (umove(tcp, esp, &sc) < 0)
981                         return 0;
982                 tprintsigmask_val(") (mask ", sc.oldmask);
983         }
984 #elif defined(M68K)
985         if (entering(tcp)) {
986                 long usp;
987                 struct sigcontext sc;
988                 if (upeek(tcp->pid, 4*PT_USP, &usp) < 0)
989                         return 0;
990                 if (umove(tcp, usp, &sc) < 0)
991                         return 0;
992                 tprintsigmask_val(") (mask ", sc.sc_mask);
993         }
994 #elif defined(ALPHA)
995         if (entering(tcp)) {
996                 long fp;
997                 struct sigcontext sc;
998                 if (upeek(tcp->pid, REG_FP, &fp) < 0)
999                         return 0;
1000                 if (umove(tcp, fp, &sc) < 0)
1001                         return 0;
1002                 tprintsigmask_val(") (mask ", sc.sc_mask);
1003         }
1004 #elif defined(SPARC) || defined(SPARC64)
1005         if (entering(tcp)) {
1006                 long i1;
1007                 m_siginfo_t si;
1008                 i1 = sparc_regs.u_regs[U_REG_O1];
1009                 if (umove(tcp, i1, &si) < 0) {
1010                         perror_msg("sigreturn: umove");
1011                         return 0;
1012                 }
1013                 tprintsigmask_val(") (mask ", si.si_mask);
1014         }
1015 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1016         /* This decodes rt_sigreturn.  The 64-bit ABIs do not have
1017            sigreturn.  */
1018         if (entering(tcp)) {
1019                 long sp;
1020                 struct ucontext uc;
1021                 if (upeek(tcp->pid, REG_SP, &sp) < 0)
1022                         return 0;
1023                 /* There are six words followed by a 128-byte siginfo.  */
1024                 sp = sp + 6 * 4 + 128;
1025                 if (umove(tcp, sp, &uc) < 0)
1026                         return 0;
1027                 tprintsigmask_val(") (mask ", uc.uc_sigmask);
1028         }
1029 #elif defined(MIPS)
1030         if (entering(tcp)) {
1031                 long sp;
1032                 struct pt_regs regs;
1033                 m_siginfo_t si;
1034                 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0) {
1035                         perror_msg("sigreturn: PTRACE_GETREGS");
1036                         return 0;
1037                 }
1038                 sp = regs.regs[29];
1039                 if (umove(tcp, sp, &si) < 0)
1040                         return 0;
1041                 tprintsigmask_val(") (mask ", si.si_mask);
1042         }
1043 #elif defined(CRISV10) || defined(CRISV32)
1044         if (entering(tcp)) {
1045                 struct sigcontext sc;
1046                 long regs[PT_MAX+1];
1047                 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long)regs) < 0) {
1048                         perror_msg("sigreturn: PTRACE_GETREGS");
1049                         return 0;
1050                 }
1051                 if (umove(tcp, regs[PT_USP], &sc) < 0)
1052                         return 0;
1053                 tprintsigmask_val(") (mask ", sc.oldmask);
1054         }
1055 #elif defined(TILE)
1056         if (entering(tcp)) {
1057                 struct ucontext uc;
1058
1059                 /* offset of ucontext in the kernel's sigframe structure */
1060 #               define SIGFRAME_UC_OFFSET C_ABI_SAVE_AREA_SIZE + sizeof(siginfo_t)
1061                 if (umove(tcp, tile_regs.sp + SIGFRAME_UC_OFFSET, &uc) < 0)
1062                         return 0;
1063                 tprintsigmask_val(") (mask ", uc.uc_sigmask);
1064         }
1065 #elif defined(MICROBLAZE)
1066         /* TODO: Verify that this is correct...  */
1067         if (entering(tcp)) {
1068                 struct sigcontext sc;
1069                 long sp;
1070                 /* Read r1, the stack pointer.  */
1071                 if (upeek(tcp->pid, 1 * 4, &sp) < 0)
1072                         return 0;
1073                 if (umove(tcp, sp, &sc) < 0)
1074                         return 0;
1075                 tprintsigmask_val(") (mask ", sc.oldmask);
1076         }
1077 #elif defined(XTENSA)
1078         /* Xtensa only has rt_sys_sigreturn */
1079 #elif defined(ARC)
1080         /* ARC syscall ABI only supports rt_sys_sigreturn */
1081 #else
1082 # warning No sys_sigreturn() for this architecture
1083 # warning         (no problem, just a reminder :-)
1084 #endif
1085         return 0;
1086 }
1087
1088 int
1089 sys_siggetmask(struct tcb *tcp)
1090 {
1091         if (exiting(tcp)) {
1092                 tcp->auxstr = sprintsigmask_val("mask ", tcp->u_rval);
1093         }
1094         return RVAL_HEX | RVAL_STR;
1095 }
1096
1097 int
1098 sys_sigsuspend(struct tcb *tcp)
1099 {
1100         if (entering(tcp)) {
1101                 tprintsigmask_val("", tcp->u_arg[2]);
1102         }
1103         return 0;
1104 }
1105
1106 #if !defined SS_ONSTACK
1107 #define SS_ONSTACK      1
1108 #define SS_DISABLE      2
1109 #endif
1110
1111 static const struct xlat sigaltstack_flags[] = {
1112         XLAT(SS_ONSTACK),
1113         XLAT(SS_DISABLE),
1114         XLAT_END
1115 };
1116
1117 static void
1118 print_stack_t(struct tcb *tcp, unsigned long addr)
1119 {
1120         stack_t ss;
1121         int r;
1122
1123         if (!addr) {
1124                 tprints("NULL");
1125                 return;
1126         }
1127
1128 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1129         if (current_wordsize != sizeof(ss.ss_sp) && current_wordsize == 4) {
1130                 struct {
1131                         uint32_t ss_sp;
1132                         int32_t ss_flags;
1133                         uint32_t ss_size;
1134                 } ss32;
1135                 r = umove(tcp, addr, &ss32);
1136                 if (r >= 0) {
1137                         memset(&ss, 0, sizeof(ss));
1138                         ss.ss_sp = (void*)(unsigned long) ss32.ss_sp;
1139                         ss.ss_flags = ss32.ss_flags;
1140                         ss.ss_size = (unsigned long) ss32.ss_size;
1141                 }
1142         } else
1143 #endif
1144         {
1145                 r = umove(tcp, addr, &ss);
1146         }
1147         if (r < 0) {
1148                 tprintf("%#lx", addr);
1149         } else {
1150                 tprintf("{ss_sp=%#lx, ss_flags=", (unsigned long) ss.ss_sp);
1151                 printflags(sigaltstack_flags, ss.ss_flags, "SS_???");
1152                 tprintf(", ss_size=%lu}", (unsigned long) ss.ss_size);
1153         }
1154 }
1155
1156 int
1157 sys_sigaltstack(struct tcb *tcp)
1158 {
1159         if (entering(tcp)) {
1160                 print_stack_t(tcp, tcp->u_arg[0]);
1161         }
1162         else {
1163                 tprints(", ");
1164                 print_stack_t(tcp, tcp->u_arg[1]);
1165         }
1166         return 0;
1167 }
1168
1169 #ifdef HAVE_SIGACTION
1170
1171 /* "Old" sigprocmask, which operates with word-sized signal masks */
1172 int
1173 sys_sigprocmask(struct tcb *tcp)
1174 {
1175 # ifdef ALPHA
1176         if (entering(tcp)) {
1177                 /*
1178                  * Alpha/OSF is different: it doesn't pass in two pointers,
1179                  * but rather passes in the new bitmask as an argument and
1180                  * then returns the old bitmask.  This "works" because we
1181                  * only have 64 signals to worry about.  If you want more,
1182                  * use of the rt_sigprocmask syscall is required.
1183                  * Alpha:
1184                  *      old = osf_sigprocmask(how, new);
1185                  * Everyone else:
1186                  *      ret = sigprocmask(how, &new, &old, ...);
1187                  */
1188                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1189                 tprintsigmask_val(", ", tcp->u_arg[1]);
1190         }
1191         else if (!syserror(tcp)) {
1192                 tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
1193                 return RVAL_HEX | RVAL_STR;
1194         }
1195 # else /* !ALPHA */
1196         if (entering(tcp)) {
1197                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1198                 tprints(", ");
1199                 print_sigset_addr_len(tcp, tcp->u_arg[1], current_wordsize);
1200                 tprints(", ");
1201         }
1202         else {
1203                 if (syserror(tcp))
1204                         tprintf("%#lx", tcp->u_arg[2]);
1205                 else
1206                         print_sigset_addr_len(tcp, tcp->u_arg[2], current_wordsize);
1207         }
1208 # endif /* !ALPHA */
1209         return 0;
1210 }
1211
1212 #endif /* HAVE_SIGACTION */
1213
1214 int
1215 sys_kill(struct tcb *tcp)
1216 {
1217         if (entering(tcp)) {
1218                 tprintf("%ld, %s",
1219                         widen_to_long(tcp->u_arg[0]),
1220                         signame(tcp->u_arg[1])
1221                 );
1222         }
1223         return 0;
1224 }
1225
1226 int
1227 sys_tgkill(struct tcb *tcp)
1228 {
1229         if (entering(tcp)) {
1230                 tprintf("%ld, %ld, %s",
1231                         widen_to_long(tcp->u_arg[0]),
1232                         widen_to_long(tcp->u_arg[1]),
1233                         signame(tcp->u_arg[2])
1234                 );
1235         }
1236         return 0;
1237 }
1238
1239 int
1240 sys_sigpending(struct tcb *tcp)
1241 {
1242         if (exiting(tcp)) {
1243                 if (syserror(tcp))
1244                         tprintf("%#lx", tcp->u_arg[0]);
1245                 else
1246                         print_sigset_addr_len(tcp, tcp->u_arg[0], current_wordsize);
1247         }
1248         return 0;
1249 }
1250
1251 int
1252 sys_rt_sigprocmask(struct tcb *tcp)
1253 {
1254         /* Note: arg[3] is the length of the sigset. Kernel requires NSIG / 8 */
1255         if (entering(tcp)) {
1256                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1257                 tprints(", ");
1258                 print_sigset_addr_len(tcp, tcp->u_arg[1], tcp->u_arg[3]);
1259                 tprints(", ");
1260         }
1261         else {
1262                 if (syserror(tcp))
1263                         tprintf("%#lx", tcp->u_arg[2]);
1264                 else
1265                         print_sigset_addr_len(tcp, tcp->u_arg[2], tcp->u_arg[3]);
1266                 tprintf(", %lu", tcp->u_arg[3]);
1267         }
1268         return 0;
1269 }
1270
1271 /* Structure describing the action to be taken when a signal arrives.  */
1272 struct new_sigaction
1273 {
1274         /* sa_handler may be a libc #define, need to use other name: */
1275 #ifdef MIPS
1276         unsigned int sa_flags;
1277         void (*__sa_handler)(int);
1278 #else
1279         void (*__sa_handler)(int);
1280         unsigned long sa_flags;
1281         void (*sa_restorer)(void);
1282 #endif /* !MIPS */
1283         /* Kernel treats sa_mask as an array of longs. */
1284         unsigned long sa_mask[NSIG / sizeof(long) ? NSIG / sizeof(long) : 1];
1285 };
1286 /* Same for i386-on-x86_64 and similar cases */
1287 struct new_sigaction32
1288 {
1289         uint32_t __sa_handler;
1290         uint32_t sa_flags;
1291         uint32_t sa_restorer;
1292         uint32_t sa_mask[2 * (NSIG / sizeof(long) ? NSIG / sizeof(long) : 1)];
1293 };
1294
1295 static void
1296 decode_new_sigaction(struct tcb *tcp, long addr)
1297 {
1298         struct new_sigaction sa;
1299         int r;
1300
1301         if (!addr) {
1302                 tprints("NULL");
1303                 return;
1304         }
1305         if (!verbose(tcp) || (exiting(tcp) && syserror(tcp))) {
1306                 tprintf("%#lx", addr);
1307                 return;
1308         }
1309 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1310         if (current_wordsize != sizeof(sa.sa_flags) && current_wordsize == 4) {
1311                 struct new_sigaction32 sa32;
1312                 r = umove(tcp, addr, &sa32);
1313                 if (r >= 0) {
1314                         memset(&sa, 0, sizeof(sa));
1315                         sa.__sa_handler = (void*)(unsigned long)sa32.__sa_handler;
1316                         sa.sa_flags     = sa32.sa_flags;
1317                         sa.sa_restorer  = (void*)(unsigned long)sa32.sa_restorer;
1318                         /* Kernel treats sa_mask as an array of longs.
1319                          * For 32-bit process, "long" is uint32_t, thus, for example,
1320                          * 32th bit in sa_mask will end up as bit 0 in sa_mask[1].
1321                          * But for (64-bit) kernel, 32th bit in sa_mask is
1322                          * 32th bit in 0th (64-bit) long!
1323                          * For little-endian, it's the same.
1324                          * For big-endian, we swap 32-bit words.
1325                          */
1326                         sa.sa_mask[0] = sa32.sa_mask[0] + ((long)(sa32.sa_mask[1]) << 32);
1327                 }
1328         } else
1329 #endif
1330         {
1331                 r = umove(tcp, addr, &sa);
1332         }
1333         if (r < 0) {
1334                 tprints("{...}");
1335                 return;
1336         }
1337         /* Architectures using function pointers, like
1338          * hppa, may need to manipulate the function pointer
1339          * to compute the result of a comparison. However,
1340          * the __sa_handler function pointer exists only in
1341          * the address space of the traced process, and can't
1342          * be manipulated by strace. In order to prevent the
1343          * compiler from generating code to manipulate
1344          * __sa_handler we cast the function pointers to long. */
1345         if ((long)sa.__sa_handler == (long)SIG_ERR)
1346                 tprints("{SIG_ERR, ");
1347         else if ((long)sa.__sa_handler == (long)SIG_DFL)
1348                 tprints("{SIG_DFL, ");
1349         else if ((long)sa.__sa_handler == (long)SIG_IGN)
1350                 tprints("{SIG_IGN, ");
1351         else
1352                 tprintf("{%#lx, ", (long) sa.__sa_handler);
1353         /*
1354          * Sigset size is in tcp->u_arg[4] (SPARC)
1355          * or in tcp->u_arg[3] (all other),
1356          * but kernel won't handle sys_rt_sigaction
1357          * with wrong sigset size (just returns EINVAL instead).
1358          * We just fetch the right size, which is NSIG / 8.
1359          */
1360         tprintsigmask_val("", sa.sa_mask);
1361         tprints(", ");
1362
1363         printflags(sigact_flags, sa.sa_flags, "SA_???");
1364 #ifdef SA_RESTORER
1365         if (sa.sa_flags & SA_RESTORER)
1366                 tprintf(", %p", sa.sa_restorer);
1367 #endif
1368         tprints("}");
1369 }
1370
1371 int
1372 sys_rt_sigaction(struct tcb *tcp)
1373 {
1374         if (entering(tcp)) {
1375                 printsignal(tcp->u_arg[0]);
1376                 tprints(", ");
1377                 decode_new_sigaction(tcp, tcp->u_arg[1]);
1378                 tprints(", ");
1379         } else {
1380                 decode_new_sigaction(tcp, tcp->u_arg[2]);
1381 #if defined(SPARC) || defined(SPARC64)
1382                 tprintf(", %#lx, %lu", tcp->u_arg[3], tcp->u_arg[4]);
1383 #elif defined(ALPHA)
1384                 tprintf(", %lu, %#lx", tcp->u_arg[3], tcp->u_arg[4]);
1385 #else
1386                 tprintf(", %lu", tcp->u_arg[3]);
1387 #endif
1388         }
1389         return 0;
1390 }
1391
1392 int
1393 sys_rt_sigpending(struct tcb *tcp)
1394 {
1395         if (exiting(tcp)) {
1396                 /*
1397                  * One of the few syscalls where sigset size (arg[1])
1398                  * is allowed to be <= NSIG / 8, not strictly ==.
1399                  * This allows non-rt sigpending() syscall
1400                  * to reuse rt_sigpending() code in kernel.
1401                  */
1402                 if (syserror(tcp))
1403                         tprintf("%#lx", tcp->u_arg[0]);
1404                 else
1405                         print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[1]);
1406                 tprintf(", %lu", tcp->u_arg[1]);
1407         }
1408         return 0;
1409 }
1410
1411 int
1412 sys_rt_sigsuspend(struct tcb *tcp)
1413 {
1414         if (entering(tcp)) {
1415                 /* NB: kernel requires arg[1] == NSIG / 8 */
1416                 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[1]);
1417                 tprintf(", %lu", tcp->u_arg[1]);
1418         }
1419         return 0;
1420 }
1421
1422 static void
1423 print_sigqueueinfo(struct tcb *tcp, int sig, unsigned long uinfo)
1424 {
1425         printsignal(sig);
1426         tprints(", ");
1427         printsiginfo_at(tcp, uinfo);
1428 }
1429
1430 int
1431 sys_rt_sigqueueinfo(struct tcb *tcp)
1432 {
1433         if (entering(tcp)) {
1434                 tprintf("%lu, ", tcp->u_arg[0]);
1435                 print_sigqueueinfo(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1436         }
1437         return 0;
1438 }
1439
1440 int
1441 sys_rt_tgsigqueueinfo(struct tcb *tcp)
1442 {
1443         if (entering(tcp)) {
1444                 tprintf("%lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
1445                 print_sigqueueinfo(tcp, tcp->u_arg[2], tcp->u_arg[3]);
1446         }
1447         return 0;
1448 }
1449
1450 int sys_rt_sigtimedwait(struct tcb *tcp)
1451 {
1452         /* NB: kernel requires arg[3] == NSIG / 8 */
1453         if (entering(tcp)) {
1454                 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[3]);
1455                 tprints(", ");
1456                 /* This is the only "return" parameter, */
1457                 if (tcp->u_arg[1] != 0)
1458                         return 0;
1459                 /* ... if it's NULL, can decode all on entry */
1460                 tprints("NULL, ");
1461         }
1462         else if (tcp->u_arg[1] != 0) {
1463                 /* syscall exit, and u_arg[1] wasn't NULL */
1464                 printsiginfo_at(tcp, tcp->u_arg[1]);
1465                 tprints(", ");
1466         }
1467         else {
1468                 /* syscall exit, and u_arg[1] was NULL */
1469                 return 0;
1470         }
1471         print_timespec(tcp, tcp->u_arg[2]);
1472         tprintf(", %lu", tcp->u_arg[3]);
1473         return 0;
1474 };
1475
1476 int
1477 sys_restart_syscall(struct tcb *tcp)
1478 {
1479         if (entering(tcp))
1480                 tprints("<... resuming interrupted call ...>");
1481         return 0;
1482 }
1483
1484 static int
1485 do_signalfd(struct tcb *tcp, int flags_arg)
1486 {
1487         /* NB: kernel requires arg[2] == NSIG / 8 */
1488         if (entering(tcp)) {
1489                 printfd(tcp, tcp->u_arg[0]);
1490                 tprints(", ");
1491                 print_sigset_addr_len(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1492                 tprintf(", %lu", tcp->u_arg[2]);
1493                 if (flags_arg >= 0) {
1494                         tprints(", ");
1495                         printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
1496                 }
1497         }
1498         return 0;
1499 }
1500
1501 int
1502 sys_signalfd(struct tcb *tcp)
1503 {
1504         return do_signalfd(tcp, -1);
1505 }
1506
1507 int
1508 sys_signalfd4(struct tcb *tcp)
1509 {
1510         return do_signalfd(tcp, 3);
1511 }