Merge from vendor branch LESS:
[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.4 2003/11/16 15:17:36 eirikn 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 #define N_PROFHZ        1
60         { "_profhz" },
61         { NULL },
62 };
63
64 struct kvmvars {
65         kvm_t   *kd;
66         struct gmonparam gpm;
67 };
68
69 int     Bflag, bflag, hflag, kflag, rflag, pflag;
70 int     debug = 0;
71 int     getprof(struct kvmvars *);
72 int     getprofhz(struct kvmvars *);
73 void    kern_readonly(int);
74 int     openfiles(char *, char *, struct kvmvars *);
75 void    setprof(struct kvmvars *kvp, int state);
76 void    dumpstate(struct kvmvars *kvp);
77 void    reset(struct kvmvars *kvp);
78 static void usage(void);
79
80 int
81 main(int argc, char **argv)
82 {
83         int ch, mode, disp, accessmode;
84         struct kvmvars kvmvars;
85         char *system, *kmemf;
86
87         seteuid(getuid());
88         kmemf = NULL;
89         system = NULL;
90         while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) {
91                 switch((char)ch) {
92
93                 case 'M':
94                         kmemf = optarg;
95                         kflag = 1;
96                         break;
97
98                 case 'N':
99                         system = optarg;
100                         break;
101
102                 case 'B':
103                         Bflag = 1;
104                         break;
105
106                 case 'b':
107                         bflag = 1;
108                         break;
109
110                 case 'h':
111                         hflag = 1;
112                         break;
113
114                 case 'p':
115                         pflag = 1;
116                         break;
117
118                 case 'r':
119                         rflag = 1;
120                         break;
121
122                 default:
123                         usage();
124                 }
125         }
126         argc -= optind;
127         argv += optind;
128
129 #define BACKWARD_COMPATIBILITY
130 #ifdef  BACKWARD_COMPATIBILITY
131         if (*argv) {
132                 system = *argv;
133                 if (*++argv) {
134                         kmemf = *argv;
135                         ++kflag;
136                 }
137         }
138 #endif
139         if (system == NULL)
140                 system = (char *)getbootfile();
141         accessmode = openfiles(system, kmemf, &kvmvars);
142         mode = getprof(&kvmvars);
143         if (hflag)
144                 disp = GMON_PROF_OFF;
145         else if (Bflag)
146                 disp = GMON_PROF_HIRES;
147         else if (bflag)
148                 disp = GMON_PROF_ON;
149         else
150                 disp = mode;
151         if (pflag)
152                 dumpstate(&kvmvars);
153         if (rflag)
154                 reset(&kvmvars);
155         if (accessmode == O_RDWR)
156                 setprof(&kvmvars, disp);
157         (void)fprintf(stdout, "kgmon: kernel profiling is %s.\n",
158                       disp == GMON_PROF_OFF ? "off" :
159                       disp == GMON_PROF_HIRES ? "running (high resolution)" :
160                       disp == GMON_PROF_ON ? "running" :
161                       disp == GMON_PROF_BUSY ? "busy" :
162                       disp == GMON_PROF_ERROR ? "off (error)" :
163                       "in an unknown state");
164         return (0);
165 }
166
167 static void
168 usage(void)
169 {
170         fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
171         exit(1);
172 }
173
174 /*
175  * Check that profiling is enabled and open any ncessary files.
176  */
177 int
178 openfiles(char *system, char *kmemf, struct kvmvars *kvp)
179 {
180         int mib[3], state, size, openmode;
181         char errbuf[_POSIX2_LINE_MAX];
182
183         if (!kflag) {
184                 mib[0] = CTL_KERN;
185                 mib[1] = KERN_PROF;
186                 mib[2] = GPROF_STATE;
187                 size = sizeof state;
188                 if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
189                         errx(20, "profiling not defined in kernel");
190                 if (!(Bflag || bflag || hflag || rflag ||
191                     (pflag &&
192                      (state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
193                         return (O_RDONLY);
194                 (void)seteuid(0);
195                 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
196                         return (O_RDWR);
197                 (void)seteuid(getuid());
198                 kern_readonly(state);
199                 return (O_RDONLY);
200         }
201         openmode = (Bflag || bflag || hflag || pflag || rflag)
202                    ? O_RDWR : O_RDONLY;
203         kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf);
204         if (kvp->kd == NULL) {
205                 if (openmode == O_RDWR) {
206                         openmode = O_RDONLY;
207                         kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
208                             errbuf);
209                 }
210                 if (kvp->kd == NULL)
211                         errx(2, "kvm_openfiles: %s", errbuf);
212                 kern_readonly(GMON_PROF_ON);
213         }
214         if (kvm_nlist(kvp->kd, nl) < 0)
215                 errx(3, "%s: no namelist", system);
216         if (!nl[N_GMONPARAM].n_value)
217                 errx(20, "profiling not defined in kernel");
218         return (openmode);
219 }
220
221 /*
222  * Suppress options that require a writable kernel.
223  */
224 void
225 kern_readonly(int mode)
226 {
227
228         (void)fprintf(stderr, "kgmon: kernel read-only: ");
229         if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON))
230                 (void)fprintf(stderr, "data may be inconsistent\n");
231         if (rflag)
232                 (void)fprintf(stderr, "-r supressed\n");
233         if (Bflag)
234                 (void)fprintf(stderr, "-B supressed\n");
235         if (bflag)
236                 (void)fprintf(stderr, "-b supressed\n");
237         if (hflag)
238                 (void)fprintf(stderr, "-h supressed\n");
239         rflag = Bflag = bflag = hflag = 0;
240 }
241
242 /*
243  * Get the state of kernel profiling.
244  */
245 int
246 getprof(struct kvmvars *kvp)
247 {
248         int mib[3], size;
249
250         if (kflag) {
251                 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
252                     sizeof kvp->gpm);
253         } else {
254                 mib[0] = CTL_KERN;
255                 mib[1] = KERN_PROF;
256                 mib[2] = GPROF_GMONPARAM;
257                 size = sizeof kvp->gpm;
258                 if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
259                         size = 0;
260         }
261         if (size != sizeof kvp->gpm)
262                 errx(4, "cannot get gmonparam: %s",
263                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
264         return (kvp->gpm.state);
265 }
266
267 /*
268  * Enable or disable kernel profiling according to the state variable.
269  */
270 void
271 setprof(struct kvmvars *kvp, int state)
272 {
273         struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
274         int mib[3], sz, oldstate;
275
276         sz = sizeof(state);
277         if (!kflag) {
278                 mib[0] = CTL_KERN;
279                 mib[1] = KERN_PROF;
280                 mib[2] = GPROF_STATE;
281                 if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0)
282                         goto bad;
283                 if (oldstate == state)
284                         return;
285                 (void)seteuid(0);
286                 if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) {
287                         (void)seteuid(getuid());
288                         return;
289                 }
290                 (void)seteuid(getuid());
291         } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz)
292             == sz)
293                 return;
294 bad:
295         warnx("warning: cannot turn profiling %s",
296             state == GMON_PROF_OFF ? "off" : "on");
297 }
298
299 /*
300  * Build the gmon.out file.
301  */
302 void
303 dumpstate(struct kvmvars *kvp)
304 {
305         register FILE *fp;
306         struct rawarc rawarc;
307         struct tostruct *tos;
308         u_long frompc;
309         u_short *froms, *tickbuf;
310         int mib[3], i;
311         struct gmonhdr h;
312         int fromindex, endfrom, toindex;
313
314         setprof(kvp, GMON_PROF_OFF);
315         fp = fopen("gmon.out", "w");
316         if (fp == 0) {
317                 warn("gmon.out");
318                 return;
319         }
320
321         /*
322          * Build the gmon header and write it to a file.
323          */
324         bzero(&h, sizeof(h));
325         h.lpc = kvp->gpm.lowpc;
326         h.hpc = kvp->gpm.highpc;
327         h.ncnt = kvp->gpm.kcountsize + sizeof(h);
328         h.version = GMONVERSION;
329         h.profrate = kvp->gpm.profrate;
330         if (h.profrate == 0)
331                 h.profrate = getprofhz(kvp);    /* ancient kernel */
332         fwrite((char *)&h, sizeof(h), 1, fp);
333
334         /*
335          * Write out the tick buffer.
336          */
337         mib[0] = CTL_KERN;
338         mib[1] = KERN_PROF;
339         if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL)
340                 errx(5, "cannot allocate kcount space");
341         if (kflag) {
342                 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
343                     kvp->gpm.kcountsize);
344         } else {
345                 mib[2] = GPROF_COUNT;
346                 i = kvp->gpm.kcountsize;
347                 if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
348                         i = 0;
349         }
350         if (i != kvp->gpm.kcountsize)
351                 errx(6, "read ticks: read %u, got %d: %s",
352                     kvp->gpm.kcountsize, i,
353                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
354         if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
355                 err(7, "writing tocks to gmon.out");
356         free(tickbuf);
357
358         /*
359          * Write out the arc info.
360          */
361         if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL)
362                 errx(8, "cannot allocate froms space");
363         if (kflag) {
364                 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
365                     kvp->gpm.fromssize);
366         } else {
367                 mib[2] = GPROF_FROMS;
368                 i = kvp->gpm.fromssize;
369                 if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
370                         i = 0;
371         }
372         if (i != kvp->gpm.fromssize)
373                 errx(9, "read froms: read %u, got %d: %s",
374                     kvp->gpm.fromssize, i,
375                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
376         if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL)
377                 errx(10, "cannot allocate tos space");
378         if (kflag) {
379                 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
380                     kvp->gpm.tossize);
381         } else {
382                 mib[2] = GPROF_TOS;
383                 i = kvp->gpm.tossize;
384                 if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
385                         i = 0;
386         }
387         if (i != kvp->gpm.tossize)
388                 errx(11, "read tos: read %u, got %d: %s",
389                     kvp->gpm.tossize, i,
390                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
391         if (debug)
392                 warnx("lowpc 0x%x, textsize 0x%x",
393                               kvp->gpm.lowpc, kvp->gpm.textsize);
394         endfrom = kvp->gpm.fromssize / sizeof(*froms);
395         for (fromindex = 0; fromindex < endfrom; ++fromindex) {
396                 if (froms[fromindex] == 0)
397                         continue;
398                 frompc = (u_long)kvp->gpm.lowpc +
399                     (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
400                 for (toindex = froms[fromindex]; toindex != 0;
401                    toindex = tos[toindex].link) {
402                         if (debug)
403                             warnx("[mcleanup] frompc 0x%x selfpc 0x%x count %d",
404                             frompc, tos[toindex].selfpc,
405                             tos[toindex].count);
406                         rawarc.raw_frompc = frompc;
407                         rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
408                         rawarc.raw_count = tos[toindex].count;
409                         fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
410                 }
411         }
412         fclose(fp);
413 }
414
415 /*
416  * Get the profiling rate.
417  */
418 int
419 getprofhz(struct kvmvars *kvp)
420 {
421         int mib[2], size, profrate;
422         struct clockinfo clockrate;
423
424         if (kflag) {
425                 profrate = 1;
426                 if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
427                     sizeof profrate) != sizeof profrate)
428                         warnx("get clockrate: %s", kvm_geterr(kvp->kd));
429                 return (profrate);
430         }
431         mib[0] = CTL_KERN;
432         mib[1] = KERN_CLOCKRATE;
433         clockrate.profhz = 1;
434         size = sizeof clockrate;
435         if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
436                 warn("get clockrate");
437         return (clockrate.profhz);
438 }
439
440 /*
441  * Reset the kernel profiling date structures.
442  */
443 void
444 reset(struct kvmvars *kvp)
445 {
446         char *zbuf;
447         u_long biggest;
448         int mib[3];
449
450         setprof(kvp, GMON_PROF_OFF);
451
452         biggest = kvp->gpm.kcountsize;
453         if (kvp->gpm.fromssize > biggest)
454                 biggest = kvp->gpm.fromssize;
455         if (kvp->gpm.tossize > biggest)
456                 biggest = kvp->gpm.tossize;
457         if ((zbuf = (char *)malloc(biggest)) == NULL)
458                 errx(12, "cannot allocate zbuf space");
459         bzero(zbuf, biggest);
460         if (kflag) {
461                 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
462                     kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
463                         errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
464                 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
465                     kvp->gpm.fromssize) != kvp->gpm.fromssize)
466                         errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
467                 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
468                     kvp->gpm.tossize) != kvp->gpm.tossize)
469                         errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
470                 return;
471         }
472         (void)seteuid(0);
473         mib[0] = CTL_KERN;
474         mib[1] = KERN_PROF;
475         mib[2] = GPROF_COUNT;
476         if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
477                 err(13, "tickbuf zero");
478         mib[2] = GPROF_FROMS;
479         if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
480                 err(14, "froms zero");
481         mib[2] = GPROF_TOS;
482         if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
483                 err(15, "tos zero");
484         (void)seteuid(getuid());
485         free(zbuf);
486 }