2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #error This module cannot be compiled with profiling
28 * Copyright (c) 1983, 1992, 1993
29 * The Regents of the University of California. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
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.
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
61 * 2-Mar-90 Gregg Kellogg (gk) at NeXT
62 * Changed include of kern/mach.h to kern/mach_interface.h
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.
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.
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
81 #if defined(LIBC_SCCS) && !defined(lint)
82 static char sccsid
[] = "@(#)gmon.c 5.2 (Berkeley) 6/21/85";
86 * see profil(2) where this (SCALE_1_TO_1) is describe (incorrectly).
88 * The correct description: scale is a fixed point value with
89 * the binary point in the middle of the 32 bit value. (Bit 16 is
90 * 1, bit 15 is .5, etc.)
92 * Setting the scale to "1" (i.e. 0x10000), results in the kernel
93 * choosing the profile bucket address 1 to 1 with the pc sampled.
94 * Since buckets are shorts, if the profiling base were 0, then a pc
95 * of 0 increments bucket 0, a pc of 2 increments bucket 1, and a pc
96 * of 4 increments bucket 2.) (Actually, this seems a little bogus,
97 * 1 to 1 should map pc's to buckets -- that's probably what was
98 * intended from the man page, but historically....
100 #define SCALE_1_TO_1 0x10000L
102 #define MSG "No space for monitor buffer(s)\n"
107 #include <sys/types.h>
108 #include <sys/gmon.h>
109 #include <sys/param.h>
110 #include <sys/sysctl.h>
111 #include <mach/mach.h>
112 #include <mach-o/loader.h>
113 #include <mach-o/dyld.h>
114 #include <mach-o/getsect.h>
117 * These are defined in here and these declarations need to be moved to libc.h
118 * where the other declarations for the monitor(3) routines are declared.
122 extern void monaddition(
125 extern void moncount(
128 extern void monreset(
130 extern void monoutput(
131 const char *filename
);
133 static char profiling
= -1; /* tas (test and set) location for NeXT */
134 static char init
= 0; /* set while moninit() is being serviced */
136 static unsigned long order
= 0; /* call order */
139 /* the address range and size this mon struct refers to */
142 unsigned long textsize
;
143 /* the data structures to support the arc's and their counts */
144 unsigned short *froms
; /* froms is unsigned shorts indexing into tos */
147 /* the pc-sample buffer, it's size and scale */
149 long ssiz
; /* includes the gmonhdr_t */
152 static mon_t
*mon
= NULL
;
153 static unsigned long nmon
= 0;
155 static void monsetup(
159 static long getprofhz(
167 const struct section
*section
;
169 const struct section_64
*section
;
171 char *lowpc
, *highpc
;
177 section
= getsectbyname("__TEXT", "__text");
178 lowpc
= (char *)section
->addr
,
179 highpc
= (char *)(section
->addr
+ section
->size
);
182 if((mon
= malloc(sizeof(mon_t
))) == NULL
){
183 write(2, MSG
, sizeof(MSG
) - 1);
187 memset(mon
, '\0', sizeof(mon_t
));
190 * To continue to make monstartup() and the functions that existed
191 * before adding multiple profiling areas working correctly the new
192 * calls to get the dyld loaded code profiled are made after
193 * the first mon_t is allocated so that they will not use the
194 * first mon_t and the old calls will always use the first mon_t
197 monsetup(mon
, lowpc
, highpc
);
199 profil(mon
->sbuf
+ sizeof(gmonhdr_t
),
200 mon
->ssiz
- sizeof(gmonhdr_t
),
201 (u_long
)mon
->lowpc
, mon
->scale
);
202 for(i
= 1; i
< nmon
; i
++)
203 add_profil(mon
[i
].sbuf
+ sizeof(gmonhdr_t
),
204 mon
[i
].ssiz
- sizeof(gmonhdr_t
),
205 (u_long
)mon
[i
].lowpc
, mon
[i
].scale
);
209 #if defined(__DYNAMIC__)
211 * Call _dyld_moninit() if the dyld is present. This is done after the
212 * above calls so the dynamic libraries will be added after the
216 _dyld_moninit(monaddition
);
227 if((mon
= malloc(sizeof(mon_t
))) == NULL
){
228 write(2, MSG
, sizeof(MSG
) - 1);
232 memset(mon
, '\0', sizeof(mon_t
));
234 monsetup(mon
, lowpc
, highpc
);
238 * monaddtion() is used for adding additional pc ranges to profile. This is
239 * used for profiling dyld loaded code.
250 monstartup(lowpc
, highpc
);
253 save_profiling
= profiling
;
255 if((mon
= realloc(mon
, (nmon
+ 1) * sizeof(mon_t
))) == NULL
){
256 write(2, MSG
, sizeof(MSG
) - 1);
260 memset(m
, '\0', sizeof(mon_t
));
262 monsetup(m
, lowpc
, highpc
);
263 profiling
= save_profiling
;
280 * round lowpc and highpc to multiples of the density we're using
281 * so the rest of the scaling (here and in gprof) stays in longs.
283 lowpc
= (char *)ROUNDDOWN((uintptr_t)lowpc
,
284 HISTFRACTION
* sizeof(HISTCOUNTER
));
286 highpc
= (char *)ROUNDUP((uintptr_t)highpc
,
287 HISTFRACTION
* sizeof(HISTCOUNTER
));
291 vm_deallocate(mach_task_self(),
292 (vm_address_t
)m
->froms
,
293 (vm_size_t
)(m
->textsize
/ HASHFRACTION
));
294 m
->textsize
= highpc
- lowpc
;
295 ret
= vm_allocate(mach_task_self(),
296 (vm_address_t
*)&m
->froms
,
297 (vm_size_t
)(m
->textsize
/ HASHFRACTION
),
299 if(ret
!= KERN_SUCCESS
){
300 write(2, MSG
, sizeof(MSG
) - 1);
306 vm_deallocate(mach_task_self(),
307 (vm_address_t
)m
->sbuf
,
309 monsize
= (m
->textsize
/ HISTFRACTION
) + sizeof(gmonhdr_t
);
310 ret
= vm_allocate(mach_task_self(),
311 (vm_address_t
*)&buffer
,
314 if(ret
!= KERN_SUCCESS
){
315 write(2, MSG
, sizeof(MSG
) - 1);
321 vm_deallocate(mach_task_self(),
322 (vm_address_t
)m
->tos
,
323 (vm_size_t
)(m
->tolimit
* sizeof(tostruct_t
)));
324 m
->tolimit
= m
->textsize
* ARCDENSITY
/ 100;
325 if(m
->tolimit
< MINARCS
){
326 m
->tolimit
= MINARCS
;
328 else if(m
->tolimit
> 65534){
331 ret
= vm_allocate(mach_task_self(),
332 (vm_address_t
*)&m
->tos
,
333 (vm_size_t
)(m
->tolimit
* sizeof(tostruct_t
)),
335 if(ret
!= KERN_SUCCESS
){
336 write(2, MSG
, sizeof(MSG
) - 1);
340 m
->tos
[0].link
= 0; /* a nop since tos was vm_allocated and is zero */
343 * If this is call to monsetup() was via monstartup() (m == mon) then
344 * it is using or reusing the first pc range and then the pc sample
345 * buffer can be setup by the system call profil() via monitor() via
346 * a moncontrol(1) call.
348 * Otherwise this is call to monsetup() was via monaddition() and a
349 * new system call is needed to add an additional pc sample buffer in
352 if(m
== mon
&& !init
){
353 monitor(lowpc
, highpc
, buffer
, monsize
, m
->tolimit
);
356 /* monitor() functionality */
359 p
= (gmonhdr_t
*)m
->sbuf
;
360 memset(p
, '\0', sizeof(gmonhdr_t
));
361 p
->lpc
= (uintptr_t)m
->lowpc
;
362 p
->hpc
= (uintptr_t)m
->highpc
;
364 p
->version
= GMONVERSION
;
365 p
->profrate
= getprofhz();
367 if((monsize
- sizeof(gmonhdr_t
)) < o
)
368 /* POSSIBLE BUG, if "(float) (monsize - sizeof(gmonhdr_t))/ o)" is zero
369 * then m->scale will be set to zero and the add_profil() call will disable
371 m
->scale
= ((float) (monsize
- sizeof(gmonhdr_t
))/ o
) *
374 m
->scale
= SCALE_1_TO_1
;
376 /* moncontrol(mode == 1) functionality */
378 add_profil(m
->sbuf
+ sizeof(gmonhdr_t
),
379 m
->ssiz
- sizeof(gmonhdr_t
),
380 (long)m
->lowpc
, m
->scale
);
395 for(i
= 0; i
< nmon
; i
++){
398 memset(m
->sbuf
, '\0', m
->ssiz
);
399 p
= (gmonhdr_t
*)m
->sbuf
;
400 p
->lpc
= (uintptr_t)m
->lowpc
;
401 p
->hpc
= (uintptr_t)m
->highpc
;
403 p
->version
= GMONVERSION
;
404 p
->profrate
= getprofhz();
407 memset(m
->froms
, '\0', m
->textsize
/ HASHFRACTION
);
409 memset(m
->tos
, '\0', m
->tolimit
* sizeof(tostruct_t
));
417 const char *filename
)
420 unsigned long i
, fromindex
, endfrom
, toindex
;
422 gmon_data_t sample_data
, arc_data
, dyld_data
;
424 rawarc_order_t rawarc_order
;
426 uint32_t image_count
;
427 intptr_t image_header
;
434 fd
= creat(filename
, 0666);
436 perror("mcount: gmon.out");
443 magic
= GMON_MAGIC_64
;
445 write(fd
, &magic
, sizeof(uint32_t));
447 #if defined(__DYNAMIC__)
449 image_count
= _dyld_image_count();
452 printf("image_count = %lu\n", image_count
- 1);
453 for(i
= 1; i
< image_count
; i
++){
454 image_header
= _dyld_get_image_header(i
);
455 printf("\timage_header %p\n", image_header
);
456 image_name
= _dyld_get_image_name(i
);
457 printf("\timage_name %s\n", image_name
);
461 * Calculate the dyld_data.size.
463 dyld_data
.type
= GMONTYPE_DYLD2_STATE
;
464 dyld_data
.size
= sizeof(uint32_t) +
465 sizeof(intptr_t) * (image_count
- 1);
466 for(i
= 1; i
< image_count
; i
++){
467 image_name
= _dyld_get_image_name(i
);
468 dyld_data
.size
+= strlen(image_name
) + 1;
472 * Write the dyld_data.
474 write(fd
, &dyld_data
, sizeof(gmon_data_t
));
476 write(fd
, &image_count
, sizeof(uint32_t));
478 for(i
= 1; i
< image_count
; i
++){
479 image_header
= _dyld_get_image_header(i
);
480 write(fd
, &image_header
, sizeof(intptr_t));
481 image_name
= _dyld_get_image_name(i
);
482 write(fd
, image_name
, strlen(image_name
) + 1);
487 for(i
= 0; i
< nmon
; i
++){
490 fprintf(stderr
, "[monoutput] sbuf %p ssiz %d\n", m
->sbuf
, m
->ssiz
);
492 sample_data
.type
= GMONTYPE_SAMPLES
;
493 sample_data
.size
= m
->ssiz
;
494 write(fd
, &sample_data
, sizeof(gmon_data_t
));
496 * Write the gmonhdr_t and the pc-sample buffer. Note the
497 * gmonhdr_t is in sbuf at the beginning of sbuf already
500 write(fd
, m
->sbuf
, m
->ssiz
);
503 * Now write out the raw arcs.
505 endfrom
= m
->textsize
/ (HASHFRACTION
* sizeof(*m
->froms
));
506 arc_data
.type
= GMONTYPE_ARCS_ORDERS
;
509 fprintf(stderr
, "[monoutput] raw arcs, total %lu\n", endfrom
);
511 for(fromindex
= 0; fromindex
< endfrom
; fromindex
++){
512 if(m
->froms
[fromindex
] == 0){
516 fprintf(stderr
, "[monoutput] raw arc count at index[%lu] %u\n",
517 fromindex
, m
->froms
[fromindex
]);
520 (fromindex
* HASHFRACTION
* sizeof(*m
->froms
));
521 for(toindex
= m
->froms
[fromindex
];
523 toindex
= m
->tos
[toindex
].link
){
524 arc_data
.size
+= sizeof(rawarc_order_t
);
527 write(fd
, &arc_data
, sizeof(gmon_data_t
));
529 for(fromindex
= 0; fromindex
< endfrom
; fromindex
++){
530 if(m
->froms
[fromindex
] == 0){
534 (fromindex
* HASHFRACTION
* sizeof(*m
->froms
));
535 for(toindex
= m
->froms
[fromindex
];
537 toindex
= m
->tos
[toindex
].link
){
539 fprintf(stderr
, "[monoutput] frompc %p selfpc %p "
540 "count %ld order %lu\n", frompc
,
541 m
->tos
[toindex
].selfpc
,
542 m
->tos
[toindex
].count
, m
->tos
[toindex
].order
);
544 rawarc_order
.raw_frompc
= (uintptr_t)frompc
;
545 rawarc_order
.raw_selfpc
= (uintptr_t)
546 m
->tos
[toindex
].selfpc
;
547 rawarc_order
.raw_count
= m
->tos
[toindex
].count
;
548 rawarc_order
.raw_order
= m
->tos
[toindex
].order
;
549 write(fd
, &rawarc_order
, sizeof(rawarc_order_t
));
562 int nfunc
) /* nfunc is not used; available for compatability only. */
574 monoutput("gmon.out");
579 p
= (gmonhdr_t
*)buf
;
580 memset(p
, '\0', sizeof(gmonhdr_t
));
581 p
->lpc
= (uintptr_t)lowpc
;
582 p
->hpc
= (uintptr_t)highpc
;
584 p
->version
= GMONVERSION
;
585 p
->profrate
= getprofhz();
586 bufsiz
-= sizeof(gmonhdr_t
);
591 m
->scale
= ((float) bufsiz
/ o
) * SCALE_1_TO_1
;
593 m
->scale
= SCALE_1_TO_1
;
599 * profiling is what mcount checks to see if
600 * all the data structures are ready.
613 profil(m
->sbuf
+ sizeof(gmonhdr_t
),
614 m
->ssiz
- sizeof(gmonhdr_t
),
615 (u_long
)m
->lowpc
, m
->scale
);
616 for(i
= 1; i
< nmon
; i
++)
617 add_profil(mon
[i
].sbuf
+ sizeof(gmonhdr_t
),
618 mon
[i
].ssiz
- sizeof(gmonhdr_t
),
619 (u_long
)mon
[i
].lowpc
, mon
[i
].scale
);
625 profil((char *)0, 0, 0, 0);
635 unsigned short *frompcindex
;
636 tostruct_t
*top
, *prevtop
;
637 unsigned long i
, toindex
;
644 * Check that we are profiling and that we aren't recursively invoked.
645 * This should really be a test and set instruction in changing the
646 * value of profiling.
654 fprintf(stderr
, "[moncount] frompc %p selfpc %p\n", frompc
, selfpc
);
656 frompcindex
= (unsigned short *)frompc
;
659 * check that frompcindex is a reasonable pc value.
660 * for example: signal catchers get called from the stack,
661 * not from text space. too bad.
663 for(i
= 0; i
< nmon
; i
++){
665 if((uintptr_t)frompcindex
>= (uintptr_t)m
->lowpc
&&
666 (uintptr_t)frompcindex
< (uintptr_t)m
->highpc
)
673 frompcindex
= (unsigned short *)
674 ((uintptr_t)frompcindex
- (uintptr_t)m
->lowpc
);
677 &m
->froms
[((long)frompcindex
) / (HASHFRACTION
* sizeof(*m
->froms
))];
678 toindex
= *frompcindex
;
681 * first time traversing this arc
683 toindex
= ++m
->tos
[0].link
;
684 if(toindex
>= m
->tolimit
){
687 *frompcindex
= toindex
;
688 top
= &m
->tos
[toindex
];
689 top
->selfpc
= (uintptr_t)selfpc
;
692 top
->order
= ++order
;
695 top
= &m
->tos
[toindex
];
696 if(top
->selfpc
== (uintptr_t)selfpc
){
698 * arc at front of chain; usual case.
704 * have to go looking down chain for it.
705 * top points to what we are looking at,
706 * prevtop points to previous top.
707 * we know it is not at the head of the chain.
709 for(; /* goto done */; ){
712 * top is end of the chain and none of the chain
713 * had top->selfpc == selfpc.
714 * so we allocate a new tostruct_t
715 * and link it to the head of the chain.
717 toindex
= ++m
->tos
[0].link
;
718 if(toindex
>= m
->tolimit
){
721 top
= &m
->tos
[toindex
];
722 top
->selfpc
= (uintptr_t)selfpc
;
724 top
->link
= *frompcindex
;
725 top
->order
= ++order
;
726 *frompcindex
= toindex
;
730 * otherwise, check the next arc on the chain.
733 top
= &m
->tos
[top
->link
];
734 if(top
->selfpc
== (uintptr_t)selfpc
){
737 * increment its count
738 * move it to the head of the chain.
741 toindex
= prevtop
->link
;
742 prevtop
->link
= top
->link
;
743 top
->link
= *frompcindex
;
744 *frompcindex
= toindex
;
753 profiling
++; /* halt further profiling */
754 #define TOLIMIT "mcount: tos overflow\n"
755 write(2, TOLIMIT
, sizeof(TOLIMIT
) - 1);
759 * Get the profiling rate.
767 struct clockinfo clockrate
;
770 mib
[1] = KERN_CLOCKRATE
;
771 clockrate
.profhz
= 1;
772 size
= sizeof(clockrate
);
773 if(sysctl(mib
, 2, &clockrate
, &size
, NULL
, 0) < 0)
775 return(clockrate
.profhz
);