xref: /titanic_51/usr/src/cmd/sgs/libld/common/debug.c (revision c93c462eec9d46f84d567abf52eb29a27c2e134b)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include	<stdio.h>
28 #include	<stdarg.h>
29 #include	<strings.h>
30 #include	<dlfcn.h>
31 #include	<debug.h>
32 #include	"msg.h"
33 
34 /*
35  * dbg_setup() can be called a number of times.  The typical use through
36  * LD_OPTIONS, results in dbg_setup() being called as the first argument to
37  * ld(1).  It's also possible to pass debugging tokens through the compiler,
38  * for example -Wl,-Dlibs -Wl-Ddetail, in which case multiple dbg_setup()
39  * calls are made.
40  *
41  * A distinction is also made between diagnostics being requested before any
42  * other ld(1) options are read, or whether the debugging options occur
43  * between other options on the command line.  In the latter case, the
44  * debugging options can be used to isolate diagnostics around one or more
45  * input files.  The "phase" argument allows us to select which phase of
46  * dbg_setup() processing we should isolate ourselves to.
47  *
48  * dbg_print() can require the output filename for use in the diagnostics
49  * created.  Save the address of the output filename pointer for this use.
50  */
51 static const char	**Name = NULL;
52 static int		Phase = 0;
53 
54 uintptr_t
55 dbg_setup(const char *options, Dbg_desc *dbp, const char **name, int phase)
56 {
57 	if (Phase == 0)
58 		Phase = phase;
59 	else if (Phase != phase)
60 		return (0);
61 
62 	Name = name;
63 
64 	/*
65 	 * Call the debugging setup routine to initialize the mask and
66 	 * debug function array.
67 	 */
68 	return (Dbg_setup(options, dbp));
69 }
70 
71 /* PRINTFLIKE2 */
72 void
73 dbg_print(Lm_list *lml, const char *format, ...)
74 {
75 	static char	*prestr = NULL;
76 	va_list		args;
77 
78 #if	defined(lint)
79 	/*
80 	 * The lml argument is only meaningful for diagnostics sent to ld.so.1.
81 	 * Supress the lint error by making a dummy assignment.
82 	 */
83 	lml = NULL;
84 #endif
85 	/*
86 	 * Knock off any newline indicator to signify that a diagnostic has
87 	 * been processed.
88 	 */
89 	dbg_desc->d_extra &= ~DBG_E_STDNL;
90 
91 	if (DBG_ISSNAME()) {
92 		/*
93 		 * If the debugging options have requested each diagnostic line
94 		 * be prepended by a name create a prefix string.
95 		 */
96 		if ((prestr == NULL) && *Name) {
97 			const char	*name, *cls;
98 			size_t		len;
99 
100 			/*
101 			 * Select the fullname or basename of the output file
102 			 * being created.
103 			 */
104 			if (DBG_ISFNAME())
105 				name = *Name;
106 			else {
107 				if ((name =
108 				    strrchr(*Name, '/')) == NULL)
109 					name = *Name;
110 				else
111 					name++;
112 			}
113 			len = strlen(name) +
114 			    strlen(MSG_INTL(MSG_DBG_NAME_FMT)) + 1;
115 
116 			/*
117 			 * Add the output file class if required.
118 			 */
119 			if (DBG_ISCLASS()) {
120 #if	defined(_ELF64)
121 				len += MSG_DBG_CLS64_FMT_SIZE;
122 				cls = MSG_ORIG(MSG_DBG_CLS64_FMT);
123 #else
124 				len += MSG_DBG_CLS32_FMT_SIZE;
125 				cls = MSG_ORIG(MSG_DBG_CLS32_FMT);
126 #endif
127 			}
128 
129 			/*
130 			 * Allocate a string to build the prefix.
131 			 */
132 			if ((prestr = libld_malloc(len)) == NULL)
133 				prestr = (char *)MSG_INTL(MSG_DBG_DFLT_FMT);
134 			else {
135 				(void) snprintf(prestr, len,
136 				    MSG_INTL(MSG_DBG_NAME_FMT), name);
137 				if (DBG_ISCLASS())
138 					(void) strcat(prestr, cls);
139 			}
140 		}
141 		if (prestr)
142 			(void) fputs(prestr, stderr);
143 		else
144 			(void) fputs(MSG_INTL(MSG_DBG_AOUT_FMT), stderr);
145 	} else
146 		(void) fputs(MSG_INTL(MSG_DBG_DFLT_FMT), stderr);
147 
148 	va_start(args, format);
149 	(void) vfprintf(stderr, format, args);
150 	(void) fprintf(stderr, MSG_ORIG(MSG_STR_NL));
151 	va_end(args);
152 }
153