Fix miscellanious size_t related compiler warnings.
[dragonfly.git] / usr.sbin / kgmon / kgmon.c
CommitLineData
984263bc
MD
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.
1de703da
MD
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 $
0ca0cd25 36 * $DragonFly: src/usr.sbin/kgmon/kgmon.c,v 1.7 2008/04/20 13:44:26 swildner Exp $
984263bc
MD
37 */
38
984263bc
MD
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
56struct nlist nl[] = {
57#define N_GMONPARAM 0
58 { "__gmonparam" },
984263bc
MD
59 { NULL },
60};
61
62struct kvmvars {
63 kvm_t *kd;
64 struct gmonparam gpm;
65};
66
67int Bflag, bflag, hflag, kflag, rflag, pflag;
68int debug = 0;
2d8a3be7 69int getprof(struct kvmvars *);
2d8a3be7
EN
70void kern_readonly(int);
71int openfiles(char *, char *, struct kvmvars *);
72void setprof(struct kvmvars *kvp, int state);
73void dumpstate(struct kvmvars *kvp);
74void reset(struct kvmvars *kvp);
75static void usage(void);
984263bc
MD
76
77int
78main(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);
6155eec4 154 fprintf(stdout, "kgmon: kernel profiling is %s.\n",
984263bc
MD
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
164static void
68da13b1 165usage(void)
984263bc
MD
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 */
174int
68da13b1 175openfiles(char *system, char *kmemf, struct kvmvars *kvp)
984263bc 176{
424e8382
MD
177 int mib[3], state, openmode;
178 size_t size;
984263bc
MD
179 char errbuf[_POSIX2_LINE_MAX];
180
181 if (!kflag) {
182 mib[0] = CTL_KERN;
183 mib[1] = KERN_PROF;
184 mib[2] = GPROF_STATE;
185 size = sizeof state;
186 if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
187 errx(20, "profiling not defined in kernel");
188 if (!(Bflag || bflag || hflag || rflag ||
189 (pflag &&
190 (state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
191 return (O_RDONLY);
6155eec4 192 seteuid(0);
984263bc
MD
193 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
194 return (O_RDWR);
6155eec4 195 seteuid(getuid());
984263bc
MD
196 kern_readonly(state);
197 return (O_RDONLY);
198 }
199 openmode = (Bflag || bflag || hflag || pflag || rflag)
200 ? O_RDWR : O_RDONLY;
201 kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf);
202 if (kvp->kd == NULL) {
203 if (openmode == O_RDWR) {
204 openmode = O_RDONLY;
205 kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
206 errbuf);
207 }
208 if (kvp->kd == NULL)
209 errx(2, "kvm_openfiles: %s", errbuf);
210 kern_readonly(GMON_PROF_ON);
211 }
212 if (kvm_nlist(kvp->kd, nl) < 0)
213 errx(3, "%s: no namelist", system);
214 if (!nl[N_GMONPARAM].n_value)
215 errx(20, "profiling not defined in kernel");
216 return (openmode);
217}
218
219/*
220 * Suppress options that require a writable kernel.
221 */
222void
68da13b1 223kern_readonly(int mode)
984263bc 224{
6155eec4 225 fprintf(stderr, "kgmon: kernel read-only: ");
984263bc 226 if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON))
6155eec4 227 fprintf(stderr, "data may be inconsistent\n");
984263bc 228 if (rflag)
0ca0cd25 229 fprintf(stderr, "-r suppressed\n");
984263bc 230 if (Bflag)
0ca0cd25 231 fprintf(stderr, "-B suppressed\n");
984263bc 232 if (bflag)
0ca0cd25 233 fprintf(stderr, "-b suppressed\n");
984263bc 234 if (hflag)
0ca0cd25 235 fprintf(stderr, "-h suppressed\n");
984263bc
MD
236 rflag = Bflag = bflag = hflag = 0;
237}
238
239/*
240 * Get the state of kernel profiling.
241 */
242int
68da13b1 243getprof(struct kvmvars *kvp)
984263bc 244{
424e8382
MD
245 int mib[3];
246 size_t size;
984263bc
MD
247
248 if (kflag) {
249 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
250 sizeof kvp->gpm);
251 } else {
252 mib[0] = CTL_KERN;
253 mib[1] = KERN_PROF;
254 mib[2] = GPROF_GMONPARAM;
255 size = sizeof kvp->gpm;
256 if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
257 size = 0;
258 }
259 if (size != sizeof kvp->gpm)
260 errx(4, "cannot get gmonparam: %s",
261 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
262 return (kvp->gpm.state);
263}
264
265/*
266 * Enable or disable kernel profiling according to the state variable.
267 */
268void
68da13b1 269setprof(struct kvmvars *kvp, int state)
984263bc
MD
270{
271 struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
424e8382
MD
272 int mib[3], oldstate;
273 size_t size;
984263bc 274
424e8382 275 size = sizeof(state);
984263bc
MD
276 if (!kflag) {
277 mib[0] = CTL_KERN;
278 mib[1] = KERN_PROF;
279 mib[2] = GPROF_STATE;
424e8382 280 if (sysctl(mib, 3, &oldstate, &size, NULL, 0) < 0)
984263bc
MD
281 goto bad;
282 if (oldstate == state)
283 return;
6155eec4 284 seteuid(0);
424e8382 285 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0) {
6155eec4 286 seteuid(getuid());
984263bc
MD
287 return;
288 }
6155eec4 289 seteuid(getuid());
424e8382
MD
290 } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, size)
291 == size) {
984263bc 292 return;
424e8382 293 }
984263bc
MD
294bad:
295 warnx("warning: cannot turn profiling %s",
296 state == GMON_PROF_OFF ? "off" : "on");
297}
298
299/*
300 * Build the gmon.out file.
301 */
302void
68da13b1 303dumpstate(struct kvmvars *kvp)
984263bc 304{
6155eec4 305 FILE *fp;
984263bc
MD
306 struct rawarc rawarc;
307 struct tostruct *tos;
308 u_long frompc;
309 u_short *froms, *tickbuf;
424e8382
MD
310 int mib[3];
311 size_t i;
984263bc
MD
312 struct gmonhdr h;
313 int fromindex, endfrom, toindex;
314
315 setprof(kvp, GMON_PROF_OFF);
316 fp = fopen("gmon.out", "w");
317 if (fp == 0) {
318 warn("gmon.out");
319 return;
320 }
321
322 /*
323 * Build the gmon header and write it to a file.
324 */
325 bzero(&h, sizeof(h));
326 h.lpc = kvp->gpm.lowpc;
327 h.hpc = kvp->gpm.highpc;
328 h.ncnt = kvp->gpm.kcountsize + sizeof(h);
329 h.version = GMONVERSION;
330 h.profrate = kvp->gpm.profrate;
984263bc
MD
331 fwrite((char *)&h, sizeof(h), 1, fp);
332
333 /*
334 * Write out the tick buffer.
335 */
336 mib[0] = CTL_KERN;
337 mib[1] = KERN_PROF;
338 if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL)
339 errx(5, "cannot allocate kcount space");
340 if (kflag) {
341 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
342 kvp->gpm.kcountsize);
343 } else {
344 mib[2] = GPROF_COUNT;
345 i = kvp->gpm.kcountsize;
346 if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
347 i = 0;
348 }
349 if (i != kvp->gpm.kcountsize)
350 errx(6, "read ticks: read %u, got %d: %s",
351 kvp->gpm.kcountsize, i,
352 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
353 if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
354 err(7, "writing tocks to gmon.out");
355 free(tickbuf);
356
357 /*
358 * Write out the arc info.
359 */
360 if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL)
361 errx(8, "cannot allocate froms space");
362 if (kflag) {
363 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
364 kvp->gpm.fromssize);
365 } else {
366 mib[2] = GPROF_FROMS;
367 i = kvp->gpm.fromssize;
368 if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
369 i = 0;
370 }
371 if (i != kvp->gpm.fromssize)
372 errx(9, "read froms: read %u, got %d: %s",
373 kvp->gpm.fromssize, i,
374 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
375 if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL)
376 errx(10, "cannot allocate tos space");
377 if (kflag) {
378 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
379 kvp->gpm.tossize);
380 } else {
381 mib[2] = GPROF_TOS;
382 i = kvp->gpm.tossize;
383 if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
384 i = 0;
385 }
386 if (i != kvp->gpm.tossize)
387 errx(11, "read tos: read %u, got %d: %s",
388 kvp->gpm.tossize, i,
389 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
390 if (debug)
391 warnx("lowpc 0x%x, textsize 0x%x",
392 kvp->gpm.lowpc, kvp->gpm.textsize);
393 endfrom = kvp->gpm.fromssize / sizeof(*froms);
394 for (fromindex = 0; fromindex < endfrom; ++fromindex) {
395 if (froms[fromindex] == 0)
396 continue;
397 frompc = (u_long)kvp->gpm.lowpc +
398 (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
399 for (toindex = froms[fromindex]; toindex != 0;
400 toindex = tos[toindex].link) {
401 if (debug)
402 warnx("[mcleanup] frompc 0x%x selfpc 0x%x count %d",
403 frompc, tos[toindex].selfpc,
404 tos[toindex].count);
405 rawarc.raw_frompc = frompc;
406 rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
407 rawarc.raw_count = tos[toindex].count;
408 fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
409 }
410 }
411 fclose(fp);
412}
413
414/*
984263bc
MD
415 * Reset the kernel profiling date structures.
416 */
417void
68da13b1 418reset(struct kvmvars *kvp)
984263bc
MD
419{
420 char *zbuf;
421 u_long biggest;
422 int mib[3];
423
424 setprof(kvp, GMON_PROF_OFF);
425
426 biggest = kvp->gpm.kcountsize;
427 if (kvp->gpm.fromssize > biggest)
428 biggest = kvp->gpm.fromssize;
429 if (kvp->gpm.tossize > biggest)
430 biggest = kvp->gpm.tossize;
431 if ((zbuf = (char *)malloc(biggest)) == NULL)
432 errx(12, "cannot allocate zbuf space");
433 bzero(zbuf, biggest);
434 if (kflag) {
435 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
436 kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
437 errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
438 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
439 kvp->gpm.fromssize) != kvp->gpm.fromssize)
440 errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
441 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
442 kvp->gpm.tossize) != kvp->gpm.tossize)
443 errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
444 return;
445 }
6155eec4 446 seteuid(0);
984263bc
MD
447 mib[0] = CTL_KERN;
448 mib[1] = KERN_PROF;
449 mib[2] = GPROF_COUNT;
450 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
451 err(13, "tickbuf zero");
452 mib[2] = GPROF_FROMS;
453 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
454 err(14, "froms zero");
455 mib[2] = GPROF_TOS;
456 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
457 err(15, "tos zero");
6155eec4 458 seteuid(getuid());
984263bc
MD
459 free(zbuf);
460}