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 2005 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2014 Howard Su
26 * Copyright 2015 George V. Neville-Neil
27 * Copyright 2015 Ruslan Bukin <br@bsdpad.com>
28 */
29
30#pragma ident	"%Z%%M%	%I%	%E% SMI"
31
32#include <stdlib.h>
33#include <assert.h>
34#include <errno.h>
35#include <string.h>
36#include <libgen.h>
37
38#include <dt_impl.h>
39#include <dt_pid.h>
40
41#if !defined(sun)
42#include <libproc_compat.h>
43#endif
44
45/*ARGSUSED*/
46int
47dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
48    fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
49{
50
51	ftp->ftps_type = DTFTP_ENTRY;
52	ftp->ftps_pc = (uintptr_t)symp->st_value;
53	ftp->ftps_size = (size_t)symp->st_size;
54	ftp->ftps_noffs = 1;
55	ftp->ftps_offs[0] = 0;
56
57	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
58		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
59		    strerror(errno));
60		return (dt_set_errno(dtp, errno));
61	}
62
63	return (1);
64}
65
66int
67dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
68    fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret)
69{
70
71	dt_dprintf("%s: unimplemented\n", __func__);
72
73	return (DT_PROC_ERR);
74}
75
76/*ARGSUSED*/
77int
78dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
79    fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off)
80{
81
82	if (!ALIGNED_POINTER(off, 4))
83		return (DT_PROC_ALIGN);
84
85	ftp->ftps_type = DTFTP_OFFSETS;
86	ftp->ftps_pc = (uintptr_t)symp->st_value;
87	ftp->ftps_size = (size_t)symp->st_size;
88	ftp->ftps_noffs = 1;
89	ftp->ftps_offs[0] = off;
90
91	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
92		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
93		    strerror(errno));
94		return (dt_set_errno(dtp, errno));
95	}
96
97	return (1);
98}
99
100/*ARGSUSED*/
101int
102dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp,
103    fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern)
104{
105	ulong_t i;
106
107	ftp->ftps_type = DTFTP_OFFSETS;
108	ftp->ftps_pc = (uintptr_t)symp->st_value;
109	ftp->ftps_size = (size_t)symp->st_size;
110	ftp->ftps_noffs = 0;
111
112	/*
113	 * If we're matching against everything, just iterate through each
114	 * instruction in the function, otherwise look for matching offset
115	 * names by constructing the string and comparing it against the
116	 * pattern.
117	 */
118	if (strcmp("*", pattern) == 0) {
119		for (i = 0; i < symp->st_size; i += 4) {
120			ftp->ftps_offs[ftp->ftps_noffs++] = i;
121		}
122	} else {
123		char name[sizeof (i) * 2 + 1];
124
125		for (i = 0; i < symp->st_size; i += 4) {
126			(void) sprintf(name, "%lx", i);
127			if (gmatch(name, pattern))
128				ftp->ftps_offs[ftp->ftps_noffs++] = i;
129		}
130	}
131
132	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
133		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
134		    strerror(errno));
135		return (dt_set_errno(dtp, errno));
136	}
137
138	return (ftp->ftps_noffs);
139}
140