]> git.saurik.com Git - apple/system_cmds.git/blobdiff - sc_usage.tproj/sc_usage.c
system_cmds-336.20.tar.gz
[apple/system_cmds.git] / sc_usage.tproj / sc_usage.c
index 93d7ca2474bd548ecef82b47016ae464ab55e83d..6345efcd6d4060419ab461b1627a3565ff9c4bb6 100644 (file)
@@ -3,28 +3,27 @@
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
- * 
- * This file contains Original Code and/or Modifications of Original Code
- * as defined in and that are subject to the Apple Public Source License
- * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
+ * "Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
+ * Reserved.  This file contains Original Code and/or Modifications of
+ * Original Code as defined in and that are subject to the Apple Public
+ * Source License Version 1.0 (the 'License').  You may not use this file
+ * except in compliance with the License.  Please obtain a copy of the
+ * License at http://www.apple.com/publicsource and read it before using
+ * this file.
  * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights and
- * limitations under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
+ * License for the specific language governing rights and limitations
+ * under the License."
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
 
 /*
-cc -I. -DKERNEL_PRIVATE -O -o sc_usage sc_usage.c -lncurses
+cc -I. -DPRIVATE -D__APPLE_PRIVATE -O -o sc_usage sc_usage.c -lncurses
 */
 
 #define        Default_DELAY   1       /* default delay interval */
@@ -40,6 +39,7 @@ cc -I. -DKERNEL_PRIVATE -O -o sc_usage sc_usage.c -lncurses
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/time.h>
+#include <sys/ptrace.h>
 
 #include <libc.h>
 #include <termios.h>
@@ -90,9 +90,9 @@ long   start_time = 0;
 #define MAX_NESTED  8
 #define MAX_FAULTS  5
 
-/* If NUMPARMS from kernel changes, it will be reflected in PATHLENGTH as well */
+
 #define NUMPARMS 23
-#define PATHLENGTH (NUMPARMS*sizeof(long))
+
 
 char *state_name[] = {
         "Dont Know",
@@ -124,7 +124,7 @@ struct th_info {
         int  vfslookup;
         int  curpri;
         long *pathptr;
-        char pathname[PATHLENGTH + 1];
+        long pathname[NUMPARMS + 1];
         struct entry th_entry[MAX_NESTED];
 };
 
@@ -213,6 +213,9 @@ void set_pidcheck();
 void set_remove();
 void set_numbufs();
 void set_init();
+void quit(char *);
+int argtopid(char *);
+int argtoi(int, char*, char*, int);
 
 
 /*
@@ -261,7 +264,7 @@ void sigwinch()
 }
 
 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");
@@ -287,19 +290,19 @@ void print_time(char *p, unsigned int useconds, unsigned int seconds)
        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
-               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);
        }
 }
 
-
+int
 main(argc, argv)
        int     argc;
        char    *argv[];
@@ -309,15 +312,11 @@ main(argc, argv)
        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();
-       int argtopid();
-       int argtoi();
-       int quit();
 
        if ( geteuid() != 0 ) {
              printf("'sc_usage' must be run as root...\n");
@@ -402,7 +401,7 @@ main(argc, argv)
                    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);
@@ -457,7 +456,7 @@ main(argc, argv)
        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)
@@ -472,7 +471,6 @@ main(argc, argv)
 
        while (1) {
                int     i;
-               int     cnt;
                char    c;
                void    sample_sc();
                
@@ -652,7 +650,7 @@ void screen_update()
 
        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';
@@ -844,6 +842,7 @@ void screen_update()
                
        for (i = 0; i < num_of_threads; i++, ti++) {
                struct entry *te;
+               char    *p;
                uint64_t now;
                int      secs, time_secs, time_usecs;
 
@@ -871,12 +870,14 @@ void screen_update()
                clen = strlen(tbuf);
                
                /* print the tail end of the pathname */
-               plen = strlen(ti->pathname);
+               p = (char *)ti->pathname;
+
+               plen = strlen(p);
                if (plen > 34)
                  plen -= 34;
                else
                  plen = 0;
-               sprintf(&tbuf[clen], "   %-34.34s ", &ti->pathname[plen]);
+               sprintf(&tbuf[clen], "   %-34.34s ", &p[plen]);
 
                clen += strlen(&tbuf[clen]);
 
@@ -981,7 +982,6 @@ sc_tab_init(char *codefile) {
        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);
@@ -1181,7 +1181,7 @@ sort_scalls() {
                                if ((unsigned long)(((double)now - te->otime) / divisor) > 5000000) {
                                        ti->thread = 0;
                                        ti->vfslookup = 0;
-                                       ti->pathptr = (long *)0;
+                                       ti->pathptr = (long *)NULL;
                                        ti->pathname[0] = 0;
                                        num_of_threads--;
                                }
@@ -1384,7 +1384,7 @@ sample_sc()
                        th_state[i].depth = 0;
                        th_state[i].thread = 0;
                        th_state[i].vfslookup = 0;
-                       th_state[i].pathptr = (long *)0;
+                       th_state[i].pathptr = (long *)NULL;
                        th_state[i].pathname[0] = 0;
                }
                num_of_threads = 0;
@@ -1405,7 +1405,7 @@ sample_sc()
                struct sc_entry *se;
                struct entry *te;
 
-               thread  = kd[i].arg5 & KDBG_THREAD_MASK;
+               thread  = kd[i].arg5;
                debugid = kd[i].debugid;
                type    = kd[i].debugid & DBG_FUNC_MASK;
 
@@ -1413,8 +1413,8 @@ sample_sc()
                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 & KDBG_TIMESTAMP_MASK;
+               
                baseid = debugid & 0xffff0000;
 
                if (type == vfs_lookup) {
@@ -1425,12 +1425,16 @@ sample_sc()
 
                        if (ti->vfslookup == 1) {
                                ti->vfslookup++;
-                               memset(&ti->pathname[0], 0, (PATHLENGTH + 1));
-                               sargptr = (long *)&ti->pathname[0];
+                               sargptr = ti->pathname;
 
                                *sargptr++ = kd[i].arg2;
                                *sargptr++ = kd[i].arg3;
                                *sargptr++ = kd[i].arg4;
+                               /*
+                                * NULL terminate the 'string'
+                                */
+                               *sargptr = 0;
+
                                ti->pathptr = sargptr;
 
                        } else if (ti->vfslookup > 1) {
@@ -1443,7 +1447,7 @@ sample_sc()
                                  handle.
                                */
 
-                               if ((long *)sargptr >= (long *)&ti->pathname[PATHLENGTH])
+                               if (sargptr >= &ti->pathname[NUMPARMS])
                                        continue;
 
                                /*
@@ -1455,7 +1459,7 @@ sample_sc()
 
                                if (debugid & DBG_FUNC_START)
                                  {
-                                   (long *)ti->pathptr = (long *)&ti->pathname[PATHLENGTH];
+                                   ti->pathptr = &ti->pathname[NUMPARMS];
                                    continue;
                                  }
 
@@ -1463,6 +1467,11 @@ sample_sc()
                                *sargptr++ = kd[i].arg2;
                                *sargptr++ = kd[i].arg3;
                                *sargptr++ = kd[i].arg4;
+                               /*
+                                * NULL terminate the 'string'
+                                */
+                               *sargptr = 0;
+
                                ti->pathptr = sargptr;
                        }
                        continue;
@@ -1471,8 +1480,8 @@ sample_sc()
                        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);
                        switched_in  = find_thread(kd[i].arg2);
 
                        if (in_idle) {
@@ -1685,9 +1694,8 @@ sample_sc()
        delta_otime_secs += secs;
 }
 
-
-quit(s)
-char *s;
+void
+quit(char *s)
 {
         if (trace_enabled)
                set_enable(0);
@@ -1717,6 +1725,7 @@ void getdivisor()
 }
 
 
+int
 argtopid(str)
         char *str;
 {
@@ -1774,7 +1783,8 @@ find_msgcode(int debugid)
     }
   return (0);
 }
-  
+
+int
 argtoi(flag, req, str, base)
        int flag;
        char *req, *str;