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