961ed8af57c2cf3b549a1bb651cfb7f41d98c9ff
[apple/dyld.git] / testing / test-cases / dyld_process_info_unload.dtest / main.cpp
1
2 // BUILD: $CC target.c -o $BUILD_DIR/target.exe
3 // BUILD: $CC foo.c -o $BUILD_DIR/libfoo.dylib -dynamiclib -install_name $RUN_DIR/libfoo.dylib
4 // BUILD: $CXX main.cpp -o $BUILD_DIR/dyld_process_info_unload.exe -DRUN_DIR="$RUN_DIR"
5 // BUILD: $TASK_FOR_PID_ENABLE $BUILD_DIR/dyld_process_info_unload.exe
6
7 // RUN: $SUDO ./dyld_process_info_unload.exe $RUN_DIR/target.exe
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <dlfcn.h>
12 #include <unistd.h>
13 #include <signal.h>
14 #include <spawn.h>
15 #include <errno.h>
16 #include <mach/mach.h>
17 #include <mach/machine.h>
18 #include <mach-o/dyld_process_info.h>
19
20 #include "test_support.h"
21
22 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
23 _process process;
24 process.set_executable_path(RUN_DIR "/target.exe");
25 process.set_launch_suspended(true);
26 process.set_launch_async(true);
27 const char* env[] = { "TEST_OUTPUT=None", NULL};
28 process.set_env(env);
29 pid_t pid = process.launch();
30 task_t task;
31 if (task_for_pid(mach_task_self(), pid, &task) != KERN_SUCCESS) {
32 FAIL("task_for_pid() failed");
33 }
34
35 dispatch_async(dispatch_get_main_queue(), ^{
36 int failCount = 0;
37 for (int i=0; i < 100; ++i ) {
38 kern_return_t result;
39 dyld_process_info info = _dyld_process_info_create(task, 0, &result);
40 LOG("info=%p, result=%08X", info, result);
41 if ( i == 0 )
42 (void)kill(pid, SIGCONT);
43 if ( info == NULL ) {
44 //FIXME: Compact info will never fail, make this a FAIL()
45 failCount++;
46 // ideally the fail count would be zero. But the target is dlopen/dlclosing in a tight loop, so there may never be a stable set of images.
47 // The real bug driving this test case was _dyld_process_info_create() crashing when the the image list changed too fast.
48 // The important thing is to not crash. Getting NULL back is ok.
49 LOG("info=%p, result=%08X", info, result);
50 }
51 else {
52 usleep(100);
53 _dyld_process_info_release(info);
54 }
55 }
56 PASS("Success");
57 });
58
59 dispatch_main();
60 }