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