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@
24 #include "objc-config.h"
28 #include "objc-private.h"
29 #include "objc-auto-dump.h"
31 #include <auto_zone.h>
32 #include <objc/objc.h>
33 #include <objc/runtime.h>
40 static char myType() {
42 if (sizeof(void *) == 8) type |= SixtyFour;
50 * Sigh, a mutable set.
59 static pointer_set_t *new_pointer_set() {
60 pointer_set_t *result = (pointer_set_t *)malloc_zone_malloc(_objc_internal_zone(), sizeof(pointer_set_t));
61 result->items = (long *)calloc(64, sizeof(long));
63 result->capacity = 63; // last valid ptr, also mask
67 static void pointer_set_grow(pointer_set_t *set);
69 static void pointer_set_add(pointer_set_t *set, long ptr) {
70 long hash = ptr & set->capacity;
72 if (!set->items[hash]) {
73 set->items[hash] = ptr;
75 if (set->count*3 > set->capacity*2)
76 pointer_set_grow(set);
79 if (set->items[hash] == ptr) return;
80 hash = (hash + 1) & set->capacity;
84 static void pointer_set_grow(pointer_set_t *set) {
85 long oldCapacity = set->capacity;
86 long *oldItems = set->items;
89 set->capacity = 2*(oldCapacity+1)-1;
90 set->items = (long *)malloc_zone_calloc(_objc_internal_zone(), 2*(oldCapacity+1), sizeof(long));
91 for (i = 0; i < oldCapacity; ++i)
92 if (oldItems[i]) pointer_set_add(set, oldItems[i]);
96 static void pointer_set_iterate(pointer_set_t *set, void (^block)(long item)) {
98 for (i = 0; i < set->capacity; ++i)
99 if (set->items[i]) block(set->items[i]);
102 static void pointer_set_dispose(pointer_set_t *set) {
108 Quickly dump heap to a named file in a pretty raw format.
110 BOOL _objc_dumpHeap(auto_zone_t *zone, const char *filename) {
111 // just write interesting info to disk
112 int fd = secure_open(filename, O_WRONLY|O_CREAT, geteuid());
113 if (fd < 0) return NO;
114 FILE *fp = fdopen(fd, "w");
119 fwrite(HEADER, strlen(HEADER), 1, fp);
120 char type2 = myType();
121 fwrite(&type2, 1, 1, fp);
123 // for each thread...
125 // do registers first
126 auto_zone_register_dump dump_registers = ^(const void *base, unsigned long byte_size) {
127 char type = REGISTER;
128 fwrite(&type, 1, 1, fp);
129 //fwrite(REGISTER, strlen(REGISTER), 1, fp);
130 fwrite(&byte_size, sizeof(byte_size), 1, fp);
131 fwrite(base, byte_size, 1, fp);
135 auto_zone_stack_dump dump_stack = ^(const void *base, unsigned long byte_size) {
137 fwrite(&type, 1, 1, fp);
138 //fwrite(THREAD, strlen(THREAD), 1, fp);
139 fwrite(&byte_size, sizeof(byte_size), 1, fp);
140 fwrite(base, byte_size, 1, fp);
144 void (^dump_local)(const void *, unsigned long, unsigned int, unsigned long) =
145 ^(const void *address, unsigned long size, unsigned int layout, unsigned long refcount) {
146 // just write the value - rely on it showing up again as a node later
148 fwrite(&type, 1, 1, fp);
149 fwrite(&address, sizeof(address), 1, fp);
155 auto_zone_root_dump dump_root = ^(const void **address) {
157 fwrite(&type, 1, 1, fp);
158 // write the address so that we can catch misregistered globals
159 fwrite(&address, sizeof(address), 1, fp);
160 // write content, even (?) if zero
161 fwrite(address, sizeof(*address), 1, fp);
165 pointer_set_t *classes = new_pointer_set();
166 auto_zone_node_dump dump_node = ^(const void *address, unsigned long size, unsigned int layout, unsigned long refcount) {
168 fwrite(&type, 1, 1, fp);
169 fwrite(&address, sizeof(address), 1, fp);
170 fwrite(&size, sizeof(size), 1, fp);
171 fwrite(&layout, sizeof(layout), 1, fp);
172 fwrite(&refcount, sizeof(refcount), 1, fp);
173 if ((layout & AUTO_UNSCANNED) != AUTO_UNSCANNED) {
174 // now the nodes unfiltered content
175 fwrite(address, size, 1, fp);
177 if ((layout & AUTO_OBJECT) == AUTO_OBJECT) {
178 long theClass = *(long *)address;
179 if (theClass) pointer_set_add(classes, theClass);
184 auto_zone_weak_dump dump_weak = ^(const void **address, const void *item) {
186 fwrite(&type, 1, 1, fp);
187 fwrite(&address, sizeof(address), 1, fp);
188 fwrite(&item, sizeof(item), 1, fp);
191 auto_zone_dump(zone, dump_stack, dump_registers, dump_local, dump_root, dump_node, dump_weak);
193 pointer_set_iterate(classes, ^(long cls) {
195 fwrite(&type, 1, 1, fp);
196 fwrite(&cls, sizeof(cls), 1, fp); // write address so that we can map it from node isa's
197 // classname (for grins)
198 const char *className = class_getName((Class)cls);
199 unsigned int length = (int)strlen(className);
200 fwrite(&length, sizeof(length), 1, fp); // n
201 fwrite(className, length, 1, fp); // n bytes
203 const uint8_t *layout = class_getIvarLayout((Class)cls);
204 length = layout ? (int)strlen((char *)layout)+1 : 0; // format is <skipnibble><count nibble> ending with <0><0>
205 fwrite(&length, sizeof(length), 1, fp); // n
206 fwrite(layout, length, 1, fp); // n bytes
208 layout = class_getWeakIvarLayout((Class)cls);
209 length = layout ? (int)strlen((char *)layout)+1 : 0; // format is <skipnibble><count nibble> ending with <0><0>
210 fwrite(&length, sizeof(length), 1, fp); // n
211 fwrite(layout, length, 1, fp); // n bytes
217 fwrite(&type, 1, 1, fp);
219 pointer_set_dispose(classes);