xref: /titanic_51/usr/src/cmd/mdb/common/modules/libumem/misc.c (revision 422418808a6580456c11a891d69016d29dae1440)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5789d94c2Sjwadams  * Common Development and Distribution License (the "License").
6789d94c2Sjwadams  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22789d94c2Sjwadams  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include "misc.h"
277c478bd9Sstevel@tonic-gate 
28*42241880SRobert Mustacchi #define	UMEM_OBJNAME "libumem.so.1"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate int umem_debug_level = 0;
317c478bd9Sstevel@tonic-gate int umem_is_standalone = 0;
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
347c478bd9Sstevel@tonic-gate int
357c478bd9Sstevel@tonic-gate umem_debug(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
367c478bd9Sstevel@tonic-gate {
377c478bd9Sstevel@tonic-gate 	umem_debug_level ^= 1;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 	mdb_printf("umem: debugging is now %s\n",
407c478bd9Sstevel@tonic-gate 	    umem_debug_level ? "on" : "off");
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
437c478bd9Sstevel@tonic-gate }
447c478bd9Sstevel@tonic-gate 
45789d94c2Sjwadams /*
46789d94c2Sjwadams  * To further confuse the issue, this dmod can run against either
47789d94c2Sjwadams  * libumem.so.1 *or* the libstandumem.so linked into kmdb(1M).  To figure
48789d94c2Sjwadams  * out which one we are working against, we look up "umem_alloc" in both
49789d94c2Sjwadams  * libumem.so and the executable.
50789d94c2Sjwadams  *
51789d94c2Sjwadams  * A further wrinkle is that libumem.so may not yet be loaded into the
52789d94c2Sjwadams  * process' address space.  That can lead to either the lookup failing, or
53789d94c2Sjwadams  * being unable to read from the data segment.  We treat either case as
54789d94c2Sjwadams  * an error.
55789d94c2Sjwadams  */
56789d94c2Sjwadams int
577c478bd9Sstevel@tonic-gate umem_set_standalone(void)
587c478bd9Sstevel@tonic-gate {
59789d94c2Sjwadams 	GElf_Sym sym;
60789d94c2Sjwadams 	int ready;
61789d94c2Sjwadams 
62789d94c2Sjwadams 	if (mdb_lookup_by_obj(UMEM_OBJNAME, "umem_alloc", &sym) == 0)
63789d94c2Sjwadams 		umem_is_standalone = 0;
64789d94c2Sjwadams 	else if (mdb_lookup_by_obj(MDB_OBJ_EXEC, "umem_alloc", &sym) == 0)
657c478bd9Sstevel@tonic-gate 		umem_is_standalone = 1;
66789d94c2Sjwadams 	else
67789d94c2Sjwadams 		return (-1);
68789d94c2Sjwadams 
69789d94c2Sjwadams 	/*
70789d94c2Sjwadams 	 * now that we know where things should be, make sure we can actually
71789d94c2Sjwadams 	 * read things out.
72789d94c2Sjwadams 	 */
73789d94c2Sjwadams 	if (umem_readvar(&ready, "umem_ready") == -1)
74789d94c2Sjwadams 		return (-1);
75789d94c2Sjwadams 	return (0);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate ssize_t
797c478bd9Sstevel@tonic-gate umem_lookup_by_name(const char *name, GElf_Sym *sym)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	return (mdb_lookup_by_obj((umem_is_standalone ? MDB_OBJ_EXEC :
827c478bd9Sstevel@tonic-gate 	    UMEM_OBJNAME), name, sym));
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /* This is like mdb_readvar, only for libumem.so's symbols */
867c478bd9Sstevel@tonic-gate ssize_t
877c478bd9Sstevel@tonic-gate umem_readvar(void *buf, const char *name)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if (umem_lookup_by_name(name, &sym))
927c478bd9Sstevel@tonic-gate 		return (-1);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (mdb_vread(buf, sym.st_size, (uintptr_t)sym.st_value)
957c478bd9Sstevel@tonic-gate 	    == sym.st_size)
967c478bd9Sstevel@tonic-gate 		return ((ssize_t)sym.st_size);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	return (-1);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate int
1027c478bd9Sstevel@tonic-gate is_umem_sym(const char *sym, const char *prefix)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	char *tick_p = strrchr(sym, '`');
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	return (strncmp(sym, "libumem", 7) == 0 && tick_p != NULL &&
1077c478bd9Sstevel@tonic-gate 	    strncmp(tick_p + 1, prefix, strlen(prefix)) == 0);
1087c478bd9Sstevel@tonic-gate }
109