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-auto.h"
28 #include <auto_zone.h>
29 #include <objc/objc.h>
30 #include <objc/runtime.h>
31 #include "objc-auto-dump.h"
32 #include "objc-private.h"
39 static char myType() {
41 if (sizeof(void *) == 8) type |= SixtyFour;
49 * Sigh, a mutable set.
58 static pointer_set_t *new_pointer_set() {
59 pointer_set_t *result = malloc_zone_malloc(_objc_internal_zone(), sizeof(pointer_set_t));
60 result->items = calloc(64, sizeof(long));
62 result->capacity = 63; // last valid ptr, also mask
66 static void pointer_set_grow(pointer_set_t *set);
68 static void pointer_set_add(pointer_set_t *set, long ptr) {
69 long hash = ptr & set->capacity;
71 if (!set->items[hash]) {
72 set->items[hash] = ptr;
74 if (set->count*3 > set->capacity*2)
75 pointer_set_grow(set);
78 if (set->items[hash] == ptr) return;
79 hash = (hash + 1) & set->capacity;
83 static void pointer_set_grow(pointer_set_t *set) {
84 long oldCapacity = set->capacity;
85 long *oldItems = set->items;
88 set->capacity = 2*(oldCapacity+1)-1;
89 set->items = malloc_zone_calloc(_objc_internal_zone(), 2*(oldCapacity+1), sizeof(long));
90 for (i = 0; i < oldCapacity; ++i)
91 if (oldItems[i]) pointer_set_add(set, oldItems[i]);
95 static void pointer_set_iterate(pointer_set_t *set, void (^block)(long item)) {
97 for (i = 0; i < set->capacity; ++i)
98 if (set->items[i]) block(set->items[i]);
101 static void pointer_set_dispose(pointer_set_t *set) {
107 Quickly dump heap to a named file in a pretty raw format.
109 BOOL _objc_dumpHeap(auto_zone_t *zone, const char *filename) {
110 // just write interesting info to disk
111 int fd = secure_open(filename, O_WRONLY|O_CREAT, geteuid());
112 if (fd < 0) return NO;
113 FILE *fp = fdopen(fd, "w");
118 fwrite(HEADER, strlen(HEADER), 1, fp);
119 char type2 = myType();
120 fwrite(&type2, 1, 1, fp);
122 // for each thread...
124 // do registers first
125 auto_zone_register_dump dump_registers = ^(const void *base, unsigned long byte_size) {
126 char type = REGISTER;
127 fwrite(&type, 1, 1, fp);
128 //fwrite(REGISTER, strlen(REGISTER), 1, fp);
129 fwrite(&byte_size, sizeof(byte_size), 1, fp);
130 fwrite(base, byte_size, 1, fp);
134 auto_zone_stack_dump dump_stack = ^(const void *base, unsigned long byte_size) {
136 fwrite(&type, 1, 1, fp);
137 //fwrite(THREAD, strlen(THREAD), 1, fp);
138 fwrite(&byte_size, sizeof(byte_size), 1, fp);
139 fwrite(base, byte_size, 1, fp);
143 void (^dump_local)(const void *, unsigned long, unsigned int, unsigned long) =
144 ^(const void *address, unsigned long size, unsigned int layout, unsigned long refcount) {
145 // just write the value - rely on it showing up again as a node later
147 fwrite(&type, 1, 1, fp);
148 fwrite(&address, sizeof(address), 1, fp);
154 auto_zone_root_dump dump_root = ^(const void **address) {
156 fwrite(&type, 1, 1, fp);
157 // write the address so that we can catch misregistered globals
158 fwrite(&address, sizeof(address), 1, fp);
159 // write content, even (?) if zero
160 fwrite(address, sizeof(*address), 1, fp);
164 pointer_set_t *classes = new_pointer_set();
165 auto_zone_node_dump dump_node = ^(const void *address, unsigned long size, unsigned int layout, unsigned long refcount) {
167 fwrite(&type, 1, 1, fp);
168 fwrite(&address, sizeof(address), 1, fp);
169 fwrite(&size, sizeof(size), 1, fp);
170 fwrite(&layout, sizeof(layout), 1, fp);
171 fwrite(&refcount, sizeof(refcount), 1, fp);
172 if ((layout & AUTO_UNSCANNED) != AUTO_UNSCANNED) {
173 // now the nodes unfiltered content
174 fwrite(address, size, 1, fp);
176 if ((layout & AUTO_OBJECT) == AUTO_OBJECT) {
177 long theClass = *(long *)address;
178 if (theClass) pointer_set_add(classes, theClass);
183 auto_zone_weak_dump dump_weak = ^(const void **address, const void *item) {
185 fwrite(&type, 1, 1, fp);
186 fwrite(&address, sizeof(address), 1, fp);
187 fwrite(&item, sizeof(item), 1, fp);
190 auto_zone_dump(zone, dump_stack, dump_registers, dump_local, dump_root, dump_node, dump_weak);
192 pointer_set_iterate(classes, ^(long class) {
194 fwrite(&type, 1, 1, fp);
195 fwrite(&class, sizeof(class), 1, fp); // write address so that we can map it from node isa's
196 // classname (for grins)
197 const char *className = class_getName((Class)class);
198 unsigned int length = (int)strlen(className);
199 fwrite(&length, sizeof(length), 1, fp); // n
200 fwrite(className, length, 1, fp); // n bytes
202 const uint8_t *layout = class_getIvarLayout((Class)class);
203 length = layout ? (int)strlen((char *)layout)+1 : 0; // format is <skipnibble><count nibble> ending with <0><0>
204 fwrite(&length, sizeof(length), 1, fp); // n
205 fwrite(layout, length, 1, fp); // n bytes
207 layout = class_getWeakIvarLayout((Class)class);
208 length = layout ? (int)strlen((char *)layout)+1 : 0; // format is <skipnibble><count nibble> ending with <0><0>
209 fwrite(&length, sizeof(length), 1, fp); // n
210 fwrite(layout, length, 1, fp); // n bytes
216 fwrite(&type, 1, 1, fp);
218 pointer_set_dispose(classes);