]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/execperf/printexecinfo.c
xnu-2422.115.4.tar.gz
[apple/xnu.git] / tools / tests / execperf / printexecinfo.c
1 #include <stdio.h>
2 #include <err.h>
3 #include <crt_externs.h>
4 #include <string.h>
5 #include <mach/mach.h>
6 #include <mach-o/ldsyms.h>
7 #include <mach-o/dyld_images.h>
8 #include <stdlib.h>
9 #include <sys/sysctl.h>
10
11 __attribute__((constructor))
12 void init(int argc, const char *argv[], const char *envp[], const char *appl[], void *vars __attribute__((unused))) {
13 int i;
14
15 printf("argv = %p\n", argv);
16 for (i=0; argv[i]; i++) {
17 printf("argv[%2d] = %p %.100s%s\n", i, argv[i], argv[i], strlen(argv[i]) > 100 ? "..." : "");
18 }
19 printf("envp = %p\n", envp);
20 for (i=0; envp[i]; i++) {
21 printf("envp[%2d] = %p %.100s%s\n", i, envp[i], envp[i], strlen(envp[i]) > 100 ? "..." : "");
22 }
23 printf("appl = %p\n", appl);
24 for (i=0; appl[i]; i++) {
25 printf("appl[%2d] = %p %.100s%s\n", i, appl[i], appl[i], strlen(appl[i]) > 100 ? "..." : "");
26 }
27 }
28
29 void printexecinfo(void)
30 {
31 int ret;
32 uint64_t stackaddr;
33 size_t len = sizeof(stackaddr);
34
35 printf("executable load address = 0x%016llx\n", (uint64_t)(uintptr_t)&_mh_execute_header);
36
37 ret = sysctlbyname("kern.usrstack64", &stackaddr, &len, NULL, 0);
38 if (ret == -1)
39 err(1, "sysctlbyname");
40
41 printf(" stack address = 0x%016llx\n", stackaddr);
42 }
43
44 void printdyldinfo(void)
45 {
46 task_dyld_info_data_t info;
47 mach_msg_type_number_t size = TASK_DYLD_INFO_COUNT;
48 kern_return_t kret;
49 struct dyld_all_image_infos *all_image_infos;
50
51 kret = task_info(mach_task_self(), TASK_DYLD_INFO,
52 (void *)&info, &size);
53 if (kret != KERN_SUCCESS)
54 errx(1, "task_info: %s", mach_error_string(kret));
55
56 all_image_infos = (struct dyld_all_image_infos *)(uintptr_t)info.all_image_info_addr;
57
58 printf(" dyld load address = 0x%016llx\n", (uint64_t)(uintptr_t)all_image_infos->dyldImageLoadAddress);
59 printf(" shared cache slide = 0x%016llx\n", (uint64_t)(uintptr_t)all_image_infos->sharedCacheSlide);
60
61 }
62
63 int main(int argc, char *argv[]) {
64
65 printexecinfo();
66 printdyldinfo();
67
68 return 0;
69 }