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