Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 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 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 32 | #include "defs.h" |
| 33 | |
Wichert Akkerman | 2e2553a | 1999-05-09 00:29:58 +0000 | [diff] [blame] | 34 | #if defined(LINUX) |
| 35 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 36 | #include <fcntl.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/time.h> |
| 39 | #include <sys/wait.h> |
| 40 | #include <sys/resource.h> |
| 41 | #include <sys/utsname.h> |
| 42 | #include <sys/user.h> |
| 43 | #include <sys/syscall.h> |
| 44 | #include <signal.h> |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 45 | |
| 46 | /* Bits of module.flags. */ |
| 47 | |
| 48 | #define MOD_UNINITIALIZED 0 |
| 49 | #define MOD_RUNNING 1 |
| 50 | #define MOD_DELETED 2 |
| 51 | #define MOD_AUTOCLEAN 4 |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 52 | #define MOD_VISITED 8 |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 53 | #define MOD_USED_ONCE 16 |
| 54 | #define MOD_JUST_FREED 32 |
| 55 | #define MOD_INITIALIZING 64 |
| 56 | |
| 57 | /* Values for query_module's which. */ |
| 58 | |
| 59 | #define QM_MODULES 1 |
| 60 | #define QM_DEPS 2 |
| 61 | #define QM_REFS 3 |
| 62 | #define QM_SYMBOLS 4 |
| 63 | #define QM_INFO 5 |
| 64 | |
| 65 | struct module_symbol |
| 66 | { |
| 67 | unsigned long value; |
| 68 | const char *name; |
| 69 | }; |
| 70 | |
| 71 | struct module_info |
| 72 | { |
| 73 | unsigned long addr; |
| 74 | unsigned long size; |
| 75 | unsigned long flags; |
| 76 | long usecount; |
| 77 | }; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 78 | |
Roland McGrath | d9f816f | 2004-09-04 03:39:20 +0000 | [diff] [blame] | 79 | static const struct xlat which[] = { |
Wichert Akkerman | 9123ac8 | 1999-11-27 21:58:20 +0000 | [diff] [blame] | 80 | { 0, "0" }, |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 81 | { QM_MODULES, "QM_MODULES" }, |
Wichert Akkerman | 9123ac8 | 1999-11-27 21:58:20 +0000 | [diff] [blame] | 82 | { QM_DEPS, "QM_DEPS" }, |
| 83 | { QM_REFS, "QM_REFS" }, |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 84 | { QM_SYMBOLS, "QM_SYMBOLS" }, |
Wichert Akkerman | 9123ac8 | 1999-11-27 21:58:20 +0000 | [diff] [blame] | 85 | { QM_INFO, "QM_INFO" }, |
| 86 | { 0, NULL }, |
| 87 | }; |
| 88 | |
Roland McGrath | d9f816f | 2004-09-04 03:39:20 +0000 | [diff] [blame] | 89 | static const struct xlat modflags[] = { |
Wichert Akkerman | 9123ac8 | 1999-11-27 21:58:20 +0000 | [diff] [blame] | 90 | { MOD_UNINITIALIZED, "MOD_UNINITIALIZED" }, |
| 91 | { MOD_RUNNING, "MOD_RUNNING" }, |
| 92 | { MOD_DELETED, "MOD_DELETED" }, |
| 93 | { MOD_AUTOCLEAN, "MOD_AUTOCLEAN" }, |
| 94 | { MOD_VISITED, "MOD_VISITED" }, |
| 95 | { MOD_USED_ONCE, "MOD_USED_ONCE" }, |
| 96 | { MOD_JUST_FREED, "MOD_JUST_FREED" }, |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 97 | { 0, NULL }, |
| 98 | }; |
| 99 | |
| 100 | int |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 101 | sys_query_module(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 102 | { |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 103 | if (entering(tcp)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 104 | printstr(tcp, tcp->u_arg[0], -1); |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame^] | 105 | tprints(", "); |
Wichert Akkerman | 9123ac8 | 1999-11-27 21:58:20 +0000 | [diff] [blame] | 106 | printxval(which, tcp->u_arg[1], "QM_???"); |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame^] | 107 | tprints(", "); |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 108 | } else { |
| 109 | size_t ret; |
| 110 | |
| 111 | if (!verbose(tcp) || syserror(tcp) || |
| 112 | umove(tcp, tcp->u_arg[4], &ret) < 0) { |
| 113 | tprintf("%#lx, %lu, %#lx", tcp->u_arg[2], |
| 114 | tcp->u_arg[3], tcp->u_arg[4]); |
Wichert Akkerman | 9123ac8 | 1999-11-27 21:58:20 +0000 | [diff] [blame] | 115 | } else if (tcp->u_arg[1]==QM_INFO) { |
| 116 | struct module_info mi; |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 117 | if (umove(tcp, tcp->u_arg[2], &mi) < 0) { |
| 118 | tprintf("%#lx, ", tcp->u_arg[2]); |
| 119 | } else { |
| 120 | tprintf("{address=%#lx, size=%lu, flags=", |
| 121 | mi.addr, mi.size); |
| 122 | printflags(modflags, mi.flags, "MOD_???"); |
| 123 | tprintf(", usecount=%lu}, ", mi.usecount); |
| 124 | } |
| 125 | tprintf("%Zu", ret); |
Wichert Akkerman | 9123ac8 | 1999-11-27 21:58:20 +0000 | [diff] [blame] | 126 | } else if ((tcp->u_arg[1]==QM_MODULES) || |
Dmitry V. Levin | 414fe7d | 2009-07-08 11:21:17 +0000 | [diff] [blame] | 127 | (tcp->u_arg[1]==QM_DEPS) || |
| 128 | (tcp->u_arg[1]==QM_REFS)) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame^] | 129 | tprints("{"); |
Wichert Akkerman | aec6238 | 1999-11-28 01:56:12 +0000 | [diff] [blame] | 130 | if (!abbrev(tcp)) { |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 131 | char* data = malloc(tcp->u_arg[3]); |
Wichert Akkerman | aec6238 | 1999-11-28 01:56:12 +0000 | [diff] [blame] | 132 | char* mod = data; |
| 133 | size_t idx; |
| 134 | |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 135 | if (!data) { |
Roland McGrath | 46100d0 | 2005-06-01 18:55:42 +0000 | [diff] [blame] | 136 | fprintf(stderr, "out of memory\n"); |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 137 | tprintf(" /* %Zu entries */ ", ret); |
| 138 | } else { |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 139 | if (umoven(tcp, tcp->u_arg[2], |
| 140 | tcp->u_arg[3], data) < 0) { |
| 141 | tprintf(" /* %Zu entries */ ", ret); |
| 142 | } else { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 143 | for (idx = 0; idx < ret; idx++) { |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 144 | tprintf("%s%s", |
| 145 | (idx ? ", " : ""), |
| 146 | mod); |
| 147 | mod += strlen(mod)+1; |
| 148 | } |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 149 | } |
| 150 | free(data); |
Wichert Akkerman | aec6238 | 1999-11-28 01:56:12 +0000 | [diff] [blame] | 151 | } |
Roland McGrath | d9f816f | 2004-09-04 03:39:20 +0000 | [diff] [blame] | 152 | } else |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 153 | tprintf(" /* %Zu entries */ ", ret); |
| 154 | tprintf("}, %Zu", ret); |
Wichert Akkerman | 9123ac8 | 1999-11-27 21:58:20 +0000 | [diff] [blame] | 155 | } else if (tcp->u_arg[1]==QM_SYMBOLS) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame^] | 156 | tprints("{"); |
Wichert Akkerman | aec6238 | 1999-11-28 01:56:12 +0000 | [diff] [blame] | 157 | if (!abbrev(tcp)) { |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 158 | char* data = malloc(tcp->u_arg[3]); |
Wichert Akkerman | aec6238 | 1999-11-28 01:56:12 +0000 | [diff] [blame] | 159 | struct module_symbol* sym = (struct module_symbol*)data; |
| 160 | size_t idx; |
| 161 | |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 162 | if (!data) { |
Roland McGrath | 46100d0 | 2005-06-01 18:55:42 +0000 | [diff] [blame] | 163 | fprintf(stderr, "out of memory\n"); |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 164 | tprintf(" /* %Zu entries */ ", ret); |
| 165 | } else { |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 166 | if (umoven(tcp, tcp->u_arg[2], |
| 167 | tcp->u_arg[3], data) < 0) { |
| 168 | tprintf(" /* %Zu entries */ ", ret); |
| 169 | } else { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 170 | for (idx = 0; idx < ret; idx++) { |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 171 | tprintf("%s{name=%s, value=%lu}", |
| 172 | (idx ? " " : ""), |
| 173 | data+(long)sym->name, |
| 174 | sym->value); |
| 175 | sym++; |
| 176 | } |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 177 | } |
| 178 | free(data); |
Wichert Akkerman | aec6238 | 1999-11-28 01:56:12 +0000 | [diff] [blame] | 179 | } |
Wichert Akkerman | aec6238 | 1999-11-28 01:56:12 +0000 | [diff] [blame] | 180 | } else |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 181 | tprintf(" /* %Zu entries */ ", ret); |
| 182 | tprintf("}, %Zd", ret); |
Wichert Akkerman | 9123ac8 | 1999-11-27 21:58:20 +0000 | [diff] [blame] | 183 | } else { |
| 184 | printstr(tcp, tcp->u_arg[2], tcp->u_arg[3]); |
| 185 | tprintf(", %#lx", tcp->u_arg[4]); |
| 186 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
Wichert Akkerman | 5052482 | 1999-10-10 22:40:07 +0000 | [diff] [blame] | 191 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 192 | sys_create_module(struct tcb *tcp) |
Wichert Akkerman | 5052482 | 1999-10-10 22:40:07 +0000 | [diff] [blame] | 193 | { |
| 194 | if (entering(tcp)) { |
| 195 | printpath(tcp, tcp->u_arg[0]); |
| 196 | tprintf(", %lu", tcp->u_arg[1]); |
| 197 | } |
| 198 | return RVAL_HEX; |
| 199 | } |
| 200 | |
Wichert Akkerman | 2f473da | 1999-11-01 19:53:31 +0000 | [diff] [blame] | 201 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 202 | sys_init_module(struct tcb *tcp) |
Wichert Akkerman | 2f473da | 1999-11-01 19:53:31 +0000 | [diff] [blame] | 203 | { |
| 204 | if (entering(tcp)) { |
Roland McGrath | 7dbfe57 | 2005-08-03 11:27:30 +0000 | [diff] [blame] | 205 | tprintf("%#lx, ", tcp->u_arg[0]); |
| 206 | tprintf("%lu, ", tcp->u_arg[1]); |
| 207 | printstr(tcp, tcp->u_arg[2], -1); |
Wichert Akkerman | 2f473da | 1999-11-01 19:53:31 +0000 | [diff] [blame] | 208 | } |
| 209 | return 0; |
| 210 | } |
Wichert Akkerman | 2e2553a | 1999-05-09 00:29:58 +0000 | [diff] [blame] | 211 | #endif /* LINUX */ |