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 "ImageLoader.h"
38 #if __IPHONE_OS_VERSION_MIN_REQUIRED
39 #define INITIAL_UUID_IMAGE_COUNT 4
41 #define INITIAL_UUID_IMAGE_COUNT 32
44 VECTOR_NEVER_DESTRUCTED(dyld_image_info
);
45 VECTOR_NEVER_DESTRUCTED(dyld_uuid_info
);
47 static std::vector
<dyld_image_info
> sImageInfos
;
48 static std::vector
<dyld_uuid_info
> sImageUUIDs
;
51 void addImagesToAllImages(uint32_t infoCount
, const dyld_image_info info
[])
53 // make initial size large enough that we probably won't need to re-alloc it
54 if ( sImageInfos
.size() == 0 )
55 sImageInfos
.reserve(INITIAL_IMAGE_COUNT
);
56 if ( sImageUUIDs
.capacity() == 0 )
57 sImageUUIDs
.reserve(4);
58 // set infoArray to NULL to denote it is in-use
59 dyld::gProcessInfo
->infoArray
= NULL
;
61 // append all new images
62 for (uint32_t i
=0; i
< infoCount
; ++i
)
63 sImageInfos
.push_back(info
[i
]);
64 dyld::gProcessInfo
->infoArrayCount
= (uint32_t)sImageInfos
.size();
66 // set infoArray back to base address of vector (other process can now read)
67 dyld::gProcessInfo
->infoArray
= &sImageInfos
[0];
70 #if TARGET_IPHONE_SIMULATOR
71 // called once in dyld_sim start up to copy image list from host dyld to sImageInfos
72 void syncProcessInfo()
74 // may want to set version field of gProcessInfo if it might be different than host
75 if ( sImageInfos
.size() == 0 ) {
76 sImageInfos
.reserve(INITIAL_IMAGE_COUNT
);
77 if ( dyld::gProcessInfo
->infoArray
!= NULL
) {
78 for (uint32_t i
=0; i
< dyld::gProcessInfo
->infoArrayCount
; ++i
) {
79 sImageInfos
.push_back(dyld::gProcessInfo
->infoArray
[i
]);
81 dyld::gProcessInfo
->infoArray
= &sImageInfos
[0];
82 dyld::gProcessInfo
->infoArrayCount
= (uint32_t)sImageInfos
.size();
85 dyld::gProcessInfo
->notification(dyld_image_info_change
, 0, NULL
);
89 const char* notifyGDB(enum dyld_image_states state
, uint32_t infoCount
, const dyld_image_info info
[])
91 // tell gdb that about the new images
92 dyld::gProcessInfo
->notification(dyld_image_adding
, infoCount
, info
);
93 // <rdar://problem/7739489> record initial count of images
94 // so CrashReporter can note which images were dynamically loaded
95 if ( dyld::gProcessInfo
->initialImageCount
== 0 )
96 dyld::gProcessInfo
->initialImageCount
= infoCount
;
102 void addNonSharedCacheImageUUID(const dyld_uuid_info
& info
)
104 // set uuidArray to NULL to denote it is in-use
105 dyld::gProcessInfo
->uuidArray
= NULL
;
107 // append all new images
108 sImageUUIDs
.push_back(info
);
109 dyld::gProcessInfo
->uuidArrayCount
= sImageUUIDs
.size();
111 // set uuidArray back to base address of vector (other process can now read)
112 dyld::gProcessInfo
->uuidArray
= &sImageUUIDs
[0];
115 void removeImageFromAllImages(const struct mach_header
* loadAddress
)
117 dyld_image_info goingAway
;
119 // set infoArray to NULL to denote it is in-use
120 dyld::gProcessInfo
->infoArray
= NULL
;
122 // remove image from infoArray
123 for (std::vector
<dyld_image_info
>::iterator it
=sImageInfos
.begin(); it
!= sImageInfos
.end(); it
++) {
124 if ( it
->imageLoadAddress
== loadAddress
) {
126 sImageInfos
.erase(it
);
130 dyld::gProcessInfo
->infoArrayCount
= (uint32_t)sImageInfos
.size();
132 // set infoArray back to base address of vector
133 dyld::gProcessInfo
->infoArray
= &sImageInfos
[0];
136 // set uuidArrayCount to NULL to denote it is in-use
137 dyld::gProcessInfo
->uuidArray
= NULL
;
139 // remove image from infoArray
140 for (std::vector
<dyld_uuid_info
>::iterator it
=sImageUUIDs
.begin(); it
!= sImageUUIDs
.end(); it
++) {
141 if ( it
->imageLoadAddress
== loadAddress
) {
142 sImageUUIDs
.erase(it
);
146 dyld::gProcessInfo
->uuidArrayCount
= sImageUUIDs
.size();
148 // set infoArray back to base address of vector
149 dyld::gProcessInfo
->uuidArray
= &sImageUUIDs
[0];
151 // tell gdb that about the new images
152 dyld::gProcessInfo
->notification(dyld_image_removing
, 1, &goingAway
);
155 void setAlImageInfosHalt(const char* message
, uintptr_t flags
)
157 dyld::gProcessInfo
->errorMessage
= message
;
158 dyld::gProcessInfo
->terminationFlags
= flags
;
162 #if TARGET_IPHONE_SIMULATOR
164 struct dyld_all_image_infos
* gProcessInfo
= NULL
;
169 // work around for: <rdar://problem/6530727> gdb-1109: notifier in dyld does not work if it is in thumb
170 extern "C" void gdb_image_notifier(enum dyld_image_mode mode
, uint32_t infoCount
, const dyld_image_info info
[]);
172 static void gdb_image_notifier(enum dyld_image_mode mode
, uint32_t infoCount
, const dyld_image_info info
[])
175 // gdb sets a break point here to catch notifications
176 //dyld::log("dyld: gdb_image_notifier(%s, %d, ...)\n", mode ? "dyld_image_removing" : "dyld_image_adding", infoCount);
177 //for (uint32_t i=0; i < infoCount; ++i)
178 // dyld::log("dyld: %d loading at %p %s\n", i, info[i].imageLoadAddress, info[i].imageFilePath);
179 //for (uint32_t i=0; i < dyld::gProcessInfo->infoArrayCount; ++i)
180 // dyld::log("dyld: %d loading at %p %s\n", i, dyld::gProcessInfo->infoArray[i].imageLoadAddress, dyld::gProcessInfo->infoArray[i].imageFilePath);
184 extern void* __dso_handle
;
186 #define XSTR(s) STR(s)
188 struct dyld_all_image_infos dyld_all_image_infos
__attribute__ ((section ("__DATA,__all_image_info")))
190 14, 0, NULL
, &gdb_image_notifier
, false, false, (const mach_header
*)&__dso_handle
, NULL
,
191 XSTR(DYLD_VERSION
), NULL
, 0, NULL
, 0, 0, NULL
, &dyld_all_image_infos
,
192 0, dyld_error_kind_none
, NULL
, NULL
, NULL
, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
193 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}
196 struct dyld_shared_cache_ranges dyld_shared_cache_ranges
;
199 struct dyld_all_image_infos
* gProcessInfo
= &dyld_all_image_infos
;