Print this page
6375 Add native name demangling support


   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 2001-2002 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 #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 
  40 #ifdef _LP64
  41 static const char LIB_DEMANGLE[] = "/usr/lib/64/libdemangle.so.1";
  42 #else
  43 static const char LIB_DEMANGLE[] = "/usr/lib/libdemangle.so.1";
  44 #endif
  45 











  46 mdb_demangler_t *
  47 mdb_dem_load(const char *path)
  48 {
  49         mdb_demangler_t *dmp;
  50         void *hdl, *func;
  51 
  52         if (access(path, F_OK) == -1)
  53                 return (NULL);
  54 
  55         if ((hdl = dlmopen(LM_ID_BASE, path, RTLD_LAZY | RTLD_LOCAL)) == NULL) {
  56                 (void) set_errno(EMDB_RTLD);
  57                 return (NULL);
  58         }
  59 
  60         if ((func = dlsym(hdl, "cplus_demangle")) == NULL) {
  61                 (void) dlclose(hdl);
  62                 (void) set_errno(EMDB_NODEM);
  63                 return (NULL);
  64         }
  65 
  66         dmp = mdb_alloc(sizeof (mdb_demangler_t), UM_SLEEP);
  67         (void) strncpy(dmp->dm_pathname, path, MAXPATHLEN);
  68         dmp->dm_pathname[MAXPATHLEN - 1] = '\0';
  69         dmp->dm_handle = hdl;
  70         dmp->dm_convert = (int (*)())func;
  71         dmp->dm_len = MDB_SYM_NAMLEN * 2;
  72         dmp->dm_buf = mdb_alloc(dmp->dm_len, UM_SLEEP);
  73         dmp->dm_flags = MDB_DM_SCOPE;


  74 
  75         return (dmp);
  76 }
  77 
  78 void
  79 mdb_dem_unload(mdb_demangler_t *dmp)
  80 {
  81         (void) dlclose(dmp->dm_handle);
  82         mdb_free(dmp->dm_buf, dmp->dm_len);
  83         mdb_free(dmp, sizeof (mdb_demangler_t));
  84 }
  85 
  86 static const char *
  87 mdb_dem_filter(mdb_demangler_t *dmp, const char *name)
  88 {
  89         static const char s_pref[] = "static ";
  90         static const char c_suff[] = " const";
  91         static const char v_suff[] = " volatile";
  92 
  93         /*
  94          * We process dm_dem, which skips the prefix in dm_buf (if any)
  95          */
  96         size_t len = strlen(dmp->dm_dem);
  97         char *end = dmp->dm_dem + len;
  98         size_t resid;
  99 
 100         /*
 101          * If static, const, and volatile qualifiers should not be displayed,


 185                 (void) strcpy(p, "]");
 186         }
 187 
 188         /*
 189          * We return the whole string
 190          */
 191         return (dmp->dm_buf);
 192 }
 193 
 194 /*
 195  * Take a name: (the foo`bar` is optional)
 196  *      foo`bar`__mangled_
 197  * and put:
 198  *      foo`bar`demangled
 199  * into dmp->dm_buf.  Point dmp->dm_dem to the beginning of the
 200  * demangled section of the result.
 201  */
 202 static int
 203 mdb_dem_process(mdb_demangler_t *dmp, const char *name)
 204 {
 205         char *buf = dmp->dm_buf;
 206         size_t len = dmp->dm_len;
 207 
 208         char *prefix = strrchr(name, '`');
 209         size_t prefixlen;
 210 
 211         if (prefix) {
 212                 prefix++;               /* the ` is part of the prefix */
 213                 prefixlen = prefix - name;

 214 
 215                 if (prefixlen >= len)
 216                         return (DEMANGLE_ESPACE);

 217 
 218                 (void) strncpy(buf, name, prefixlen);


 219 
 220                 /*
 221                  * Fix up the arguments to dmp->dm_convert()
 222                  */
 223                 name += prefixlen;
 224                 buf += prefixlen;
 225                 len -= prefixlen;


 226         }
 227 







 228         /*
 229          * Save the position of the demangled string for mdb_dem_filter()
 230          */
 231         dmp->dm_dem = buf;
 232 
 233         return (dmp->dm_convert(name, buf, len));
 234 }
 235 

 236 const char *
 237 mdb_dem_convert(mdb_demangler_t *dmp, const char *name)
 238 {
 239         int err;


 240 
 241         while ((err = mdb_dem_process(dmp, name)) == DEMANGLE_ESPACE) {
 242                 size_t len = dmp->dm_len * 2;
 243                 char *buf = mdb_alloc(len, UM_NOSLEEP);
 244 
 245                 if (buf == NULL) {
 246                         mdb_warn("failed to allocate memory for demangling");
 247                         return (name); /* just return original name */
 248                 }
 249 
 250                 mdb_free(dmp->dm_buf, dmp->dm_len);
 251                 dmp->dm_buf = buf;
 252                 dmp->dm_len = len;
 253         }
 254 
 255         if (err != 0 || strcmp(dmp->dm_buf, name) == 0)
 256                 return (name); /* return original name if not mangled */
 257 
 258         return (mdb_dem_filter(dmp, name));
 259 }
 260 
 261 /*ARGSUSED*/
 262 int
 263 cmd_demangle(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
 264 {
 265         mdb_demangler_t *dmp = mdb.m_demangler;
 266         const char *path = LIB_DEMANGLE;
 267 
 268         if (argc > 1 || (argc > 0 && argv->a_type != MDB_TYPE_STRING))
 269                 return (DCMD_USAGE);
 270 
 271         if (argc > 0) {
 272                 if (dmp != NULL)
 273                         mdb_dem_unload(mdb.m_demangler);
 274                 path = argv->a_un.a_str;
 275         }
 276 
 277         if (dmp != NULL && argc == 0 && !(mdb.m_flags & MDB_FL_DEMANGLE)) {
 278                 mdb_printf("C++ symbol demangling enabled\n");
 279                 mdb.m_flags |= MDB_FL_DEMANGLE;
 280 
 281         } else if (dmp == NULL || argc > 0) {
 282                 if ((mdb.m_demangler = mdb_dem_load(path)) != NULL) {
 283                         mdb_printf("C++ symbol demangling enabled\n");
 284                         mdb.m_flags |= MDB_FL_DEMANGLE;
 285                 } else {
 286                         mdb_warn("failed to load C++ demangler %s", path);
 287                         mdb.m_flags &= ~MDB_FL_DEMANGLE;
 288                 }
 289 
 290         } else {
 291                 mdb_dem_unload(mdb.m_demangler);
 292                 mdb.m_flags &= ~MDB_FL_DEMANGLE;
 293                 mdb.m_demangler = NULL;
 294                 mdb_printf("C++ symbol demangling disabled\n");
 295         }
 296 
 297         return (DCMD_OK);
 298 }
 299 
 300 /*ARGSUSED*/
 301 int
 302 cmd_demflags(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
 303 {
 304         static const char *const dm_desc[] = {
 305                 "static/const/volatile member func qualifiers displayed",
 306                 "scope resolution specifiers displayed",


 320         }
 321 
 322         if (flags & DCMD_ADDRSPEC)
 323                 dmp->dm_flags = ((uint_t)addr & MDB_DM_ALL);
 324 
 325         for (i = 0; i < sizeof (dm_desc) / sizeof (dm_desc[0]); i++) {
 326                 mdb_printf("0x%x\t%s\t%s\n", 1 << i,
 327                     (dmp->dm_flags & (1 << i)) ? "on" : "off", dm_desc[i]);
 328         }
 329 
 330         return (DCMD_OK);
 331 }
 332 
 333 /*ARGSUSED*/
 334 int
 335 cmd_demstr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
 336 {
 337         if ((flags & DCMD_ADDRSPEC) || argc == 0)
 338                 return (DCMD_USAGE);
 339 
 340         if (mdb.m_demangler == NULL && (mdb.m_demangler =
 341             mdb_dem_load(LIB_DEMANGLE)) == NULL) {
 342                 mdb_warn("failed to load C++ demangler %s", LIB_DEMANGLE);
 343                 return (DCMD_ERR);
 344         }
 345 
 346         for (; argc != 0; argc--, argv++) {
 347                 mdb_printf("%s == %s\n", argv->a_un.a_str,
 348                     mdb_dem_convert(mdb.m_demangler, argv->a_un.a_str));
 349         }
 350 
 351         return (DCMD_OK);
 352 }


   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 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)
  60 {
  61         mdb_demangler_t *dmp;

  62 














  63         dmp = mdb_alloc(sizeof (mdb_demangler_t), UM_SLEEP);
  64         dmp->dm_len = 0;
  65         dmp->dm_buf = NULL;




  66         dmp->dm_flags = MDB_DM_SCOPE;
  67         /* stick with C++ for now to match old behavior */
  68         dmp->dm_lang = SYSDEM_LANG_CPP;
  69 
  70         return (dmp);
  71 }
  72 
  73 void
  74 mdb_dem_unload(mdb_demangler_t *dmp)
  75 {

  76         mdb_free(dmp->dm_buf, dmp->dm_len);
  77         mdb_free(dmp, sizeof (mdb_demangler_t));
  78 }
  79 
  80 static const char *
  81 mdb_dem_filter(mdb_demangler_t *dmp, const char *name)
  82 {
  83         static const char s_pref[] = "static ";
  84         static const char c_suff[] = " const";
  85         static const char v_suff[] = " volatile";
  86 
  87         /*
  88          * We process dm_dem, which skips the prefix in dm_buf (if any)
  89          */
  90         size_t len = strlen(dmp->dm_dem);
  91         char *end = dmp->dm_dem + len;
  92         size_t resid;
  93 
  94         /*
  95          * If static, const, and volatile qualifiers should not be displayed,


 179                 (void) strcpy(p, "]");
 180         }
 181 
 182         /*
 183          * We return the whole string
 184          */
 185         return (dmp->dm_buf);
 186 }
 187 
 188 /*
 189  * Take a name: (the foo`bar` is optional)
 190  *      foo`bar`__mangled_
 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         mdb_free(dmp->dm_buf, dmp->dm_len);
 215         dmp->dm_dem = NULL;
 216         dmp->dm_len = 0;
 217 
 218         reslen = (res != NULL) ? strlen(res) : 0;
 219         reslen += prefixlen;
 220         reslen += 1;
 221 
 222         dmp->dm_buf = mdb_zalloc(reslen, UM_SLEEP);
 223         if (dmp->dm_buf == NULL) {
 224                 mdb_warn("Unable to allocate memory for demangling");
 225                 return (-1);
 226         }
 227 
 228         dmp->dm_len = reslen;
 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 
 254 /*ARGSUSED*/
 255 int
 256 cmd_demangle(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
 257 {
 258         mdb_demangler_t *dmp = mdb.m_demangler;

 259 
 260         if (argc > 0)
 261                 return (DCMD_USAGE);
 262 
 263         if (dmp != NULL && !(mdb.m_flags & MDB_FL_DEMANGLE)) {






 264                 mdb_printf("C++ symbol demangling enabled\n");
 265                 mdb.m_flags |= MDB_FL_DEMANGLE;
 266 
 267         } else if (dmp == NULL) {
 268                 if ((mdb.m_demangler = mdb_dem_load()) != NULL) {
 269                         mdb_printf("C++ symbol demangling enabled\n");
 270                         mdb.m_flags |= MDB_FL_DEMANGLE;
 271                 } else {
 272                         mdb_warn("no memory to load C++ demangler");
 273                         mdb.m_flags &= ~MDB_FL_DEMANGLE;
 274                 }
 275 
 276         } else {
 277                 mdb_dem_unload(mdb.m_demangler);
 278                 mdb.m_flags &= ~MDB_FL_DEMANGLE;
 279                 mdb.m_demangler = NULL;
 280                 mdb_printf("C++ symbol demangling disabled\n");
 281         }
 282 
 283         return (DCMD_OK);
 284 }
 285 
 286 /*ARGSUSED*/
 287 int
 288 cmd_demflags(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
 289 {
 290         static const char *const dm_desc[] = {
 291                 "static/const/volatile member func qualifiers displayed",
 292                 "scope resolution specifiers displayed",


 306         }
 307 
 308         if (flags & DCMD_ADDRSPEC)
 309                 dmp->dm_flags = ((uint_t)addr & MDB_DM_ALL);
 310 
 311         for (i = 0; i < sizeof (dm_desc) / sizeof (dm_desc[0]); i++) {
 312                 mdb_printf("0x%x\t%s\t%s\n", 1 << i,
 313                     (dmp->dm_flags & (1 << i)) ? "on" : "off", dm_desc[i]);
 314         }
 315 
 316         return (DCMD_OK);
 317 }
 318 
 319 /*ARGSUSED*/
 320 int
 321 cmd_demstr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
 322 {
 323         if ((flags & DCMD_ADDRSPEC) || argc == 0)
 324                 return (DCMD_USAGE);
 325 






 326         for (; argc != 0; argc--, argv++) {
 327                 mdb_printf("%s == %s\n", argv->a_un.a_str,
 328                     mdb_dem_convert(mdb.m_demangler, argv->a_un.a_str));
 329         }
 330 
 331         return (DCMD_OK);
 332 }