]> granicus.if.org Git - strace/blobdiff - ipc.c
Fix BLKTRACESTOP definition
[strace] / ipc.c
diff --git a/ipc.c b/ipc.c
index 3755ab530e157a9cc19330757f350fe6f92b5009..4c902bf0cd8bd32e2bf96f63174c13bb1cf3ce65 100644 (file)
--- a/ipc.c
+++ b/ipc.c
 # define IPC_64 0x100
 #endif
 
-extern struct xlat openmodes[];
 extern void printsigevent(struct tcb *tcp, long arg);
 
-static struct xlat msgctl_flags[] = {
+static const struct xlat msgctl_flags[] = {
        { IPC_RMID,     "IPC_RMID"      },
        { IPC_SET,      "IPC_SET"       },
        { IPC_STAT,     "IPC_STAT"      },
@@ -82,7 +81,7 @@ static struct xlat msgctl_flags[] = {
        { 0,            NULL            },
 };
 
-static struct xlat semctl_flags[] = {
+static const struct xlat semctl_flags[] = {
        { IPC_RMID,     "IPC_RMID"      },
        { IPC_SET,      "IPC_SET"       },
        { IPC_STAT,     "IPC_STAT"      },
@@ -101,7 +100,7 @@ static struct xlat semctl_flags[] = {
        { 0,            NULL            },
 };
 
-static struct xlat shmctl_flags[] = {
+static const struct xlat shmctl_flags[] = {
        { IPC_RMID,     "IPC_RMID"      },
        { IPC_SET,      "IPC_SET"       },
        { IPC_STAT,     "IPC_STAT"      },
@@ -119,17 +118,23 @@ static struct xlat shmctl_flags[] = {
        { 0,            NULL            },
 };
 
-static struct xlat resource_flags[] = {
+static const struct xlat resource_flags[] = {
        { IPC_CREAT,    "IPC_CREAT"     },
        { IPC_EXCL,     "IPC_EXCL"      },
        { IPC_NOWAIT,   "IPC_NOWAIT"    },
+       { 0,            NULL            },
+};
+
+static const struct xlat shm_resource_flags[] = {
+       { IPC_CREAT,    "IPC_CREAT"     },
+       { IPC_EXCL,     "IPC_EXCL"      },
 #ifdef SHM_HUGETLB
        { SHM_HUGETLB,  "SHM_HUGETLB"   },
 #endif
        { 0,            NULL            },
 };
 
-static struct xlat shm_flags[] = {
+static const struct xlat shm_flags[] = {
 #ifdef LINUX
        { SHM_REMAP,    "SHM_REMAP"     },
 #endif /* LINUX */
@@ -138,7 +143,7 @@ static struct xlat shm_flags[] = {
        { 0,            NULL            },
 };
 
-static struct xlat msg_flags[] = {
+static const struct xlat msg_flags[] = {
        { MSG_NOERROR,  "MSG_NOERROR"   },
 #ifdef LINUX
        { MSG_EXCEPT,   "MSG_EXCEPT"    },
@@ -147,16 +152,22 @@ static struct xlat msg_flags[] = {
        { 0,            NULL            },
 };
 
+static const struct xlat semop_flags[] = {
+       { SEM_UNDO,     "SEM_UNDO"      },
+       { IPC_NOWAIT,   "IPC_NOWAIT"    },
+       { 0,            NULL            },
+};
+
 int sys_msgget(tcp)
 struct tcb *tcp;
 {
        if (entering(tcp)) {
                if (tcp->u_arg[0])
-                       tprintf("%lu", tcp->u_arg[0]);
+                       tprintf("%#lx", tcp->u_arg[0]);
                else
                        tprintf("IPC_PRIVATE");
                tprintf(", ");
-               if (printflags(resource_flags, tcp->u_arg[1] & ~0777) != 0)
+               if (printflags(resource_flags, tcp->u_arg[1] & ~0777, NULL) != 0)
                        tprintf("|");
                tprintf("%#lo", tcp->u_arg[1] & 0777);
        }
@@ -171,116 +182,179 @@ struct tcb *tcp;
 # define PRINTCTL printxval
 #endif
 
+static int
+indirect_ipccall(tcp)
+struct tcb *tcp;
+{
+#ifdef LINUX
+#ifdef X86_64
+       return current_personality > 0;
+#endif
+#if defined IA64
+       return tcp->scno < 1024; /* ia32 emulation syscalls are low */
+#endif
+#if !defined MIPS && !defined HPPA
+       return 1;
+#endif
+#endif /* LINUX */
+       return 0;
+}
+
 int sys_msgctl(tcp)
 struct tcb *tcp;
 {
        if (entering(tcp)) {
                tprintf("%lu, ", tcp->u_arg[0]);
                PRINTCTL(msgctl_flags, tcp->u_arg[1], "MSG_???");
-#ifdef LINUX
-               tprintf(", %#lx", tcp->u_arg[3]);
-#else /* !LINUX */
-               tprintf(", %#lx", tcp->u_arg[2]);
-#endif /* !LINUX */
+               tprintf(", %#lx", tcp->u_arg[indirect_ipccall(tcp) ? 3 : 2]);
        }
        return 0;
 }
 
-int sys_msgsnd(tcp)
-struct tcb *tcp;
+static void
+tprint_msgsnd(struct tcb *tcp, long addr, unsigned long count,
+             unsigned long flags)
 {
        long mtype;
 
+       if (umove(tcp, addr, &mtype) < 0) {
+               tprintf("%#lx", addr);
+       } else {
+               tprintf("{%lu, ", mtype);
+               printstr(tcp, addr + sizeof(mtype), count);
+               tprintf("}");
+       }
+       tprintf(", %lu, ", count);
+       printflags(msg_flags, flags, "MSG_???");
+}
+
+int sys_msgsnd(struct tcb *tcp)
+{
        if (entering(tcp)) {
-               tprintf("%lu", tcp->u_arg[0]);
-#ifdef LINUX
-               umove(tcp, tcp->u_arg[3], &mtype);
-               tprintf(", {%lu, ", mtype);
-               printstr(tcp, tcp->u_arg[3] + sizeof(long),
-                       tcp->u_arg[1]);
-               tprintf("}, %lu", tcp->u_arg[1]);
-               tprintf(", ");
-               if (printflags(msg_flags, tcp->u_arg[2]) == 0)
-                       tprintf("0");
-#else /* !LINUX */
-               umove(tcp, tcp->u_arg[1], &mtype);
-               tprintf(", {%lu, ", mtype);
-               printstr(tcp, tcp->u_arg[1] + sizeof(long),
-                       tcp->u_arg[2]);
-               tprintf("}, %lu", tcp->u_arg[2]);
-               tprintf(", ");
-               if (printflags(msg_flags, tcp->u_arg[3]) == 0)
-                       tprintf("0");
-#endif /* !LINUX */
+               tprintf("%d, ", (int) tcp->u_arg[0]);
+               if (indirect_ipccall(tcp)) {
+                       tprint_msgsnd(tcp, tcp->u_arg[3], tcp->u_arg[1],
+                                     tcp->u_arg[2]);
+               } else {
+                       tprint_msgsnd(tcp, tcp->u_arg[1], tcp->u_arg[2],
+                                     tcp->u_arg[3]);
+               }
        }
        return 0;
 }
 
-int sys_msgrcv(tcp)
-struct tcb *tcp;
+static void
+tprint_msgrcv(struct tcb *tcp, long addr, unsigned long count, long msgtyp)
 {
        long mtype;
-#ifdef LINUX
-       struct ipc_wrapper {
-               struct msgbuf *msgp;
-               long msgtyp;
-       } tmp;
-#endif
 
+       if (syserror(tcp) || umove(tcp, addr, &mtype) < 0) {
+               tprintf("%#lx", addr);
+       } else {
+               tprintf("{%lu, ", mtype);
+               printstr(tcp, addr + sizeof(mtype), count);
+               tprintf("}");
+       }
+       tprintf(", %lu, %ld, ", count, msgtyp);
+}
 
-       if (exiting(tcp)) {
-               tprintf("%lu", tcp->u_arg[0]);
-#ifdef LINUX
-               umove(tcp, tcp->u_arg[3], &tmp);
-               umove(tcp, (long) tmp.msgp, &mtype);
-               tprintf(", {%lu, ", mtype);
-               printstr(tcp, (long) (tmp.msgp) + sizeof(long),
-                       tcp->u_arg[1]);
-               tprintf("}, %lu", tcp->u_arg[1]);
-               tprintf(", %ld", tmp.msgtyp);
-               tprintf(", ");
-               if (printflags(msg_flags, tcp->u_arg[2]) == 0)
-                       tprintf("0");
-#else /* !LINUX */
-               umove(tcp, tcp->u_arg[1], &mtype);
-               tprintf(", {%lu, ", mtype);
-               printstr(tcp, tcp->u_arg[1] + sizeof(long),
-                       tcp->u_arg[2]);
-               tprintf("}, %lu", tcp->u_arg[2]);
-               tprintf(", %ld", tcp->u_arg[3]);
-               tprintf(", ");
-               if (printflags(msg_flags, tcp->u_arg[4]) == 0)
-                       tprintf("0");
-#endif /* !LINUX */
+int sys_msgrcv(struct tcb *tcp)
+{
+       if (entering(tcp)) {
+               tprintf("%d, ", (int) tcp->u_arg[0]);
+       } else {
+               if (indirect_ipccall(tcp)) {
+                       struct ipc_wrapper {
+                               struct msgbuf *msgp;
+                               long msgtyp;
+                       } tmp;
+
+                       if (umove(tcp, tcp->u_arg[3], &tmp) < 0) {
+                               tprintf("%#lx, %lu, ",
+                                       tcp->u_arg[3], tcp->u_arg[1]);
+                       } else {
+                               tprint_msgrcv(tcp, (long) tmp.msgp,
+                                       tcp->u_arg[1], tmp.msgtyp);
+                       }
+                       printflags(msg_flags, tcp->u_arg[2], "MSG_???");
+               } else {
+                       tprint_msgrcv(tcp, tcp->u_arg[1],
+                               tcp->u_arg[2], tcp->u_arg[3]);
+                       printflags(msg_flags, tcp->u_arg[4], "MSG_???");
+               }
        }
        return 0;
 }
 
-int sys_semop(tcp)
-struct tcb *tcp;
+static void
+tprint_sembuf(struct tcb *tcp, long addr, unsigned long count)
+{
+       unsigned long i, max_count;
+
+       if (abbrev(tcp))
+               max_count = (max_strlen < count) ? max_strlen : count;
+       else
+               max_count = count;
+
+       if (!max_count) {
+               tprintf("%#lx, %lu", addr, count);
+               return;
+       }
+
+       for(i = 0; i < max_count; ++i) {
+               struct sembuf sb;
+               if (i)
+                       tprintf(", ");
+               if (umove(tcp, addr + i * sizeof(struct sembuf), &sb) < 0) {
+                       if (i) {
+                               tprintf("{???}");
+                               break;
+                       } else {
+                               tprintf("%#lx, %lu", addr, count);
+                               return;
+                       }
+               } else {
+                       if (!i)
+                               tprintf("{");
+                       tprintf("{%u, %d, ", sb.sem_num, sb.sem_op);
+                       printflags(semop_flags, sb.sem_flg, "SEM_???");
+                       tprintf("}");
+               }
+       }
+
+       if (i < max_count || max_count < count)
+               tprintf(", ...");
+
+       tprintf("}, %lu", count);
+}
+
+int sys_semop(struct tcb *tcp)
 {
        if (entering(tcp)) {
-               tprintf("%lu", tcp->u_arg[0]);
-#ifdef LINUX
-               tprintf(", %#lx", tcp->u_arg[3]);
-               tprintf(", %lu", tcp->u_arg[1]);
-#else /* !LINUX */
-               tprintf(", %#lx", tcp->u_arg[1]);
-               tprintf(", %lu", tcp->u_arg[2]);
-#endif /* !LINUX */
+               tprintf("%lu, ", tcp->u_arg[0]);
+               if (indirect_ipccall(tcp)) {
+                       tprint_sembuf(tcp, tcp->u_arg[3], tcp->u_arg[1]);
+               } else {
+                       tprint_sembuf(tcp, tcp->u_arg[1], tcp->u_arg[2]);
+               }
        }
        return 0;
 }
 
 #ifdef LINUX
-int sys_semtimedop(tcp)
-struct tcb *tcp;
+int sys_semtimedop(struct tcb *tcp)
 {
        if (entering(tcp)) {
-               tprintf("%lu", tcp->u_arg[0]);
-               tprintf(", %#lx", tcp->u_arg[3]);
-               tprintf(", %lu, ", tcp->u_arg[1]);
-               printtv(tcp, tcp->u_arg[5]);
+               tprintf("%lu, ", tcp->u_arg[0]);
+               if (indirect_ipccall(tcp)) {
+                       tprint_sembuf(tcp, tcp->u_arg[3], tcp->u_arg[1]);
+                       tprintf(", ");
+                       printtv(tcp, tcp->u_arg[5]);
+               } else {
+                       tprint_sembuf(tcp, tcp->u_arg[1], tcp->u_arg[2]);
+                       tprintf(", ");
+                       printtv(tcp, tcp->u_arg[3]);
+               }
        }
        return 0;
 }
@@ -291,12 +365,12 @@ struct tcb *tcp;
 {
        if (entering(tcp)) {
                if (tcp->u_arg[0])
-                       tprintf("%lu", tcp->u_arg[0]);
+                       tprintf("%#lx", tcp->u_arg[0]);
                else
                        tprintf("IPC_PRIVATE");
                tprintf(", %lu", tcp->u_arg[1]);
                tprintf(", ");
-               if (printflags(resource_flags, tcp->u_arg[2] & ~0777) != 0)
+               if (printflags(resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
                        tprintf("|");
                tprintf("%#lo", tcp->u_arg[2] & 0777);
        }
@@ -320,12 +394,12 @@ struct tcb *tcp;
 {
        if (entering(tcp)) {
                if (tcp->u_arg[0])
-                       tprintf("%lu", tcp->u_arg[0]);
+                       tprintf("%#lx", tcp->u_arg[0]);
                else
                        tprintf("IPC_PRIVATE");
                tprintf(", %lu", tcp->u_arg[1]);
                tprintf(", ");
-               if (printflags(resource_flags, tcp->u_arg[2] & ~0777) != 0)
+               if (printflags(shm_resource_flags, tcp->u_arg[2] & ~0777, NULL) != 0)
                        tprintf("|");
                tprintf("%#lo", tcp->u_arg[2] & 0777);
        }
@@ -338,11 +412,11 @@ struct tcb *tcp;
        if (entering(tcp)) {
                tprintf("%lu, ", tcp->u_arg[0]);
                PRINTCTL(shmctl_flags, tcp->u_arg[1], "SHM_???");
-#ifdef LINUX
-               tprintf(", %#lx", tcp->u_arg[3]);
-#else /* !LINUX */
-               tprintf(", %#lx", tcp->u_arg[2]);
-#endif /* !LINUX */
+               if (indirect_ipccall(tcp)) {
+                       tprintf(", %#lx", tcp->u_arg[3]);
+               } else {
+                       tprintf(", %#lx", tcp->u_arg[2]);
+               }
        }
        return 0;
 }
@@ -356,20 +430,19 @@ struct tcb *tcp;
 
        if (exiting(tcp)) {
                tprintf("%lu", tcp->u_arg[0]);
-#ifdef LINUX
-               tprintf(", %#lx", tcp->u_arg[3]);
-               tprintf(", ");
-               if (printflags(shm_flags, tcp->u_arg[1]) == 0)
-                       tprintf("0");
-#else /* !LINUX */
-               tprintf(", %#lx", tcp->u_arg[1]);
-               tprintf(", ");
-               if (printflags(shm_flags, tcp->u_arg[2]) == 0)
-                       tprintf("0");
-#endif /* !LINUX */
+               if (indirect_ipccall(tcp)) {
+                       tprintf(", %#lx", tcp->u_arg[3]);
+                       tprintf(", ");
+                       printflags(shm_flags, tcp->u_arg[1], "SHM_???");
+               } else {
+                       tprintf(", %#lx", tcp->u_arg[1]);
+                       tprintf(", ");
+                       printflags(shm_flags, tcp->u_arg[2], "SHM_???");
+               }
                if (syserror(tcp))
                        return 0;
-#ifdef LINUX
+/* HPPA does not use an IPC multiplexer on Linux.  */
+#if defined(LINUX) && !defined(HPPA)
                if (umove(tcp, tcp->u_arg[2], &raddr) < 0)
                        return RVAL_NONE;
                tcp->u_rval = raddr;
@@ -382,26 +455,27 @@ struct tcb *tcp;
 int sys_shmdt(tcp)
 struct tcb *tcp;
 {
-       if (entering(tcp))
-#ifdef LINUX
-               tprintf("%#lx", tcp->u_arg[3]);
-#else /* !LINUX */
-               tprintf("%#lx", tcp->u_arg[0]);
-#endif /* !LINUX */
+       if (entering(tcp)) {
+               if (indirect_ipccall(tcp)) {
+                       tprintf("%#lx", tcp->u_arg[3]);
+               } else {
+                       tprintf("%#lx", tcp->u_arg[0]);
+               }
+       }
        return 0;
 }
 
 #endif /* defined(LINUX) || defined(SUNOS4) || defined(FREEBSD) */
 
 #ifdef LINUX
-int sys_mq_open(tcp)
-struct tcb *tcp;
+int
+sys_mq_open(struct tcb *tcp)
 {
        if (entering(tcp)) {
                printpath(tcp, tcp->u_arg[0]);
                tprintf(", ");
                /* flags */
-               printflags(openmodes, tcp->u_arg[1] + 1);
+               tprint_open_modes(tcp->u_arg[1]);
                if (tcp->u_arg[1] & O_CREAT) {
 # ifndef HAVE_MQUEUE_H
                        tprintf(", %lx", tcp->u_arg[2]);
@@ -420,8 +494,8 @@ struct tcb *tcp;
        return 0;
 }
 
-int sys_mq_timedsend(tcp)
-struct tcb *tcp;
+int
+sys_mq_timedsend(struct tcb *tcp)
 {
        if (entering(tcp)) {
                tprintf("%ld, ", tcp->u_arg[0]);
@@ -432,8 +506,8 @@ struct tcb *tcp;
        return 0;
 }
 
-int sys_mq_timedreceive(tcp)
-struct tcb *tcp;
+int
+sys_mq_timedreceive(struct tcb *tcp)
 {
        if (entering(tcp))
                tprintf("%ld, ", tcp->u_arg[0]);
@@ -445,8 +519,8 @@ struct tcb *tcp;
        return 0;
 }
 
-int sys_mq_notify(tcp)
-struct tcb *tcp;
+int
+sys_mq_notify(struct tcb *tcp)
 {
        if (entering(tcp)) {
                tprintf("%ld, ", tcp->u_arg[0]);
@@ -455,9 +529,8 @@ struct tcb *tcp;
        return 0;
 }
 
-static void printmqattr(tcp, addr)
-struct tcb *tcp;
-long addr;
+static void
+printmqattr(struct tcb *tcp, long addr)
 {
        if (addr == 0)
                tprintf("NULL");
@@ -471,15 +544,15 @@ long addr;
                        return;
                }
                tprintf("{mq_flags=");
-               printflags(openmodes, attr.mq_flags + 1);
+               tprint_open_modes(attr.mq_flags);
                tprintf(", mq_maxmsg=%ld, mq_msgsize=%ld, mq_curmsg=%ld}",
                        attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
 # endif
        }
 }
 
-int sys_mq_getsetattr(tcp)
-struct tcb *tcp;
+int
+sys_mq_getsetattr(struct tcb *tcp)
 {
        if (entering(tcp)) {
                tprintf("%ld, ", tcp->u_arg[0]);