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