Print this page
gprof shouldn't assume names are less than 500 bytes

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/sgs/gprof/common/printgprof.c
          +++ new/usr/src/cmd/sgs/gprof/common/printgprof.c
↓ open down ↓ 16 lines elided ↑ open up ↑
  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 2008 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
  25   25   *
  26   26   * Copyright 2018 Jason King
       27 + * Copyright 2018, Joyent, Inc.
  27   28   */
  28   29  
  29   30  #include <ctype.h>
  30   31  #include <string.h>
  31   32  #include <sys/param.h>
  32   33  #include <stdlib.h>
  33   34  #include "conv.h"
  34   35  #include "gprof.h"
  35   36  
  36   37  void print_demangled_name(int, nltype *);
  37      -void striped_name(char *, nltype **);
       38 +static void stripped_name(char **, size_t *, nltype **);
  38   39  
  39   40  extern long hz;
  40   41  
  41   42  /*
  42   43   * Symbols that must never be printed, no matter what.
  43   44   */
  44   45  char *splsym[] = {
  45   46          PRF_ETEXT,
  46   47          PRF_EXTSYM,
  47   48          PRF_MEMTERM,
↓ open down ↓ 806 lines elided ↑ open up ↑
 854  855                  perror(blurbname);
 855  856                  return;
 856  857          }
 857  858  
 858  859          while ((input = getc(blurbfile)) != EOF)
 859  860                  (void) putchar(input);
 860  861  
 861  862          (void) fclose(blurbfile);
 862  863  }
 863  864  
 864      -char *s1, *s2;
 865      -
 866  865  static int
 867  866  namecmp(const void *arg1, const void *arg2)
 868  867  {
 869  868          nltype **npp1 = (nltype **)arg1;
 870  869          nltype **npp2 = (nltype **)arg2;
 871  870  
 872  871          if (!Cflag)
 873  872                  return (strcmp((*npp1)->name, (*npp2)->name));
 874  873          else {
 875      -                striped_name(s1, npp1);
 876      -                striped_name(s2, npp2);
      874 +                static char *s1 = NULL, *s2 = NULL;
      875 +                static size_t s1len = 0, s2len = 0;
      876 +
      877 +                stripped_name(&s1, &s1len, npp1);
      878 +                stripped_name(&s2, &s2len, npp2);
 877  879                  return (strcmp(s1, s2));
 878  880          }
 879  881  }
 880  882  
 881      -void
 882      -striped_name(char *s, nltype **npp)
      883 +#define NAME_CHUNK 512
      884 +#define ROUNDLEN(x) (((x) + NAME_CHUNK - 1) / NAME_CHUNK * NAME_CHUNK)
      885 +static void
      886 +adjust_size(char **pp, size_t *lenp, const char *name)
 883  887  {
      888 +        void *newp;
      889 +        size_t nlen = strlen(name);
      890 +        size_t buflen;
      891 +
      892 +        if (*lenp > nlen) {
      893 +                (void) memset(*pp, '\0', *lenp);
      894 +                return;
      895 +        }
      896 +
      897 +        buflen = ROUNDLEN(nlen + 1);
      898 +        if ((newp = realloc(*pp, buflen)) == NULL) {
      899 +                (void) fprintf(stderr,
      900 +                    "gprof: out of memory comparing names\n");
      901 +                exit(EXIT_FAILURE);
      902 +        }
      903 +        (void) memset(newp, '\0', buflen);
      904 +
      905 +        *lenp = buflen;
      906 +        *pp = newp;
      907 +}
      908 +
      909 +static void
      910 +stripped_name(char **sp, size_t *slenp, nltype **npp)
      911 +{
 884  912          const char *name, *d;
 885  913          char *c;
 886  914  
 887      -        c = (char *)s;
 888  915          name = d = demangled_name(*npp);
      916 +        adjust_size(sp, slenp, name);
      917 +        c = *sp;
 889  918  
 890  919          while ((*d != '(') && (*d != '\0')) {
 891  920                  if (*d != ':')
 892  921                          *c++ = *d++;
 893  922                  else
 894  923                          d++;
 895  924          }
 896  925          *c = '\0';
 897  926  
 898  927          if ((*npp)->name != name)
↓ open down ↓ 68 lines elided ↑ open up ↑
 967  996                           * Do not print certain special symbols, like
 968  997                           * PRF_EXTSYM, etc. even if zflag was on.
 969  998                           */
 970  999                          if (is_special_sym(&(mi->nl[index])))
 971 1000                                  continue;
 972 1001  
 973 1002                          namesortnlp[nnames++] = &(mi->nl[index]);
 974 1003                  }
 975 1004          }
 976 1005  
 977      -        if (Cflag) {
 978      -                s1 = malloc(500 * sizeof (char));
 979      -                s2 = malloc(500 * sizeof (char));
 980      -        }
 981      -
 982 1006          qsort(namesortnlp, nnames, sizeof (nltype *), namecmp);
 983 1007  
 984 1008          for (index = 1, todo = nnames; index <= ncycle; index++)
 985 1009                  namesortnlp[todo++] = &cyclenl[index];
 986 1010  
 987 1011          (void) printf("\f\nIndex by function name\n\n");
 988 1012  
 989 1013          if (!Cflag)
 990 1014                  index = (todo + 2) / 3;
 991 1015          else
↓ open down ↓ 72 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX