2 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <stdbool.h> // fprintf(), NULL
24 #include <stdio.h> // fprintf(), NULL
25 #include <stdlib.h> // exit(), EXIT_SUCCESS
27 #include <mach-o/dyld.h>
28 #include <mach-o/dyld_priv.h>
31 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
35 static int singleMappedCount
= 0;
36 static int batchMappedCount
= 0;
37 static int singleUnMappedCount
= 0;
38 static int batchBoundCount
= 0;
39 static int singleDepsInitedCount
= 0;
40 static int singleBoundCount
= 0;
42 static const char* batchMappedHandler(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
44 for (uint32_t i
=0; i
< infoCount
; ++i
)
45 printf("batchMappedHandler(): %u/%u -> %s\n", i
, infoCount
, info
[i
].imageFilePath
);
46 if ( state
!= dyld_image_state_dependents_mapped
) {
47 FAIL("image-state-change: batchMappedHandler passed state %d", state
);
50 batchMappedCount
+= infoCount
;
54 static const char* batchBoundHandler(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
56 for (uint32_t i
=0; i
< infoCount
; ++i
)
57 printf("batchBoundHandler(): %u/%u -> %s\n", i
, infoCount
, info
[i
].imageFilePath
);
58 if ( state
!= dyld_image_state_bound
) {
59 FAIL("image-state-change: batchBoundHandler passed state %d", state
);
62 batchBoundCount
+= infoCount
;
66 static const char* singleMappedHandler(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
68 printf("singleMappedHandler(%s)\n", info
[0].imageFilePath
);
69 if ( state
!= dyld_image_state_mapped
) {
70 FAIL("image-state-change: singleMappedHandler passed state %d", state
);
73 if ( infoCount
!= 1 ) {
74 FAIL("image-state-change: singleMappedHandler given %d images", infoCount
);
82 static const char* singleBoundHandler(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
84 printf("singleBoundHandler(%s)\n", info
[0].imageFilePath
);
85 if ( state
!= dyld_image_state_bound
) {
86 FAIL("image-state-change: singleBoundHandler passed state %d", state
);
89 if ( infoCount
!= 1 ) {
90 FAIL("image-state-change: singleBoundHandler given %d images", infoCount
);
99 static const char* singleDepsInitedHandler(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
101 printf("singleDepsInitedHandler(%s)\n", info
[0].imageFilePath
);
102 if ( state
!= dyld_image_state_dependents_initialized
) {
103 FAIL("image-state-change: singleDepsInitedHandler passed state %d", state
);
106 if ( infoCount
!= 1 ) {
107 FAIL("image-state-change: singleDepsInitedHandler given %d images", infoCount
);
110 ++singleDepsInitedCount
;
116 static const char* singleUnmappedHandler(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
118 printf("singleUnmappedHandler(%s)\n", info
[0].imageFilePath
);
119 if ( state
!= dyld_image_state_terminated
) {
120 FAIL("image-state-change: singleUnmappedHandler passed state %d", state
);
123 if ( infoCount
!= 1 ) {
124 FAIL("image-state-change: singleUnmappedHandler given %d images", infoCount
);
127 ++singleUnMappedCount
;
131 static void loadAndUnLoad()
133 void* handle
= dlopen("foo.bundle", RTLD_LAZY
);
134 if ( handle
== NULL
) {
135 FAIL("image-state-change: dlopen(foo.bundle) failed: %s", dlerror());
139 int result
= dlclose(handle
);
141 FAIL("image-state-change: dlclose(handle) returned %d, %s", result
, dlerror());
147 int main(int argc
, const char* argv
[])
149 // tell dyld we want to know when images are mapped
150 dyld_register_image_state_change_handler(dyld_image_state_dependents_mapped
, true, batchMappedHandler
);
151 dyld_register_image_state_change_handler(dyld_image_state_mapped
, false, singleMappedHandler
);
152 dyld_register_image_state_change_handler(dyld_image_state_bound
, true, batchBoundHandler
);
153 dyld_register_image_state_change_handler(dyld_image_state_bound
, false, singleBoundHandler
);
154 dyld_register_image_state_change_handler(dyld_image_state_dependents_initialized
, false, singleDepsInitedHandler
);
155 dyld_register_image_state_change_handler(dyld_image_state_terminated
, false, singleUnmappedHandler
);
157 // with batch mode we get notified of existing images, but not with single mode, so re-sync counts
164 if ( (singleMappedCount
== batchMappedCount
) && (singleMappedCount
== singleUnMappedCount
) )
165 PASS("image-state-change");
167 FAIL("image-state-change: batch count=%d, single count=%d", batchMappedCount
, singleMappedCount
);