]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kperf/callstack.c
xnu-2782.1.97.tar.gz
[apple/xnu.git] / osfmk / kperf / callstack.c
CommitLineData
316670eb
A
1/*
2 * Copyright (c) 2011 Apple Computer, 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/* Collect kernel callstacks */
30
31#include <mach/mach_types.h>
32#include <machine/machine_routines.h> /* XXX: remove me */
33#include <kern/thread.h>
34
35#include <chud/chud_xnu.h>
36
37#include <kperf/buffer.h>
38#include <kperf/context.h>
39#include <kperf/callstack.h>
40#include <kperf/ast.h>
41
42static void
43callstack_sample( struct callstack *cs,
44 struct kperf_context *context,
45 uint32_t is_user )
46{
47 kern_return_t kr;
48 mach_msg_type_number_t nframes; /* WTF with the type? */
49 uint32_t code;
50
51 if( is_user )
52 code = PERF_CS_USAMPLE;
53 else
54 code = PERF_CS_KSAMPLE;
55
56 BUF_INFO1( code, (uintptr_t)context->cur_thread );
57
58 /* fill out known flags */
59 cs->flags = 0;
60 if( !is_user )
61 {
62 cs->flags |= CALLSTACK_KERNEL;
63#ifdef __LP64__
64 cs->flags |= CALLSTACK_64BIT;
65#endif
66 }
67 else
68 {
69 /* FIXME: detect 32 vs 64-bit? */
70 }
71
72 /* collect the callstack */
73 nframes = MAX_CALLSTACK_FRAMES;
39236c6e
A
74 kr = chudxnu_thread_get_callstack64_kperf( context->cur_thread,
75 cs->frames,
76 &nframes,
77 is_user );
316670eb
A
78
79 /* check for overflow */
80 if( kr == KERN_SUCCESS )
81 {
82 cs->flags |= CALLSTACK_VALID;
83 cs->nframes = nframes;
84 }
85 else if( kr == KERN_RESOURCE_SHORTAGE )
86 {
87 /* FIXME: more here */
88 cs->flags |= CALLSTACK_TRUNCATED;
89 cs->flags |= CALLSTACK_VALID;
90 cs->nframes = nframes;
91 }
92 else
93 {
39236c6e 94 BUF_INFO2(PERF_CS_ERROR, ERR_GETSTACK, kr);
316670eb
A
95 cs->nframes = 0;
96 }
97
98 if( cs->nframes >= MAX_CALLSTACK_FRAMES )
99 {
100 /* necessary? */
39236c6e 101 BUF_INFO1(PERF_CS_ERROR, ERR_FRAMES);
316670eb
A
102 cs->nframes = 0;
103 }
104
105}
106
107void
108kperf_kcallstack_sample( struct callstack *cs, struct kperf_context *context )
109{
110 callstack_sample( cs, context, 0 );
111}
112
113void
114kperf_ucallstack_sample( struct callstack *cs, struct kperf_context *context )
115{
116 callstack_sample( cs, context, 1 );
117}
118
119static void
39236c6e 120callstack_log( struct callstack *cs, uint32_t hcode, uint32_t dcode )
316670eb
A
121{
122 unsigned int i, j, n, of = 4;
123
124 /* Header on the stack */
39236c6e 125 BUF_DATA2( hcode, cs->flags, cs->nframes );
316670eb
A
126
127 /* look for how many batches of 4 */
128 n = cs->nframes / 4;
129 of = cs->nframes % 4;
130 if( of != 0 )
131 n++;
132
133 /* print all the stack data, and zero the overflow */
134 for( i = 0; i < n; i++ )
135 {
136#define SCRUB_FRAME(x) (((x)<cs->nframes)?cs->frames[x]:0)
137 j = i * 4;
39236c6e 138 BUF_DATA ( dcode,
316670eb
A
139 SCRUB_FRAME(j+0),
140 SCRUB_FRAME(j+1),
141 SCRUB_FRAME(j+2),
142 SCRUB_FRAME(j+3) );
143#undef SCRUB_FRAME
144 }
145}
146
147void
148kperf_kcallstack_log( struct callstack *cs )
149{
39236c6e 150 callstack_log( cs, PERF_CS_KHDR, PERF_CS_KDATA );
316670eb
A
151}
152
153void
154kperf_ucallstack_log( struct callstack *cs )
155{
39236c6e 156 callstack_log( cs, PERF_CS_UHDR, PERF_CS_UDATA );
316670eb
A
157}
158
159int
160kperf_ucallstack_pend( struct kperf_context * context )
161{
162 return kperf_ast_pend( context->cur_thread, T_AST_CALLSTACK,
163 T_AST_CALLSTACK );
164}
165
166// kr = chudxnu_thread_get_callstack(context->generic->threadID,
167// (uint32_t*)frames, &frameCount, !collectingSupervisorStack);