]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-auto-dump.mm
objc4-551.1.tar.gz
[apple/objc4.git] / runtime / objc-auto-dump.mm
1 /*
2 * Copyright (c) 2008 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include "objc-config.h"
25
26 #if SUPPORT_GC
27
28 #include "objc-private.h"
29 #include "objc-auto-dump.h"
30
31 #include <auto_zone.h>
32 #include <objc/objc.h>
33 #include <objc/runtime.h>
34 #include <strings.h>
35
36 /*
37 * Utilities
38 */
39
40 static char myType() {
41 char type = 0;
42 if (sizeof(void *) == 8) type |= SixtyFour;
43 #if __LITTLE_ENDIAN__
44 type |= Little;
45 #endif
46 return type;
47 }
48
49 /*
50 * Sigh, a mutable set.
51 */
52
53 typedef struct {
54 long *items;
55 long count;
56 long capacity;
57 } pointer_set_t;
58
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));
62 result->count = 0;
63 result->capacity = 63; // last valid ptr, also mask
64 return result;
65 }
66
67 static void pointer_set_grow(pointer_set_t *set);
68
69 static void pointer_set_add(pointer_set_t *set, long ptr) {
70 long hash = ptr & set->capacity;
71 while (1) {
72 if (!set->items[hash]) {
73 set->items[hash] = ptr;
74 ++set->count;
75 if (set->count*3 > set->capacity*2)
76 pointer_set_grow(set);
77 return;
78 }
79 if (set->items[hash] == ptr) return;
80 hash = (hash + 1) & set->capacity;
81 }
82 }
83
84 static void pointer_set_grow(pointer_set_t *set) {
85 long oldCapacity = set->capacity;
86 long *oldItems = set->items;
87 long i;
88 set->count = 0;
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]);
93 free(oldItems);
94 }
95
96 static void pointer_set_iterate(pointer_set_t *set, void (^block)(long item)) {
97 long i;
98 for (i = 0; i < set->capacity; ++i)
99 if (set->items[i]) block(set->items[i]);
100 }
101
102 static void pointer_set_dispose(pointer_set_t *set) {
103 free(set->items);
104 free(set);
105 }
106
107 /*
108 Quickly dump heap to a named file in a pretty raw format.
109 */
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");
115 if (fp == NULL) {
116 return NO;
117 }
118
119 fwrite(HEADER, strlen(HEADER), 1, fp);
120 char type2 = myType();
121 fwrite(&type2, 1, 1, fp);
122
123 // for each thread...
124
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);
132 };
133
134 // then stacks
135 auto_zone_stack_dump dump_stack = ^(const void *base, unsigned long byte_size) {
136 char type = THREAD;
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);
141 };
142
143 // then locals
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
147 char type = LOCAL;
148 fwrite(&type, 1, 1, fp);
149 fwrite(&address, sizeof(address), 1, fp);
150 };
151
152
153
154 // roots
155 auto_zone_root_dump dump_root = ^(const void **address) {
156 char type = ROOT;
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);
162 };
163
164 // the nodes
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) {
167 char type = NODE;
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);
176 }
177 if ((layout & AUTO_OBJECT) == AUTO_OBJECT) {
178 long theClass = *(long *)address;
179 if (theClass) pointer_set_add(classes, theClass);
180 }
181 };
182
183 // weak
184 auto_zone_weak_dump dump_weak = ^(const void **address, const void *item) {
185 char type = WEAK;
186 fwrite(&type, 1, 1, fp);
187 fwrite(&address, sizeof(address), 1, fp);
188 fwrite(&item, sizeof(item), 1, fp);
189 };
190
191 auto_zone_dump(zone, dump_stack, dump_registers, dump_local, dump_root, dump_node, dump_weak);
192
193 pointer_set_iterate(classes, ^(long cls) {
194 char type = CLASS;
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
202 // strong layout
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
207 // weak layout
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
212 });
213
214 {
215 // end
216 char type = END;
217 fwrite(&type, 1, 1, fp);
218 fclose(fp);
219 pointer_set_dispose(classes);
220 }
221 return YES;
222 }
223
224 #endif