]> granicus.if.org Git - strace/blob - signal.c
Fix sigreturn decoding on MIPS
[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  *      $Id$
34  */
35
36 #include "defs.h"
37
38 #include <stdint.h>
39 #include <signal.h>
40 #include <sys/user.h>
41 #include <fcntl.h>
42
43 #ifdef SVR4
44 #include <sys/ucontext.h>
45 #endif /* SVR4 */
46
47 #ifdef HAVE_SYS_REG_H
48 # include <sys/reg.h>
49 #ifndef PTRACE_PEEKUSR
50 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
51 #endif
52 #ifndef PTRACE_POKEUSR
53 # define PTRACE_POKEUSR PTRACE_POKEUSER
54 #endif
55 #elif defined(HAVE_LINUX_PTRACE_H)
56 #undef PTRACE_SYSCALL
57 # ifdef HAVE_STRUCT_IA64_FPREG
58 #  define ia64_fpreg XXX_ia64_fpreg
59 # endif
60 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
61 #  define pt_all_user_regs XXX_pt_all_user_regs
62 # endif
63 #include <linux/ptrace.h>
64 # undef ia64_fpreg
65 # undef pt_all_user_regs
66 #endif
67
68
69 #ifdef LINUX
70
71 #ifdef IA64
72 # include <asm/ptrace_offsets.h>
73 #endif /* !IA64 */
74
75 #if defined (LINUX) && defined (SPARC64)
76 # undef PTRACE_GETREGS
77 # define PTRACE_GETREGS PTRACE_GETREGS64
78 # undef PTRACE_SETREGS
79 # define PTRACE_SETREGS PTRACE_SETREGS64
80 #endif /* LINUX && SPARC64 */
81
82 #if defined (SPARC) || defined (SPARC64) || defined (MIPS)
83 typedef struct {
84         struct pt_regs          si_regs;
85         int                     si_mask;
86 } m_siginfo_t;
87 #elif defined HAVE_ASM_SIGCONTEXT_H
88 #if !defined(IA64) && !defined(X86_64)
89 #include <asm/sigcontext.h>
90 #endif /* !IA64 && !X86_64 */
91 #else /* !HAVE_ASM_SIGCONTEXT_H */
92 #if defined I386 && !defined HAVE_STRUCT_SIGCONTEXT_STRUCT
93 struct sigcontext_struct {
94         unsigned short gs, __gsh;
95         unsigned short fs, __fsh;
96         unsigned short es, __esh;
97         unsigned short ds, __dsh;
98         unsigned long edi;
99         unsigned long esi;
100         unsigned long ebp;
101         unsigned long esp;
102         unsigned long ebx;
103         unsigned long edx;
104         unsigned long ecx;
105         unsigned long eax;
106         unsigned long trapno;
107         unsigned long err;
108         unsigned long eip;
109         unsigned short cs, __csh;
110         unsigned long eflags;
111         unsigned long esp_at_signal;
112         unsigned short ss, __ssh;
113         unsigned long i387;
114         unsigned long oldmask;
115         unsigned long cr2;
116 };
117 #else /* !I386 */
118 #if defined M68K && !defined HAVE_STRUCT_SIGCONTEXT
119 struct sigcontext
120 {
121         unsigned long sc_mask;
122         unsigned long sc_usp;
123         unsigned long sc_d0;
124         unsigned long sc_d1;
125         unsigned long sc_a0;
126         unsigned long sc_a1;
127         unsigned short sc_sr;
128         unsigned long sc_pc;
129         unsigned short sc_formatvec;
130 };
131 #endif /* M68K */
132 #endif /* !I386 */
133 #endif /* !HAVE_ASM_SIGCONTEXT_H */
134 #ifndef NSIG
135 #define NSIG 32
136 #endif
137 #ifdef ARM
138 #undef NSIG
139 #define NSIG 32
140 #endif
141 #endif /* LINUX */
142
143 const char *const signalent0[] = {
144 #include "signalent.h"
145 };
146 const int nsignals0 = sizeof signalent0 / sizeof signalent0[0];
147
148 #if SUPPORTED_PERSONALITIES >= 2
149 const char *const signalent1[] = {
150 #include "signalent1.h"
151 };
152 const int nsignals1 = sizeof signalent1 / sizeof signalent1[0];
153 #endif /* SUPPORTED_PERSONALITIES >= 2 */
154
155 #if SUPPORTED_PERSONALITIES >= 3
156 const char *const signalent2[] = {
157 #include "signalent2.h"
158 };
159 const int nsignals2 = sizeof signalent2 / sizeof signalent2[0];
160 #endif /* SUPPORTED_PERSONALITIES >= 3 */
161
162 const char *const *signalent;
163 int nsignals;
164
165 #if defined(SUNOS4) || defined(FREEBSD)
166
167 static const struct xlat sigvec_flags[] = {
168         { SV_ONSTACK,   "SV_ONSTACK"    },
169         { SV_INTERRUPT, "SV_INTERRUPT"  },
170         { SV_RESETHAND, "SV_RESETHAND"  },
171         { SA_NOCLDSTOP, "SA_NOCLDSTOP"  },
172         { 0,            NULL            },
173 };
174
175 #endif /* SUNOS4 || FREEBSD */
176
177 #ifdef HAVE_SIGACTION
178
179 #if defined LINUX && (defined I386 || defined X86_64)
180 /* The libc headers do not define this constant since it should only be
181    used by the implementation.  So wwe define it here.  */
182 # ifndef SA_RESTORER
183 #  define SA_RESTORER 0x04000000
184 # endif
185 #endif
186
187 static const struct xlat sigact_flags[] = {
188 #ifdef SA_RESTORER
189         { SA_RESTORER,  "SA_RESTORER"   },
190 #endif
191 #ifdef SA_STACK
192         { SA_STACK,     "SA_STACK"      },
193 #endif
194 #ifdef SA_RESTART
195         { SA_RESTART,   "SA_RESTART"    },
196 #endif
197 #ifdef SA_INTERRUPT
198         { SA_INTERRUPT, "SA_INTERRUPT"  },
199 #endif
200 #ifdef SA_NODEFER
201         { SA_NODEFER,   "SA_NODEFER"    },
202 #endif
203 #if defined SA_NOMASK && SA_NODEFER != SA_NOMASK
204         { SA_NOMASK,    "SA_NOMASK"     },
205 #endif
206 #ifdef SA_RESETHAND
207         { SA_RESETHAND, "SA_RESETHAND"  },
208 #endif
209 #if defined SA_ONESHOT && SA_ONESHOT != SA_RESETHAND
210         { SA_ONESHOT,   "SA_ONESHOT"    },
211 #endif
212 #ifdef SA_SIGINFO
213         { SA_SIGINFO,   "SA_SIGINFO"    },
214 #endif
215 #ifdef SA_RESETHAND
216         { SA_RESETHAND, "SA_RESETHAND"  },
217 #endif
218 #ifdef SA_ONSTACK
219         { SA_ONSTACK,   "SA_ONSTACK"    },
220 #endif
221 #ifdef SA_NODEFER
222         { SA_NODEFER,   "SA_NODEFER"    },
223 #endif
224 #ifdef SA_NOCLDSTOP
225         { SA_NOCLDSTOP, "SA_NOCLDSTOP"  },
226 #endif
227 #ifdef SA_NOCLDWAIT
228         { SA_NOCLDWAIT, "SA_NOCLDWAIT"  },
229 #endif
230 #ifdef _SA_BSDCALL
231         { _SA_BSDCALL,  "_SA_BSDCALL"   },
232 #endif
233 #ifdef SA_NOPTRACE
234         { SA_NOPTRACE,  "SA_NOPTRACE"   },
235 #endif
236         { 0,            NULL            },
237 };
238
239 static const struct xlat sigprocmaskcmds[] = {
240         { SIG_BLOCK,    "SIG_BLOCK"     },
241         { SIG_UNBLOCK,  "SIG_UNBLOCK"   },
242         { SIG_SETMASK,  "SIG_SETMASK"   },
243 #ifdef SIG_SETMASK32
244         { SIG_SETMASK32,"SIG_SETMASK32" },
245 #endif
246         { 0,            NULL            },
247 };
248
249 #endif /* HAVE_SIGACTION */
250
251 /* Anonymous realtime signals. */
252 /* Under glibc 2.1, SIGRTMIN et al are functions, but __SIGRTMIN is a
253    constant.  This is what we want.  Otherwise, just use SIGRTMIN. */
254 #ifdef SIGRTMIN
255 #ifndef __SIGRTMIN
256 #define __SIGRTMIN SIGRTMIN
257 #define __SIGRTMAX SIGRTMAX /* likewise */
258 #endif
259 #endif
260
261 const char *
262 signame(sig)
263 int sig;
264 {
265         static char buf[30];
266         if (sig >= 0 && sig < nsignals) {
267                 return signalent[sig];
268 #ifdef SIGRTMIN
269         } else if (sig >= __SIGRTMIN && sig <= __SIGRTMAX) {
270                 sprintf(buf, "SIGRT_%ld", (long)(sig - __SIGRTMIN));
271                 return buf;
272 #endif /* SIGRTMIN */
273         } else {
274                 sprintf(buf, "%d", sig);
275                 return buf;
276         }
277 }
278
279 #ifndef UNIXWARE
280 static void
281 long_to_sigset(l, s)
282 long l;
283 sigset_t *s;
284 {
285         sigemptyset(s);
286         *(long *)s = l;
287 }
288 #endif
289
290 static int
291 copy_sigset_len(struct tcb *tcp, long addr, sigset_t *s, int len)
292 {
293         if (len > sizeof(*s))
294                 len = sizeof(*s);
295         sigemptyset(s);
296         if (umoven(tcp, addr, len, (char *)s) < 0)
297                 return -1;
298         return 0;
299 }
300
301 #ifdef LINUX
302 /* Original sigset is unsigned long */
303 #define copy_sigset(tcp, addr, s) copy_sigset_len(tcp, addr, s, sizeof(long))
304 #else
305 #define copy_sigset(tcp, addr, s) copy_sigset_len(tcp, addr, s, sizeof(sigset_t))
306 #endif
307
308 static const char *
309 sprintsigmask(const char *str, sigset_t *mask, int rt)
310 /* set might include realtime sigs */
311 {
312         int i, nsigs;
313         int maxsigs;
314         const char *format;
315         char *s;
316         static char outstr[8 * sizeof(sigset_t) * 8];
317
318         strcpy(outstr, str);
319         s = outstr + strlen(outstr);
320         nsigs = 0;
321         maxsigs = nsignals;
322 #ifdef __SIGRTMAX
323         if (rt)
324                 maxsigs = __SIGRTMAX; /* instead */
325 #endif
326         for (i = 1; i < maxsigs; i++) {
327                 if (sigismember(mask, i) == 1)
328                         nsigs++;
329         }
330         if (nsigs >= nsignals * 2 / 3) {
331                 *s++ = '~';
332                 for (i = 1; i < maxsigs; i++) {
333                         switch (sigismember(mask, i)) {
334                         case 1:
335                                 sigdelset(mask, i);
336                                 break;
337                         case 0:
338                                 sigaddset(mask, i);
339                                 break;
340                         }
341                 }
342         }
343         format = "%s";
344         *s++ = '[';
345         for (i = 1; i < maxsigs; i++) {
346                 if (sigismember(mask, i) == 1) {
347                         /* real-time signals on solaris don't have
348                          * signalent entries
349                          */
350                         if (i < nsignals) {
351                                 sprintf(s, format, signalent[i] + 3);
352                         }
353 #ifdef SIGRTMIN
354                         else if (i >= __SIGRTMIN && i <= __SIGRTMAX) {
355                                 char tsig[40];
356                                 sprintf(tsig, "RT_%u", i - __SIGRTMIN);
357                                 sprintf(s, format, tsig);
358                         }
359 #endif /* SIGRTMIN */
360                         else {
361                                 char tsig[32];
362                                 sprintf(tsig, "%u", i);
363                                 sprintf(s, format, tsig);
364                         }
365                         s += strlen(s);
366                         format = " %s";
367                 }
368         }
369         *s++ = ']';
370         *s = '\0';
371         return outstr;
372 }
373
374 static void
375 printsigmask(mask, rt)
376 sigset_t *mask;
377 int rt;
378 {
379         tprintf("%s", sprintsigmask("", mask, rt));
380 }
381
382 void
383 printsignal(nr)
384 int nr;
385 {
386         tprintf("%s", signame(nr));
387 }
388
389 void
390 print_sigset(struct tcb *tcp, long addr, int rt)
391 {
392         sigset_t ss;
393
394         if (!addr)
395                 tprintf("NULL");
396         else if (copy_sigset(tcp, addr, &ss) < 0)
397                 tprintf("%#lx", addr);
398         else
399                 printsigmask(&ss, rt);
400 }
401
402 #ifdef LINUX
403
404 #ifndef ILL_ILLOPC
405 #define ILL_ILLOPC      1       /* illegal opcode */
406 #define ILL_ILLOPN      2       /* illegal operand */
407 #define ILL_ILLADR      3       /* illegal addressing mode */
408 #define ILL_ILLTRP      4       /* illegal trap */
409 #define ILL_PRVOPC      5       /* privileged opcode */
410 #define ILL_PRVREG      6       /* privileged register */
411 #define ILL_COPROC      7       /* coprocessor error */
412 #define ILL_BADSTK      8       /* internal stack error */
413 #define FPE_INTDIV      1       /* integer divide by zero */
414 #define FPE_INTOVF      2       /* integer overflow */
415 #define FPE_FLTDIV      3       /* floating point divide by zero */
416 #define FPE_FLTOVF      4       /* floating point overflow */
417 #define FPE_FLTUND      5       /* floating point underflow */
418 #define FPE_FLTRES      6       /* floating point inexact result */
419 #define FPE_FLTINV      7       /* floating point invalid operation */
420 #define FPE_FLTSUB      8       /* subscript out of range */
421 #define SEGV_MAPERR     1       /* address not mapped to object */
422 #define SEGV_ACCERR     2       /* invalid permissions for mapped object */
423 #define BUS_ADRALN      1       /* invalid address alignment */
424 #define BUS_ADRERR      2       /* non-existant physical address */
425 #define BUS_OBJERR      3       /* object specific hardware error */
426 #define TRAP_BRKPT      1       /* process breakpoint */
427 #define TRAP_TRACE      2       /* process trace trap */
428 #define CLD_EXITED      1       /* child has exited */
429 #define CLD_KILLED      2       /* child was killed */
430 #define CLD_DUMPED      3       /* child terminated abnormally */
431 #define CLD_TRAPPED     4       /* traced child has trapped */
432 #define CLD_STOPPED     5       /* child has stopped */
433 #define CLD_CONTINUED   6       /* stopped child has continued */
434 #define POLL_IN         1       /* data input available */
435 #define POLL_OUT        2       /* output buffers available */
436 #define POLL_MSG        3       /* input message available */
437 #define POLL_ERR        4       /* i/o error */
438 #define POLL_PRI        5       /* high priority input available */
439 #define POLL_HUP        6       /* device disconnected */
440 #define SI_KERNEL       0x80    /* sent by kernel */
441 #define SI_USER         0       /* sent by kill, sigsend, raise */
442 #define SI_QUEUE        -1      /* sent by sigqueue */
443 #define SI_TIMER        -2      /* sent by timer expiration */
444 #define SI_MESGQ        -3      /* sent by real time mesq state change */
445 #define SI_ASYNCIO      -4      /* sent by AIO completion */
446 #define SI_SIGIO        -5      /* sent by SIGIO */
447 #define SI_TKILL        -6      /* sent by tkill */
448 #define SI_ASYNCNL      -60     /* sent by asynch name lookup completion */
449
450 #define SI_FROMUSER(sip)        ((sip)->si_code <= 0)
451
452 #endif /* LINUX */
453
454 #if __GLIBC_MINOR__ < 1
455 /* Type for data associated with a signal.  */
456 typedef union sigval
457 {
458         int sival_int;
459         void *sival_ptr;
460 } sigval_t;
461
462 # define __SI_MAX_SIZE     128
463 # define __SI_PAD_SIZE     ((__SI_MAX_SIZE / sizeof(int)) - 3)
464
465 typedef struct siginfo
466 {
467         int si_signo;               /* Signal number.  */
468         int si_errno;               /* If non-zero, an errno value associated with
469                                                                    this signal, as defined in <errno.h>.  */
470         int si_code;                /* Signal code.  */
471
472         union
473         {
474                 int _pad[__SI_PAD_SIZE];
475
476                 /* kill().  */
477                 struct
478                 {
479                         __pid_t si_pid;     /* Sending process ID.  */
480                         __uid_t si_uid;     /* Real user ID of sending process.  */
481                 } _kill;
482
483                 /* POSIX.1b timers.  */
484                 struct
485                 {
486                         unsigned int _timer1;
487                         unsigned int _timer2;
488                 } _timer;
489
490                 /* POSIX.1b signals.  */
491                 struct
492                 {
493                         __pid_t si_pid;     /* Sending process ID.  */
494                         __uid_t si_uid;     /* Real user ID of sending process.  */
495                         sigval_t si_sigval; /* Signal value.  */
496                 } _rt;
497
498                 /* SIGCHLD.  */
499                 struct
500                 {
501                         __pid_t si_pid;     /* Which child.  */
502                         int si_status;      /* Exit value or signal.  */
503                         __clock_t si_utime;
504                         __clock_t si_stime;
505                 } _sigchld;
506
507                 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS.  */
508                 struct
509                 {
510                         void *si_addr;      /* Faulting insn/memory ref.  */
511                 } _sigfault;
512
513                 /* SIGPOLL.  */
514                 struct
515                 {
516                         int si_band;        /* Band event for SIGPOLL.  */
517                         int si_fd;
518                 } _sigpoll;
519         } _sifields;
520 } siginfo_t;
521
522 #define si_pid          _sifields._kill.si_pid
523 #define si_uid          _sifields._kill.si_uid
524 #define si_status       _sifields._sigchld.si_status
525 #define si_utime        _sifields._sigchld.si_utime
526 #define si_stime        _sifields._sigchld.si_stime
527 #define si_value        _sifields._rt.si_sigval
528 #define si_int          _sifields._rt.si_sigval.sival_int
529 #define si_ptr          _sifields._rt.si_sigval.sival_ptr
530 #define si_addr         _sifields._sigfault.si_addr
531 #define si_band         _sifields._sigpoll.si_band
532 #define si_fd           _sifields._sigpoll.si_fd
533
534 #endif
535
536 #endif
537
538 #if defined (SVR4) || defined (LINUX)
539
540 static const struct xlat siginfo_codes[] = {
541 #ifdef SI_KERNEL
542         { SI_KERNEL,    "SI_KERNEL"     },
543 #endif
544 #ifdef SI_USER
545         { SI_USER,      "SI_USER"       },
546 #endif
547 #ifdef SI_QUEUE
548         { SI_QUEUE,     "SI_QUEUE"      },
549 #endif
550 #ifdef SI_TIMER
551         { SI_TIMER,     "SI_TIMER"      },
552 #endif
553 #ifdef SI_MESGQ
554         { SI_MESGQ,     "SI_MESGQ"      },
555 #endif
556 #ifdef SI_ASYNCIO
557         { SI_ASYNCIO,   "SI_ASYNCIO"    },
558 #endif
559 #ifdef SI_SIGIO
560         { SI_SIGIO,     "SI_SIGIO"      },
561 #endif
562 #ifdef SI_TKILL
563         { SI_TKILL,     "SI_TKILL"      },
564 #endif
565 #ifdef SI_ASYNCNL
566         { SI_ASYNCNL,   "SI_ASYNCNL"    },
567 #endif
568 #ifdef SI_NOINFO
569         { SI_NOINFO,    "SI_NOINFO"     },
570 #endif
571 #ifdef SI_LWP
572         { SI_LWP,       "SI_LWP"        },
573 #endif
574         { 0,            NULL            },
575 };
576
577 static const struct xlat sigill_codes[] = {
578         { ILL_ILLOPC,   "ILL_ILLOPC"    },
579         { ILL_ILLOPN,   "ILL_ILLOPN"    },
580         { ILL_ILLADR,   "ILL_ILLADR"    },
581         { ILL_ILLTRP,   "ILL_ILLTRP"    },
582         { ILL_PRVOPC,   "ILL_PRVOPC"    },
583         { ILL_PRVREG,   "ILL_PRVREG"    },
584         { ILL_COPROC,   "ILL_COPROC"    },
585         { ILL_BADSTK,   "ILL_BADSTK"    },
586         { 0,            NULL            },
587 };
588
589 static const struct xlat sigfpe_codes[] = {
590         { FPE_INTDIV,   "FPE_INTDIV"    },
591         { FPE_INTOVF,   "FPE_INTOVF"    },
592         { FPE_FLTDIV,   "FPE_FLTDIV"    },
593         { FPE_FLTOVF,   "FPE_FLTOVF"    },
594         { FPE_FLTUND,   "FPE_FLTUND"    },
595         { FPE_FLTRES,   "FPE_FLTRES"    },
596         { FPE_FLTINV,   "FPE_FLTINV"    },
597         { FPE_FLTSUB,   "FPE_FLTSUB"    },
598         { 0,            NULL            },
599 };
600
601 static const struct xlat sigtrap_codes[] = {
602         { TRAP_BRKPT,   "TRAP_BRKPT"    },
603         { TRAP_TRACE,   "TRAP_TRACE"    },
604         { 0,            NULL            },
605 };
606
607 static const struct xlat sigchld_codes[] = {
608         { CLD_EXITED,   "CLD_EXITED"    },
609         { CLD_KILLED,   "CLD_KILLED"    },
610         { CLD_DUMPED,   "CLD_DUMPED"    },
611         { CLD_TRAPPED,  "CLD_TRAPPED"   },
612         { CLD_STOPPED,  "CLD_STOPPED"   },
613         { CLD_CONTINUED,"CLD_CONTINUED" },
614         { 0,            NULL            },
615 };
616
617 static const struct xlat sigpoll_codes[] = {
618         { POLL_IN,      "POLL_IN"       },
619         { POLL_OUT,     "POLL_OUT"      },
620         { POLL_MSG,     "POLL_MSG"      },
621         { POLL_ERR,     "POLL_ERR"      },
622         { POLL_PRI,     "POLL_PRI"      },
623         { POLL_HUP,     "POLL_HUP"      },
624         { 0,            NULL            },
625 };
626
627 static const struct xlat sigprof_codes[] = {
628 #ifdef PROF_SIG
629         { PROF_SIG,     "PROF_SIG"      },
630 #endif
631         { 0,            NULL            },
632 };
633
634 #ifdef SIGEMT
635 static const struct xlat sigemt_codes[] = {
636 #ifdef EMT_TAGOVF
637         { EMT_TAGOVF,   "EMT_TAGOVF"    },
638 #endif
639         { 0,            NULL            },
640 };
641 #endif
642
643 static const struct xlat sigsegv_codes[] = {
644         { SEGV_MAPERR,  "SEGV_MAPERR"   },
645         { SEGV_ACCERR,  "SEGV_ACCERR"   },
646         { 0,            NULL            },
647 };
648
649 static const struct xlat sigbus_codes[] = {
650         { BUS_ADRALN,   "BUS_ADRALN"    },
651         { BUS_ADRERR,   "BUS_ADRERR"    },
652         { BUS_OBJERR,   "BUS_OBJERR"    },
653         { 0,            NULL            },
654 };
655
656 void
657 printsiginfo(siginfo_t *sip, int verbose)
658 {
659         const char *code;
660
661         if (sip->si_signo == 0) {
662                 tprintf("{}");
663                 return;
664         }
665         tprintf("{si_signo=");
666         printsignal(sip->si_signo);
667         code = xlookup(siginfo_codes, sip->si_code);
668         if (!code) {
669                 switch (sip->si_signo) {
670                 case SIGTRAP:
671                         code = xlookup(sigtrap_codes, sip->si_code);
672                         break;
673                 case SIGCHLD:
674                         code = xlookup(sigchld_codes, sip->si_code);
675                         break;
676                 case SIGPOLL:
677                         code = xlookup(sigpoll_codes, sip->si_code);
678                         break;
679                 case SIGPROF:
680                         code = xlookup(sigprof_codes, sip->si_code);
681                         break;
682                 case SIGILL:
683                         code = xlookup(sigill_codes, sip->si_code);
684                         break;
685 #ifdef SIGEMT
686                 case SIGEMT:
687                         code = xlookup(sigemt_codes, sip->si_code);
688                         break;
689 #endif
690                 case SIGFPE:
691                         code = xlookup(sigfpe_codes, sip->si_code);
692                         break;
693                 case SIGSEGV:
694                         code = xlookup(sigsegv_codes, sip->si_code);
695                         break;
696                 case SIGBUS:
697                         code = xlookup(sigbus_codes, sip->si_code);
698                         break;
699                 }
700         }
701         if (code)
702                 tprintf(", si_code=%s", code);
703         else
704                 tprintf(", si_code=%#x", sip->si_code);
705 #ifdef SI_NOINFO
706         if (sip->si_code != SI_NOINFO)
707 #endif
708         {
709                 if (sip->si_errno) {
710                         if (sip->si_errno < 0 || sip->si_errno >= nerrnos)
711                                 tprintf(", si_errno=%d", sip->si_errno);
712                         else
713                                 tprintf(", si_errno=%s",
714                                         errnoent[sip->si_errno]);
715                 }
716 #ifdef SI_FROMUSER
717                 if (SI_FROMUSER(sip)) {
718                         tprintf(", si_pid=%lu, si_uid=%lu",
719                                 (unsigned long) sip->si_pid,
720                                 (unsigned long) sip->si_uid);
721                         switch (sip->si_code) {
722 #ifdef SI_USER
723                         case SI_USER:
724                                 break;
725 #endif
726 #ifdef SI_TKILL
727                         case SI_TKILL:
728                                 break;
729 #endif
730 #ifdef SI_TIMER
731                         case SI_TIMER:
732                                 tprintf(", si_value=%d", sip->si_int);
733                                 break;
734 #endif
735 #ifdef LINUX
736                         default:
737                                 if (!sip->si_ptr)
738                                         break;
739                                 if (!verbose)
740                                         tprintf(", ...");
741                                 else
742                                         tprintf(", si_value={int=%u, ptr=%#lx}",
743                                                 sip->si_int,
744                                                 (unsigned long) sip->si_ptr);
745                                 break;
746 #endif
747                         }
748                 }
749                 else
750 #endif /* SI_FROMUSER */
751                 {
752                         switch (sip->si_signo) {
753                         case SIGCHLD:
754                                 tprintf(", si_pid=%ld, si_status=",
755                                         (long) sip->si_pid);
756                                 if (sip->si_code == CLD_EXITED)
757                                         tprintf("%d", sip->si_status);
758                                 else
759                                         printsignal(sip->si_status);
760 #if LINUX
761                                 if (!verbose)
762                                         tprintf(", ...");
763                                 else
764                                         tprintf(", si_utime=%lu, si_stime=%lu",
765                                                 sip->si_utime,
766                                                 sip->si_stime);
767 #endif
768                                 break;
769                         case SIGILL: case SIGFPE:
770                         case SIGSEGV: case SIGBUS:
771                                 tprintf(", si_addr=%#lx",
772                                         (unsigned long) sip->si_addr);
773                                 break;
774                         case SIGPOLL:
775                                 switch (sip->si_code) {
776                                 case POLL_IN: case POLL_OUT: case POLL_MSG:
777                                         tprintf(", si_band=%ld",
778                                                 (long) sip->si_band);
779                                         break;
780                                 }
781                                 break;
782 #ifdef LINUX
783                         default:
784                                 if (sip->si_pid || sip->si_uid)
785                                         tprintf(", si_pid=%lu, si_uid=%lu",
786                                                 (unsigned long) sip->si_pid,
787                                                 (unsigned long) sip->si_uid);
788                                 if (!sip->si_ptr)
789                                         break;
790                                 if (!verbose)
791                                         tprintf(", ...");
792                                 else {
793                                         tprintf(", si_value={int=%u, ptr=%#lx}",
794                                                 sip->si_int,
795                                                 (unsigned long) sip->si_ptr);
796                                 }
797 #endif
798
799                         }
800                 }
801         }
802         tprintf("}");
803 }
804
805 #endif /* SVR4 || LINUX */
806
807 #ifdef LINUX
808
809 static void
810 parse_sigset_t(const char *str, sigset_t *set)
811 {
812         const char *p;
813         unsigned int digit;
814         int i;
815
816         sigemptyset(set);
817
818         p = strchr(str, '\n');
819         if (p == NULL)
820                 p = strchr(str, '\0');
821         for (i = 0; p-- > str; i += 4) {
822                 if (*p >= '0' && *p <= '9')
823                         digit = *p - '0';
824                 else if (*p >= 'a' && *p <= 'f')
825                         digit = *p - 'a' + 10;
826                 else if (*p >= 'A' && *p <= 'F')
827                         digit = *p - 'A' + 10;
828                 else
829                         break;
830                 if (digit & 1)
831                         sigaddset(set, i + 1);
832                 if (digit & 2)
833                         sigaddset(set, i + 2);
834                 if (digit & 4)
835                         sigaddset(set, i + 3);
836                 if (digit & 8)
837                         sigaddset(set, i + 4);
838         }
839 }
840
841 #endif
842
843 /*
844  * Check process TCP for the disposition of signal SIG.
845  * Return 1 if the process would somehow manage to  survive signal SIG,
846  * else return 0.  This routine will never be called with SIGKILL.
847  */
848 int
849 sigishandled(struct tcb *tcp, int sig)
850 {
851 #ifdef LINUX
852         int sfd;
853         char sname[32];
854         char buf[2048];
855         const char *s;
856         int i;
857         sigset_t ignored, caught;
858 #endif
859 #ifdef SVR4
860         /*
861          * Since procfs doesn't interfere with wait I think it is safe
862          * to punt on this question.  If not, the information is there.
863          */
864         return 1;
865 #else /* !SVR4 */
866         switch (sig) {
867         case SIGCONT:
868         case SIGSTOP:
869         case SIGTSTP:
870         case SIGTTIN:
871         case SIGTTOU:
872         case SIGCHLD:
873         case SIGIO:
874 #if defined(SIGURG) && SIGURG != SIGIO
875         case SIGURG:
876 #endif
877         case SIGWINCH:
878                 /* Gloria Gaynor says ... */
879                 return 1;
880         default:
881                 break;
882         }
883 #endif /* !SVR4 */
884 #ifdef LINUX
885
886         /* This is incredibly costly but it's worth it. */
887         /* NOTE: LinuxThreads internally uses SIGRTMIN, SIGRTMIN + 1 and
888            SIGRTMIN + 2, so we can't use the obsolete /proc/%d/stat which
889            doesn't handle real-time signals). */
890         sprintf(sname, "/proc/%d/status", tcp->pid);
891         if ((sfd = open(sname, O_RDONLY)) == -1) {
892                 perror(sname);
893                 return 1;
894         }
895         i = read(sfd, buf, sizeof(buf));
896         buf[i] = '\0';
897         close(sfd);
898         /*
899          * Skip the extraneous fields. We need to skip
900          * command name has any spaces in it.  So be it.
901          */
902         s = strstr(buf, "SigIgn:\t");
903         if (!s)
904         {
905                 fprintf(stderr, "/proc/pid/status format error\n");
906                 return 1;
907         }
908         parse_sigset_t(s + 8, &ignored);
909
910         s = strstr(buf, "SigCgt:\t");
911         if (!s)
912         {
913                 fprintf(stderr, "/proc/pid/status format error\n");
914                 return 1;
915         }
916         parse_sigset_t(s + 8, &caught);
917
918 #ifdef DEBUG
919         fprintf(stderr, "sigs: %016qx %016qx (sig=%d)\n",
920                 *(long long *) &ignored, *(long long *) &caught, sig);
921 #endif
922         if (sigismember(&ignored, sig) || sigismember(&caught, sig))
923                 return 1;
924 #endif /* LINUX */
925
926 #ifdef SUNOS4
927         void (*u_signal)();
928
929         if (upeek(tcp, uoff(u_signal[0]) + sig*sizeof(u_signal),
930             (long *) &u_signal) < 0) {
931                 return 0;
932         }
933         if (u_signal != SIG_DFL)
934                 return 1;
935 #endif /* SUNOS4 */
936
937         return 0;
938 }
939
940 #if defined(SUNOS4) || defined(FREEBSD)
941
942 int
943 sys_sigvec(struct tcb *tcp)
944 {
945         struct sigvec sv;
946         long addr;
947
948         if (entering(tcp)) {
949                 printsignal(tcp->u_arg[0]);
950                 tprintf(", ");
951                 addr = tcp->u_arg[1];
952         } else {
953                 addr = tcp->u_arg[2];
954         }
955         if (addr == 0)
956                 tprintf("NULL");
957         else if (!verbose(tcp))
958                 tprintf("%#lx", addr);
959         else if (umove(tcp, addr, &sv) < 0)
960                 tprintf("{...}");
961         else {
962                 switch ((int) sv.sv_handler) {
963                 case (int) SIG_ERR:
964                         tprintf("{SIG_ERR}");
965                         break;
966                 case (int) SIG_DFL:
967                         tprintf("{SIG_DFL}");
968                         break;
969                 case (int) SIG_IGN:
970                         if (tcp->u_arg[0] == SIGTRAP) {
971                                 tcp->flags |= TCB_SIGTRAPPED;
972                                 kill(tcp->pid, SIGSTOP);
973                         }
974                         tprintf("{SIG_IGN}");
975                         break;
976                 case (int) SIG_HOLD:
977                         if (tcp->u_arg[0] == SIGTRAP) {
978                                 tcp->flags |= TCB_SIGTRAPPED;
979                                 kill(tcp->pid, SIGSTOP);
980                         }
981                         tprintf("SIG_HOLD");
982                         break;
983                 default:
984                         if (tcp->u_arg[0] == SIGTRAP) {
985                                 tcp->flags |= TCB_SIGTRAPPED;
986                                 kill(tcp->pid, SIGSTOP);
987                         }
988                         tprintf("{%#lx, ", (unsigned long) sv.sv_handler);
989                         printsigmask(&sv.sv_mask, 0);
990                         tprintf(", ");
991                         printflags(sigvec_flags, sv.sv_flags, "SV_???");
992                         tprintf("}");
993                 }
994         }
995         if (entering(tcp))
996                 tprintf(", ");
997         return 0;
998 }
999
1000 int
1001 sys_sigpause(struct tcb *tcp)
1002 {
1003         if (entering(tcp)) {    /* WTA: UD had a bug here: he forgot the braces */
1004                 sigset_t sigm;
1005                 long_to_sigset(tcp->u_arg[0], &sigm);
1006                 printsigmask(&sigm, 0);
1007         }
1008         return 0;
1009 }
1010
1011 int
1012 sys_sigstack(struct tcb *tcp)
1013 {
1014         struct sigstack ss;
1015         long addr;
1016
1017         if (entering(tcp))
1018                 addr = tcp->u_arg[0];
1019         else
1020                 addr = tcp->u_arg[1];
1021         if (addr == 0)
1022                 tprintf("NULL");
1023         else if (umove(tcp, addr, &ss) < 0)
1024                 tprintf("%#lx", addr);
1025         else {
1026                 tprintf("{ss_sp %#lx ", (unsigned long) ss.ss_sp);
1027                 tprintf("ss_onstack %s}", ss.ss_onstack ? "YES" : "NO");
1028         }
1029         if (entering(tcp))
1030                 tprintf(", ");
1031         return 0;
1032 }
1033
1034 int
1035 sys_sigcleanup(struct tcb *tcp)
1036 {
1037         return 0;
1038 }
1039
1040 #endif /* SUNOS4 || FREEBSD */
1041
1042 #ifndef SVR4
1043
1044 int
1045 sys_sigsetmask(struct tcb *tcp)
1046 {
1047         if (entering(tcp)) {
1048                 sigset_t sigm;
1049                 long_to_sigset(tcp->u_arg[0], &sigm);
1050                 printsigmask(&sigm, 0);
1051 #ifndef USE_PROCFS
1052                 if ((tcp->u_arg[0] & sigmask(SIGTRAP))) {
1053                         /* Mark attempt to block SIGTRAP */
1054                         tcp->flags |= TCB_SIGTRAPPED;
1055                         /* Send unblockable signal */
1056                         kill(tcp->pid, SIGSTOP);
1057                 }
1058 #endif /* !USE_PROCFS */
1059         }
1060         else if (!syserror(tcp)) {
1061                 sigset_t sigm;
1062                 long_to_sigset(tcp->u_rval, &sigm);
1063                 tcp->auxstr = sprintsigmask("old mask ", &sigm, 0);
1064
1065                 return RVAL_HEX | RVAL_STR;
1066         }
1067         return 0;
1068 }
1069
1070 #if defined(SUNOS4) || defined(FREEBSD)
1071 int
1072 sys_sigblock(struct tcb *tcp)
1073 {
1074         return sys_sigsetmask(tcp);
1075 }
1076 #endif /* SUNOS4 || FREEBSD */
1077
1078 #endif /* !SVR4 */
1079
1080 #ifdef HAVE_SIGACTION
1081
1082 #ifdef LINUX
1083 struct old_sigaction {
1084         __sighandler_t __sa_handler;
1085         unsigned long sa_mask;
1086         unsigned long sa_flags;
1087         void (*sa_restorer)(void);
1088 };
1089 #define SA_HANDLER __sa_handler
1090 #endif /* LINUX */
1091
1092 #ifndef SA_HANDLER
1093 #define SA_HANDLER sa_handler
1094 #endif
1095
1096 int
1097 sys_sigaction(struct tcb *tcp)
1098 {
1099         long addr;
1100 #ifdef LINUX
1101         sigset_t sigset;
1102         struct old_sigaction sa;
1103 #else
1104         struct sigaction sa;
1105 #endif
1106
1107
1108         if (entering(tcp)) {
1109                 printsignal(tcp->u_arg[0]);
1110                 tprintf(", ");
1111                 addr = tcp->u_arg[1];
1112         } else
1113                 addr = tcp->u_arg[2];
1114         if (addr == 0)
1115                 tprintf("NULL");
1116         else if (!verbose(tcp))
1117                 tprintf("%#lx", addr);
1118         else if (umove(tcp, addr, &sa) < 0)
1119                 tprintf("{...}");
1120         else {
1121                 /* Architectures using function pointers, like
1122                  * hppa, may need to manipulate the function pointer
1123                  * to compute the result of a comparison. However,
1124                  * the SA_HANDLER function pointer exists only in
1125                  * the address space of the traced process, and can't
1126                  * be manipulated by strace. In order to prevent the
1127                  * compiler from generating code to manipulate
1128                  * SA_HANDLER we cast the function pointers to long. */
1129                 if ((long)sa.SA_HANDLER == (long)SIG_ERR)
1130                         tprintf("{SIG_ERR, ");
1131                 else if ((long)sa.SA_HANDLER == (long)SIG_DFL)
1132                         tprintf("{SIG_DFL, ");
1133                 else if ((long)sa.SA_HANDLER == (long)SIG_IGN) {
1134 #ifndef USE_PROCFS
1135                         if (tcp->u_arg[0] == SIGTRAP) {
1136                                 tcp->flags |= TCB_SIGTRAPPED;
1137                                 kill(tcp->pid, SIGSTOP);
1138                         }
1139 #endif /* !USE_PROCFS */
1140                         tprintf("{SIG_IGN, ");
1141                 }
1142                 else {
1143 #ifndef USE_PROCFS
1144                         if (tcp->u_arg[0] == SIGTRAP) {
1145                                 tcp->flags |= TCB_SIGTRAPPED;
1146                                 kill(tcp->pid, SIGSTOP);
1147                         }
1148 #endif /* !USE_PROCFS */
1149                         tprintf("{%#lx, ", (long) sa.SA_HANDLER);
1150 #ifndef LINUX
1151                         printsigmask(&sa.sa_mask, 0);
1152 #else
1153                         long_to_sigset(sa.sa_mask, &sigset);
1154                         printsigmask(&sigset, 0);
1155 #endif
1156                         tprintf(", ");
1157                         printflags(sigact_flags, sa.sa_flags, "SA_???");
1158 #ifdef SA_RESTORER
1159                         if (sa.sa_flags & SA_RESTORER)
1160                                 tprintf(", %p", sa.sa_restorer);
1161 #endif
1162                         tprintf("}");
1163                 }
1164         }
1165         if (entering(tcp))
1166                 tprintf(", ");
1167 #ifdef LINUX
1168         else
1169                 tprintf(", %#lx", (unsigned long) sa.sa_restorer);
1170 #endif
1171         return 0;
1172 }
1173
1174 int
1175 sys_signal(struct tcb *tcp)
1176 {
1177         if (entering(tcp)) {
1178                 printsignal(tcp->u_arg[0]);
1179                 tprintf(", ");
1180                 switch (tcp->u_arg[1]) {
1181                 case (long) SIG_ERR:
1182                         tprintf("SIG_ERR");
1183                         break;
1184                 case (long) SIG_DFL:
1185                         tprintf("SIG_DFL");
1186                         break;
1187                 case (long) SIG_IGN:
1188 #ifndef USE_PROCFS
1189                         if (tcp->u_arg[0] == SIGTRAP) {
1190                                 tcp->flags |= TCB_SIGTRAPPED;
1191                                 kill(tcp->pid, SIGSTOP);
1192                         }
1193 #endif /* !USE_PROCFS */
1194                         tprintf("SIG_IGN");
1195                         break;
1196                 default:
1197 #ifndef USE_PROCFS
1198                         if (tcp->u_arg[0] == SIGTRAP) {
1199                                 tcp->flags |= TCB_SIGTRAPPED;
1200                                 kill(tcp->pid, SIGSTOP);
1201                         }
1202 #endif /* !USE_PROCFS */
1203                         tprintf("%#lx", tcp->u_arg[1]);
1204                 }
1205                 return 0;
1206         }
1207         else if (!syserror(tcp)) {
1208                 switch (tcp->u_rval) {
1209                     case (long) SIG_ERR:
1210                         tcp->auxstr = "SIG_ERR"; break;
1211                     case (long) SIG_DFL:
1212                         tcp->auxstr = "SIG_DFL"; break;
1213                     case (long) SIG_IGN:
1214                         tcp->auxstr = "SIG_IGN"; break;
1215                     default:
1216                         tcp->auxstr = NULL;
1217                 }
1218                 return RVAL_HEX | RVAL_STR;
1219         }
1220         return 0;
1221 }
1222
1223 #ifdef SVR4
1224 int
1225 sys_sighold(struct tcb *tcp)
1226 {
1227         if (entering(tcp)) {
1228                 printsignal(tcp->u_arg[0]);
1229         }
1230         return 0;
1231 }
1232 #endif /* SVR4 */
1233
1234 #endif /* HAVE_SIGACTION */
1235
1236 #ifdef LINUX
1237
1238 int
1239 sys_sigreturn(struct tcb *tcp)
1240 {
1241 #if defined(ARM)
1242         struct pt_regs regs;
1243         struct sigcontext_struct sc;
1244
1245         if (entering(tcp)) {
1246                 tcp->u_arg[0] = 0;
1247
1248                 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)&regs) == -1)
1249                         return 0;
1250
1251                 if (umove(tcp, regs.ARM_sp, &sc) < 0)
1252                         return 0;
1253
1254                 tcp->u_arg[0] = 1;
1255                 tcp->u_arg[1] = sc.oldmask;
1256         } else {
1257                 sigset_t sigm;
1258                 long_to_sigset(tcp->u_arg[1], &sigm);
1259                 tcp->u_rval = tcp->u_error = 0;
1260                 if (tcp->u_arg[0] == 0)
1261                         return 0;
1262                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1263                 return RVAL_NONE | RVAL_STR;
1264         }
1265         return 0;
1266 #elif defined(S390) || defined(S390X)
1267         long usp;
1268         struct sigcontext_struct sc;
1269
1270         if (entering(tcp)) {
1271                 tcp->u_arg[0] = 0;
1272                 if (upeek(tcp, PT_GPR15, &usp) < 0)
1273                         return 0;
1274                 if (umove(tcp, usp+__SIGNAL_FRAMESIZE, &sc) < 0)
1275                         return 0;
1276                 tcp->u_arg[0] = 1;
1277                 memcpy(&tcp->u_arg[1], &sc.oldmask[0], sizeof(sigset_t));
1278         } else {
1279                 tcp->u_rval = tcp->u_error = 0;
1280                 if (tcp->u_arg[0] == 0)
1281                         return 0;
1282                 tcp->auxstr = sprintsigmask("mask now ", (sigset_t *)&tcp->u_arg[1], 0);
1283                 return RVAL_NONE | RVAL_STR;
1284         }
1285         return 0;
1286 #elif defined(I386)
1287         long esp;
1288         struct sigcontext_struct sc;
1289
1290         if (entering(tcp)) {
1291                 tcp->u_arg[0] = 0;
1292                 if (upeek(tcp, 4*UESP, &esp) < 0)
1293                         return 0;
1294                 if (umove(tcp, esp, &sc) < 0)
1295                         return 0;
1296                 tcp->u_arg[0] = 1;
1297                 tcp->u_arg[1] = sc.oldmask;
1298         }
1299         else {
1300                 sigset_t sigm;
1301                 long_to_sigset(tcp->u_arg[1], &sigm);
1302                 tcp->u_rval = tcp->u_error = 0;
1303                 if (tcp->u_arg[0] == 0)
1304                         return 0;
1305                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1306                 return RVAL_NONE | RVAL_STR;
1307         }
1308         return 0;
1309 #elif defined(IA64)
1310         struct sigcontext sc;
1311         long sp;
1312
1313         if (entering(tcp)) {
1314                 /* offset of sigcontext in the kernel's sigframe structure: */
1315 #               define SIGFRAME_SC_OFFSET       0x90
1316                 tcp->u_arg[0] = 0;
1317                 if (upeek(tcp, PT_R12, &sp) < 0)
1318                         return 0;
1319                 if (umove(tcp, sp + 16 + SIGFRAME_SC_OFFSET, &sc) < 0)
1320                         return 0;
1321                 tcp->u_arg[0] = 1;
1322                 memcpy(tcp->u_arg + 1, &sc.sc_mask, sizeof(sc.sc_mask));
1323         }
1324         else {
1325                 sigset_t sigm;
1326
1327                 memcpy(&sigm, tcp->u_arg + 1, sizeof(sigm));
1328                 tcp->u_rval = tcp->u_error = 0;
1329                 if (tcp->u_arg[0] == 0)
1330                         return 0;
1331                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1332                 return RVAL_NONE | RVAL_STR;
1333         }
1334         return 0;
1335 #elif defined(POWERPC)
1336         long esp;
1337         struct sigcontext_struct sc;
1338
1339         if (entering(tcp)) {
1340                 tcp->u_arg[0] = 0;
1341                 if (upeek(tcp, sizeof(unsigned long)*PT_R1, &esp) < 0)
1342                         return 0;
1343                 /* Skip dummy stack frame. */
1344 #ifdef POWERPC64
1345                 if (current_personality == 0)
1346                         esp += 128;
1347                 else
1348                         esp += 64;
1349 #else
1350                 esp += 64;
1351 #endif
1352                 if (umove(tcp, esp, &sc) < 0)
1353                         return 0;
1354                 tcp->u_arg[0] = 1;
1355                 tcp->u_arg[1] = sc.oldmask;
1356         }
1357         else {
1358                 sigset_t sigm;
1359                 long_to_sigset(tcp->u_arg[1], &sigm);
1360                 tcp->u_rval = tcp->u_error = 0;
1361                 if (tcp->u_arg[0] == 0)
1362                         return 0;
1363                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1364                 return RVAL_NONE | RVAL_STR;
1365         }
1366         return 0;
1367 #elif defined(M68K)
1368         long usp;
1369         struct sigcontext sc;
1370
1371         if (entering(tcp)) {
1372                 tcp->u_arg[0] = 0;
1373                 if (upeek(tcp, 4*PT_USP, &usp) < 0)
1374                         return 0;
1375                 if (umove(tcp, usp, &sc) < 0)
1376                         return 0;
1377                 tcp->u_arg[0] = 1;
1378                 tcp->u_arg[1] = sc.sc_mask;
1379         }
1380         else {
1381                 sigset_t sigm;
1382                 long_to_sigset(tcp->u_arg[1], &sigm);
1383                 tcp->u_rval = tcp->u_error = 0;
1384                 if (tcp->u_arg[0] == 0)
1385                         return 0;
1386                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1387                 return RVAL_NONE | RVAL_STR;
1388         }
1389         return 0;
1390 #elif defined(ALPHA)
1391         long fp;
1392         struct sigcontext_struct sc;
1393
1394         if (entering(tcp)) {
1395                 tcp->u_arg[0] = 0;
1396                 if (upeek(tcp, REG_FP, &fp) < 0)
1397                         return 0;
1398                 if (umove(tcp, fp, &sc) < 0)
1399                         return 0;
1400                 tcp->u_arg[0] = 1;
1401                 tcp->u_arg[1] = sc.sc_mask;
1402         }
1403         else {
1404                 sigset_t sigm;
1405                 long_to_sigset(tcp->u_arg[1], &sigm);
1406                 tcp->u_rval = tcp->u_error = 0;
1407                 if (tcp->u_arg[0] == 0)
1408                         return 0;
1409                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1410                 return RVAL_NONE | RVAL_STR;
1411         }
1412         return 0;
1413 #elif defined (SPARC) || defined (SPARC64)
1414         long i1;
1415         struct pt_regs regs;
1416         m_siginfo_t si;
1417
1418         if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0) {
1419                 perror("sigreturn: PTRACE_GETREGS ");
1420                 return 0;
1421         }
1422         if (entering(tcp)) {
1423                 tcp->u_arg[0] = 0;
1424                 i1 = regs.u_regs[U_REG_O1];
1425                 if (umove(tcp, i1, &si) < 0) {
1426                         perror("sigreturn: umove ");
1427                         return 0;
1428                 }
1429                 tcp->u_arg[0] = 1;
1430                 tcp->u_arg[1] = si.si_mask;
1431         } else {
1432                 sigset_t sigm;
1433                 long_to_sigset(tcp->u_arg[1], &sigm);
1434                 tcp->u_rval = tcp->u_error = 0;
1435                 if (tcp->u_arg[0] == 0)
1436                         return 0;
1437                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1438                 return RVAL_NONE | RVAL_STR;
1439         }
1440         return 0;
1441 #elif defined (LINUX_MIPSN32) || defined (LINUX_MIPSN64)
1442         /* This decodes rt_sigreturn.  The 64-bit ABIs do not have
1443            sigreturn.  */
1444         long sp;
1445         struct ucontext uc;
1446
1447         if (entering(tcp)) {
1448                 tcp->u_arg[0] = 0;
1449                 if (upeek(tcp, REG_SP, &sp) < 0)
1450                         return 0;
1451                 /* There are six words followed by a 128-byte siginfo.  */
1452                 sp = sp + 6 * 4 + 128;
1453                 if (umove(tcp, sp, &uc) < 0)
1454                         return 0;
1455                 tcp->u_arg[0] = 1;
1456                 tcp->u_arg[1] = *(long *) &uc.uc_sigmask;
1457         } else {
1458                 sigset_t sigm;
1459                 long_to_sigset(tcp->u_arg[1], &sigm);
1460                 tcp->u_rval = tcp->u_error = 0;
1461                 if (tcp->u_arg[0] == 0)
1462                         return 0;
1463                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1464                 return RVAL_NONE | RVAL_STR;
1465         }
1466         return 0;
1467 #elif defined(MIPS)
1468         long sp;
1469         struct pt_regs regs;
1470         m_siginfo_t si;
1471
1472         if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0) {
1473                 perror("sigreturn: PTRACE_GETREGS ");
1474                 return 0;
1475         }
1476         if (entering(tcp)) {
1477                 tcp->u_arg[0] = 0;
1478                 sp = regs.regs[29];
1479                 if (umove(tcp, sp, &si) < 0)
1480                         return 0;
1481                 tcp->u_arg[0] = 1;
1482                 tcp->u_arg[1] = si.si_mask;
1483         } else {
1484                 sigset_t sigm;
1485                 long_to_sigset(tcp->u_arg[1], &sigm);
1486                 tcp->u_rval = tcp->u_error = 0;
1487                 if (tcp->u_arg[0] == 0)
1488                         return 0;
1489                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1490                 return RVAL_NONE | RVAL_STR;
1491         }
1492         return 0;
1493 #elif defined(CRISV10) || defined(CRISV32)
1494         struct sigcontext sc;
1495
1496         if (entering(tcp)) {
1497                 long regs[PT_MAX+1];
1498
1499                 tcp->u_arg[0] = 0;
1500
1501                 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long)regs) < 0) {
1502                         perror("sigreturn: PTRACE_GETREGS");
1503                         return 0;
1504                 }
1505                 if (umove(tcp, regs[PT_USP], &sc) < 0)
1506                         return 0;
1507                 tcp->u_arg[0] = 1;
1508                 tcp->u_arg[1] = sc.oldmask;
1509         } else {
1510                 sigset_t sigm;
1511                 long_to_sigset(tcp->u_arg[1], &sigm);
1512                 tcp->u_rval = tcp->u_error = 0;
1513
1514                 if (tcp->u_arg[0] == 0)
1515                         return 0;
1516                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1517                 return RVAL_NONE | RVAL_STR;
1518         }
1519         return 0;
1520 #elif defined(TILE)
1521         struct ucontext uc;
1522         long sp;
1523
1524         /* offset of ucontext in the kernel's sigframe structure */
1525 #       define SIGFRAME_UC_OFFSET C_ABI_SAVE_AREA_SIZE + sizeof(struct siginfo)
1526
1527         if (entering(tcp)) {
1528                 tcp->u_arg[0] = 0;
1529                 if (upeek(tcp, PTREGS_OFFSET_SP, &sp) < 0)
1530                         return 0;
1531                 if (umove(tcp, sp + SIGFRAME_UC_OFFSET, &uc) < 0)
1532                         return 0;
1533                 tcp->u_arg[0] = 1;
1534                 memcpy(tcp->u_arg + 1, &uc.uc_sigmask, sizeof(uc.uc_sigmask));
1535         }
1536         else {
1537                 sigset_t sigm;
1538
1539                 memcpy(&sigm, tcp->u_arg + 1, sizeof(sigm));
1540                 tcp->u_rval = tcp->u_error = 0;
1541                 if (tcp->u_arg[0] == 0)
1542                         return 0;
1543                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1544                 return RVAL_NONE | RVAL_STR;
1545         }
1546         return 0;
1547 #elif defined(MICROBLAZE)
1548         struct sigcontext sc;
1549
1550         /* TODO: Verify that this is correct...  */
1551         if (entering(tcp)) {
1552                 long sp;
1553
1554                 tcp->u_arg[0] = 0;
1555
1556                 /* Read r1, the stack pointer.  */
1557                 if (upeek(tcp, 1 * 4, &sp) < 0)
1558                         return 0;
1559                 if (umove(tcp, sp, &sc) < 0)
1560                         return 0;
1561                 tcp->u_arg[0] = 1;
1562                 tcp->u_arg[1] = sc.oldmask;
1563         } else {
1564                 sigset_t sigm;
1565                 long_to_sigset(tcp->u_arg[1], &sigm);
1566                 tcp->u_rval = tcp->u_error = 0;
1567                 if (tcp->u_arg[0] == 0)
1568                         return 0;
1569                 tcp->auxstr = sprintsigmask("mask now ", &sigm, 0);
1570                 return RVAL_NONE | RVAL_STR;
1571         }
1572         return 0;
1573 #else
1574 #warning No sys_sigreturn() for this architecture
1575 #warning         (no problem, just a reminder :-)
1576         return 0;
1577 #endif
1578 }
1579
1580 int
1581 sys_siggetmask(struct tcb *tcp)
1582 {
1583         if (exiting(tcp)) {
1584                 sigset_t sigm;
1585                 long_to_sigset(tcp->u_rval, &sigm);
1586                 tcp->auxstr = sprintsigmask("mask ", &sigm, 0);
1587         }
1588         return RVAL_HEX | RVAL_STR;
1589 }
1590
1591 int
1592 sys_sigsuspend(struct tcb *tcp)
1593 {
1594         if (entering(tcp)) {
1595                 sigset_t sigm;
1596                 long_to_sigset(tcp->u_arg[2], &sigm);
1597                 printsigmask(&sigm, 0);
1598         }
1599         return 0;
1600 }
1601
1602 #endif /* LINUX */
1603
1604 #if defined(SVR4) || defined(FREEBSD)
1605
1606 int
1607 sys_sigsuspend(struct tcb *tcp)
1608 {
1609         sigset_t sigset;
1610
1611         if (entering(tcp)) {
1612                 if (umove(tcp, tcp->u_arg[0], &sigset) < 0)
1613                         tprintf("[?]");
1614                 else
1615                         printsigmask(&sigset, 0);
1616         }
1617         return 0;
1618 }
1619 #ifndef FREEBSD
1620 static const struct xlat ucontext_flags[] = {
1621         { UC_SIGMASK,   "UC_SIGMASK"    },
1622         { UC_STACK,     "UC_STACK"      },
1623         { UC_CPU,       "UC_CPU"        },
1624 #ifdef UC_FPU
1625         { UC_FPU,       "UC_FPU"        },
1626 #endif
1627 #ifdef UC_INTR
1628         { UC_INTR,      "UC_INTR"       },
1629 #endif
1630         { 0,            NULL            },
1631 };
1632 #endif /* !FREEBSD */
1633 #endif /* SVR4 || FREEBSD */
1634
1635 #if defined SVR4 || defined LINUX || defined FREEBSD
1636 #if defined LINUX && !defined SS_ONSTACK
1637 #define SS_ONSTACK      1
1638 #define SS_DISABLE      2
1639 #if __GLIBC_MINOR__ == 0
1640 typedef struct
1641 {
1642         __ptr_t ss_sp;
1643         int ss_flags;
1644         size_t ss_size;
1645 } stack_t;
1646 #endif
1647 #endif
1648 #ifdef FREEBSD
1649 #define stack_t struct sigaltstack
1650 #endif
1651
1652 static const struct xlat sigaltstack_flags[] = {
1653         { SS_ONSTACK,   "SS_ONSTACK"    },
1654         { SS_DISABLE,   "SS_DISABLE"    },
1655         { 0,            NULL            },
1656 };
1657 #endif
1658
1659 #ifdef SVR4
1660 static void
1661 printcontext(struct tcb *tcp, ucontext_t *ucp)
1662 {
1663         tprintf("{");
1664         if (!abbrev(tcp)) {
1665                 tprintf("uc_flags=");
1666                 printflags(ucontext_flags, ucp->uc_flags, "UC_???");
1667                 tprintf(", uc_link=%#lx, ", (unsigned long) ucp->uc_link);
1668         }
1669         tprintf("uc_sigmask=");
1670         printsigmask(&ucp->uc_sigmask, 0);
1671         if (!abbrev(tcp)) {
1672                 tprintf(", uc_stack={ss_sp=%#lx, ss_size=%d, ss_flags=",
1673                         (unsigned long) ucp->uc_stack.ss_sp,
1674                         ucp->uc_stack.ss_size);
1675                 printflags(sigaltstack_flags, ucp->uc_stack.ss_flags, "SS_???");
1676                 tprintf("}");
1677         }
1678         tprintf(", ...}");
1679 }
1680
1681 int
1682 sys_getcontext(struct tcb *tcp)
1683 {
1684         ucontext_t uc;
1685
1686         if (exiting(tcp)) {
1687                 if (tcp->u_error)
1688                         tprintf("%#lx", tcp->u_arg[0]);
1689                 else if (!tcp->u_arg[0])
1690                         tprintf("NULL");
1691                 else if (umove(tcp, tcp->u_arg[0], &uc) < 0)
1692                         tprintf("{...}");
1693                 else
1694                         printcontext(tcp, &uc);
1695         }
1696         return 0;
1697 }
1698
1699 int
1700 sys_setcontext(struct tcb *tcp)
1701 {
1702         ucontext_t uc;
1703
1704         if (entering(tcp)) {
1705                 if (!tcp->u_arg[0])
1706                         tprintf("NULL");
1707                 else if (umove(tcp, tcp->u_arg[0], &uc) < 0)
1708                         tprintf("{...}");
1709                 else
1710                         printcontext(tcp, &uc);
1711         }
1712         else {
1713                 tcp->u_rval = tcp->u_error = 0;
1714                 if (tcp->u_arg[0] == 0)
1715                         return 0;
1716                 return RVAL_NONE;
1717         }
1718         return 0;
1719 }
1720
1721 #endif /* SVR4 */
1722
1723 #if defined(LINUX) || defined(FREEBSD)
1724
1725 static int
1726 print_stack_t(struct tcb *tcp, unsigned long addr)
1727 {
1728         stack_t ss;
1729         if (umove(tcp, addr, &ss) < 0)
1730                 return -1;
1731         tprintf("{ss_sp=%#lx, ss_flags=", (unsigned long) ss.ss_sp);
1732         printflags(sigaltstack_flags, ss.ss_flags, "SS_???");
1733         tprintf(", ss_size=%lu}", (unsigned long) ss.ss_size);
1734         return 0;
1735 }
1736
1737 int
1738 sys_sigaltstack(struct tcb *tcp)
1739 {
1740         if (entering(tcp)) {
1741                 if (tcp->u_arg[0] == 0)
1742                         tprintf("NULL");
1743                 else if (print_stack_t(tcp, tcp->u_arg[0]) < 0)
1744                         return -1;
1745         }
1746         else {
1747                 tprintf(", ");
1748                 if (tcp->u_arg[1] == 0)
1749                         tprintf("NULL");
1750                 else if (print_stack_t(tcp, tcp->u_arg[1]) < 0)
1751                         return -1;
1752         }
1753         return 0;
1754 }
1755 #endif
1756
1757 #ifdef HAVE_SIGACTION
1758
1759 int
1760 sys_sigprocmask(struct tcb *tcp)
1761 {
1762 #ifdef ALPHA
1763         if (entering(tcp)) {
1764                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1765                 tprintf(", ");
1766                 printsigmask(tcp->u_arg[1], 0);
1767         }
1768         else if (!syserror(tcp)) {
1769                 tcp->auxstr = sprintsigmask("old mask ", tcp->u_rval, 0);
1770                 return RVAL_HEX | RVAL_STR;
1771         }
1772 #else /* !ALPHA */
1773         if (entering(tcp)) {
1774 #ifdef SVR4
1775                 if (tcp->u_arg[0] == 0)
1776                         tprintf("0");
1777                 else
1778 #endif /* SVR4 */
1779                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1780                 tprintf(", ");
1781                 print_sigset(tcp, tcp->u_arg[1], 0);
1782                 tprintf(", ");
1783         }
1784         else {
1785                 if (!tcp->u_arg[2])
1786                         tprintf("NULL");
1787                 else if (syserror(tcp))
1788                         tprintf("%#lx", tcp->u_arg[2]);
1789                 else
1790                         print_sigset(tcp, tcp->u_arg[2], 0);
1791         }
1792 #endif /* !ALPHA */
1793         return 0;
1794 }
1795
1796 #endif /* HAVE_SIGACTION */
1797
1798 int
1799 sys_kill(struct tcb *tcp)
1800 {
1801         if (entering(tcp)) {
1802                 /*
1803                  * Sign-extend a 32-bit value when that's what it is.
1804                  */
1805                 long pid = tcp->u_arg[0];
1806                 if (personality_wordsize[current_personality] < sizeof pid)
1807                         pid = (long) (int) pid;
1808                 tprintf("%ld, %s", pid, signame(tcp->u_arg[1]));
1809         }
1810         return 0;
1811 }
1812
1813 #if defined(FREEBSD) || defined(SUNOS4)
1814 int
1815 sys_killpg(struct tcb *tcp)
1816 {
1817         return sys_kill(tcp);
1818 }
1819 #endif /* FREEBSD || SUNOS4 */
1820
1821 #ifdef LINUX
1822 int
1823 sys_tgkill(struct tcb *tcp)
1824 {
1825         if (entering(tcp)) {
1826                 tprintf("%ld, %ld, %s",
1827                         tcp->u_arg[0], tcp->u_arg[1], signame(tcp->u_arg[2]));
1828         }
1829         return 0;
1830 }
1831 #endif
1832
1833 int
1834 sys_sigpending(struct tcb *tcp)
1835 {
1836         sigset_t sigset;
1837
1838         if (exiting(tcp)) {
1839                 if (syserror(tcp))
1840                         tprintf("%#lx", tcp->u_arg[0]);
1841                 else if (copy_sigset(tcp, tcp->u_arg[0], &sigset) < 0)
1842                         tprintf("[?]");
1843                 else
1844                         printsigmask(&sigset, 0);
1845         }
1846         return 0;
1847 }
1848
1849 #ifdef SVR4
1850 int sys_sigwait(struct tcb *tcp)
1851 {
1852         sigset_t sigset;
1853
1854         if (entering(tcp)) {
1855                 if (copy_sigset(tcp, tcp->u_arg[0], &sigset) < 0)
1856                         tprintf("[?]");
1857                 else
1858                         printsigmask(&sigset, 0);
1859         }
1860         else {
1861                 if (!syserror(tcp)) {
1862                         tcp->auxstr = signalent[tcp->u_rval];
1863                         return RVAL_DECIMAL | RVAL_STR;
1864                 }
1865         }
1866         return 0;
1867 }
1868 #endif /* SVR4 */
1869
1870 #ifdef LINUX
1871
1872 int
1873 sys_rt_sigprocmask(struct tcb *tcp)
1874 {
1875         sigset_t sigset;
1876
1877         /* Note: arg[3] is the length of the sigset. */
1878         if (entering(tcp)) {
1879                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1880                 tprintf(", ");
1881                 if (!tcp->u_arg[1])
1882                         tprintf("NULL, ");
1883                 else if (copy_sigset_len(tcp, tcp->u_arg[1], &sigset, tcp->u_arg[3]) < 0)
1884                         tprintf("%#lx, ", tcp->u_arg[1]);
1885                 else {
1886                         printsigmask(&sigset, 1);
1887                         tprintf(", ");
1888                 }
1889         }
1890         else {
1891                 if (!tcp->u_arg[2])
1892
1893                         tprintf("NULL");
1894                 else if (syserror(tcp))
1895                         tprintf("%#lx", tcp->u_arg[2]);
1896                 else if (copy_sigset_len(tcp, tcp->u_arg[2], &sigset, tcp->u_arg[3]) < 0)
1897                         tprintf("[?]");
1898                 else
1899                         printsigmask(&sigset, 1);
1900                 tprintf(", %lu", tcp->u_arg[3]);
1901         }
1902         return 0;
1903 }
1904
1905
1906 /* Structure describing the action to be taken when a signal arrives.  */
1907 struct new_sigaction
1908 {
1909         __sighandler_t __sa_handler;
1910         unsigned long sa_flags;
1911         void (*sa_restorer) (void);
1912         /* Kernel treats sa_mask as an array of longs. */
1913         unsigned long sa_mask[NSIG / sizeof(long) ? NSIG / sizeof(long) : 1];
1914 };
1915 /* Same for i386-on-x86_64 and similar cases */
1916 struct new_sigaction32
1917 {
1918         uint32_t __sa_handler;
1919         uint32_t sa_flags;
1920         uint32_t sa_restorer;
1921         uint32_t sa_mask[2 * (NSIG / sizeof(long) ? NSIG / sizeof(long) : 1)];
1922 };
1923
1924
1925 int
1926 sys_rt_sigaction(struct tcb *tcp)
1927 {
1928         struct new_sigaction sa;
1929         sigset_t sigset;
1930         long addr;
1931         int r;
1932
1933         if (entering(tcp)) {
1934                 printsignal(tcp->u_arg[0]);
1935                 tprintf(", ");
1936                 addr = tcp->u_arg[1];
1937         } else
1938                 addr = tcp->u_arg[2];
1939
1940         if (addr == 0) {
1941                 tprintf("NULL");
1942                 goto after_sa;
1943         }
1944         if (!verbose(tcp)) {
1945                 tprintf("%#lx", addr);
1946                 goto after_sa;
1947         }
1948 #if SUPPORTED_PERSONALITIES > 1
1949         if (personality_wordsize[current_personality] != sizeof(sa.sa_flags)
1950          && personality_wordsize[current_personality] == 4
1951         ) {
1952                 struct new_sigaction32 sa32;
1953                 r = umove(tcp, addr, &sa32);
1954                 if (r >= 0) {
1955                         memset(&sa, 0, sizeof(sa));
1956                         sa.__sa_handler = (void*)(unsigned long)sa32.__sa_handler;
1957                         sa.sa_flags     = sa32.sa_flags;
1958                         sa.sa_restorer  = (void*)(unsigned long)sa32.sa_restorer;
1959                         /* Kernel treats sa_mask as an array of longs.
1960                          * For 32-bit process, "long" is uint32_t, thus, for example,
1961                          * 32th bit in sa_mask will end up as bit 0 in sa_mask[1].
1962                          * But for (64-bit) kernel, 32th bit in sa_mask is
1963                          * 32th bit in 0th (64-bit) long!
1964                          * For little-endian, it's the same.
1965                          * For big-endian, we swap 32-bit words.
1966                          */
1967                         sa.sa_mask[0] = sa32.sa_mask[0] + ((long)(sa32.sa_mask[1]) << 32);
1968                 }
1969         } else
1970 #endif
1971         {
1972                 r = umove(tcp, addr, &sa);
1973         }
1974         if (r < 0) {
1975                 tprintf("{...}");
1976                 goto after_sa;
1977         }
1978         /* Architectures using function pointers, like
1979          * hppa, may need to manipulate the function pointer
1980          * to compute the result of a comparison. However,
1981          * the SA_HANDLER function pointer exists only in
1982          * the address space of the traced process, and can't
1983          * be manipulated by strace. In order to prevent the
1984          * compiler from generating code to manipulate
1985          * SA_HANDLER we cast the function pointers to long. */
1986         if ((long)sa.__sa_handler == (long)SIG_ERR)
1987                 tprintf("{SIG_ERR, ");
1988         else if ((long)sa.__sa_handler == (long)SIG_DFL)
1989                 tprintf("{SIG_DFL, ");
1990         else if ((long)sa.__sa_handler == (long)SIG_IGN)
1991                 tprintf("{SIG_IGN, ");
1992         else
1993                 tprintf("{%#lx, ", (long) sa.__sa_handler);
1994         /* Questionable code below.
1995          * Kernel won't handle sys_rt_sigaction
1996          * with wrong sigset size (just returns EINVAL)
1997          * therefore tcp->u_arg[3(4)] _must_ be NSIG / 8 here,
1998          * and we always use smaller memcpy. */
1999         sigemptyset(&sigset);
2000 #ifdef LINUXSPARC
2001         if (tcp->u_arg[4] <= sizeof(sigset))
2002                 memcpy(&sigset, &sa.sa_mask, tcp->u_arg[4]);
2003 #else
2004         if (tcp->u_arg[3] <= sizeof(sigset))
2005                 memcpy(&sigset, &sa.sa_mask, tcp->u_arg[3]);
2006 #endif
2007         else
2008                 memcpy(&sigset, &sa.sa_mask, sizeof(sigset));
2009         printsigmask(&sigset, 1);
2010         tprintf(", ");
2011         printflags(sigact_flags, sa.sa_flags, "SA_???");
2012 #ifdef SA_RESTORER
2013         if (sa.sa_flags & SA_RESTORER)
2014                 tprintf(", %p", sa.sa_restorer);
2015 #endif
2016         tprintf("}");
2017
2018  after_sa:
2019         if (entering(tcp))
2020                 tprintf(", ");
2021         else
2022 #ifdef LINUXSPARC
2023                 tprintf(", %#lx, %lu", tcp->u_arg[3], tcp->u_arg[4]);
2024 #elif defined(ALPHA)
2025                 tprintf(", %lu, %#lx", tcp->u_arg[3], tcp->u_arg[4]);
2026 #else
2027                 tprintf(", %lu", tcp->u_arg[3]);
2028 #endif
2029         return 0;
2030 }
2031
2032 int
2033 sys_rt_sigpending(struct tcb *tcp)
2034 {
2035         sigset_t sigset;
2036
2037         if (exiting(tcp)) {
2038                 if (syserror(tcp))
2039                         tprintf("%#lx", tcp->u_arg[0]);
2040                 else if (copy_sigset_len(tcp, tcp->u_arg[0],
2041                                          &sigset, tcp->u_arg[1]) < 0)
2042                         tprintf("[?]");
2043                 else
2044                         printsigmask(&sigset, 1);
2045         }
2046         return 0;
2047 }
2048
2049 int
2050 sys_rt_sigsuspend(struct tcb *tcp)
2051 {
2052         if (entering(tcp)) {
2053                 sigset_t sigm;
2054                 if (copy_sigset_len(tcp, tcp->u_arg[0], &sigm, tcp->u_arg[1]) < 0)
2055                         tprintf("[?]");
2056                 else
2057                         printsigmask(&sigm, 1);
2058         }
2059         return 0;
2060 }
2061
2062 int
2063 sys_rt_sigqueueinfo(struct tcb *tcp)
2064 {
2065         if (entering(tcp)) {
2066                 siginfo_t si;
2067                 tprintf("%lu, ", tcp->u_arg[0]);
2068                 printsignal(tcp->u_arg[1]);
2069                 tprintf(", ");
2070                 if (umove(tcp, tcp->u_arg[2], &si) < 0)
2071                         tprintf("%#lx", tcp->u_arg[2]);
2072                 else
2073                         printsiginfo(&si, verbose(tcp));
2074         }
2075         return 0;
2076 }
2077
2078 int sys_rt_sigtimedwait(struct tcb *tcp)
2079 {
2080         if (entering(tcp)) {
2081                 sigset_t sigset;
2082
2083                 if (copy_sigset_len(tcp, tcp->u_arg[0],
2084                                     &sigset, tcp->u_arg[3]) < 0)
2085                         tprintf("[?]");
2086                 else
2087                         printsigmask(&sigset, 1);
2088                 tprintf(", ");
2089                 /* This is the only "return" parameter, */
2090                 if (tcp->u_arg[1] != 0)
2091                         return 0;
2092                 /* ... if it's NULL, can decode all on entry */
2093                 tprintf("NULL, ");
2094         }
2095         else if (tcp->u_arg[1] != 0) {
2096                 /* syscall exit, and u_arg[1] wasn't NULL */
2097                 if (syserror(tcp))
2098                         tprintf("%#lx, ", tcp->u_arg[1]);
2099                 else {
2100                         siginfo_t si;
2101                         if (umove(tcp, tcp->u_arg[1], &si) < 0)
2102                                 tprintf("%#lx, ", tcp->u_arg[1]);
2103                         else {
2104                                 printsiginfo(&si, verbose(tcp));
2105                                 tprintf(", ");
2106                         }
2107                 }
2108         }
2109         else {
2110                 /* syscall exit, and u_arg[1] was NULL */
2111                 return 0;
2112         }
2113         print_timespec(tcp, tcp->u_arg[2]);
2114         tprintf(", %d", (int) tcp->u_arg[3]);
2115         return 0;
2116 };
2117
2118 int
2119 sys_restart_syscall(struct tcb *tcp)
2120 {
2121         if (entering(tcp))
2122                 tprintf("<... resuming interrupted call ...>");
2123         return 0;
2124 }
2125
2126 static int
2127 do_signalfd(struct tcb *tcp, int flags_arg)
2128 {
2129         if (entering(tcp)) {
2130                 printfd(tcp, tcp->u_arg[0]);
2131                 tprintf(", ");
2132                 print_sigset(tcp, tcp->u_arg[1], 1);
2133                 tprintf(", %lu", tcp->u_arg[2]);
2134                 if (flags_arg >= 0) {
2135                         tprintf(", ");
2136                         printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
2137                 }
2138         }
2139         return 0;
2140 }
2141
2142 int
2143 sys_signalfd(struct tcb *tcp)
2144 {
2145         return do_signalfd(tcp, -1);
2146 }
2147
2148 int
2149 sys_signalfd4(struct tcb *tcp)
2150 {
2151         return do_signalfd(tcp, 3);
2152 }
2153 #endif /* LINUX */