Sweep-fix comparing pointers with 0 (and assigning 0 to pointers).
[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  */
37
38 #include <sys/param.h>
39 #include <sys/file.h>
40 #include <sys/time.h>
41 #include <sys/sysctl.h>
42 #include <sys/gmon.h>
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <kvm.h>
47 #include <limits.h>
48 #include <nlist.h>
49 #include <paths.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 struct nlist nl[] = {
56 #define N_GMONPARAM     0
57         { "__gmonparam" },
58         { NULL },
59 };
60
61 struct kvmvars {
62         kvm_t   *kd;
63         struct gmonparam gpm;
64 };
65
66 int     Bflag, bflag, hflag, kflag, rflag, pflag;
67 int     debug = 0;
68 int     getprof(struct kvmvars *);
69 void    kern_readonly(int);
70 int     openfiles(char *, char *, struct kvmvars *);
71 void    setprof(struct kvmvars *kvp, int state);
72 void    dumpstate(struct kvmvars *kvp);
73 void    reset(struct kvmvars *kvp);
74 static void usage(void);
75
76 int
77 main(int argc, char **argv)
78 {
79         int ch, mode, disp, accessmode;
80         struct kvmvars kvmvars;
81         char *system, *kmemf;
82
83         seteuid(getuid());
84         kmemf = NULL;
85         system = NULL;
86         while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) {
87                 switch((char)ch) {
88
89                 case 'M':
90                         kmemf = optarg;
91                         kflag = 1;
92                         break;
93
94                 case 'N':
95                         system = optarg;
96                         break;
97
98                 case 'B':
99                         Bflag = 1;
100                         break;
101
102                 case 'b':
103                         bflag = 1;
104                         break;
105
106                 case 'h':
107                         hflag = 1;
108                         break;
109
110                 case 'p':
111                         pflag = 1;
112                         break;
113
114                 case 'r':
115                         rflag = 1;
116                         break;
117
118                 default:
119                         usage();
120                 }
121         }
122         argc -= optind;
123         argv += optind;
124
125 #define BACKWARD_COMPATIBILITY
126 #ifdef  BACKWARD_COMPATIBILITY
127         if (*argv) {
128                 system = *argv;
129                 if (*++argv) {
130                         kmemf = *argv;
131                         ++kflag;
132                 }
133         }
134 #endif
135         if (system == NULL)
136                 system = (char *)getbootfile();
137         accessmode = openfiles(system, kmemf, &kvmvars);
138         mode = getprof(&kvmvars);
139         if (hflag)
140                 disp = GMON_PROF_OFF;
141         else if (Bflag)
142                 disp = GMON_PROF_HIRES;
143         else if (bflag)
144                 disp = GMON_PROF_ON;
145         else
146                 disp = mode;
147         if (pflag)
148                 dumpstate(&kvmvars);
149         if (rflag)
150                 reset(&kvmvars);
151         if (accessmode == O_RDWR)
152                 setprof(&kvmvars, disp);
153         fprintf(stdout, "kgmon: kernel profiling is %s.\n",
154                       disp == GMON_PROF_OFF ? "off" :
155                       disp == GMON_PROF_HIRES ? "running (high resolution)" :
156                       disp == GMON_PROF_ON ? "running" :
157                       disp == GMON_PROF_BUSY ? "busy" :
158                       disp == GMON_PROF_ERROR ? "off (error)" :
159                       "in an unknown state");
160         return (0);
161 }
162
163 static void
164 usage(void)
165 {
166         fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
167         exit(1);
168 }
169
170 /*
171  * Check that profiling is enabled and open any ncessary files.
172  */
173 int
174 openfiles(char *system, char *kmemf, struct kvmvars *kvp)
175 {
176         int mib[3], state, openmode;
177         size_t size;
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];
245         size_t size;
246
247         if (kflag) {
248                 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
249                     sizeof kvp->gpm);
250         } else {
251                 mib[0] = CTL_KERN;
252                 mib[1] = KERN_PROF;
253                 mib[2] = GPROF_GMONPARAM;
254                 size = sizeof kvp->gpm;
255                 if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
256                         size = 0;
257         }
258         if (size != sizeof kvp->gpm)
259                 errx(4, "cannot get gmonparam: %s",
260                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
261         return (kvp->gpm.state);
262 }
263
264 /*
265  * Enable or disable kernel profiling according to the state variable.
266  */
267 void
268 setprof(struct kvmvars *kvp, int state)
269 {
270         struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
271         int mib[3], oldstate;
272         size_t size;
273
274         size = sizeof(state);
275         if (!kflag) {
276                 mib[0] = CTL_KERN;
277                 mib[1] = KERN_PROF;
278                 mib[2] = GPROF_STATE;
279                 if (sysctl(mib, 3, &oldstate, &size, NULL, 0) < 0)
280                         goto bad;
281                 if (oldstate == state)
282                         return;
283                 seteuid(0);
284                 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0) {
285                         seteuid(getuid());
286                         return;
287                 }
288                 seteuid(getuid());
289         } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, size)
290                     == size) {
291                 return;
292         }
293 bad:
294         warnx("warning: cannot turn profiling %s",
295             state == GMON_PROF_OFF ? "off" : "on");
296 }
297
298 /*
299  * Build the gmon.out file.
300  */
301 void
302 dumpstate(struct kvmvars *kvp)
303 {
304         FILE *fp;
305         struct rawarc rawarc;
306         struct tostruct *tos;
307         u_long frompc;
308         u_short *froms, *tickbuf;
309         int mib[3];
310         size_t 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 == NULL) {
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         fwrite((char *)&h, sizeof(h), 1, fp);
331
332         /*
333          * Write out the tick buffer.
334          */
335         mib[0] = CTL_KERN;
336         mib[1] = KERN_PROF;
337         if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL)
338                 errx(5, "cannot allocate kcount space");
339         if (kflag) {
340                 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
341                     kvp->gpm.kcountsize);
342         } else {
343                 mib[2] = GPROF_COUNT;
344                 i = kvp->gpm.kcountsize;
345                 if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
346                         i = 0;
347         }
348         if (i != kvp->gpm.kcountsize)
349                 errx(6, "read ticks: read %lu, got %zd: %s",
350                     kvp->gpm.kcountsize, i,
351                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
352         if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
353                 err(7, "writing tocks to gmon.out");
354         free(tickbuf);
355
356         /*
357          * Write out the arc info.
358          */
359         if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL)
360                 errx(8, "cannot allocate froms space");
361         if (kflag) {
362                 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
363                     kvp->gpm.fromssize);
364         } else {
365                 mib[2] = GPROF_FROMS;
366                 i = kvp->gpm.fromssize;
367                 if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
368                         i = 0;
369         }
370         if (i != kvp->gpm.fromssize)
371                 errx(9, "read froms: read %lu, got %zd: %s",
372                     kvp->gpm.fromssize, i,
373                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
374         if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL)
375                 errx(10, "cannot allocate tos space");
376         if (kflag) {
377                 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
378                     kvp->gpm.tossize);
379         } else {
380                 mib[2] = GPROF_TOS;
381                 i = kvp->gpm.tossize;
382                 if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
383                         i = 0;
384         }
385         if (i != kvp->gpm.tossize)
386                 errx(11, "read tos: read %lu, got %zd: %s",
387                     kvp->gpm.tossize, i,
388                     kflag ? kvm_geterr(kvp->kd) : strerror(errno));
389         if (debug)
390                 warnx("lowpc 0x%tx, textsize 0x%lx",
391                               kvp->gpm.lowpc, kvp->gpm.textsize);
392         endfrom = kvp->gpm.fromssize / sizeof(*froms);
393         for (fromindex = 0; fromindex < endfrom; ++fromindex) {
394                 if (froms[fromindex] == 0)
395                         continue;
396                 frompc = (u_long)kvp->gpm.lowpc +
397                     (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
398                 for (toindex = froms[fromindex]; toindex != 0;
399                    toindex = tos[toindex].link) {
400                         if (debug)
401                             warnx("[mcleanup] frompc 0x%lx selfpc 0x%lx count %ld",
402                             frompc, tos[toindex].selfpc,
403                             tos[toindex].count);
404                         rawarc.raw_frompc = frompc;
405                         rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
406                         rawarc.raw_count = tos[toindex].count;
407                         fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
408                 }
409         }
410         fclose(fp);
411 }
412
413 /*
414  * Reset the kernel profiling date structures.
415  */
416 void
417 reset(struct kvmvars *kvp)
418 {
419         char *zbuf;
420         u_long biggest;
421         int mib[3];
422
423         setprof(kvp, GMON_PROF_OFF);
424
425         biggest = kvp->gpm.kcountsize;
426         if (kvp->gpm.fromssize > biggest)
427                 biggest = kvp->gpm.fromssize;
428         if (kvp->gpm.tossize > biggest)
429                 biggest = kvp->gpm.tossize;
430         if ((zbuf = (char *)malloc(biggest)) == NULL)
431                 errx(12, "cannot allocate zbuf space");
432         bzero(zbuf, biggest);
433         if (kflag) {
434                 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
435                     kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
436                         errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
437                 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
438                     kvp->gpm.fromssize) != kvp->gpm.fromssize)
439                         errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
440                 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
441                     kvp->gpm.tossize) != kvp->gpm.tossize)
442                         errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
443                 return;
444         }
445         seteuid(0);
446         mib[0] = CTL_KERN;
447         mib[1] = KERN_PROF;
448         mib[2] = GPROF_COUNT;
449         if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
450                 err(13, "tickbuf zero");
451         mib[2] = GPROF_FROMS;
452         if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
453                 err(14, "froms zero");
454         mib[2] = GPROF_TOS;
455         if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
456                 err(15, "tos zero");
457         seteuid(getuid());
458         free(zbuf);
459 }