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#include <sys/user.h>
36#include <fcntl.h>
37
38#ifdef HAVE_SYS_REG_H
39# include <sys/reg.h>
40#elif defined(HAVE_LINUX_PTRACE_H)
41# undef PTRACE_SYSCALL
42# ifdef HAVE_STRUCT_IA64_FPREG
43#  define ia64_fpreg XXX_ia64_fpreg
44# endif
45# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
46#  define pt_all_user_regs XXX_pt_all_user_regs
47# endif
48# ifdef HAVE_STRUCT_PTRACE_PEEKSIGINFO_ARGS
49#  define ptrace_peeksiginfo_args XXX_ptrace_peeksiginfo_args
50# endif
51# include <linux/ptrace.h>
52# undef ptrace_peeksiginfo_args
53# undef ia64_fpreg
54# undef pt_all_user_regs
55#endif
56
57#ifdef IA64
58# include <asm/ptrace_offsets.h>
59#endif
60
61#if defined(SPARC) || defined(SPARC64) || defined(MIPS)
62typedef struct {
63	struct pt_regs		si_regs;
64	int			si_mask;
65} m_siginfo_t;
66#elif defined HAVE_ASM_SIGCONTEXT_H
67# if !defined(IA64) && !defined(X86_64) && !defined(X32)
68#  include <asm/sigcontext.h>
69# endif
70#else /* !HAVE_ASM_SIGCONTEXT_H */
71# if defined M68K && !defined HAVE_STRUCT_SIGCONTEXT
72struct sigcontext {
73	unsigned long sc_mask;
74	unsigned long sc_usp;
75	unsigned long sc_d0;
76	unsigned long sc_d1;
77	unsigned long sc_a0;
78	unsigned long sc_a1;
79	unsigned short sc_sr;
80	unsigned long sc_pc;
81	unsigned short sc_formatvec;
82};
83# endif /* M68K */
84#endif /* !HAVE_ASM_SIGCONTEXT_H */
85
86#ifndef NSIG
87# warning: NSIG is not defined, using 32
88# define NSIG 32
89#elif NSIG < 32
90# error: NSIG < 32
91#endif
92
93#ifdef HAVE_SIGACTION
94
95/* The libc headers do not define this constant since it should only be
96   used by the implementation.  So we define it here.  */
97#ifndef SA_RESTORER
98# ifdef ASM_SA_RESTORER
99#  define SA_RESTORER ASM_SA_RESTORER
100# endif
101#endif
102
103#include "xlat/sigact_flags.h"
104#include "xlat/sigprocmaskcmds.h"
105
106#endif /* HAVE_SIGACTION */
107
108/* Anonymous realtime signals. */
109/* Under glibc 2.1, SIGRTMIN et al are functions, but __SIGRTMIN is a
110   constant.  This is what we want.  Otherwise, just use SIGRTMIN. */
111#ifdef SIGRTMIN
112#ifndef __SIGRTMIN
113#define __SIGRTMIN SIGRTMIN
114#define __SIGRTMAX SIGRTMAX /* likewise */
115#endif
116#endif
117
118/* Note on the size of sigset_t:
119 *
120 * In glibc, sigset_t is an array with space for 1024 bits (!),
121 * even though all arches supported by Linux have only 64 signals
122 * except MIPS, which has 128. IOW, it is 128 bytes long.
123 *
124 * In-kernel sigset_t is sized correctly (it is either 64 or 128 bit long).
125 * However, some old syscall return only 32 lower bits (one word).
126 * Example: sys_sigpending vs sys_rt_sigpending.
127 *
128 * Be aware of this fact when you try to
129 *     memcpy(&tcp->u_arg[1], &something, sizeof(sigset_t))
130 * - sizeof(sigset_t) is much bigger than you think,
131 * it may overflow tcp->u_arg[] array, and it may try to copy more data
132 * than is really available in <something>.
133 * Similarly,
134 *     umoven(tcp, addr, sizeof(sigset_t), &sigset)
135 * may be a bad idea: it'll try to read much more data than needed
136 * to fetch a sigset_t.
137 * Use (NSIG / 8) as a size instead.
138 */
139
140const char *
141signame(int sig)
142{
143	static char buf[sizeof("SIGRT_%d") + sizeof(int)*3];
144
145	if (sig >= 0 && sig < nsignals)
146		return signalent[sig];
147#ifdef SIGRTMIN
148	if (sig >= __SIGRTMIN && sig <= __SIGRTMAX) {
149		sprintf(buf, "SIGRT_%d", (int)(sig - __SIGRTMIN));
150		return buf;
151	}
152#endif
153	sprintf(buf, "%d", sig);
154	return buf;
155}
156
157static unsigned int
158popcount32(const uint32_t *a, unsigned int size)
159{
160	unsigned int count = 0;
161
162	for (; size; ++a, --size) {
163		uint32_t x = *a;
164
165#ifdef HAVE___BUILTIN_POPCOUNT
166		count += __builtin_popcount(x);
167#else
168		for (; x; ++count)
169			x &= x - 1;
170#endif
171	}
172
173	return count;
174}
175
176static const char *
177sprintsigmask_n(const char *prefix, const void *sig_mask, unsigned int bytes)
178{
179	/*
180	 * The maximum number of signal names to be printed is NSIG * 2 / 3.
181	 * Most of signal names have length 7,
182	 * average length of signal names is less than 7.
183	 * The length of prefix string does not exceed 16.
184	 */
185	static char outstr[128 + 8 * (NSIG * 2 / 3)];
186
187	char *s;
188	const uint32_t *mask;
189	uint32_t inverted_mask[NSIG / 32];
190	unsigned int size;
191	int i;
192	char sep;
193
194	s = stpcpy(outstr, prefix);
195
196	mask = sig_mask;
197	/* length of signal mask in 4-byte words */
198	size = (bytes >= NSIG / 8) ? NSIG / 32 : (bytes + 3) / 4;
199
200	/* check whether 2/3 or more bits are set */
201	if (popcount32(mask, size) >= size * 32 * 2 / 3) {
202		/* show those signals that are NOT in the mask */
203		unsigned int j;
204		for (j = 0; j < size; ++j)
205			inverted_mask[j] = ~mask[j];
206		mask = inverted_mask;
207		*s++ = '~';
208	}
209
210	sep = '[';
211	for (i = 0; (i = next_set_bit(mask, i, size * 32)) >= 0; ) {
212		++i;
213		*s++ = sep;
214		if (i < nsignals) {
215			s = stpcpy(s, signalent[i] + 3);
216		}
217#ifdef SIGRTMIN
218		else if (i >= __SIGRTMIN && i <= __SIGRTMAX) {
219			s += sprintf(s, "RT_%u", i - __SIGRTMIN);
220		}
221#endif
222		else {
223			s += sprintf(s, "%u", i);
224		}
225		sep = ' ';
226	}
227	if (sep == '[')
228		*s++ = sep;
229	*s++ = ']';
230	*s = '\0';
231	return outstr;
232}
233
234#define tprintsigmask_addr(prefix, mask) \
235	tprints(sprintsigmask_n((prefix), (mask), sizeof(mask)))
236
237#define sprintsigmask_val(prefix, mask) \
238	sprintsigmask_n((prefix), &(mask), sizeof(mask))
239
240#define tprintsigmask_val(prefix, mask) \
241	tprints(sprintsigmask_n((prefix), &(mask), sizeof(mask)))
242
243void
244printsignal(int nr)
245{
246	tprints(signame(nr));
247}
248
249void
250print_sigset_addr_len(struct tcb *tcp, long addr, long len)
251{
252	char mask[NSIG / 8];
253
254	if (!addr) {
255		tprints("NULL");
256		return;
257	}
258	/* Here len is usually equals NSIG / 8 or current_wordsize.
259	 * But we code this defensively:
260	 */
261	if (len < 0) {
262 bad:
263		tprintf("%#lx", addr);
264		return;
265	}
266	if (len >= NSIG / 8)
267		len = NSIG / 8;
268	else
269		len = (len + 3) & ~3;
270
271	if (umoven(tcp, addr, len, mask) < 0)
272		goto bad;
273	tprints(sprintsigmask_n("", mask, len));
274}
275
276#ifndef ILL_ILLOPC
277#define ILL_ILLOPC      1       /* illegal opcode */
278#define ILL_ILLOPN      2       /* illegal operand */
279#define ILL_ILLADR      3       /* illegal addressing mode */
280#define ILL_ILLTRP      4       /* illegal trap */
281#define ILL_PRVOPC      5       /* privileged opcode */
282#define ILL_PRVREG      6       /* privileged register */
283#define ILL_COPROC      7       /* coprocessor error */
284#define ILL_BADSTK      8       /* internal stack error */
285#define FPE_INTDIV      1       /* integer divide by zero */
286#define FPE_INTOVF      2       /* integer overflow */
287#define FPE_FLTDIV      3       /* floating point divide by zero */
288#define FPE_FLTOVF      4       /* floating point overflow */
289#define FPE_FLTUND      5       /* floating point underflow */
290#define FPE_FLTRES      6       /* floating point inexact result */
291#define FPE_FLTINV      7       /* floating point invalid operation */
292#define FPE_FLTSUB      8       /* subscript out of range */
293#define SEGV_MAPERR     1       /* address not mapped to object */
294#define SEGV_ACCERR     2       /* invalid permissions for mapped object */
295#define BUS_ADRALN      1       /* invalid address alignment */
296#define BUS_ADRERR      2       /* non-existant physical address */
297#define BUS_OBJERR      3       /* object specific hardware error */
298#define SYS_SECCOMP     1       /* seccomp triggered */
299#define TRAP_BRKPT      1       /* process breakpoint */
300#define TRAP_TRACE      2       /* process trace trap */
301#define CLD_EXITED      1       /* child has exited */
302#define CLD_KILLED      2       /* child was killed */
303#define CLD_DUMPED      3       /* child terminated abnormally */
304#define CLD_TRAPPED     4       /* traced child has trapped */
305#define CLD_STOPPED     5       /* child has stopped */
306#define CLD_CONTINUED   6       /* stopped child has continued */
307#define POLL_IN         1       /* data input available */
308#define POLL_OUT        2       /* output buffers available */
309#define POLL_MSG        3       /* input message available */
310#define POLL_ERR        4       /* i/o error */
311#define POLL_PRI        5       /* high priority input available */
312#define POLL_HUP        6       /* device disconnected */
313#define SI_KERNEL	0x80	/* sent by kernel */
314#define SI_USER         0       /* sent by kill, sigsend, raise */
315#define SI_QUEUE        -1      /* sent by sigqueue */
316#define SI_TIMER        -2      /* sent by timer expiration */
317#define SI_MESGQ        -3      /* sent by real time mesq state change */
318#define SI_ASYNCIO      -4      /* sent by AIO completion */
319#define SI_SIGIO	-5	/* sent by SIGIO */
320#define SI_TKILL	-6	/* sent by tkill */
321#define SI_DETHREAD	-7	/* sent by execve killing subsidiary threads */
322#define SI_ASYNCNL	-60     /* sent by asynch name lookup completion */
323#endif
324
325#ifndef SI_FROMUSER
326# define SI_FROMUSER(sip)	((sip)->si_code <= 0)
327#endif
328
329#include "xlat/siginfo_codes.h"
330#include "xlat/sigill_codes.h"
331#include "xlat/sigfpe_codes.h"
332#include "xlat/sigtrap_codes.h"
333#include "xlat/sigchld_codes.h"
334#include "xlat/sigpoll_codes.h"
335#include "xlat/sigprof_codes.h"
336
337#ifdef SIGEMT
338#include "xlat/sigemt_codes.h"
339#endif
340
341#include "xlat/sigsegv_codes.h"
342#include "xlat/sigbus_codes.h"
343
344#ifndef SYS_SECCOMP
345# define SYS_SECCOMP 1
346#endif
347#include "xlat/sigsys_codes.h"
348
349static void
350printsigsource(const siginfo_t *sip)
351{
352	tprintf(", si_pid=%lu, si_uid=%lu",
353		(unsigned long) sip->si_pid,
354		(unsigned long) sip->si_uid);
355}
356
357static void
358printsigval(const siginfo_t *sip, int verbose)
359{
360	if (!verbose)
361		tprints(", ...");
362	else
363		tprintf(", si_value={int=%u, ptr=%#lx}",
364			sip->si_int,
365			(unsigned long) sip->si_ptr);
366}
367
368void
369printsiginfo(siginfo_t *sip, int verbose)
370{
371	const char *code;
372
373	if (sip->si_signo == 0) {
374		tprints("{}");
375		return;
376	}
377	tprints("{si_signo=");
378	printsignal(sip->si_signo);
379	code = xlookup(siginfo_codes, sip->si_code);
380	if (!code) {
381		switch (sip->si_signo) {
382		case SIGTRAP:
383			code = xlookup(sigtrap_codes, sip->si_code);
384			break;
385		case SIGCHLD:
386			code = xlookup(sigchld_codes, sip->si_code);
387			break;
388		case SIGPOLL:
389			code = xlookup(sigpoll_codes, sip->si_code);
390			break;
391		case SIGPROF:
392			code = xlookup(sigprof_codes, sip->si_code);
393			break;
394		case SIGILL:
395			code = xlookup(sigill_codes, sip->si_code);
396			break;
397#ifdef SIGEMT
398		case SIGEMT:
399			code = xlookup(sigemt_codes, sip->si_code);
400			break;
401#endif
402		case SIGFPE:
403			code = xlookup(sigfpe_codes, sip->si_code);
404			break;
405		case SIGSEGV:
406			code = xlookup(sigsegv_codes, sip->si_code);
407			break;
408		case SIGBUS:
409			code = xlookup(sigbus_codes, sip->si_code);
410			break;
411		case SIGSYS:
412			code = xlookup(sigsys_codes, sip->si_code);
413			break;
414		}
415	}
416	if (code)
417		tprintf(", si_code=%s", code);
418	else
419		tprintf(", si_code=%#x", sip->si_code);
420#ifdef SI_NOINFO
421	if (sip->si_code != SI_NOINFO)
422#endif
423	{
424		if (sip->si_errno) {
425			if (sip->si_errno < 0 || sip->si_errno >= nerrnos)
426				tprintf(", si_errno=%d", sip->si_errno);
427			else
428				tprintf(", si_errno=%s",
429					errnoent[sip->si_errno]);
430		}
431#ifdef SI_FROMUSER
432		if (SI_FROMUSER(sip)) {
433			switch (sip->si_code) {
434#ifdef SI_USER
435			case SI_USER:
436				printsigsource(sip);
437				break;
438#endif
439#ifdef SI_TKILL
440			case SI_TKILL:
441				printsigsource(sip);
442				break;
443#endif
444#ifdef SI_TIMER
445			case SI_TIMER:
446				tprintf(", si_timerid=%#x, si_overrun=%d",
447					sip->si_timerid, sip->si_overrun);
448				printsigval(sip, verbose);
449				break;
450#endif
451			default:
452				printsigsource(sip);
453				if (sip->si_ptr)
454					printsigval(sip, verbose);
455				break;
456			}
457		}
458		else
459#endif /* SI_FROMUSER */
460		{
461			switch (sip->si_signo) {
462			case SIGCHLD:
463				printsigsource(sip);
464				tprints(", si_status=");
465				if (sip->si_code == CLD_EXITED)
466					tprintf("%d", sip->si_status);
467				else
468					printsignal(sip->si_status);
469				if (!verbose)
470					tprints(", ...");
471				else
472					tprintf(", si_utime=%llu, si_stime=%llu",
473						(unsigned long long) sip->si_utime,
474						(unsigned long long) sip->si_stime);
475				break;
476			case SIGILL: case SIGFPE:
477			case SIGSEGV: case SIGBUS:
478				tprintf(", si_addr=%#lx",
479					(unsigned long) sip->si_addr);
480				break;
481			case SIGPOLL:
482				switch (sip->si_code) {
483				case POLL_IN: case POLL_OUT: case POLL_MSG:
484					tprintf(", si_band=%ld",
485						(long) sip->si_band);
486					break;
487				}
488				break;
489#ifdef HAVE_SIGINFO_T_SI_SYSCALL
490			case SIGSYS:
491				tprintf(", si_call_addr=%#lx, si_syscall=%d, si_arch=%u",
492					(unsigned long) sip->si_call_addr,
493					sip->si_syscall, sip->si_arch);
494				break;
495#endif
496			default:
497				if (sip->si_pid || sip->si_uid)
498				        printsigsource(sip);
499				if (sip->si_ptr)
500					printsigval(sip, verbose);
501			}
502		}
503	}
504	tprints("}");
505}
506
507void
508printsiginfo_at(struct tcb *tcp, long addr)
509{
510	siginfo_t si;
511	if (!addr) {
512		tprints("NULL");
513		return;
514	}
515	if (syserror(tcp)) {
516		tprintf("%#lx", addr);
517		return;
518	}
519	if (umove(tcp, addr, &si) < 0) {
520		tprints("{???}");
521		return;
522	}
523	printsiginfo(&si, verbose(tcp));
524}
525
526int
527sys_sigsetmask(struct tcb *tcp)
528{
529	if (entering(tcp)) {
530		tprintsigmask_val("", tcp->u_arg[0]);
531	}
532	else if (!syserror(tcp)) {
533		tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
534		return RVAL_HEX | RVAL_STR;
535	}
536	return 0;
537}
538
539#ifdef HAVE_SIGACTION
540
541struct old_sigaction {
542	/* sa_handler may be a libc #define, need to use other name: */
543#ifdef MIPS
544	unsigned int sa_flags;
545	void (*__sa_handler)(int);
546	/* Kernel treats sa_mask as an array of longs. */
547	unsigned long sa_mask[NSIG / sizeof(long) ? NSIG / sizeof(long) : 1];
548#else
549	void (*__sa_handler)(int);
550	unsigned long sa_mask;
551	unsigned long sa_flags;
552	void (*sa_restorer)(void);
553#endif /* !MIPS */
554};
555
556struct old_sigaction32 {
557	/* sa_handler may be a libc #define, need to use other name: */
558	uint32_t __sa_handler;
559	uint32_t sa_mask;
560	uint32_t sa_flags;
561	uint32_t sa_restorer;
562};
563
564static void
565decode_old_sigaction(struct tcb *tcp, long addr)
566{
567	struct old_sigaction sa;
568	int r;
569
570	if (!addr) {
571		tprints("NULL");
572		return;
573	}
574	if (!verbose(tcp) || (exiting(tcp) && syserror(tcp))) {
575		tprintf("%#lx", addr);
576		return;
577	}
578
579#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
580	if (current_wordsize != sizeof(sa.__sa_handler) && current_wordsize == 4) {
581		struct old_sigaction32 sa32;
582		r = umove(tcp, addr, &sa32);
583		if (r >= 0) {
584			memset(&sa, 0, sizeof(sa));
585			sa.__sa_handler = (void*)(uintptr_t)sa32.__sa_handler;
586			sa.sa_flags = sa32.sa_flags;
587			sa.sa_restorer = (void*)(uintptr_t)sa32.sa_restorer;
588			sa.sa_mask = sa32.sa_mask;
589		}
590	} else
591#endif
592	{
593		r = umove(tcp, addr, &sa);
594	}
595	if (r < 0) {
596		tprints("{...}");
597		return;
598	}
599
600	/* Architectures using function pointers, like
601	 * hppa, may need to manipulate the function pointer
602	 * to compute the result of a comparison. However,
603	 * the __sa_handler function pointer exists only in
604	 * the address space of the traced process, and can't
605	 * be manipulated by strace. In order to prevent the
606	 * compiler from generating code to manipulate
607	 * __sa_handler we cast the function pointers to long. */
608	if ((long)sa.__sa_handler == (long)SIG_ERR)
609		tprints("{SIG_ERR, ");
610	else if ((long)sa.__sa_handler == (long)SIG_DFL)
611		tprints("{SIG_DFL, ");
612	else if ((long)sa.__sa_handler == (long)SIG_IGN)
613		tprints("{SIG_IGN, ");
614	else
615		tprintf("{%#lx, ", (long) sa.__sa_handler);
616#ifdef MIPS
617	tprintsigmask_addr("", sa.sa_mask);
618#else
619	tprintsigmask_val("", sa.sa_mask);
620#endif
621	tprints(", ");
622	printflags(sigact_flags, sa.sa_flags, "SA_???");
623#ifdef SA_RESTORER
624	if (sa.sa_flags & SA_RESTORER)
625		tprintf(", %p", sa.sa_restorer);
626#endif
627	tprints("}");
628}
629
630int
631sys_sigaction(struct tcb *tcp)
632{
633	if (entering(tcp)) {
634		printsignal(tcp->u_arg[0]);
635		tprints(", ");
636		decode_old_sigaction(tcp, tcp->u_arg[1]);
637		tprints(", ");
638	} else
639		decode_old_sigaction(tcp, tcp->u_arg[2]);
640	return 0;
641}
642
643int
644sys_signal(struct tcb *tcp)
645{
646	if (entering(tcp)) {
647		printsignal(tcp->u_arg[0]);
648		tprints(", ");
649		switch (tcp->u_arg[1]) {
650		case (long) SIG_ERR:
651			tprints("SIG_ERR");
652			break;
653		case (long) SIG_DFL:
654			tprints("SIG_DFL");
655			break;
656		case (long) SIG_IGN:
657			tprints("SIG_IGN");
658			break;
659		default:
660			tprintf("%#lx", tcp->u_arg[1]);
661		}
662		return 0;
663	}
664	else if (!syserror(tcp)) {
665		switch (tcp->u_rval) {
666		case (long) SIG_ERR:
667			tcp->auxstr = "SIG_ERR"; break;
668		case (long) SIG_DFL:
669			tcp->auxstr = "SIG_DFL"; break;
670		case (long) SIG_IGN:
671			tcp->auxstr = "SIG_IGN"; break;
672		default:
673			tcp->auxstr = NULL;
674		}
675		return RVAL_HEX | RVAL_STR;
676	}
677	return 0;
678}
679
680#endif /* HAVE_SIGACTION */
681
682int
683sys_sigreturn(struct tcb *tcp)
684{
685#if defined(ARM)
686	if (entering(tcp)) {
687		struct arm_sigcontext {
688			unsigned long trap_no;
689			unsigned long error_code;
690			unsigned long oldmask;
691			unsigned long arm_r0;
692			unsigned long arm_r1;
693			unsigned long arm_r2;
694			unsigned long arm_r3;
695			unsigned long arm_r4;
696			unsigned long arm_r5;
697			unsigned long arm_r6;
698			unsigned long arm_r7;
699			unsigned long arm_r8;
700			unsigned long arm_r9;
701			unsigned long arm_r10;
702			unsigned long arm_fp;
703			unsigned long arm_ip;
704			unsigned long arm_sp;
705			unsigned long arm_lr;
706			unsigned long arm_pc;
707			unsigned long arm_cpsr;
708			unsigned long fault_address;
709		};
710		struct arm_ucontext {
711			unsigned long uc_flags;
712			unsigned long uc_link;  /* struct ucontext* */
713			/* The next three members comprise stack_t struct: */
714			unsigned long ss_sp;    /* void*   */
715			unsigned long ss_flags; /* int     */
716			unsigned long ss_size;  /* size_t  */
717			struct arm_sigcontext sc;
718			/* These two members are sigset_t: */
719			unsigned long uc_sigmask[2];
720			/* more fields follow, which we aren't interested in */
721		};
722		struct arm_ucontext uc;
723		if (umove(tcp, arm_regs.ARM_sp, &uc) < 0)
724			return 0;
725		/*
726		 * Kernel fills out uc.sc.oldmask too when it sets up signal stack,
727		 * but for sigmask restore, sigreturn syscall uses uc.uc_sigmask instead.
728		 */
729		tprintsigmask_addr(") (mask ", uc.uc_sigmask);
730	}
731#elif defined(S390) || defined(S390X)
732	if (entering(tcp)) {
733		long usp;
734		struct sigcontext sc;
735		if (upeek(tcp->pid, PT_GPR15, &usp) < 0)
736			return 0;
737		if (umove(tcp, usp + __SIGNAL_FRAMESIZE, &sc) < 0)
738			return 0;
739		tprintsigmask_addr(") (mask ", sc.oldmask);
740	}
741#elif defined(I386) || defined(X86_64)
742# if defined(X86_64)
743	if (current_personality == 0) /* 64-bit */
744		return 0;
745# endif
746	if (entering(tcp)) {
747		struct i386_sigcontext_struct {
748			uint16_t gs, __gsh;
749			uint16_t fs, __fsh;
750			uint16_t es, __esh;
751			uint16_t ds, __dsh;
752			uint32_t edi;
753			uint32_t esi;
754			uint32_t ebp;
755			uint32_t esp;
756			uint32_t ebx;
757			uint32_t edx;
758			uint32_t ecx;
759			uint32_t eax;
760			uint32_t trapno;
761			uint32_t err;
762			uint32_t eip;
763			uint16_t cs, __csh;
764			uint32_t eflags;
765			uint32_t esp_at_signal;
766			uint16_t ss, __ssh;
767			uint32_t i387;
768			uint32_t oldmask;
769			uint32_t cr2;
770		};
771		struct i386_fpstate {
772			uint32_t cw;
773			uint32_t sw;
774			uint32_t tag;
775			uint32_t ipoff;
776			uint32_t cssel;
777			uint32_t dataoff;
778			uint32_t datasel;
779			uint8_t  st[8][10]; /* 8*10 bytes: FP regs */
780			uint16_t status;
781			uint16_t magic;
782			uint32_t fxsr_env[6];
783			uint32_t mxcsr;
784			uint32_t reserved;
785			uint8_t  stx[8][16]; /* 8*16 bytes: FP regs, each padded to 16 bytes */
786			uint8_t  xmm[8][16]; /* 8 XMM regs */
787			uint32_t padding1[44];
788			uint32_t padding2[12]; /* union with struct _fpx_sw_bytes */
789		};
790		struct {
791			struct i386_sigcontext_struct sc;
792			struct i386_fpstate fp;
793			uint32_t extramask[1];
794		} signal_stack;
795		/* On i386, sc is followed on stack by struct fpstate
796		 * and after it an additional u32 extramask[1] which holds
797		 * upper half of the mask.
798		 */
799		uint32_t sigmask[2];
800		if (umove(tcp, *i386_esp_ptr, &signal_stack) < 0)
801			return 0;
802		sigmask[0] = signal_stack.sc.oldmask;
803		sigmask[1] = signal_stack.extramask[0];
804		tprintsigmask_addr(") (mask ", sigmask);
805	}
806#elif defined(IA64)
807	if (entering(tcp)) {
808		struct sigcontext sc;
809		long sp;
810		/* offset of sigcontext in the kernel's sigframe structure: */
811#		define SIGFRAME_SC_OFFSET	0x90
812		if (upeek(tcp->pid, PT_R12, &sp) < 0)
813			return 0;
814		if (umove(tcp, sp + 16 + SIGFRAME_SC_OFFSET, &sc) < 0)
815			return 0;
816		tprintsigmask_val(") (mask ", sc.sc_mask);
817	}
818#elif defined(POWERPC)
819	if (entering(tcp)) {
820		long esp;
821		struct sigcontext sc;
822
823		esp = ppc_regs.gpr[1];
824
825		/* Skip dummy stack frame. */
826#ifdef POWERPC64
827		if (current_personality == 0)
828			esp += 128;
829		else
830			esp += 64;
831#else
832		esp += 64;
833#endif
834		if (umove(tcp, esp, &sc) < 0)
835			return 0;
836		tprintsigmask_val(") (mask ", sc.oldmask);
837	}
838#elif defined(M68K)
839	if (entering(tcp)) {
840		long usp;
841		struct sigcontext sc;
842		if (upeek(tcp->pid, 4*PT_USP, &usp) < 0)
843			return 0;
844		if (umove(tcp, usp, &sc) < 0)
845			return 0;
846		tprintsigmask_val(") (mask ", sc.sc_mask);
847	}
848#elif defined(ALPHA)
849	if (entering(tcp)) {
850		long fp;
851		struct sigcontext sc;
852		if (upeek(tcp->pid, REG_FP, &fp) < 0)
853			return 0;
854		if (umove(tcp, fp, &sc) < 0)
855			return 0;
856		tprintsigmask_val(") (mask ", sc.sc_mask);
857	}
858#elif defined(SPARC) || defined(SPARC64)
859	if (entering(tcp)) {
860		long i1;
861		m_siginfo_t si;
862		i1 = sparc_regs.u_regs[U_REG_O1];
863		if (umove(tcp, i1, &si) < 0) {
864			perror_msg("sigreturn: umove");
865			return 0;
866		}
867		tprintsigmask_val(") (mask ", si.si_mask);
868	}
869#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
870	/* This decodes rt_sigreturn.  The 64-bit ABIs do not have
871	   sigreturn.  */
872	if (entering(tcp)) {
873		long sp;
874		struct ucontext uc;
875		if (upeek(tcp->pid, REG_SP, &sp) < 0)
876			return 0;
877		/* There are six words followed by a 128-byte siginfo.  */
878		sp = sp + 6 * 4 + 128;
879		if (umove(tcp, sp, &uc) < 0)
880			return 0;
881		tprintsigmask_val(") (mask ", uc.uc_sigmask);
882	}
883#elif defined(MIPS)
884	if (entering(tcp)) {
885		long sp;
886		struct pt_regs regs;
887		m_siginfo_t si;
888		if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0) {
889			perror_msg("sigreturn: PTRACE_GETREGS");
890			return 0;
891		}
892		sp = regs.regs[29];
893		if (umove(tcp, sp, &si) < 0)
894			return 0;
895		tprintsigmask_val(") (mask ", si.si_mask);
896	}
897#elif defined(CRISV10) || defined(CRISV32)
898	if (entering(tcp)) {
899		struct sigcontext sc;
900		long regs[PT_MAX+1];
901		if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long)regs) < 0) {
902			perror_msg("sigreturn: PTRACE_GETREGS");
903			return 0;
904		}
905		if (umove(tcp, regs[PT_USP], &sc) < 0)
906			return 0;
907		tprintsigmask_val(") (mask ", sc.oldmask);
908	}
909#elif defined(TILE)
910	if (entering(tcp)) {
911		struct ucontext uc;
912
913		/* offset of ucontext in the kernel's sigframe structure */
914#		define SIGFRAME_UC_OFFSET C_ABI_SAVE_AREA_SIZE + sizeof(siginfo_t)
915		if (umove(tcp, tile_regs.sp + SIGFRAME_UC_OFFSET, &uc) < 0)
916			return 0;
917		tprintsigmask_val(") (mask ", uc.uc_sigmask);
918	}
919#elif defined(MICROBLAZE)
920	/* TODO: Verify that this is correct...  */
921	if (entering(tcp)) {
922		struct sigcontext sc;
923		long sp;
924		/* Read r1, the stack pointer.  */
925		if (upeek(tcp->pid, 1 * 4, &sp) < 0)
926			return 0;
927		if (umove(tcp, sp, &sc) < 0)
928			return 0;
929		tprintsigmask_val(") (mask ", sc.oldmask);
930	}
931#elif defined(XTENSA)
932	/* Xtensa only has rt_sys_sigreturn */
933#elif defined(ARC)
934	/* ARC syscall ABI only supports rt_sys_sigreturn */
935#else
936# warning No sys_sigreturn() for this architecture
937# warning         (no problem, just a reminder :-)
938#endif
939	return 0;
940}
941
942int
943sys_siggetmask(struct tcb *tcp)
944{
945	if (exiting(tcp)) {
946		tcp->auxstr = sprintsigmask_val("mask ", tcp->u_rval);
947	}
948	return RVAL_HEX | RVAL_STR;
949}
950
951int
952sys_sigsuspend(struct tcb *tcp)
953{
954	if (entering(tcp)) {
955		tprintsigmask_val("", tcp->u_arg[2]);
956	}
957	return 0;
958}
959
960#if !defined SS_ONSTACK
961#define SS_ONSTACK      1
962#define SS_DISABLE      2
963#endif
964
965#include "xlat/sigaltstack_flags.h"
966
967static void
968print_stack_t(struct tcb *tcp, unsigned long addr)
969{
970	stack_t ss;
971	int r;
972
973	if (!addr) {
974		tprints("NULL");
975		return;
976	}
977
978#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
979	if (current_wordsize != sizeof(ss.ss_sp) && current_wordsize == 4) {
980		struct {
981			uint32_t ss_sp;
982			int32_t ss_flags;
983			uint32_t ss_size;
984		} ss32;
985		r = umove(tcp, addr, &ss32);
986		if (r >= 0) {
987			memset(&ss, 0, sizeof(ss));
988			ss.ss_sp = (void*)(unsigned long) ss32.ss_sp;
989			ss.ss_flags = ss32.ss_flags;
990			ss.ss_size = (unsigned long) ss32.ss_size;
991		}
992	} else
993#endif
994	{
995		r = umove(tcp, addr, &ss);
996	}
997	if (r < 0) {
998		tprintf("%#lx", addr);
999	} else {
1000		tprintf("{ss_sp=%#lx, ss_flags=", (unsigned long) ss.ss_sp);
1001		printflags(sigaltstack_flags, ss.ss_flags, "SS_???");
1002		tprintf(", ss_size=%lu}", (unsigned long) ss.ss_size);
1003	}
1004}
1005
1006int
1007sys_sigaltstack(struct tcb *tcp)
1008{
1009	if (entering(tcp)) {
1010		print_stack_t(tcp, tcp->u_arg[0]);
1011	}
1012	else {
1013		tprints(", ");
1014		print_stack_t(tcp, tcp->u_arg[1]);
1015	}
1016	return 0;
1017}
1018
1019#ifdef HAVE_SIGACTION
1020
1021/* "Old" sigprocmask, which operates with word-sized signal masks */
1022int
1023sys_sigprocmask(struct tcb *tcp)
1024{
1025# ifdef ALPHA
1026	if (entering(tcp)) {
1027		/*
1028		 * Alpha/OSF is different: it doesn't pass in two pointers,
1029		 * but rather passes in the new bitmask as an argument and
1030		 * then returns the old bitmask.  This "works" because we
1031		 * only have 64 signals to worry about.  If you want more,
1032		 * use of the rt_sigprocmask syscall is required.
1033		 * Alpha:
1034		 *	old = osf_sigprocmask(how, new);
1035		 * Everyone else:
1036		 *	ret = sigprocmask(how, &new, &old, ...);
1037		 */
1038		printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1039		tprintsigmask_val(", ", tcp->u_arg[1]);
1040	}
1041	else if (!syserror(tcp)) {
1042		tcp->auxstr = sprintsigmask_val("old mask ", tcp->u_rval);
1043		return RVAL_HEX | RVAL_STR;
1044	}
1045# else /* !ALPHA */
1046	if (entering(tcp)) {
1047		printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1048		tprints(", ");
1049		print_sigset_addr_len(tcp, tcp->u_arg[1], current_wordsize);
1050		tprints(", ");
1051	}
1052	else {
1053		if (syserror(tcp))
1054			tprintf("%#lx", tcp->u_arg[2]);
1055		else
1056			print_sigset_addr_len(tcp, tcp->u_arg[2], current_wordsize);
1057	}
1058# endif /* !ALPHA */
1059	return 0;
1060}
1061
1062#endif /* HAVE_SIGACTION */
1063
1064int
1065sys_kill(struct tcb *tcp)
1066{
1067	if (entering(tcp)) {
1068		tprintf("%ld, %s",
1069			widen_to_long(tcp->u_arg[0]),
1070			signame(tcp->u_arg[1])
1071		);
1072	}
1073	return 0;
1074}
1075
1076int
1077sys_tgkill(struct tcb *tcp)
1078{
1079	if (entering(tcp)) {
1080		tprintf("%ld, %ld, %s",
1081			widen_to_long(tcp->u_arg[0]),
1082			widen_to_long(tcp->u_arg[1]),
1083			signame(tcp->u_arg[2])
1084		);
1085	}
1086	return 0;
1087}
1088
1089int
1090sys_sigpending(struct tcb *tcp)
1091{
1092	if (exiting(tcp)) {
1093		if (syserror(tcp))
1094			tprintf("%#lx", tcp->u_arg[0]);
1095		else
1096			print_sigset_addr_len(tcp, tcp->u_arg[0], current_wordsize);
1097	}
1098	return 0;
1099}
1100
1101int
1102sys_rt_sigprocmask(struct tcb *tcp)
1103{
1104	/* Note: arg[3] is the length of the sigset. Kernel requires NSIG / 8 */
1105	if (entering(tcp)) {
1106		printxval(sigprocmaskcmds, tcp->u_arg[0], "SIG_???");
1107		tprints(", ");
1108		print_sigset_addr_len(tcp, tcp->u_arg[1], tcp->u_arg[3]);
1109		tprints(", ");
1110	}
1111	else {
1112		if (syserror(tcp))
1113			tprintf("%#lx", tcp->u_arg[2]);
1114		else
1115			print_sigset_addr_len(tcp, tcp->u_arg[2], tcp->u_arg[3]);
1116		tprintf(", %lu", tcp->u_arg[3]);
1117	}
1118	return 0;
1119}
1120
1121/* Structure describing the action to be taken when a signal arrives.  */
1122struct new_sigaction
1123{
1124	/* sa_handler may be a libc #define, need to use other name: */
1125#ifdef MIPS
1126	unsigned int sa_flags;
1127	void (*__sa_handler)(int);
1128#else
1129	void (*__sa_handler)(int);
1130	unsigned long sa_flags;
1131	void (*sa_restorer)(void);
1132#endif /* !MIPS */
1133	/* Kernel treats sa_mask as an array of longs. */
1134	unsigned long sa_mask[NSIG / sizeof(long) ? NSIG / sizeof(long) : 1];
1135};
1136/* Same for i386-on-x86_64 and similar cases */
1137struct new_sigaction32
1138{
1139	uint32_t __sa_handler;
1140	uint32_t sa_flags;
1141	uint32_t sa_restorer;
1142	uint32_t sa_mask[2 * (NSIG / sizeof(long) ? NSIG / sizeof(long) : 1)];
1143};
1144
1145static void
1146decode_new_sigaction(struct tcb *tcp, long addr)
1147{
1148	struct new_sigaction sa;
1149	int r;
1150
1151	if (!addr) {
1152		tprints("NULL");
1153		return;
1154	}
1155	if (!verbose(tcp) || (exiting(tcp) && syserror(tcp))) {
1156		tprintf("%#lx", addr);
1157		return;
1158	}
1159#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1160	if (current_wordsize != sizeof(sa.sa_flags) && current_wordsize == 4) {
1161		struct new_sigaction32 sa32;
1162		r = umove(tcp, addr, &sa32);
1163		if (r >= 0) {
1164			memset(&sa, 0, sizeof(sa));
1165			sa.__sa_handler = (void*)(unsigned long)sa32.__sa_handler;
1166			sa.sa_flags     = sa32.sa_flags;
1167			sa.sa_restorer  = (void*)(unsigned long)sa32.sa_restorer;
1168			/* Kernel treats sa_mask as an array of longs.
1169			 * For 32-bit process, "long" is uint32_t, thus, for example,
1170			 * 32th bit in sa_mask will end up as bit 0 in sa_mask[1].
1171			 * But for (64-bit) kernel, 32th bit in sa_mask is
1172			 * 32th bit in 0th (64-bit) long!
1173			 * For little-endian, it's the same.
1174			 * For big-endian, we swap 32-bit words.
1175			 */
1176			sa.sa_mask[0] = sa32.sa_mask[0] + ((long)(sa32.sa_mask[1]) << 32);
1177		}
1178	} else
1179#endif
1180	{
1181		r = umove(tcp, addr, &sa);
1182	}
1183	if (r < 0) {
1184		tprints("{...}");
1185		return;
1186	}
1187	/* Architectures using function pointers, like
1188	 * hppa, may need to manipulate the function pointer
1189	 * to compute the result of a comparison. However,
1190	 * the __sa_handler function pointer exists only in
1191	 * the address space of the traced process, and can't
1192	 * be manipulated by strace. In order to prevent the
1193	 * compiler from generating code to manipulate
1194	 * __sa_handler we cast the function pointers to long. */
1195	if ((long)sa.__sa_handler == (long)SIG_ERR)
1196		tprints("{SIG_ERR, ");
1197	else if ((long)sa.__sa_handler == (long)SIG_DFL)
1198		tprints("{SIG_DFL, ");
1199	else if ((long)sa.__sa_handler == (long)SIG_IGN)
1200		tprints("{SIG_IGN, ");
1201	else
1202		tprintf("{%#lx, ", (long) sa.__sa_handler);
1203	/*
1204	 * Sigset size is in tcp->u_arg[4] (SPARC)
1205	 * or in tcp->u_arg[3] (all other),
1206	 * but kernel won't handle sys_rt_sigaction
1207	 * with wrong sigset size (just returns EINVAL instead).
1208	 * We just fetch the right size, which is NSIG / 8.
1209	 */
1210	tprintsigmask_val("", sa.sa_mask);
1211	tprints(", ");
1212
1213	printflags(sigact_flags, sa.sa_flags, "SA_???");
1214#ifdef SA_RESTORER
1215	if (sa.sa_flags & SA_RESTORER)
1216		tprintf(", %p", sa.sa_restorer);
1217#endif
1218	tprints("}");
1219}
1220
1221int
1222sys_rt_sigaction(struct tcb *tcp)
1223{
1224	if (entering(tcp)) {
1225		printsignal(tcp->u_arg[0]);
1226		tprints(", ");
1227		decode_new_sigaction(tcp, tcp->u_arg[1]);
1228		tprints(", ");
1229	} else {
1230		decode_new_sigaction(tcp, tcp->u_arg[2]);
1231#if defined(SPARC) || defined(SPARC64)
1232		tprintf(", %#lx, %lu", tcp->u_arg[3], tcp->u_arg[4]);
1233#elif defined(ALPHA)
1234		tprintf(", %lu, %#lx", tcp->u_arg[3], tcp->u_arg[4]);
1235#else
1236		tprintf(", %lu", tcp->u_arg[3]);
1237#endif
1238	}
1239	return 0;
1240}
1241
1242int
1243sys_rt_sigpending(struct tcb *tcp)
1244{
1245	if (exiting(tcp)) {
1246		/*
1247		 * One of the few syscalls where sigset size (arg[1])
1248		 * is allowed to be <= NSIG / 8, not strictly ==.
1249		 * This allows non-rt sigpending() syscall
1250		 * to reuse rt_sigpending() code in kernel.
1251		 */
1252		if (syserror(tcp))
1253			tprintf("%#lx", tcp->u_arg[0]);
1254		else
1255			print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[1]);
1256		tprintf(", %lu", tcp->u_arg[1]);
1257	}
1258	return 0;
1259}
1260
1261int
1262sys_rt_sigsuspend(struct tcb *tcp)
1263{
1264	if (entering(tcp)) {
1265		/* NB: kernel requires arg[1] == NSIG / 8 */
1266		print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[1]);
1267		tprintf(", %lu", tcp->u_arg[1]);
1268	}
1269	return 0;
1270}
1271
1272static void
1273print_sigqueueinfo(struct tcb *tcp, int sig, unsigned long uinfo)
1274{
1275	printsignal(sig);
1276	tprints(", ");
1277	printsiginfo_at(tcp, uinfo);
1278}
1279
1280int
1281sys_rt_sigqueueinfo(struct tcb *tcp)
1282{
1283	if (entering(tcp)) {
1284		tprintf("%lu, ", tcp->u_arg[0]);
1285		print_sigqueueinfo(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1286	}
1287	return 0;
1288}
1289
1290int
1291sys_rt_tgsigqueueinfo(struct tcb *tcp)
1292{
1293	if (entering(tcp)) {
1294		tprintf("%lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
1295		print_sigqueueinfo(tcp, tcp->u_arg[2], tcp->u_arg[3]);
1296	}
1297	return 0;
1298}
1299
1300int sys_rt_sigtimedwait(struct tcb *tcp)
1301{
1302	/* NB: kernel requires arg[3] == NSIG / 8 */
1303	if (entering(tcp)) {
1304		print_sigset_addr_len(tcp, tcp->u_arg[0], tcp->u_arg[3]);
1305		tprints(", ");
1306		/* This is the only "return" parameter, */
1307		if (tcp->u_arg[1] != 0)
1308			return 0;
1309		/* ... if it's NULL, can decode all on entry */
1310		tprints("NULL, ");
1311	}
1312	else if (tcp->u_arg[1] != 0) {
1313		/* syscall exit, and u_arg[1] wasn't NULL */
1314		printsiginfo_at(tcp, tcp->u_arg[1]);
1315		tprints(", ");
1316	}
1317	else {
1318		/* syscall exit, and u_arg[1] was NULL */
1319		return 0;
1320	}
1321	print_timespec(tcp, tcp->u_arg[2]);
1322	tprintf(", %lu", tcp->u_arg[3]);
1323	return 0;
1324};
1325
1326int
1327sys_restart_syscall(struct tcb *tcp)
1328{
1329	if (entering(tcp))
1330		tprints("<... resuming interrupted call ...>");
1331	return 0;
1332}
1333
1334static int
1335do_signalfd(struct tcb *tcp, int flags_arg)
1336{
1337	/* NB: kernel requires arg[2] == NSIG / 8 */
1338	if (entering(tcp)) {
1339		printfd(tcp, tcp->u_arg[0]);
1340		tprints(", ");
1341		print_sigset_addr_len(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1342		tprintf(", %lu", tcp->u_arg[2]);
1343		if (flags_arg >= 0) {
1344			tprints(", ");
1345			printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
1346		}
1347	}
1348	return 0;
1349}
1350
1351int
1352sys_signalfd(struct tcb *tcp)
1353{
1354	return do_signalfd(tcp, -1);
1355}
1356
1357int
1358sys_signalfd4(struct tcb *tcp)
1359{
1360	return do_signalfd(tcp, 3);
1361}
1362