xref: /onnv-gate/usr/src/cmd/mdb/common/modules/libumem/misc.c (revision 1528:30ad6286c4a5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1528Sjwadams  * Common Development and Distribution License (the "License").
6*1528Sjwadams  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1528Sjwadams  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include "misc.h"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #define	UMEM_OBJNAME "libumem.so"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate int umem_debug_level = 0;
330Sstevel@tonic-gate int umem_is_standalone = 0;
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*ARGSUSED*/
360Sstevel@tonic-gate int
umem_debug(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)370Sstevel@tonic-gate umem_debug(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
380Sstevel@tonic-gate {
390Sstevel@tonic-gate 	umem_debug_level ^= 1;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 	mdb_printf("umem: debugging is now %s\n",
420Sstevel@tonic-gate 	    umem_debug_level ? "on" : "off");
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 	return (DCMD_OK);
450Sstevel@tonic-gate }
460Sstevel@tonic-gate 
47*1528Sjwadams /*
48*1528Sjwadams  * To further confuse the issue, this dmod can run against either
49*1528Sjwadams  * libumem.so.1 *or* the libstandumem.so linked into kmdb(1M).  To figure
50*1528Sjwadams  * out which one we are working against, we look up "umem_alloc" in both
51*1528Sjwadams  * libumem.so and the executable.
52*1528Sjwadams  *
53*1528Sjwadams  * A further wrinkle is that libumem.so may not yet be loaded into the
54*1528Sjwadams  * process' address space.  That can lead to either the lookup failing, or
55*1528Sjwadams  * being unable to read from the data segment.  We treat either case as
56*1528Sjwadams  * an error.
57*1528Sjwadams  */
58*1528Sjwadams int
umem_set_standalone(void)590Sstevel@tonic-gate umem_set_standalone(void)
600Sstevel@tonic-gate {
61*1528Sjwadams 	GElf_Sym sym;
62*1528Sjwadams 	int ready;
63*1528Sjwadams 
64*1528Sjwadams 	if (mdb_lookup_by_obj(UMEM_OBJNAME, "umem_alloc", &sym) == 0)
65*1528Sjwadams 		umem_is_standalone = 0;
66*1528Sjwadams 	else if (mdb_lookup_by_obj(MDB_OBJ_EXEC, "umem_alloc", &sym) == 0)
67*1528Sjwadams 		umem_is_standalone = 1;
68*1528Sjwadams 	else
69*1528Sjwadams 		return (-1);
70*1528Sjwadams 
71*1528Sjwadams 	/*
72*1528Sjwadams 	 * now that we know where things should be, make sure we can actually
73*1528Sjwadams 	 * read things out.
74*1528Sjwadams 	 */
75*1528Sjwadams 	if (umem_readvar(&ready, "umem_ready") == -1)
76*1528Sjwadams 		return (-1);
77*1528Sjwadams 	return (0);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate ssize_t
umem_lookup_by_name(const char * name,GElf_Sym * sym)810Sstevel@tonic-gate umem_lookup_by_name(const char *name, GElf_Sym *sym)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	return (mdb_lookup_by_obj((umem_is_standalone ? MDB_OBJ_EXEC :
840Sstevel@tonic-gate 	    UMEM_OBJNAME), name, sym));
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /* This is like mdb_readvar, only for libumem.so's symbols */
880Sstevel@tonic-gate ssize_t
umem_readvar(void * buf,const char * name)890Sstevel@tonic-gate umem_readvar(void *buf, const char *name)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	GElf_Sym sym;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if (umem_lookup_by_name(name, &sym))
940Sstevel@tonic-gate 		return (-1);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (mdb_vread(buf, sym.st_size, (uintptr_t)sym.st_value)
970Sstevel@tonic-gate 	    == sym.st_size)
980Sstevel@tonic-gate 		return ((ssize_t)sym.st_size);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	return (-1);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate int
is_umem_sym(const char * sym,const char * prefix)1040Sstevel@tonic-gate is_umem_sym(const char *sym, const char *prefix)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	char *tick_p = strrchr(sym, '`');
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	return (strncmp(sym, "libumem", 7) == 0 && tick_p != NULL &&
109*1528Sjwadams 	    strncmp(tick_p + 1, prefix, strlen(prefix)) == 0);
1100Sstevel@tonic-gate }
111