xref: /titanic_52/usr/src/cmd/mdb/common/modules/libumem/misc.c (revision fb3fb4f3d76d55b64440afd0af72775dfad3bd1d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "misc.h"
30 
31 #define	UMEM_OBJNAME "libumem.so"
32 
33 int umem_debug_level = 0;
34 int umem_is_standalone = 0;
35 
36 /*ARGSUSED*/
37 int
38 umem_debug(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
39 {
40 	umem_debug_level ^= 1;
41 
42 	mdb_printf("umem: debugging is now %s\n",
43 	    umem_debug_level ? "on" : "off");
44 
45 	return (DCMD_OK);
46 }
47 
48 void
49 umem_set_standalone(void)
50 {
51 	umem_is_standalone = 1;
52 }
53 
54 ssize_t
55 umem_lookup_by_name(const char *name, GElf_Sym *sym)
56 {
57 	return (mdb_lookup_by_obj((umem_is_standalone ? MDB_OBJ_EXEC :
58 	    UMEM_OBJNAME), name, sym));
59 }
60 
61 /* This is like mdb_readvar, only for libumem.so's symbols */
62 ssize_t
63 umem_readvar(void *buf, const char *name)
64 {
65 	GElf_Sym sym;
66 
67 	if (umem_lookup_by_name(name, &sym))
68 		return (-1);
69 
70 	if (mdb_vread(buf, sym.st_size, (uintptr_t)sym.st_value)
71 	    == sym.st_size)
72 		return ((ssize_t)sym.st_size);
73 
74 	return (-1);
75 }
76 
77 int
78 is_umem_sym(const char *sym, const char *prefix)
79 {
80 	char *tick_p = strrchr(sym, '`');
81 
82 	return (strncmp(sym, "libumem", 7) == 0 && tick_p != NULL &&
83 		strncmp(tick_p + 1, prefix, strlen(prefix)) == 0);
84 }
85