xref: /titanic_52/usr/src/cmd/mdb/common/kmdb/kmdb_module.c (revision 06e46062ef4f5f4b687cbafb4518fb123fe23920)
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 /*
30  * Routines for manipulating the kmdb-specific aspects of dmods.
31  */
32 
33 #include <sys/param.h>
34 
35 #include <mdb/mdb_target_impl.h>
36 #include <kmdb/kmdb_module.h>
37 #include <mdb/mdb_debug.h>
38 #include <mdb/mdb_err.h>
39 #include <mdb/mdb.h>
40 
41 typedef struct kmod_symarg {
42 	mdb_tgt_sym_f *sym_cb;		/* Caller's callback function */
43 	void *sym_data;			/* Callback function argument */
44 	uint_t sym_type;		/* Symbol type/binding filter */
45 	mdb_syminfo_t sym_info;		/* Symbol id and table id */
46 	const char *sym_obj;		/* Containing object */
47 } kmod_symarg_t;
48 
49 void
50 kmdb_module_path_set(const char **path, size_t pathlen)
51 {
52 	kmdb_wr_path_t *wr;
53 
54 	wr = mdb_zalloc(sizeof (kmdb_wr_path_t), UM_SLEEP);
55 	wr->dpth_node.wn_task = WNTASK_DMOD_PATH_CHANGE;
56 	wr->dpth_path = mdb_path_dup(path, pathlen, &wr->dpth_pathlen);
57 
58 	kmdb_wr_driver_notify(wr);
59 }
60 
61 void
62 kmdb_module_path_ack(kmdb_wr_path_t *dpth)
63 {
64 	if (dpth->dpth_path != NULL)
65 		mdb_path_free(dpth->dpth_path, dpth->dpth_pathlen);
66 	mdb_free(dpth, sizeof (kmdb_wr_path_t));
67 }
68 
69 /*
70  * Given an address, try to match it up with a dmod symbol.
71  */
72 int
73 kmdb_module_lookup_by_addr(uintptr_t addr, uint_t flags, char *buf,
74     size_t nbytes, GElf_Sym *symp, mdb_syminfo_t *sip)
75 {
76 	kmdb_modctl_t *sym_kmc = NULL;
77 	GElf_Sym sym;
78 	uint_t symid;
79 	mdb_var_t *v;
80 	const char *name;
81 
82 	mdb_nv_rewind(&mdb.m_dmodctl);
83 	while ((v = mdb_nv_advance(&mdb.m_dmodctl)) != NULL) {
84 		kmdb_modctl_t *kmc = MDB_NV_COOKIE(v);
85 
86 		if (kmc->kmc_state != KMDB_MC_STATE_LOADED)
87 			continue;
88 
89 		if (mdb_gelf_symtab_lookup_by_addr(kmc->kmc_symtab, addr, flags,
90 		    buf, nbytes, symp, &sip->sym_id) != 0 ||
91 		    symp->st_value == 0)
92 			continue;
93 
94 		if (flags & MDB_TGT_SYM_EXACT) {
95 			sym_kmc = kmc;
96 			goto found;
97 		}
98 
99 		/*
100 		 * If this is the first match we've found, or if this symbol is
101 		 * closer to the specified address than the last one we found,
102 		 * use it.
103 		 */
104 		if (sym_kmc == NULL || mdb_gelf_sym_closer(symp, &sym, addr)) {
105 			sym_kmc = kmc;
106 			sym = *symp;
107 			symid = sip->sym_id;
108 		}
109 	}
110 
111 	if (sym_kmc == NULL)
112 		return (set_errno(EMDB_NOSYMADDR));
113 
114 	*symp = sym;
115 	sip->sym_id = symid;
116 
117 found:
118 	/*
119 	 * Once we've found something, copy the final name into the caller's
120 	 * buffer, prefixed with a marker identifying this as a dmod symbol.
121 	 */
122 	if (buf != NULL) {
123 		name = mdb_gelf_sym_name(sym_kmc->kmc_symtab, symp);
124 
125 		(void) mdb_snprintf(buf, nbytes, "DMOD`%s`%s",
126 		    sym_kmc->kmc_modname, name);
127 	}
128 	sip->sym_table = MDB_TGT_SYMTAB;
129 
130 	return (0);
131 }
132 
133 /*
134  * Locate a given dmod symbol
135  */
136 int
137 kmdb_module_lookup_by_name(const char *obj, const char *name, GElf_Sym *symp,
138     mdb_syminfo_t *sip)
139 {
140 	kmdb_modctl_t *kmc;
141 	mdb_var_t *v;
142 
143 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, obj)) == NULL)
144 		return (set_errno(EMDB_NOSYMADDR));
145 
146 	kmc = MDB_NV_COOKIE(v);
147 
148 	if (kmc->kmc_state != KMDB_MC_STATE_LOADED)
149 		return (set_errno(EMDB_NOSYMADDR));
150 
151 	if (mdb_gelf_symtab_lookup_by_name(kmc->kmc_symtab, name,
152 	    symp, &sip->sym_id) == 0) {
153 		sip->sym_table = MDB_TGT_SYMTAB;
154 		return (0);
155 	}
156 
157 	return (set_errno(EMDB_NOSYM));
158 }
159 
160 static int
161 kmdb_module_symtab_func(void *data, const GElf_Sym *sym, const char *name,
162     uint_t id)
163 {
164 	kmod_symarg_t *arg = data;
165 
166 	if (mdb_tgt_sym_match(sym, arg->sym_type)) {
167 		arg->sym_info.sym_id = id;
168 
169 		return (arg->sym_cb(arg->sym_data, sym, name, &arg->sym_info,
170 		    arg->sym_obj));
171 	}
172 
173 	return (0);
174 }
175 
176 int
177 kmdb_module_symbol_iter(const char *obj, uint_t type, mdb_tgt_sym_f *cb,
178     void *p)
179 {
180 	kmdb_modctl_t *kmc;
181 	kmod_symarg_t arg;
182 	mdb_var_t *v;
183 
184 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, obj)) == NULL)
185 		return (set_errno(EMDB_NOMOD));
186 
187 	kmc = MDB_NV_COOKIE(v);
188 
189 	if (kmc->kmc_state != KMDB_MC_STATE_LOADED)
190 		return (set_errno(EMDB_NOMOD));
191 
192 	arg.sym_cb = cb;
193 	arg.sym_data = p;
194 	arg.sym_type = type;
195 	arg.sym_info.sym_table = kmc->kmc_symtab->gst_tabid;
196 	arg.sym_obj = obj;
197 
198 	mdb_gelf_symtab_iter(kmc->kmc_symtab, kmdb_module_symtab_func, &arg);
199 
200 	return (0);
201 }
202