summaryrefslogtreecommitdiff
path: root/src/printsiginfo.c
blob: 49b31fc05e3691b8aec9017a277e77d954b4e3ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
 * Copyright (c) 1993-1996 Rick Sladkey <jrs@world.std.com>
 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
 * Copyright (c) 2001 John Hughes <john@Calva.COM>
 * Copyright (c) 2013 Denys Vlasenko <vda.linux@googlemail.com>
 * Copyright (c) 2011-2015 Dmitry V. Levin <ldv@strace.io>
 * Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
 * Copyright (c) 2015-2022 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "defs.h"

#include DEF_MPERS_TYPE(siginfo_t)

#include <signal.h>
#include <linux/audit.h>

#include MPERS_DEFS

#ifndef IN_MPERS
# include "printsiginfo.h"
#endif

#define XLAT_MACROS_ONLY
/* For xlat/audit_arch.h */
# include "xlat/elf_em.h"
#undef XLAT_MACROS_ONLY

#include "xlat/audit_arch.h"
#include "xlat/sigbus_codes.h"
#include "xlat/sigchld_codes.h"
#include "xlat/sigfpe_codes.h"
#include "xlat/sigill_codes.h"
#include "xlat/siginfo_codes.h"
#include "xlat/sigpoll_codes.h"
#include "xlat/sigprof_codes.h"
#include "xlat/sigsegv_codes.h"
#include "xlat/sigsys_codes.h"
#include "xlat/sigtrap_codes.h"

#ifdef ALPHA
# include "xlat/alpha_trap_codes.h"
#endif

#ifdef SIGEMT
# include "xlat/sigemt_codes.h"
#endif

#ifdef HAVE_SIGINFO_T_SI_PERF_FLAGS
# include "xlat/sigtrap_perf_flags.h"
#endif

#ifndef SI_FROMUSER
# define SI_FROMUSER(sip)	((sip)->si_code <= 0)
#endif

static void
printsigsource(struct tcb *tcp, const siginfo_t *sip)
{
	tprint_struct_next();
	PRINT_FIELD_TGID(*sip, si_pid, tcp);
	tprint_struct_next();
	PRINT_FIELD_ID(*sip, si_uid);
}

static void
printsigval(const siginfo_t *sip)
{
	tprint_struct_next();
	PRINT_FIELD_D(*sip, si_int);
	tprint_struct_next();
	PRINT_FIELD_PTR(*sip, si_ptr);
}

static void
print_si_code(const unsigned int si_code, const int si_signo)
{
	static const struct xlat * const si_codes[] = {
		[SIGTRAP] = sigtrap_codes,
		[SIGCHLD] = sigchld_codes,
		[SIGIO]   = sigpoll_codes, /* SIGPOLL */
		[SIGPROF] = sigprof_codes,
		[SIGILL]  = sigill_codes,
#ifdef SIGEMT
		[SIGEMT]  = sigemt_codes,
#endif
		[SIGFPE]  = sigfpe_codes,
		[SIGSEGV] = sigsegv_codes,
		[SIGBUS]  = sigbus_codes,
		[SIGSYS]  = sigsys_codes,
	};

	const char *code = xlookup(siginfo_codes, si_code);

	if (!code && (unsigned int) si_signo < ARRAY_SIZE(si_codes)
	    && si_codes[si_signo])
		code = xlookup(si_codes[si_signo], si_code);

	print_xlat_ex(si_code, code, XLAT_STYLE_DEFAULT);
}

static void
print_si_info(struct tcb *tcp, const siginfo_t *sip)
{
	if (sip->si_errno) {
		tprint_struct_next();
		PRINT_FIELD_ERR_U(*sip, si_errno);
	}

	if (SI_FROMUSER(sip)) {
		switch (sip->si_code) {
		case SI_USER:
			printsigsource(tcp, sip);
			break;
		case SI_TKILL:
			printsigsource(tcp, sip);
			break;
#if defined HAVE_SIGINFO_T_SI_TIMERID && defined HAVE_SIGINFO_T_SI_OVERRUN
		case SI_TIMER:
			tprint_struct_next();
			PRINT_FIELD_X(*sip, si_timerid);
			tprint_struct_next();
			PRINT_FIELD_D(*sip, si_overrun);
			printsigval(sip);
			break;
#endif
		case SI_SIGIO:
			tprint_struct_next();
			PRINT_FIELD_D(*sip, si_band);
			tprint_struct_next();
			PRINT_FIELD_FD(*sip, si_fd, tcp);
			break;
		default:
			printsigsource(tcp, sip);
			if (sip->si_ptr)
				printsigval(sip);
			break;
		}
	} else {
		switch (sip->si_signo) {
		case SIGCHLD:
			printsigsource(tcp, sip);
			if (sip->si_code == CLD_EXITED) {
				tprint_struct_next();
				PRINT_FIELD_D(*sip, si_status);
			} else {
				tprint_struct_next();
				PRINT_FIELD_OBJ_VAL(*sip, si_status, printsignal);
			}
			tprint_struct_next();
			PRINT_FIELD_CLOCK_T(*sip, si_utime);
			tprint_struct_next();
			PRINT_FIELD_CLOCK_T(*sip, si_stime);
			break;
		case SIGILL:
			tprint_struct_next();
			PRINT_FIELD_PTR(*sip, si_addr);
#if defined(SPARC) || defined(SPARC64)
			tprint_struct_next();
			PRINT_FIELD_D(*sip, si_trapno);
#endif /* SPARC || SPARC64 */
			break;
		case SIGFPE:
			tprint_struct_next();
			PRINT_FIELD_PTR(*sip, si_addr);
#if defined ALPHA && defined HAVE_SIGINFO_T_SI_TRAPNO
			tprint_struct_next();
			PRINT_FIELD_XVAL_D(*sip, si_trapno, alpha_trap_codes,
					   "GEN_???");
#endif /* ALPHA */
			break;
		case SIGBUS:
			tprint_struct_next();
			PRINT_FIELD_PTR(*sip, si_addr);
#if !defined(BUS_OPFETCH) && defined(HAVE_SIGINFO_T_SI_ADDR_LSB)
			switch (sip->si_code) {
			case BUS_MCEERR_AR:
			case BUS_MCEERR_AO:
				tprint_struct_next();
				PRINT_FIELD_X(*sip, si_addr_lsb);
				break;
			}
#endif /* !BUS_OPFETCH && HAVE_SIGINFO_T_SI_ADDR_LSB */
			break;
		case SIGSEGV:
			tprint_struct_next();
			PRINT_FIELD_PTR(*sip, si_addr);
#if (!defined(SEGV_STACKFLOW) && defined(HAVE_SIGINFO_T_SI_LOWER)) \
    || (!defined(__SEGV_PSTKOVF) && defined(HAVE_SIGINFO_T_SI_PKEY))
			switch (sip->si_code) {
# if !defined(SEGV_STACKFLOW) && defined(HAVE_SIGINFO_T_SI_LOWER)
			case SEGV_BNDERR:
				tprint_struct_next();
				PRINT_FIELD_PTR(*sip, si_lower);
				tprint_struct_next();
				PRINT_FIELD_PTR(*sip, si_upper);
				break;
# endif /* !SEGV_STACKFLOW && HAVE_SIGINFO_T_SI_LOWER */
# if !defined(__SEGV_PSTKOVF) && defined(HAVE_SIGINFO_T_SI_PKEY)
			case SEGV_PKUERR:
				tprint_struct_next();
				PRINT_FIELD_U(*sip, si_pkey);
				break;
# endif /* !__SEGV_PSTKOVF && HAVE_SIGINFO_T_SI_PKEY */
			}
#endif /* !SEGV_STACKFLOW && HAVE_SIGINFO_T_SI_LOWER
	* || !__SEGV_PSTKOVF && HAVE_SIGINFO_T_SI_PKEY */
			break;
		case SIGTRAP:
			tprint_struct_next();
			PRINT_FIELD_PTR(*sip, si_addr);
#if (defined ALPHA && defined HAVE_SIGINFO_T_SI_TRAPNO) \
 || defined HAVE_SIGINFO_T_SI_PERF_DATA
			switch (sip->si_code) {
# if defined ALPHA && defined HAVE_SIGINFO_T_SI_TRAPNO
			case TRAP_UNK:
				tprint_struct_next();
				PRINT_FIELD_XVAL_D(*sip, si_trapno,
						   alpha_trap_codes, "GEN_???");
				break;
# endif /* ALPHA && HAVE_SIGINFO_T_SI_TRAPNO */
# ifdef HAVE_SIGINFO_T_SI_PERF_DATA
			case TRAP_PERF:
				tprint_struct_next();
				PRINT_FIELD_X(*sip, si_perf_data);
#  ifdef HAVE_SIGINFO_T_SI_PERF_TYPE
				tprint_struct_next();
				PRINT_FIELD_XVAL(*sip, si_perf_type,
						 perf_type_id, "PERF_TYPE_???");
#  endif /* HAVE_SIGINFO_T_SI_PERF_TYPE */
#  ifdef HAVE_SIGINFO_T_SI_PERF_FLAGS
				tprint_struct_next();
				PRINT_FIELD_FLAGS(*sip, si_perf_flags,
						  sigtrap_perf_flags,
						  "TRAP_PERF_FLAG_???");
#  endif /* HAVE_SIGINFO_T_SI_PERF_FLAGS */
# endif /* HAVE_SIGINFO_T_SI_PERF_DATA */
			}
#endif /* ALPHA || HAVE_SIGINFO_T_SI_PERF_DATA */
			break;
#ifdef SIGEMT
		case SIGEMT:
			tprint_struct_next();
			PRINT_FIELD_PTR(*sip, si_addr);
			break;
#endif
		case SIGIO: /* SIGPOLL */
			switch (sip->si_code) {
			case POLL_IN:  case POLL_OUT: case POLL_MSG:
			case POLL_ERR: case POLL_PRI: case POLL_HUP:
				tprint_struct_next();
				PRINT_FIELD_D(*sip, si_band);
				tprint_struct_next();
				PRINT_FIELD_FD(*sip, si_fd, tcp);
				break;
			}
			break;
#ifdef HAVE_SIGINFO_T_SI_SYSCALL
		case SIGSYS:
			tprint_struct_next();
			PRINT_FIELD_PTR(*sip, si_call_addr);
			tprint_struct_next();
			PRINT_FIELD_SYSCALL_NAME(*sip, si_syscall,
						 sip->si_arch);
			tprint_struct_next();
			PRINT_FIELD_XVAL(*sip, si_arch, audit_arch,
					 "AUDIT_ARCH_???");
			break;
#endif
		default:
			if (sip->si_pid || sip->si_uid)
				printsigsource(tcp, sip);
			if (sip->si_ptr)
				printsigval(sip);
		}
	}
}

#ifdef IN_MPERS
static
#endif
void
printsiginfo(struct tcb *tcp, const siginfo_t *sip)
{
	tprint_struct_begin();

	if (sip->si_signo) {
		PRINT_FIELD_OBJ_VAL(*sip, si_signo, printsignal);
		tprint_struct_next();
		PRINT_FIELD_OBJ_VAL(*sip, si_code, print_si_code,
				    sip->si_signo);

#ifdef SI_NOINFO
		if (sip->si_code != SI_NOINFO)
#endif
			print_si_info(tcp, sip);
	}

	tprint_struct_end();
}

MPERS_PRINTER_DECL(void, printsiginfo_at,
		   struct tcb *const tcp, const kernel_ulong_t addr)
{
	siginfo_t si;

	if (!umove_or_printaddr(tcp, addr, &si))
		printsiginfo(tcp, &si);
}

static bool
print_siginfo_t(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
{
	printsiginfo(tcp, (const siginfo_t *) elem_buf);
	return true;
}

MPERS_PRINTER_DECL(void, print_siginfo_array, struct tcb *const tcp,
		   const kernel_ulong_t addr, const kernel_ulong_t len)
{
	siginfo_t si;

	print_array(tcp, addr, len, &si, sizeof(si),
		    tfetch_mem, print_siginfo_t, 0);
}