]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kdebug.c
xnu-517.tar.gz
[apple/xnu.git] / bsd / kern / kdebug.c
index 82c594c8bc649e8985566bc73a6e345cd89c87c4..f9e4fdd5ea790ef38bca74ede9048b081531d2c5 100644 (file)
@@ -3,19 +3,22 @@
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (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.
+ * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 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.
+ * 
+ * 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 OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * 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.
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
@@ -25,6 +28,7 @@
 #define HZ      100
 #include <mach/clock_types.h>
 #include <mach/mach_types.h>
+#include <mach/mach_time.h>
 #include <machine/machine_routines.h>
 
 #include <sys/kdebug.h>
@@ -43,7 +47,7 @@
 unsigned int kdebug_enable = 0;
 
 /* track timestamps for security server's entropy needs */
-mach_timespec_t * kd_entropy_buffer = 0;
+uint64_t *               kd_entropy_buffer = 0;
 unsigned int      kd_entropy_bufsize = 0;
 unsigned int      kd_entropy_count  = 0;
 unsigned int      kd_entropy_indx   = 0;
@@ -94,7 +98,8 @@ struct kdebug_args {
 /* task to string structure */
 struct tts
 {
-  task_t   *task;
+  task_t   *task;            /* from procs task */
+  pid_t     pid;             /* from procs p_pid  */
   char      task_comm[20];   /* from procs p_comm */
 };
 
@@ -156,7 +161,7 @@ unsigned int debugid, arg1, arg2, arg3, arg4, arg5;
          {
            if (kd_entropy_indx < kd_entropy_count)
              {
-               ml_get_timebase((unsigned long long *) &kd_entropy_buffer [ kd_entropy_indx]);
+               kd_entropy_buffer [ kd_entropy_indx] = mach_absolute_time();
                kd_entropy_indx++;
              }
            
@@ -228,23 +233,17 @@ unsigned int debugid, arg1, arg2, arg3, arg4, arg5;
        kd->arg2 = arg2;
        kd->arg3 = arg3;
        kd->arg4 = arg4;
-       kd->arg5 = (int)current_thread();
+       kd->arg5 = (int)current_act();
         if (cpu_number())
             kd->arg5 |= KDBG_CPU_MASK;
                  
-       ml_get_timebase((unsigned long long *)&kd->timestamp);
+       now = kd->timestamp = mach_absolute_time();
 
        /* Watch for out of order timestamps */ 
-       now = (((unsigned long long)kd->timestamp.tv_sec) << 32) |
-         (unsigned long long)((unsigned int)(kd->timestamp.tv_nsec));
 
        if (now < kd_prev_timebase)
          {
-           /* timestamps are out of order -- adjust */
-           kd_prev_timebase++;
-           tsp = (mach_timespec_t *)&kd_prev_timebase;
-           kd->timestamp.tv_sec =  tsp->tv_sec;
-           kd->timestamp.tv_nsec = tsp->tv_nsec;
+           kd->timestamp = ++kd_prev_timebase;
          }
        else
          {
@@ -350,19 +349,14 @@ unsigned int debugid, arg1, arg2, arg3, arg4, arg5;
        kd->arg3 = arg3;
        kd->arg4 = arg4;
        kd->arg5 = arg5;
-       ml_get_timebase((unsigned long long *)&kd->timestamp);
+       now = kd->timestamp = mach_absolute_time();
 
        /* Watch for out of order timestamps */ 
-       now = (((unsigned long long)kd->timestamp.tv_sec) << 32) |
-         (unsigned long long)((unsigned int)(kd->timestamp.tv_nsec));
 
        if (now < kd_prev_timebase)
          {
            /* timestamps are out of order -- adjust */
-           kd_prev_timebase++;
-           tsp = (mach_timespec_t *)&kd_prev_timebase;
-           kd->timestamp.tv_sec =  tsp->tv_sec;
-           kd->timestamp.tv_nsec = tsp->tv_nsec;
+           kd->timestamp = ++kd_prev_timebase;
          }
        else
          {
@@ -418,11 +412,11 @@ kdbg_reinit()
     kdebug_nolog = 1;
 
     if ((kdebug_flags & KDBG_INIT) && (kdebug_flags & KDBG_BUFINIT) && kd_bufsize && kd_buffer)
-        kmem_free(kernel_map, (char *)kd_buffer, kd_bufsize);
+        kmem_free(kernel_map, (vm_offset_t)kd_buffer, kd_bufsize);
 
     if ((kdebug_flags & KDBG_MAPINIT) && kd_mapsize && kd_mapptr)
       {
-       kmem_free(kernel_map, (char *)kd_mapptr, kd_mapsize);
+       kmem_free(kernel_map, (vm_offset_t)kd_mapptr, kd_mapsize);
        kdebug_flags &= ~KDBG_MAPINIT;
        kd_mapsize = 0;
        kd_mapptr = (kd_threadmap *) 0;
@@ -434,6 +428,17 @@ kdbg_reinit()
     return(ret);
 }
 
+void kdbg_trace_data(struct proc *proc, long *arg_pid)
+{
+    if (!proc)
+        *arg_pid = 0;
+    else
+       *arg_pid = proc->p_pid;
+    
+    return;
+}
+
+
 void kdbg_trace_string(struct proc *proc, long *arg1, long *arg2, long *arg3, long *arg4)
 {
     int i;
@@ -481,11 +486,20 @@ kdbg_resolve_map(thread_act_t th_act, krt_t *t)
   if(t->count < t->maxcount)
     {
       mapptr=&t->map[t->count];
-      mapptr->thread  = (unsigned int)getshuttle_thread(th_act);
-      mapptr->valid = 1;
+      mapptr->thread  = (unsigned int)th_act;
       (void) strncpy (mapptr->command, t->atts->task_comm,
                      sizeof(t->atts->task_comm)-1);
       mapptr->command[sizeof(t->atts->task_comm)-1] = '\0';
+
+      /*
+       Some kernel threads have no associated pid.
+       We still need to mark the entry as valid.
+      */
+      if (t->atts->pid)
+         mapptr->valid = t->atts->pid;
+      else
+         mapptr->valid = 1;
+
       t->count++;
     }
 }
@@ -524,14 +538,20 @@ void kdbg_mapinit()
        kd_mapsize = kd_mapcount * sizeof(kd_threadmap);
        if((kmem_alloc(kernel_map, & kd_maptomem,
                       (vm_size_t)kd_mapsize) == KERN_SUCCESS))
+       {
            kd_mapptr = (kd_threadmap *) kd_maptomem;
+           bzero(kd_mapptr, kd_mapsize);
+       }
        else
            kd_mapptr = (kd_threadmap *) 0;
 
        tts_mapsize = tts_count * sizeof(struct tts);
        if((kmem_alloc(kernel_map, & tts_maptomem,
                       (vm_size_t)tts_mapsize) == KERN_SUCCESS))
+       {
            tts_mapptr = (struct tts *) tts_maptomem;
+           bzero(tts_mapptr, tts_mapsize);
+       }
        else
            tts_mapptr = (struct tts *) 0;
 
@@ -550,6 +570,7 @@ void kdbg_mapinit()
 
                        if (task_reference_try(p->task)) {
                                tts_mapptr[i].task = p->task;
+                               tts_mapptr[i].pid  = p->p_pid;
                                (void)strncpy(&tts_mapptr[i].task_comm, p->p_comm, sizeof(tts_mapptr[i].task_comm) - 1);
                                i++;
                        }
@@ -570,9 +591,9 @@ void kdbg_mapinit()
              {
                akrt.atts = &tts_mapptr[i];
                task_act_iterate_wth_args(tts_mapptr[i].task, kdbg_resolve_map, &akrt);
-               task_deallocate(tts_mapptr[i].task);
+               task_deallocate((task_t) tts_mapptr[i].task);
              }
-           kmem_free(kernel_map, (char *)tts_mapptr, tts_mapsize);
+           kmem_free(kernel_map, (vm_offset_t)tts_mapptr, tts_mapsize);
          }
 }
 
@@ -588,14 +609,14 @@ int x;
        kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
        kdebug_flags &= ~(KDBG_NOWRAP | KDBG_RANGECHECK | KDBG_VALCHECK);
        kdebug_flags &= ~(KDBG_PIDCHECK | KDBG_PIDEXCLUDE);
-       kmem_free(kernel_map, (char *)kd_buffer, kd_bufsize);
+       kmem_free(kernel_map, (vm_offset_t)kd_buffer, kd_bufsize);
        kd_buffer = (kd_buf *)0;
        kd_bufsize = 0;
        kd_prev_timebase = 0LL;
 
        /* Clean up the thread map buffer */
        kdebug_flags &= ~KDBG_MAPINIT;
-       kmem_free(kernel_map, (char *)kd_mapptr, kd_mapsize);
+       kmem_free(kernel_map, (vm_offset_t)kd_mapptr, kd_mapsize);
        kd_mapptr = (kd_threadmap *) 0;
        kd_mapsize = 0;
        kd_mapcount = 0;
@@ -816,7 +837,7 @@ kdbg_readmap(kd_threadmap *buffer, size_t *number)
 
   if ((kdebug_flags & KDBG_MAPINIT) && kd_mapsize && kd_mapptr)
     {
-      kmem_free(kernel_map, (char *)kd_mapptr, kd_mapsize);
+      kmem_free(kernel_map, (vm_offset_t)kd_mapptr, kd_mapsize);
       kdebug_flags &= ~KDBG_MAPINIT;
       kd_mapsize = 0;
       kd_mapptr = (kd_threadmap *) 0;
@@ -845,11 +866,11 @@ kdbg_getentropy (mach_timespec_t * buffer, size_t *number, int ms_timeout)
   if (kmem_alloc(kernel_map, &kd_entropy_buftomem,
                 (vm_size_t)kd_entropy_bufsize) == KERN_SUCCESS)
     {
-      kd_entropy_buffer = (mach_timespec_t *)kd_entropy_buftomem;
+      kd_entropy_buffer = (uint64_t *) kd_entropy_buftomem;
     }
   else
     {
-      kd_entropy_buffer = (mach_timespec_t *) 0;
+      kd_entropy_buffer = (uint64_t *) 0;
       kd_entropy_count = 0;
       kd_entropy_indx = 0;
       return (EINVAL);
@@ -882,8 +903,8 @@ kdbg_getentropy (mach_timespec_t * buffer, size_t *number, int ms_timeout)
   kd_entropy_count = 0;
   kd_entropy_indx = 0;
   kd_entropy_buftomem = 0;
-  kmem_free(kernel_map, (char *)kd_entropy_buffer, kd_entropy_bufsize);
-  kd_entropy_buffer = (mach_timespec_t *) 0;
+  kmem_free(kernel_map, (vm_offset_t)kd_entropy_buffer, kd_entropy_bufsize);
+  kd_entropy_buffer = (uint64_t *) 0;
   return(ret);
 }
 
@@ -1022,9 +1043,9 @@ struct proc *p, *curproc;
                      kdbg_mapinit();
                  break;
                case KERN_KDSETBUF:
-                 /* We allow a maximum buffer size of 25% of memory */
+                 /* We allow a maximum buffer size of 25% of either ram or max mapped address, whichever is smaller */
                  /* 'value' is the desired number of trace entries */
-                       max_entries = (mem_size/4) / sizeof(kd_buf);
+                       max_entries = (sane_size/4) / sizeof(kd_buf);
                        if (value <= max_entries)
                                nkdbufs = value;
                        else
@@ -1201,3 +1222,10 @@ kd_buf * my_kd_bufptr;
        } /* end if count */
        return (EINVAL);
 }
+
+unsigned char *getProcName(struct proc *proc);
+unsigned char *getProcName(struct proc *proc) {
+
+       return (unsigned char *) &proc->p_comm; /* Return pointer to the proc name */
+
+}