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;
40 static const char* batchMappedHandler(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
42 for (uint32_t i
=0; i
< infoCount
; ++i
)
43 printf("batchMappedHandler(): %u/%u -> %s\n", i
, infoCount
, info
[i
].imageFilePath
);
44 if ( state
!= dyld_image_state_dependents_mapped
) {
45 FAIL("image-state-change: batchMappedHandler passed state %d", state
);
48 batchMappedCount
+= infoCount
;
52 static const char* singleMappedHandler(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
54 printf("singleMappedHandler(%s)\n", info
[0].imageFilePath
);
55 if ( state
!= dyld_image_state_mapped
) {
56 FAIL("image-state-change: singleMappedHandler passed state %d", state
);
59 if ( infoCount
!= 1 ) {
60 FAIL("image-state-change: singleMappedHandler given %d images", infoCount
);
67 static const char* singleUnmappedHandler(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[])
69 printf("singleUnmappedHandler(%s)\n", info
[0].imageFilePath
);
70 if ( state
!= dyld_image_state_terminated
) {
71 FAIL("image-state-change: singleUnmappedHandler passed state %d", state
);
74 if ( infoCount
!= 1 ) {
75 FAIL("image-state-change: singleUnmappedHandler given %d images", infoCount
);
78 ++singleUnMappedCount
;
82 static void loadAndUnLoad()
84 void* handle
= dlopen("foo.bundle", RTLD_LAZY
);
85 if ( handle
== NULL
) {
86 FAIL("image-state-change: dlopen(foo.bundle) failed: %s", dlerror());
90 int result
= dlclose(handle
);
92 FAIL("image-state-change: dlclose(handle) returned %d, %s", result
, dlerror());
98 int main(int argc
, const char* argv
[])
100 // tell dyld we want to know when images are mapped
101 dyld_register_image_state_change_handler(dyld_image_state_dependents_mapped
, true, batchMappedHandler
);
102 dyld_register_image_state_change_handler(dyld_image_state_mapped
, false, singleMappedHandler
);
103 dyld_register_image_state_change_handler(dyld_image_state_terminated
, false, singleUnmappedHandler
);
104 // with batch mode we get notified of existing images, but not with single mode, so re-sync counts
111 if ( (singleMappedCount
== batchMappedCount
) && (singleMappedCount
== singleUnMappedCount
) )
112 PASS("image-state-change");
114 FAIL("image-state-change: batch count=%d, single count=%d", batchMappedCount
, singleMappedCount
);