summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sgs/link_audit/common/truss.c
blob: 6e8a1ca1e1498d2dd8e81903062a5764e03ac0ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
 */
#include <link.h>
#include <sys/types.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "env.h"
#include "mach.h"

static Elist		*bindto_list = NULL;
static Elist		*bindfrom_list = NULL;

static uint_t		pidout = 0;
static pid_t		pid;
static FILE		*outfile = stderr;
static uint_t		indent = 1;
static uint_t		indent_level = 1;
static uint_t		trussall = 0;
static uint_t		noexit = 0;
static sigset_t		iset;

/*
 * It's not possible to gather the return code on routines
 * which actually have a dependence on the 'stack frame structure'.
 * Below is a list of known symbols which have this dependency,
 * truss.so will disable the la_pltexit() entry point for these
 * routines, which will remove the requirement for the extra
 * stackframe that the link_auditing interface creates.
 *
 * NOTE: this list *must* be mainted in alphabetical order.
 *	 if this list ever became to long a faster search mechanism
 *	 should be considered.
 */
static char	*spec_sym[] = {
#if	defined(__sparc)
	".stret1",
	".stret2",
	".stret4",
	".stret8",
#endif
	"__getcontext",
	"_getcontext",
	"_getsp",
	"_longjmp",
	"_setcontext",
	"_setjmp",
	"_siglongjmp",
	"_sigsetjmp",
	"_vfork",
	"getcontext",
	"getsp",
	"longjmp",
	"setcontext",
	"setjmp",
	"siglongjmp",
	"sigsetjmp",
	"vfork",
	(char *)0
};

uint_t
la_version(uint_t version)
{
	char	*str;

	if (version > LAV_CURRENT)
		(void) fprintf(stderr, "truss.so: unexpected version: %d\n",
		    version);

	build_env_list(&bindto_list, (const char *)"TRUSS_BINDTO");
	build_env_list(&bindfrom_list, (const char *)"TRUSS_BINDFROM");

	if (checkenv((const char *)"TRUSS_PID")) {
		pidout = 1;
		pid = getpid();
	} else {
		char	*str = "LD_AUDIT=";
		/*
		 * This disables truss output in subsequent fork()/exec
		 * processes.
		 */
		(void) putenv(str);
	}

	if (checkenv((const char *)"TRUSS_NOEXIT")) {
		noexit++;
		indent = 0;
	}

	if (checkenv((const char *)"TRUSS_NOINDENT"))
		indent = 0;

	if (checkenv((const char *)"TRUSS_ALL"))
		trussall++;

	if (str = checkenv((const char *)"TRUSS_OUTPUT")) {
		FILE	*fp;
		char	fname[MAXPATHLEN];

		if (pidout)
			(void) snprintf(fname, MAXPATHLEN, "%s.%d", str,
			    (int)pid);
		else
			(void) strncpy(fname, str, MAXPATHLEN);

		if (fp = fopen(fname, (const char *)"w")) {
			outfile = fp;
		} else
			(void) fprintf(stderr,
			    "truss.so: unable to open file=`%s': %s\n",
			    fname, strerror(errno));
	}

	/*
	 * Initalize iset to the full set of signals to be masked durring
	 * pltenter/pltexit
	 */
	(void) sigfillset(&iset);

	return (LAV_CURRENT);
}

/* ARGSUSED1 */
uint_t
la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie)
{
	uint_t	flags;
	char	*basename;
	static int	first = 1;

	if ((bindto_list == NULL) || (trussall))
		flags = LA_FLG_BINDTO;
	else if (check_list(bindto_list, lmp->l_name))
		flags = LA_FLG_BINDTO;
	else
		flags = 0;

	if (((bindfrom_list == NULL) && first) || trussall ||
	    (check_list(bindfrom_list, lmp->l_name)))
		flags |= LA_FLG_BINDFROM;

	first = 0;

	if (flags) {
		if ((basename = strrchr(lmp->l_name, '/')) != NULL)
			basename++;
		else
			basename = lmp->l_name;
		*cookie = (uintptr_t)basename;
	}

	return (flags);
}

/* ARGSUSED1 */
#if	defined(_LP64)
uintptr_t
la_symbind64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcook,
	uintptr_t *defcook, uint_t *sb_flags, const char *sym_name)
#else
uintptr_t
la_symbind32(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcook,
	uintptr_t *defcook, uint_t *sb_flags)
#endif
{
#if	!defined(_LP64)
	const char	*sym_name = (const char *)symp->st_name;
#endif


	if (noexit)
		*sb_flags |= LA_SYMB_NOPLTEXIT;

	/*
	 * Check to see if this symbol is one of the 'special' symbols.
	 * If so we disable PLTEXIT calls for that symbol.
	 */
	if ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0) {
		uint_t	ndx;
		char	*str;
		/* LINTED */
		for (ndx = 0; str = spec_sym[ndx]; ndx++) {
			int	cmpval;
			cmpval = strcmp(sym_name, str);
			if (cmpval < 0)
				break;
			if (cmpval == 0) {
				*sb_flags |= LA_SYMB_NOPLTEXIT;
				break;
			}
		}
	}
	return (symp->st_value);
}

/* ARGSUSED1 */
#if	defined(__sparcv9)
uintptr_t
la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
	uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sb_flags,
	const char *sym_name)
#elif	defined(__sparc)
uintptr_t
la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
	uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sb_flags)
#elif   defined(__amd64)
uintptr_t
la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
	uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sb_flags,
	const char *sym_name)
#elif   defined(__i386)
uintptr_t
la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
	uintptr_t *defcookie, La_i86_regs *regset, uint_t *sb_flags)
#endif
{
	char		*istr;
	char		*defname = (char *)(*defcookie);
	char		*refname = (char *)(*refcookie);
#if	!defined(_LP64)
	const char	*sym_name = (const char *)symp->st_name;
#endif
	sigset_t	oset;

	(void) sigprocmask(SIG_BLOCK, &iset, &oset);

	if (pidout)
		(void) fprintf(outfile, "%5d:", (int)getpid());

	if ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0)
		istr = "";
	else
		istr = "*";

	(void) fprintf(outfile, "%-15s -> %15s:%-*s%s(0x%lx, 0x%lx, 0x%lx)\n",
		refname, defname, indent_level, istr, sym_name,
		(long)GETARG0(regset), (long)GETARG1(regset),
		(long)GETARG2(regset));

	(void) fflush(outfile);
	if (indent && ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0))
		indent_level++;
	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
	return (symp->st_value);
}

/* ARGSUSED1 */
#if	defined(_LP64)
/* ARGSUSED */
uintptr_t
la_pltexit64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
	uintptr_t *defcookie, uintptr_t retval, const char *sym_name)
#else
uintptr_t
la_pltexit(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
	uintptr_t *defcookie, uintptr_t retval)
#endif
{
	char		*defname = (char *)(*defcookie);
	char		*refname = (char *)(*refcookie);
	sigset_t	oset;
#if	!defined(_LP64)
	const char	*sym_name = (const char *)symp->st_name;
#endif

	(void) sigprocmask(SIG_BLOCK, &iset, &oset);

	if (pidout)
		(void) fprintf(outfile, "%5d:", (int)pid);
	if (indent)
		indent_level--;
	(void) fprintf(outfile, "%-15s -> %15s:%*s%s - 0x%lx\n", refname,
		defname, indent_level, "", sym_name, (ulong_t)retval);
	(void) fflush(outfile);
	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
	return (retval);
}