1 /* $NetBSD: ktrace.h,v 1.56 2009/01/11 02:45:55 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)ktrace.h 8.2 (Berkeley) 2/19/95 32 */ 33 34 #ifndef _SYS_KTRACE_H_ 35 #define _SYS_KTRACE_H_ 36 37 #include <sys/mutex.h> 38 39 /* 40 * operations to ktrace system call (KTROP(op)) 41 */ 42 #define KTROP_SET 0 /* set trace points */ 43 #define KTROP_CLEAR 1 /* clear trace points */ 44 #define KTROP_CLEARFILE 2 /* stop all tracing to file */ 45 #define KTROP_MASK 0x3 46 #define KTROP(o) ((o)&KTROP_MASK) /* macro to extract operation */ 47 /* 48 * flags (ORed in with operation) 49 */ 50 #define KTRFLAG_DESCEND 4 /* perform op on all children too */ 51 52 /* 53 * ktrace record header 54 */ 55 struct ktr_header { 56 int ktr_len; /* length of record minus length of old header */ 57 #if BYTE_ORDER == LITTLE_ENDIAN 58 short ktr_type; /* trace record type */ 59 short ktr_version; /* trace record version */ 60 #else 61 short ktr_version; /* trace record version */ 62 short ktr_type; /* trace record type */ 63 #endif 64 pid_t ktr_pid; /* process id */ 65 char ktr_comm[MAXCOMLEN+1]; /* command name */ 66 union { 67 struct { /* v0 */ 68 struct { 69 int32_t tv_sec; 70 long tv_usec; 71 } _tv; 72 const void *_buf; 73 } _v0; 74 struct { /* v1 */ 75 struct { 76 int32_t tv_sec; 77 long tv_nsec; 78 } _ts; 79 lwpid_t _lid; 80 } _v1; 81 struct { /* v2 */ 82 struct timespec _ts; 83 lwpid_t _lid; 84 } _v2; 85 } _v; 86 }; 87 88 #define ktr_lid _v._v2._lid 89 #define ktr_olid _v._v1._lid 90 #define ktr_time _v._v2._ts 91 #define ktr_otv _v._v0._tv 92 #define ktr_ots _v._v1._ts 93 #define ktr_ts _v._v2._ts 94 #define ktr_unused _v._v0._buf 95 96 #define KTR_SHIMLEN offsetof(struct ktr_header, ktr_pid) 97 98 /* 99 * Test for kernel trace point 100 */ 101 #define KTRPOINT(p, type) \ 102 (((p)->p_traceflag & (1<<(type))) != 0) 103 104 /* 105 * ktrace record types 106 */ 107 108 /* 109 * KTR_SYSCALL - system call record 110 */ 111 #define KTR_SYSCALL 1 112 struct ktr_syscall { 113 int ktr_code; /* syscall number */ 114 int ktr_argsize; /* size of arguments */ 115 /* 116 * followed by ktr_argsize/sizeof(register_t) "register_t"s 117 */ 118 }; 119 120 /* 121 * KTR_SYSRET - return from system call record 122 */ 123 #define KTR_SYSRET 2 124 struct ktr_sysret { 125 short ktr_code; 126 short ktr_eosys; /* XXX unused */ 127 int ktr_error; 128 register_t ktr_retval; 129 register_t ktr_retval_1; 130 }; 131 132 /* 133 * KTR_NAMEI - namei record 134 */ 135 #define KTR_NAMEI 3 136 /* record contains pathname */ 137 138 /* 139 * KTR_GENIO - trace generic process i/o 140 */ 141 #define KTR_GENIO 4 142 struct ktr_genio { 143 int ktr_fd; 144 enum uio_rw ktr_rw; 145 /* 146 * followed by data successfully read/written 147 */ 148 }; 149 150 /* 151 * KTR_PSIG - trace processed signal 152 */ 153 #define KTR_PSIG 5 154 struct ktr_psig { 155 int signo; 156 sig_t action; 157 sigset_t mask; 158 int code; 159 /* 160 * followed by optional siginfo_t 161 */ 162 }; 163 164 /* 165 * KTR_CSW - trace context switches 166 */ 167 #define KTR_CSW 6 168 struct ktr_csw { 169 int out; /* 1 if switch out, 0 if switch in */ 170 int user; /* 1 if usermode (ivcsw), 0 if kernel (vcsw) */ 171 }; 172 173 /* 174 * KTR_EMUL - emulation change 175 */ 176 #define KTR_EMUL 7 177 /* record contains emulation name */ 178 179 /* 180 * KTR_USER - user record 181 */ 182 #define KTR_USER 8 183 #define KTR_USER_MAXIDLEN 20 184 #define KTR_USER_MAXLEN 2048 /* maximum length of passed data */ 185 struct ktr_user { 186 char ktr_id[KTR_USER_MAXIDLEN]; /* string id of caller */ 187 /* 188 * Followed by ktr_len - sizeof(struct ktr_user) of user data. 189 */ 190 }; 191 192 /* 193 * KTR_MMSG - Mach message 194 */ 195 #define KTR_MMSG 9 196 struct ktr_mmsg { 197 /* 198 * This is a Mach message header 199 */ 200 int ktr_bits; 201 int ktr_size; 202 int ktr_remote_port; 203 int ktr_local_port; 204 int ktr_reserved; 205 int ktr_id; 206 /* 207 * Followed by ktr_size - sizeof(mach_msg_header_t) of message payload 208 */ 209 }; 210 211 /* 212 * KTR_EXEC_ARG, KTR_EXEC_ENV - Arguments and environment from exec 213 */ 214 #define KTR_EXEC_ARG 10 215 #define KTR_EXEC_ENV 11 216 /* record contains arg/env string */ 217 218 /* 219 * KTR_MOOL - Mach Out Of Line data 220 */ 221 #define KTR_MOOL 12 222 struct ktr_mool { 223 const void *uaddr; /* User address */ 224 size_t size; /* Data len */ 225 /* Followed by size bytes of data */ 226 }; 227 228 /* 229 * KTR_SAUPCALL - scheduler activated upcall. 230 */ 231 #define KTR_SAUPCALL 13 232 struct ktr_saupcall { 233 int ktr_type; 234 int ktr_nevent; 235 int ktr_nint; 236 void *ktr_sas; 237 void *ktr_ap; 238 /* 239 * followed by nevent sa_t's from sas[] 240 */ 241 }; 242 243 /* 244 * KTR_MIB - MIB name and data 245 */ 246 #define KTR_MIB 14 247 /* Record contains MIB name */ 248 249 250 /* 251 * kernel trace points (in p_traceflag) 252 */ 253 #define KTRFAC_MASK 0x00ffffff 254 #define KTRFAC_SYSCALL (1<<KTR_SYSCALL) 255 #define KTRFAC_SYSRET (1<<KTR_SYSRET) 256 #define KTRFAC_NAMEI (1<<KTR_NAMEI) 257 #define KTRFAC_GENIO (1<<KTR_GENIO) 258 #define KTRFAC_PSIG (1<<KTR_PSIG) 259 #define KTRFAC_CSW (1<<KTR_CSW) 260 #define KTRFAC_EMUL (1<<KTR_EMUL) 261 #define KTRFAC_USER (1<<KTR_USER) 262 #define KTRFAC_MMSG (1<<KTR_MMSG) 263 #define KTRFAC_EXEC_ARG (1<<KTR_EXEC_ARG) 264 #define KTRFAC_EXEC_ENV (1<<KTR_EXEC_ENV) 265 #define KTRFAC_MOOL (1<<KTR_MOOL) 266 #define KTRFAC_SAUPCALL (1<<KTR_SAUPCALL) 267 #define KTRFAC_MIB (1<<KTR_MIB) 268 /* 269 * trace flags (also in p_traceflags) 270 */ 271 #define KTRFAC_PERSISTENT 0x80000000 /* persistent trace across sugid 272 exec (exclusive) */ 273 #define KTRFAC_INHERIT 0x40000000 /* pass trace flags to children */ 274 #define KTRFAC_TRC_EMUL 0x10000000 /* ktrace KTR_EMUL before next trace */ 275 #define KTRFAC_VER_MASK 0x0f000000 /* record version mask */ 276 #define KTRFAC_VER_SHIFT 24 /* record version shift */ 277 278 #define KTRFAC_VERSION(tf) (((tf) & KTRFAC_VER_MASK) >> KTRFAC_VER_SHIFT) 279 280 #define KTRFACv0 (0 << KTRFAC_VER_SHIFT) 281 #define KTRFACv1 (1 << KTRFAC_VER_SHIFT) 282 #define KTRFACv2 (2 << KTRFAC_VER_SHIFT) 283 284 #ifndef _KERNEL 285 286 #include <sys/cdefs.h> 287 288 __BEGIN_DECLS 289 int ktrace(const char *, int, int, pid_t); 290 int fktrace(int, int, int, pid_t); 291 int utrace(const char *, void *, size_t); 292 __END_DECLS 293 294 #else 295 296 void ktrinit(void); 297 void ktrderef(struct proc *); 298 void ktradref(struct proc *); 299 300 extern kmutex_t ktrace_lock; 301 extern int ktrace_on; 302 303 int ktruser(const char *, void *, size_t, int); 304 bool ktr_point(int); 305 306 void ktr_csw(int, int); 307 void ktr_emul(void); 308 void ktr_geniov(int, enum uio_rw, struct iovec *, size_t, int); 309 void ktr_genio(int, enum uio_rw, const void *, size_t, int); 310 void ktr_mibio(int, enum uio_rw, const void *, size_t, int); 311 void ktr_namei(const char *, size_t); 312 void ktr_namei2(const char *, size_t, const char *, size_t); 313 void ktr_psig(int, sig_t, const sigset_t *, const ksiginfo_t *); 314 void ktr_syscall(register_t, const register_t [], int); 315 void ktr_sysret(register_t, int, register_t *); 316 void ktr_kuser(const char *, void *, size_t); 317 void ktr_mmsg(const void *, size_t); 318 void ktr_mib(const int *a , u_int b); 319 void ktr_mool(const void *, size_t, const void *); 320 void ktr_execarg(const void *, size_t); 321 void ktr_execenv(const void *, size_t); 322 void ktr_saupcall(struct lwp *, int, int, int, void *, void *, void *); 323 324 static inline bool 325 ktrpoint(int fac) 326 { 327 return __predict_false(ktrace_on) && __predict_false(ktr_point(1 << fac)); 328 } 329 330 static inline void 331 ktrcsw(int a, int b) 332 { 333 if (__predict_false(ktrace_on)) 334 ktr_csw(a, b); 335 } 336 337 static inline void 338 ktremul(void) 339 { 340 if (__predict_false(ktrace_on)) 341 ktr_emul(); 342 } 343 344 static inline void 345 ktrgenio(int a, enum uio_rw b, const void *c, size_t d, int e) 346 { 347 if (__predict_false(ktrace_on)) 348 ktr_genio(a, b, c, d, e); 349 } 350 351 static inline void 352 ktrgeniov(int a, enum uio_rw b, struct iovec *c, int d, int e) 353 { 354 if (__predict_false(ktrace_on)) 355 ktr_geniov(a, b, c, d, e); 356 } 357 358 static inline void 359 ktrmibio(int a, enum uio_rw b, const void *c, size_t d, int e) 360 { 361 if (__predict_false(ktrace_on)) 362 ktr_mibio(a, b, c, d, e); 363 } 364 365 static inline void 366 ktrnamei(const char *a, size_t b) 367 { 368 if (__predict_false(ktrace_on)) 369 ktr_namei(a, b); 370 } 371 372 static inline void 373 ktrnamei2(const char *a, size_t b, const char *c, size_t d) 374 { 375 if (__predict_false(ktrace_on)) 376 ktr_namei2(a, b, c, d); 377 } 378 379 static inline void 380 ktrpsig(int a, sig_t b, const sigset_t *c, const ksiginfo_t * d) 381 { 382 if (__predict_false(ktrace_on)) 383 ktr_psig(a, b, c, d); 384 } 385 386 static inline void 387 ktrsyscall(register_t code, const register_t args[], int narg) 388 { 389 if (__predict_false(ktrace_on)) 390 ktr_syscall(code, args, narg); 391 } 392 393 static inline void 394 ktrsysret(register_t a, int b, register_t *c) 395 { 396 if (__predict_false(ktrace_on)) 397 ktr_sysret(a, b, c); 398 } 399 400 static inline void 401 ktrkuser(const char *a, void *b, size_t c) 402 { 403 if (__predict_false(ktrace_on)) 404 ktr_kuser(a, b, c); 405 } 406 407 static inline void 408 ktrmmsg(const void *a, size_t b) 409 { 410 if (__predict_false(ktrace_on)) 411 ktr_mmsg(a, b); 412 } 413 414 static inline void 415 ktrmib(const int *a , u_int b) 416 { 417 if (__predict_false(ktrace_on)) 418 ktr_mib(a, b); 419 } 420 421 static inline void 422 ktrmool(const void *a, size_t b, const void *c) 423 { 424 if (__predict_false(ktrace_on)) 425 ktr_mool(a, b, c); 426 } 427 428 static inline void 429 ktrexecarg(const void *a, size_t b) 430 { 431 if (__predict_false(ktrace_on)) 432 ktr_execarg(a, b); 433 } 434 435 static inline void 436 ktrexecenv(const void *a, size_t b) 437 { 438 if (__predict_false(ktrace_on)) 439 ktr_execenv(a, b); 440 } 441 442 static inline void 443 ktrsaupcall(struct lwp *a, int b, int c, int d, void *e, void *f, void *g) 444 { 445 if (__predict_false(ktrace_on)) 446 ktr_saupcall(a, b, c, d, e, f, g); 447 } 448 449 #endif /* !_KERNEL */ 450 451 #endif /* _SYS_KTRACE_H_ */ 452