fa.openbsd.tech
[Top] [All Lists]

useland ktrace bits 5/4

Subject: useland ktrace bits 5/4
From: Ted Unangst
Date: Wed, 28 Nov 2007 11:49:58 UTC
Newsgroups: fa.openbsd.tech

this is basic tool support for the kernel tracing.

Index: ktrace/ktrace.c
===================================================================
RCS file: /home/tedu/cvs/src/usr.bin/ktrace/ktrace.c,v
retrieving revision 1.20
diff -u -u -r1.20 ktrace.c
--- ktrace/ktrace.c     3 Mar 2007 23:46:55 -0000       1.20
+++ ktrace/ktrace.c     27 Nov 2007 12:06:07 -0000
@@ -68,17 +68,17 @@
 main(int argc, char *argv[])
 {
        enum { NOTSET, CLEAR, CLEARALL } clear;
-       int append, ch, fd, inherit, ops, pidset, trpoints;
+       int append, ch, fd, inherit, flags, ops, pidset, trpoints;
        pid_t pid;
        char *tracefile;
        mode_t omask;
        struct stat sb;
 
        clear = NOTSET;
-       append = ops = pidset = inherit = 0;
+       append = flags = ops = pidset = inherit = 0;
        trpoints = DEF_POINTS;
        tracefile = DEF_TRACEFILE;
-       while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1)
+       while ((ch = getopt(argc,argv,"aCcdf:g:ikp:t:")) != -1)
                switch((char)ch) {
                case 'a':
                        append = 1;
@@ -91,7 +91,7 @@
                        clear = CLEAR;
                        break;
                case 'd':
-                       ops |= KTRFLAG_DESCEND;
+                       flags |= KTRFLAG_DESCEND;
                        break;
                case 'f':
                        tracefile = optarg;
@@ -103,6 +103,9 @@
                case 'i':
                        inherit = 1;
                        break;
+               case 'k':
+                       ops = KTROP_KERN;
+                       break;
                case 'p':
                        pid = rpid(optarg);
                        pidset = 1;
@@ -120,7 +123,8 @@
        argv += optind;
        argc -= optind;
        
-       if ((pidset && *argv) || (!pidset && !*argv && clear != CLEAR))
+       if ((pidset && *argv) || (!pidset && !*argv && clear != CLEAR &&
+                   ops != KTROP_KERN))
                usage();
                        
        if (inherit)
@@ -129,12 +133,16 @@
        (void)signal(SIGSYS, no_ktrace);
        if (clear != NOTSET) {
                if (clear == CLEARALL) {
-                       ops = KTROP_CLEAR | KTRFLAG_DESCEND;
+                       if (ops)
+                               ops = KTROP_KERNCLEAR;
+                       else
+                               ops = KTROP_CLEAR | KTRFLAG_DESCEND;
                        trpoints = ALL_POINTS;
                        pid = 1;
-               } else
-                       ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
+               } else if (!ops)
+                       ops = pid ? KTROP_CLEAR : KTROP_CLEARFILE;
 
+               ops |= flags;
                if (ktrace(tracefile, ops, trpoints, pid) < 0)
                        err(1, "%s", tracefile);
                exit(0);
@@ -157,7 +165,8 @@
        (void)umask(omask);
        (void)close(fd);
 
-       if (*argv) { 
+       ops |= flags;
+       if (*argv) {
                if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
                        err(1, "%s", tracefile);
                execvp(argv[0], &argv[0]);
Index: ktrace/ktrace.h
===================================================================
RCS file: /home/tedu/cvs/src/usr.bin/ktrace/ktrace.h,v
retrieving revision 1.3
diff -u -u -r1.3 ktrace.h
--- ktrace/ktrace.h     3 Jun 2003 02:56:09 -0000       1.3
+++ ktrace/ktrace.h     27 Nov 2007 12:40:56 -0000
@@ -33,6 +33,6 @@
 #define DEF_POINTS (KTRFAC_SYSCALL | KTRFAC_SYSRET | KTRFAC_NAMEI | \
                  KTRFAC_GENIO | KTRFAC_PSIG | KTRFAC_EMUL)
 
-#define ALL_POINTS (DEF_POINTS | KTRFAC_CSW)
+#define ALL_POINTS (DEF_POINTS | KTRFAC_CSW | KTRFAC_KERN)
 
 #define DEF_TRACEFILE  "ktrace.out"
Index: kdump/kdump.c
===================================================================
RCS file: /home/tedu/cvs/src/usr.bin/kdump/kdump.c,v
retrieving revision 1.39
diff -u -u -r1.39 kdump.c
--- kdump/kdump.c       29 May 2007 02:01:03 -0000      1.39
+++ kdump/kdump.c       27 Nov 2007 15:07:55 -0000
@@ -159,6 +159,7 @@
 static void ktrpsig(struct ktr_psig *);
 static void ktrsyscall(struct ktr_syscall *);
 static void ktrsysret(struct ktr_sysret *);
+static void ktrkern(struct ktrkern_event *);
 static void setemul(const char *);
 static void usage(void);
 
@@ -230,6 +231,9 @@
                if (silent == 0 && trpoints & (1<<ktr_header.ktr_type))
                        dumpheader(&ktr_header);
                ktrlen = ktr_header.ktr_len;
+               if (ktr_header.ktr_type == KTR_KERN) {
+                       ktrlen = sizeof(struct ktrkern_event) - sizeof(struct 
ktr_header);
+               }
                if (ktrlen > size) {
                        void *newm;
 
@@ -247,26 +251,29 @@
                        continue;
                switch (ktr_header.ktr_type) {
                case KTR_SYSCALL:
-                       ktrsyscall((struct ktr_syscall *)m);
+                       ktrsyscall(m);
                        break;
                case KTR_SYSRET:
-                       ktrsysret((struct ktr_sysret *)m);
+                       ktrsysret(m);
                        break;
                case KTR_NAMEI:
                        ktrnamei(m, ktrlen);
                        break;
                case KTR_GENIO:
-                       ktrgenio((struct ktr_genio *)m, ktrlen);
+                       ktrgenio(m, ktrlen);
                        break;
                case KTR_PSIG:
-                       ktrpsig((struct ktr_psig *)m);
+                       ktrpsig(m);
                        break;
                case KTR_CSW:
-                       ktrcsw((struct ktr_csw *)m);
+                       ktrcsw(m);
                        break;
                case KTR_EMUL:
                        ktremul(m, ktrlen);
                        break;
+               case KTR_KERN:
+                       ktrkern((struct ktrkern_event *)((char *)m - 
sizeof(struct ktr_header)));
+                       break;
                }
                if (tail)
                        (void)fflush(stdout);
@@ -315,6 +322,9 @@
        case KTR_EMUL:
                type = "EMUL";
                break;
+       case KTR_KERN:
+               type = "KERN";
+               break;
        default:
                (void)snprintf(unknown, sizeof unknown, "UNKNOWN(%d)",
                    kth->ktr_type);
@@ -454,6 +464,30 @@
                }
        }
        (void)printf(")\n");
+}
+
+static void
+ktrkern(struct ktrkern_event *kev)
+{
+       char *ev = NULL;
+
+       switch (kev->ktr_event) {
+       case 0:
+               ev = "sched off";
+       case 1:
+               if (!ev)
+                       ev = "sched on";
+               printf(" cpu %d %s", (int)kev->ktr_data, ev);
+               break;
+       case 2:
+               printf(" workq %p", kev->ktr_data);
+               break;
+       default:
+               printf(" unknown");
+               break;
+       }
+       printf("\n");
+
 }
 
 static void


<Prev in Thread] Current Thread [Next in Thread>
  • useland ktrace bits 5/4, Ted Unangst <=