Print this page
Rich's feedback
Address Robert's feedback
6375 Add native name demangling support

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/dis/dis_util.c
          +++ new/usr/src/cmd/dis/dis_util.c
↓ open down ↓ 14 lines elided ↑ open up ↑
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
       25 + *
       26 + * Copyright 2018 Jason King.
  25   27   */
  26   28  
  27      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  28      -
  29   29  #include <dlfcn.h>
  30   30  #include <stdarg.h>
  31   31  #include <stdio.h>
  32   32  #include <stdlib.h>
  33      -#include <demangle.h>
       33 +#include <demangle-sys.h>
  34   34  
  35   35  #include "dis_util.h"
  36   36  
  37   37  int g_error;    /* global process exit status, set when warn() is called */
  38   38  
  39   39  /*
  40   40   * Fatal error.  Print out the error with a leading "dis: ", and then exit the
  41   41   * program.
  42   42   */
  43   43  void
↓ open down ↓ 41 lines elided ↑ open up ↑
  85   85          void *ret;
  86   86  
  87   87          if ((ret = calloc(1, size)) == NULL)
  88   88                  die("Out of memory");
  89   89  
  90   90          return (ret);
  91   91  }
  92   92  
  93   93  
  94   94  /*
  95      - * Generic interface to demangle C++ names.  Calls cplus_demangle to do the
  96      - * necessary translation.  If the translation fails, the argument is returned
  97      - * unchanged.  The memory returned is only valid until the next call to
  98      - * demangle().
  99      - *
 100      - * We dlopen() libdemangle.so rather than linking directly against it in case it
 101      - * is not installed on the system.
       95 + * Since the -C flag explicitly says C++, for now at least, force language to
       96 + * C++
 102   97   */
 103   98  const char *
 104   99  dis_demangle(const char *name)
 105  100  {
 106      -        static char *demangled_name;
 107      -        static int (*demangle_func)() = NULL;
 108      -        static int size = BUFSIZE;
 109      -        static int first_flag = 0;
 110      -        int ret;
      101 +        static char *demangled_name = NULL;
 111  102  
 112  103          /*
 113      -         * If this is the first call, allocate storage
 114      -         * for the buffer.
      104 +         * Since demangled_name is static, it may be preserved across
      105 +         * invocations.  As such, make sure any memory that might be present
      106 +         * from previous invocations is freed.
 115  107           */
 116      -        if (first_flag == 0) {
 117      -                void *demangle_hand;
 118      -
 119      -                demangle_hand = dlopen("libdemangle.so.1", RTLD_LAZY);
 120      -                if (demangle_hand != NULL)
 121      -                        demangle_func = (int (*)(int))dlsym(
 122      -                                demangle_hand, "cplus_demangle");
 123      -
 124      -                demangled_name = safe_malloc(size);
 125      -                first_flag = 1;
 126      -        }
 127      -
 128      -        /*
 129      -         * If libdemangle is not present, pass through unchanged.
 130      -         */
 131      -        if (demangle_func == NULL)
 132      -                return (name);
 133      -
 134      -        /*
 135      -         * The function returns -1 when the buffer size is not sufficient.
 136      -         */
 137      -        while ((ret = (*demangle_func)(name, demangled_name, size)) == -1) {
 138      -                free(demangled_name);
 139      -                size = size + BUFSIZE;
 140      -                demangled_name = safe_malloc(size);
 141      -        }
 142      -
 143      -        if (ret != 0)
 144      -                return (name);
 145      -
 146      -        return (demangled_name);
      108 +        free(demangled_name);
      109 +        demangled_name = sysdemangle(name, SYSDEM_LANG_CPP, NULL);
      110 +        return ((demangled_name != NULL) ? demangled_name : name);
 147  111  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX