]> git.saurik.com Git - apple/system_cmds.git/blobdiff - sc_usage.tproj/sc_usage.c
system_cmds-279.1.tar.gz
[apple/system_cmds.git] / sc_usage.tproj / sc_usage.c
index 5198d9aeadbba17d4bda465472d4a51f951a5070..adab8fab5e097bd92d3e85228e466739e15635a2 100644 (file)
@@ -23,7 +23,7 @@
  */
 
 /*
  */
 
 /*
-cc -I. -DKERNEL_PRIVATE -O -o sc_usage sc_usage.c
+cc -I. -DKERNEL_PRIVATE -O -o sc_usage sc_usage.c -lncurses
 */
 
 #define        Default_DELAY   1       /* default delay interval */
 */
 
 #define        Default_DELAY   1       /* default delay interval */
@@ -39,10 +39,11 @@ cc -I. -DKERNEL_PRIVATE -O -o sc_usage sc_usage.c
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/time.h>
+#include <sys/ptrace.h>
 
 #include <libc.h>
 #include <termios.h>
 
 #include <libc.h>
 #include <termios.h>
-#include <bsd/curses.h>
+#include <curses.h>
 
 #include <sys/ioctl.h>
 
 
 #include <sys/ioctl.h>
 
@@ -192,7 +193,6 @@ int    scalls;
 #define DIVISOR 16.6666        /* Trace divisor converts to microseconds */
 double divisor = DIVISOR;
 
 #define DIVISOR 16.6666        /* Trace divisor converts to microseconds */
 double divisor = DIVISOR;
 
-struct termios tmode, omode;
 
 int mib[6];
 size_t needed;
 
 int mib[6];
 size_t needed;
@@ -213,6 +213,9 @@ void set_pidcheck();
 void set_remove();
 void set_numbufs();
 void set_init();
 void set_remove();
 void set_numbufs();
 void set_init();
+void quit(char *);
+int argtopid(char *);
+int argtoi(int, char*, char*, int);
 
 
 /*
 
 
 /*
@@ -226,8 +229,6 @@ void leave()                        /* exit under normal conditions -- INT handler */
                move(LINES - 1, 0);
                refresh();
                endwin();
                move(LINES - 1, 0);
                refresh();
                endwin();
-
-               tcsetattr(0, TCSANOW, &omode);
        }
        set_enable(0);
        set_pidcheck(pid, 0);
        }
        set_enable(0);
        set_pidcheck(pid, 0);
@@ -243,8 +244,6 @@ char *s;
                move(LINES - 1, 0);
                refresh();
                endwin();
                move(LINES - 1, 0);
                refresh();
                endwin();
-
-               tcsetattr(0, TCSANOW, &omode);
        }
 
         printf("sc_usage: ");
        }
 
         printf("sc_usage: ");
@@ -265,7 +264,7 @@ void sigwinch()
 }
 
 int
 }
 
 int
-exit_usage(myname) {
+exit_usage(char *myname) {
 
         fprintf(stderr, "Usage: %s [-c codefile] [-e] [-l] [-sn] pid | cmd | -E execute path\n", myname);
        fprintf(stderr, "  -c         name of codefile containing mappings for syscalls\n");
 
         fprintf(stderr, "Usage: %s [-c codefile] [-e] [-l] [-sn] pid | cmd | -E execute path\n", myname);
        fprintf(stderr, "  -c         name of codefile containing mappings for syscalls\n");
@@ -291,19 +290,19 @@ void print_time(char *p, unsigned int useconds, unsigned int seconds)
        hours = minutes / 60;
 
        if (minutes < 100) { // up to 100 minutes
        hours = minutes / 60;
 
        if (minutes < 100) { // up to 100 minutes
-               sprintf(p, "%2ld:%02ld.%03ld", minutes, seconds % 60,
-                       usec_to_1000ths(useconds));
+               sprintf(p, "%2ld:%02ld.%03ld", minutes, (unsigned long)(seconds % 60),
+                       (unsigned long)usec_to_1000ths(useconds));
        }
        else if (hours < 100) { // up to 100 hours
        }
        else if (hours < 100) { // up to 100 hours
-               sprintf(p, "%2ld:%02ld:%02ld ", hours, minutes % 60,
-                               seconds % 60);
+               sprintf(p, "%2ld:%02ld:%02ld ", hours, (minutes % 60),
+                               (unsigned long)(seconds % 60));
        }
        else {
                sprintf(p, "%4ld hrs ", hours);
        }
 }
 
        }
        else {
                sprintf(p, "%4ld hrs ", hours);
        }
 }
 
-
+int
 main(argc, argv)
        int     argc;
        char    *argv[];
 main(argc, argv)
        int     argc;
        char    *argv[];
@@ -313,15 +312,11 @@ main(argc, argv)
        char    ch;
        char    *ptr;
        int     delay = Default_DELAY;
        char    ch;
        char    *ptr;
        int     delay = Default_DELAY;
-       int     length;
         void screen_update();
        void sort_scalls();
        void sc_tab_init();
        void getdivisor();
        void reset_counters();
         void screen_update();
        void sort_scalls();
        void sc_tab_init();
        void getdivisor();
        void reset_counters();
-       int argtopid();
-       int argtoi();
-       int quit();
 
        if ( geteuid() != 0 ) {
              printf("'sc_usage' must be run as root...\n");
 
        if ( geteuid() != 0 ) {
              printf("'sc_usage' must be run as root...\n");
@@ -406,7 +401,7 @@ main(argc, argv)
                    exit(1);
                  case 0: /* child */
                    setsid();
                    exit(1);
                  case 0: /* child */
                    setsid();
-                   ptrace(0,0,0,0); /* PT_TRACE_ME */
+                   ptrace(0,(pid_t)0,(caddr_t)0,0); /* PT_TRACE_ME */
                    execve(argv[optind], &argv[optind], environ);
                    perror("execve:");
                    exit(1);
                    execve(argv[optind], &argv[optind], environ);
                    perror("execve:");
                    exit(1);
@@ -423,22 +418,17 @@ main(argc, argv)
 
 
        if (no_screen_refresh == 0) {
 
 
        if (no_screen_refresh == 0) {
-               if (tcgetattr(0, &tmode) < 0) {
-                       printf("can't get terminal attributes\n");
-                       exit(1);
-               }
-               omode = tmode;
-       
-               tmode.c_lflag &= ~ICANON;
-               tmode.c_cc[VMIN] = 0;
-               tmode.c_cc[VTIME] = 1;
 
 
-               if (tcsetattr(0, TCSANOW, &tmode) < 0) {
-                       printf("can't set terminal attributes\n");
-                       exit(1);
-               }
                /* initializes curses and screen (last) */
                /* initializes curses and screen (last) */
-               initscr();
+               if (initscr() == (WINDOW *) 0)
+                 {
+                   printf("Unrecognized TERM type, try vt100\n");
+                   exit(1);
+                 }
+               cbreak();
+               timeout(100);
+               noecho();
+
                clear();
                refresh();
        }
                clear();
                refresh();
        }
@@ -447,6 +437,8 @@ main(argc, argv)
        /* set up signal handlers */
        signal(SIGINT, leave);
        signal(SIGQUIT, leave);
        /* set up signal handlers */
        signal(SIGINT, leave);
        signal(SIGQUIT, leave);
+        signal(SIGHUP, leave);
+        signal(SIGTERM, leave);
        signal(SIGWINCH, sigwinch);
 
         if (no_screen_refresh == 0)
        signal(SIGWINCH, sigwinch);
 
         if (no_screen_refresh == 0)
@@ -464,7 +456,7 @@ main(argc, argv)
        set_pidcheck(pid, 1);
        set_enable(1);
        if (execute_flag)
        set_pidcheck(pid, 1);
        set_enable(1);
        if (execute_flag)
-         ptrace(7, pid, 1, 0);  /* PT_CONTINUE */
+         ptrace(7, pid, (caddr_t)1, 0);  /* PT_CONTINUE */
        getdivisor();
 
        if (delay == 0)
        getdivisor();
 
        if (delay == 0)
@@ -479,22 +471,16 @@ main(argc, argv)
 
        while (1) {
                int     i;
 
        while (1) {
                int     i;
-               int     cnt;
-               char    ibuf[128];
+               char    c;
                void    sample_sc();
                
                for (i = 0; i < (10 * delay) && newLINES == 0; i++) {
 
                        if (no_screen_refresh == 0) {
                void    sample_sc();
                
                for (i = 0; i < (10 * delay) && newLINES == 0; i++) {
 
                        if (no_screen_refresh == 0) {
-                               if ((cnt = read(0, &ibuf, 128)) > 0) {
-                                       int   n;
-
-                                       for (n = 0; n < cnt; n++)
-                                               if (ibuf[n] == 'q')
-                                                       leave();
-                                       reset_counters();
-                                       break;
-                               }
+                               if ((c = getch()) != ERR && (char)c == 'q') 
+                                       leave();
+                               if (c != ERR)
+                                       reset_counters();
                        } else
                                usleep(100000);
                        sample_sc();
                        } else
                                usleep(100000);
                        sample_sc();
@@ -502,7 +488,11 @@ main(argc, argv)
                (void)sort_scalls();
 
                if (newLINES) {
                (void)sort_scalls();
 
                if (newLINES) {
-                       initscr();
+                       /*
+                         No need to check for initscr error return.
+                         We won't get here if it fails on the first call.
+                       */
+                       endwin();
                        clear();
                        refresh();
 
                        clear();
                        refresh();
 
@@ -660,7 +650,7 @@ void screen_update()
 
        clen = strlen(tbuf);
        sprintf(&tbuf[clen], "                    %3ld:%02ld:%02ld\n", 
 
        clen = strlen(tbuf);
        sprintf(&tbuf[clen], "                    %3ld:%02ld:%02ld\n", 
-               hours, minutes % 60, elapsed_secs % 60);
+               (long)hours, (long)(minutes % 60), (long)(elapsed_secs % 60));
 
        if (tbuf[COLS-2] != '\n') {
                tbuf[COLS-1] = '\n';
 
        if (tbuf[COLS-2] != '\n') {
                tbuf[COLS-1] = '\n';
@@ -989,7 +979,6 @@ sc_tab_init(char *codefile) {
        int msgcode_indx=0;
        char name[56];
         FILE *fp;
        int msgcode_indx=0;
        char name[56];
         FILE *fp;
-       int quit();
 
        if ((fp = fopen(codefile,"r")) == (FILE *)0) {
                printf("Failed to open code description file %s\n", codefile);
 
        if ((fp = fopen(codefile,"r")) == (FILE *)0) {
                printf("Failed to open code description file %s\n", codefile);
@@ -1421,15 +1410,16 @@ sample_sc()
                switched_out = (struct th_info *)0;
                switched_in  = (struct th_info *)0;
 
                switched_out = (struct th_info *)0;
                switched_in  = (struct th_info *)0;
 
-               now = (((uint64_t)kd[i].timestamp.tv_sec) << 32) |
-                       (uint64_t)((unsigned int)(kd[i].timestamp.tv_nsec));
+               now = kd[i].timestamp;
+               
                baseid = debugid & 0xffff0000;
 
                baseid = debugid & 0xffff0000;
 
-               if (debugid == vfs_lookup) {
+               if (type == vfs_lookup) {
                        long *sargptr;
 
                        if ((ti = find_thread(thread)) == (struct th_info *)0)
                                continue;
                        long *sargptr;
 
                        if ((ti = find_thread(thread)) == (struct th_info *)0)
                                continue;
+
                        if (ti->vfslookup == 1) {
                                ti->vfslookup++;
                                memset(&ti->pathname[0], 0, (PATHLENGTH + 1));
                        if (ti->vfslookup == 1) {
                                ti->vfslookup++;
                                memset(&ti->pathname[0], 0, (PATHLENGTH + 1));
@@ -1451,7 +1441,21 @@ sample_sc()
                                */
 
                                if ((long *)sargptr >= (long *)&ti->pathname[PATHLENGTH])
                                */
 
                                if ((long *)sargptr >= (long *)&ti->pathname[PATHLENGTH])
-                                 continue;
+                                       continue;
+
+                               /*
+                                 We need to detect consecutive vfslookup entries.
+                                 So, if we get here and find a START entry,
+                                 fake the pathptr so we can bypass all further
+                                 vfslookup entries.
+                               */
+
+                               if (debugid & DBG_FUNC_START)
+                                 {
+                                   (long *)ti->pathptr = (long *)&ti->pathname[PATHLENGTH];
+                                   continue;
+                                 }
+
                                *sargptr++ = kd[i].arg1;
                                *sargptr++ = kd[i].arg2;
                                *sargptr++ = kd[i].arg3;
                                *sargptr++ = kd[i].arg1;
                                *sargptr++ = kd[i].arg2;
                                *sargptr++ = kd[i].arg3;
@@ -1464,8 +1468,8 @@ sample_sc()
                        code = (debugid >> 2) & 0x1ff;
                else if (baseid == msc_base)
                        code = 512 + ((debugid >> 2) & 0x1ff);
                        code = (debugid >> 2) & 0x1ff;
                else if (baseid == msc_base)
                        code = 512 + ((debugid >> 2) & 0x1ff);
-               else if (baseid == mach_sched || baseid == mach_stkhandoff) {
-                       switched_out = find_thread(kd[i].arg1);
+               else if (type == mach_sched || type == mach_stkhandoff) {
+                       switched_out = find_thread((kd[i].arg5 & KDBG_THREAD_MASK));
                        switched_in  = find_thread(kd[i].arg2);
 
                        if (in_idle) {
                        switched_in  = find_thread(kd[i].arg2);
 
                        if (in_idle) {
@@ -1678,9 +1682,8 @@ sample_sc()
        delta_otime_secs += secs;
 }
 
        delta_otime_secs += secs;
 }
 
-
-quit(s)
-char *s;
+void
+quit(char *s)
 {
         if (trace_enabled)
                set_enable(0);
 {
         if (trace_enabled)
                set_enable(0);
@@ -1710,6 +1713,7 @@ void getdivisor()
 }
 
 
 }
 
 
+int
 argtopid(str)
         char *str;
 {
 argtopid(str)
         char *str;
 {
@@ -1767,7 +1771,8 @@ find_msgcode(int debugid)
     }
   return (0);
 }
     }
   return (0);
 }
-  
+
+int
 argtoi(flag, req, str, base)
        int flag;
        char *req, *str;
 argtoi(flag, req, str, base)
        int flag;
        char *req, *str;