]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-auto-dump.m
objc4-437.3.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 #import <auto_zone.h>
25 #import <objc/objc.h>
26 #import <objc/runtime.h>
27 #import "objc-auto-dump.h"
28 #import "objc-private.h"
29 #import <strings.h>
30
31 /*
32 * Utilities
33 */
34
35 static char myType() {
36 char type = 0;
37 if (sizeof(void *) == 8) type |= SixtyFour;
38 #if __LITTLE_ENDIAN__
39 type |= Little;
40 #endif
41 return type;
42 }
43
44 /*
45 * Sigh, a mutable set.
46 */
47
48 typedef struct {
49 long *items;
50 long count;
51 long capacity;
52 } pointer_set_t;
53
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));
57 result->count = 0;
58 result->capacity = 63; // last valid ptr, also mask
59 return result;
60 }
61
62 static void pointer_set_grow(pointer_set_t *set);
63
64 static void pointer_set_add(pointer_set_t *set, long ptr) {
65 long hash = ptr & set->capacity;
66 while (1) {
67 if (!set->items[hash]) {
68 set->items[hash] = ptr;
69 ++set->count;
70 if (set->count*3 > set->capacity*2)
71 pointer_set_grow(set);
72 return;
73 }
74 if (set->items[hash] == ptr) return;
75 hash = (hash + 1) & set->capacity;
76 }
77 }
78
79 static void pointer_set_grow(pointer_set_t *set) {
80 long oldCapacity = set->capacity;
81 long *oldItems = set->items;
82 long i;
83 set->count = 0;
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]);
88 free(oldItems);
89 }
90
91 static void pointer_set_iterate(pointer_set_t *set, void (^block)(long item)) {
92 long i;
93 for (i = 0; i < set->capacity; ++i)
94 if (set->items[i]) block(set->items[i]);
95 }
96
97 static void pointer_set_dispose(pointer_set_t *set) {
98 free(set->items);
99 free(set);
100 }
101
102 /*
103 Quickly dump heap to a named file in a pretty raw format.
104 */
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");
110 if (fp == NULL) {
111 return NO;
112 }
113
114 fwrite(HEADER, strlen(HEADER), 1, fp);
115 char type = myType();
116 fwrite(&type, 1, 1, fp);
117
118 // for each thread...
119
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);
127 };
128
129 // then stacks
130 void (^dump_stack)(const void *, unsigned long) = ^(const void *base, unsigned long byte_size) {
131 char type = THREAD;
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);
136 };
137
138 // then locals
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
142 char type = LOCAL;
143 fwrite(&type, 1, 1, fp);
144 fwrite(&address, sizeof(address), 1, fp);
145 };
146
147
148
149 // roots
150 void (^dump_root)(const void **) = ^(const void **address) {
151 char type = ROOT;
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);
157 };
158
159 // the nodes
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) {
163 char type = NODE;
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);
172 }
173 if ((layout & AUTO_OBJECT) == AUTO_OBJECT) {
174 long theClass = *(long *)address;
175 if (theClass) pointer_set_add(classes, theClass);
176 }
177 };
178
179 // weak
180 void (^dump_weak)(const void **, const void *) = ^(const void **address, const void *item) {
181 char type = WEAK;
182 fwrite(&type, 1, 1, fp);
183 fwrite(&address, sizeof(address), 1, fp);
184 fwrite(&item, sizeof(item), 1, fp);
185 };
186
187 auto_zone_dump(zone, dump_stack, dump_registers, dump_local, dump_root, dump_node, dump_weak);
188
189 pointer_set_iterate(classes, ^(long class) {
190 char type = 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
198 // strong layout
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
203 // weak layout
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
208 });
209
210 {
211 // end
212 char type = END;
213 fwrite(&type, 1, 1, fp);
214 fclose(fp);
215 pointer_set_dispose(classes);
216 }
217 return YES;
218 }