xref: /titanic_50/usr/src/uts/common/syscall/profil.c (revision 7b07063d906859b2be1e88791f801b3c96e432f6)
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  * Copyright 2012 Milan Jurik. All rights reserved.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved	*/
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/systm.h>
35 #include <sys/errno.h>
36 #include <sys/proc.h>
37 #include <sys/debug.h>
38 
39 /*
40  * Profiling.
41  */
42 int
43 profil(unsigned short *bufbase, size_t bufsize, ulong_t pcoffset,
44     uint_t pcscale)
45 {
46 	struct proc *p = ttoproc(curthread);
47 
48 	if (pcscale == 1)
49 		pcscale = 0;
50 
51 	mutex_enter(&p->p_pflock);
52 	p->p_prof.pr_base = bufbase;
53 	p->p_prof.pr_size = bufsize;
54 	p->p_prof.pr_off = pcoffset;
55 	p->p_prof.pr_scale = pcscale;
56 
57 	/* pcsample and profil are mutually exclusive */
58 	p->p_prof.pr_samples = 0;
59 
60 	mutex_exit(&p->p_pflock);
61 	mutex_enter(&p->p_lock);
62 	set_proc_post_sys(p);	/* activate post_syscall profiling code */
63 	mutex_exit(&p->p_lock);
64 	return (0);
65 }
66 
67 
68 /*
69  * PC Sampling
70  */
71 long
72 pcsample(void *buf, long nsamples)
73 {
74 	struct proc *p = ttoproc(curthread);
75 	long count = 0;
76 
77 	if (nsamples < 0 ||
78 	    ((get_udatamodel() != DATAMODEL_NATIVE) && (nsamples > INT32_MAX)))
79 		return (set_errno(EINVAL));
80 
81 	mutex_enter(&p->p_pflock);
82 	p->p_prof.pr_base = buf;
83 	p->p_prof.pr_size = nsamples;
84 	p->p_prof.pr_scale = 1;
85 	count = p->p_prof.pr_samples;
86 	p->p_prof.pr_samples = 0;
87 	mutex_exit(&p->p_pflock);
88 
89 	mutex_enter(&p->p_lock);
90 	set_proc_post_sys(p);	/* activate post_syscall profiling code */
91 	mutex_exit(&p->p_lock);
92 
93 	return (count);
94 }
95