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