Merge branch 'vendor/FILE'
[dragonfly.git] / usr.sbin / kgmon / kgmon.c
1 /*
2  * Copyright (c) 1983, 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1983, 1992, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)kgmon.c  8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.sbin/kgmon/kgmon.c,v 1.9 1999/08/28 01:16:42 peter Exp $
36  * $DragonFly: src/usr.sbin/kgmon/kgmon.c,v 1.7 2008/04/20 13:44:26 swildner Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/file.h>
41 #include <sys/time.h>
42 #include <sys/sysctl.h>
43 #include <sys/gmon.h>
44 #include <ctype.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <kvm.h>
48 #include <limits.h>
49 #include <nlist.h>
50 #include <paths.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 struct nlist nl[] = {
57 #define N_GMONPARAM     0
58         { "__gmonparam" },
59         { NULL },
60 };
61
62 struct kvmvars {
63         kvm_t   *kd;
64         struct gmonparam gpm;
65 };
66
67 int     Bflag, bflag, hflag, kflag, rflag, pflag;
68 int     debug = 0;
69 int     getprof(struct kvmvars *);
70 void    kern_readonly(int);
71 int     openfiles(char *, char *, struct kvmvars *);
72 void    setprof(struct kvmvars *kvp, int state);
73 void    dumpstate(struct kvmvars *kvp);
74 void    reset(struct kvmvars *kvp);
75 static void usage(void);
76
77 int
78 main(int argc, char **argv)
79 {
80         int ch, mode, disp, accessmode;
81         struct kvmvars kvmvars;
82         char *system, *kmemf;
83
84         seteuid(getuid());
85         kmemf = NULL;
86         system = NULL;
87         while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) {
88                 switch((char)ch) {
89
90                 case 'M':
91                         kmemf = optarg;
92                         kflag = 1;
93                         break;
94
95                 case 'N':
96                         system = optarg;
97                         break;
98
99                 case 'B':
100                         Bflag = 1;
101                         break;
102
103                 case 'b':
104                         bflag = 1;
105                         break;
106
107                 case 'h':
108                         hflag = 1;
109                         break;
110
111                 case 'p':
112                         pflag = 1;
113                         break;
114
115                 case 'r':
116                         rflag = 1;
117                         break;
118
119                 default:
120                         usage();
121                 }
122         }
123         argc -= optind;
124         argv += optind;
125
126 #define BACKWARD_COMPATIBILITY
127 #ifdef  BACKWARD_COMPATIBILITY
128         if (*argv) {
129                 system = *argv;
130                 if (*++argv) {
131                         kmemf = *argv;
132                         ++kflag;
133                 }
134         }
135 #endif
136         if (system == NULL)
137                 system = (char *)getbootfile();
138         accessmode = openfiles(system, kmemf, &kvmvars);
139         mode = getprof(&kvmvars);
140         if (hflag)
141                 disp = GMON_PROF_OFF;
142         else if (Bflag)
143                 disp = GMON_PROF_HIRES;
144         else if (bflag)
145                 disp = GMON_PROF_ON;
146         else
147                 disp = mode;
148         if (pflag)
149                 dumpstate(&kvmvars);
150         if (rflag)
151                 reset(&kvmvars);
152         if (accessmode == O_RDWR)
153                 setprof(&kvmvars, disp);
154         fprintf(stdout, "kgmon: kernel profiling is %s.\n",
155                       disp == GMON_PROF_OFF ? "off" :
156                       disp == GMON_PROF_HIRES ? "running (high resolution)" :
157                       disp == GMON_PROF_ON ? "running" :
158                       disp == GMON_PROF_BUSY ? "busy" :
159                       disp == GMON_PROF_ERROR ? "off (error)" :
160                       "in an unknown state");
161         return (0);
162 }
163
164 static void
165 usage(void)
166 {
167         fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
168         exit(1);
169 }
170
171 /*
172  * Check that profiling is enabled and open any ncessary files.
173  */
174 int
175 openfiles(char *system, char *kmemf, struct kvmvars *kvp)
176 {
177         int mib[3], state, size, openmode;
178         char errbuf[_POSIX2_LINE_MAX];
179
180         if (!kflag) {
181                 mib[0] = CTL_KERN;
182                 mib[1] = KERN_PROF;
183                 mib[2] = GPROF_STATE;
184                 size = sizeof state;
185                 if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
186                         errx(20, "profiling not defined in kernel");
187                 if (!(Bflag || bflag || hflag || rflag ||
188                     (pflag &&
189                      (state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
190                         return (O_RDONLY);
191                 seteuid(0);
192                 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
193                         return (O_RDWR);
194                 seteuid(getuid());
195                 kern_readonly(state);
196                 return (O_RDONLY);
197         }
198         openmode = (Bflag || bflag || hflag || pflag || rflag)
199                    ? O_RDWR : O_RDONLY;
200         kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf);
201         if (kvp->kd == NULL) {
202                 if (openmode == O_RDWR) {
203                         openmode = O_RDONLY;
204                         kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
205                             errbuf);
206                 }
207                 if (kvp->kd == NULL)
208                         errx(2, "kvm_openfiles: %s", errbuf);
209                 kern_readonly(GMON_PROF_ON);
210         }
211         if (kvm_nlist(kvp->kd, nl) < 0)
212                 errx(3, "%s: no namelist", system);
213         if (!nl[N_GMONPARAM].n_value)
214                 errx(20, "profiling not defined in kernel");
215         return (openmode);
216 }
217
218 /*
219  * Suppress options that require a writable kernel.
220  */
221 void
222 kern_readonly(int mode)
223 {
224         fprintf(stderr, "kgmon: kernel read-only: ");
225         if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON))
226                 fprintf(stderr, "data may be inconsistent\n");
227         if (rflag)
228                 fprintf(stderr, "-r suppressed\n");
229         if (Bflag)
230                 fprintf(stderr, "-B suppressed\n");
231         if (bflag)
232                 fprintf(stderr, "-b suppressed\n");
233         if (hflag)
234                 fprintf(stderr, "-h suppressed\n");
235         rflag = Bflag = bflag = hflag = 0;
236 }
237
238 /*
239  * Get the state of kernel profiling.
240  */
241 int
242 getprof(struct kvmvars *kvp)
243 {
244         int mib[3], size;
245
246         if (kflag) {
247                 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
248                     sizeof kvp->gpm);
249         } else {
250                 mib[0] = CTL_KERN;
251                 mib[1] = KERN_PROF;
252                 mib[2] = GPROF_GMONPARAM;
253                 size = sizeof kvp->gpm;
254                 if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
255                         size = 0;
256         }
257         if (size != sizeof kvp->gpm)
258                 errx(4, "cannot get gmonparam: %s",
259                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
260         return (kvp->gpm.state);
261 }
262
263 /*
264  * Enable or disable kernel profiling according to the state variable.
265  */
266 void
267 setprof(struct kvmvars *kvp, int state)
268 {
269         struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
270         int mib[3], sz, oldstate;
271
272         sz = sizeof(state);
273         if (!kflag) {
274                 mib[0] = CTL_KERN;
275                 mib[1] = KERN_PROF;
276                 mib[2] = GPROF_STATE;
277                 if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0)
278                         goto bad;
279                 if (oldstate == state)
280                         return;
281                 seteuid(0);
282                 if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) {
283                         seteuid(getuid());
284                         return;
285                 }
286                 seteuid(getuid());
287         } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz)
288             == sz)
289                 return;
290 bad:
291         warnx("warning: cannot turn profiling %s",
292             state == GMON_PROF_OFF ? "off" : "on");
293 }
294
295 /*
296  * Build the gmon.out file.
297  */
298 void
299 dumpstate(struct kvmvars *kvp)
300 {
301         FILE *fp;
302         struct rawarc rawarc;
303         struct tostruct *tos;
304         u_long frompc;
305         u_short *froms, *tickbuf;
306         int mib[3], i;
307         struct gmonhdr h;
308         int fromindex, endfrom, toindex;
309
310         setprof(kvp, GMON_PROF_OFF);
311         fp = fopen("gmon.out", "w");
312         if (fp == 0) {
313                 warn("gmon.out");
314                 return;
315         }
316
317         /*
318          * Build the gmon header and write it to a file.
319          */
320         bzero(&h, sizeof(h));
321         h.lpc = kvp->gpm.lowpc;
322         h.hpc = kvp->gpm.highpc;
323         h.ncnt = kvp->gpm.kcountsize + sizeof(h);
324         h.version = GMONVERSION;
325         h.profrate = kvp->gpm.profrate;
326         fwrite((char *)&h, sizeof(h), 1, fp);
327
328         /*
329          * Write out the tick buffer.
330          */
331         mib[0] = CTL_KERN;
332         mib[1] = KERN_PROF;
333         if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL)
334                 errx(5, "cannot allocate kcount space");
335         if (kflag) {
336                 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
337                     kvp->gpm.kcountsize);
338         } else {
339                 mib[2] = GPROF_COUNT;
340                 i = kvp->gpm.kcountsize;
341                 if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
342                         i = 0;
343         }
344         if (i != kvp->gpm.kcountsize)
345                 errx(6, "read ticks: read %u, got %d: %s",
346                     kvp->gpm.kcountsize, i,
347                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
348         if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
349                 err(7, "writing tocks to gmon.out");
350         free(tickbuf);
351
352         /*
353          * Write out the arc info.
354          */
355         if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL)
356                 errx(8, "cannot allocate froms space");
357         if (kflag) {
358                 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
359                     kvp->gpm.fromssize);
360         } else {
361                 mib[2] = GPROF_FROMS;
362                 i = kvp->gpm.fromssize;
363                 if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
364                         i = 0;
365         }
366         if (i != kvp->gpm.fromssize)
367                 errx(9, "read froms: read %u, got %d: %s",
368                     kvp->gpm.fromssize, i,
369                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
370         if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL)
371                 errx(10, "cannot allocate tos space");
372         if (kflag) {
373                 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
374                     kvp->gpm.tossize);
375         } else {
376                 mib[2] = GPROF_TOS;
377                 i = kvp->gpm.tossize;
378                 if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
379                         i = 0;
380         }
381         if (i != kvp->gpm.tossize)
382                 errx(11, "read tos: read %u, got %d: %s",
383                     kvp->gpm.tossize, i,
384                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
385         if (debug)
386                 warnx("lowpc 0x%x, textsize 0x%x",
387                               kvp->gpm.lowpc, kvp->gpm.textsize);
388         endfrom = kvp->gpm.fromssize / sizeof(*froms);
389         for (fromindex = 0; fromindex < endfrom; ++fromindex) {
390                 if (froms[fromindex] == 0)
391                         continue;
392                 frompc = (u_long)kvp->gpm.lowpc +
393                     (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
394                 for (toindex = froms[fromindex]; toindex != 0;
395                    toindex = tos[toindex].link) {
396                         if (debug)
397                             warnx("[mcleanup] frompc 0x%x selfpc 0x%x count %d",
398                             frompc, tos[toindex].selfpc,
399                             tos[toindex].count);
400                         rawarc.raw_frompc = frompc;
401                         rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
402                         rawarc.raw_count = tos[toindex].count;
403                         fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
404                 }
405         }
406         fclose(fp);
407 }
408
409 /*
410  * Reset the kernel profiling date structures.
411  */
412 void
413 reset(struct kvmvars *kvp)
414 {
415         char *zbuf;
416         u_long biggest;
417         int mib[3];
418
419         setprof(kvp, GMON_PROF_OFF);
420
421         biggest = kvp->gpm.kcountsize;
422         if (kvp->gpm.fromssize > biggest)
423                 biggest = kvp->gpm.fromssize;
424         if (kvp->gpm.tossize > biggest)
425                 biggest = kvp->gpm.tossize;
426         if ((zbuf = (char *)malloc(biggest)) == NULL)
427                 errx(12, "cannot allocate zbuf space");
428         bzero(zbuf, biggest);
429         if (kflag) {
430                 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
431                     kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
432                         errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
433                 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
434                     kvp->gpm.fromssize) != kvp->gpm.fromssize)
435                         errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
436                 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
437                     kvp->gpm.tossize) != kvp->gpm.tossize)
438                         errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
439                 return;
440         }
441         seteuid(0);
442         mib[0] = CTL_KERN;
443         mib[1] = KERN_PROF;
444         mib[2] = GPROF_COUNT;
445         if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
446                 err(13, "tickbuf zero");
447         mib[2] = GPROF_FROMS;
448         if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
449                 err(14, "froms zero");
450         mib[2] = GPROF_TOS;
451         if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
452                 err(15, "tos zero");
453         seteuid(getuid());
454         free(zbuf);
455 }