xref: /titanic_50/usr/src/cmd/mdb/common/modules/libumem/misc.c (revision 6a37fc30652374065d6e4ab52366c499e5a34b66)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include "misc.h"
27 
28 #define	UMEM_OBJNAME "libumem.so.1"
29 
30 int umem_debug_level = 0;
31 int umem_is_standalone = 0;
32 
33 /*ARGSUSED*/
34 int
35 umem_debug(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
36 {
37 	umem_debug_level ^= 1;
38 
39 	mdb_printf("umem: debugging is now %s\n",
40 	    umem_debug_level ? "on" : "off");
41 
42 	return (DCMD_OK);
43 }
44 
45 /*
46  * To further confuse the issue, this dmod can run against either
47  * libumem.so.1 *or* the libstandumem.so linked into kmdb(1M).  To figure
48  * out which one we are working against, we look up "umem_alloc" in both
49  * libumem.so and the executable.
50  *
51  * A further wrinkle is that libumem.so may not yet be loaded into the
52  * process' address space.  That can lead to either the lookup failing, or
53  * being unable to read from the data segment.  We treat either case as
54  * an error.
55  */
56 int
57 umem_set_standalone(void)
58 {
59 	GElf_Sym sym;
60 	int ready;
61 
62 	if (mdb_lookup_by_obj(UMEM_OBJNAME, "umem_alloc", &sym) == 0)
63 		umem_is_standalone = 0;
64 	else if (mdb_lookup_by_obj(MDB_OBJ_EXEC, "umem_alloc", &sym) == 0)
65 		umem_is_standalone = 1;
66 	else
67 		return (-1);
68 
69 	/*
70 	 * now that we know where things should be, make sure we can actually
71 	 * read things out.
72 	 */
73 	if (umem_readvar(&ready, "umem_ready") == -1)
74 		return (-1);
75 	return (0);
76 }
77 
78 ssize_t
79 umem_lookup_by_name(const char *name, GElf_Sym *sym)
80 {
81 	return (mdb_lookup_by_obj((umem_is_standalone ? MDB_OBJ_EXEC :
82 	    UMEM_OBJNAME), name, sym));
83 }
84 
85 /* This is like mdb_readvar, only for libumem.so's symbols */
86 ssize_t
87 umem_readvar(void *buf, const char *name)
88 {
89 	GElf_Sym sym;
90 
91 	if (umem_lookup_by_name(name, &sym))
92 		return (-1);
93 
94 	if (mdb_vread(buf, sym.st_size, (uintptr_t)sym.st_value)
95 	    == sym.st_size)
96 		return ((ssize_t)sym.st_size);
97 
98 	return (-1);
99 }
100 
101 int
102 is_umem_sym(const char *sym, const char *prefix)
103 {
104 	char *tick_p = strrchr(sym, '`');
105 
106 	return (strncmp(sym, "libumem", 7) == 0 && tick_p != NULL &&
107 	    strncmp(tick_p + 1, prefix, strlen(prefix)) == 0);
108 }
109