summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sgs/libld/common/debug.c
blob: a42c0a6ec9fadb92db9378ff01910f1a222f8b0a (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
/*
 * 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) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include	<stdio.h>
#include	<stdarg.h>
#include	<errno.h>
#include	<strings.h>
#include	<dlfcn.h>
#include	<debug.h>
#include	<conv.h>
#include	"msg.h"
#include	"_libld.h"

/*
 * dbg_setup() can be called a number of times.  The typical use through
 * LD_OPTIONS, results in dbg_setup() being called as the first argument to
 * ld(1).  It's also possible to pass debugging tokens through the compiler,
 * for example -Wl,-Dlibs -Wl-Ddetail, in which case multiple dbg_setup()
 * calls are made.
 *
 * A distinction is also made between diagnostics being requested before any
 * other ld(1) options are read, or whether the debugging options occur
 * between other options on the command line.  In the latter case, the
 * debugging options can be used to isolate diagnostics around one or more
 * input files.  The "phase" argument allows us to select which phase of
 * dbg_setup() processing we should isolate ourselves to.
 *
 * dbg_print() can require the output filename for use in the diagnostics
 * created.  Save the address of the output filename pointer for this use.
 */
static const char	**Name = NULL;
static int		Phase = 0;

/* Debug file output state */
static struct {
	FILE	*fptr;	/* File to send debug output */
	int	close_needed;	/* True if explicitly opened stream */
} dbg_ofile = {
	stderr,
	0
};


/*
 * If there is an explicitly opened debug file, close it and reset the state.
 */
void
dbg_cleanup(void)
{
	if (dbg_ofile.close_needed) {
		(void) fclose(dbg_ofile.fptr);
		dbg_ofile.close_needed = 0;
		dbg_ofile.fptr = stderr;
	}
}

/*
 * Process debug tokens. Returns True (1) on success, and False (0)
 * on failure.
 */
int
dbg_setup(Ofl_desc *ofl, const char *options, int phase)
{
	const char	*ofile;

	if (Phase == 0)
		Phase = phase;
	else if (Phase != phase)
		return (1);

	Name = &ofl->ofl_name;

	/*
	 * Call the debugging setup routine to initialize the mask and
	 * debug function array.
	 */
	if (Dbg_setup(DBG_CALLER_LD, options, dbg_desc, &ofile) == 0)
		return (0);

	/*
	 * If output= token was used, close the old file if necessary
	 * and open a new one if the file name is not NULL.
	 */
	if (ofile) {
		dbg_cleanup();
		if (*ofile != '\0') {
			FILE *fptr = fopen(ofile, MSG_ORIG(MSG_DBG_FOPEN_MODE));
			if (fptr == NULL) {
				int	err = errno;

				ld_eprintf(ofl, ERR_FATAL,
				    MSG_INTL(MSG_SYS_OPEN), ofile,
				    strerror(err));
				return (0);
			} else {
				dbg_ofile.fptr = fptr;
				dbg_ofile.close_needed = 1;
			}
		}
	}

	/*
	 * Now that the output file is established, identify the linker
	 * package, and generate help output if the user specified the
	 * debug help token.
	 */
	Dbg_version();
	if (dbg_desc->d_extra & DBG_E_HELP)
		Dbg_help();

	return (1);
}

/* PRINTFLIKE2 */
void
dbg_print(Lm_list *lml, const char *format, ...)
{
	static char	*prestr = NULL;
	va_list		args;

#if	defined(lint)
	/*
	 * The lml argument is only meaningful for diagnostics sent to ld.so.1.
	 * Supress the lint error by making a dummy assignment.
	 */
	lml = NULL;
#endif
	/*
	 * Knock off any newline indicator to signify that a diagnostic has
	 * been processed.
	 */
	dbg_desc->d_extra &= ~DBG_E_STDNL;

	if (DBG_ISSNAME()) {
		/*
		 * If the debugging options have requested each diagnostic line
		 * be prepended by a name create a prefix string.
		 */
		if ((prestr == NULL) && *Name) {
			const char	*name, *cls;
			size_t		len;

			/*
			 * Select the fullname or basename of the output file
			 * being created.
			 */
			if (DBG_ISFNAME())
				name = *Name;
			else {
				if ((name =
				    strrchr(*Name, '/')) == NULL)
					name = *Name;
				else
					name++;
			}
			len = strlen(name) +
			    strlen(MSG_INTL(MSG_DBG_NAME_FMT)) + 1;

			/*
			 * Add the output file class if required.
			 */
			if (DBG_ISCLASS()) {
#if	defined(_ELF64)
				len += MSG_DBG_CLS64_FMT_SIZE;
				cls = MSG_ORIG(MSG_DBG_CLS64_FMT);
#else
				len += MSG_DBG_CLS32_FMT_SIZE;
				cls = MSG_ORIG(MSG_DBG_CLS32_FMT);
#endif
			}

			/*
			 * Allocate a string to build the prefix.
			 */
			if ((prestr = libld_malloc(len)) == NULL)
				prestr = (char *)MSG_INTL(MSG_DBG_DFLT_FMT);
			else {
				(void) snprintf(prestr, len,
				    MSG_INTL(MSG_DBG_NAME_FMT), name);
				if (DBG_ISCLASS())
					(void) strcat(prestr, cls);
			}
		}
		(void) fputs(prestr ? prestr : MSG_INTL(MSG_DBG_AOUT_FMT),
		    dbg_ofile.fptr);
	} else
		(void) fputs(MSG_INTL(MSG_DBG_DFLT_FMT), dbg_ofile.fptr);

	if (DBG_ISTIME()) {
		Conv_time_buf_t	buf;
		struct timeval	new;

		if (gettimeofday(&new, NULL) == 0) {
			if (DBG_ISTTIME())
				(void) fputs(conv_time(&DBG_TOTALTIME, &new,
				    &buf), stderr);
			if (DBG_ISDTIME())
				(void) fputs(conv_time(&DBG_DELTATIME, &new,
				    &buf), stderr);

			DBG_DELTATIME = new;
		}
	}

	va_start(args, format);
	(void) vfprintf(dbg_ofile.fptr, format, args);
	(void) fprintf(dbg_ofile.fptr, MSG_ORIG(MSG_STR_NL));
	va_end(args);
}