]>
Commit | Line | Data |
---|---|---|
39037602 A |
1 | #include <darwintest.h> |
2 | #include <dispatch/dispatch.h> | |
3 | #include <inttypes.h> | |
4 | #include <ktrace.h> | |
5 | #include <ktrace_private.h> | |
6 | #include <mach/dyld_kernel.h> | |
7 | #include <mach/host_info.h> | |
8 | #include <mach/mach.h> | |
9 | #include <mach/mach_init.h> | |
10 | #include <mach/task.h> | |
11 | #include <os/assumes.h> | |
12 | #include <sys/kdebug.h> | |
13 | #include <sys/kdebug_signpost.h> | |
14 | #include <sys/sysctl.h> | |
15 | ||
16 | #define KTRACE_WAIT_TIMEOUT_S (10) | |
17 | ||
18 | #define TRACE_DEBUGID (0xfedfed00U) | |
19 | ||
20 | T_DECL(kdebug_trace_syscall, "test that kdebug_trace(2) emits correct events", | |
21 | T_META_ASROOT(YES)) | |
22 | { | |
23 | ktrace_session_t s; | |
24 | dispatch_time_t timeout; | |
25 | __block int events_seen = 0; | |
26 | ||
27 | s = ktrace_session_create(); | |
28 | os_assert(s != NULL); | |
29 | ||
30 | ktrace_events_class(s, DBG_MACH, ^(__unused struct trace_point *tp){}); | |
31 | ktrace_events_single(s, TRACE_DEBUGID, ^void(struct trace_point *tp) { | |
32 | events_seen++; | |
33 | T_PASS("saw traced event"); | |
34 | ||
35 | T_EXPECT_EQ(tp->arg1, 1UL, "argument 1 of traced event is correct"); | |
36 | T_EXPECT_EQ(tp->arg2, 2UL, "argument 2 of traced event is correct"); | |
37 | T_EXPECT_EQ(tp->arg3, 3UL, "argument 3 of traced event is correct"); | |
38 | T_EXPECT_EQ(tp->arg4, 4UL, "argument 4 of traced event is correct"); | |
39 | ||
40 | ktrace_end(s, 1); | |
41 | }); | |
42 | ||
43 | ktrace_set_completion_handler(s, ^(void) { | |
44 | T_EXPECT_GE(events_seen, 1, NULL); | |
45 | ktrace_session_destroy(s); | |
46 | T_END; | |
47 | }); | |
48 | ||
49 | ktrace_filter_pid(s, getpid()); | |
50 | ||
51 | T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL); | |
52 | T_ASSERT_POSIX_SUCCESS(kdebug_trace(TRACE_DEBUGID, 1, 2, 3, 4), NULL); | |
53 | ktrace_end(s, 0); | |
54 | ||
55 | dispatch_main(); | |
56 | } | |
57 | ||
58 | #define SIGNPOST_SINGLE_CODE (0x10U) | |
59 | #define SIGNPOST_PAIRED_CODE (0x20U) | |
60 | ||
61 | T_DECL(kdebug_signpost_syscall, | |
62 | "test that kdebug_signpost(2) emits correct events", | |
63 | T_META_ASROOT(YES)) | |
64 | { | |
65 | ktrace_session_t s; | |
66 | __block int single_seen = 0; | |
67 | __block int paired_seen = 0; | |
68 | dispatch_time_t timeout; | |
69 | ||
70 | s = ktrace_session_create(); | |
71 | T_ASSERT_NOTNULL(s, NULL); | |
72 | ||
73 | /* make sure to get enough events for the KDBUFWAIT to trigger */ | |
74 | // ktrace_events_class(s, DBG_MACH, ^(__unused struct trace_point *tp){}); | |
75 | ktrace_events_single(s, | |
76 | APPSDBG_CODE(DBG_APP_SIGNPOST, SIGNPOST_SINGLE_CODE), | |
77 | ^void(struct trace_point *tp) | |
78 | { | |
79 | single_seen++; | |
80 | T_PASS("single signpost is traced"); | |
81 | ||
82 | T_EXPECT_EQ(tp->arg1, 1UL, "argument 1 of single signpost is correct"); | |
83 | T_EXPECT_EQ(tp->arg2, 2UL, "argument 2 of single signpost is correct"); | |
84 | T_EXPECT_EQ(tp->arg3, 3UL, "argument 3 of single signpost is correct"); | |
85 | T_EXPECT_EQ(tp->arg4, 4UL, "argument 4 of single signpost is correct"); | |
86 | }); | |
87 | ||
88 | ktrace_events_single_paired(s, | |
89 | APPSDBG_CODE(DBG_APP_SIGNPOST, SIGNPOST_PAIRED_CODE), | |
90 | ^void(struct trace_point *start, struct trace_point *end) | |
91 | { | |
92 | paired_seen++; | |
93 | T_PASS("paired signposts are traced"); | |
94 | ||
95 | T_EXPECT_EQ(start->arg1, 5UL, "argument 1 of start signpost is correct"); | |
96 | T_EXPECT_EQ(start->arg2, 6UL, "argument 2 of start signpost is correct"); | |
97 | T_EXPECT_EQ(start->arg3, 7UL, "argument 3 of start signpost is correct"); | |
98 | T_EXPECT_EQ(start->arg4, 8UL, "argument 4 of start signpost is correct"); | |
99 | ||
100 | T_EXPECT_EQ(end->arg1, 9UL, "argument 1 of end signpost is correct"); | |
101 | T_EXPECT_EQ(end->arg2, 10UL, "argument 2 of end signpost is correct"); | |
102 | T_EXPECT_EQ(end->arg3, 11UL, "argument 3 of end signpost is correct"); | |
103 | T_EXPECT_EQ(end->arg4, 12UL, "argument 4 of end signpost is correct"); | |
104 | ||
105 | T_EXPECT_EQ(single_seen, 1, | |
106 | "signposts are traced in the correct order"); | |
107 | ||
108 | ktrace_end(s, 1); | |
109 | }); | |
110 | ||
111 | ktrace_set_completion_handler(s, ^(void) { | |
112 | if (single_seen == 0) { | |
113 | T_FAIL("did not see single tracepoint before timeout"); | |
114 | } | |
115 | if (paired_seen == 0) { | |
116 | T_FAIL("did not see paired tracepoints before timeout"); | |
117 | } | |
118 | ktrace_session_destroy(s); | |
119 | T_END; | |
120 | }); | |
121 | ||
122 | ktrace_filter_pid(s, getpid()); | |
123 | ||
124 | T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL); | |
125 | ||
126 | T_EXPECT_POSIX_SUCCESS(kdebug_signpost( | |
127 | SIGNPOST_SINGLE_CODE, 1, 2, 3, 4), NULL); | |
128 | T_EXPECT_POSIX_SUCCESS(kdebug_signpost_start( | |
129 | SIGNPOST_PAIRED_CODE, 5, 6, 7, 8), NULL); | |
130 | T_EXPECT_POSIX_SUCCESS(kdebug_signpost_end( | |
131 | SIGNPOST_PAIRED_CODE, 9, 10, 11, 12), NULL); | |
132 | ktrace_end(s, 0); | |
133 | ||
134 | dispatch_main(); | |
135 | } | |
136 | ||
137 | #define WRAPPING_EVENTS_COUNT (150000) | |
138 | #define TRACE_ITERATIONS (5000) | |
139 | #define WRAPPING_EVENTS_THRESHOLD (100) | |
140 | ||
141 | T_DECL(kdebug_wrapping, | |
142 | "ensure that wrapping traces lost events and no events prior to the wrap", | |
143 | T_META_ASROOT(YES), T_META_CHECK_LEAKS(NO)) | |
144 | { | |
145 | ktrace_session_t s; | |
146 | __block int events = 0; | |
147 | int mib[4]; | |
148 | size_t needed; | |
149 | kbufinfo_t buf_info; | |
150 | int wait_wrapping_secs = (WRAPPING_EVENTS_COUNT / TRACE_ITERATIONS) + 5; | |
151 | int current_secs = wait_wrapping_secs; | |
152 | ||
153 | /* use sysctls manually to bypass libktrace assumptions */ | |
154 | ||
155 | mib[0] = CTL_KERN; mib[1] = KERN_KDEBUG; mib[2] = KERN_KDSETUP; mib[3] = 0; | |
156 | needed = 0; | |
157 | T_ASSERT_POSIX_SUCCESS(sysctl(mib, 3, NULL, &needed, NULL, 0), | |
158 | "KERN_KDSETUP"); | |
159 | ||
160 | mib[2] = KERN_KDSETBUF; mib[3] = WRAPPING_EVENTS_COUNT; | |
161 | T_ASSERT_POSIX_SUCCESS(sysctl(mib, 4, NULL, 0, NULL, 0), "KERN_KDSETBUF"); | |
162 | ||
163 | mib[2] = KERN_KDENABLE; mib[3] = 1; | |
164 | T_ASSERT_POSIX_SUCCESS(sysctl(mib, 4, NULL, 0, NULL, 0), "KERN_KDENABLE"); | |
165 | ||
166 | /* wrapping is on by default */ | |
167 | ||
168 | /* wait until wrapped */ | |
169 | T_LOG("waiting for trace to wrap"); | |
170 | mib[2] = KERN_KDGETBUF; | |
171 | needed = sizeof(buf_info); | |
172 | do { | |
173 | sleep(1); | |
174 | for (int i = 0; i < TRACE_ITERATIONS; i++) { | |
175 | T_QUIET; | |
176 | T_ASSERT_POSIX_SUCCESS(kdebug_trace(0xfefe0000, 0, 0, 0, 0), NULL); | |
177 | } | |
178 | T_QUIET; | |
179 | T_ASSERT_POSIX_SUCCESS(sysctl(mib, 3, &buf_info, &needed, NULL, 0), | |
180 | NULL); | |
181 | } while (!(buf_info.flags & KDBG_WRAPPED) && --current_secs > 0); | |
182 | ||
183 | T_ASSERT_TRUE(buf_info.flags & KDBG_WRAPPED, | |
184 | "trace wrapped (after %d seconds within %d second timeout)", | |
185 | wait_wrapping_secs - current_secs, wait_wrapping_secs); | |
186 | ||
187 | s = ktrace_session_create(); | |
188 | T_QUIET; T_ASSERT_NOTNULL(s, NULL); | |
189 | T_QUIET; T_ASSERT_POSIX_ZERO(ktrace_set_use_existing(s), NULL); | |
190 | ||
191 | ktrace_events_all(s, ^void(struct trace_point *tp) { | |
192 | if (events == 0) { | |
193 | T_EXPECT_EQ(tp->debugid, (unsigned int)TRACE_LOST_EVENTS, | |
194 | "first event's debugid 0x%08x (%s) should be TRACE_LOST_EVENTS", | |
195 | tp->debugid, | |
196 | ktrace_name_for_eventid(s, tp->debugid & KDBG_EVENTID_MASK)); | |
197 | } else { | |
198 | T_QUIET; | |
199 | T_EXPECT_NE(tp->debugid, (unsigned int)TRACE_LOST_EVENTS, | |
200 | "event debugid 0x%08x (%s) should not be TRACE_LOST_EVENTS", | |
201 | tp->debugid, | |
202 | ktrace_name_for_eventid(s, tp->debugid & KDBG_EVENTID_MASK)); | |
203 | } | |
204 | ||
205 | events++; | |
206 | if (events > WRAPPING_EVENTS_THRESHOLD) { | |
207 | ktrace_end(s, 1); | |
208 | } | |
209 | }); | |
210 | ||
211 | ktrace_set_completion_handler(s, ^(void) { | |
212 | ktrace_session_destroy(s); | |
213 | T_END; | |
214 | }); | |
215 | ||
216 | T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL); | |
217 | ||
218 | dispatch_main(); | |
219 | } | |
220 | ||
221 | __attribute__((aligned(8))) | |
222 | static const char map_uuid[16] = "map UUID"; | |
223 | ||
224 | __attribute__((aligned(8))) | |
225 | static const char unmap_uuid[16] = "unmap UUID"; | |
226 | ||
227 | __attribute__((aligned(8))) | |
228 | static const char sc_uuid[16] = "shared UUID"; | |
229 | ||
230 | static fsid_t map_fsid = { .val = { 42, 43 } }; | |
231 | static fsid_t unmap_fsid = { .val = { 44, 45 } }; | |
232 | static fsid_t sc_fsid = { .val = { 46, 47 } }; | |
233 | ||
234 | static fsobj_id_t map_fsobjid = { .fid_objno = 42, .fid_generation = 43 }; | |
235 | static fsobj_id_t unmap_fsobjid = { .fid_objno = 44, .fid_generation = 45 }; | |
236 | static fsobj_id_t sc_fsobjid = { .fid_objno = 46, .fid_generation = 47 }; | |
237 | ||
238 | #define MAP_LOAD_ADDR 0xabadcafe | |
239 | #define UNMAP_LOAD_ADDR 0xfeedface | |
240 | #define SC_LOAD_ADDR 0xfedfaced | |
241 | ||
242 | __unused | |
243 | static void | |
244 | expect_dyld_image_info(struct trace_point *tp, const uint64_t *exp_uuid, | |
245 | uint64_t exp_load_addr, fsid_t *exp_fsid, fsobj_id_t *exp_fsobjid, | |
246 | int order) | |
247 | { | |
248 | #if defined(__LP64__) | |
249 | if (order == 0) { | |
250 | uint64_t uuid[2]; | |
251 | uint64_t load_addr; | |
252 | fsid_t fsid; | |
253 | ||
254 | uuid[0] = (uint64_t)tp->arg1; | |
255 | uuid[1] = (uint64_t)tp->arg2; | |
256 | load_addr = (uint64_t)tp->arg3; | |
257 | fsid.val[0] = (int32_t)(tp->arg4 & UINT32_MAX); | |
258 | fsid.val[1] = (int32_t)((uint64_t)tp->arg4 >> 32); | |
259 | ||
260 | T_QUIET; T_EXPECT_EQ(uuid[0], exp_uuid[0], NULL); | |
261 | T_QUIET; T_EXPECT_EQ(uuid[1], exp_uuid[1], NULL); | |
262 | T_QUIET; T_EXPECT_EQ(load_addr, exp_load_addr, NULL); | |
263 | T_QUIET; T_EXPECT_EQ(fsid.val[0], exp_fsid->val[0], NULL); | |
264 | T_QUIET; T_EXPECT_EQ(fsid.val[1], exp_fsid->val[1], NULL); | |
265 | } else if (order == 1) { | |
266 | fsobj_id_t fsobjid; | |
267 | ||
268 | fsobjid.fid_objno = (uint32_t)(tp->arg1 & UINT32_MAX); | |
269 | fsobjid.fid_generation = (uint32_t)((uint64_t)tp->arg1 >> 32); | |
270 | ||
271 | T_QUIET; T_EXPECT_EQ(fsobjid.fid_objno, exp_fsobjid->fid_objno, NULL); | |
272 | T_QUIET; T_EXPECT_EQ(fsobjid.fid_generation, | |
273 | exp_fsobjid->fid_generation, NULL); | |
274 | } else { | |
275 | T_ASSERT_FAIL("unrecognized order of events %d", order); | |
276 | } | |
277 | #else /* defined(__LP64__) */ | |
278 | if (order == 0) { | |
279 | uint32_t uuid[4]; | |
280 | ||
281 | uuid[0] = (uint32_t)tp->arg1; | |
282 | uuid[1] = (uint32_t)tp->arg2; | |
283 | uuid[2] = (uint32_t)tp->arg3; | |
284 | uuid[3] = (uint32_t)tp->arg4; | |
285 | ||
286 | T_QUIET; T_EXPECT_EQ(uuid[0], (uint32_t)exp_uuid[0], NULL); | |
287 | T_QUIET; T_EXPECT_EQ(uuid[1], (uint32_t)(exp_uuid[0] >> 32), NULL); | |
288 | T_QUIET; T_EXPECT_EQ(uuid[2], (uint32_t)exp_uuid[1], NULL); | |
289 | T_QUIET; T_EXPECT_EQ(uuid[3], (uint32_t)(exp_uuid[1] >> 32), NULL); | |
290 | } else if (order == 1) { | |
291 | uint32_t load_addr; | |
292 | fsid_t fsid; | |
293 | fsobj_id_t fsobjid; | |
294 | ||
295 | load_addr = (uint32_t)tp->arg1; | |
296 | fsid.val[0] = (int32_t)tp->arg2; | |
297 | fsid.val[1] = (int32_t)tp->arg3; | |
298 | fsobjid.fid_objno = (uint32_t)tp->arg4; | |
299 | ||
300 | T_QUIET; T_EXPECT_EQ(load_addr, (uint32_t)exp_load_addr, NULL); | |
301 | T_QUIET; T_EXPECT_EQ(fsid.val[0], exp_fsid->val[0], NULL); | |
302 | T_QUIET; T_EXPECT_EQ(fsid.val[1], exp_fsid->val[1], NULL); | |
303 | T_QUIET; T_EXPECT_EQ(fsobjid.fid_objno, exp_fsobjid->fid_objno, NULL); | |
304 | } else if (order == 2) { | |
305 | fsobj_id_t fsobjid; | |
306 | ||
307 | fsobjid.fid_generation = tp->arg1; | |
308 | ||
309 | T_QUIET; T_EXPECT_EQ(fsobjid.fid_generation, | |
310 | exp_fsobjid->fid_generation, NULL); | |
311 | } else { | |
312 | T_ASSERT_FAIL("unrecognized order of events %d", order); | |
313 | } | |
314 | #endif /* defined(__LP64__) */ | |
315 | } | |
316 | ||
317 | #if defined(__LP64__) | |
318 | #define DYLD_CODE_OFFSET (0) | |
319 | #define DYLD_EVENTS (2) | |
320 | #else | |
321 | #define DYLD_CODE_OFFSET (2) | |
322 | #define DYLD_EVENTS (3) | |
323 | #endif | |
324 | ||
325 | static void | |
326 | expect_dyld_events(ktrace_session_t s, const char *name, uint32_t base_code, | |
327 | const char *exp_uuid, uint64_t exp_load_addr, fsid_t *exp_fsid, | |
328 | fsobj_id_t *exp_fsobjid, uint8_t *saw_events) | |
329 | { | |
330 | for (int i = 0; i < DYLD_EVENTS; i++) { | |
331 | ktrace_events_single(s, | |
332 | KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID, | |
333 | base_code + DYLD_CODE_OFFSET + (unsigned int)i), | |
334 | ^(struct trace_point *tp) | |
335 | { | |
336 | T_LOG("checking %s event %c", name, 'A' + i); | |
337 | expect_dyld_image_info(tp, (const void *)exp_uuid, exp_load_addr, | |
338 | exp_fsid, exp_fsobjid, i); | |
339 | *saw_events |= (1U << i); | |
340 | }); | |
341 | } | |
342 | } | |
343 | ||
344 | T_DECL(dyld_events, "test that dyld registering libraries emits events", | |
345 | T_META_ASROOT(YES)) | |
346 | { | |
347 | ktrace_session_t s; | |
348 | dyld_kernel_image_info_t info; | |
349 | ||
350 | /* | |
351 | * Use pointers instead of __block variables in order to use these variables | |
352 | * in the completion block below _and_ pass pointers to them to the | |
353 | * expect_dyld_events function. | |
354 | */ | |
355 | uint8_t saw_events[3] = { 0 }; | |
356 | uint8_t *saw_mapping = &(saw_events[0]); | |
357 | uint8_t *saw_unmapping = &(saw_events[1]); | |
358 | uint8_t *saw_shared_cache = &(saw_events[2]); | |
359 | ||
360 | s = ktrace_session_create(); | |
361 | T_ASSERT_NOTNULL(s, NULL); | |
362 | ||
363 | expect_dyld_events(s, "mapping", DBG_DYLD_UUID_MAP_A, map_uuid, | |
364 | MAP_LOAD_ADDR, &map_fsid, &map_fsobjid, saw_mapping); | |
365 | expect_dyld_events(s, "unmapping", DBG_DYLD_UUID_UNMAP_A, unmap_uuid, | |
366 | UNMAP_LOAD_ADDR, &unmap_fsid, &unmap_fsobjid, saw_unmapping); | |
367 | expect_dyld_events(s, "shared cache", DBG_DYLD_UUID_SHARED_CACHE_A, | |
368 | sc_uuid, SC_LOAD_ADDR, &sc_fsid, &sc_fsobjid, saw_shared_cache); | |
369 | ||
370 | ktrace_set_completion_handler(s, ^(void) { | |
371 | T_EXPECT_EQ(__builtin_popcount(*saw_mapping), DYLD_EVENTS, NULL); | |
372 | T_EXPECT_EQ(__builtin_popcount(*saw_unmapping), DYLD_EVENTS, NULL); | |
373 | T_EXPECT_EQ(__builtin_popcount(*saw_shared_cache), DYLD_EVENTS, NULL); | |
374 | ktrace_session_destroy(s); | |
375 | T_END; | |
376 | }); | |
377 | ||
378 | T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL); | |
379 | ||
380 | info.load_addr = MAP_LOAD_ADDR; | |
381 | memcpy(info.uuid, map_uuid, sizeof(info.uuid)); | |
382 | info.fsid = map_fsid; | |
383 | info.fsobjid = map_fsobjid; | |
384 | T_EXPECT_MACH_SUCCESS(task_register_dyld_image_infos(mach_task_self(), | |
385 | &info, 1), NULL); | |
386 | ||
387 | info.load_addr = UNMAP_LOAD_ADDR; | |
388 | memcpy(info.uuid, unmap_uuid, sizeof(info.uuid)); | |
389 | info.fsid = unmap_fsid; | |
390 | info.fsobjid = unmap_fsobjid; | |
391 | T_EXPECT_MACH_SUCCESS(task_unregister_dyld_image_infos(mach_task_self(), | |
392 | &info, 1), NULL); | |
393 | ||
394 | info.load_addr = SC_LOAD_ADDR; | |
395 | memcpy(info.uuid, sc_uuid, sizeof(info.uuid)); | |
396 | info.fsid = sc_fsid; | |
397 | info.fsobjid = sc_fsobjid; | |
398 | T_EXPECT_MACH_SUCCESS(task_register_dyld_shared_cache_image_info( | |
399 | mach_task_self(), info, FALSE, FALSE), NULL); | |
400 | ||
401 | ktrace_end(s, 0); | |
402 | ||
403 | dispatch_main(); | |
404 | } | |
405 | ||
406 | #define EXP_KERNEL_EVENTS 5U | |
407 | ||
408 | static const uint32_t dev_evts[EXP_KERNEL_EVENTS] = { | |
409 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 0), | |
410 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 1), | |
411 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 2), | |
412 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 3), | |
413 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 4), | |
414 | }; | |
415 | ||
416 | static const uint32_t rel_evts[EXP_KERNEL_EVENTS] = { | |
417 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 5), | |
418 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 6), | |
419 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 7), | |
420 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 8), | |
421 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 9), | |
422 | }; | |
423 | ||
424 | static const uint32_t filt_evts[EXP_KERNEL_EVENTS] = { | |
425 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 10), | |
426 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 11), | |
427 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 12), | |
428 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 13), | |
429 | BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 14), | |
430 | }; | |
431 | ||
432 | static bool | |
433 | is_development_kernel(void) | |
434 | { | |
435 | static dispatch_once_t is_development_once; | |
436 | static bool is_development; | |
437 | ||
438 | dispatch_once(&is_development_once, ^(void) { | |
439 | host_debug_info_internal_data_t info; | |
440 | mach_msg_type_number_t count = HOST_DEBUG_INFO_INTERNAL_COUNT; | |
441 | kern_return_t kr; | |
442 | ||
443 | kr = host_info(mach_host_self(), HOST_DEBUG_INFO_INTERNAL, | |
444 | (host_info_t)(void *)&info, &count); | |
445 | if (kr != KERN_SUCCESS && kr != KERN_NOT_SUPPORTED) { | |
446 | T_ASSERT_FAIL("check for development kernel failed %d", kr); | |
447 | } | |
448 | ||
449 | is_development = (kr == KERN_SUCCESS); | |
450 | }); | |
451 | ||
452 | return is_development; | |
453 | } | |
454 | ||
455 | static void | |
456 | assert_kdebug_test(void) | |
457 | { | |
458 | int mib[] = { CTL_KERN, KERN_KDEBUG, KERN_KDTEST }; | |
459 | T_ASSERT_POSIX_SUCCESS( | |
460 | sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, NULL, NULL, 0), | |
461 | "KERN_KDTEST"); | |
462 | } | |
463 | ||
464 | static void | |
465 | expect_event(struct trace_point *tp, unsigned int *events, | |
466 | const uint32_t *event_ids, size_t event_ids_len) | |
467 | { | |
468 | unsigned int event_idx = *events; | |
469 | bool event_found = false; | |
470 | size_t i; | |
471 | for (i = 0; i < event_ids_len; i++) { | |
472 | if (event_ids[i] == (tp->debugid & KDBG_EVENTID_MASK)) { | |
473 | T_LOG("found event 0x%x", tp->debugid); | |
474 | event_found = true; | |
475 | } | |
476 | } | |
477 | ||
478 | if (!event_found) { | |
479 | return; | |
480 | } | |
481 | ||
482 | *events += 1; | |
483 | for (i = 0; i < event_idx; i++) { | |
484 | T_QUIET; T_EXPECT_EQ(((uintptr_t *)&tp->arg1)[i], (uintptr_t)i + 1, | |
485 | NULL); | |
486 | } | |
487 | for (; i < 4; i++) { | |
488 | T_QUIET; T_EXPECT_EQ(((uintptr_t *)&tp->arg1)[i], (uintptr_t)0, NULL); | |
489 | } | |
490 | } | |
491 | ||
492 | static void | |
493 | expect_release_event(struct trace_point *tp, unsigned int *events) | |
494 | { | |
495 | expect_event(tp, events, rel_evts, | |
496 | sizeof(rel_evts) / sizeof(rel_evts[0])); | |
497 | } | |
498 | ||
499 | static void | |
500 | expect_development_event(struct trace_point *tp, unsigned int *events) | |
501 | { | |
502 | expect_event(tp, events, dev_evts, | |
503 | sizeof(dev_evts) / sizeof(dev_evts[0])); | |
504 | } | |
505 | ||
506 | static void | |
507 | expect_filtered_event(struct trace_point *tp, unsigned int *events) | |
508 | { | |
509 | expect_event(tp, events, filt_evts, | |
510 | sizeof(filt_evts) / sizeof(filt_evts[0])); | |
511 | } | |
512 | ||
513 | T_DECL(kernel_events, "ensure kernel macros work", | |
514 | T_META_ASROOT(YES)) | |
515 | { | |
516 | ktrace_session_t s; | |
517 | ||
518 | s = ktrace_session_create(); | |
519 | T_QUIET; T_ASSERT_NOTNULL(s, NULL); | |
520 | ||
521 | __block unsigned int dev_seen = 0; | |
522 | __block unsigned int rel_seen = 0; | |
523 | __block unsigned int filt_seen = 0; | |
524 | ktrace_events_range(s, KDBG_EVENTID(DBG_BSD, DBG_BSD_KDEBUG_TEST, 0), | |
525 | KDBG_EVENTID(DBG_BSD + 1, 0, 0), | |
526 | ^(struct trace_point *tp) | |
527 | { | |
528 | expect_development_event(tp, &dev_seen); | |
529 | expect_release_event(tp, &rel_seen); | |
530 | expect_filtered_event(tp, &filt_seen); | |
531 | }); | |
532 | ||
533 | ktrace_set_completion_handler(s, ^(void) { | |
534 | T_EXPECT_EQ(rel_seen, EXP_KERNEL_EVENTS, NULL); | |
535 | T_EXPECT_EQ(dev_seen, is_development_kernel() ? EXP_KERNEL_EVENTS : 0U, | |
536 | NULL); | |
537 | T_EXPECT_EQ(filt_seen, EXP_KERNEL_EVENTS, NULL); | |
538 | ktrace_session_destroy(s); | |
539 | T_END; | |
540 | }); | |
541 | ||
542 | ktrace_filter_pid(s, getpid()); | |
543 | ||
544 | T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL); | |
545 | assert_kdebug_test(); | |
546 | ||
547 | ktrace_end(s, 0); | |
548 | ||
549 | dispatch_main(); | |
550 | } | |
551 | ||
552 | T_DECL(kernel_events_filtered, "ensure that the filtered kernel macros work", | |
553 | T_META_ASROOT(YES)) | |
554 | { | |
555 | ktrace_session_t s; | |
556 | ||
557 | s = ktrace_session_create(); | |
558 | T_QUIET; T_ASSERT_NOTNULL(s, NULL); | |
559 | ||
560 | __block unsigned int dev_seen = 0; | |
561 | __block unsigned int rel_seen = 0; | |
562 | __block unsigned int filt_seen = 0; | |
563 | ktrace_events_all(s, ^(struct trace_point *tp) { | |
564 | expect_development_event(tp, &dev_seen); | |
565 | expect_release_event(tp, &rel_seen); | |
566 | /* to make sure no filtered events are emitted */ | |
567 | expect_filtered_event(tp, &filt_seen); | |
568 | }); | |
569 | ||
570 | ktrace_set_completion_handler(s, ^(void) { | |
571 | ktrace_session_destroy(s); | |
572 | ||
573 | T_EXPECT_EQ(rel_seen, EXP_KERNEL_EVENTS, NULL); | |
574 | T_EXPECT_EQ(dev_seen, EXP_KERNEL_EVENTS, NULL); | |
575 | T_EXPECT_EQ(filt_seen, 0U, NULL); | |
576 | T_END; | |
577 | }); | |
578 | ||
579 | T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL); | |
580 | assert_kdebug_test(); | |
581 | ||
582 | ktrace_end(s, 0); | |
583 | ||
584 | dispatch_main(); | |
585 | } | |
586 |