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