ktrdump.c revision 153270
193504Sjake/*- 293504Sjake * Copyright (c) 2002 Jake Burkholder 3129574Srwatson * Copyright (c) 2004 Robert Watson 493504Sjake * All rights reserved. 593504Sjake * 693504Sjake * Redistribution and use in source and binary forms, with or without 793504Sjake * modification, are permitted provided that the following conditions 893504Sjake * are met: 993504Sjake * 1. Redistributions of source code must retain the above copyright 1093504Sjake * notice, this list of conditions and the following disclaimer. 1193504Sjake * 2. Redistributions in binary form must reproduce the above copyright 1293504Sjake * notice, this list of conditions and the following disclaimer in the 1393504Sjake * documentation and/or other materials provided with the distribution. 1493504Sjake * 1593504Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1693504Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1793504Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1893504Sjake * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1993504Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2093504Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2193504Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2293504Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2393504Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2493504Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2593504Sjake * SUCH DAMAGE. 2693504Sjake */ 2793504Sjake 2893504Sjake#include <sys/cdefs.h> 2993504Sjake__FBSDID("$FreeBSD: head/usr.bin/ktrdump/ktrdump.c 153270 2005-12-09 14:27:03Z njl $"); 3093504Sjake 3193504Sjake#include <sys/types.h> 3293504Sjake#include <sys/ktr.h> 33103791Sjeff#include <sys/mman.h> 34103791Sjeff#include <sys/stat.h> 3593504Sjake 3693504Sjake#include <err.h> 3793504Sjake#include <fcntl.h> 3893504Sjake#include <kvm.h> 3993504Sjake#include <limits.h> 4093504Sjake#include <nlist.h> 4193617Sjake#include <stdint.h> 4293504Sjake#include <stdio.h> 4393504Sjake#include <stdlib.h> 4493504Sjake#include <string.h> 4593504Sjake#include <unistd.h> 4693504Sjake 4793504Sjake#define SBUFLEN 128 4893504Sjake#define USAGE \ 49147034Scsjp "usage: ktrdump [-cfqrt] [-e execfile] [-i ktrfile] [-m corefile] [-o outfile]\n" 5093504Sjake 5193504Sjakeextern char *optarg; 5293504Sjakeextern int optind; 5393504Sjake 5493504Sjakestatic void usage(void); 5593504Sjake 5693504Sjakestatic struct nlist nl[] = { 5793504Sjake { "_ktr_version" }, 5893504Sjake { "_ktr_entries" }, 5993504Sjake { "_ktr_idx" }, 6093504Sjake { "_ktr_buf" }, 6193504Sjake { NULL } 6293504Sjake}; 6393504Sjake 6493504Sjakestatic int cflag; 6593504Sjakestatic int eflag; 6693504Sjakestatic int fflag; 6793504Sjakestatic int mflag; 68129559Srwatsonstatic int qflag; 69129574Srwatsonstatic int rflag; 7093504Sjakestatic int tflag; 71103791Sjeffstatic int iflag; 7293504Sjake 7393504Sjakestatic char corefile[PATH_MAX]; 7493504Sjakestatic char execfile[PATH_MAX]; 7593504Sjake 7693504Sjakestatic char desc[SBUFLEN]; 7793504Sjakestatic char errbuf[_POSIX2_LINE_MAX]; 7893504Sjakestatic char fbuf[PATH_MAX]; 7993504Sjakestatic char obuf[PATH_MAX]; 8093504Sjakestatic char sbuf[KTR_PARMS][SBUFLEN]; 8193504Sjake 8293504Sjake/* 8393504Sjake * Reads the ktr trace buffer from kernel memory and prints the trace entries. 8493504Sjake */ 8593504Sjakeint 8693504Sjakemain(int ac, char **av) 8793504Sjake{ 8893504Sjake u_long parms[KTR_PARMS]; 8993504Sjake struct ktr_entry *buf; 90129574Srwatson uintmax_t tlast, tnow; 91103791Sjeff struct stat sb; 9293504Sjake kvm_t *kd; 9393504Sjake FILE *out; 9493504Sjake char *p; 9593504Sjake int version; 9693504Sjake int entries; 9793504Sjake int index; 9893504Sjake int parm; 99103791Sjeff int in; 10093504Sjake int c; 101135842Sjulian int i = 0; 10293504Sjake 10393504Sjake /* 10493504Sjake * Parse commandline arguments. 10593504Sjake */ 10693504Sjake out = stdout; 107129574Srwatson while ((c = getopt(ac, av, "cfqrte:i:m:o:")) != -1) 10893504Sjake switch (c) { 10993504Sjake case 'c': 11093504Sjake cflag = 1; 11193504Sjake break; 11293504Sjake case 'e': 113104586Skris if (strlcpy(execfile, optarg, sizeof(execfile)) 114104586Skris >= sizeof(execfile)) 115104586Skris errx(1, "%s: File name too long", optarg); 11693504Sjake eflag = 1; 11793504Sjake break; 11893504Sjake case 'f': 11993504Sjake fflag = 1; 12093504Sjake break; 121103791Sjeff case 'i': 122103791Sjeff iflag = 1; 123103791Sjeff if ((in = open(optarg, O_RDONLY)) == -1) 124103791Sjeff err(1, "%s", optarg); 125103791Sjeff break; 12693504Sjake case 'm': 127104586Skris if (strlcpy(corefile, optarg, sizeof(corefile)) 128104586Skris >= sizeof(corefile)) 129104586Skris errx(1, "%s: File name too long", optarg); 13093504Sjake mflag = 1; 13193504Sjake break; 13293504Sjake case 'o': 13393504Sjake if ((out = fopen(optarg, "w")) == NULL) 13493504Sjake err(1, "%s", optarg); 13593504Sjake break; 136129559Srwatson case 'q': 137129559Srwatson qflag++; 138129559Srwatson break; 139129574Srwatson case 'r': 140129574Srwatson rflag = 1; 141129574Srwatson break; 14293504Sjake case 't': 14393504Sjake tflag = 1; 14493504Sjake break; 14593504Sjake case '?': 14693504Sjake default: 14793504Sjake usage(); 14893504Sjake } 14993504Sjake ac -= optind; 15093504Sjake av += optind; 15193504Sjake if (ac != 0) 15293504Sjake usage(); 15393504Sjake 15493504Sjake /* 15593504Sjake * Open our execfile and corefile, resolve needed symbols and read in 15693504Sjake * the trace buffer. 15793504Sjake */ 15893504Sjake if ((kd = kvm_openfiles(eflag ? execfile : NULL, 15993504Sjake mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL) 16093504Sjake errx(1, "%s", errbuf); 16193504Sjake if (kvm_nlist(kd, nl) != 0 || 16293504Sjake kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1) 16393504Sjake errx(1, "%s", kvm_geterr(kd)); 16493504Sjake if (version != KTR_VERSION) 16593504Sjake errx(1, "ktr version mismatch"); 166103791Sjeff if (iflag) { 167103791Sjeff if (fstat(in, &sb) == -1) 168103791Sjeff errx(1, "stat"); 169103791Sjeff entries = sb.st_size / sizeof(*buf); 170103791Sjeff index = 0; 171103791Sjeff buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, in, 0); 172103791Sjeff if (buf == MAP_FAILED) 173103791Sjeff errx(1, "mmap"); 174103791Sjeff } else { 175103791Sjeff if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries)) 176103791Sjeff == -1) 177103791Sjeff errx(1, "%s", kvm_geterr(kd)); 178103791Sjeff if ((buf = malloc(sizeof(*buf) * entries)) == NULL) 179103791Sjeff err(1, NULL); 180103791Sjeff if (kvm_read(kd, nl[2].n_value, &index, sizeof(index)) == -1 || 181103791Sjeff kvm_read(kd, nl[3].n_value, buf, sizeof(*buf) * entries) 182103791Sjeff == -1) 183103791Sjeff errx(1, "%s", kvm_geterr(kd)); 184103791Sjeff } 18593504Sjake 18693504Sjake /* 18793504Sjake * Print a nice header. 18893504Sjake */ 189129559Srwatson if (!qflag) { 190129559Srwatson fprintf(out, "%-6s ", "index"); 191129559Srwatson if (cflag) 192129559Srwatson fprintf(out, "%-3s ", "cpu"); 193129559Srwatson if (tflag) 194129559Srwatson fprintf(out, "%-16s ", "timestamp"); 195129559Srwatson if (fflag) 196129559Srwatson fprintf(out, "%-40s ", "file and line"); 197129559Srwatson fprintf(out, "%s", "trace"); 198129559Srwatson fprintf(out, "\n"); 19993504Sjake 200129559Srwatson fprintf(out, "------ "); 201129559Srwatson if (cflag) 202129559Srwatson fprintf(out, "--- "); 203129559Srwatson if (tflag) 204129559Srwatson fprintf(out, "---------------- "); 205129559Srwatson if (fflag) 206129559Srwatson fprintf(out, 207129559Srwatson "---------------------------------------- "); 208129559Srwatson fprintf(out, "----- "); 209129559Srwatson fprintf(out, "\n"); 210129559Srwatson } 21193504Sjake 21293504Sjake /* 21393504Sjake * Now tear through the trace buffer. 21493504Sjake */ 215103791Sjeff if (!iflag) 216103791Sjeff i = (index - 1) & (entries - 1); 217129574Srwatson tlast = -1; 21893504Sjake for (;;) { 21993504Sjake if (buf[i].ktr_desc == NULL) 22093504Sjake break; 22193504Sjake if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc, 22293504Sjake sizeof(desc)) == -1) 22393504Sjake errx(1, "%s", kvm_geterr(kd)); 22493504Sjake desc[sizeof(desc) - 1] = '\0'; 22593504Sjake parm = 0; 22693504Sjake for (p = desc; (c = *p++) != '\0';) { 22793504Sjake if (c != '%') 22893504Sjake continue; 22993504Sjake if ((c = *p++) == '\0') 23093504Sjake break; 23193504Sjake if (parm == KTR_PARMS) 23293504Sjake errx(1, "too many parameters"); 23393504Sjake switch (c) { 23493504Sjake case 's': 23593504Sjake if (kvm_read(kd, (u_long)buf[i].ktr_parms[parm], 23693504Sjake sbuf[parm], sizeof(sbuf[parm])) == -1) 23793504Sjake strcpy(sbuf[parm], "(null)"); 23893504Sjake sbuf[parm][sizeof(sbuf[0]) - 1] = '\0'; 23993504Sjake parms[parm] = (u_long)sbuf[parm]; 24093504Sjake parm++; 24193504Sjake break; 24293504Sjake default: 24393504Sjake parms[parm] = buf[i].ktr_parms[parm]; 24493504Sjake parm++; 24593504Sjake break; 24693504Sjake } 24793504Sjake } 24893504Sjake fprintf(out, "%6d ", i); 24993504Sjake if (cflag) 25093504Sjake fprintf(out, "%3d ", buf[i].ktr_cpu); 251129574Srwatson if (tflag) { 252129574Srwatson tnow = (uintmax_t)buf[i].ktr_timestamp; 253129574Srwatson if (rflag) { 254129574Srwatson if (tlast == -1) 255129574Srwatson tlast = tnow; 256153270Snjl fprintf(out, "%16ju ", !iflag ? tlast - tnow : 257153270Snjl tnow - tlast); 258129574Srwatson tlast = tnow; 259129574Srwatson } else 260129574Srwatson fprintf(out, "%16ju ", tnow); 261129574Srwatson } 26293504Sjake if (fflag) { 26393504Sjake if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf, 26493504Sjake sizeof(fbuf)) == -1) 26593504Sjake strcpy(fbuf, "(null)"); 26693504Sjake snprintf(obuf, sizeof(obuf), "%s:%d", fbuf, 26793504Sjake buf[i].ktr_line); 26893504Sjake fprintf(out, "%-40s ", obuf); 26993504Sjake } 27093504Sjake fprintf(out, desc, parms[0], parms[1], parms[2], parms[3], 27193504Sjake parms[4], parms[5]); 27293504Sjake fprintf(out, "\n"); 273103791Sjeff if (!iflag) { 274103791Sjeff if (i == index) 275103791Sjeff break; 276103791Sjeff i = (i - 1) & (entries - 1); 277103791Sjeff } else { 278103791Sjeff if (++i == entries) 279103791Sjeff break; 280103791Sjeff } 28193504Sjake } 28293504Sjake 28393504Sjake return (0); 28493504Sjake} 28593504Sjake 28693504Sjakestatic void 28793504Sjakeusage(void) 28893504Sjake{ 289146466Sru 290146466Sru fprintf(stderr, USAGE); 291146466Sru exit(1); 29293504Sjake} 293