2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #error This module cannot be compiled with profiling
27 * Copyright (c) 1983, 1992, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * 2-Mar-90 Gregg Kellogg (gk) at NeXT
61 * Changed include of kern/mach.h to kern/mach_interface.h
63 * 1-May-90 Matthew Self (mself) at NeXT
64 * Added prototypes, and added casts to remove all warnings.
65 * Made all private data static.
66 * vm_deallocate old data defore vm_allocate'ing new data.
67 * Added new functions monoutput and monreset.
69 * 18-Dec-92 Development Environment Group at NeXT
70 * Added multiple profile areas, the ability to profile shlibs and the
71 * ability to profile rld loaded code. Moved the machine dependent mcount
72 * routine out of this source file.
74 * 13-Dec-92 Development Environment Group at NeXT
75 * Added support for dynamic shared libraries. Also removed the code that
76 * had been ifdef'ed out for profiling fixed shared libraries and
80 #if defined(LIBC_SCCS) && !defined(lint)
81 static char sccsid
[] = "@(#)gmon.c 5.2 (Berkeley) 6/21/85";
85 * see profil(2) where this (SCALE_1_TO_1) is describe (incorrectly).
87 * The correct description: scale is a fixed point value with
88 * the binary point in the middle of the 32 bit value. (Bit 16 is
89 * 1, bit 15 is .5, etc.)
91 * Setting the scale to "1" (i.e. 0x10000), results in the kernel
92 * choosing the profile bucket address 1 to 1 with the pc sampled.
93 * Since buckets are shorts, if the profiling base were 0, then a pc
94 * of 0 increments bucket 0, a pc of 2 increments bucket 1, and a pc
95 * of 4 increments bucket 2.) (Actually, this seems a little bogus,
96 * 1 to 1 should map pc's to buckets -- that's probably what was
97 * intended from the man page, but historically....
99 #define SCALE_1_TO_1 0x10000L
101 #define MSG "No space for monitor buffer(s)\n"
105 extern const struct section
*getsectbyname(
107 const char *sectname
);
109 #include <sys/types.h>
110 #include <sys/gmon.h>
111 #include <sys/param.h>
112 #include <sys/sysctl.h>
113 #include <mach/mach.h>
114 #include <mach-o/loader.h>
115 #include <mach-o/dyld.h>
118 * These are defined in here and these declarations need to be moved to libc.h
119 * where the other declarations for the monitor(3) routines are declared.
123 extern void monaddition(
126 extern void moncount(
129 extern void monreset(
131 extern void monoutput(
132 const char *filename
);
133 extern int add_profil(char *, int, int, int);
135 static char profiling
= -1; /* tas (test and set) location for NeXT */
136 static char init
= 0; /* set while moninit() is being serviced */
138 static unsigned long order
= 0; /* call order */
141 /* the address range and size this mon struct refers to */
144 unsigned long textsize
;
145 /* the data structures to support the arc's and their counts */
146 unsigned short *froms
; /* froms is unsigned shorts indexing into tos */
147 struct tostruct
*tos
;
149 /* the pc-sample buffer, it's size and scale */
151 int ssiz
; /* includes the gmonhdr struct */
154 static struct mon_t
*mon
= NULL
;
155 static unsigned long nmon
= 0;
157 static void monsetup(
161 static int getprofhz(
168 const struct section
*section
;
169 char *lowpc
, *highpc
;
175 section
= getsectbyname ("__TEXT", "__text");
176 lowpc
= (char *)section
->addr
,
177 highpc
= (char *)(section
->addr
+ section
->size
);
180 if((mon
= malloc(sizeof(struct mon_t
))) == NULL
){
181 write(2, MSG
, sizeof(MSG
) - 1);
185 memset(mon
, '\0', sizeof(struct mon_t
));
188 * To continue to make monstartup() and the functions that existed
189 * before adding multiple profiling areas working correctly the new
190 * calls to get the dyld loaded code profiled are made after
191 * the first mon_t struct is allocated so that they will not use the
192 * first mon_t and the old calls will always use the first mon_t struct
195 monsetup(mon
, lowpc
, highpc
);
197 profil(mon
->sbuf
+ sizeof(struct gmonhdr
),
198 mon
->ssiz
- sizeof(struct gmonhdr
),
199 (int)mon
->lowpc
, mon
->scale
);
200 for(i
= 1; i
< nmon
; i
++)
201 add_profil(mon
[i
].sbuf
+ sizeof(struct gmonhdr
),
202 mon
[i
].ssiz
- sizeof(struct gmonhdr
),
203 (int)mon
[i
].lowpc
, mon
[i
].scale
);
207 #if defined(__DYNAMIC__)
209 * Call _dyld_moninit() if the dyld is present. This is done after the
210 * above calls so the dynamic libraries will be added after the
214 _dyld_moninit(monaddition
);
225 if((mon
= malloc(sizeof(struct mon_t
))) == NULL
){
226 write(2, MSG
, sizeof(MSG
) - 1);
230 memset(mon
, '\0', sizeof(struct mon_t
));
232 monsetup(mon
, lowpc
, highpc
);
236 * monaddtion() is used for adding additional pc ranges to profile. This is
237 * used for profiling dyld loaded code.
248 monstartup(lowpc
, highpc
);
251 save_profiling
= profiling
;
253 if((mon
= realloc(mon
, (nmon
+ 1) * sizeof(struct mon_t
))) == NULL
){
254 write(2, MSG
, sizeof(MSG
) - 1);
258 memset(m
, '\0', sizeof(struct mon_t
));
260 monsetup(m
, lowpc
, highpc
);
261 profiling
= save_profiling
;
278 * round lowpc and highpc to multiples of the density we're using
279 * so the rest of the scaling (here and in gprof) stays in ints.
281 lowpc
= (char *)ROUNDDOWN((unsigned)lowpc
,
282 HISTFRACTION
* sizeof(HISTCOUNTER
));
284 highpc
= (char *)ROUNDUP((unsigned)highpc
,
285 HISTFRACTION
* sizeof(HISTCOUNTER
));
289 vm_deallocate(mach_task_self(),
290 (vm_address_t
)m
->froms
,
291 (vm_size_t
)(m
->textsize
/ HASHFRACTION
));
292 m
->textsize
= highpc
- lowpc
;
293 ret
= vm_allocate(mach_task_self(),
294 (vm_address_t
*)&m
->froms
,
295 (vm_size_t
)(m
->textsize
/ HASHFRACTION
),
297 if(ret
!= KERN_SUCCESS
){
298 write(2, MSG
, sizeof(MSG
) - 1);
304 vm_deallocate(mach_task_self(),
305 (vm_address_t
)m
->sbuf
,
307 monsize
= (m
->textsize
/ HISTFRACTION
) + sizeof(struct gmonhdr
);
308 ret
= vm_allocate(mach_task_self(),
309 (vm_address_t
*)&buffer
,
312 if(ret
!= KERN_SUCCESS
){
313 write(2, MSG
, sizeof(MSG
) - 1);
319 vm_deallocate(mach_task_self(),
320 (vm_address_t
)m
->tos
,
321 (vm_size_t
)(m
->tolimit
* sizeof(struct tostruct
)));
322 m
->tolimit
= m
->textsize
* ARCDENSITY
/ 100;
323 if(m
->tolimit
< MINARCS
){
324 m
->tolimit
= MINARCS
;
326 else if(m
->tolimit
> 65534){
329 ret
= vm_allocate(mach_task_self(),
330 (vm_address_t
*)&m
->tos
,
331 (vm_size_t
)(m
->tolimit
* sizeof(struct tostruct
)),
333 if(ret
!= KERN_SUCCESS
){
334 write(2, MSG
, sizeof(MSG
) - 1);
338 m
->tos
[0].link
= 0; /* a nop since tos was vm_allocated and is zero */
341 * If this is call to monsetup() was via monstartup() (m == mon) then
342 * it is using or reusing the first pc range and then the pc sample
343 * buffer can be setup by the system call profil() via monitor() via
344 * a moncontrol(1) call.
346 * Otherwise this is call to monsetup() was via monaddition() and a
347 * new system call is needed to add an additional pc sample buffer in
350 if(m
== mon
&& !init
){
351 monitor(lowpc
, highpc
, buffer
, monsize
, m
->tolimit
);
354 /* monitor() functionality */
357 p
= (struct gmonhdr
*)m
->sbuf
;
358 memset(p
, '\0', sizeof(struct gmonhdr
));
359 p
->lpc
= (unsigned long)m
->lowpc
;
360 p
->hpc
= (unsigned long)m
->highpc
;
362 p
->version
= GMONVERSION
;
363 p
->profrate
= getprofhz();
365 if((monsize
- sizeof(struct gmonhdr
)) < o
)
366 m
->scale
= ((float) (monsize
- sizeof(struct gmonhdr
))/ o
) *
369 m
->scale
= SCALE_1_TO_1
;
371 /* moncontrol(mode == 1) functionality */
373 add_profil(m
->sbuf
+ sizeof(struct gmonhdr
),
374 m
->ssiz
- sizeof(struct gmonhdr
),
375 (int)m
->lowpc
, m
->scale
);
390 for(i
= 0; i
< nmon
; i
++){
393 memset(m
->sbuf
, '\0', m
->ssiz
);
394 p
= (struct gmonhdr
*)m
->sbuf
;
395 p
->lpc
= (unsigned long)m
->lowpc
;
396 p
->hpc
= (unsigned long)m
->highpc
;
400 memset(m
->froms
, '\0', m
->textsize
/ HASHFRACTION
);
402 memset(m
->tos
, '\0', m
->tolimit
* sizeof (struct tostruct
));
410 const char *filename
)
413 unsigned long magic
, i
, fromindex
, endfrom
, toindex
;
414 struct gmon_data sample_data
, arc_data
, dyld_data
;
416 struct rawarc_order rawarc_order
;
418 unsigned long image_count
, vmaddr_slide
;
425 fd
= creat(filename
, 0666);
427 perror("mcount: gmon.out");
432 write(fd
, &magic
, sizeof(unsigned long));
434 #if defined(__DYNAMIC__)
436 image_count
= _dyld_image_count();
439 printf("image_count = %lu\n", image_count
- 1);
440 for(i
= 1; i
< image_count
; i
++){
441 vmaddr_slide
= _dyld_get_image_vmaddr_slide(i
);
442 printf("\tvmaddr_slide 0x%x\n", (unsigned int)vmaddr_slide
);
443 image_name
= _dyld_get_image_name(i
);
444 printf("\timage_name %s\n", image_name
);
448 * Calculate the dyld_data.size.
450 dyld_data
.type
= GMONTYPE_DYLD_STATE
;
451 dyld_data
.size
= sizeof(unsigned long) +
452 sizeof(unsigned long) * (image_count
- 1);
453 for(i
= 1; i
< image_count
; i
++){
454 image_name
= _dyld_get_image_name(i
);
455 dyld_data
.size
+= strlen(image_name
) + 1;
459 * Write the dyld_data.
461 write(fd
, &dyld_data
, sizeof(struct gmon_data
));
463 write(fd
, &image_count
, sizeof(unsigned long));
465 for(i
= 1; i
< image_count
; i
++){
466 vmaddr_slide
= _dyld_get_image_vmaddr_slide(i
);
467 write(fd
, &vmaddr_slide
, sizeof(unsigned long));
468 image_name
= _dyld_get_image_name(i
);
469 write(fd
, image_name
, strlen(image_name
) + 1);
474 for(i
= 0; i
< nmon
; i
++){
477 fprintf(stderr
, "[monoutput] sbuf 0x%x ssiz %d\n",
480 sample_data
.type
= GMONTYPE_SAMPLES
;
481 sample_data
.size
= m
->ssiz
;
482 write(fd
, &sample_data
, sizeof(struct gmon_data
));
484 * Write the gmonhdr struct and the pc-sample buffer. Note the
485 * gmonhdr struct is in sbuf at the beginning of sbuf already
488 write(fd
, m
->sbuf
, m
->ssiz
);
491 * Now write out the raw arcs.
493 endfrom
= m
->textsize
/ (HASHFRACTION
* sizeof(*m
->froms
));
494 arc_data
.type
= GMONTYPE_ARCS_ORDERS
;
496 for(fromindex
= 0; fromindex
< endfrom
; fromindex
++){
497 if(m
->froms
[fromindex
] == 0){
501 (fromindex
* HASHFRACTION
* sizeof(*m
->froms
));
502 for(toindex
= m
->froms
[fromindex
];
504 toindex
= m
->tos
[toindex
].link
){
505 arc_data
.size
+= sizeof(struct rawarc_order
);
508 write(fd
, &arc_data
, sizeof(struct gmon_data
));
510 for(fromindex
= 0; fromindex
< endfrom
; fromindex
++){
511 if(m
->froms
[fromindex
] == 0){
515 (fromindex
* HASHFRACTION
* sizeof(*m
->froms
));
516 for(toindex
= m
->froms
[fromindex
];
518 toindex
= m
->tos
[toindex
].link
){
520 fprintf(stderr
, "[monoutput] frompc 0x%x selfpc 0x%x "
521 "count %ld order %lu\n", (unsigned int)frompc
,
522 (unsigned int)m
->tos
[toindex
].selfpc
,
523 m
->tos
[toindex
].count
, m
->tos
[toindex
].order
);
525 rawarc_order
.raw_frompc
= (unsigned long)frompc
;
526 rawarc_order
.raw_selfpc
= (unsigned long)
527 m
->tos
[toindex
].selfpc
;
528 rawarc_order
.raw_count
= m
->tos
[toindex
].count
;
529 rawarc_order
.raw_order
= m
->tos
[toindex
].order
;
530 write(fd
, &rawarc_order
, sizeof(struct rawarc_order
));
543 int nfunc
) /* nfunc is not used; available for compatability only. */
555 monoutput("gmon.out");
560 p
= (struct gmonhdr
*)buf
;
561 memset(p
, '\0', sizeof(struct gmonhdr
));
562 p
->lpc
= (unsigned long)lowpc
;
563 p
->hpc
= (unsigned long)highpc
;
565 p
->version
= GMONVERSION
;
566 p
->profrate
= getprofhz();
567 bufsiz
-= sizeof(struct gmonhdr
);
572 m
->scale
= ((float) bufsiz
/ o
) * SCALE_1_TO_1
;
574 m
->scale
= SCALE_1_TO_1
;
580 * profiling is what mcount checks to see if
581 * all the data structures are ready.
594 profil(m
->sbuf
+ sizeof(struct gmonhdr
),
595 m
->ssiz
- sizeof(struct gmonhdr
),
596 (int)m
->lowpc
, m
->scale
);
597 for(i
= 1; i
< nmon
; i
++)
598 add_profil(mon
[i
].sbuf
+ sizeof(struct gmonhdr
),
599 mon
[i
].ssiz
- sizeof(struct gmonhdr
),
600 (int)mon
[i
].lowpc
, mon
[i
].scale
);
606 profil((char *)0, 0, 0, 0);
616 unsigned short *frompcindex
;
617 struct tostruct
*top
, *prevtop
;
618 unsigned long i
, toindex
;
625 * Check that we are profiling and that we aren't recursively invoked.
626 * This should really be a test and set instruction in changing the
627 * value of profiling.
635 fprintf(stderr
, "[moncount] frompc 0x%x selfpc 0x%x\n",
636 (unsigned int)frompc
, (unsigned int)selfpc
);
638 frompcindex
= (unsigned short *)frompc
;
641 * check that frompcindex is a reasonable pc value.
642 * for example: signal catchers get called from the stack,
643 * not from text space. too bad.
645 for(i
= 0; i
< nmon
; i
++){
647 if((unsigned long)frompcindex
>= (unsigned long)m
->lowpc
&&
648 (unsigned long)frompcindex
< (unsigned long)m
->highpc
)
655 frompcindex
= (unsigned short *)
656 ((unsigned long)frompcindex
- (unsigned long)m
->lowpc
);
659 &m
->froms
[((long)frompcindex
) / (HASHFRACTION
* sizeof(*m
->froms
))];
660 toindex
= *frompcindex
;
663 * first time traversing this arc
665 toindex
= ++m
->tos
[0].link
;
666 if(toindex
>= m
->tolimit
){
669 *frompcindex
= toindex
;
670 top
= &m
->tos
[toindex
];
671 top
->selfpc
= (unsigned long)selfpc
;
674 top
->order
= ++order
;
677 top
= &m
->tos
[toindex
];
678 if(top
->selfpc
== (unsigned long)selfpc
){
680 * arc at front of chain; usual case.
686 * have to go looking down chain for it.
687 * top points to what we are looking at,
688 * prevtop points to previous top.
689 * we know it is not at the head of the chain.
691 for(; /* goto done */; ){
694 * top is end of the chain and none of the chain
695 * had top->selfpc == selfpc.
696 * so we allocate a new tostruct
697 * and link it to the head of the chain.
699 toindex
= ++m
->tos
[0].link
;
700 if(toindex
>= m
->tolimit
){
703 top
= &m
->tos
[toindex
];
704 top
->selfpc
= (unsigned long)selfpc
;
706 top
->link
= *frompcindex
;
707 top
->order
= ++order
;
708 *frompcindex
= toindex
;
712 * otherwise, check the next arc on the chain.
715 top
= &m
->tos
[top
->link
];
716 if(top
->selfpc
== (unsigned long)selfpc
){
719 * increment its count
720 * move it to the head of the chain.
723 toindex
= prevtop
->link
;
724 prevtop
->link
= top
->link
;
725 top
->link
= *frompcindex
;
726 *frompcindex
= toindex
;
735 profiling
++; /* halt further profiling */
736 #define TOLIMIT "mcount: tos overflow\n"
737 write(2, TOLIMIT
, sizeof(TOLIMIT
) - 1);
741 * Get the profiling rate.
749 struct clockinfo clockrate
;
752 mib
[1] = KERN_CLOCKRATE
;
753 clockrate
.profhz
= 1;
754 size
= sizeof(clockrate
);
755 if(sysctl(mib
, 2, &clockrate
, &size
, NULL
, 0) < 0)
757 return(clockrate
.profhz
);