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