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 2001-2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 *
26 * Copyright 2017 Jason King
27 */
28
29 #include <mdb/mdb_modapi.h>
30 #include <mdb/mdb_demangle.h>
31 #include <mdb/mdb_err.h>
32 #include <mdb/mdb.h>
33
34 #include <demangle.h>
35 #include <strings.h>
36 #include <unistd.h>
37 #include <dlfcn.h>
38 #include <link.h>
39 #include <sysdemangle.h>
40
41 static void *
42 mdb_dem_alloc(size_t len)
43 {
44 return (mdb_alloc(len, UM_SLEEP));
45 }
46
47 static void
48 mdb_dem_free(void *p, size_t len)
49 {
50 mdb_free(p, len);
51 }
52
53 static sysdem_ops_t mdb_dem_demops = {
54 .alloc = mdb_dem_alloc,
55 .free = mdb_dem_free
56 };
57
58 mdb_demangler_t *
59 mdb_dem_load(void)
191 * and put:
192 * foo`bar`demangled
193 * into dmp->dm_buf. Point dmp->dm_dem to the beginning of the
194 * demangled section of the result.
195 */
196 static int
197 mdb_dem_process(mdb_demangler_t *dmp, const char *name)
198 {
199 char *res = NULL;
200 size_t reslen = 0;
201
202 char *prefix = strrchr(name, '`');
203 size_t prefixlen = 0;
204
205 if (prefix) {
206 prefix++; /* the ` is part of the prefix */
207 prefixlen = prefix - name;
208 }
209
210 res = sysdemangle(name + prefixlen, dmp->dm_lang, &mdb_dem_demops);
211 if (res == NULL && errno != EINVAL)
212 mdb_warn("Error while demangling");
213
214 reslen = (res != NULL) ? strlen(res) : 0;
215 reslen += prefixlen;
216 reslen += 1;
217
218 if (reslen > dmp->dm_len) {
219 mdb_free(dmp->dm_buf, dmp->dm_len);
220
221 dmp->dm_buf = mdb_zalloc(reslen, UM_SLEEP);
222 if (dmp->dm_buf == NULL) {
223 dmp->dm_len = 0;
224 mdb_warn("Unable to allocate memory for demangling");
225 return (-1);
226 }
227 dmp->dm_len = reslen;
228 }
229
230 if (prefixlen > 0)
231 (void) strlcpy(dmp->dm_buf, name, prefixlen);
232 if (res != NULL)
233 (void) strlcat(dmp->dm_buf, res, dmp->dm_len);
234
235 /*
236 * Save the position of the demangled string for mdb_dem_filter()
237 */
238 dmp->dm_dem = dmp->dm_buf + prefixlen;
239
240 return (0);
241 }
242
243 /* used by mdb_io.c:iob_addr2str */
244 const char *
245 mdb_dem_convert(mdb_demangler_t *dmp, const char *name)
246 {
247 if (mdb_dem_process(dmp, name) != 0 ||
248 strcmp(dmp->dm_buf, name) == 0)
249 return (name);
250
251 return (mdb_dem_filter(dmp, name));
252 }
253
|
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 2001-2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 *
26 * Copyright 2018 Jason King
27 */
28
29 #include <mdb/mdb_modapi.h>
30 #include <mdb/mdb_demangle.h>
31 #include <mdb/mdb_err.h>
32 #include <mdb/mdb.h>
33
34 #include <strings.h>
35 #include <unistd.h>
36 #include <dlfcn.h>
37 #include <link.h>
38
39 static void *
40 mdb_dem_alloc(size_t len)
41 {
42 return (mdb_alloc(len, UM_SLEEP));
43 }
44
45 static void
46 mdb_dem_free(void *p, size_t len)
47 {
48 mdb_free(p, len);
49 }
50
51 static sysdem_ops_t mdb_dem_demops = {
52 .alloc = mdb_dem_alloc,
53 .free = mdb_dem_free
54 };
55
56 mdb_demangler_t *
57 mdb_dem_load(void)
189 * and put:
190 * foo`bar`demangled
191 * into dmp->dm_buf. Point dmp->dm_dem to the beginning of the
192 * demangled section of the result.
193 */
194 static int
195 mdb_dem_process(mdb_demangler_t *dmp, const char *name)
196 {
197 char *res = NULL;
198 size_t reslen = 0;
199
200 char *prefix = strrchr(name, '`');
201 size_t prefixlen = 0;
202
203 if (prefix) {
204 prefix++; /* the ` is part of the prefix */
205 prefixlen = prefix - name;
206 }
207
208 res = sysdemangle(name + prefixlen, dmp->dm_lang, &mdb_dem_demops);
209 if (res == NULL) {
210 if (errno != EINVAL)
211 mdb_warn("Error while demangling");
212 return (-1);
213 }
214
215 reslen = (res != NULL) ? strlen(res) : 0;
216 reslen += prefixlen;
217 reslen += 1;
218
219 if (reslen > dmp->dm_len) {
220 mdb_free(dmp->dm_buf, dmp->dm_len);
221
222 dmp->dm_buf = mdb_zalloc(reslen, UM_SLEEP);
223 if (dmp->dm_buf == NULL) {
224 dmp->dm_len = 0;
225 mdb_warn("Unable to allocate memory for demangling");
226 return (-1);
227 }
228 dmp->dm_len = reslen;
229 }
230
231 if (prefixlen > 0)
232 (void) strlcpy(dmp->dm_buf, name, prefixlen);
233
234 if (res != NULL) {
235 (void) strlcat(dmp->dm_buf, res, dmp->dm_len);
236 mdb_dem_free(res, strlen(res) + 1);
237 }
238
239 /*
240 * Save the position of the demangled string for mdb_dem_filter()
241 */
242 dmp->dm_dem = dmp->dm_buf + prefixlen;
243
244 return (0);
245 }
246
247 /* used by mdb_io.c:iob_addr2str */
248 const char *
249 mdb_dem_convert(mdb_demangler_t *dmp, const char *name)
250 {
251 if (mdb_dem_process(dmp, name) != 0 ||
252 strcmp(dmp->dm_buf, name) == 0)
253 return (name);
254
255 return (mdb_dem_filter(dmp, name));
256 }
257
|