]> git.saurik.com Git - apple/libc.git/blob - gmon/gmon.c
ff2f4edc43aeadba15e1efee2b62f2455d6ce0f3
[apple/libc.git] / gmon / gmon.c
1 /*
2 * Copyright (c) 1999, 2003, 2004, 2007, 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #if defined(PROFILE)
24 #error This module cannot be compiled with profiling
25 #endif
26
27 /*-
28 * Copyright (c) 1983, 1992, 1993
29 * The Regents of the University of California. All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59 /*
60 * History
61 * 2-Mar-90 Gregg Kellogg (gk) at NeXT
62 * Changed include of kern/mach.h to kern/mach_interface.h
63 *
64 * 1-May-90 Matthew Self (mself) at NeXT
65 * Added prototypes, and added casts to remove all warnings.
66 * Made all private data static.
67 * vm_deallocate old data defore vm_allocate'ing new data.
68 * Added new functions monoutput and monreset.
69 *
70 * 18-Dec-92 Development Environment Group at NeXT
71 * Added multiple profile areas, the ability to profile shlibs and the
72 * ability to profile rld loaded code. Moved the machine dependent mcount
73 * routine out of this source file.
74 *
75 * 13-Dec-92 Development Environment Group at NeXT
76 * Added support for dynamic shared libraries. Also removed the code that
77 * had been ifdef'ed out for profiling fixed shared libraries and
78 * objective-C.
79 *
80 * 29-Aug-11 Vishal Patel (vishal_patel) at Apple
81 * Removed code that made calls to deprecated syscalls profil() and
82 * add_profil(). The syscalls are not supported since 2008 and planned
83 * to be completely removed soon. Similarly the monitor apis are also
84 * deprecated.
85 *
86 */
87
88 #if defined(LIBC_SCCS) && !defined(lint)
89 static char sccsid[] = "@(#)gmon.c 5.2 (Berkeley) 6/21/85";
90 #endif
91
92 /*
93 * see profil(2) where this (SCALE_1_TO_1) is describe (incorrectly).
94 *
95 * The correct description: scale is a fixed point value with
96 * the binary point in the middle of the 32 bit value. (Bit 16 is
97 * 1, bit 15 is .5, etc.)
98 *
99 * Setting the scale to "1" (i.e. 0x10000), results in the kernel
100 * choosing the profile bucket address 1 to 1 with the pc sampled.
101 * Since buckets are shorts, if the profiling base were 0, then a pc
102 * of 0 increments bucket 0, a pc of 2 increments bucket 1, and a pc
103 * of 4 increments bucket 2.) (Actually, this seems a little bogus,
104 * 1 to 1 should map pc's to buckets -- that's probably what was
105 * intended from the man page, but historically....
106 */
107 #define SCALE_1_TO_1 0x10000L
108
109 #define MSG "No space for monitor buffer(s)\n"
110
111 #include <stdio.h>
112 #include <libc.h>
113 #include <monitor.h>
114 #include <sys/types.h>
115 #include <sys/gmon.h>
116 #include <sys/param.h>
117 #include <sys/sysctl.h>
118 #include <mach/mach.h>
119 #include <mach-o/loader.h>
120 #include <mach-o/dyld.h>
121 #include <mach-o/getsect.h>
122
123 /*
124 * These are defined in here and these declarations need to be moved to libc.h
125 * where the other declarations for the monitor(3) routines are declared.
126 */
127 extern void moninit(
128 void);
129 extern void monaddition(
130 char *lowpc,
131 char *highpc);
132 extern void moncount(
133 char *frompc,
134 char *selfpc);
135 extern void monreset(
136 void);
137 extern void monoutput(
138 const char *filename);
139
140 static char profiling = -1; /* tas (test and set) location for NeXT */
141 static char init = 0; /* set while moninit() is being serviced */
142
143 static unsigned long order = 0; /* call order */
144
145 typedef struct {
146 /* the address range and size this mon struct refers to */
147 char *lowpc;
148 char *highpc;
149 unsigned long textsize;
150 /* the data structures to support the arc's and their counts */
151 unsigned short *froms; /* froms is unsigned shorts indexing into tos */
152 tostruct_t *tos;
153 long tolimit;
154 /* the pc-sample buffer, it's size and scale */
155 char *sbuf;
156 long ssiz; /* includes the gmonhdr_t */
157 long scale;
158 } mon_t;
159 static mon_t *mon = NULL;
160 static unsigned long nmon = 0;
161
162 void
163 moninit(
164 void)
165 {
166 return; // Deprecated api. do nothing
167 }
168
169 void
170 monstartup(
171 char *lowpc,
172 char *highpc)
173 {
174 return; // Deprecated api. do nothing
175 }
176
177 /*
178 * monaddtion() is used for adding additional pc ranges to profile. This is
179 * used for profiling dyld loaded code.
180 */
181 void
182 monaddition(
183 char *lowpc,
184 char *highpc)
185 {
186 return; // Deprecated api. do nothing
187 }
188
189 void
190 monreset(
191 void)
192 {
193 return; // Deprecated api. do nothing
194 }
195
196 void
197 monoutput(
198 const char *filename)
199 {
200 return; // Deprecated api. do nothing
201 }
202
203 void
204 monitor(
205 char *lowpc,
206 char *highpc,
207 char *buf,
208 int bufsiz,
209 int nfunc) /* nfunc is not used; available for compatability only. */
210 {
211 return; // Deprecated api. do nothing
212 }
213
214 /*
215 * Control profiling
216 * profiling is what mcount checks to see if
217 * all the data structures are ready.
218 */
219 void
220 moncontrol(
221 int mode)
222 {
223 return; // Deprecated api. do nothing
224 }
225
226 void
227 moncount(
228 char *frompc,
229 char *selfpc)
230 {
231 return; //Deprecated api. do nothing
232 }