Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gmondump.c « utils « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3472a147ed32e333517911b8b62368c55125dd07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
    gmondump.c
    Displays summary info about given profile data file(s).

    Written by Mark Geisert <mark@maxrnd.com>.

    This file is part of Cygwin.

    This software is a copyrighted work licensed under the terms of the
    Cygwin license.  Please consult the file "CYGWIN_LICENSE" for details.
*/

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "cygwin/version.h"

typedef unsigned short ushort;
typedef uint16_t u_int16_t; // Non-standard sized type needed by ancient gmon.h
#include "gmon.h"

FILE       *ofile;
const char *pgm = "gmondump";
int         verbose = 0;

void __attribute__ ((__noreturn__))
usage (FILE *where)
{
  fprintf (where, "\
Usage: %s [OPTIONS] FILENAME...\n\
\n\
Display formatted contents of profile data file(s).\n\
Such files usually have names starting with \"gmon.out\".\n\
OPTIONS are:\n\
\n\
  -h, --help             Display usage information and exit\n\
  -v, --verbose          Display more file details (toggle: default false)\n\
  -V, --version          Display version information and exit\n\
\n", pgm);

  exit (where == stderr ? 1 : 0 );
}

void __attribute__ ((__noreturn__))
usage1 (FILE *where)
{
  fprintf (where, "Usage: %s [OPTIONS] FILENAME...\n", pgm);

  exit (where == stderr ? 1 : 0 );
}

void
note (const char *fmt, ...)
{
  va_list args;
  char    buf[BUFSIZ];

  va_start (args, fmt);
  vsprintf (buf, fmt, args);
  va_end (args);

  fputs (buf, ofile);
  fflush (ofile);
}

void
warn (int geterrno, const char *fmt, ...)
{
  va_list args;
  char    buf[BUFSIZ];

  va_start (args, fmt);
  sprintf (buf, "%s: ", pgm);
  vsprintf (strchr (buf, '\0'), fmt, args);
  va_end (args);
  if (geterrno)
    perror (buf);
  else
    {
      fputs (buf, ofile);
      fputs ("\n", ofile);
      fflush (ofile);
    }
}

void __attribute__ ((noreturn))
error (int geterrno, const char *fmt, ...)
{
  va_list args;
  char    buf[BUFSIZ];

  va_start (args, fmt);
  vsprintf (buf, fmt, args);
  va_end (args);
  warn (geterrno, "%s", buf);

  exit (1);
}

void
gmondump1 (char *filename)
{
  int        addrincr;
  ushort    *bucket = NULL;
  int        fd;
  struct gmonhdr hdr;
  int        hitbuckets;
  int        hitcount;
  int        numbuckets;
  int        numrawarcs;
  struct rawarc *rawarc = NULL;
  int        res;
  struct stat stat;

  fd = open (filename, O_RDONLY | O_BINARY);
  if (fd < 0)
    {
      note ("file%s %s couldn't be opened; continuing\n",
            strchr (filename, '*') ? "s" : "", filename);
      return;
    }

  /* Read and sanity-check what should be a gmon header. */
  res = fstat (fd, &stat);
  if (res < 0)
    goto notgmon;
  if (S_IFREG != (stat.st_mode & S_IFMT))
    goto notgmon;
  res = read (fd, &hdr, sizeof (hdr));
  if (res != sizeof (hdr))
    goto notgmon;
  if (hdr.lpc >= hdr.hpc)
    goto notgmon;
  numbuckets = (hdr.ncnt - sizeof (hdr)) / sizeof (short);
  addrincr = (hdr.hpc - hdr.lpc) / numbuckets;
  numrawarcs = 0;
  if (stat.st_size != hdr.ncnt)
    {
      numrawarcs = stat.st_size - hdr.ncnt;
      if (numrawarcs != (int) sizeof (struct rawarc) *
                        (numrawarcs / (int) sizeof (struct rawarc)))
        goto notgmon;
      numrawarcs /= (int) sizeof (struct rawarc);
    }

  /* Looks good, so read and display the profiling info. */
  bucket = (ushort *) calloc (numbuckets, sizeof (ushort));
  res = read (fd, bucket, hdr.ncnt - sizeof (hdr));
  if (res != hdr.ncnt - (int) sizeof (hdr))
    goto notgmon;
  hitcount = hitbuckets = 0;
  for (res = 0; res < numbuckets; ++bucket, ++res)
    if (*bucket)
      {
        ++hitbuckets;
        hitcount += *bucket;
      }
  bucket -= numbuckets;

  note ("file %s, gmon version 0x%x, sample rate %d\n",
        filename, hdr.version, hdr.profrate);
  note ("  address range %p..%p, address increment %d/bucket\n",
        hdr.lpc, hdr.hpc, addrincr);
  note ("  numbuckets %d, hitbuckets %d, hitcount %d, numrawarcs %d\n",
        numbuckets, hitbuckets, hitcount, numrawarcs);

  /* If verbose is set, display contents of buckets and rawarcs arrays. */
  if (verbose)
    {
      if (hitbuckets)
        note ("  bucket data follows...\n");
      else
        note ("  no bucket data present\n");
      char *addr = (char *) hdr.lpc;
      for (res = 0; res < numbuckets; ++bucket, ++res, addr += addrincr)
        if (*bucket)
          note ("    address %p, hitcount %d\n", addr, *bucket);
      bucket -= numbuckets;

      if (numrawarcs)
        {
          rawarc = (struct rawarc *) calloc (numrawarcs,
                                             sizeof (struct rawarc));
          res = read (fd, rawarc, numrawarcs * (int) sizeof (struct rawarc));
          if (res != numrawarcs * (int) sizeof (struct rawarc))
            error (0, "unable to read rawarc data");
          note ("  rawarc data follows...\n");
          for (res = 0; res < numrawarcs; ++rawarc, ++res)
            note ("    from %p, self %p, count %d\n",
                  rawarc->raw_frompc, rawarc->raw_selfpc, rawarc->raw_count);
          rawarc -= numrawarcs;
        }
      else
        note ("  no rawarc data present\n");
    }

  if (0)
    {
notgmon:
      note ("file %s isn't a profile data file; continuing\n", filename);
    }
  if (rawarc)
    free (rawarc);
  if (bucket)
    free (bucket);
  close (fd);
}

struct option longopts[] = {
  {"help",    no_argument, NULL, 'h'},
  {"verbose", no_argument, NULL, 'v'},
  {"version", no_argument, NULL, 'V'},
  {NULL,      0,           NULL, 0  }
};

const char *const opts = "+hvV";

void __attribute__ ((__noreturn__))
print_version ()
{
  char *year_of_build = strrchr (__DATE__, ' ') + 1;
  printf ("gmondump (cygwin) %d.%d.%d\n"
          "Profiler data file viewer\n"
          "Copyright (C) %s%s Cygwin Authors\n"
          "This is free software; see the source for copying conditions.  "
          "There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS "
          "FOR A PARTICULAR PURPOSE.\n",
          CYGWIN_VERSION_DLL_MAJOR / 1000,
          CYGWIN_VERSION_DLL_MAJOR % 1000,
          CYGWIN_VERSION_DLL_MINOR,
          strncmp (year_of_build, "2021", 4) ? "2021 - " : "",
          year_of_build);
  exit (0);
}

int
main(int argc, char **argv)
{
  ofile = stdout;
  int opt;

  while ((opt = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
    switch (opt)
      {
      case 'h':
        /* Print help and exit. */
        usage (ofile);

      case 'v':
        verbose ^= 1;
        break;

      case 'V':
        /* Print version and exit. */
        print_version ();

      default:
        ;
      }

  if (optind >= argc)
    /* Print one-line help and exit. */
    usage1 (ofile);

  for (int i = optind; i < argc; i++)
    {
      gmondump1 (argv[i]);
      if ((i + 1) < argc)
        note ("\n");
    }

  return 0;
}