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