1 /*- 2 * Copyright (c) 2002 Jake Burkholder 3 * Copyright (c) 2004 Robert Watson 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/types.h> 32 #include <sys/ktr.h> 33 #include <sys/mman.h> 34 #include <sys/stat.h> 35 36 #include <err.h> 37 #include <fcntl.h> 38 #include <kvm.h> 39 #include <limits.h> 40 #include <nlist.h> 41 #include <stdint.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 #define SBUFLEN 128 48 #define USAGE \ 49 "usage: ktrdump [-cfqrtH] [-e execfile] [-i ktrfile] [-m corefile] [-o outfile]\n" 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 qflag; 66 static int rflag; 67 static int tflag; 68 static int iflag; 69 static int hflag; 70 71 static char corefile[PATH_MAX]; 72 static char execfile[PATH_MAX]; 73 74 static char desc[SBUFLEN]; 75 static char errbuf[_POSIX2_LINE_MAX]; 76 static char fbuf[PATH_MAX]; 77 static char obuf[PATH_MAX]; 78 static char sbuf[KTR_PARMS][SBUFLEN]; 79 80 /* 81 * Reads the ktr trace buffer from kernel memory and prints the trace entries. 82 */ 83 int 84 main(int ac, char **av) 85 { 86 u_long parms[KTR_PARMS]; 87 struct ktr_entry *buf; 88 uintmax_t tlast, tnow; 89 struct stat sb; 90 kvm_t *kd; 91 FILE *out; 92 char *p; 93 int version; 94 int entries; 95 int index; 96 int parm; 97 int in; 98 int c; 99 int i = 0; 100 101 /* 102 * Parse commandline arguments. 103 */ 104 out = stdout; 105 while ((c = getopt(ac, av, "cfqrtHe:i:m:o:")) != -1) 106 switch (c) { 107 case 'c': 108 cflag = 1; 109 break; 110 case 'e': 111 if (strlcpy(execfile, optarg, sizeof(execfile)) 112 >= sizeof(execfile)) 113 errx(1, "%s: File name too long", optarg); 114 eflag = 1; 115 break; 116 case 'f': 117 fflag = 1; 118 break; 119 case 'i': 120 iflag = 1; 121 if ((in = open(optarg, O_RDONLY)) == -1) 122 err(1, "%s", optarg); 123 break; 124 case 'm': 125 if (strlcpy(corefile, optarg, sizeof(corefile)) 126 >= sizeof(corefile)) 127 errx(1, "%s: File name too long", optarg); 128 mflag = 1; 129 break; 130 case 'o': 131 if ((out = fopen(optarg, "w")) == NULL) 132 err(1, "%s", optarg); 133 break; 134 case 'q': 135 qflag++; 136 break; 137 case 'r': 138 rflag = 1; 139 break; 140 case 't': 141 tflag = 1; 142 break; 143 case 'H': 144 hflag = 1; 145 break; 146 case '?': 147 default: 148 usage(); 149 } 150 ac -= optind; 151 av += optind; 152 if (ac != 0) 153 usage(); 154 155 /* 156 * Open our execfile and corefile, resolve needed symbols and read in 157 * the trace buffer. 158 */ 159 if ((kd = kvm_openfiles(eflag ? execfile : NULL, 160 mflag ? corefile : NULL, NULL, O_RDONLY, errbuf)) == NULL) 161 errx(1, "%s", errbuf); 162 if (kvm_nlist(kd, nl) != 0 || 163 kvm_read(kd, nl[0].n_value, &version, sizeof(version)) == -1) 164 errx(1, "%s", kvm_geterr(kd)); 165 if (version != KTR_VERSION) 166 errx(1, "ktr version mismatch"); 167 if (iflag) { 168 if (fstat(in, &sb) == -1) 169 errx(1, "stat"); 170 entries = sb.st_size / sizeof(*buf); 171 index = 0; 172 buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, in, 0); 173 if (buf == MAP_FAILED) 174 errx(1, "mmap"); 175 } else { 176 if (kvm_read(kd, nl[1].n_value, &entries, sizeof(entries)) 177 == -1) 178 errx(1, "%s", kvm_geterr(kd)); 179 if ((buf = malloc(sizeof(*buf) * entries)) == NULL) 180 err(1, NULL); 181 if (kvm_read(kd, nl[2].n_value, &index, sizeof(index)) == -1 || 182 kvm_read(kd, nl[3].n_value, buf, sizeof(*buf) * entries) 183 == -1) 184 errx(1, "%s", kvm_geterr(kd)); 185 } 186 187 /* 188 * Print a nice header. 189 */ 190 if (!qflag) { 191 fprintf(out, "%-6s ", "index"); 192 if (cflag) 193 fprintf(out, "%-3s ", "cpu"); 194 if (tflag) 195 fprintf(out, "%-16s ", "timestamp"); 196 if (fflag) 197 fprintf(out, "%-40s ", "file and line"); 198 if (hflag) 199 fprintf(out, "%-18s ", "tid"); 200 fprintf(out, "%s", "trace"); 201 fprintf(out, "\n"); 202 203 fprintf(out, "------ "); 204 if (cflag) 205 fprintf(out, "--- "); 206 if (tflag) 207 fprintf(out, "---------------- "); 208 if (fflag) 209 fprintf(out, 210 "---------------------------------------- "); 211 if (hflag) 212 fprintf(out, "------------------ "); 213 fprintf(out, "----- "); 214 fprintf(out, "\n"); 215 } 216 217 /* 218 * Now tear through the trace buffer. 219 */ 220 if (!iflag) 221 i = (index - 1) % entries; 222 tlast = -1; 223 for (;;) { 224 if (buf[i].ktr_desc == NULL) 225 break; 226 if (kvm_read(kd, (u_long)buf[i].ktr_desc, desc, 227 sizeof(desc)) == -1) 228 errx(1, "%s", kvm_geterr(kd)); 229 desc[sizeof(desc) - 1] = '\0'; 230 parm = 0; 231 for (p = desc; (c = *p++) != '\0';) { 232 if (c != '%') 233 continue; 234 next: if ((c = *p++) == '\0') 235 break; 236 if (parm == KTR_PARMS) 237 errx(1, "too many parameters"); 238 switch (c) { 239 case '0': case '1': case '2': case '3': case '4': 240 case '5': case '6': case '7': case '8': case '9': 241 case '#': case '-': case ' ': case '+': case '\'': 242 case 'h': case 'l': case 'j': case 't': case 'z': 243 case 'q': case 'L': case '.': 244 goto next; 245 case 's': 246 if (kvm_read(kd, (u_long)buf[i].ktr_parms[parm], 247 sbuf[parm], sizeof(sbuf[parm])) == -1) 248 strcpy(sbuf[parm], "(null)"); 249 sbuf[parm][sizeof(sbuf[0]) - 1] = '\0'; 250 parms[parm] = (u_long)sbuf[parm]; 251 parm++; 252 break; 253 default: 254 parms[parm] = buf[i].ktr_parms[parm]; 255 parm++; 256 break; 257 } 258 } 259 fprintf(out, "%6d ", i); 260 if (cflag) 261 fprintf(out, "%3d ", buf[i].ktr_cpu); 262 if (tflag) { 263 tnow = (uintmax_t)buf[i].ktr_timestamp; 264 if (rflag) { 265 if (tlast == -1) 266 tlast = tnow; 267 fprintf(out, "%16ju ", !iflag ? tlast - tnow : 268 tnow - tlast); 269 tlast = tnow; 270 } else 271 fprintf(out, "%16ju ", tnow); 272 } 273 if (fflag) { 274 if (kvm_read(kd, (u_long)buf[i].ktr_file, fbuf, 275 sizeof(fbuf)) == -1) 276 strcpy(fbuf, "(null)"); 277 snprintf(obuf, sizeof(obuf), "%s:%d", fbuf, 278 buf[i].ktr_line); 279 fprintf(out, "%-40s ", obuf); 280 } 281 if (hflag) 282 fprintf(out, "%p ", buf[i].ktr_thread); 283 fprintf(out, desc, parms[0], parms[1], parms[2], parms[3], 284 parms[4], parms[5]); 285 fprintf(out, "\n"); 286 if (!iflag) { 287 if (i == index) 288 break; 289 i = (i - 1) % entries; 290 } else { 291 if (++i == entries) 292 break; 293 } 294 } 295 296 return (0); 297 } 298 299 static void 300 usage(void) 301 { 302 303 fprintf(stderr, USAGE); 304 exit(1); 305 } 306