xref: /illumos-gate/usr/src/cmd/mdb/common/modules/libumem/misc.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include "misc.h"
29 
30 #define	UMEM_OBJNAME "libumem.so"
31 
32 int umem_debug_level = 0;
33 int umem_is_standalone = 0;
34 
35 /*ARGSUSED*/
36 int
37 umem_debug(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
38 {
39 	umem_debug_level ^= 1;
40 
41 	mdb_printf("umem: debugging is now %s\n",
42 	    umem_debug_level ? "on" : "off");
43 
44 	return (DCMD_OK);
45 }
46 
47 /*
48  * To further confuse the issue, this dmod can run against either
49  * libumem.so.1 *or* the libstandumem.so linked into kmdb(1M).  To figure
50  * out which one we are working against, we look up "umem_alloc" in both
51  * libumem.so and the executable.
52  *
53  * A further wrinkle is that libumem.so may not yet be loaded into the
54  * process' address space.  That can lead to either the lookup failing, or
55  * being unable to read from the data segment.  We treat either case as
56  * an error.
57  */
58 int
59 umem_set_standalone(void)
60 {
61 	GElf_Sym sym;
62 	int ready;
63 
64 	if (mdb_lookup_by_obj(UMEM_OBJNAME, "umem_alloc", &sym) == 0)
65 		umem_is_standalone = 0;
66 	else if (mdb_lookup_by_obj(MDB_OBJ_EXEC, "umem_alloc", &sym) == 0)
67 		umem_is_standalone = 1;
68 	else
69 		return (-1);
70 
71 	/*
72 	 * now that we know where things should be, make sure we can actually
73 	 * read things out.
74 	 */
75 	if (umem_readvar(&ready, "umem_ready") == -1)
76 		return (-1);
77 	return (0);
78 }
79 
80 ssize_t
81 umem_lookup_by_name(const char *name, GElf_Sym *sym)
82 {
83 	return (mdb_lookup_by_obj((umem_is_standalone ? MDB_OBJ_EXEC :
84 	    UMEM_OBJNAME), name, sym));
85 }
86 
87 /* This is like mdb_readvar, only for libumem.so's symbols */
88 ssize_t
89 umem_readvar(void *buf, const char *name)
90 {
91 	GElf_Sym sym;
92 
93 	if (umem_lookup_by_name(name, &sym))
94 		return (-1);
95 
96 	if (mdb_vread(buf, sym.st_size, (uintptr_t)sym.st_value)
97 	    == sym.st_size)
98 		return ((ssize_t)sym.st_size);
99 
100 	return (-1);
101 }
102 
103 int
104 is_umem_sym(const char *sym, const char *prefix)
105 {
106 	char *tick_p = strrchr(sym, '`');
107 
108 	return (strncmp(sym, "libumem", 7) == 0 && tick_p != NULL &&
109 	    strncmp(tick_p + 1, prefix, strlen(prefix)) == 0);
110 }
111