xref: /illumos-gate/usr/src/uts/common/syscall/profil.c (revision dbed73cbda2229fd1aa6dc5743993cae7f0a7ee9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1998, Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 
31 #ident	"%Z%%M%	%I%	%E% SMI"	/* from SVr4.0 1.78 */
32 
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/sysmacros.h>
36 #include <sys/systm.h>
37 #include <sys/errno.h>
38 #include <sys/proc.h>
39 #include <sys/debug.h>
40 
41 /*
42  * Profiling.
43  */
44 int
45 profil(unsigned short *bufbase, size_t bufsize, u_long pcoffset, u_int pcscale)
46 {
47 	struct proc *p = ttoproc(curthread);
48 
49 	if (pcscale == 1)
50 		pcscale = 0;
51 
52 	mutex_enter(&p->p_pflock);
53 	p->p_prof.pr_base = bufbase;
54 	p->p_prof.pr_size = bufsize;
55 	p->p_prof.pr_off = pcoffset;
56 	p->p_prof.pr_scale = pcscale;
57 
58 	/* pcsample and profil are mutually exclusive */
59 	p->p_prof.pr_samples = 0;
60 
61 	mutex_exit(&p->p_pflock);
62 	mutex_enter(&p->p_lock);
63 	set_proc_post_sys(p);	/* activate post_syscall profiling code */
64 	mutex_exit(&p->p_lock);
65 	return (0);
66 }
67 
68 
69 /*
70  * PC Sampling
71  */
72 long
73 pcsample(void *buf, long nsamples)
74 {
75 	struct proc *p = ttoproc(curthread);
76 	long count = 0;
77 
78 	if (nsamples < 0 ||
79 	    ((get_udatamodel() != DATAMODEL_NATIVE) && (nsamples > INT32_MAX)))
80 		return (set_errno(EINVAL));
81 
82 	mutex_enter(&p->p_pflock);
83 	p->p_prof.pr_base = buf;
84 	p->p_prof.pr_size = nsamples;
85 	p->p_prof.pr_scale = 1;
86 	count = p->p_prof.pr_samples;
87 	p->p_prof.pr_samples = 0;
88 	mutex_exit(&p->p_pflock);
89 
90 	mutex_enter(&p->p_lock);
91 	set_proc_post_sys(p);	/* activate post_syscall profiling code */
92 	mutex_exit(&p->p_lock);
93 
94 	return (count);
95 }
96