xref: /xnu-8796.141.3/tools/cred_dump_backtraces.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /* quick and dirty hack to grab credential backtrace info from kernel via sysctl.
2*1b191cb5SApple OSS Distributions  * sysctl is only defined if xnu is built with DEBUG_CRED defined.
3*1b191cb5SApple OSS Distributions  * The current version of this is used to target a specific credential and gather
4*1b191cb5SApple OSS Distributions  * backtrace info on all references and unreferences.
5*1b191cb5SApple OSS Distributions  */
6*1b191cb5SApple OSS Distributions 
7*1b191cb5SApple OSS Distributions #include <stdio.h>
8*1b191cb5SApple OSS Distributions #include <stdlib.h>
9*1b191cb5SApple OSS Distributions #include <fcntl.h>
10*1b191cb5SApple OSS Distributions #include <limits.h>
11*1b191cb5SApple OSS Distributions #include <string.h>
12*1b191cb5SApple OSS Distributions #include <errno.h>
13*1b191cb5SApple OSS Distributions #include <unistd.h>
14*1b191cb5SApple OSS Distributions #include <sys/stat.h>
15*1b191cb5SApple OSS Distributions #include <sys/types.h>
16*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
17*1b191cb5SApple OSS Distributions #include <bsm/audit.h>
18*1b191cb5SApple OSS Distributions 
19*1b191cb5SApple OSS Distributions /* bad!  this is replicated in kern_credential.c.  make sure they stay in sync!
20*1b191cb5SApple OSS Distributions  * Or better yet have commone header file?
21*1b191cb5SApple OSS Distributions  */
22*1b191cb5SApple OSS Distributions #define MAX_STACK_DEPTH 8
23*1b191cb5SApple OSS Distributions struct cred_backtrace {
24*1b191cb5SApple OSS Distributions 	int                             depth;
25*1b191cb5SApple OSS Distributions 	uint32_t                stack[MAX_STACK_DEPTH];
26*1b191cb5SApple OSS Distributions };
27*1b191cb5SApple OSS Distributions typedef struct cred_backtrace cred_backtrace;
28*1b191cb5SApple OSS Distributions 
29*1b191cb5SApple OSS Distributions struct cred_debug_buffer {
30*1b191cb5SApple OSS Distributions 	int                             next_slot;
31*1b191cb5SApple OSS Distributions 	cred_backtrace  stack_buffer[1];
32*1b191cb5SApple OSS Distributions };
33*1b191cb5SApple OSS Distributions typedef struct cred_debug_buffer cred_debug_buffer;
34*1b191cb5SApple OSS Distributions 
35*1b191cb5SApple OSS Distributions 
main(int argc,char * argv[])36*1b191cb5SApple OSS Distributions main( int argc, char *argv[] )
37*1b191cb5SApple OSS Distributions {
38*1b191cb5SApple OSS Distributions 	int                             err, i, j;
39*1b191cb5SApple OSS Distributions 	size_t                  len;
40*1b191cb5SApple OSS Distributions 	char                        *my_bufferp = NULL;
41*1b191cb5SApple OSS Distributions 	cred_debug_buffer       *bt_buffp;
42*1b191cb5SApple OSS Distributions 	cred_backtrace          *btp;
43*1b191cb5SApple OSS Distributions 
44*1b191cb5SApple OSS Distributions 	/* get size of buffer we will need */
45*1b191cb5SApple OSS Distributions 	len = 0;
46*1b191cb5SApple OSS Distributions 	err = sysctlbyname( "kern.cred_bt", NULL, &len, NULL, 0 );
47*1b191cb5SApple OSS Distributions 	if (err != 0) {
48*1b191cb5SApple OSS Distributions 		printf( "sysctl failed  \n" );
49*1b191cb5SApple OSS Distributions 		printf( "\terrno %d - \"%s\" \n", errno, strerror( errno ));
50*1b191cb5SApple OSS Distributions 		return;
51*1b191cb5SApple OSS Distributions 	}
52*1b191cb5SApple OSS Distributions 
53*1b191cb5SApple OSS Distributions 	/* get a buffer for our back traces */
54*1b191cb5SApple OSS Distributions 	my_bufferp = malloc( len );
55*1b191cb5SApple OSS Distributions 	if (my_bufferp == NULL) {
56*1b191cb5SApple OSS Distributions 		printf( "malloc error %d - \"%s\" \n", errno, strerror( errno ));
57*1b191cb5SApple OSS Distributions 		return;
58*1b191cb5SApple OSS Distributions 	}
59*1b191cb5SApple OSS Distributions 	err = sysctlbyname( "kern.cred_bt", my_bufferp, &len, NULL, 0 );
60*1b191cb5SApple OSS Distributions 	if (err != 0) {
61*1b191cb5SApple OSS Distributions 		printf( "sysctl 2 failed  \n" );
62*1b191cb5SApple OSS Distributions 		printf( "\terrno %d - \"%s\" \n", errno, strerror( errno ));
63*1b191cb5SApple OSS Distributions 		return;
64*1b191cb5SApple OSS Distributions 	}
65*1b191cb5SApple OSS Distributions 
66*1b191cb5SApple OSS Distributions 	bt_buffp = (cred_debug_buffer *) my_bufferp;
67*1b191cb5SApple OSS Distributions 	btp = &bt_buffp->stack_buffer[0];
68*1b191cb5SApple OSS Distributions 
69*1b191cb5SApple OSS Distributions 	printf("number of traces %d \n", bt_buffp->next_slot);
70*1b191cb5SApple OSS Distributions 	for (i = 0; i < bt_buffp->next_slot; i++, btp++) {
71*1b191cb5SApple OSS Distributions 		printf("[%d] ", i);
72*1b191cb5SApple OSS Distributions 		for (j = 0; j < btp->depth; j++) {
73*1b191cb5SApple OSS Distributions 			printf("%p ", btp->stack[j]);
74*1b191cb5SApple OSS Distributions 		}
75*1b191cb5SApple OSS Distributions 		printf("\n");
76*1b191cb5SApple OSS Distributions 	}
77*1b191cb5SApple OSS Distributions 
78*1b191cb5SApple OSS Distributions 	return;
79*1b191cb5SApple OSS Distributions }
80