]> granicus.if.org Git - strace/blob - bjm.c
Fix preadv/pwritev offset decoding
[strace] / bjm.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  * 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 #include <fcntl.h>
33 #include <sys/stat.h>
34 #include <sys/wait.h>
35 #include <sys/resource.h>
36 #include <sys/utsname.h>
37 #include <sys/user.h>
38
39 /* Bits of module.flags.  */
40
41 #define MOD_UNINITIALIZED       0
42 #define MOD_RUNNING             1
43 #define MOD_DELETED             2
44 #define MOD_AUTOCLEAN           4
45 #define MOD_VISITED             8
46 #define MOD_USED_ONCE           16
47 #define MOD_JUST_FREED          32
48 #define MOD_INITIALIZING        64
49
50 /* Values for query_module's which.  */
51
52 #define QM_MODULES      1
53 #define QM_DEPS         2
54 #define QM_REFS         3
55 #define QM_SYMBOLS      4
56 #define QM_INFO         5
57
58 struct module_symbol
59 {
60         unsigned long value;
61         const char *name;
62 };
63
64 struct module_info
65 {
66         unsigned long addr;
67         unsigned long size;
68         unsigned long flags;
69         long usecount;
70 };
71
72 static const struct xlat which[] = {
73         XLAT(0),
74         XLAT(QM_MODULES),
75         XLAT(QM_DEPS),
76         XLAT(QM_REFS),
77         XLAT(QM_SYMBOLS),
78         XLAT(QM_INFO),
79         XLAT_END
80 };
81
82 static const struct xlat modflags[] = {
83         XLAT(MOD_UNINITIALIZED),
84         XLAT(MOD_RUNNING),
85         XLAT(MOD_DELETED),
86         XLAT(MOD_AUTOCLEAN),
87         XLAT(MOD_VISITED),
88         XLAT(MOD_USED_ONCE),
89         XLAT(MOD_JUST_FREED),
90         XLAT_END
91 };
92
93 int
94 sys_query_module(struct tcb *tcp)
95 {
96         if (entering(tcp)) {
97                 printstr(tcp, tcp->u_arg[0], -1);
98                 tprints(", ");
99                 printxval(which, tcp->u_arg[1], "QM_???");
100                 tprints(", ");
101         } else {
102                 size_t ret;
103
104                 if (!verbose(tcp) || syserror(tcp) ||
105                     umove(tcp, tcp->u_arg[4], &ret) < 0) {
106                         tprintf("%#lx, %lu, %#lx", tcp->u_arg[2],
107                                 tcp->u_arg[3], tcp->u_arg[4]);
108                 } else if (tcp->u_arg[1]==QM_INFO) {
109                         struct module_info      mi;
110                         if (umove(tcp, tcp->u_arg[2], &mi) < 0) {
111                                 tprintf("%#lx, ", tcp->u_arg[2]);
112                         } else {
113                                 tprintf("{address=%#lx, size=%lu, flags=",
114                                         mi.addr, mi.size);
115                                 printflags(modflags, mi.flags, "MOD_???");
116                                 tprintf(", usecount=%lu}, ", mi.usecount);
117                         }
118                         tprintf("%lu", (unsigned long)ret);
119                 } else if ((tcp->u_arg[1]==QM_MODULES) ||
120                            (tcp->u_arg[1]==QM_DEPS) ||
121                            (tcp->u_arg[1]==QM_REFS)) {
122                         tprints("{");
123                         if (!abbrev(tcp)) {
124                                 char*   data    = malloc(tcp->u_arg[3]);
125                                 char*   mod     = data;
126                                 size_t  idx;
127
128                                 if (!data) {
129                                         fprintf(stderr, "out of memory\n");
130                                         tprintf(" /* %lu entries */ ", (unsigned long)ret);
131                                 } else {
132                                         if (umoven(tcp, tcp->u_arg[2],
133                                                 tcp->u_arg[3], data) < 0) {
134                                                 tprintf(" /* %lu entries */ ", (unsigned long)ret);
135                                         } else {
136                                                 for (idx = 0; idx < ret; idx++) {
137                                                         tprintf("%s%s",
138                                                                 (idx ? ", " : ""),
139                                                                 mod);
140                                                         mod += strlen(mod)+1;
141                                                 }
142                                         }
143                                         free(data);
144                                 }
145                         } else
146                                 tprintf(" /* %lu entries */ ", (unsigned long)ret);
147                         tprintf("}, %lu", (unsigned long)ret);
148                 } else if (tcp->u_arg[1]==QM_SYMBOLS) {
149                         tprints("{");
150                         if (!abbrev(tcp)) {
151                                 char*                   data    = malloc(tcp->u_arg[3]);
152                                 struct module_symbol*   sym     = (struct module_symbol*)data;
153                                 size_t                  idx;
154
155                                 if (!data) {
156                                         fprintf(stderr, "out of memory\n");
157                                         tprintf(" /* %lu entries */ ", (unsigned long)ret);
158                                 } else {
159                                         if (umoven(tcp, tcp->u_arg[2],
160                                                 tcp->u_arg[3], data) < 0) {
161                                                 tprintf(" /* %lu entries */ ", (unsigned long)ret);
162                                         } else {
163                                                 for (idx = 0; idx < ret; idx++) {
164                                                         tprintf("%s{name=%s, value=%lu}",
165                                                                 (idx ? " " : ""),
166                                                                 data+(long)sym->name,
167                                                                 sym->value);
168                                                         sym++;
169                                                 }
170                                         }
171                                         free(data);
172                                 }
173                         } else
174                                 tprintf(" /* %lu entries */ ", (unsigned long)ret);
175                         tprintf("}, %ld", (unsigned long)ret);
176                 } else {
177                         printstr(tcp, tcp->u_arg[2], tcp->u_arg[3]);
178                         tprintf(", %#lx", tcp->u_arg[4]);
179                 }
180         }
181         return 0;
182 }
183
184 int
185 sys_create_module(struct tcb *tcp)
186 {
187         if (entering(tcp)) {
188                 printpath(tcp, tcp->u_arg[0]);
189                 tprintf(", %lu", tcp->u_arg[1]);
190         }
191         return RVAL_HEX;
192 }
193
194 int
195 sys_init_module(struct tcb *tcp)
196 {
197         if (entering(tcp)) {
198                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
199                 printstr(tcp, tcp->u_arg[2], -1);
200         }
201         return 0;
202 }
203
204 #define MODULE_INIT_IGNORE_MODVERSIONS  1
205 #define MODULE_INIT_IGNORE_VERMAGIC     2
206
207 static const struct xlat module_init_flags[] = {
208         XLAT(MODULE_INIT_IGNORE_MODVERSIONS),
209         XLAT(MODULE_INIT_IGNORE_VERMAGIC),
210         XLAT_END
211 };
212
213 int
214 sys_finit_module(struct tcb *tcp)
215 {
216         if (exiting(tcp))
217                 return 0;
218
219         /* file descriptor */
220         printfd(tcp, tcp->u_arg[0]);
221         tprints(", ");
222         /* param_values */
223         printstr(tcp, tcp->u_arg[1], -1);
224         tprints(", ");
225         /* flags */
226         printflags(module_init_flags, tcp->u_arg[2], "MODULE_INIT_???");
227
228         return 0;
229 }