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                                             \
31   1 /* anything that is not allocation/deallocation */
32 #define stack_logging_type_alloc 2   /* malloc, realloc, etc... */
33 #define stack_logging_type_dealloc 4 /* free, realloc, etc... */
34 
35 // Following flags are absorbed by stack_logging_log_stack()
36 #define stack_logging_flag_zone 8    /* NSZoneMalloc, etc... */
37 #define stack_logging_flag_calloc 16 /* multiply arguments to get the size */
38 #define stack_logging_flag_object                                              \
39   32 /* NSAllocateObject(Class, extraBytes, zone) */
40 #define stack_logging_flag_cleared 64 /* for NewEmptyHandle */
41 #define stack_logging_flag_handle 128 /* for Handle (de-)allocation routines   \
42                                          */
43 #define stack_logging_flag_set_handle_size                                     \
44   256 /* (Handle, newSize) treated specially */
45 
46 /* Macro used to disguise addresses so that leak finding can work */
47 #define STACK_LOGGING_DISGUISE(address)                                        \
48   ((address) ^ 0x00005555) /* nicely idempotent */
49 
50 extern "C" int
51     stack_logging_enable_logging; /* when clear, no logging takes place */
52 extern "C" int stack_logging_dontcompact; /* default is to compact; when set
53                                              does not compact alloc/free logs;
54                                              useful for tracing history */
55 
56 extern "C" void stack_logging_log_stack(unsigned type, unsigned arg1,
57                                         unsigned arg2, unsigned arg3,
58                                         unsigned result,
59                                         unsigned num_hot_to_skip);
60 /* This is the old log-to-memory logger, which is now deprecated.  It remains
61  * for compatibility with performance tools that haven't been updated to
62  * disk_stack_logging_log_stack() yet. */
63 
64 extern "C" void
65 __disk_stack_logging_log_stack(uint32_t type_flags, uintptr_t zone_ptr,
66                                uintptr_t size, uintptr_t ptr_arg,
67                                uintptr_t return_val, uint32_t num_hot_to_skip);
68 /* Fits as the malloc_logger; logs malloc/free/realloc events and can log custom
69  * events if called directly */
70 
71 /* 64-bit-aware stack log access. */
72 typedef struct {
73   uint32_t type_flags;
74   uint64_t stack_identifier;
75   uint64_t argument;
76   mach_vm_address_t address;
77 } mach_stack_logging_record_t;
78 
79 extern "C" kern_return_t
80 __mach_stack_logging_get_frames(task_t task, mach_vm_address_t address,
81                                 mach_vm_address_t *stack_frames_buffer,
82                                 uint32_t max_stack_frames, uint32_t *count);
83 /* Gets the last allocation record (malloc, realloc, or free) about address */
84 
85 extern "C" kern_return_t __mach_stack_logging_enumerate_records(
86     task_t task, mach_vm_address_t address,
87     void enumerator(mach_stack_logging_record_t, void *), void *context);
88 /* Applies enumerator to all records involving address sending context as
89  * enumerator's second parameter; if !address, applies enumerator to all records
90  */
91 
92 extern "C" kern_return_t __mach_stack_logging_frames_for_uniqued_stack(
93     task_t task, uint64_t stack_identifier,
94     mach_vm_address_t *stack_frames_buffer, uint32_t max_stack_frames,
95     uint32_t *count);
96 /* Given a uniqued_stack fills stack_frames_buffer */
97 
98 #pragma mark -
99 #pragma mark Legacy
100 
101 /* The following is the old 32-bit-only, in-process-memory stack logging.  This
102  * is deprecated and clients should move to the above 64-bit-aware disk stack
103  * logging SPI. */
104 
105 typedef struct {
106   unsigned type;
107   unsigned uniqued_stack;
108   unsigned argument;
109   unsigned address; /* disguised, to avoid confusing leaks */
110 } stack_logging_record_t;
111 
112 typedef struct {
113   unsigned overall_num_bytes;
114   unsigned num_records;
115   unsigned lock; /* 0 means OK to lock; used for inter-process locking */
116   unsigned *uniquing_table; /* allocated using vm_allocate() */
117   /* hashtable organized as (PC, uniqued parent)
118    Only the second half of the table is active
119    To enable us to grow dynamically */
120   unsigned uniquing_table_num_pages; /* number of pages of the table */
121   unsigned extra_retain_count;       /* not used by stack_logging_log_stack */
122   unsigned filler[2]; /* align to cache lines for better performance */
123   stack_logging_record_t records[0]; /* records follow here */
124 } stack_logging_record_list_t;
125 
126 extern "C" stack_logging_record_list_t *stack_logging_the_record_list;
127 /* This is the global variable containing all logs */
128 
129 extern "C" kern_return_t
130 stack_logging_get_frames(task_t task, memory_reader_t reader,
131                          vm_address_t address,
132                          vm_address_t *stack_frames_buffer,
133                          unsigned max_stack_frames, unsigned *num_frames);
134 /* Gets the last record in stack_logging_the_record_list about address */
135 
136 #define STACK_LOGGING_ENUMERATION_PROVIDED                                     \
137   1 // temporary to avoid dependencies between projects
138 
139 extern "C" kern_return_t stack_logging_enumerate_records(
140     task_t task, memory_reader_t reader, vm_address_t address,
141     void enumerator(stack_logging_record_t, void *), void *context);
142 /* Gets all the records about address;
143  If !address, gets all records */
144 
145 extern "C" kern_return_t stack_logging_frames_for_uniqued_stack(
146     task_t task, memory_reader_t reader, unsigned uniqued_stack,
147     vm_address_t *stack_frames_buffer, unsigned max_stack_frames,
148     unsigned *num_frames);
149 /* Given a uniqued_stack fills stack_frames_buffer */
150 
151 extern "C" void thread_stack_pcs(vm_address_t *buffer, unsigned max,
152                                  unsigned *num);
153 /* Convenience to fill buffer with the PCs of the frames, starting with the hot
154  frames;
155  num: returned number of frames
156  */
157 
158 #endif
159