aboutsummaryrefslogtreecommitdiffstats
path: root/system_cmds/stackshot.tproj/stackshot.c
blob: 254d0db4b7bf104442c237e493a6d7c3c8470dff (plain) (blame)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* Copyright (c) 2017 Apple Inc. All rights reserved. */

#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>

#include <libiosexec.h>

static 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);
}

__dead2 static void usage(char **argv)
{
    fprintf (stderr, "usage: %s [options] [file]\n", argv[0]);
    fprintf (stderr, "    -d      : take delta stackshot\n");
    fprintf (stderr, "    -b      : get bootprofile\n");
    fprintf (stderr, "    -c      : get coalition data\n");
    fprintf (stderr, "    -i      : get instructions and cycles\n");
    fprintf (stderr, "    -g      : get thread group data\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, "    -S      : stress test: while(1) stackshot; \n");
    fprintf (stderr, "    -p PID  : target a pid\n");
    fprintf (stderr, "    -E      : grab existing kernel buffer\n");
    fprintf (stderr, "If no file is provided and stdout is not a TTY, the stackshot will be written to stdout.\n");
    exit(1);
}

static 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 bootprofile = 0;
    uint32_t thread_group = 0;
    uint32_t coalition = 0;
    uint32_t instrs_cycles = 0;
    uint32_t flags = 0;
    uint32_t loadinfo = STACKSHOT_SAVE_LOADINFO | STACKSHOT_SAVE_KEXT_LOADINFO;
    boolean_t delta = FALSE;
    boolean_t sleep = FALSE;
    boolean_t stress = FALSE;
    pid_t pid = -1;
    int c;
    FILE *file;
    bool closefile;

    while ((c = getopt(argc, argv, "SgIikbcLdtsp:E")) != -1) {
        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 'c':
            coalition |= STACKSHOT_SAVE_JETSAM_COALITIONS;
            break;
        case 'i':
            instrs_cycles |= STACKSHOT_INSTRS_CYCLES;
            break;
        case 'L':
            loadinfo = 0;
            break;
        case 'g':
            thread_group |= STACKSHOT_THREAD_GROUP;
            break;
        case 'd':
            delta = TRUE;
            break;
        case 's':
            sleep = TRUE;
            break;
        case 'p':
            pid = atoi(optarg);
            break;
        case 'S':
            stress = TRUE;
            break;
        case 'E':
            flags = flags | STACKSHOT_RETRIEVE_EXISTING_BUFFER;
            break;
        case '?':
        case 'h':
        default:
            usage(argv);
            break;
        }
    }

    if (thread_group && delta) {
        fprintf(stderr, "stackshot does not support delta snapshots with thread groups\n");
        return 1;
    }

    if (optind == argc - 1) {
        const char *const filename = argv[optind];
        file = fopen(filename, "wx");
        closefile = true;

        if (file == NULL) {
            perror("fopen");
            return EX_CANTCREAT;
        }
    } else if (optind == argc && !isatty(STDOUT_FILENO)) {
        file = stdout;
        closefile = false;
    } else {
        usage(argv);
    }

top:
    ;

    void * config = stackshot_config_create();
    if (!config) {
        perror("stackshot_config_create");
        return 1;
    }
    flags =  flags | loadinfo | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_GET_DQ | STACKSHOT_KCDATA_FORMAT | STACKSHOT_THREAD_WAITINFO |
        bootprofile | active_kernel_threads_only | iostats | thread_group | coalition | instrs_cycles;

    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);


    }

    if (stress) {
        if (config) {
            stackshot_config_dealloc(config);
            config = NULL;
        }
        goto top;
    }

    fwrite(buf, size, 1, file);

    if (closefile) {
        fclose(file);
    }

    return 0;
}