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