2 * Copyright (c) 2008 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 #import <objc/runtime.h>
27 #import "objc-auto-dump.h"
28 #import "objc-private.h"
35 static char myType() {
37 if (sizeof(void *) == 8) type |= SixtyFour;
45 * Sigh, a mutable set.
54 static pointer_set_t *new_pointer_set() {
55 pointer_set_t *result = malloc_zone_malloc(_objc_internal_zone(), sizeof(pointer_set_t));
56 result->items = calloc(64, sizeof(long));
58 result->capacity = 63; // last valid ptr, also mask
62 static void pointer_set_grow(pointer_set_t *set);
64 static void pointer_set_add(pointer_set_t *set, long ptr) {
65 long hash = ptr & set->capacity;
67 if (!set->items[hash]) {
68 set->items[hash] = ptr;
70 if (set->count*3 > set->capacity*2)
71 pointer_set_grow(set);
74 if (set->items[hash] == ptr) return;
75 hash = (hash + 1) & set->capacity;
79 static void pointer_set_grow(pointer_set_t *set) {
80 long oldCapacity = set->capacity;
81 long *oldItems = set->items;
84 set->capacity = 2*(oldCapacity+1)-1;
85 set->items = malloc_zone_calloc(_objc_internal_zone(), 2*(oldCapacity+1), sizeof(long));
86 for (i = 0; i < oldCapacity; ++i)
87 if (oldItems[i]) pointer_set_add(set, oldItems[i]);
91 static void pointer_set_iterate(pointer_set_t *set, void (^block)(long item)) {
93 for (i = 0; i < set->capacity; ++i)
94 if (set->items[i]) block(set->items[i]);
97 static void pointer_set_dispose(pointer_set_t *set) {
103 Quickly dump heap to a named file in a pretty raw format.
105 __private_extern__ BOOL _objc_dumpHeap(auto_zone_t *zone, const char *filename) {
106 // just write interesting info to disk
107 int fd = secure_open(filename, O_WRONLY|O_CREAT, geteuid());
108 if (fd < 0) return NO;
109 FILE *fp = fdopen(fd, "w");
114 fwrite(HEADER, strlen(HEADER), 1, fp);
115 char type = myType();
116 fwrite(&type, 1, 1, fp);
118 // for each thread...
120 // do registers first
121 void (^dump_registers)(const void *, unsigned long) = ^(const void *base, unsigned long byte_size) {
122 char type = REGISTER;
123 fwrite(&type, 1, 1, fp);
124 //fwrite(REGISTER, strlen(REGISTER), 1, fp);
125 fwrite(&byte_size, sizeof(byte_size), 1, fp);
126 fwrite(base, byte_size, 1, fp);
130 void (^dump_stack)(const void *, unsigned long) = ^(const void *base, unsigned long byte_size) {
132 fwrite(&type, 1, 1, fp);
133 //fwrite(THREAD, strlen(THREAD), 1, fp);
134 fwrite(&byte_size, sizeof(byte_size), 1, fp);
135 fwrite(base, byte_size, 1, fp);
139 void (^dump_local)(const void *, unsigned long, unsigned int, unsigned long) =
140 ^(const void *address, unsigned long size, unsigned int layout, unsigned long refcount) {
141 // just write the value - rely on it showing up again as a node later
143 fwrite(&type, 1, 1, fp);
144 fwrite(&address, sizeof(address), 1, fp);
150 void (^dump_root)(const void **) = ^(const void **address) {
152 fwrite(&type, 1, 1, fp);
153 // write the address so that we can catch misregistered globals
154 fwrite(&address, sizeof(address), 1, fp);
155 // write content, even (?) if zero
156 fwrite(address, sizeof(*address), 1, fp);
160 pointer_set_t *classes = new_pointer_set();
161 void (^dump_node)(const void *, unsigned long, unsigned int, unsigned long) =
162 ^(const void *address, unsigned long size, unsigned int layout, unsigned long refcount) {
164 fwrite(&type, 1, 1, fp);
165 fwrite(&address, sizeof(address), 1, fp);
166 fwrite(&size, sizeof(size), 1, fp);
167 fwrite(&layout, sizeof(layout), 1, fp);
168 fwrite(&refcount, sizeof(refcount), 1, fp);
169 if ((layout & AUTO_UNSCANNED) != AUTO_UNSCANNED) {
170 // now the nodes unfiltered content
171 fwrite(address, size, 1, fp);
173 if ((layout & AUTO_OBJECT) == AUTO_OBJECT) {
174 long theClass = *(long *)address;
175 if (theClass) pointer_set_add(classes, theClass);
180 void (^dump_weak)(const void **, const void *) = ^(const void **address, const void *item) {
182 fwrite(&type, 1, 1, fp);
183 fwrite(&address, sizeof(address), 1, fp);
184 fwrite(&item, sizeof(item), 1, fp);
187 auto_zone_dump(zone, dump_stack, dump_registers, dump_local, dump_root, dump_node, dump_weak);
189 pointer_set_iterate(classes, ^(long class) {
191 fwrite(&type, 1, 1, fp);
192 fwrite(&class, sizeof(class), 1, fp); // write address so that we can map it from node isa's
193 // classname (for grins)
194 const char *className = class_getName((Class)class);
195 unsigned int length = (int)strlen(className);
196 fwrite(&length, sizeof(length), 1, fp); // n
197 fwrite(className, length, 1, fp); // n bytes
199 const char *layout = class_getIvarLayout((Class)class);
200 length = layout ? (int)strlen(layout)+1 : 0; // format is <skipnibble><count nibble> ending with <0><0>
201 fwrite(&length, sizeof(length), 1, fp); // n
202 fwrite(layout, length, 1, fp); // n bytes
204 layout = class_getWeakIvarLayout((Class)class);
205 length = layout ? (int)strlen(layout)+1 : 0; // format is <skipnibble><count nibble> ending with <0><0>
206 fwrite(&length, sizeof(length), 1, fp); // n
207 fwrite(layout, length, 1, fp); // n bytes
213 fwrite(&type, 1, 1, fp);
215 pointer_set_dispose(classes);