1 /* 2 * Copyright (c) 2016-2020 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29 #include <kern/backtrace.h> 30 #include <kern/kalloc.h> 31 #include <sys/errno.h> 32 #include <sys/sysctl.h> 33 #include <sys/systm.h> 34 35 #if DEVELOPMENT || DEBUG 36 37 #define MAX_BACKTRACE (128) 38 39 #define BACKTRACE_USER (0) 40 #define BACKTRACE_USER_RESUME (1) 41 42 static int backtrace_sysctl SYSCTL_HANDLER_ARGS; 43 44 SYSCTL_NODE(_kern, OID_AUTO, backtrace, CTLFLAG_RW | CTLFLAG_LOCKED, 0, 45 "backtrace"); 46 47 SYSCTL_PROC(_kern_backtrace, OID_AUTO, user, 48 CTLFLAG_RW | CTLFLAG_LOCKED, (void *)BACKTRACE_USER, 49 sizeof(uint64_t), backtrace_sysctl, "O", 50 "take user backtrace of current thread"); 51 52 static int 53 backtrace_sysctl SYSCTL_HANDLER_ARGS 54 { 55 #pragma unused(oidp, arg1, arg2) 56 unsigned int scenario = (unsigned int)req->newlen; 57 uintptr_t *bt = NULL; 58 unsigned int bt_len = 0, bt_filled = 0, bt_space = 0; 59 size_t bt_size = 0; 60 errno_t error = 0; 61 62 bool user_scenario = scenario == BACKTRACE_USER; 63 bool resume_scenario = scenario == BACKTRACE_USER_RESUME; 64 if (!user_scenario && !resume_scenario) { 65 return ENOTSUP; 66 } 67 68 if (req->oldptr == USER_ADDR_NULL || req->oldlen == 0) { 69 return EFAULT; 70 } 71 72 bt_len = req->oldlen > MAX_BACKTRACE ? MAX_BACKTRACE : 73 (unsigned int)req->oldlen; 74 bt_size = sizeof(bt[0]) * bt_len; 75 bt = kalloc_data(bt_size, Z_WAITOK | Z_ZERO); 76 if (!bt) { 77 return ENOBUFS; 78 } 79 bt_space = resume_scenario ? bt_len / 2 : bt_len; 80 struct backtrace_user_info btinfo = BTUINFO_INIT; 81 bt_filled = backtrace_user(bt, bt_space, NULL, &btinfo); 82 error = btinfo.btui_error; 83 if (error != 0) { 84 goto out; 85 } 86 if (resume_scenario) { 87 if (!(btinfo.btui_info & BTI_TRUNCATED)) { 88 error = ENOSPC; 89 goto out; 90 } 91 struct backtrace_control ctl = { 92 .btc_frame_addr = btinfo.btui_next_frame_addr, 93 }; 94 btinfo = BTUINFO_INIT; 95 unsigned int bt_more = backtrace_user(bt + bt_filled, bt_space, &ctl, 96 &btinfo); 97 error = btinfo.btui_error; 98 if (error != 0) { 99 goto out; 100 } 101 bt_filled += bt_more; 102 } 103 bt_filled = min(bt_filled, bt_len); 104 if (btinfo.btui_async_frame_addr != 0 && 105 btinfo.btui_async_start_index != 0) { 106 // Put the async call stack inline after the real call stack. 107 unsigned int start_index = btinfo.btui_async_start_index; 108 uintptr_t frame_addr = btinfo.btui_async_frame_addr; 109 unsigned int bt_left = bt_len - start_index; 110 struct backtrace_control ctl = { .btc_frame_addr = frame_addr, }; 111 btinfo = BTUINFO_INIT; 112 unsigned int async_filled = backtrace_user(bt + start_index, bt_left, 113 &ctl, &btinfo); 114 error = btinfo.btui_error; 115 if (error != 0) { 116 goto out; 117 } 118 bt_filled = min(start_index + async_filled, bt_len); 119 } 120 121 error = copyout(bt, req->oldptr, sizeof(bt[0]) * bt_filled); 122 if (error) { 123 goto out; 124 } 125 req->oldidx = bt_filled; 126 127 out: 128 kfree_data(bt, bt_size); 129 return error; 130 } 131 132 #endif /* DEVELOPMENT || DEBUG */ 133