79
79
// C++ includes
80
80
#include < vector>
81
81
82
- // ----------------------------------------------------------------------
83
82
// Redefine private types from "/usr/local/include/stack_logging.h"
84
- // ----------------------------------------------------------------------
85
83
typedef struct {
86
84
uint32_t type_flags;
87
85
uint64_t stack_identifier;
88
86
uint64_t argument;
89
87
mach_vm_address_t address;
90
88
} mach_stack_logging_record_t ;
91
89
92
- // ----------------------------------------------------------------------
93
90
// Redefine private defines from "/usr/local/include/stack_logging.h"
94
- // ----------------------------------------------------------------------
95
91
#define stack_logging_type_free 0
96
92
#define stack_logging_type_generic 1
97
93
#define stack_logging_type_alloc 2
98
94
#define stack_logging_type_dealloc 4
99
95
// This bit is made up by this code
100
96
#define stack_logging_type_vm_region 8
101
97
102
- // ----------------------------------------------------------------------
103
98
// Redefine private function prototypes from
104
99
// "/usr/local/include/stack_logging.h"
105
- // ----------------------------------------------------------------------
106
100
extern " C" kern_return_t __mach_stack_logging_set_file_path (task_t task,
107
101
char *file_path);
108
102
@@ -125,21 +119,15 @@ extern "C" void *gdb_class_getClass(void *objc_class);
125
119
static void range_info_callback (task_t task, void *baton, unsigned type,
126
120
uint64_t ptr_addr, uint64_t ptr_size);
127
121
128
- // ----------------------------------------------------------------------
129
122
// Redefine private global variables prototypes from
130
123
// "/usr/local/include/stack_logging.h"
131
- // ----------------------------------------------------------------------
132
124
133
125
extern " C" int stack_logging_enable_logging;
134
126
135
- // ----------------------------------------------------------------------
136
127
// Local defines
137
- // ----------------------------------------------------------------------
138
128
#define MAX_FRAMES 1024
139
129
140
- // ----------------------------------------------------------------------
141
130
// Local Typedefs and Types
142
- // ----------------------------------------------------------------------
143
131
typedef void range_callback_t (task_t task, void *baton, unsigned type,
144
132
uint64_t ptr_addr, uint64_t ptr_size);
145
133
typedef void zone_callback_t (void *info, const malloc_zone_t *zone);
@@ -294,10 +282,8 @@ class MallocStackLoggingEntries {
294
282
uint32_t m_size;
295
283
};
296
284
297
- // ----------------------------------------------------------------------
298
285
// A safe way to allocate memory and keep it from interfering with the
299
286
// malloc enumerators.
300
- // ----------------------------------------------------------------------
301
287
void *safe_malloc (size_t n_bytes) {
302
288
if (n_bytes > 0 ) {
303
289
const int k_page_size = getpagesize ();
@@ -311,9 +297,7 @@ void *safe_malloc(size_t n_bytes) {
311
297
return NULL ;
312
298
}
313
299
314
- // ----------------------------------------------------------------------
315
300
// ObjCClasses
316
- // ----------------------------------------------------------------------
317
301
class ObjCClasses {
318
302
public:
319
303
ObjCClasses () : m_objc_class_ptrs(NULL ), m_size(0 ) {}
@@ -356,16 +340,12 @@ class ObjCClasses {
356
340
uint32_t m_size;
357
341
};
358
342
359
- // ----------------------------------------------------------------------
360
343
// Local global variables
361
- // ----------------------------------------------------------------------
362
344
MatchResults g_matches;
363
345
MallocStackLoggingEntries g_malloc_stack_history;
364
346
ObjCClasses g_objc_classes;
365
347
366
- // ----------------------------------------------------------------------
367
348
// ObjCClassInfo
368
- // ----------------------------------------------------------------------
369
349
370
350
enum HeapInfoSortType { eSortTypeNone, eSortTypeBytes, eSortTypeCount };
371
351
@@ -469,13 +449,11 @@ class ObjCClassInfo {
469
449
470
450
ObjCClassInfo g_objc_class_snapshot;
471
451
472
- // ----------------------------------------------------------------------
473
452
// task_peek
474
453
//
475
454
// Reads memory from this tasks address space. This callback is needed
476
455
// by the code that iterates through all of the malloc blocks to read
477
456
// the memory in this process.
478
- // ----------------------------------------------------------------------
479
457
static kern_return_t task_peek (task_t task, vm_address_t remote_address,
480
458
vm_size_t size, void **local_memory) {
481
459
*local_memory = (void *)remote_address;
@@ -534,12 +512,10 @@ static const void foreach_zone_in_this_process(range_callback_info_t *info) {
534
512
}
535
513
}
536
514
537
- // ----------------------------------------------------------------------
538
515
// dump_malloc_block_callback
539
516
//
540
517
// A simple callback that will dump each malloc block and all available
541
518
// info from the enumeration callback perspective.
542
- // ----------------------------------------------------------------------
543
519
static void dump_malloc_block_callback (task_t task, void *baton, unsigned type,
544
520
uint64_t ptr_addr, uint64_t ptr_size) {
545
521
printf (" task = 0x%4.4x: baton = %p, type = %u, ptr_addr = 0x%llx + 0x%llu\n " ,
@@ -739,12 +715,10 @@ malloc_stack_entry *get_stack_history_for_address(const void *addr,
739
715
return g_malloc_stack_history.data ();
740
716
}
741
717
742
- // ----------------------------------------------------------------------
743
718
// find_pointer_in_heap
744
719
//
745
720
// Finds a pointer value inside one or more currently valid malloc
746
721
// blocks.
747
- // ----------------------------------------------------------------------
748
722
malloc_match *find_pointer_in_heap (const void *addr, int check_vm_regions) {
749
723
g_matches.clear ();
750
724
// Setup "info" to look for a malloc block that contains data
@@ -767,12 +741,10 @@ malloc_match *find_pointer_in_heap(const void *addr, int check_vm_regions) {
767
741
return g_matches.data ();
768
742
}
769
743
770
- // ----------------------------------------------------------------------
771
744
// find_pointer_in_memory
772
745
//
773
746
// Finds a pointer value inside one or more currently valid malloc
774
747
// blocks.
775
- // ----------------------------------------------------------------------
776
748
malloc_match *find_pointer_in_memory (uint64_t memory_addr, uint64_t memory_size,
777
749
const void *addr) {
778
750
g_matches.clear ();
@@ -793,13 +765,11 @@ malloc_match *find_pointer_in_memory(uint64_t memory_addr, uint64_t memory_size,
793
765
return g_matches.data ();
794
766
}
795
767
796
- // ----------------------------------------------------------------------
797
768
// find_objc_objects_in_memory
798
769
//
799
770
// Find all instances of ObjC classes 'c', or all ObjC classes if 'c' is
800
771
// NULL. If 'c' is non NULL, then also check objects to see if they
801
772
// inherit from 'c'
802
- // ----------------------------------------------------------------------
803
773
malloc_match *find_objc_objects_in_memory (void *isa, int check_vm_regions) {
804
774
g_matches.clear ();
805
775
if (g_objc_classes.Update ()) {
@@ -819,12 +789,10 @@ malloc_match *find_objc_objects_in_memory(void *isa, int check_vm_regions) {
819
789
return g_matches.data ();
820
790
}
821
791
822
- // ----------------------------------------------------------------------
823
792
// get_heap_info
824
793
//
825
794
// Gather information for all allocations on the heap and report
826
795
// statistics.
827
- // ----------------------------------------------------------------------
828
796
829
797
void get_heap_info (int sort_type) {
830
798
if (g_objc_classes.Update ()) {
@@ -859,11 +827,9 @@ void get_heap_info(int sort_type) {
859
827
}
860
828
}
861
829
862
- // ----------------------------------------------------------------------
863
830
// find_cstring_in_heap
864
831
//
865
832
// Finds a C string inside one or more currently valid malloc blocks.
866
- // ----------------------------------------------------------------------
867
833
malloc_match *find_cstring_in_heap (const char *s, int check_vm_regions) {
868
834
g_matches.clear ();
869
835
if (s == NULL || s[0 ] == ' \0 ' ) {
@@ -887,11 +853,9 @@ malloc_match *find_cstring_in_heap(const char *s, int check_vm_regions) {
887
853
return g_matches.data ();
888
854
}
889
855
890
- // ----------------------------------------------------------------------
891
856
// find_block_for_address
892
857
//
893
858
// Find the malloc block that whose address range contains "addr".
894
- // ----------------------------------------------------------------------
895
859
malloc_match *find_block_for_address (const void *addr, int check_vm_regions) {
896
860
g_matches.clear ();
897
861
// Setup "info" to look for a malloc block that contains data
0 commit comments