]> granicus.if.org Git - strace/blob - process.c
xlat: treat socketlayers as a sorted array
[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  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. The name of the author may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include "defs.h"
39
40 #ifdef HAVE_ELF_H
41 # include <elf.h>
42 #endif
43
44 #include "ptrace.h"
45 #include "regs.h"
46
47 #include "xlat/nt_descriptor_types.h"
48 #include "xlat/ptrace_cmds.h"
49 #include "xlat/ptrace_setoptions_flags.h"
50 #include "xlat/ptrace_peeksiginfo_flags.h"
51
52 #define uoff(member)    offsetof(struct user, member)
53 #define XLAT_UOFF(member)       { uoff(member), "offsetof(struct user, " #member ")" }
54
55 static const struct xlat struct_user_offsets[] = {
56 #include "userent.h"
57         XLAT_END
58 };
59
60 static void
61 print_user_offset_addr(const kernel_ulong_t addr)
62 {
63         const struct xlat *x;
64
65         for (x = struct_user_offsets; x->str; ++x) {
66                 if (x->val >= addr)
67                         break;
68         }
69
70         if (!x->str) {
71                 printaddr(addr);
72         } else if (x->val > addr) {
73                 if (x == struct_user_offsets) {
74                         printaddr(addr);
75                 } else {
76                         --x;
77                         tprintf("%s + %" PRI_klu,
78                                 x->str, addr - (kernel_ulong_t) x->val);
79                 }
80         } else {
81                 tprints(x->str);
82         }
83 }
84
85 SYS_FUNC(ptrace)
86 {
87         const kernel_ulong_t request = tcp->u_arg[0];
88         const int pid = tcp->u_arg[1];
89         const kernel_ulong_t addr = tcp->u_arg[2];
90         const kernel_ulong_t data = tcp->u_arg[3];
91
92         if (entering(tcp)) {
93                 /* request */
94                 printxval64(ptrace_cmds, request, "PTRACE_???");
95
96                 if (request == PTRACE_TRACEME) {
97                         /* pid, addr, and data are ignored. */
98                         return RVAL_DECODED;
99                 }
100
101                 /* pid */
102                 tprintf(", %d", pid);
103
104                 /* addr */
105                 switch (request) {
106                 case PTRACE_ATTACH:
107                 case PTRACE_INTERRUPT:
108                 case PTRACE_KILL:
109                 case PTRACE_LISTEN:
110                         /* addr and data are ignored */
111                         return RVAL_DECODED;
112                 case PTRACE_PEEKUSER:
113                 case PTRACE_POKEUSER:
114                         tprints(", ");
115                         print_user_offset_addr(addr);
116                         break;
117                 case PTRACE_GETREGSET:
118                 case PTRACE_SETREGSET:
119                         tprints(", ");
120                         printxval(nt_descriptor_types, addr, "NT_???");
121                         break;
122                 case PTRACE_GETSIGMASK:
123                 case PTRACE_SETSIGMASK:
124                 case PTRACE_SECCOMP_GET_FILTER:
125                 case PTRACE_SECCOMP_GET_METADATA:
126                         tprintf(", %" PRI_klu, addr);
127                         break;
128                 case PTRACE_PEEKSIGINFO: {
129                         tprints(", ");
130                         struct {
131                                 uint64_t off;
132                                 uint32_t flags;
133                                 uint32_t nr;
134                         } psi;
135                         if (umove_or_printaddr(tcp, addr, &psi)) {
136                                 tprints(", ");
137                                 printaddr(data);
138                                 return RVAL_DECODED;
139                         }
140                         tprintf("{off=%" PRIu64 ", flags=", psi.off);
141                         printflags(ptrace_peeksiginfo_flags, psi.flags,
142                                    "PTRACE_PEEKSIGINFO_???");
143                         tprintf(", nr=%u}", psi.nr);
144                         break;
145                 }
146                 default:
147                         tprints(", ");
148                         printaddr(addr);
149                 }
150
151 # if defined IA64 || defined SPARC || defined SPARC64
152                 switch (request) {
153 #  ifdef IA64
154                 case PTRACE_PEEKDATA:
155                 case PTRACE_PEEKTEXT:
156                 case PTRACE_PEEKUSER:
157                         /* data is ignored */
158                         return RVAL_DECODED | RVAL_HEX;
159 #  endif /* IA64 */
160 #  if defined SPARC || defined SPARC64
161                 case PTRACE_GETREGS:
162                 case PTRACE_SETREGS:
163                 case PTRACE_GETFPREGS:
164                 case PTRACE_SETFPREGS:
165                         /* data is ignored */
166                         return RVAL_DECODED;
167 #  endif /* SPARC || SPARC64 */
168                 }
169 # endif /* IA64 || SPARC || SPARC64 */
170
171                 tprints(", ");
172
173                 /* data */
174                 switch (request) {
175                 case PTRACE_CONT:
176                 case PTRACE_DETACH:
177                 case PTRACE_SYSCALL:
178 #ifdef PTRACE_SINGLESTEP
179                 case PTRACE_SINGLESTEP:
180 #endif
181 #ifdef PTRACE_SINGLEBLOCK
182                 case PTRACE_SINGLEBLOCK:
183 #endif
184 #ifdef PTRACE_SYSEMU
185                 case PTRACE_SYSEMU:
186 #endif
187 #ifdef PTRACE_SYSEMU_SINGLESTEP
188                 case PTRACE_SYSEMU_SINGLESTEP:
189 #endif
190                         printsignal(data);
191                         break;
192                 case PTRACE_SEIZE:
193                 case PTRACE_SETOPTIONS:
194 #ifdef PTRACE_OLDSETOPTIONS
195                 case PTRACE_OLDSETOPTIONS:
196 #endif
197                         printflags64(ptrace_setoptions_flags, data, "PTRACE_O_???");
198                         break;
199                 case PTRACE_SETSIGINFO:
200                         printsiginfo_at(tcp, data);
201                         break;
202                 case PTRACE_SETSIGMASK:
203                         print_sigset_addr_len(tcp, data, addr);
204                         break;
205                 case PTRACE_SETREGSET:
206                         tprint_iov(tcp, /*len:*/ 1, data, IOV_DECODE_ADDR);
207                         break;
208                 case PTRACE_SECCOMP_GET_METADATA:
209                         if (verbose(tcp)) {
210                                 uint64_t filter_off;
211                                 if (addr < sizeof(filter_off) ||
212                                     umove(tcp, data, &filter_off)) {
213                                         printaddr(data);
214                                         return RVAL_DECODED;
215                                 }
216
217                                 tprintf("{filter_off=%" PRIu64, filter_off);
218                                 return 0;
219                         }
220
221                         printaddr(data);
222                         break;
223 #ifndef IA64
224                 case PTRACE_PEEKDATA:
225                 case PTRACE_PEEKTEXT:
226                 case PTRACE_PEEKUSER:
227 #endif
228                 case PTRACE_GETEVENTMSG:
229                 case PTRACE_GETREGSET:
230                 case PTRACE_GETSIGINFO:
231                 case PTRACE_GETSIGMASK:
232                 case PTRACE_PEEKSIGINFO:
233                 case PTRACE_SECCOMP_GET_FILTER:
234                         if (verbose(tcp)) {
235                                 /* print data on exiting syscall */
236                                 return 0;
237                         }
238                         ATTRIBUTE_FALLTHROUGH;
239                 default:
240                         printaddr(data);
241                         break;
242                 }
243
244                 return RVAL_DECODED;
245         } else {
246                 switch (request) {
247 #ifndef IA64
248                 case PTRACE_PEEKDATA:
249                 case PTRACE_PEEKTEXT:
250                 case PTRACE_PEEKUSER:
251                         printnum_ptr(tcp, data);
252                         break;
253 #endif
254                 case PTRACE_GETEVENTMSG:
255                         printnum_ulong(tcp, data);
256                         break;
257                 case PTRACE_GETREGSET:
258                         tprint_iov(tcp, /*len:*/ 1, data, IOV_DECODE_ADDR);
259                         break;
260                 case PTRACE_GETSIGINFO:
261                         printsiginfo_at(tcp, data);
262                         break;
263                 case PTRACE_GETSIGMASK:
264                         print_sigset_addr_len(tcp, data, addr);
265                         break;
266                 case PTRACE_PEEKSIGINFO:
267                         print_siginfo_array(tcp, data, tcp->u_rval);
268                         break;
269                 case PTRACE_SECCOMP_GET_FILTER:
270                         print_seccomp_fprog(tcp, data, tcp->u_rval);
271                         break;
272                 case PTRACE_SECCOMP_GET_METADATA: {
273                         const size_t offset = sizeof(uint64_t);
274                         uint64_t flags = 0;
275                         size_t ret_size = MIN((kernel_ulong_t) tcp->u_rval,
276                                               offset + sizeof(flags));
277
278                         if (syserror(tcp) || ret_size <= offset) {
279                                 tprints("}");
280                                 return 0;
281                         }
282
283                         if (umoven(tcp, data + offset, ret_size - offset,
284                                    &flags)) {
285                                 tprints(", ...}");
286                                 return 0;
287                         }
288
289                         tprints(", flags=");
290                         printflags64(seccomp_filter_flags, flags,
291                                      "SECCOMP_FILTER_FLAG_???");
292
293                         if ((kernel_ulong_t) tcp->u_rval > ret_size)
294                                 tprints(", ...");
295
296                         tprints("}");
297                 }
298                 }
299         }
300         return 0;
301 }