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