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