1 #include <darwintest.h>
2 #include <darwintest_utils.h>
3 #include <kern/debug.h>
4 #include <kern/kern_cdata.h>
7 #include <sys/syscall.h>
8 #include <sys/stackshot.h>
11 T_META_NAMESPACE("xnu.stackshot"),
12 T_META_CHECK_LEAKS(false),
16 static const char *current_process_name(void);
17 static void parse_stackshot(bool delta, void *ssbuf, size_t sslen);
18 static void parse_thread_group_stackshot(void **sbuf, size_t sslen);
19 static uint64_t stackshot_timestamp(void *ssbuf, size_t sslen);
20 static void initialize_thread(void);
22 #define DEFAULT_STACKSHOT_BUFFER_SIZE (1024 * 1024)
23 #define MAX_STACKSHOT_BUFFER_SIZE (6 * 1024 * 1024)
25 T_DECL(microstackshots, "test the microstackshot syscall")
28 unsigned int size = DEFAULT_STACKSHOT_BUFFER_SIZE;
32 T_QUIET; T_ASSERT_NOTNULL(buf, "allocated stackshot buffer");
34 #pragma clang diagnostic push
35 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
36 int len = syscall(SYS_microstackshot, buf, size,
37 STACKSHOT_GET_MICROSTACKSHOT);
38 #pragma clang diagnostic pop
40 T_SKIP("microstackshot syscall failed, likely not compiled with CONFIG_TELEMETRY");
42 if (len == -1 && errno == ENOSPC) {
43 /* syscall failed because buffer wasn't large enough, try again */
47 T_ASSERT_LE(size, (unsigned int)MAX_STACKSHOT_BUFFER_SIZE,
48 "growing stackshot buffer to sane size");
51 T_ASSERT_POSIX_SUCCESS(len, "called microstackshot syscall");
55 T_EXPECT_EQ(*(uint32_t *)buf,
56 (uint32_t)STACKSHOT_MICRO_SNAPSHOT_MAGIC,
57 "magic value for microstackshot matches");
66 uint64_t since_timestamp;
72 quiet(struct scenario *scenario)
74 if (scenario->timer) {
80 take_stackshot(struct scenario *scenario, void (^cb)(void *buf, size_t size))
82 void *config = stackshot_config_create();
84 T_ASSERT_NOTNULL(config, "created stackshot config");
86 int ret = stackshot_config_set_flags(config, scenario->flags);
88 T_ASSERT_POSIX_ZERO(ret, "set flags %#x on stackshot config", scenario->flags);
90 if (scenario->size_hint > 0) {
91 ret = stackshot_config_set_size_hint(config, scenario->size_hint);
93 T_ASSERT_POSIX_ZERO(ret, "set size hint %" PRIu32 " on stackshot config",
97 if (scenario->target_pid > 0) {
98 ret = stackshot_config_set_pid(config, scenario->target_pid);
100 T_ASSERT_POSIX_ZERO(ret, "set target pid %d on stackshot config",
101 scenario->target_pid);
104 if (scenario->since_timestamp > 0) {
105 ret = stackshot_config_set_delta_timestamp(config, scenario->since_timestamp);
107 T_ASSERT_POSIX_ZERO(ret, "set since timestamp %" PRIu64 " on stackshot config",
108 scenario->since_timestamp);
111 int retries_remaining = 5;
114 uint64_t start_time = mach_absolute_time();
115 ret = stackshot_capture_with_config(config);
116 uint64_t end_time = mach_absolute_time();
118 if (scenario->should_fail) {
120 T_ASSERT_POSIX_ZERO(ret, "called stackshot_capture_with_config");
124 if (ret == EBUSY || ret == ETIMEDOUT) {
125 if (retries_remaining > 0) {
126 if (!scenario->timer) {
127 T_LOG("stackshot_capture_with_config failed with %s (%d), retrying",
134 T_ASSERT_POSIX_ZERO(ret,
135 "called stackshot_capture_with_config (no retries remaining)");
139 T_ASSERT_POSIX_ZERO(ret, "called stackshot_capture_with_config");
142 if (scenario->timer) {
143 dt_stat_mach_time_add(scenario->timer, end_time - start_time);
145 cb(stackshot_config_get_stackshot_buffer(config), stackshot_config_get_stackshot_size(config));
147 ret = stackshot_config_dealloc(config);
148 T_QUIET; T_EXPECT_POSIX_ZERO(ret, "deallocated stackshot config");
151 T_DECL(kcdata, "test that kcdata stackshots can be taken and parsed")
153 struct scenario scenario = {
154 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS |
155 STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT)
159 T_LOG("taking kcdata stackshot");
160 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
161 parse_stackshot(false, ssbuf, sslen);
165 T_DECL(kcdata_faulting, "test that kcdata stackshots while faulting can be taken and parsed")
167 struct scenario scenario = {
168 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS
169 | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT
170 | STACKSHOT_ENABLE_BT_FAULTING | STACKSHOT_ENABLE_UUID_FAULTING),
174 T_LOG("taking faulting stackshot");
175 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
176 parse_stackshot(false, ssbuf, sslen);
180 T_DECL(bad_flags, "test a poorly-formed stackshot syscall")
182 struct scenario scenario = {
183 .flags = STACKSHOT_SAVE_IN_KERNEL_BUFFER /* not allowed from user space */,
187 T_LOG("attempting to take stackshot with kernel-only flag");
188 take_stackshot(&scenario, ^(__unused void *ssbuf, __unused size_t sslen) {
189 T_ASSERT_FAIL("stackshot data callback called");
193 T_DECL(delta, "test delta stackshots")
195 struct scenario scenario = {
196 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS
197 | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT)
201 T_LOG("taking full stackshot");
202 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
203 uint64_t stackshot_time = stackshot_timestamp(ssbuf, sslen);
205 T_LOG("taking delta stackshot since time %" PRIu64, stackshot_time);
207 parse_stackshot(false, ssbuf, sslen);
209 struct scenario delta_scenario = {
210 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS
211 | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT
212 | STACKSHOT_COLLECT_DELTA_SNAPSHOT),
213 .since_timestamp = stackshot_time
216 take_stackshot(&delta_scenario, ^(void *dssbuf, size_t dsslen) {
217 parse_stackshot(true, dssbuf, dsslen);
223 expect_instrs_cycles_in_stackshot(void *ssbuf, size_t sslen)
225 kcdata_iter_t iter = kcdata_iter(ssbuf, sslen);
227 bool in_task = false;
228 bool in_thread = false;
229 bool saw_instrs_cycles = false;
230 iter = kcdata_iter_next(iter);
232 KCDATA_ITER_FOREACH(iter) {
233 switch (kcdata_iter_type(iter)) {
234 case KCDATA_TYPE_CONTAINER_BEGIN:
235 switch (kcdata_iter_container_type(iter)) {
236 case STACKSHOT_KCCONTAINER_TASK:
238 saw_instrs_cycles = false;
241 case STACKSHOT_KCCONTAINER_THREAD:
243 saw_instrs_cycles = false;
251 case STACKSHOT_KCTYPE_INSTRS_CYCLES:
252 saw_instrs_cycles = true;
255 case KCDATA_TYPE_CONTAINER_END:
257 T_QUIET; T_EXPECT_TRUE(saw_instrs_cycles, "saw instructions and cycles in thread");
259 } else if (in_task) {
260 T_QUIET; T_EXPECT_TRUE(saw_instrs_cycles, "saw instructions and cycles in task");
271 skip_if_monotonic_unsupported(void)
274 size_t supported_size = sizeof(supported);
275 int ret = sysctlbyname("kern.monotonic.supported", &supported, &supported_size, 0, 0);
276 if (ret < 0 || !supported) {
277 T_SKIP("monotonic is unsupported");
281 T_DECL(instrs_cycles, "test a getting instructions and cycles in stackshot")
283 skip_if_monotonic_unsupported();
285 struct scenario scenario = {
286 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES
287 | STACKSHOT_KCDATA_FORMAT)
290 T_LOG("attempting to take stackshot with instructions and cycles");
291 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
292 parse_stackshot(false, ssbuf, sslen);
293 expect_instrs_cycles_in_stackshot(ssbuf, sslen);
297 T_DECL(delta_instrs_cycles, "test delta stackshots with instructions and cycles")
299 skip_if_monotonic_unsupported();
301 struct scenario scenario = {
302 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES
303 | STACKSHOT_KCDATA_FORMAT)
307 T_LOG("taking full stackshot");
308 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
309 uint64_t stackshot_time = stackshot_timestamp(ssbuf, sslen);
311 T_LOG("taking delta stackshot since time %" PRIu64, stackshot_time);
313 parse_stackshot(false, ssbuf, sslen);
314 expect_instrs_cycles_in_stackshot(ssbuf, sslen);
316 struct scenario delta_scenario = {
317 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_INSTRS_CYCLES
318 | STACKSHOT_KCDATA_FORMAT
319 | STACKSHOT_COLLECT_DELTA_SNAPSHOT),
320 .since_timestamp = stackshot_time
323 take_stackshot(&delta_scenario, ^(void *dssbuf, size_t dsslen) {
324 parse_stackshot(true, dssbuf, dsslen);
325 expect_instrs_cycles_in_stackshot(dssbuf, dsslen);
331 check_thread_groups_supported()
335 size_t supported_size = sizeof(supported);
336 err = sysctlbyname("kern.thread_groups_supported", &supported, &supported_size, NULL, 0);
338 if (err || !supported)
339 T_SKIP("thread groups not supported on this system");
342 T_DECL(thread_groups, "test getting thread groups in stackshot")
344 check_thread_groups_supported();
346 struct scenario scenario = {
347 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_THREAD_GROUP
348 | STACKSHOT_KCDATA_FORMAT)
351 T_LOG("attempting to take stackshot with thread group flag");
352 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
353 parse_thread_group_stackshot(ssbuf, sslen);
358 #pragma mark performance tests
360 #define SHOULD_REUSE_SIZE_HINT 0x01
361 #define SHOULD_USE_DELTA 0x02
362 #define SHOULD_TARGET_SELF 0x04
365 stackshot_perf(unsigned int options)
367 struct scenario scenario = {
368 .flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS
369 | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT),
372 dt_stat_t size = dt_stat_create("bytes", "size");
373 dt_stat_time_t duration = dt_stat_time_create("duration");
374 scenario.timer = duration;
376 if (options & SHOULD_TARGET_SELF) {
377 scenario.target_pid = getpid();
380 while (!dt_stat_stable(duration) || !dt_stat_stable(size)) {
381 __block uint64_t last_time = 0;
382 __block uint32_t size_hint = 0;
383 take_stackshot(&scenario, ^(void *ssbuf, size_t sslen) {
384 dt_stat_add(size, (double)sslen);
385 last_time = stackshot_timestamp(ssbuf, sslen);
386 size_hint = (uint32_t)sslen;
388 if (options & SHOULD_USE_DELTA) {
389 scenario.since_timestamp = last_time;
390 scenario.flags |= STACKSHOT_COLLECT_DELTA_SNAPSHOT;
392 if (options & SHOULD_REUSE_SIZE_HINT) {
393 scenario.size_hint = size_hint;
397 dt_stat_finalize(duration);
398 dt_stat_finalize(size);
401 T_DECL(perf_no_size_hint, "test stackshot performance with no size hint")
406 T_DECL(perf_size_hint, "test stackshot performance with size hint")
408 stackshot_perf(SHOULD_REUSE_SIZE_HINT);
411 T_DECL(perf_process, "test stackshot performance targeted at process")
413 stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_TARGET_SELF);
416 T_DECL(perf_delta, "test delta stackshot performance")
418 stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_USE_DELTA);
421 T_DECL(perf_delta_process, "test delta stackshot performance targeted at a process")
423 stackshot_perf(SHOULD_REUSE_SIZE_HINT | SHOULD_USE_DELTA | SHOULD_TARGET_SELF);
427 stackshot_timestamp(void *ssbuf, size_t sslen)
429 kcdata_iter_t iter = kcdata_iter(ssbuf, sslen);
431 uint32_t type = kcdata_iter_type(iter);
432 if (type != KCDATA_BUFFER_BEGIN_STACKSHOT && type != KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT) {
433 T_ASSERT_FAIL("invalid kcdata type %u", kcdata_iter_type(iter));
436 iter = kcdata_iter_find_type(iter, KCDATA_TYPE_MACH_ABSOLUTE_TIME);
438 T_ASSERT_TRUE(kcdata_iter_valid(iter), "timestamp found in stackshot");
440 return *(uint64_t *)kcdata_iter_payload(iter);
443 #define TEST_THREAD_NAME "stackshot_test_thread"
446 parse_thread_group_stackshot(void **ssbuf, size_t sslen)
448 bool seen_thread_group_snapshot = false;
449 kcdata_iter_t iter = kcdata_iter(ssbuf, sslen);
450 T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT,
451 "buffer provided is a stackshot");
453 NSMutableSet *thread_groups = [[NSMutableSet alloc] init];
455 iter = kcdata_iter_next(iter);
456 KCDATA_ITER_FOREACH(iter) {
457 switch (kcdata_iter_type(iter)) {
458 case KCDATA_TYPE_ARRAY: {
460 T_ASSERT_TRUE(kcdata_iter_array_valid(iter),
461 "checked that array is valid");
463 if (kcdata_iter_array_elem_type(iter) != STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT) {
467 seen_thread_group_snapshot = true;
469 if (kcdata_iter_array_elem_size(iter) >= sizeof(struct thread_group_snapshot_v2)) {
470 struct thread_group_snapshot_v2 *tgs_array = kcdata_iter_payload(iter);
471 for (uint32_t j = 0; j < kcdata_iter_array_elem_count(iter); j++) {
472 struct thread_group_snapshot_v2 *tgs = tgs_array + j;
473 [thread_groups addObject:@(tgs->tgs_id)];
478 struct thread_group_snapshot *tgs_array = kcdata_iter_payload(iter);
479 for (uint32_t j = 0; j < kcdata_iter_array_elem_count(iter); j++) {
480 struct thread_group_snapshot *tgs = tgs_array + j;
481 [thread_groups addObject:@(tgs->tgs_id)];
488 KCDATA_ITER_FOREACH(iter) {
489 NSError *error = nil;
491 switch (kcdata_iter_type(iter)) {
493 case KCDATA_TYPE_CONTAINER_BEGIN: {
495 T_ASSERT_TRUE(kcdata_iter_container_valid(iter),
496 "checked that container is valid");
498 NSDictionary *container = parseKCDataContainer(&iter, &error);
499 T_QUIET; T_ASSERT_NOTNULL(container, "parsed container from stackshot");
500 T_QUIET; T_ASSERT_NULL(error, "error unset after parsing container");
502 if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_THREAD) {
506 int tg = [container[@"thread_snapshots"][@"thread_group"] intValue];
508 T_ASSERT_TRUE([thread_groups containsObject:@(tg)], "check that the thread group the thread is in exists");
515 T_ASSERT_TRUE(seen_thread_group_snapshot, "check that we have seen a thread group snapshot");
519 parse_stackshot(bool delta, void *ssbuf, size_t sslen)
521 kcdata_iter_t iter = kcdata_iter(ssbuf, sslen);
523 T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT,
524 "buffer provided is a delta stackshot");
526 T_ASSERT_EQ(kcdata_iter_type(iter), KCDATA_BUFFER_BEGIN_STACKSHOT,
527 "buffer provided is a stackshot");
530 iter = kcdata_iter_next(iter);
531 KCDATA_ITER_FOREACH(iter) {
532 NSError *error = nil;
534 switch (kcdata_iter_type(iter)) {
535 case KCDATA_TYPE_ARRAY: {
537 T_ASSERT_TRUE(kcdata_iter_array_valid(iter),
538 "checked that array is valid");
540 NSMutableDictionary *array = parseKCDataArray(iter, &error);
541 T_QUIET; T_ASSERT_NOTNULL(array, "parsed array from stackshot");
542 T_QUIET; T_ASSERT_NULL(error, "error unset after parsing array");
546 case KCDATA_TYPE_CONTAINER_BEGIN: {
548 T_ASSERT_TRUE(kcdata_iter_container_valid(iter),
549 "checked that container is valid");
551 NSDictionary *container = parseKCDataContainer(&iter, &error);
552 T_QUIET; T_ASSERT_NOTNULL(container, "parsed container from stackshot");
553 T_QUIET; T_ASSERT_NULL(error, "error unset after parsing container");
555 if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_TASK) {
558 int pid = [container[@"task_snapshots"][@"task_snapshot"][@"ts_pid"] intValue];
559 if (pid != getpid()) {
563 T_EXPECT_EQ_STR(current_process_name(),
564 [container[@"task_snapshots"][@"task_snapshot"][@"ts_p_comm"] UTF8String],
565 "current process name matches in stackshot");
568 T_EXPECT_LE(pid, [container[@"task_snapshots"][@"task_snapshot"][@"ts_unique_pid"] intValue],
569 "unique pid is greater than pid");
571 bool found_main_thread = 0;
572 for (id thread_key in container[@"task_snapshots"][@"thread_snapshots"]) {
573 NSMutableDictionary *thread = container[@"task_snapshots"][@"thread_snapshots"][thread_key];
574 NSDictionary *thread_snap = thread[@"thread_snapshot"];
576 T_QUIET; T_EXPECT_GT([thread_snap[@"ths_thread_id"] intValue], 0,
577 "thread ID of thread in current task is valid");
578 T_QUIET; T_EXPECT_GT([thread_snap[@"ths_total_syscalls"] intValue], 0,
579 "total syscalls of thread in current task is valid");
580 T_QUIET; T_EXPECT_GT([thread_snap[@"ths_base_priority"] intValue], 0,
581 "base priority of thread in current task is valid");
582 T_QUIET; T_EXPECT_GT([thread_snap[@"ths_sched_priority"] intValue], 0,
583 "scheduling priority of thread in current task is valid");
585 NSString *pth_name = thread_snap[@"pth_name"];
586 if (pth_name != nil && [pth_name isEqualToString:@TEST_THREAD_NAME]) {
587 found_main_thread = true;
590 T_EXPECT_TRUE(found_main_thread, "found main thread for current task in stackshot");
596 T_ASSERT_FALSE(KCDATA_ITER_FOREACH_FAILED(iter), "successfully iterated kcdata");
600 current_process_name(void)
602 static char name[64];
605 int ret = proc_name(getpid(), name, sizeof(name));
607 T_ASSERT_POSIX_ZERO(ret, "proc_pidname failed for current process");
614 initialize_thread(void)
616 int ret = pthread_setname_np(TEST_THREAD_NAME);
618 T_ASSERT_POSIX_ZERO(ret, "set thread name to %s", TEST_THREAD_NAME);