]> granicus.if.org Git - strace/blob - signal.c
Show f_flags field in printstatfs
[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
34 #include "defs.h"
35
36 #ifndef NSIG
37 # warning NSIG is not defined, using 32
38 # define NSIG 32
39 #elif NSIG < 32
40 # error NSIG < 32
41 #endif
42
43 /* The libc headers do not define this constant since it should only be
44    used by the implementation.  So we define it here.  */
45 #ifndef SA_RESTORER
46 # ifdef ASM_SA_RESTORER
47 #  define SA_RESTORER ASM_SA_RESTORER
48 # endif
49 #endif
50
51 /*
52  * Some architectures define SA_RESTORER in their headers,
53  * but do not actually have sa_restorer.
54  *
55  * Some architectures, otherwise, do not define SA_RESTORER in their headers,
56  * but actually have sa_restorer.
57  */
58 #ifdef SA_RESTORER
59 # if defined HPPA || defined IA64
60 #  define HAVE_SA_RESTORER 0
61 # else
62 #  define HAVE_SA_RESTORER 1
63 # endif
64 #else /* !SA_RESTORER */
65 # if defined SPARC || defined SPARC64 || defined M68K
66 #  define HAVE_SA_RESTORER 1
67 # else
68 #  define HAVE_SA_RESTORER 0
69 # endif
70 #endif
71
72 #include "xlat/sigact_flags.h"
73 #include "xlat/sigprocmaskcmds.h"
74
75 /* Anonymous realtime signals. */
76 #ifndef ASM_SIGRTMIN
77 /* Linux kernel >= 3.18 defines SIGRTMIN to 32 on all architectures. */
78 # define ASM_SIGRTMIN 32
79 #endif
80 #ifndef ASM_SIGRTMAX
81 /* Under glibc 2.1, SIGRTMAX et al are functions, but __SIGRTMAX is a
82    constant.  This is what we want.  Otherwise, just use SIGRTMAX. */
83 # ifdef SIGRTMAX
84 #  ifndef __SIGRTMAX
85 #   define __SIGRTMAX SIGRTMAX
86 #  endif
87 # endif
88 # ifdef __SIGRTMAX
89 #  define ASM_SIGRTMAX __SIGRTMAX
90 # endif
91 #endif
92
93 /* Note on the size of sigset_t:
94  *
95  * In glibc, sigset_t is an array with space for 1024 bits (!),
96  * even though all arches supported by Linux have only 64 signals
97  * except MIPS, which has 128. IOW, it is 128 bytes long.
98  *
99  * In-kernel sigset_t is sized correctly (it is either 64 or 128 bit long).
100  * However, some old syscall return only 32 lower bits (one word).
101  * Example: sys_sigpending vs sys_rt_sigpending.
102  *
103  * Be aware of this fact when you try to
104  *     memcpy(&tcp->u_arg[1], &something, sizeof(sigset_t))
105  * - sizeof(sigset_t) is much bigger than you think,
106  * it may overflow tcp->u_arg[] array, and it may try to copy more data
107  * than is really available in <something>.
108  * Similarly,
109  *     umoven(tcp, addr, sizeof(sigset_t), &sigset)
110  * may be a bad idea: it'll try to read much more data than needed
111  * to fetch a sigset_t.
112  * Use (NSIG / 8) as a size instead.
113  */
114
115 const char *
116 signame(const int sig)
117 {
118         static char buf[sizeof("SIGRT_%u") + sizeof(int)*3];
119
120         if (sig >= 0) {
121                 const unsigned int s = sig;
122
123                 if (s < nsignals)
124                         return signalent[s];
125 #ifdef ASM_SIGRTMAX
126                 if (s >= ASM_SIGRTMIN && s <= ASM_SIGRTMAX) {
127                         sprintf(buf, "SIGRT_%u", s - ASM_SIGRTMIN);
128                         return buf;
129                 }
130 #endif
131         }
132         sprintf(buf, "%d", sig);
133         return buf;
134 }
135
136 static unsigned int
137 popcount32(const uint32_t *a, unsigned int size)
138 {
139         unsigned int count = 0;
140
141         for (; size; ++a, --size) {
142                 uint32_t x = *a;
143
144 #ifdef HAVE___BUILTIN_POPCOUNT
145                 count += __builtin_popcount(x);
146 #else
147                 for (; x; ++count)
148                         x &= x - 1;
149 #endif
150         }
151
152         return count;
153 }
154
155 const char *
156 sprintsigmask_n(const char *prefix, const void *sig_mask, unsigned int bytes)
157 {
158         /*
159          * The maximum number of signal names to be printed is NSIG * 2 / 3.
160          * Most of signal names have length 7,
161          * average length of signal names is less than 7.
162          * The length of prefix string does not exceed 16.
163          */
164         static char outstr[128 + 8 * (NSIG * 2 / 3)];
165
166         char *s;
167         const uint32_t *mask;
168         uint32_t inverted_mask[NSIG / 32];
169         unsigned int size;
170         int i;
171         char sep;
172
173         s = stpcpy(outstr, prefix);
174
175         mask = sig_mask;
176         /* length of signal mask in 4-byte words */
177         size = (bytes >= NSIG / 8) ? NSIG / 32 : (bytes + 3) / 4;
178
179         /* check whether 2/3 or more bits are set */
180         if (popcount32(mask, size) >= size * 32 * 2 / 3) {
181                 /* show those signals that are NOT in the mask */
182                 unsigned int j;
183                 for (j = 0; j < size; ++j)
184                         inverted_mask[j] = ~mask[j];
185                 mask = inverted_mask;
186                 *s++ = '~';
187         }
188
189         sep = '[';
190         for (i = 0; (i = next_set_bit(mask, i, size * 32)) >= 0; ) {
191                 ++i;
192                 *s++ = sep;
193                 if ((unsigned) i < nsignals) {
194                         s = stpcpy(s, signalent[i] + 3);
195                 }
196 #ifdef ASM_SIGRTMAX
197                 else if (i >= ASM_SIGRTMIN && i <= ASM_SIGRTMAX) {
198                         s += sprintf(s, "RT_%u", i - ASM_SIGRTMIN);
199                 }
200 #endif
201                 else {
202                         s += sprintf(s, "%u", i);
203                 }
204                 sep = ' ';
205         }
206         if (sep == '[')
207                 *s++ = sep;
208         *s++ = ']';
209         *s = '\0';
210         return outstr;
211 }
212
213 #define sprintsigmask_val(prefix, mask) \
214         sprintsigmask_n((prefix), &(mask), sizeof(mask))
215
216 #define tprintsigmask_val(prefix, mask) \
217         tprints(sprintsigmask_n((prefix), &(mask), sizeof(mask)))
218
219 void
220 printsignal(int nr)
221 {
222         tprints(signame(nr));
223 }
224
225 void
226 print_sigset_addr_len(struct tcb *tcp, long addr, long len)
227 {
228         char mask[NSIG / 8];
229
230         if (!addr) {
231                 tprints("NULL");
232                 return;
233         }
234         /* Here len is usually equals NSIG / 8 or current_wordsize.
235          * But we code this defensively:
236          */
237         if (len < 0) {
238  bad:
239                 tprintf("%#lx", addr);
240                 return;
241         }
242         if (len >= NSIG / 8)
243                 len = NSIG / 8;
244         else
245                 len = (len + 3) & ~3;
246
247         if (umoven(tcp, addr, len, mask) < 0)
248                 goto bad;
249         tprints(sprintsigmask_n("", mask, len));
250 }
251
252 #ifndef ILL_ILLOPC
253 #define ILL_ILLOPC      1       /* illegal opcode */
254 #define ILL_ILLOPN      2       /* illegal operand */
255 #define ILL_ILLADR      3       /* illegal addressing mode */
256 #define ILL_ILLTRP      4       /* illegal trap */
257 #define ILL_PRVOPC      5       /* privileged opcode */
258 #define ILL_PRVREG      6       /* privileged register */
259 #define ILL_COPROC      7       /* coprocessor error */
260 #define ILL_BADSTK      8       /* internal stack error */
261 #define FPE_INTDIV      1       /* integer divide by zero */
262 #define FPE_INTOVF      2       /* integer overflow */
263 #define FPE_FLTDIV      3       /* floating point divide by zero */
264 #define FPE_FLTOVF      4       /* floating point overflow */
265 #define FPE_FLTUND      5       /* floating point underflow */
266 #define FPE_FLTRES      6       /* floating point inexact result */
267 #define FPE_FLTINV      7       /* floating point invalid operation */
268 #define FPE_FLTSUB      8       /* subscript out of range */
269 #define SEGV_MAPERR     1       /* address not mapped to object */
270 #define SEGV_ACCERR     2       /* invalid permissions for mapped object */
271 #define BUS_ADRALN      1       /* invalid address alignment */
272 #define BUS_ADRERR      2       /* non-existant physical address */
273 #define BUS_OBJERR      3       /* object specific hardware error */
274 #define SYS_SECCOMP     1       /* seccomp triggered */
275 #define TRAP_BRKPT      1       /* process breakpoint */
276 #define TRAP_TRACE      2       /* process trace trap */
277 #define CLD_EXITED      1       /* child has exited */
278 #define CLD_KILLED      2       /* child was killed */
279 #define CLD_DUMPED      3       /* child terminated abnormally */
280 #define CLD_TRAPPED     4       /* traced child has trapped */
281 #define CLD_STOPPED     5       /* child has stopped */
282 #define CLD_CONTINUED   6       /* stopped child has continued */
283 #define POLL_IN         1       /* data input available */
284 #define POLL_OUT        2       /* output buffers available */
285 #define POLL_MSG        3       /* input message available */
286 #define POLL_ERR        4       /* i/o error */
287 #define POLL_PRI        5       /* high priority input available */
288 #define POLL_HUP        6       /* device disconnected */
289 #define SI_KERNEL       0x80    /* sent by kernel */
290 #define SI_USER         0       /* sent by kill, sigsend, raise */
291 #define SI_QUEUE        -1      /* sent by sigqueue */
292 #define SI_TIMER        -2      /* sent by timer expiration */
293 #define SI_MESGQ        -3      /* sent by real time mesq state change */
294 #define SI_ASYNCIO      -4      /* sent by AIO completion */
295 #define SI_SIGIO        -5      /* sent by SIGIO */
296 #define SI_TKILL        -6      /* sent by tkill */
297 #define SI_DETHREAD     -7      /* sent by execve killing subsidiary threads */
298 #define SI_ASYNCNL      -60     /* sent by asynch name lookup completion */
299 #endif
300
301 #ifndef SI_FROMUSER
302 # define SI_FROMUSER(sip)       ((sip)->si_code <= 0)
303 #endif
304
305 #include "xlat/siginfo_codes.h"
306 #include "xlat/sigill_codes.h"
307 #include "xlat/sigfpe_codes.h"
308 #include "xlat/sigtrap_codes.h"
309 #include "xlat/sigchld_codes.h"
310 #include "xlat/sigpoll_codes.h"
311 #include "xlat/sigprof_codes.h"
312
313 #ifdef SIGEMT
314 #include "xlat/sigemt_codes.h"
315 #endif
316
317 #include "xlat/sigsegv_codes.h"
318 #include "xlat/sigbus_codes.h"
319
320 #ifndef SYS_SECCOMP
321 # define SYS_SECCOMP 1
322 #endif
323 #include "xlat/sigsys_codes.h"
324
325 static void
326 printsigsource(const siginfo_t *sip)
327 {
328         tprintf(", si_pid=%lu, si_uid=%lu",
329                 (unsigned long) sip->si_pid,
330                 (unsigned long) sip->si_uid);
331 }
332
333 static void
334 printsigval(const siginfo_t *sip, int verbose)
335 {
336         if (!verbose)
337                 tprints(", ...");
338         else
339                 tprintf(", si_value={int=%u, ptr=%#lx}",
340                         sip->si_int,
341                         (unsigned long) sip->si_ptr);
342 }
343
344 void
345 printsiginfo(const siginfo_t *sip, int verbose)
346 {
347         const char *code;
348
349         if (sip->si_signo == 0) {
350                 tprints("{}");
351                 return;
352         }
353         tprints("{si_signo=");
354         printsignal(sip->si_signo);
355         code = xlookup(siginfo_codes, sip->si_code);
356         if (!code) {
357                 switch (sip->si_signo) {
358                 case SIGTRAP:
359                         code = xlookup(sigtrap_codes, sip->si_code);
360                         break;
361                 case SIGCHLD:
362                         code = xlookup(sigchld_codes, sip->si_code);
363                         break;
364                 case SIGPOLL:
365                         code = xlookup(sigpoll_codes, sip->si_code);
366                         break;
367                 case SIGPROF:
368                         code = xlookup(sigprof_codes, sip->si_code);
369                         break;
370                 case SIGILL:
371                         code = xlookup(sigill_codes, sip->si_code);
372                         break;
373 #ifdef SIGEMT
374                 case SIGEMT:
375                         code = xlookup(sigemt_codes, sip->si_code);
376                         break;
377 #endif
378                 case SIGFPE:
379                         code = xlookup(sigfpe_codes, sip->si_code);
380                         break;
381                 case SIGSEGV:
382                         code = xlookup(sigsegv_codes, sip->si_code);
383                         break;
384                 case SIGBUS:
385                         code = xlookup(sigbus_codes, sip->si_code);
386                         break;
387                 case SIGSYS:
388                         code = xlookup(sigsys_codes, sip->si_code);
389                         break;
390                 }
391         }
392         if (code)
393                 tprintf(", si_code=%s", code);
394         else
395                 tprintf(", si_code=%#x", sip->si_code);
396 #ifdef SI_NOINFO
397         if (sip->si_code != SI_NOINFO)
398 #endif
399         {
400                 if (sip->si_errno) {
401                         tprints(", si_errno=");
402                         if ((unsigned) sip->si_errno < nerrnos
403                             && errnoent[sip->si_errno])
404                                 tprints(errnoent[sip->si_errno]);
405                         else
406                                 tprintf("%d", sip->si_errno);
407                 }
408 #ifdef SI_FROMUSER
409                 if (SI_FROMUSER(sip)) {
410                         switch (sip->si_code) {
411 #ifdef SI_USER
412                         case SI_USER:
413                                 printsigsource(sip);
414                                 break;
415 #endif
416 #ifdef SI_TKILL
417                         case SI_TKILL:
418                                 printsigsource(sip);
419                                 break;
420 #endif
421 #if defined SI_TIMER \
422  && defined HAVE_SIGINFO_T_SI_TIMERID && defined HAVE_SIGINFO_T_SI_OVERRUN
423                         case SI_TIMER:
424                                 tprintf(", si_timerid=%#x, si_overrun=%d",
425                                         sip->si_timerid, sip->si_overrun);
426                                 printsigval(sip, verbose);
427                                 break;
428 #endif
429                         default:
430                                 printsigsource(sip);
431                                 if (sip->si_ptr)
432                                         printsigval(sip, verbose);
433                                 break;
434                         }
435                 }
436                 else
437 #endif /* SI_FROMUSER */
438                 {
439                         switch (sip->si_signo) {
440                         case SIGCHLD:
441                                 printsigsource(sip);
442                                 tprints(", si_status=");
443                                 if (sip->si_code == CLD_EXITED)
444                                         tprintf("%d", sip->si_status);
445                                 else
446                                         printsignal(sip->si_status);
447                                 if (!verbose)
448                                         tprints(", ...");
449                                 else
450                                         tprintf(", si_utime=%llu, si_stime=%llu",
451                                                 (unsigned long long) sip->si_utime,
452                                                 (unsigned long long) sip->si_stime);
453                                 break;
454                         case SIGILL: case SIGFPE:
455                         case SIGSEGV: case SIGBUS:
456                                 tprintf(", si_addr=%#lx",
457                                         (unsigned long) sip->si_addr);
458                                 break;
459                         case SIGPOLL:
460                                 switch (sip->si_code) {
461                                 case POLL_IN: case POLL_OUT: case POLL_MSG:
462                                         tprintf(", si_band=%ld",
463                                                 (long) sip->si_band);
464                                         break;
465                                 }
466                                 break;
467 #ifdef HAVE_SIGINFO_T_SI_SYSCALL
468                         case SIGSYS:
469                                 tprintf(", si_call_addr=%#lx, si_syscall=%d, si_arch=%u",
470                                         (unsigned long) sip->si_call_addr,
471                                         sip->si_syscall, sip->si_arch);
472                                 break;
473 #endif
474                         default:
475                                 if (sip->si_pid || sip->si_uid)
476                                         printsigsource(sip);
477                                 if (sip->si_ptr)
478                                         printsigval(sip, verbose);
479                         }
480                 }
481         }
482         tprints("}");
483 }
484
485 void
486 printsiginfo_at(struct tcb *tcp, long addr)
487 {
488         siginfo_t si;
489         if (!addr) {
490                 tprints("NULL");
491                 return;
492         }
493         if (syserror(tcp)) {
494                 tprintf("%#lx", addr);
495                 return;
496         }
497         if (umove(tcp, addr, &si) < 0) {
498                 tprints("{???}");
499                 return;
500         }
501         printsiginfo(&si, verbose(tcp));
502 }
503
504 int
505 sys_sigsetmask(struct tcb *tcp)
506 {
507         if (entering(tcp)) {
508                 tprintsigmask_val("", tcp->u_arg[0]);
509         }
510         else if (!syserror(tcp)) {
511                 tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
512                 return RVAL_HEX | RVAL_STR;
513         }
514         return 0;
515 }
516
517 #ifdef HAVE_SIGACTION
518
519 struct old_sigaction {
520         /* sa_handler may be a libc #define, need to use other name: */
521 #ifdef MIPS
522         unsigned int sa_flags;
523         void (*__sa_handler)(int);
524         /* Kernel treats sa_mask as an array of longs. */
525         unsigned long sa_mask[NSIG / sizeof(long) ? NSIG / sizeof(long) : 1];
526 #else
527         void (*__sa_handler)(int);
528         unsigned long sa_mask;
529         unsigned long sa_flags;
530 #endif /* !MIPS */
531 #if HAVE_SA_RESTORER
532         void (*sa_restorer)(void);
533 #endif
534 };
535
536 struct old_sigaction32 {
537         /* sa_handler may be a libc #define, need to use other name: */
538         uint32_t __sa_handler;
539         uint32_t sa_mask;
540         uint32_t sa_flags;
541 #if HAVE_SA_RESTORER
542         uint32_t sa_restorer;
543 #endif
544 };
545
546 static void
547 decode_old_sigaction(struct tcb *tcp, long addr)
548 {
549         struct old_sigaction sa;
550         int r;
551
552         if (!addr) {
553                 tprints("NULL");
554                 return;
555         }
556         if (!verbose(tcp) || (exiting(tcp) && syserror(tcp))) {
557                 tprintf("%#lx", addr);
558                 return;
559         }
560
561 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
562         if (current_wordsize != sizeof(sa.__sa_handler) && current_wordsize == 4) {
563                 struct old_sigaction32 sa32;
564                 r = umove(tcp, addr, &sa32);
565                 if (r >= 0) {
566                         memset(&sa, 0, sizeof(sa));
567                         sa.__sa_handler = (void*)(uintptr_t)sa32.__sa_handler;
568                         sa.sa_flags = sa32.sa_flags;
569 #if HAVE_SA_RESTORER && defined SA_RESTORER
570                         sa.sa_restorer = (void*)(uintptr_t)sa32.sa_restorer;
571 #endif
572                         sa.sa_mask = sa32.sa_mask;
573                 }
574         } else
575 #endif
576         {
577                 r = umove(tcp, addr, &sa);
578         }
579         if (r < 0) {
580                 tprints("{...}");
581                 return;
582         }
583
584         /* Architectures using function pointers, like
585          * hppa, may need to manipulate the function pointer
586          * to compute the result of a comparison. However,
587          * the __sa_handler function pointer exists only in
588          * the address space of the traced process, and can't
589          * be manipulated by strace. In order to prevent the
590          * compiler from generating code to manipulate
591          * __sa_handler we cast the function pointers to long. */
592         if ((long)sa.__sa_handler == (long)SIG_ERR)
593                 tprints("{SIG_ERR, ");
594         else if ((long)sa.__sa_handler == (long)SIG_DFL)
595                 tprints("{SIG_DFL, ");
596         else if ((long)sa.__sa_handler == (long)SIG_IGN)
597                 tprints("{SIG_IGN, ");
598         else
599                 tprintf("{%#lx, ", (long) sa.__sa_handler);
600 #ifdef MIPS
601         tprintsigmask_addr("", sa.sa_mask);
602 #else
603         tprintsigmask_val("", sa.sa_mask);
604 #endif
605         tprints(", ");
606         printflags(sigact_flags, sa.sa_flags, "SA_???");
607 #if HAVE_SA_RESTORER && defined SA_RESTORER
608         if (sa.sa_flags & SA_RESTORER)
609                 tprintf(", %p", sa.sa_restorer);
610 #endif
611         tprints("}");
612 }
613
614 int
615 sys_sigaction(struct tcb *tcp)
616 {
617         if (entering(tcp)) {
618                 printsignal(tcp->u_arg[0]);
619                 tprints(", ");
620                 decode_old_sigaction(tcp, tcp->u_arg[1]);
621                 tprints(", ");
622         } else
623                 decode_old_sigaction(tcp, tcp->u_arg[2]);
624         return 0;
625 }
626
627 int
628 sys_signal(struct tcb *tcp)
629 {
630         if (entering(tcp)) {
631                 printsignal(tcp->u_arg[0]);
632                 tprints(", ");
633                 switch (tcp->u_arg[1]) {
634                 case (long) SIG_ERR:
635                         tprints("SIG_ERR");
636                         break;
637                 case (long) SIG_DFL:
638                         tprints("SIG_DFL");
639                         break;
640                 case (long) SIG_IGN:
641                         tprints("SIG_IGN");
642                         break;
643                 default:
644                         tprintf("%#lx", tcp->u_arg[1]);
645                 }
646                 return 0;
647         }
648         else if (!syserror(tcp)) {
649                 switch (tcp->u_rval) {
650                 case (long) SIG_ERR:
651                         tcp->auxstr = "SIG_ERR"; break;
652                 case (long) SIG_DFL:
653                         tcp->auxstr = "SIG_DFL"; break;
654                 case (long) SIG_IGN:
655                         tcp->auxstr = "SIG_IGN"; break;
656                 default:
657                         tcp->auxstr = NULL;
658                 }
659                 return RVAL_HEX | RVAL_STR;
660         }
661         return 0;
662 }
663
664 #endif /* HAVE_SIGACTION */
665
666 int
667 sys_siggetmask(struct tcb *tcp)
668 {
669         if (exiting(tcp)) {
670                 tcp->auxstr = sprintsigmask_val("mask ", tcp->u_rval);
671         }
672         return RVAL_HEX | RVAL_STR;
673 }
674
675 int
676 sys_sigsuspend(struct tcb *tcp)
677 {
678         if (entering(tcp)) {
679                 tprintsigmask_val("", tcp->u_arg[2]);
680         }
681         return 0;
682 }
683
684 #if !defined SS_ONSTACK
685 #define SS_ONSTACK      1
686 #define SS_DISABLE      2
687 #endif
688
689 #include "xlat/sigaltstack_flags.h"
690
691 static void
692 print_stack_t(struct tcb *tcp, unsigned long addr)
693 {
694         stack_t ss;
695         int r;
696
697         if (!addr) {
698                 tprints("NULL");
699                 return;
700         }
701
702 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
703         if (current_wordsize != sizeof(ss.ss_sp) && current_wordsize == 4) {
704                 struct {
705                         uint32_t ss_sp;
706                         int32_t ss_flags;
707                         uint32_t ss_size;
708                 } ss32;
709                 r = umove(tcp, addr, &ss32);
710                 if (r >= 0) {
711                         memset(&ss, 0, sizeof(ss));
712                         ss.ss_sp = (void*)(unsigned long) ss32.ss_sp;
713                         ss.ss_flags = ss32.ss_flags;
714                         ss.ss_size = (unsigned long) ss32.ss_size;
715                 }
716         } else
717 #endif
718         {
719                 r = umove(tcp, addr, &ss);
720         }
721         if (r < 0) {
722                 tprintf("%#lx", addr);
723         } else {
724                 tprintf("{ss_sp=%#lx, ss_flags=", (unsigned long) ss.ss_sp);
725                 printflags(sigaltstack_flags, ss.ss_flags, "SS_???");
726                 tprintf(", ss_size=%lu}", (unsigned long) ss.ss_size);
727         }
728 }
729
730 int
731 sys_sigaltstack(struct tcb *tcp)
732 {
733         if (entering(tcp)) {
734                 print_stack_t(tcp, tcp->u_arg[0]);
735         }
736         else {
737                 tprints(", ");
738                 print_stack_t(tcp, tcp->u_arg[1]);
739         }
740         return 0;
741 }
742
743 #ifdef HAVE_SIGACTION
744
745 /* "Old" sigprocmask, which operates with word-sized signal masks */
746 int
747 sys_sigprocmask(struct tcb *tcp)
748 {
749 # ifdef ALPHA
750         if (entering(tcp)) {
751                 /*
752                  * Alpha/OSF is different: it doesn't pass in two pointers,
753                  * but rather passes in the new bitmask as an argument and
754                  * then returns the old bitmask.  This "works" because we
755                  * only have 64 signals to worry about.  If you want more,
756                  * use of the rt_sigprocmask syscall is required.
757                  * Alpha:
758                  *      old = osf_sigprocmask(how, new);
759                  * Everyone else:
760                  *      ret = sigprocmask(how, &new, &old, ...);
761                  */
762                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
763                 tprintsigmask_val(", ", tcp->u_arg[1]);
764         }
765         else if (!syserror(tcp)) {
766                 tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
767                 return RVAL_HEX | RVAL_STR;
768         }
769 # else /* !ALPHA */
770         if (entering(tcp)) {
771                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
772                 tprints(", ");
773                 print_sigset_addr_len(tcp, tcp->u_arg[1], current_wordsize);
774                 tprints(", ");
775         }
776         else {
777                 if (syserror(tcp))
778                         tprintf("%#lx", tcp->u_arg[2]);
779                 else
780                         print_sigset_addr_len(tcp, tcp->u_arg[2], current_wordsize);
781         }
782 # endif /* !ALPHA */
783         return 0;
784 }
785
786 #endif /* HAVE_SIGACTION */
787
788 int
789 sys_kill(struct tcb *tcp)
790 {
791         if (entering(tcp)) {
792                 tprintf("%ld, %s",
793                         widen_to_long(tcp->u_arg[0]),
794                         signame(tcp->u_arg[1])
795                 );
796         }
797         return 0;
798 }
799
800 int
801 sys_tgkill(struct tcb *tcp)
802 {
803         if (entering(tcp)) {
804                 tprintf("%ld, %ld, %s",
805                         widen_to_long(tcp->u_arg[0]),
806                         widen_to_long(tcp->u_arg[1]),
807                         signame(tcp->u_arg[2])
808                 );
809         }
810         return 0;
811 }
812
813 int
814 sys_sigpending(struct tcb *tcp)
815 {
816         if (exiting(tcp)) {
817                 if (syserror(tcp))
818                         tprintf("%#lx", tcp->u_arg[0]);
819                 else
820                         print_sigset_addr_len(tcp, tcp->u_arg[0], current_wordsize);
821         }
822         return 0;
823 }
824
825 int
826 sys_rt_sigprocmask(struct tcb *tcp)
827 {
828         /* Note: arg[3] is the length of the sigset. Kernel requires NSIG / 8 */
829         if (entering(tcp)) {
830                 printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
831                 tprints(", ");
832                 print_sigset_addr_len(tcp, tcp->u_arg[1], tcp->u_arg[3]);
833                 tprints(", ");
834         }
835         else {
836                 if (syserror(tcp))
837                         tprintf("%#lx", tcp->u_arg[2]);
838                 else
839                         print_sigset_addr_len(tcp, tcp->u_arg[2], tcp->u_arg[3]);
840                 tprintf(", %lu", tcp->u_arg[3]);
841         }
842         return 0;
843 }
844
845 /* Structure describing the action to be taken when a signal arrives.  */
846 struct new_sigaction
847 {
848         /* sa_handler may be a libc #define, need to use other name: */
849 #ifdef MIPS
850         unsigned int sa_flags;
851         void (*__sa_handler)(int);
852 #else
853         void (*__sa_handler)(int);
854         unsigned long sa_flags;
855 #endif /* !MIPS */
856 #if HAVE_SA_RESTORER
857         void (*sa_restorer)(void);
858 #endif
859         /* Kernel treats sa_mask as an array of longs. */
860         unsigned long sa_mask[NSIG / sizeof(long) ? NSIG / sizeof(long) : 1];
861 };
862 /* Same for i386-on-x86_64 and similar cases */
863 struct new_sigaction32
864 {
865         uint32_t __sa_handler;
866         uint32_t sa_flags;
867 #if HAVE_SA_RESTORER
868         uint32_t sa_restorer;
869 #endif
870         uint32_t sa_mask[2 * (NSIG / sizeof(long) ? NSIG / sizeof(long) : 1)];
871 };
872
873 static void
874 decode_new_sigaction(struct tcb *tcp, long addr)
875 {
876         struct new_sigaction sa;
877         int r;
878
879         if (!addr) {
880                 tprints("NULL");
881                 return;
882         }
883         if (!verbose(tcp) || (exiting(tcp) && syserror(tcp))) {
884                 tprintf("%#lx", addr);
885                 return;
886         }
887 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
888         if (current_wordsize != sizeof(sa.sa_flags) && current_wordsize == 4) {
889                 struct new_sigaction32 sa32;
890                 r = umove(tcp, addr, &sa32);
891                 if (r >= 0) {
892                         memset(&sa, 0, sizeof(sa));
893                         sa.__sa_handler = (void*)(unsigned long)sa32.__sa_handler;
894                         sa.sa_flags     = sa32.sa_flags;
895 #if HAVE_SA_RESTORER && defined SA_RESTORER
896                         sa.sa_restorer  = (void*)(unsigned long)sa32.sa_restorer;
897 #endif
898                         /* Kernel treats sa_mask as an array of longs.
899                          * For 32-bit process, "long" is uint32_t, thus, for example,
900                          * 32th bit in sa_mask will end up as bit 0 in sa_mask[1].
901                          * But for (64-bit) kernel, 32th bit in sa_mask is
902                          * 32th bit in 0th (64-bit) long!
903                          * For little-endian, it's the same.
904                          * For big-endian, we swap 32-bit words.
905                          */
906                         sa.sa_mask[0] = sa32.sa_mask[0] + ((long)(sa32.sa_mask[1]) << 32);
907                 }
908         } else
909 #endif
910         {
911                 r = umove(tcp, addr, &sa);
912         }
913         if (r < 0) {
914                 tprints("{...}");
915                 return;
916         }
917         /* Architectures using function pointers, like
918          * hppa, may need to manipulate the function pointer
919          * to compute the result of a comparison. However,
920          * the __sa_handler function pointer exists only in
921          * the address space of the traced process, and can't
922          * be manipulated by strace. In order to prevent the
923          * compiler from generating code to manipulate
924          * __sa_handler we cast the function pointers to long. */
925         if ((long)sa.__sa_handler == (long)SIG_ERR)
926                 tprints("{SIG_ERR, ");
927         else if ((long)sa.__sa_handler == (long)SIG_DFL)
928                 tprints("{SIG_DFL, ");
929         else if ((long)sa.__sa_handler == (long)SIG_IGN)
930                 tprints("{SIG_IGN, ");
931         else
932                 tprintf("{%#lx, ", (long) sa.__sa_handler);
933         /*
934          * Sigset size is in tcp->u_arg[4] (SPARC)
935          * or in tcp->u_arg[3] (all other),
936          * but kernel won't handle sys_rt_sigaction
937          * with wrong sigset size (just returns EINVAL instead).
938          * We just fetch the right size, which is NSIG / 8.
939          */
940         tprintsigmask_val("", sa.sa_mask);
941         tprints(", ");
942
943         printflags(sigact_flags, sa.sa_flags, "SA_???");
944 #if HAVE_SA_RESTORER && defined SA_RESTORER
945         if (sa.sa_flags & SA_RESTORER)
946                 tprintf(", %p", sa.sa_restorer);
947 #endif
948         tprints("}");
949 }
950
951 int
952 sys_rt_sigaction(struct tcb *tcp)
953 {
954         if (entering(tcp)) {
955                 printsignal(tcp->u_arg[0]);
956                 tprints(", ");
957                 decode_new_sigaction(tcp, tcp->u_arg[1]);
958                 tprints(", ");
959         } else {
960                 decode_new_sigaction(tcp, tcp->u_arg[2]);
961 #if defined(SPARC) || defined(SPARC64)
962                 tprintf(", %#lx, %lu", tcp->u_arg[3], tcp->u_arg[4]);
963 #elif defined(ALPHA)
964                 tprintf(", %lu, %#lx", tcp->u_arg[3], tcp->u_arg[4]);
965 #else
966                 tprintf(", %lu", tcp->u_arg[3]);
967 #endif
968         }
969         return 0;
970 }
971
972 int
973 sys_rt_sigpending(struct tcb *tcp)
974 {
975         if (exiting(tcp)) {
976                 /*
977                  * One of the few syscalls where sigset size (arg[1])
978                  * is allowed to be <= NSIG / 8, not strictly ==.
979                  * This allows non-rt sigpending() syscall
980                  * to reuse rt_sigpending() code in kernel.
981                  */
982                 if (syserror(tcp))
983                         tprintf("%#lx", tcp->u_arg[0]);
984                 else
985                         print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[1]);
986                 tprintf(", %lu", tcp->u_arg[1]);
987         }
988         return 0;
989 }
990
991 int
992 sys_rt_sigsuspend(struct tcb *tcp)
993 {
994         if (entering(tcp)) {
995                 /* NB: kernel requires arg[1] == NSIG / 8 */
996                 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[1]);
997                 tprintf(", %lu", tcp->u_arg[1]);
998         }
999         return 0;
1000 }
1001
1002 static void
1003 print_sigqueueinfo(struct tcb *tcp, int sig, unsigned long uinfo)
1004 {
1005         printsignal(sig);
1006         tprints(", ");
1007         printsiginfo_at(tcp, uinfo);
1008 }
1009
1010 int
1011 sys_rt_sigqueueinfo(struct tcb *tcp)
1012 {
1013         if (entering(tcp)) {
1014                 tprintf("%lu, ", tcp->u_arg[0]);
1015                 print_sigqueueinfo(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1016         }
1017         return 0;
1018 }
1019
1020 int
1021 sys_rt_tgsigqueueinfo(struct tcb *tcp)
1022 {
1023         if (entering(tcp)) {
1024                 tprintf("%lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
1025                 print_sigqueueinfo(tcp, tcp->u_arg[2], tcp->u_arg[3]);
1026         }
1027         return 0;
1028 }
1029
1030 int sys_rt_sigtimedwait(struct tcb *tcp)
1031 {
1032         /* NB: kernel requires arg[3] == NSIG / 8 */
1033         if (entering(tcp)) {
1034                 print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[3]);
1035                 tprints(", ");
1036                 /* This is the only "return" parameter, */
1037                 if (tcp->u_arg[1] != 0)
1038                         return 0;
1039                 /* ... if it's NULL, can decode all on entry */
1040                 tprints("NULL, ");
1041         }
1042         else if (tcp->u_arg[1] != 0) {
1043                 /* syscall exit, and u_arg[1] wasn't NULL */
1044                 printsiginfo_at(tcp, tcp->u_arg[1]);
1045                 tprints(", ");
1046         }
1047         else {
1048                 /* syscall exit, and u_arg[1] was NULL */
1049                 return 0;
1050         }
1051         print_timespec(tcp, tcp->u_arg[2]);
1052         tprintf(", %lu", tcp->u_arg[3]);
1053         return 0;
1054 };
1055
1056 int
1057 sys_restart_syscall(struct tcb *tcp)
1058 {
1059         if (entering(tcp))
1060                 tprints("<... resuming interrupted call ...>");
1061         return 0;
1062 }
1063
1064 static int
1065 do_signalfd(struct tcb *tcp, int flags_arg)
1066 {
1067         /* NB: kernel requires arg[2] == NSIG / 8 */
1068         if (entering(tcp)) {
1069                 printfd(tcp, tcp->u_arg[0]);
1070                 tprints(", ");
1071                 print_sigset_addr_len(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1072                 tprintf(", %lu", tcp->u_arg[2]);
1073                 if (flags_arg >= 0) {
1074                         tprints(", ");
1075                         printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
1076                 }
1077         }
1078         return 0;
1079 }
1080
1081 int
1082 sys_signalfd(struct tcb *tcp)
1083 {
1084         return do_signalfd(tcp, -1);
1085 }
1086
1087 int
1088 sys_signalfd4(struct tcb *tcp)
1089 {
1090         return do_signalfd(tcp, 3);
1091 }