]> granicus.if.org Git - strace/blob - process.c
Implement PTRACE_GET_SYSCALL_INFO decoder
[strace] / process.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  * Copyright (c) 2000 PocketPenguins Inc.  Linux for Hitachi SuperH
10  *                    port by Greg Banks <gbanks@pocketpenguins.com>
11  * Copyright (c) 1999-2018 The strace developers.
12  *
13  * All rights reserved.
14  *
15  * SPDX-License-Identifier: LGPL-2.1-or-later
16  */
17
18 #include "defs.h"
19
20 #ifdef HAVE_ELF_H
21 # include <elf.h>
22 #endif
23
24 #include "ptrace.h"
25 #include "ptrace_syscall_info.h"
26 #include "regs.h"
27
28 #include "xlat/nt_descriptor_types.h"
29 #include "xlat/ptrace_cmds.h"
30 #include "xlat/ptrace_setoptions_flags.h"
31 #include "xlat/ptrace_peeksiginfo_flags.h"
32
33 #define uoff(member)    offsetof(struct user, member)
34 #define XLAT_UOFF(member)       { uoff(member), "offsetof(struct user, " #member ")" }
35
36 static const struct xlat struct_user_offsets[] = {
37 #include "userent.h"
38         XLAT_END
39 };
40
41 static void
42 print_user_offset_addr(const kernel_ulong_t addr)
43 {
44         bool no_str = false;
45         const struct xlat *x;
46
47         for (x = struct_user_offsets; x->str; ++x) {
48                 if (x->val >= addr)
49                         break;
50         }
51
52         if (!x->str || (x == struct_user_offsets && x->val > addr))
53                 no_str = true;
54         if (no_str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
55                 printaddr(addr);
56         if (no_str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
57                 return;
58
59         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE)
60                 tprints(" /* ");
61
62         if (x->val > addr) {
63                 --x;
64                 tprintf("%s + %" PRI_klu,
65                         x->str, addr - (kernel_ulong_t) x->val);
66         } else {
67                 tprints(x->str);
68         }
69
70         if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE)
71                 tprints(" */");
72 }
73
74 SYS_FUNC(ptrace)
75 {
76         const kernel_ulong_t request = tcp->u_arg[0];
77         const int pid = tcp->u_arg[1];
78         const kernel_ulong_t addr = tcp->u_arg[2];
79         const kernel_ulong_t data = tcp->u_arg[3];
80
81         if (entering(tcp)) {
82                 /* request */
83                 printxval64(ptrace_cmds, request, "PTRACE_???");
84
85                 if (request == PTRACE_TRACEME) {
86                         /* pid, addr, and data are ignored. */
87                         return RVAL_DECODED;
88                 }
89
90                 /* pid */
91                 tprintf(", %d", pid);
92
93                 /* addr */
94                 switch (request) {
95                 case PTRACE_ATTACH:
96                 case PTRACE_INTERRUPT:
97                 case PTRACE_KILL:
98                 case PTRACE_LISTEN:
99                         /* addr and data are ignored */
100                         return RVAL_DECODED;
101                 case PTRACE_PEEKUSER:
102                 case PTRACE_POKEUSER:
103                         tprints(", ");
104                         print_user_offset_addr(addr);
105                         break;
106                 case PTRACE_GETREGSET:
107                 case PTRACE_SETREGSET:
108                         tprints(", ");
109                         printxval(nt_descriptor_types, addr, "NT_???");
110                         break;
111                 case PTRACE_GETSIGMASK:
112                 case PTRACE_SETSIGMASK:
113                 case PTRACE_SECCOMP_GET_FILTER:
114                 case PTRACE_SECCOMP_GET_METADATA:
115                 case PTRACE_GET_SYSCALL_INFO:
116                         tprintf(", %" PRI_klu, addr);
117                         break;
118                 case PTRACE_PEEKSIGINFO: {
119                         tprints(", ");
120                         struct {
121                                 uint64_t off;
122                                 uint32_t flags;
123                                 uint32_t nr;
124                         } psi;
125                         if (umove_or_printaddr(tcp, addr, &psi)) {
126                                 tprints(", ");
127                                 printaddr(data);
128                                 return RVAL_DECODED;
129                         }
130                         tprintf("{off=%" PRIu64 ", flags=", psi.off);
131                         printflags(ptrace_peeksiginfo_flags, psi.flags,
132                                    "PTRACE_PEEKSIGINFO_???");
133                         tprintf(", nr=%u}", psi.nr);
134                         break;
135                 }
136                 default:
137                         tprints(", ");
138                         printaddr(addr);
139                 }
140
141 # if defined IA64 || defined SPARC || defined SPARC64
142                 switch (request) {
143 #  ifdef IA64
144                 case PTRACE_PEEKDATA:
145                 case PTRACE_PEEKTEXT:
146                 case PTRACE_PEEKUSER:
147                         /* data is ignored */
148                         return RVAL_DECODED | RVAL_HEX;
149 #  endif /* IA64 */
150 #  if defined SPARC || defined SPARC64
151                 case PTRACE_GETREGS:
152                 case PTRACE_SETREGS:
153                 case PTRACE_GETFPREGS:
154                 case PTRACE_SETFPREGS:
155                         /* data is ignored */
156                         return RVAL_DECODED;
157 #  endif /* SPARC || SPARC64 */
158                 }
159 # endif /* IA64 || SPARC || SPARC64 */
160
161                 tprints(", ");
162
163                 /* data */
164                 switch (request) {
165                 case PTRACE_CONT:
166                 case PTRACE_DETACH:
167                 case PTRACE_SYSCALL:
168 #ifdef PTRACE_SINGLESTEP
169                 case PTRACE_SINGLESTEP:
170 #endif
171 #ifdef PTRACE_SINGLEBLOCK
172                 case PTRACE_SINGLEBLOCK:
173 #endif
174 #ifdef PTRACE_SYSEMU
175                 case PTRACE_SYSEMU:
176 #endif
177 #ifdef PTRACE_SYSEMU_SINGLESTEP
178                 case PTRACE_SYSEMU_SINGLESTEP:
179 #endif
180                         printsignal(data);
181                         break;
182                 case PTRACE_SEIZE:
183                 case PTRACE_SETOPTIONS:
184 #ifdef PTRACE_OLDSETOPTIONS
185                 case PTRACE_OLDSETOPTIONS:
186 #endif
187                         printflags64(ptrace_setoptions_flags, data, "PTRACE_O_???");
188                         break;
189                 case PTRACE_SETSIGINFO:
190                         printsiginfo_at(tcp, data);
191                         break;
192                 case PTRACE_SETSIGMASK:
193                         print_sigset_addr_len(tcp, data, addr);
194                         break;
195                 case PTRACE_SETREGSET:
196                         tprint_iov(tcp, /*len:*/ 1, data, IOV_DECODE_ADDR);
197                         break;
198                 case PTRACE_SECCOMP_GET_METADATA:
199                         if (verbose(tcp)) {
200                                 uint64_t filter_off;
201                                 if (addr < sizeof(filter_off) ||
202                                     umove(tcp, data, &filter_off)) {
203                                         printaddr(data);
204                                         return RVAL_DECODED;
205                                 }
206
207                                 tprintf("{filter_off=%" PRIu64, filter_off);
208                                 return 0;
209                         }
210
211                         printaddr(data);
212                         break;
213 #ifndef IA64
214                 case PTRACE_PEEKDATA:
215                 case PTRACE_PEEKTEXT:
216                 case PTRACE_PEEKUSER:
217 #endif
218                 case PTRACE_GETEVENTMSG:
219                 case PTRACE_GETREGSET:
220                 case PTRACE_GETSIGINFO:
221                 case PTRACE_GETSIGMASK:
222                 case PTRACE_PEEKSIGINFO:
223                 case PTRACE_SECCOMP_GET_FILTER:
224                 case PTRACE_GET_SYSCALL_INFO:
225                         if (verbose(tcp)) {
226                                 /* print data on exiting syscall */
227                                 return 0;
228                         }
229                         ATTRIBUTE_FALLTHROUGH;
230                 default:
231                         printaddr(data);
232                         break;
233                 }
234
235                 return RVAL_DECODED;
236         } else {
237                 switch (request) {
238 #ifndef IA64
239                 case PTRACE_PEEKDATA:
240                 case PTRACE_PEEKTEXT:
241                 case PTRACE_PEEKUSER:
242                         printnum_ptr(tcp, data);
243                         break;
244 #endif
245                 case PTRACE_GETEVENTMSG:
246                         printnum_ulong(tcp, data);
247                         break;
248                 case PTRACE_GETREGSET:
249                         tprint_iov(tcp, /*len:*/ 1, data, IOV_DECODE_ADDR);
250                         break;
251                 case PTRACE_GETSIGINFO:
252                         printsiginfo_at(tcp, data);
253                         break;
254                 case PTRACE_GETSIGMASK:
255                         print_sigset_addr_len(tcp, data, addr);
256                         break;
257                 case PTRACE_PEEKSIGINFO:
258                         print_siginfo_array(tcp, data, tcp->u_rval);
259                         break;
260                 case PTRACE_SECCOMP_GET_FILTER:
261                         print_seccomp_fprog(tcp, data, tcp->u_rval);
262                         break;
263                 case PTRACE_SECCOMP_GET_METADATA: {
264                         const size_t offset = sizeof(uint64_t);
265                         uint64_t flags = 0;
266                         size_t ret_size = MIN((kernel_ulong_t) tcp->u_rval,
267                                               offset + sizeof(flags));
268
269                         if (syserror(tcp) || ret_size <= offset) {
270                                 tprints("}");
271                                 return 0;
272                         }
273
274                         if (umoven(tcp, data + offset, ret_size - offset,
275                                    &flags)) {
276                                 tprints(", ...}");
277                                 return 0;
278                         }
279
280                         tprints(", flags=");
281                         printflags64(seccomp_filter_flags, flags,
282                                      "SECCOMP_FILTER_FLAG_???");
283
284                         if ((kernel_ulong_t) tcp->u_rval > ret_size)
285                                 tprints(", ...");
286
287                         tprints("}");
288                         break;
289                 }
290                 case PTRACE_GET_SYSCALL_INFO:
291                         print_ptrace_syscall_info(tcp, data, addr);
292                         break;
293                 }
294         }
295         return 0;
296 }