The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/libkern/gen/OSDebug.cpp

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
    3  *
    4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
    5  * 
    6  * This file contains Original Code and/or Modifications of Original Code
    7  * as defined in and that are subject to the Apple Public Source License
    8  * Version 2.0 (the 'License'). You may not use this file except in
    9  * compliance with the License. The rights granted to you under the License
   10  * may not be used to create, or enable the creation or redistribution of,
   11  * unlawful or unlicensed copies of an Apple operating system, or to
   12  * circumvent, violate, or enable the circumvention or violation of, any
   13  * terms of an Apple operating system software license agreement.
   14  * 
   15  * Please obtain a copy of the License at
   16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
   17  * 
   18  * The Original Code and all software distributed under the License are
   19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
   20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
   21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
   22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
   23  * Please see the License for the specific language governing rights and
   24  * limitations under the License.
   25  * 
   26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
   27  */
   28 
   29 // NOTE:  This file is only c++ so I can get static initialisers going
   30 #include <libkern/OSDebug.h>
   31 
   32 #include <sys/cdefs.h>
   33 
   34 #include <stdarg.h>
   35 #include <mach/mach_types.h>
   36 #include <mach/kmod.h>
   37 #include <kern/locks.h>
   38 
   39 #include <libkern/libkern.h>    // From bsd's libkern directory
   40 #include <mach/vm_param.h>
   41 
   42 #include <sys/kdebug.h>
   43 #include <kern/thread.h>
   44 
   45 extern int etext;
   46 __BEGIN_DECLS
   47 // From osmfk/kern/thread.h but considered to be private
   48 extern vm_offset_t min_valid_stack_address(void);
   49 extern vm_offset_t max_valid_stack_address(void);
   50 
   51 // From osfmk/kmod.c
   52 extern void kmod_dump_log(vm_offset_t *addr, unsigned int cnt);
   53 
   54 extern addr64_t kvtophys(vm_offset_t va);
   55 
   56 __END_DECLS
   57 
   58 extern lck_grp_t *IOLockGroup;
   59 
   60 static lck_mtx_t *sOSReportLock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL);
   61 
   62 /* Use kernel_debug() to log a backtrace */ 
   63 void
   64 trace_backtrace(uint32_t debugid, uint32_t debugid2, uintptr_t size, uintptr_t data) {
   65         void *bt[16];
   66         const unsigned cnt = sizeof(bt) / sizeof(bt[0]);
   67         unsigned i;
   68         int found = 0;
   69 
   70         OSBacktrace(bt, cnt);   
   71   
   72         /* find first non-kernel frame */
   73         for (i = 3; i < cnt && bt[i]; i++) {
   74                 if (bt[i] > (void*)&etext) {
   75                         found = 1;
   76                         break;
   77                 }
   78         }
   79         /* 
   80          * if there are non-kernel frames, only log these
   81          * otherwise, log everything but the first two
   82          */
   83         if (!found) i=2;
   84 
   85 #define safe_bt(a) (uintptr_t)(a<cnt ? bt[a] : 0)
   86         kernel_debug(debugid, data, size, safe_bt(i), safe_bt(i+1), 0);
   87         kernel_debug(debugid2, safe_bt(i+2), safe_bt(i+3), safe_bt(i+4), safe_bt(i+5), 0);
   88 }
   89 
   90 /* Report a message with a 4 entry backtrace - very slow */
   91 void
   92 OSReportWithBacktrace(const char *str, ...)
   93 {
   94     char buf[128];
   95     void *bt[9];
   96     const unsigned cnt = sizeof(bt) / sizeof(bt[0]);
   97     va_list listp;
   98 
   99     // Ignore the our and our callers stackframes, skipping frames 0 & 1
  100     (void) OSBacktrace(bt, cnt);
  101 
  102     va_start(listp, str);
  103     vsnprintf(buf, sizeof(buf), str, listp);
  104     va_end(listp);
  105 
  106     lck_mtx_lock(sOSReportLock);
  107     {
  108         printf("%s\nBacktrace %p %p %p %p %p %p %p\n",
  109             buf, bt[2], bt[3], bt[4], bt[5], bt[6], bt[7], bt[8]);
  110         kmod_dump_log((vm_offset_t *) &bt[2], cnt - 2);
  111     }
  112     lck_mtx_unlock(sOSReportLock);
  113 }
  114 
  115 static vm_offset_t minstackaddr = min_valid_stack_address();
  116 static vm_offset_t maxstackaddr = max_valid_stack_address();
  117 
  118 #if __i386__
  119 #define i386_RETURN_OFFSET 4
  120 
  121 static unsigned int
  122 i386_validate_stackptr(vm_offset_t stackptr)
  123 {
  124         /* Existence and alignment check
  125          */
  126         if (!stackptr || (stackptr & 0x3))
  127                 return 0;
  128   
  129         /* Is a virtual->physical translation present?
  130          */
  131         if (!kvtophys(stackptr))
  132                 return 0;
  133   
  134         /* Check if the return address lies on the same page;
  135          * If not, verify that a translation exists.
  136          */
  137         if (((PAGE_SIZE - (stackptr & PAGE_MASK)) < i386_RETURN_OFFSET) &&
  138             !kvtophys(stackptr + i386_RETURN_OFFSET))
  139                 return 0;
  140         return 1;
  141 }
  142 
  143 static unsigned int
  144 i386_validate_raddr(vm_offset_t raddr)
  145 {
  146         return ((raddr > VM_MIN_KERNEL_AND_KEXT_ADDRESS) &&
  147             (raddr < VM_MAX_KERNEL_ADDRESS));
  148 }
  149 #endif
  150 
  151 #if __x86_64__
  152 #define x86_64_RETURN_OFFSET 8
  153 static unsigned int
  154 x86_64_validate_raddr(vm_offset_t raddr)
  155 {
  156         return ((raddr > VM_MIN_KERNEL_AND_KEXT_ADDRESS) &&
  157             (raddr < VM_MAX_KERNEL_ADDRESS));
  158 }
  159 static unsigned int
  160 x86_64_validate_stackptr(vm_offset_t stackptr)
  161 {
  162         /* Existence and alignment check
  163          */
  164         if (!stackptr || (stackptr & 0x7) || !x86_64_validate_raddr(stackptr))
  165                 return 0;
  166   
  167         /* Is a virtual->physical translation present?
  168          */
  169         if (!kvtophys(stackptr))
  170                 return 0;
  171   
  172         /* Check if the return address lies on the same page;
  173          * If not, verify that a translation exists.
  174          */
  175         if (((PAGE_SIZE - (stackptr & PAGE_MASK)) < x86_64_RETURN_OFFSET) &&
  176             !kvtophys(stackptr + x86_64_RETURN_OFFSET))
  177                 return 0;
  178         return 1;
  179 }
  180 #endif
  181 
  182 
  183 unsigned OSBacktrace(void **bt, unsigned maxAddrs)
  184 {
  185     unsigned frame;
  186 
  187 #if __ppc__
  188     vm_offset_t stackptr, stackptr_prev;
  189     const vm_offset_t * const mem = (vm_offset_t *) 0;
  190     unsigned i = 0;
  191 
  192     __asm__ volatile("mflr %0" : "=r" (stackptr)); 
  193     bt[i++] = (void *) stackptr;
  194 
  195     __asm__ volatile("mr %0,r1" : "=r" (stackptr)); 
  196     for ( ; i < maxAddrs; i++) {
  197         // Validate we have a reasonable stackptr
  198         if ( !(minstackaddr <= stackptr && stackptr < maxstackaddr)
  199         || (stackptr & 3))
  200             break;
  201 
  202         stackptr_prev = stackptr;
  203         stackptr = mem[stackptr_prev >> 2];
  204         if ((stackptr - stackptr_prev) > 8 * 1024)      // Sanity check
  205             break;
  206 
  207         vm_offset_t addr = mem[(stackptr >> 2) + 2]; 
  208         if ((addr & 3) || (addr < 0x8000))      // More sanity checks
  209             break;
  210         bt[i] = (void *) addr;
  211     }
  212     frame = i;
  213 
  214     for ( ; i < maxAddrs; i++)
  215             bt[i] = (void *) 0;
  216 #elif __i386__
  217 #define SANE_i386_FRAME_SIZE (kernel_stack_size >> 1)
  218     vm_offset_t stackptr, stackptr_prev, raddr;
  219     unsigned frame_index = 0;
  220 /* Obtain current frame pointer */
  221     __asm__ volatile("movl %%ebp, %0" : "=m" (stackptr)); 
  222 
  223     if (!i386_validate_stackptr(stackptr))
  224             goto pad;
  225 
  226     raddr = *((vm_offset_t *) (stackptr + i386_RETURN_OFFSET));
  227 
  228     if (!i386_validate_raddr(raddr))
  229             goto pad;
  230 
  231     bt[frame_index++] = (void *) raddr;
  232 
  233     for ( ; frame_index < maxAddrs; frame_index++) {
  234             stackptr_prev = stackptr;
  235             stackptr = *((vm_offset_t *) stackptr_prev);
  236 
  237             if (!i386_validate_stackptr(stackptr))
  238                     break;
  239         /* Stack grows downwards */
  240             if (stackptr < stackptr_prev)
  241                     break;
  242 
  243             if ((stackptr - stackptr_prev) > SANE_i386_FRAME_SIZE)
  244                     break;
  245 
  246             raddr = *((vm_offset_t *) (stackptr + i386_RETURN_OFFSET));
  247 
  248             if (!i386_validate_raddr(raddr))
  249                     break;
  250 
  251             bt[frame_index] = (void *) raddr;
  252     }
  253 pad:
  254     frame = frame_index;
  255 
  256     for ( ; frame_index < maxAddrs; frame_index++)
  257             bt[frame_index] = (void *) 0;
  258 #elif __x86_64__
  259 #define SANE_x86_64_FRAME_SIZE (kernel_stack_size >> 1)
  260     vm_offset_t stackptr, stackptr_prev, raddr;
  261     unsigned frame_index = 0;
  262 /* Obtain current frame pointer */
  263 
  264     __asm__ volatile("movq %%rbp, %0" : "=m" (stackptr)); 
  265 
  266     if (!x86_64_validate_stackptr(stackptr))
  267             goto pad;
  268 
  269     raddr = *((vm_offset_t *) (stackptr + x86_64_RETURN_OFFSET));
  270 
  271     if (!x86_64_validate_raddr(raddr))
  272             goto pad;
  273 
  274     bt[frame_index++] = (void *) raddr;
  275 
  276     for ( ; frame_index < maxAddrs; frame_index++) {
  277             stackptr_prev = stackptr;
  278             stackptr = *((vm_offset_t *) stackptr_prev);
  279 
  280             if (!x86_64_validate_stackptr(stackptr))
  281                     break;
  282         /* Stack grows downwards */
  283             if (stackptr < stackptr_prev)
  284                     break;
  285 
  286             if ((stackptr - stackptr_prev) > SANE_x86_64_FRAME_SIZE)
  287                     break;
  288 
  289             raddr = *((vm_offset_t *) (stackptr + x86_64_RETURN_OFFSET));
  290 
  291             if (!x86_64_validate_raddr(raddr))
  292                     break;
  293 
  294             bt[frame_index] = (void *) raddr;
  295     }
  296 pad:
  297     frame = frame_index;
  298 
  299     for ( ; frame_index < maxAddrs; frame_index++)
  300             bt[frame_index] = (void *) 0;
  301 #else
  302 #error arch
  303 #endif
  304     return frame;
  305 }
  306 

Cache object: 3b884e73ece91566ca022b991e63f952


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.