1 /*- 2 * Copyright (c) 2002 Jake Burkholder 3 * 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 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/types.h> 31 #include <sys/ktr.h> 32 33 #include <err.h> 34 #include <fcntl.h> 35 #include <kvm.h> 36 #include <limits.h> 37 #include <nlist.h> 38 #include <stdint.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 44 #define SBUFLEN 128 45 #define USAGE \ 46 "usage: ktrdump [-c] [-f] [-t] [-e execfile] [-m corefile] [-o outfile]" 47 48 extern char *optarg; 49 extern int optind; 50 51 static void usage(void); 52 53 static struct nlist nl[] = { 54 { "_ktr_version" }, 55 { "_ktr_entries" }, 56 { "_ktr_idx" }, 57 { "_ktr_buf" }, 58 { NULL } 59 }; 60 61 static int cflag; 62 static int eflag; 63 static int fflag; 64 static int mflag; 65 static int tflag; 66 67 static char corefile[PATH_MAX]; 68 static char execfile[PATH_MAX]; 69 70 static char desc[SBUFLEN]; 71 static char errbuf[_POSIX2_LINE_MAX]; 72 static char fbuf[PATH_MAX]; 73 static char obuf[PATH_MAX]; 74 static char sbuf[KTR_PARMS][SBUFLEN]; 75 76 /* 77 * Reads the ktr trace buffer from kernel memory and prints the trace entries. 78 */ 79 int 80 main(int ac, char **av) 81 { 82 u_long parms[KTR_PARMS]; 83 struct ktr_entry *buf; 84 kvm_t *kd; 85 FILE *out; 86 char *p; 87 int version; 88 int entries; 89 int index; 90 int parm; 91 int c; 92 int i; 93 int n; 94 95 /* 96 * Parse commandline arguments. 97 */ 98 out = stdout; 99 while ((c = getopt(ac, av, "cfte:m:o:")) != -1) 100 switch (c) { 101 case 'c': 102 cflag = 1; 103 break; 104 case 'e': 105 strcpy(execfile, optarg); 106 eflag = 1; 107 break; 108 case 'f': 109 fflag = 1; 110 break; 111 case 'm': 112 strcpy(corefile, optarg); 113 mflag = 1; 114 break; 115 case 'o': 116 if ((out = fopen(optarg, "w")) == NULL) 117 err(1, "%s", optarg); 118 break; 119 case 't': 120 tflag = 1; 121 break; 122 case '?': 123 default: 124 usage(); 125 } 126 ac -= optind; 127 av += optind; 128 if (ac != 0) 129 usage(); 130 131 /* 132 * Open our execfile and corefile, resolve needed symbols and read in 133 * the trace buffer. 134 */ 135 if ((kd = kvm_openfiles(eflag ? execfile : NULL, 136 mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL) 137 errx(1, "%s", errbuf); 138 if (kvm_nlist(kd, nl) != 0 || 139 kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1) 140 errx(1, "%s", kvm_geterr(kd)); 141 if (version != KTR_VERSION) 142 errx(1, "ktr version mismatch"); 143 if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries)) == -1) 144 errx(1, "%s", kvm_geterr(kd)); 145 if ((buf = malloc(sizeof(*buf) * entries)) == NULL) 146 err(1, NULL); 147 if (kvm_read(kd, nl[2].n_value, &index, sizeof(index)) == -1 || 148 kvm_read(kd, nl[3].n_value, buf, sizeof(*buf) * entries) == -1) 149 errx(1, "%s", kvm_geterr(kd)); 150 151 /* 152 * Print a nice header. 153 */ 154 fprintf(out, "%-6s ", "index"); 155 if (cflag) 156 fprintf(out, "%-3s ", "cpu"); 157 if (tflag) 158 fprintf(out, "%-16s ", "timestamp"); 159 if (fflag) 160 fprintf(out, "%-32s ", "file and line"); 161 fprintf(out, "%s", "trace"); 162 fprintf(out, "\n"); 163 164 fprintf(out, "------ "); 165 if (cflag) 166 fprintf(out, "--- "); 167 if (tflag) 168 fprintf(out, "---------------- "); 169 if (fflag) 170 fprintf(out, "---------------------------------------- "); 171 fprintf(out, "----- "); 172 fprintf(out, "\n"); 173 174 /* 175 * Now tear through the trace buffer. 176 */ 177 i = (index - 1) & (entries - 1); 178 for (;;) { 179 if (buf[i].ktr_desc == NULL) 180 break; 181 if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc, 182 sizeof(desc)) == -1) 183 errx(1, "%s", kvm_geterr(kd)); 184 desc[sizeof(desc) - 1] = '\0'; 185 parm = 0; 186 for (p = desc; (c = *p++) != '\0';) { 187 if (c != '%') 188 continue; 189 if ((c = *p++) == '\0') 190 break; 191 if (parm == KTR_PARMS) 192 errx(1, "too many parameters"); 193 switch (c) { 194 case 's': 195 if (kvm_read(kd, (u_long)buf[i].ktr_parms[parm], 196 sbuf[parm], sizeof(sbuf[parm])) == -1) 197 strcpy(sbuf[parm], "(null)"); 198 sbuf[parm][sizeof(sbuf[0]) - 1] = '\0'; 199 parms[parm] = (u_long)sbuf[parm]; 200 parm++; 201 break; 202 default: 203 parms[parm] = buf[i].ktr_parms[parm]; 204 parm++; 205 break; 206 } 207 } 208 fprintf(out, "%6d ", i); 209 if (cflag) 210 fprintf(out, "%3d ", buf[i].ktr_cpu); 211 if (tflag) 212 fprintf(out, "%16ju ", 213 (uintmax_t)buf[i].ktr_timestamp); 214 if (fflag) { 215 if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf, 216 sizeof(fbuf)) == -1) 217 strcpy(fbuf, "(null)"); 218 snprintf(obuf, sizeof(obuf), "%s:%d", fbuf, 219 buf[i].ktr_line); 220 fprintf(out, "%-40s ", obuf); 221 } 222 fprintf(out, desc, parms[0], parms[1], parms[2], parms[3], 223 parms[4], parms[5]); 224 fprintf(out, "\n"); 225 if (i == index) 226 break; 227 i = (i - 1) & (entries - 1); 228 } 229 230 return (0); 231 } 232 233 static void 234 usage(void) 235 { 236 errx(1, USAGE); 237 } 238