]> granicus.if.org Git - strace/blob - ipc.c
Fix time syscall decoding for some personalities
[strace] / ipc.c
1 /*
2  * Copyright (c) 1993 Ulrich Pegelow <pegelow@moorea.uni-muenster.de>
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  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "defs.h"
32 #ifdef HAVE_MQUEUE_H
33 # include <mqueue.h>
34 #endif
35 #include <fcntl.h>
36 #include <sys/ipc.h>
37 #include <sys/sem.h>
38 #include <sys/msg.h>
39 #include <sys/shm.h>
40
41 #if !defined IPC_64
42 # define IPC_64 0x100
43 #endif
44
45 extern void printsigevent(struct tcb *tcp, long arg);
46
47 #include "xlat/msgctl_flags.h"
48 #include "xlat/semctl_flags.h"
49 #include "xlat/shmctl_flags.h"
50 #include "xlat/resource_flags.h"
51 #include "xlat/shm_resource_flags.h"
52 #include "xlat/shm_flags.h"
53 #include "xlat/ipc_msg_flags.h"
54 #include "xlat/semop_flags.h"
55
56 SYS_FUNC(msgget)
57 {
58         if (tcp->u_arg[0])
59                 tprintf("%#lx, ", tcp->u_arg[0]);
60         else
61                 tprints("IPC_PRIVATE, ");
62         if (printflags(resource_flags, tcp->u_arg[1] & ~0777, NULL) != 0)
63                 tprints("|");
64         tprintf("%#lo", tcp->u_arg[1] & 0777);
65         return RVAL_DECODED;
66 }
67
68 #define PRINTCTL(flagset, arg, dflt) \
69         if ((arg) & IPC_64) tprints("IPC_64|"); \
70         printxval((flagset), (arg) &~ IPC_64, dflt)
71
72 static int
73 indirect_ipccall(struct tcb *tcp)
74 {
75         return tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL;
76 }
77
78 SYS_FUNC(msgctl)
79 {
80         tprintf("%lu, ", tcp->u_arg[0]);
81         PRINTCTL(msgctl_flags, tcp->u_arg[1], "MSG_???");
82         tprints(", ");
83         printaddr(tcp->u_arg[indirect_ipccall(tcp) ? 3 : 2]);
84         return RVAL_DECODED;
85 }
86
87 static void
88 tprint_msgbuf(struct tcb *tcp, const long addr, const unsigned long count)
89 {
90         long mtype;
91
92         if (!umove_or_printaddr(tcp, addr, &mtype)) {
93                 tprintf("{%lu, ", mtype);
94                 printstr(tcp, addr + sizeof(mtype), count);
95                 tprints("}");
96         }
97         tprintf(", %lu, ", count);
98 }
99
100 static void
101 tprint_msgsnd(struct tcb *tcp, const long addr, const unsigned long count,
102               const unsigned long flags)
103 {
104         tprint_msgbuf(tcp, addr, count);
105         printflags(ipc_msg_flags, flags, "MSG_???");
106 }
107
108 SYS_FUNC(msgsnd)
109 {
110         tprintf("%d, ", (int) tcp->u_arg[0]);
111         if (indirect_ipccall(tcp)) {
112                 tprint_msgsnd(tcp, tcp->u_arg[3], tcp->u_arg[1],
113                               tcp->u_arg[2]);
114         } else {
115                 tprint_msgsnd(tcp, tcp->u_arg[1], tcp->u_arg[2],
116                               tcp->u_arg[3]);
117         }
118         return RVAL_DECODED;
119 }
120
121 static void
122 tprint_msgrcv(struct tcb *tcp, const long addr, const unsigned long count,
123               const long msgtyp)
124 {
125         tprint_msgbuf(tcp, addr, count);
126         tprintf("%ld, ", msgtyp);
127 }
128
129 SYS_FUNC(msgrcv)
130 {
131         if (entering(tcp)) {
132                 tprintf("%d, ", (int) tcp->u_arg[0]);
133         } else {
134                 if (indirect_ipccall(tcp)) {
135                         struct ipc_wrapper {
136                                 struct msgbuf *msgp;
137                                 long msgtyp;
138                         } tmp;
139
140                         if (umove_or_printaddr(tcp, tcp->u_arg[3], &tmp))
141                                 tprintf(", %lu, ", tcp->u_arg[1]);
142                         else
143                                 tprint_msgrcv(tcp, (long) tmp.msgp,
144                                         tcp->u_arg[1], tmp.msgtyp);
145                         printflags(ipc_msg_flags, tcp->u_arg[2], "MSG_???");
146                 } else {
147                         tprint_msgrcv(tcp, tcp->u_arg[1],
148                                 tcp->u_arg[2], tcp->u_arg[3]);
149                         printflags(ipc_msg_flags, tcp->u_arg[4], "MSG_???");
150                 }
151         }
152         return 0;
153 }
154
155 static void
156 tprint_sembuf(const struct sembuf *sb)
157 {
158         tprintf("{%u, %d, ", sb->sem_num, sb->sem_op);
159         printflags(semop_flags, sb->sem_flg, "SEM_???");
160         tprints("}");
161 }
162
163 static void
164 tprint_sembuf_array(struct tcb *tcp, const long addr, const unsigned long count)
165 {
166         unsigned long max_count;
167         struct sembuf sb;
168
169         if (abbrev(tcp))
170                 max_count = (max_strlen < count) ? max_strlen : count;
171         else
172                 max_count = count;
173
174         if (!max_count)
175                 printaddr(addr);
176         else if (!umove_or_printaddr(tcp, addr, &sb)) {
177                 unsigned long i;
178
179                 tprints("[");
180                 tprint_sembuf(&sb);
181
182                 for (i = 1; i < max_count; ++i) {
183                         tprints(", ");
184                         if (umove_or_printaddr(tcp, addr + i * sizeof(sb), &sb))
185                                 break;
186                         else
187                                 tprint_sembuf(&sb);
188                 }
189
190                 if (i < max_count || max_count < count)
191                         tprints(", ...");
192
193                 tprints("]");
194         }
195         tprintf(", %lu", count);
196 }
197
198 SYS_FUNC(semop)
199 {
200         tprintf("%lu, ", tcp->u_arg[0]);
201         if (indirect_ipccall(tcp)) {
202                 tprint_sembuf_array(tcp, tcp->u_arg[3], tcp->u_arg[1]);
203         } else {
204                 tprint_sembuf_array(tcp, tcp->u_arg[1], tcp->u_arg[2]);
205         }
206         return RVAL_DECODED;
207 }
208
209 SYS_FUNC(semtimedop)
210 {
211         tprintf("%lu, ", tcp->u_arg[0]);
212         if (indirect_ipccall(tcp)) {
213                 tprint_sembuf_array(tcp, tcp->u_arg[3], tcp->u_arg[1]);
214                 tprints(", ");
215 #if defined(S390) || defined(S390X)
216                 printtv(tcp, tcp->u_arg[2]);
217 #else
218                 printtv(tcp, tcp->u_arg[4]);
219 #endif
220         } else {
221                 tprint_sembuf_array(tcp, tcp->u_arg[1], tcp->u_arg[2]);
222                 tprints(", ");
223                 printtv(tcp, tcp->u_arg[3]);
224         }
225         return RVAL_DECODED;
226 }
227
228 SYS_FUNC(semget)
229 {
230         if (tcp->u_arg[0])
231                 tprintf("%#lx", tcp->u_arg[0]);
232         else
233                 tprints("IPC_PRIVATE");
234         tprintf(", %lu, ", tcp->u_arg[1]);
235         if (printflags(resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
236                 tprints("|");
237         tprintf("%#lo", tcp->u_arg[2] & 0777);
238         return RVAL_DECODED;
239 }
240
241 SYS_FUNC(semctl)
242 {
243         tprintf("%lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
244         PRINTCTL(semctl_flags, tcp->u_arg[2], "SEM_???");
245         tprints(", ");
246         if (indirect_ipccall(tcp)) {
247                 if (current_wordsize == sizeof(int))
248                         printnum_int(tcp, tcp->u_arg[3], "%#x");
249                 else
250                         printnum_long(tcp, tcp->u_arg[3], "%#lx");
251         } else {
252                 tprintf("%#lx", tcp->u_arg[3]);
253         }
254         return RVAL_DECODED;
255 }
256
257 SYS_FUNC(shmget)
258 {
259         if (tcp->u_arg[0])
260                 tprintf("%#lx", tcp->u_arg[0]);
261         else
262                 tprints("IPC_PRIVATE");
263         tprintf(", %lu, ", tcp->u_arg[1]);
264         if (printflags(shm_resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
265                 tprints("|");
266         tprintf("%#lo", tcp->u_arg[2] & 0777);
267         return RVAL_DECODED;
268 }
269
270 SYS_FUNC(shmctl)
271 {
272         tprintf("%lu, ", tcp->u_arg[0]);
273         PRINTCTL(shmctl_flags, tcp->u_arg[1], "SHM_???");
274         tprints(", ");
275         printaddr(tcp->u_arg[indirect_ipccall(tcp) ? 3 : 2]);
276         return RVAL_DECODED;
277 }
278
279 SYS_FUNC(shmat)
280 {
281         if (entering(tcp)) {
282                 tprintf("%lu, ", tcp->u_arg[0]);
283                 if (indirect_ipccall(tcp)) {
284                         printaddr(tcp->u_arg[3]);
285                         tprints(", ");
286                         printflags(shm_flags, tcp->u_arg[1], "SHM_???");
287                 } else {
288                         printaddr(tcp->u_arg[1]);
289                         tprints(", ");
290                         printflags(shm_flags, tcp->u_arg[2], "SHM_???");
291                 }
292                 return 0;
293         } else {
294                 if (syserror(tcp))
295                         return 0;
296                 if (indirect_ipccall(tcp)) {
297                         unsigned long raddr;
298                         if (umove(tcp, tcp->u_arg[2], &raddr) < 0)
299                                 return RVAL_NONE;
300                         tcp->u_rval = raddr;
301                 }
302                 return RVAL_HEX;
303         }
304 }
305
306 SYS_FUNC(shmdt)
307 {
308         printaddr(tcp->u_arg[indirect_ipccall(tcp) ? 3 : 0]);
309         return RVAL_DECODED;
310 }
311
312 SYS_FUNC(mq_open)
313 {
314         printpath(tcp, tcp->u_arg[0]);
315         tprints(", ");
316         /* flags */
317         tprint_open_modes(tcp->u_arg[1]);
318         if (tcp->u_arg[1] & O_CREAT) {
319                 /* mode */
320                 tprintf(", %#lo, ", tcp->u_arg[2]);
321 # ifndef HAVE_MQUEUE_H
322                 printaddr(tcp->u_arg[3]);
323 # else
324                 struct mq_attr attr;
325
326                 if (!umove_or_printaddr(tcp, tcp->u_arg[3], &attr))
327                         tprintf("{mq_maxmsg=%ld, mq_msgsize=%ld}",
328                                 (long) attr.mq_maxmsg,
329                                 (long) attr.mq_msgsize);
330 # endif
331         }
332         return RVAL_DECODED;
333 }
334
335 SYS_FUNC(mq_timedsend)
336 {
337         tprintf("%ld, ", tcp->u_arg[0]);
338         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
339         tprintf(", %lu, %ld, ", tcp->u_arg[2], tcp->u_arg[3]);
340         printtv(tcp, tcp->u_arg[4]);
341         return RVAL_DECODED;
342 }
343
344 SYS_FUNC(mq_timedreceive)
345 {
346         if (entering(tcp))
347                 tprintf("%ld, ", tcp->u_arg[0]);
348         else {
349                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
350                 tprintf(", %lu, %ld, ", tcp->u_arg[2], tcp->u_arg[3]);
351                 printtv(tcp, tcp->u_arg[4]);
352         }
353         return 0;
354 }
355
356 SYS_FUNC(mq_notify)
357 {
358         tprintf("%ld, ", tcp->u_arg[0]);
359         printsigevent(tcp, tcp->u_arg[1]);
360         return RVAL_DECODED;
361 }
362
363 static void
364 printmqattr(struct tcb *tcp, const long addr)
365 {
366 # ifndef HAVE_MQUEUE_H
367         printaddr(addr);
368 # else
369         struct mq_attr attr;
370         if (umove_or_printaddr(tcp, addr, &attr))
371                 return;
372         tprints("{mq_flags=");
373         tprint_open_modes(attr.mq_flags);
374         tprintf(", mq_maxmsg=%ld, mq_msgsize=%ld, mq_curmsg=%ld}",
375                 (long) attr.mq_maxmsg, (long) attr.mq_msgsize,
376                 (long) attr.mq_curmsgs);
377 # endif
378 }
379
380 SYS_FUNC(mq_getsetattr)
381 {
382         if (entering(tcp)) {
383                 tprintf("%ld, ", tcp->u_arg[0]);
384                 printmqattr(tcp, tcp->u_arg[1]);
385                 tprints(", ");
386         } else
387                 printmqattr(tcp, tcp->u_arg[2]);
388         return 0;
389 }