dyld-655.1.1.tar.gz
[apple/dyld.git] / src / dyld_gdb.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004-2009 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #include <stddef.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <mach-o/loader.h>
30
31 #include <vector>
32
33 #include "mach-o/dyld_gdb.h"
34 #include "mach-o/dyld_images.h"
35 #include "mach-o/dyld_process_info.h"
36 #include "Tracing.h"
37 #include "ImageLoader.h"
38 #include "dyld.h"
39
40 extern "C" void _dyld_debugger_notification(enum dyld_notify_mode mode, unsigned long count, uint64_t machHeaders[]);
41
42 #if __IPHONE_OS_VERSION_MIN_REQUIRED
43 #define INITIAL_UUID_IMAGE_COUNT 4
44 #else
45 #define INITIAL_UUID_IMAGE_COUNT 32
46 #endif
47
48 VECTOR_NEVER_DESTRUCTED(dyld_image_info);
49 VECTOR_NEVER_DESTRUCTED(dyld_uuid_info);
50
51 static std::vector<dyld_image_info> sImageInfos;
52 static std::vector<dyld_uuid_info> sImageUUIDs;
53
54 size_t allImagesCount()
55 {
56 return sImageInfos.size();
57 }
58
59 const mach_header* allImagesIndexedMachHeader(uint32_t index)
60 {
61 if ( index < sImageInfos.size() )
62 return sImageInfos[index].imageLoadAddress;
63 else
64 return NULL;
65 }
66
67 const char* allImagesIndexedPath(uint32_t index)
68 {
69 if ( index < sImageInfos.size() )
70 return sImageInfos[index].imageFilePath;
71 else
72 return NULL;
73 }
74
75
76 void addImagesToAllImages(uint32_t infoCount, const dyld_image_info info[])
77 {
78 // make initial size large enough that we probably won't need to re-alloc it
79 if ( sImageInfos.size() == 0 )
80 sImageInfos.reserve(INITIAL_IMAGE_COUNT);
81 if ( sImageUUIDs.capacity() == 0 )
82 sImageUUIDs.reserve(4);
83 // set infoArray to NULL to denote it is in-use
84 dyld::gProcessInfo->infoArray = NULL;
85
86 // append all new images
87 for (uint32_t i=0; i < infoCount; ++i)
88 sImageInfos.push_back(info[i]);
89 dyld::gProcessInfo->infoArrayCount = (uint32_t)sImageInfos.size();
90 dyld::gProcessInfo->infoArrayChangeTimestamp = mach_absolute_time();
91
92 // set infoArray back to base address of vector (other process can now read)
93 dyld::gProcessInfo->infoArray = &sImageInfos[0];
94 }
95
96 #if TARGET_IPHONE_SIMULATOR
97 // called once in dyld_sim start up to copy image list from host dyld to sImageInfos
98 void syncProcessInfo()
99 {
100 // may want to set version field of gProcessInfo if it might be different than host
101 if ( sImageInfos.size() == 0 ) {
102 sImageInfos.reserve(INITIAL_IMAGE_COUNT);
103 if ( dyld::gProcessInfo->infoArray != NULL ) {
104 for (uint32_t i=0; i < dyld::gProcessInfo->infoArrayCount; ++i) {
105 sImageInfos.push_back(dyld::gProcessInfo->infoArray[i]);
106 }
107 dyld::gProcessInfo->infoArray = &sImageInfos[0];
108 dyld::gProcessInfo->infoArrayCount = (uint32_t)sImageInfos.size();
109 }
110 }
111 dyld::gProcessInfo->notification(dyld_image_info_change, 0, NULL);
112 }
113 #endif
114
115 const char* notifyGDB(enum dyld_image_states state, uint32_t infoCount, const dyld_image_info info[])
116 {
117 // tell gdb that about the new images
118 uint64_t t0 = mach_absolute_time();
119 dyld::gProcessInfo->notification(dyld_image_adding, infoCount, info);
120 uint64_t t1 = mach_absolute_time();
121 ImageLoader::fgTotalDebuggerPausedTime += (t1-t0);
122
123 // <rdar://problem/7739489> record initial count of images
124 // so CrashReporter can note which images were dynamically loaded
125 if ( dyld::gProcessInfo->initialImageCount == 0 )
126 dyld::gProcessInfo->initialImageCount = dyld::gProcessInfo->infoArrayCount;
127 return NULL;
128 }
129
130
131
132 void addNonSharedCacheImageUUID(const dyld_uuid_info& info)
133 {
134 // set uuidArray to NULL to denote it is in-use
135 dyld::gProcessInfo->uuidArray = NULL;
136
137 // append all new images
138 sImageUUIDs.push_back(info);
139 dyld::gProcessInfo->uuidArrayCount = sImageUUIDs.size();
140
141 // set uuidArray back to base address of vector (other process can now read)
142 dyld::gProcessInfo->uuidArray = &sImageUUIDs[0];
143 }
144
145 void removeImageFromAllImages(const struct mach_header* loadAddress)
146 {
147 dyld_image_info goingAway;
148
149 // set infoArray to NULL to denote it is in-use
150 dyld::gProcessInfo->infoArray = NULL;
151
152 // remove image from infoArray
153 for (std::vector<dyld_image_info>::iterator it=sImageInfos.begin(); it != sImageInfos.end(); it++) {
154 if ( it->imageLoadAddress == loadAddress ) {
155 goingAway = *it;
156 sImageInfos.erase(it);
157 break;
158 }
159 }
160 dyld::gProcessInfo->infoArrayCount = (uint32_t)sImageInfos.size();
161
162 // set infoArray back to base address of vector
163 dyld::gProcessInfo->infoArray = &sImageInfos[0];
164
165
166 // set uuidArrayCount to NULL to denote it is in-use
167 dyld::gProcessInfo->uuidArray = NULL;
168
169 // remove image from infoArray
170 for (std::vector<dyld_uuid_info>::iterator it=sImageUUIDs.begin(); it != sImageUUIDs.end(); it++) {
171 if ( it->imageLoadAddress == loadAddress ) {
172 sImageUUIDs.erase(it);
173 break;
174 }
175 }
176 dyld::gProcessInfo->uuidArrayCount = sImageUUIDs.size();
177 dyld::gProcessInfo->infoArrayChangeTimestamp = mach_absolute_time();
178
179 // set infoArray back to base address of vector
180 dyld::gProcessInfo->uuidArray = &sImageUUIDs[0];
181
182 // tell gdb that about the new images
183 dyld::gProcessInfo->notification(dyld_image_removing, 1, &goingAway);
184 }
185
186
187 #if TARGET_IPHONE_SIMULATOR
188 namespace dyld {
189 struct dyld_all_image_infos* gProcessInfo = NULL;
190 }
191 #else
192
193 static void gdb_image_notifier(enum dyld_image_mode mode, uint32_t infoCount, const dyld_image_info info[])
194 {
195 dyld3::ScopedTimer(DBG_DYLD_GDB_IMAGE_NOTIFIER, 0, 0, 0);
196 uint64_t machHeaders[infoCount];
197 for (uint32_t i=0; i < infoCount; ++i) {
198 machHeaders[i] = (uintptr_t)(info[i].imageLoadAddress);
199 }
200 switch ( mode ) {
201 case dyld_image_adding:
202 _dyld_debugger_notification(dyld_notify_adding, infoCount, machHeaders);
203 break;
204 case dyld_image_removing:
205 _dyld_debugger_notification(dyld_notify_removing, infoCount, machHeaders);
206 break;
207 default:
208 break;
209 }
210 // do nothing
211 // gdb sets a break point here to catch notifications
212 //dyld::log("dyld: gdb_image_notifier(%s, %d, ...)\n", mode ? "dyld_image_removing" : "dyld_image_adding", infoCount);
213 //for (uint32_t i=0; i < infoCount; ++i)
214 // dyld::log("dyld: %d loading at %p %s\n", i, info[i].imageLoadAddress, info[i].imageFilePath);
215 //for (uint32_t i=0; i < dyld::gProcessInfo->infoArrayCount; ++i)
216 // dyld::log("dyld: %d loading at %p %s\n", i, dyld::gProcessInfo->infoArray[i].imageLoadAddress, dyld::gProcessInfo->infoArray[i].imageFilePath);
217 }
218
219 // only used with accelerator tables and ASan which images need to be re-loaded
220 void resetAllImages()
221 {
222 sImageInfos.clear();
223 sImageUUIDs.clear();
224 _dyld_debugger_notification(dyld_notify_remove_all, 0, NULL);
225 }
226
227 extern void* __dso_handle;
228 #define STR(s) # s
229 #define XSTR(s) STR(s)
230
231 struct dyld_all_image_infos dyld_all_image_infos __attribute__ ((section ("__DATA,__all_image_info")))
232 = {
233 15, 0, {NULL}, &gdb_image_notifier, false, false, (const mach_header*)&__dso_handle, NULL,
234 XSTR(DYLD_VERSION), NULL, 0, NULL, 0, 0, NULL, &dyld_all_image_infos,
235 0, 0, NULL, NULL, NULL, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
236 0, {0}, "/usr/lib/dyld", {0}, {0}
237 };
238
239 struct dyld_shared_cache_ranges dyld_shared_cache_ranges;
240
241 namespace dyld {
242 struct dyld_all_image_infos* gProcessInfo = &dyld_all_image_infos;
243 }
244 #endif
245