1/* 2 * Copyright (c) 1999-2007 Apple Inc. All rights reserved. 3 * 4 * @APPLE_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. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24#ifndef malloc_history_test_stack_logging_h 25#define malloc_history_test_stack_logging_h 26 27#import <malloc/malloc.h> 28 29#define stack_logging_type_free 0 30#define stack_logging_type_generic 1 /* anything that is not allocation/deallocation */ 31#define stack_logging_type_alloc 2 /* malloc, realloc, etc... */ 32#define stack_logging_type_dealloc 4 /* free, realloc, etc... */ 33 34// Following flags are absorbed by stack_logging_log_stack() 35#define stack_logging_flag_zone 8 /* NSZoneMalloc, etc... */ 36#define stack_logging_flag_calloc 16 /* multiply arguments to get the size */ 37#define stack_logging_flag_object 32 /* NSAllocateObject(Class, extraBytes, zone) */ 38#define stack_logging_flag_cleared 64 /* for NewEmptyHandle */ 39#define stack_logging_flag_handle 128 /* for Handle (de-)allocation routines */ 40#define stack_logging_flag_set_handle_size 256 /* (Handle, newSize) treated specially */ 41 42/* Macro used to disguise addresses so that leak finding can work */ 43#define STACK_LOGGING_DISGUISE(address) ((address) ^ 0x00005555) /* nicely idempotent */ 44 45extern "C" int stack_logging_enable_logging; /* when clear, no logging takes place */ 46extern "C" int stack_logging_dontcompact; /* default is to compact; when set does not compact alloc/free logs; useful for tracing history */ 47 48 49extern "C" void stack_logging_log_stack(unsigned type, unsigned arg1, unsigned arg2, unsigned arg3, unsigned result, unsigned num_hot_to_skip); 50/* This is the old log-to-memory logger, which is now deprecated. It remains for compatibility with performance tools that haven't been updated to disk_stack_logging_log_stack() yet. */ 51 52extern "C" void __disk_stack_logging_log_stack(uint32_t type_flags, uintptr_t zone_ptr, uintptr_t size, uintptr_t ptr_arg, uintptr_t return_val, uint32_t num_hot_to_skip); 53/* Fits as the malloc_logger; logs malloc/free/realloc events and can log custom events if called directly */ 54 55 56/* 64-bit-aware stack log access. */ 57typedef struct { 58 uint32_t type_flags; 59 uint64_t stack_identifier; 60 uint64_t argument; 61 mach_vm_address_t address; 62} mach_stack_logging_record_t; 63 64extern "C" kern_return_t __mach_stack_logging_get_frames(task_t task, mach_vm_address_t address, mach_vm_address_t *stack_frames_buffer, uint32_t max_stack_frames, uint32_t *count); 65/* Gets the last allocation record (malloc, realloc, or free) about address */ 66 67extern "C" kern_return_t __mach_stack_logging_enumerate_records(task_t task, mach_vm_address_t address, void enumerator(mach_stack_logging_record_t, void *), void *context); 68/* Applies enumerator to all records involving address sending context as enumerator's second parameter; if !address, applies enumerator to all records */ 69 70extern "C" kern_return_t __mach_stack_logging_frames_for_uniqued_stack(task_t task, uint64_t stack_identifier, mach_vm_address_t *stack_frames_buffer, uint32_t max_stack_frames, uint32_t *count); 71/* Given a uniqued_stack fills stack_frames_buffer */ 72 73 74#pragma mark - 75#pragma mark Legacy 76 77/* The following is the old 32-bit-only, in-process-memory stack logging. This is deprecated and clients should move to the above 64-bit-aware disk stack logging SPI. */ 78 79typedef struct { 80 unsigned type; 81 unsigned uniqued_stack; 82 unsigned argument; 83 unsigned address; /* disguised, to avoid confusing leaks */ 84} stack_logging_record_t; 85 86typedef struct { 87 unsigned overall_num_bytes; 88 unsigned num_records; 89 unsigned lock; /* 0 means OK to lock; used for inter-process locking */ 90 unsigned *uniquing_table; /* allocated using vm_allocate() */ 91 /* hashtable organized as (PC, uniqued parent) 92 Only the second half of the table is active 93 To enable us to grow dynamically */ 94 unsigned uniquing_table_num_pages; /* number of pages of the table */ 95 unsigned extra_retain_count; /* not used by stack_logging_log_stack */ 96 unsigned filler[2]; /* align to cache lines for better performance */ 97 stack_logging_record_t records[0]; /* records follow here */ 98} stack_logging_record_list_t; 99 100extern "C" stack_logging_record_list_t *stack_logging_the_record_list; 101/* This is the global variable containing all logs */ 102 103extern "C" kern_return_t stack_logging_get_frames(task_t task, memory_reader_t reader, vm_address_t address, vm_address_t *stack_frames_buffer, unsigned max_stack_frames, unsigned *num_frames); 104/* Gets the last record in stack_logging_the_record_list about address */ 105 106#define STACK_LOGGING_ENUMERATION_PROVIDED 1 // temporary to avoid dependencies between projects 107 108extern "C" kern_return_t stack_logging_enumerate_records(task_t task, memory_reader_t reader, vm_address_t address, void enumerator(stack_logging_record_t, void *), void *context); 109/* Gets all the records about address; 110 If !address, gets all records */ 111 112extern "C" kern_return_t stack_logging_frames_for_uniqued_stack(task_t task, memory_reader_t reader, unsigned uniqued_stack, vm_address_t *stack_frames_buffer, unsigned max_stack_frames, unsigned *num_frames); 113/* Given a uniqued_stack fills stack_frames_buffer */ 114 115 116 117extern "C" void thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *num); 118/* Convenience to fill buffer with the PCs of the frames, starting with the hot frames; 119 num: returned number of frames 120 */ 121 122#endif 123