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
5*789d94c2Sjwadams * Common Development and Distribution License (the "License").
6*789d94c2Sjwadams * 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 /*
22*789d94c2Sjwadams * 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 #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include "misc.h"
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #define UMEM_OBJNAME "libumem.so"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate int umem_debug_level = 0;
337c478bd9Sstevel@tonic-gate int umem_is_standalone = 0;
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
367c478bd9Sstevel@tonic-gate int
umem_debug(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)377c478bd9Sstevel@tonic-gate umem_debug(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate umem_debug_level ^= 1;
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate mdb_printf("umem: debugging is now %s\n",
427c478bd9Sstevel@tonic-gate umem_debug_level ? "on" : "off");
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate return (DCMD_OK);
457c478bd9Sstevel@tonic-gate }
467c478bd9Sstevel@tonic-gate
47*789d94c2Sjwadams /*
48*789d94c2Sjwadams * To further confuse the issue, this dmod can run against either
49*789d94c2Sjwadams * libumem.so.1 *or* the libstandumem.so linked into kmdb(1M). To figure
50*789d94c2Sjwadams * out which one we are working against, we look up "umem_alloc" in both
51*789d94c2Sjwadams * libumem.so and the executable.
52*789d94c2Sjwadams *
53*789d94c2Sjwadams * A further wrinkle is that libumem.so may not yet be loaded into the
54*789d94c2Sjwadams * process' address space. That can lead to either the lookup failing, or
55*789d94c2Sjwadams * being unable to read from the data segment. We treat either case as
56*789d94c2Sjwadams * an error.
57*789d94c2Sjwadams */
58*789d94c2Sjwadams int
umem_set_standalone(void)597c478bd9Sstevel@tonic-gate umem_set_standalone(void)
607c478bd9Sstevel@tonic-gate {
61*789d94c2Sjwadams GElf_Sym sym;
62*789d94c2Sjwadams int ready;
63*789d94c2Sjwadams
64*789d94c2Sjwadams if (mdb_lookup_by_obj(UMEM_OBJNAME, "umem_alloc", &sym) == 0)
65*789d94c2Sjwadams umem_is_standalone = 0;
66*789d94c2Sjwadams else if (mdb_lookup_by_obj(MDB_OBJ_EXEC, "umem_alloc", &sym) == 0)
677c478bd9Sstevel@tonic-gate umem_is_standalone = 1;
68*789d94c2Sjwadams else
69*789d94c2Sjwadams return (-1);
70*789d94c2Sjwadams
71*789d94c2Sjwadams /*
72*789d94c2Sjwadams * now that we know where things should be, make sure we can actually
73*789d94c2Sjwadams * read things out.
74*789d94c2Sjwadams */
75*789d94c2Sjwadams if (umem_readvar(&ready, "umem_ready") == -1)
76*789d94c2Sjwadams return (-1);
77*789d94c2Sjwadams return (0);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate ssize_t
umem_lookup_by_name(const char * name,GElf_Sym * sym)817c478bd9Sstevel@tonic-gate umem_lookup_by_name(const char *name, GElf_Sym *sym)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate return (mdb_lookup_by_obj((umem_is_standalone ? MDB_OBJ_EXEC :
847c478bd9Sstevel@tonic-gate UMEM_OBJNAME), name, sym));
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /* This is like mdb_readvar, only for libumem.so's symbols */
887c478bd9Sstevel@tonic-gate ssize_t
umem_readvar(void * buf,const char * name)897c478bd9Sstevel@tonic-gate umem_readvar(void *buf, const char *name)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate GElf_Sym sym;
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate if (umem_lookup_by_name(name, &sym))
947c478bd9Sstevel@tonic-gate return (-1);
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate if (mdb_vread(buf, sym.st_size, (uintptr_t)sym.st_value)
977c478bd9Sstevel@tonic-gate == sym.st_size)
987c478bd9Sstevel@tonic-gate return ((ssize_t)sym.st_size);
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate return (-1);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate int
is_umem_sym(const char * sym,const char * prefix)1047c478bd9Sstevel@tonic-gate is_umem_sym(const char *sym, const char *prefix)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate char *tick_p = strrchr(sym, '`');
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate return (strncmp(sym, "libumem", 7) == 0 && tick_p != NULL &&
1097c478bd9Sstevel@tonic-gate strncmp(tick_p + 1, prefix, strlen(prefix)) == 0);
1107c478bd9Sstevel@tonic-gate }
111