]> granicus.if.org Git - strace/blob - signal.c
Update siginfo codes
[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_DETHREAD     -7      /* sent by execve killing subsidiary threads */
380 #define SI_ASYNCNL      -60     /* sent by asynch name lookup completion */
381 #endif
382
383 #ifndef SI_FROMUSER
384 # define SI_FROMUSER(sip)       ((sip)->si_code <= 0)
385 #endif
386
387 static const struct xlat siginfo_codes[] = {
388 #ifdef SI_KERNEL
389         XLAT(SI_KERNEL),
390 #endif
391 #ifdef SI_USER
392         XLAT(SI_USER),
393 #endif
394 #ifdef SI_QUEUE
395         XLAT(SI_QUEUE),
396 #endif
397 #ifdef SI_TIMER
398         XLAT(SI_TIMER),
399 #endif
400 #ifdef SI_MESGQ
401         XLAT(SI_MESGQ),
402 #endif
403 #ifdef SI_ASYNCIO
404         XLAT(SI_ASYNCIO),
405 #endif
406 #ifdef SI_SIGIO
407         XLAT(SI_SIGIO),
408 #endif
409 #ifdef SI_TKILL
410         XLAT(SI_TKILL),
411 #endif
412 #ifdef SI_DETHREAD
413         XLAT(SI_DETHREAD),
414 #endif
415 #ifdef SI_ASYNCNL
416         XLAT(SI_ASYNCNL),
417 #endif
418 #ifdef SI_NOINFO
419         XLAT(SI_NOINFO),
420 #endif
421 #ifdef SI_LWP
422         XLAT(SI_LWP),
423 #endif
424         XLAT_END
425 };
426
427 static const struct xlat sigill_codes[] = {
428         XLAT(ILL_ILLOPC),
429         XLAT(ILL_ILLOPN),
430         XLAT(ILL_ILLADR),
431         XLAT(ILL_ILLTRP),
432         XLAT(ILL_PRVOPC),
433         XLAT(ILL_PRVREG),
434         XLAT(ILL_COPROC),
435         XLAT(ILL_BADSTK),
436         XLAT_END
437 };
438
439 static const struct xlat sigfpe_codes[] = {
440         XLAT(FPE_INTDIV),
441         XLAT(FPE_INTOVF),
442         XLAT(FPE_FLTDIV),
443         XLAT(FPE_FLTOVF),
444         XLAT(FPE_FLTUND),
445         XLAT(FPE_FLTRES),
446         XLAT(FPE_FLTINV),
447         XLAT(FPE_FLTSUB),
448         XLAT_END
449 };
450
451 static const struct xlat sigtrap_codes[] = {
452         XLAT(TRAP_BRKPT),
453         XLAT(TRAP_TRACE),
454         XLAT_END
455 };
456
457 static const struct xlat sigchld_codes[] = {
458         XLAT(CLD_EXITED),
459         XLAT(CLD_KILLED),
460         XLAT(CLD_DUMPED),
461         XLAT(CLD_TRAPPED),
462         XLAT(CLD_STOPPED),
463         XLAT(CLD_CONTINUED),
464         XLAT_END
465 };
466
467 static const struct xlat sigpoll_codes[] = {
468         XLAT(POLL_IN),
469         XLAT(POLL_OUT),
470         XLAT(POLL_MSG),
471         XLAT(POLL_ERR),
472         XLAT(POLL_PRI),
473         XLAT(POLL_HUP),
474         XLAT_END
475 };
476
477 static const struct xlat sigprof_codes[] = {
478 #ifdef PROF_SIG
479         XLAT(PROF_SIG),
480 #endif
481         XLAT_END
482 };
483
484 #ifdef SIGEMT
485 static const struct xlat sigemt_codes[] = {
486 #ifdef EMT_TAGOVF
487         XLAT(EMT_TAGOVF),
488 #endif
489         XLAT_END
490 };
491 #endif
492
493 static const struct xlat sigsegv_codes[] = {
494         XLAT(SEGV_MAPERR),
495         XLAT(SEGV_ACCERR),
496         XLAT_END
497 };
498
499 static const struct xlat sigbus_codes[] = {
500         XLAT(BUS_ADRALN),
501         XLAT(BUS_ADRERR),
502         XLAT(BUS_OBJERR),
503         XLAT_END
504 };
505
506 static void
507 printsigsource(const siginfo_t *sip)
508 {
509         tprintf(", si_pid=%lu, si_uid=%lu",
510                 (unsigned long) sip->si_pid,
511                 (unsigned long) sip->si_uid);
512 }
513
514 static void
515 printsigval(const siginfo_t *sip, int verbose)
516 {
517         if (!verbose)
518                 tprints(", ...");
519         else
520                 tprintf(", si_value={int=%u, ptr=%#lx}",
521                         sip->si_int,
522                         (unsigned long) sip->si_ptr);
523 }
524
525 void
526 printsiginfo(siginfo_t *sip, int verbose)
527 {
528         const char *code;
529
530         if (sip->si_signo == 0) {
531                 tprints("{}");
532                 return;
533         }
534         tprints("{si_signo=");
535         printsignal(sip->si_signo);
536         code = xlookup(siginfo_codes, sip->si_code);
537         if (!code) {
538                 switch (sip->si_signo) {
539                 case SIGTRAP:
540                         code = xlookup(sigtrap_codes, sip->si_code);
541                         break;
542                 case SIGCHLD:
543                         code = xlookup(sigchld_codes, sip->si_code);
544                         break;
545                 case SIGPOLL:
546                         code = xlookup(sigpoll_codes, sip->si_code);
547                         break;
548                 case SIGPROF:
549                         code = xlookup(sigprof_codes, sip->si_code);
550                         break;
551                 case SIGILL:
552                         code = xlookup(sigill_codes, sip->si_code);
553                         break;
554 #ifdef SIGEMT
555                 case SIGEMT:
556                         code = xlookup(sigemt_codes, sip->si_code);
557                         break;
558 #endif
559                 case SIGFPE:
560                         code = xlookup(sigfpe_codes, sip->si_code);
561                         break;
562                 case SIGSEGV:
563                         code = xlookup(sigsegv_codes, sip->si_code);
564                         break;
565                 case SIGBUS:
566                         code = xlookup(sigbus_codes, sip->si_code);
567                         break;
568                 }
569         }
570         if (code)
571                 tprintf(", si_code=%s", code);
572         else
573                 tprintf(", si_code=%#x", sip->si_code);
574 #ifdef SI_NOINFO
575         if (sip->si_code != SI_NOINFO)
576 #endif
577         {
578                 if (sip->si_errno) {
579                         if (sip->si_errno < 0 || sip->si_errno >= nerrnos)
580                                 tprintf(", si_errno=%d", sip->si_errno);
581                         else
582                                 tprintf(", si_errno=%s",
583                                         errnoent[sip->si_errno]);
584                 }
585 #ifdef SI_FROMUSER
586                 if (SI_FROMUSER(sip)) {
587                         switch (sip->si_code) {
588 #ifdef SI_USER
589                         case SI_USER:
590                                 printsigsource(sip);
591                                 break;
592 #endif
593 #ifdef SI_TKILL
594                         case SI_TKILL:
595                                 printsigsource(sip);
596                                 break;
597 #endif
598 #ifdef SI_TIMER
599                         case SI_TIMER:
600                                 tprintf(", si_timerid=%#x, si_overrun=%d",
601                                         sip->si_timerid, sip->si_overrun);
602                                 printsigval(sip, verbose);
603                                 break;
604 #endif
605                         default:
606                                 printsigsource(sip);
607                                 if (sip->si_ptr)
608                                         printsigval(sip, verbose);
609                                 break;
610                         }
611                 }
612                 else
613 #endif /* SI_FROMUSER */
614                 {
615                         switch (sip->si_signo) {
616                         case SIGCHLD:
617                                 printsigsource(sip);
618                                 tprints(", si_status=");
619                                 if (sip->si_code == CLD_EXITED)
620                                         tprintf("%d", sip->si_status);
621                                 else
622                                         printsignal(sip->si_status);
623                                 if (!verbose)
624                                         tprints(", ...");
625                                 else
626                                         tprintf(", si_utime=%llu, si_stime=%llu",
627                                                 (unsigned long long) sip->si_utime,
628                                                 (unsigned long long) sip->si_stime);
629                                 break;
630                         case SIGILL: case SIGFPE:
631                         case SIGSEGV: case SIGBUS:
632                                 tprintf(", si_addr=%#lx",
633                                         (unsigned long) sip->si_addr);
634                                 break;
635                         case SIGPOLL:
636                                 switch (sip->si_code) {
637                                 case POLL_IN: case POLL_OUT: case POLL_MSG:
638                                         tprintf(", si_band=%ld",
639                                                 (long) sip->si_band);
640                                         break;
641                                 }
642                                 break;
643                         default:
644                                 if (sip->si_pid || sip->si_uid)
645                                         printsigsource(sip);
646                                 if (sip->si_ptr)
647                                         printsigval(sip, verbose);
648                         }
649                 }
650         }
651         tprints("}");
652 }
653
654 void
655 printsiginfo_at(struct tcb *tcp, long addr)
656 {
657         siginfo_t si;
658         if (!addr) {
659                 tprints("NULL");
660                 return;
661         }
662         if (syserror(tcp)) {
663                 tprintf("%#lx", addr);
664                 return;
665         }
666         if (umove(tcp, addr, &si) < 0) {
667                 tprints("{???}");
668                 return;
669         }
670         printsiginfo(&si, verbose(tcp));
671 }
672
673 int
674 sys_sigsetmask(struct tcb *tcp)
675 {
676         if (entering(tcp)) {
677                 tprintsigmask_val("", tcp->u_arg[0]);
678         }
679         else if (!syserror(tcp)) {
680                 tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
681                 return RVAL_HEX | RVAL_STR;
682         }
683         return 0;
684 }
685
686 #ifdef HAVE_SIGACTION
687
688 struct old_sigaction {
689         /* sa_handler may be a libc #define, need to use other name: */
690 #ifdef MIPS
691         unsigned int sa_flags;
692         void (*__sa_handler)(int);
693         /* Kernel treats sa_mask as an array of longs. */
694         unsigned long sa_mask[NSIG / sizeof(long) ? NSIG / sizeof(long) : 1];
695 #else
696         void (*__sa_handler)(int);
697         unsigned long sa_mask;
698         unsigned long sa_flags;
699         void (*sa_restorer)(void);
700 #endif /* !MIPS */
701 };
702
703 struct old_sigaction32 {
704         /* sa_handler may be a libc #define, need to use other name: */
705         uint32_t __sa_handler;
706         uint32_t sa_mask;
707         uint32_t sa_flags;
708         uint32_t sa_restorer;
709 };
710
711 static void
712 decode_old_sigaction(struct tcb *tcp, long addr)
713 {
714         struct old_sigaction sa;
715         int r;
716
717         if (!addr) {
718                 tprints("NULL");
719                 return;
720         }
721         if (!verbose(tcp) || (exiting(tcp) && syserror(tcp))) {
722                 tprintf("%#lx", addr);
723                 return;
724         }
725
726 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
727         if (current_wordsize != sizeof(sa.__sa_handler) && current_wordsize == 4) {
728                 struct old_sigaction32 sa32;
729                 r = umove(tcp, addr, &sa32);
730                 if (r >= 0) {
731                         memset(&sa, 0, sizeof(sa));
732                         sa.__sa_handler = (void*)(uintptr_t)sa32.__sa_handler;
733                         sa.sa_flags = sa32.sa_flags;
734                         sa.sa_restorer = (void*)(uintptr_t)sa32.sa_restorer;
735                         sa.sa_mask = sa32.sa_mask;
736                 }
737         } else
738 #endif
739         {
740                 r = umove(tcp, addr, &sa);
741         }
742         if (r < 0) {
743                 tprints("{...}");
744                 return;
745         }
746
747         /* Architectures using function pointers, like
748          * hppa, may need to manipulate the function pointer
749          * to compute the result of a comparison. However,
750          * the __sa_handler function pointer exists only in
751          * the address space of the traced process, and can't
752          * be manipulated by strace. In order to prevent the
753          * compiler from generating code to manipulate
754          * __sa_handler we cast the function pointers to long. */
755         if ((long)sa.__sa_handler == (long)SIG_ERR)
756                 tprints("{SIG_ERR, ");
757         else if ((long)sa.__sa_handler == (long)SIG_DFL)
758                 tprints("{SIG_DFL, ");
759         else if ((long)sa.__sa_handler == (long)SIG_IGN)
760                 tprints("{SIG_IGN, ");
761         else
762                 tprintf("{%#lx, ", (long) sa.__sa_handler);
763 #ifdef MIPS
764         tprintsigmask_addr("", sa.sa_mask);
765 #else
766         tprintsigmask_val("", sa.sa_mask);
767 #endif
768         tprints(", ");
769         printflags(sigact_flags, sa.sa_flags, "SA_???");
770 #ifdef SA_RESTORER
771         if (sa.sa_flags & SA_RESTORER)
772                 tprintf(", %p", sa.sa_restorer);
773 #endif
774         tprints("}");
775 }
776
777 int
778 sys_sigaction(struct tcb *tcp)
779 {
780         if (entering(tcp)) {
781                 printsignal(tcp->u_arg[0]);
782                 tprints(", ");
783                 decode_old_sigaction(tcp, tcp->u_arg[1]);
784                 tprints(", ");
785         } else
786                 decode_old_sigaction(tcp, tcp->u_arg[2]);
787         return 0;
788 }
789
790 int
791 sys_signal(struct tcb *tcp)
792 {
793         if (entering(tcp)) {
794                 printsignal(tcp->u_arg[0]);
795                 tprints(", ");
796                 switch (tcp->u_arg[1]) {
797                 case (long) SIG_ERR:
798                         tprints("SIG_ERR");
799                         break;
800                 case (long) SIG_DFL:
801                         tprints("SIG_DFL");
802                         break;
803                 case (long) SIG_IGN:
804                         tprints("SIG_IGN");
805                         break;
806                 default:
807                         tprintf("%#lx", tcp->u_arg[1]);
808                 }
809                 return 0;
810         }
811         else if (!syserror(tcp)) {
812                 switch (tcp->u_rval) {
813                 case (long) SIG_ERR:
814                         tcp->auxstr = "SIG_ERR"; break;
815                 case (long) SIG_DFL:
816                         tcp->auxstr = "SIG_DFL"; break;
817                 case (long) SIG_IGN:
818                         tcp->auxstr = "SIG_IGN"; break;
819                 default:
820                         tcp->auxstr = NULL;
821                 }
822                 return RVAL_HEX | RVAL_STR;
823         }
824         return 0;
825 }
826
827 #endif /* HAVE_SIGACTION */
828
829 int
830 sys_sigreturn(struct tcb *tcp)
831 {
832 #if defined(ARM)
833         if (entering(tcp)) {
834                 struct arm_sigcontext {
835                         unsigned long trap_no;
836                         unsigned long error_code;
837                         unsigned long oldmask;
838                         unsigned long arm_r0;
839                         unsigned long arm_r1;
840                         unsigned long arm_r2;
841                         unsigned long arm_r3;
842                         unsigned long arm_r4;
843                         unsigned long arm_r5;
844                         unsigned long arm_r6;
845                         unsigned long arm_r7;
846                         unsigned long arm_r8;
847                         unsigned long arm_r9;
848                         unsigned long arm_r10;
849                         unsigned long arm_fp;
850                         unsigned long arm_ip;
851                         unsigned long arm_sp;
852                         unsigned long arm_lr;
853                         unsigned long arm_pc;
854                         unsigned long arm_cpsr;
855                         unsigned long fault_address;
856                 };
857                 struct arm_ucontext {
858                         unsigned long uc_flags;
859                         unsigned long uc_link;  /* struct ucontext* */
860                         /* The next three members comprise stack_t struct: */
861                         unsigned long ss_sp;    /* void*   */
862                         unsigned long ss_flags; /* int     */
863                         unsigned long ss_size;  /* size_t  */
864                         struct arm_sigcontext sc;
865                         /* These two members are sigset_t: */
866                         unsigned long uc_sigmask[2];
867                         /* more fields follow, which we aren't interested in */
868                 };
869                 struct arm_ucontext uc;
870                 if (umove(tcp, arm_regs.ARM_sp, &uc) < 0)
871                         return 0;
872                 /*
873                  * Kernel fills out uc.sc.oldmask too when it sets up signal stack,
874                  * but for sigmask restore, sigreturn syscall uses uc.uc_sigmask instead.
875                  */
876                 tprintsigmask_addr(") (mask ", uc.uc_sigmask);
877         }
878 #elif defined(S390) || defined(S390X)
879         if (entering(tcp)) {
880                 long usp;
881                 struct sigcontext sc;
882                 if (upeek(tcp->pid, PT_GPR15, &usp) < 0)
883                         return 0;
884                 if (umove(tcp, usp + __SIGNAL_FRAMESIZE, &sc) < 0)
885                         return 0;
886                 tprintsigmask_addr(") (mask ", sc.oldmask);
887         }
888 #elif defined(I386) || defined(X86_64)
889 # if defined(X86_64)
890         if (current_personality == 0) /* 64-bit */
891                 return 0;
892 # endif
893         if (entering(tcp)) {
894                 struct i386_sigcontext_struct {
895                         uint16_t gs, __gsh;
896                         uint16_t fs, __fsh;
897                         uint16_t es, __esh;
898                         uint16_t ds, __dsh;
899                         uint32_t edi;
900                         uint32_t esi;
901                         uint32_t ebp;
902                         uint32_t esp;
903                         uint32_t ebx;
904                         uint32_t edx;
905                         uint32_t ecx;
906                         uint32_t eax;
907                         uint32_t trapno;
908                         uint32_t err;
909                         uint32_t eip;
910                         uint16_t cs, __csh;
911                         uint32_t eflags;
912                         uint32_t esp_at_signal;
913                         uint16_t ss, __ssh;
914                         uint32_t i387;
915                         uint32_t oldmask;
916                         uint32_t cr2;
917                 };
918                 struct i386_fpstate {
919                         uint32_t cw;
920                         uint32_t sw;
921                         uint32_t tag;
922                         uint32_t ipoff;
923                         uint32_t cssel;
924                         uint32_t dataoff;
925                         uint32_t datasel;
926                         uint8_t  st[8][10]; /* 8*10 bytes: FP regs */
927                         uint16_t status;
928                         uint16_t magic;
929                         uint32_t fxsr_env[6];
930                         uint32_t mxcsr;
931                         uint32_t reserved;
932                         uint8_t  stx[8][16]; /* 8*16 bytes: FP regs, each padded to 16 bytes */
933                         uint8_t  xmm[8][16]; /* 8 XMM regs */
934                         uint32_t padding1[44];
935                         uint32_t padding2[12]; /* union with struct _fpx_sw_bytes */
936                 };
937                 struct {
938                         struct i386_sigcontext_struct sc;
939                         struct i386_fpstate fp;
940                         uint32_t extramask[1];
941                 } signal_stack;
942                 /* On i386, sc is followed on stack by struct fpstate
943                  * and after it an additional u32 extramask[1] which holds
944                  * upper half of the mask.
945                  */
946                 uint32_t sigmask[2];
947                 if (umove(tcp, *i386_esp_ptr, &signal_stack) < 0)
948                         return 0;
949                 sigmask[0] = signal_stack.sc.oldmask;
950                 sigmask[1] = signal_stack.extramask[0];
951                 tprintsigmask_addr(") (mask ", sigmask);
952         }
953 #elif defined(IA64)
954         if (entering(tcp)) {
955                 struct sigcontext sc;
956                 long sp;
957                 /* offset of sigcontext in the kernel's sigframe structure: */
958 #               define SIGFRAME_SC_OFFSET       0x90
959                 if (upeek(tcp->pid, PT_R12, &sp) < 0)
960                         return 0;
961                 if (umove(tcp, sp + 16 + SIGFRAME_SC_OFFSET, &sc) < 0)
962                         return 0;
963                 tprintsigmask_val(") (mask ", sc.sc_mask);
964         }
965 #elif defined(POWERPC)
966         if (entering(tcp)) {
967                 long esp;
968                 struct sigcontext sc;
969
970                 esp = ppc_regs.gpr[1];
971
972                 /* Skip dummy stack frame. */
973 #ifdef POWERPC64
974                 if (current_personality == 0)
975                         esp += 128;
976                 else
977                         esp += 64;
978 #else
979                 esp += 64;
980 #endif
981                 if (umove(tcp, esp, &sc) < 0)
982                         return 0;
983                 tprintsigmask_val(") (mask ", sc.oldmask);
984         }
985 #elif defined(M68K)
986         if (entering(tcp)) {
987                 long usp;
988                 struct sigcontext sc;
989                 if (upeek(tcp->pid, 4*PT_USP, &usp) < 0)
990                         return 0;
991                 if (umove(tcp, usp, &sc) < 0)
992                         return 0;
993                 tprintsigmask_val(") (mask ", sc.sc_mask);
994         }
995 #elif defined(ALPHA)
996         if (entering(tcp)) {
997                 long fp;
998                 struct sigcontext sc;
999                 if (upeek(tcp->pid, REG_FP, &fp) < 0)
1000                         return 0;
1001                 if (umove(tcp, fp, &sc) < 0)
1002                         return 0;
1003                 tprintsigmask_val(") (mask ", sc.sc_mask);
1004         }
1005 #elif defined(SPARC) || defined(SPARC64)
1006         if (entering(tcp)) {
1007                 long i1;
1008                 m_siginfo_t si;
1009                 i1 = sparc_regs.u_regs[U_REG_O1];
1010                 if (umove(tcp, i1, &si) < 0) {
1011                         perror_msg("sigreturn: umove");
1012                         return 0;
1013                 }
1014                 tprintsigmask_val(") (mask ", si.si_mask);
1015         }
1016 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1017         /* This decodes rt_sigreturn.  The 64-bit ABIs do not have
1018            sigreturn.  */
1019         if (entering(tcp)) {
1020                 long sp;
1021                 struct ucontext uc;
1022                 if (upeek(tcp->pid, REG_SP, &sp) < 0)
1023                         return 0;
1024                 /* There are six words followed by a 128-byte siginfo.  */
1025                 sp = sp + 6 * 4 + 128;
1026                 if (umove(tcp, sp, &uc) < 0)
1027                         return 0;
1028                 tprintsigmask_val(") (mask ", uc.uc_sigmask);
1029         }
1030 #elif defined(MIPS)
1031         if (entering(tcp)) {
1032                 long sp;
1033                 struct pt_regs regs;
1034                 m_siginfo_t si;
1035                 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0) {
1036                         perror_msg("sigreturn: PTRACE_GETREGS");
1037                         return 0;
1038                 }
1039                 sp = regs.regs[29];
1040                 if (umove(tcp, sp, &si) < 0)
1041                         return 0;
1042                 tprintsigmask_val(") (mask ", si.si_mask);
1043         }
1044 #elif defined(CRISV10) || defined(CRISV32)
1045         if (entering(tcp)) {
1046                 struct sigcontext sc;
1047                 long regs[PT_MAX+1];
1048                 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long)regs) < 0) {
1049                         perror_msg("sigreturn: PTRACE_GETREGS");
1050                         return 0;
1051                 }
1052                 if (umove(tcp, regs[PT_USP], &sc) < 0)
1053                         return 0;
1054                 tprintsigmask_val(") (mask ", sc.oldmask);
1055         }
1056 #elif defined(TILE)
1057         if (entering(tcp)) {
1058                 struct ucontext uc;
1059
1060                 /* offset of ucontext in the kernel's sigframe structure */
1061 #               define SIGFRAME_UC_OFFSET C_ABI_SAVE_AREA_SIZE + sizeof(siginfo_t)
1062                 if (umove(tcp, tile_regs.sp + SIGFRAME_UC_OFFSET, &uc) < 0)
1063                         return 0;
1064                 tprintsigmask_val(") (mask ", uc.uc_sigmask);
1065         }
1066 #elif defined(MICROBLAZE)
1067         /* TODO: Verify that this is correct...  */
1068         if (entering(tcp)) {
1069                 struct sigcontext sc;
1070                 long sp;
1071                 /* Read r1, the stack pointer.  */
1072                 if (upeek(tcp->pid, 1 * 4, &sp) < 0)
1073                         return 0;
1074                 if (umove(tcp, sp, &sc) < 0)
1075                         return 0;
1076                 tprintsigmask_val(") (mask ", sc.oldmask);
1077         }
1078 #elif defined(XTENSA)
1079         /* Xtensa only has rt_sys_sigreturn */
1080 #elif defined(ARC)
1081         /* ARC syscall ABI only supports rt_sys_sigreturn */
1082 #else
1083 # warning No sys_sigreturn() for this architecture
1084 # warning         (no problem, just a reminder :-)
1085 #endif
1086         return 0;
1087 }
1088
1089 int
1090 sys_siggetmask(struct tcb *tcp)
1091 {
1092         if (exiting(tcp)) {
1093                 tcp->auxstr = sprintsigmask_val("mask ", tcp->u_rval);
1094         }
1095         return RVAL_HEX | RVAL_STR;
1096 }
1097
1098 int
1099 sys_sigsuspend(struct tcb *tcp)
1100 {
1101         if (entering(tcp)) {
1102                 tprintsigmask_val("", tcp->u_arg[2]);
1103         }
1104         return 0;
1105 }
1106
1107 #if !defined SS_ONSTACK
1108 #define SS_ONSTACK      1
1109 #define SS_DISABLE      2
1110 #endif
1111
1112 static const struct xlat sigaltstack_flags[] = {
1113         XLAT(SS_ONSTACK),
1114         XLAT(SS_DISABLE),
1115         XLAT_END
1116 };
1117
1118 static void
1119 print_stack_t(struct tcb *tcp, unsigned long addr)
1120 {
1121         stack_t ss;
1122         int r;
1123
1124         if (!addr) {
1125                 tprints("NULL");
1126                 return;
1127         }
1128
1129 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1130         if (current_wordsize != sizeof(ss.ss_sp) && current_wordsize == 4) {
1131                 struct {
1132                         uint32_t ss_sp;
1133                         int32_t ss_flags;
1134                         uint32_t ss_size;
1135                 } ss32;
1136                 r = umove(tcp, addr, &ss32);
1137                 if (r >= 0) {
1138                         memset(&ss, 0, sizeof(ss));
1139                         ss.ss_sp = (void*)(unsigned long) ss32.ss_sp;
1140                         ss.ss_flags = ss32.ss_flags;
1141                         ss.ss_size = (unsigned long) ss32.ss_size;
1142                 }
1143         } else
1144 #endif
1145         {
1146                 r = umove(tcp, addr, &ss);
1147         }
1148         if (r < 0) {
1149                 tprintf("%#lx", addr);
1150         } else {
1151                 tprintf("{ss_sp=%#lx, ss_flags=", (unsigned long) ss.ss_sp);
1152                 printflags(sigaltstack_flags, ss.ss_flags, "SS_???");
1153                 tprintf(", ss_size=%lu}", (unsigned long) ss.ss_size);
1154         }
1155 }
1156
1157 int
1158 sys_sigaltstack(struct tcb *tcp)
1159 {
1160         if (entering(tcp)) {
1161                 print_stack_t(tcp, tcp->u_arg[0]);
1162         }
1163         else {
1164                 tprints(", ");
1165                 print_stack_t(tcp, tcp->u_arg[1]);
1166         }
1167         return 0;
1168 }
1169
1170 #ifdef HAVE_SIGACTION
1171
1172 /* "Old" sigprocmask, which operates with word-sized signal masks */
1173 int
1174 sys_sigprocmask(struct tcb *tcp)
1175 {
1176 # ifdef ALPHA
1177         if (entering(tcp)) {
1178                 /*
1179                  * Alpha/OSF is different: it doesn't pass in two pointers,
1180                  * but rather passes in the new bitmask as an argument and
1181                  * then returns the old bitmask.  This "works" because we
1182                  * only have 64 signals to worry about.  If you want more,
1183                  * use of the rt_sigprocmask syscall is required.
1184                  * Alpha:
1185                  *      old = osf_sigprocmask(how, new);
1186                  * Everyone else:
1187                  *      ret = sigprocmask(how, &new, &old, ...);
1188                  */
1189                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1190                 tprintsigmask_val(", ", tcp->u_arg[1]);
1191         }
1192         else if (!syserror(tcp)) {
1193                 tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
1194                 return RVAL_HEX | RVAL_STR;
1195         }
1196 # else /* !ALPHA */
1197         if (entering(tcp)) {
1198                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1199                 tprints(", ");
1200                 print_sigset_addr_len(tcp, tcp->u_arg[1], current_wordsize);
1201                 tprints(", ");
1202         }
1203         else {
1204                 if (syserror(tcp))
1205                         tprintf("%#lx", tcp->u_arg[2]);
1206                 else
1207                         print_sigset_addr_len(tcp, tcp->u_arg[2], current_wordsize);
1208         }
1209 # endif /* !ALPHA */
1210         return 0;
1211 }
1212
1213 #endif /* HAVE_SIGACTION */
1214
1215 int
1216 sys_kill(struct tcb *tcp)
1217 {
1218         if (entering(tcp)) {
1219                 tprintf("%ld, %s",
1220                         widen_to_long(tcp->u_arg[0]),
1221                         signame(tcp->u_arg[1])
1222                 );
1223         }
1224         return 0;
1225 }
1226
1227 int
1228 sys_tgkill(struct tcb *tcp)
1229 {
1230         if (entering(tcp)) {
1231                 tprintf("%ld, %ld, %s",
1232                         widen_to_long(tcp->u_arg[0]),
1233                         widen_to_long(tcp->u_arg[1]),
1234                         signame(tcp->u_arg[2])
1235                 );
1236         }
1237         return 0;
1238 }
1239
1240 int
1241 sys_sigpending(struct tcb *tcp)
1242 {
1243         if (exiting(tcp)) {
1244                 if (syserror(tcp))
1245                         tprintf("%#lx", tcp->u_arg[0]);
1246                 else
1247                         print_sigset_addr_len(tcp, tcp->u_arg[0], current_wordsize);
1248         }
1249         return 0;
1250 }
1251
1252 int
1253 sys_rt_sigprocmask(struct tcb *tcp)
1254 {
1255         /* Note: arg[3] is the length of the sigset. Kernel requires NSIG / 8 */
1256         if (entering(tcp)) {
1257                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1258                 tprints(", ");
1259                 print_sigset_addr_len(tcp, tcp->u_arg[1], tcp->u_arg[3]);
1260                 tprints(", ");
1261         }
1262         else {
1263                 if (syserror(tcp))
1264                         tprintf("%#lx", tcp->u_arg[2]);
1265                 else
1266                         print_sigset_addr_len(tcp, tcp->u_arg[2], tcp->u_arg[3]);
1267                 tprintf(", %lu", tcp->u_arg[3]);
1268         }
1269         return 0;
1270 }
1271
1272 /* Structure describing the action to be taken when a signal arrives.  */
1273 struct new_sigaction
1274 {
1275         /* sa_handler may be a libc #define, need to use other name: */
1276 #ifdef MIPS
1277         unsigned int sa_flags;
1278         void (*__sa_handler)(int);
1279 #else
1280         void (*__sa_handler)(int);
1281         unsigned long sa_flags;
1282         void (*sa_restorer)(void);
1283 #endif /* !MIPS */
1284         /* Kernel treats sa_mask as an array of longs. */
1285         unsigned long sa_mask[NSIG / sizeof(long) ? NSIG / sizeof(long) : 1];
1286 };
1287 /* Same for i386-on-x86_64 and similar cases */
1288 struct new_sigaction32
1289 {
1290         uint32_t __sa_handler;
1291         uint32_t sa_flags;
1292         uint32_t sa_restorer;
1293         uint32_t sa_mask[2 * (NSIG / sizeof(long) ? NSIG / sizeof(long) : 1)];
1294 };
1295
1296 static void
1297 decode_new_sigaction(struct tcb *tcp, long addr)
1298 {
1299         struct new_sigaction sa;
1300         int r;
1301
1302         if (!addr) {
1303                 tprints("NULL");
1304                 return;
1305         }
1306         if (!verbose(tcp) || (exiting(tcp) && syserror(tcp))) {
1307                 tprintf("%#lx", addr);
1308                 return;
1309         }
1310 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1311         if (current_wordsize != sizeof(sa.sa_flags) && current_wordsize == 4) {
1312                 struct new_sigaction32 sa32;
1313                 r = umove(tcp, addr, &sa32);
1314                 if (r >= 0) {
1315                         memset(&sa, 0, sizeof(sa));
1316                         sa.__sa_handler = (void*)(unsigned long)sa32.__sa_handler;
1317                         sa.sa_flags     = sa32.sa_flags;
1318                         sa.sa_restorer  = (void*)(unsigned long)sa32.sa_restorer;
1319                         /* Kernel treats sa_mask as an array of longs.
1320                          * For 32-bit process, "long" is uint32_t, thus, for example,
1321                          * 32th bit in sa_mask will end up as bit 0 in sa_mask[1].
1322                          * But for (64-bit) kernel, 32th bit in sa_mask is
1323                          * 32th bit in 0th (64-bit) long!
1324                          * For little-endian, it's the same.
1325                          * For big-endian, we swap 32-bit words.
1326                          */
1327                         sa.sa_mask[0] = sa32.sa_mask[0] + ((long)(sa32.sa_mask[1]) << 32);
1328                 }
1329         } else
1330 #endif
1331         {
1332                 r = umove(tcp, addr, &sa);
1333         }
1334         if (r < 0) {
1335                 tprints("{...}");
1336                 return;
1337         }
1338         /* Architectures using function pointers, like
1339          * hppa, may need to manipulate the function pointer
1340          * to compute the result of a comparison. However,
1341          * the __sa_handler function pointer exists only in
1342          * the address space of the traced process, and can't
1343          * be manipulated by strace. In order to prevent the
1344          * compiler from generating code to manipulate
1345          * __sa_handler we cast the function pointers to long. */
1346         if ((long)sa.__sa_handler == (long)SIG_ERR)
1347                 tprints("{SIG_ERR, ");
1348         else if ((long)sa.__sa_handler == (long)SIG_DFL)
1349                 tprints("{SIG_DFL, ");
1350         else if ((long)sa.__sa_handler == (long)SIG_IGN)
1351                 tprints("{SIG_IGN, ");
1352         else
1353                 tprintf("{%#lx, ", (long) sa.__sa_handler);
1354         /*
1355          * Sigset size is in tcp->u_arg[4] (SPARC)
1356          * or in tcp->u_arg[3] (all other),
1357          * but kernel won't handle sys_rt_sigaction
1358          * with wrong sigset size (just returns EINVAL instead).
1359          * We just fetch the right size, which is NSIG / 8.
1360          */
1361         tprintsigmask_val("", sa.sa_mask);
1362         tprints(", ");
1363
1364         printflags(sigact_flags, sa.sa_flags, "SA_???");
1365 #ifdef SA_RESTORER
1366         if (sa.sa_flags & SA_RESTORER)
1367                 tprintf(", %p", sa.sa_restorer);
1368 #endif
1369         tprints("}");
1370 }
1371
1372 int
1373 sys_rt_sigaction(struct tcb *tcp)
1374 {
1375         if (entering(tcp)) {
1376                 printsignal(tcp->u_arg[0]);
1377                 tprints(", ");
1378                 decode_new_sigaction(tcp, tcp->u_arg[1]);
1379                 tprints(", ");
1380         } else {
1381                 decode_new_sigaction(tcp, tcp->u_arg[2]);
1382 #if defined(SPARC) || defined(SPARC64)
1383                 tprintf(", %#lx, %lu", tcp->u_arg[3], tcp->u_arg[4]);
1384 #elif defined(ALPHA)
1385                 tprintf(", %lu, %#lx", tcp->u_arg[3], tcp->u_arg[4]);
1386 #else
1387                 tprintf(", %lu", tcp->u_arg[3]);
1388 #endif
1389         }
1390         return 0;
1391 }
1392
1393 int
1394 sys_rt_sigpending(struct tcb *tcp)
1395 {
1396         if (exiting(tcp)) {
1397                 /*
1398                  * One of the few syscalls where sigset size (arg[1])
1399                  * is allowed to be <= NSIG / 8, not strictly ==.
1400                  * This allows non-rt sigpending() syscall
1401                  * to reuse rt_sigpending() code in kernel.
1402                  */
1403                 if (syserror(tcp))
1404                         tprintf("%#lx", tcp->u_arg[0]);
1405                 else
1406                         print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[1]);
1407                 tprintf(", %lu", tcp->u_arg[1]);
1408         }
1409         return 0;
1410 }
1411
1412 int
1413 sys_rt_sigsuspend(struct tcb *tcp)
1414 {
1415         if (entering(tcp)) {
1416                 /* NB: kernel requires arg[1] == NSIG / 8 */
1417                 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[1]);
1418                 tprintf(", %lu", tcp->u_arg[1]);
1419         }
1420         return 0;
1421 }
1422
1423 static void
1424 print_sigqueueinfo(struct tcb *tcp, int sig, unsigned long uinfo)
1425 {
1426         printsignal(sig);
1427         tprints(", ");
1428         printsiginfo_at(tcp, uinfo);
1429 }
1430
1431 int
1432 sys_rt_sigqueueinfo(struct tcb *tcp)
1433 {
1434         if (entering(tcp)) {
1435                 tprintf("%lu, ", tcp->u_arg[0]);
1436                 print_sigqueueinfo(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1437         }
1438         return 0;
1439 }
1440
1441 int
1442 sys_rt_tgsigqueueinfo(struct tcb *tcp)
1443 {
1444         if (entering(tcp)) {
1445                 tprintf("%lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
1446                 print_sigqueueinfo(tcp, tcp->u_arg[2], tcp->u_arg[3]);
1447         }
1448         return 0;
1449 }
1450
1451 int sys_rt_sigtimedwait(struct tcb *tcp)
1452 {
1453         /* NB: kernel requires arg[3] == NSIG / 8 */
1454         if (entering(tcp)) {
1455                 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[3]);
1456                 tprints(", ");
1457                 /* This is the only "return" parameter, */
1458                 if (tcp->u_arg[1] != 0)
1459                         return 0;
1460                 /* ... if it's NULL, can decode all on entry */
1461                 tprints("NULL, ");
1462         }
1463         else if (tcp->u_arg[1] != 0) {
1464                 /* syscall exit, and u_arg[1] wasn't NULL */
1465                 printsiginfo_at(tcp, tcp->u_arg[1]);
1466                 tprints(", ");
1467         }
1468         else {
1469                 /* syscall exit, and u_arg[1] was NULL */
1470                 return 0;
1471         }
1472         print_timespec(tcp, tcp->u_arg[2]);
1473         tprintf(", %lu", tcp->u_arg[3]);
1474         return 0;
1475 };
1476
1477 int
1478 sys_restart_syscall(struct tcb *tcp)
1479 {
1480         if (entering(tcp))
1481                 tprints("<... resuming interrupted call ...>");
1482         return 0;
1483 }
1484
1485 static int
1486 do_signalfd(struct tcb *tcp, int flags_arg)
1487 {
1488         /* NB: kernel requires arg[2] == NSIG / 8 */
1489         if (entering(tcp)) {
1490                 printfd(tcp, tcp->u_arg[0]);
1491                 tprints(", ");
1492                 print_sigset_addr_len(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1493                 tprintf(", %lu", tcp->u_arg[2]);
1494                 if (flags_arg >= 0) {
1495                         tprints(", ");
1496                         printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
1497                 }
1498         }
1499         return 0;
1500 }
1501
1502 int
1503 sys_signalfd(struct tcb *tcp)
1504 {
1505         return do_signalfd(tcp, -1);
1506 }
1507
1508 int
1509 sys_signalfd4(struct tcb *tcp)
1510 {
1511         return do_signalfd(tcp, 3);
1512 }