1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2004-2009 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
29 #include <mach-o/loader.h>
33 #include "mach-o/dyld_gdb.h"
34 #include "mach-o/dyld_images.h"
35 #include "mach-o/dyld_process_info.h"
37 #include "ImageLoader.h"
40 extern "C" void _dyld_debugger_notification(enum dyld_notify_mode mode
, unsigned long count
, uint64_t machHeaders
[]);
42 #if __IPHONE_OS_VERSION_MIN_REQUIRED
43 #define INITIAL_UUID_IMAGE_COUNT 4
45 #define INITIAL_UUID_IMAGE_COUNT 32
48 VECTOR_NEVER_DESTRUCTED(dyld_image_info
);
49 VECTOR_NEVER_DESTRUCTED(dyld_uuid_info
);
51 static std
::vector
<dyld_image_info
> sImageInfos
;
52 static std
::vector
<dyld_uuid_info
> sImageUUIDs
;
54 size_t allImagesCount()
56 return sImageInfos
.size();
59 const mach_header
* allImagesIndexedMachHeader(uint32_t index
)
61 if ( index
< sImageInfos
.size() )
62 return sImageInfos
[index
].imageLoadAddress
;
67 const char* allImagesIndexedPath(uint32_t index
)
69 if ( index
< sImageInfos
.size() )
70 return sImageInfos
[index
].imageFilePath
;
76 void addImagesToAllImages(uint32_t infoCount
, const dyld_image_info info
[])
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
;
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();
92 // set infoArray back to base address of vector (other process can now read)
93 dyld
::gProcessInfo
->infoArray
= &sImageInfos
[0];
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()
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
]);
107 dyld
::gProcessInfo
->infoArray
= &sImageInfos
[0];
108 dyld
::gProcessInfo
->infoArrayCount
= (uint32_t)sImageInfos
.size();
111 dyld
::gProcessInfo
->notification(dyld_image_info_change
, 0, NULL
);
115 const char* notifyGDB(enum dyld_image_states state
, uint32_t infoCount
, const dyld_image_info info
[])
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
);
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
;
132 void addNonSharedCacheImageUUID(const dyld_uuid_info
& info
)
134 // set uuidArray to NULL to denote it is in-use
135 dyld
::gProcessInfo
->uuidArray
= NULL
;
137 // append all new images
138 sImageUUIDs
.push_back(info
);
139 dyld
::gProcessInfo
->uuidArrayCount
= sImageUUIDs
.size();
141 // set uuidArray back to base address of vector (other process can now read)
142 dyld
::gProcessInfo
->uuidArray
= &sImageUUIDs
[0];
145 void removeImageFromAllImages(const struct mach_header
* loadAddress
)
147 dyld_image_info goingAway
;
149 // set infoArray to NULL to denote it is in-use
150 dyld
::gProcessInfo
->infoArray
= NULL
;
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
) {
156 sImageInfos
.erase(it
);
160 dyld
::gProcessInfo
->infoArrayCount
= (uint32_t)sImageInfos
.size();
162 // set infoArray back to base address of vector
163 dyld
::gProcessInfo
->infoArray
= &sImageInfos
[0];
166 // set uuidArrayCount to NULL to denote it is in-use
167 dyld
::gProcessInfo
->uuidArray
= NULL
;
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
);
176 dyld
::gProcessInfo
->uuidArrayCount
= sImageUUIDs
.size();
177 dyld
::gProcessInfo
->infoArrayChangeTimestamp
= mach_absolute_time();
179 // set infoArray back to base address of vector
180 dyld
::gProcessInfo
->uuidArray
= &sImageUUIDs
[0];
182 // tell gdb that about the new images
183 dyld
::gProcessInfo
->notification(dyld_image_removing
, 1, &goingAway
);
187 #if TARGET_IPHONE_SIMULATOR
189 struct dyld_all_image_infos
* gProcessInfo
= NULL
;
193 static void gdb_image_notifier(enum dyld_image_mode mode
, uint32_t infoCount
, const dyld_image_info info
[])
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
);
201 case dyld_image_adding
:
202 _dyld_debugger_notification(dyld_notify_adding
, infoCount
, machHeaders
);
204 case dyld_image_removing
:
205 _dyld_debugger_notification(dyld_notify_removing
, infoCount
, machHeaders
);
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);
219 // only used with accelerator tables and ASan which images need to be re-loaded
220 void resetAllImages()
224 _dyld_debugger_notification(dyld_notify_remove_all
, 0, NULL
);
227 extern void* __dso_handle
;
229 #define XSTR(s) STR(s)
231 struct dyld_all_image_infos dyld_all_image_infos
__attribute__ ((section ("__DATA,__all_image_info")))
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}
239 struct dyld_shared_cache_ranges dyld_shared_cache_ranges
;
242 struct dyld_all_image_infos
* gProcessInfo
= &dyld_all_image_infos
;