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