]> git.saurik.com Git - apple/libc.git/blobdiff - gen/stack_logging.c
Libc-391.5.22.tar.gz
[apple/libc.git] / gen / stack_logging.c
index 8389420c6f82a588894d02d8732a8b35b7fe5331..257d1180370670140dd9b221ee42a80af92a7e41 100644 (file)
@@ -3,19 +3,20 @@
  *
  * @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.
+ * 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.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 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@
  */
 #import <pthread.h>
 #import <mach/mach.h>
 #include <mach/vm_statistics.h>
+#import <malloc/malloc.h>
+#import <stdlib.h>
 
 extern void spin_lock(int *);
 
+static inline void *allocate_pages(unsigned) __attribute__((always_inline));
 static inline void *allocate_pages(unsigned bytes) {
     void *address;
     if (vm_allocate(mach_task_self(), (vm_address_t *)&address, bytes, 
                     VM_MAKE_TAG(VM_MEMORY_ANALYSIS_TOOL)| TRUE)) {
-       address = 0;
+       malloc_printf("*** out of memory while stack logging\n");
+       abort();
     } 
     return (void *)address;
 }
 
+static inline void deallocate_pages(void *, unsigned) __attribute__((always_inline));
 static inline void deallocate_pages(void *ptr, unsigned bytes) {
     vm_deallocate(mach_task_self(), (vm_address_t)ptr, bytes);
 }
 
+static inline void copy_pages(const void *, void *, unsigned) __attribute__((always_inline));
 static inline void copy_pages(const void *source, void *dest, unsigned bytes) {
     if (vm_copy(mach_task_self(), (vm_address_t)source, bytes, (vm_address_t)dest)) memmove(dest, source, bytes);
 }
 
 /***************       Recording stack         ***********/
 
-static void *first_frame_address(void) {
-#if 0
-    return __builtin_frame_address(1);
-#elif defined(__ppc__)
+// The three functions below are marked as noinline to ensure consistent inlining across
+// all versions of GCC and all compiler flags.  The malloc stack logging code expects
+// these functions to not be inlined.
+// For details, see <rdar://problem/4199620>.
+//
+// The performance cost of not inlining these functions is negligible, and they're only
+// called when MallocStackLogging is set anyway, so they won't affect normal usage.
+
+static __attribute__((noinline)) void *first_frame_address(void) {
+#if defined(__i386__) || defined(__x86_64__)
+    return __builtin_frame_address(0);
+#elif defined(__ppc__) || defined(__ppc64__)
     void *addr;
 #warning __builtin_frame_address IS BROKEN IN BEAKER: RADAR #2340421
     __asm__ volatile("mr %0, r1" : "=r" (addr));
@@ -64,11 +79,11 @@ static void *first_frame_address(void) {
 #endif
 }
 
-static void *next_frame_address(void *addr) {
+static __attribute__((noinline)) void *next_frame_address(void *addr) {
     void *ret;
-#if defined(__MACH__) && defined(__i386__)
-    __asm__ volatile("movl (%1),%0" : "=r" (ret) : "r" (addr));
-#elif defined(__MACH__) && defined(__ppc__)
+#if defined(__MACH__) && (defined(__i386__) || defined(__x86_64__))
+    __asm__ volatile("mov (%1),%0" : "=r" (ret) : "r" (addr));
+#elif defined(__MACH__) && (defined(__ppc__) || defined(__ppc64__))
     __asm__ volatile("lwz %0,0x0(%1)" : "=r" (ret) : "b" (addr));
 #elif defined(__hpux__)
     __asm__ volatile("ldw 0x0(%1),%0" : "=r" (ret) : "r" (addr));
@@ -81,9 +96,9 @@ static void *next_frame_address(void *addr) {
     return ret;
 }
 
-#if defined(__i386__) || defined (__m68k__)
+#if defined(__i386__) || defined(__x86_64__) || defined (__m68k__)
 #define FP_LINK_OFFSET 1
-#elif defined(__ppc__)
+#elif defined(__ppc__) || defined(__ppc64__)
 #define FP_LINK_OFFSET 2
 #elif defined(__hppa__)
 #define FP_LINK_OFFSET -5
@@ -93,7 +108,7 @@ static void *next_frame_address(void *addr) {
 #error  ********** Unimplemented architecture
 #endif
 
-void thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb) {
+__attribute__((noinline)) void thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb) {
     void *addr;
     addr = first_frame_address();
     *nb = 0;
@@ -103,7 +118,7 @@ void thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb) {
         buffer[*nb] = *((vm_address_t *)fp_link);
         (*nb)++;
         addr2 = next_frame_address(addr);
-#if defined(__ppc__)
+#if defined(__ppc__) || defined(__ppc64__)
         if ((unsigned)addr2 <= (unsigned)addr) break; // catch bozo frames
 #endif
         addr = addr2;