Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | #include <stdio.h> #include <dispatch/dispatch.h> #include <sysexits.h> #include <inttypes.h> #include <string.h> #include <stdlib.h> #include <sys/syscall.h> #include <sys/wait.h> #include <mach/mach_time.h> #include <sys/stackshot.h> #include <sys/types.h> #include <kern/debug.h> #include <unistd.h> #include <assert.h> #include <kern/kcdata.h> #define STACKSHOT_TAILSPIN (0x80000) uint64_t stackshot_get_mach_absolute_time(void *buffer, uint32_t size) { kcdata_iter_t iter = kcdata_iter_find_type(kcdata_iter(buffer, size), KCDATA_TYPE_MACH_ABSOLUTE_TIME); if (!kcdata_iter_valid(iter) || kcdata_iter_size(iter) < sizeof(uint64_t)) { fprintf(stderr, "bad kcdata\n"); exit(1); } return *(uint64_t *)kcdata_iter_payload(iter); } static void usage(char **argv) { fprintf (stderr, "usage: %s [-d] [-t] >file\n", argv[0]); fprintf (stderr, " -d : take delta stackshot\n"); fprintf (stderr, " -b : get bootprofile\n"); fprintf (stderr, " -t : enable tailspin mode\n"); fprintf (stderr, " -s : fork a sleep process\n"); fprintf (stderr, " -L : disable loadinfo\n"); fprintf (stderr, " -k : active kernel threads only\n"); fprintf (stderr, " -I : disable io statistics\n"); fprintf (stderr, " -p PID : target a pid\n"); exit(1); } void forksleep() { pid_t pid = fork(); if (pid < 0) { perror("fork"); exit(1); } if (pid == 0) { execlp("sleep", "sleep", "30", NULL); perror("execlp"); exit(1); } } int main(int argc, char **argv) { uint32_t iostats = 0; uint32_t active_kernel_threads_only = 0; uint32_t tailspin = 0; uint32_t bootprofile = 0; uint32_t loadinfo = STACKSHOT_SAVE_LOADINFO | STACKSHOT_SAVE_KEXT_LOADINFO; boolean_t delta = FALSE; boolean_t sleep = FALSE; pid_t pid = -1; int c; while ((c = getopt(argc, argv, "IkbLdtsp:")) != EOF) { switch(c) { case 'I': iostats |= STACKSHOT_NO_IO_STATS; break; case 'k': active_kernel_threads_only |= STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY; loadinfo &= ~STACKSHOT_SAVE_LOADINFO; break; case 'b': bootprofile |= STACKSHOT_GET_BOOT_PROFILE; break; case 'L': loadinfo = 0; break; case 't': tailspin |= STACKSHOT_TAILSPIN; break; case 'd': delta = TRUE; break; case 's': sleep = TRUE; break; case 'p': pid = atoi(optarg); break; case '?': case 'h': default: usage(argv); break; } } if (optind < argc) { usage(argv); } void * config = stackshot_config_create(); if (!config) { perror("stackshot_config_create"); return 1; } uint32_t flags = loadinfo | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_GET_DQ | STACKSHOT_KCDATA_FORMAT | tailspin | bootprofile | active_kernel_threads_only | iostats; int err = stackshot_config_set_flags(config, flags); if (err != 0) { perror("stackshot_config_set_flags"); return 1; } if (pid != -1) { int err = stackshot_config_set_pid(config, pid); if (err != 0) { perror("stackshot_config_set_flags"); return 1; } } err = stackshot_capture_with_config(config); if (err != 0) { perror("stackshot_capture_with_config"); return 1; } void *buf = stackshot_config_get_stackshot_buffer(config); if (!buf) { perror("stackshot_config_get_stackshot_buffer"); return 1; } uint32_t size = stackshot_config_get_stackshot_size(config); if (delta) { // output the original somewhere? uint64_t time = stackshot_get_mach_absolute_time(buf, size); err = stackshot_config_dealloc_buffer(config); assert(!err); flags |= STACKSHOT_COLLECT_DELTA_SNAPSHOT; int err = stackshot_config_set_flags(config, flags); if (err != 0) { perror("stackshot_config_set_flags"); return 1; } err = stackshot_config_set_delta_timestamp(config, time); if (err != 0) { perror("stackshot_config_delta_timestamp"); return 1; } if (sleep) { forksleep(); } usleep(10000); err = stackshot_capture_with_config(config); if (err != 0) { perror("stackshot_capture_with_config"); return 1; } buf = stackshot_config_get_stackshot_buffer(config); if (!buf) { perror("stackshot_config_get_stackshot_buffer"); return 1; } size = stackshot_config_get_stackshot_size(config); } fwrite(buf, size, 1, stdout); } |