dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / sgs / rtld / common / debug.c
blob6942ef8434a47f0726025bfa10eb212911e2c1e9
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/param.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <stdarg.h>
33 #include <dlfcn.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <thread.h>
37 #include <debug.h>
38 #include <conv.h>
39 #include "_rtld.h"
40 #include "_elf.h"
41 #include "msg.h"
44 static int dbg_fd; /* debugging output file descriptor */
45 static dev_t dbg_dev;
46 static rtld_ino_t dbg_ino;
47 static int dbg_add_pid; /* True to add pid to debug file name */
48 static pid_t pid;
51 * Enable diagnostic output. All debugging functions reside in the linker
52 * debugging library liblddbg.so which is lazy loaded when required.
54 int
55 dbg_setup(const char *options, Dbg_desc *dbp)
57 rtld_stat_t status;
58 const char *ofile;
61 * If we're running secure, only allow debugging if ld.so.1 itself is
62 * owned by root and has its mode setuid. Fail silently.
64 if ((rtld_flags & RT_FL_SECURE) && (is_rtld_setuid() == 0))
65 return (1);
68 * As Dbg_setup() will effectively lazy load the necessary support
69 * libraries, make sure ld.so.1 is initialized for plt relocations.
71 if (elf_rtld_load() == 0)
72 return (1);
75 * Call the debugging setup routine. This function verifies the
76 * debugging tokens provided and returns a mask indicating the debugging
77 * categories selected. The mask effectively enables calls to the
78 * debugging library.
80 if (Dbg_setup(DBG_CALLER_RTLD, options, dbp, &ofile) == 0)
81 return (0);
84 * Obtain the process id.
86 pid = getpid();
89 * If an LD_DEBUG_OUTPUT file was specified then we need to direct all
90 * diagnostics to the specified file. Add the process id as a file
91 * suffix so that multiple processes that inherit the same debugging
92 * environment variable don't fight over the same file.
94 * If LD_DEBUG_OUTPUT is not specified, and the output=file token
95 * was, then we direct all diagnostics to that file. Unlike
96 * LD_DEBUG_OUTPUT, we do not add the process id suffix. This
97 * is more convenient for interactive use.
99 * If neither redirection option is present, we send debugging
100 * output to stderr. Note that the caller will not be able
101 * to pipe or redirect this output at the shell level. libc
102 * has not yet initialized things to make that possible.
104 if (dbg_file == NULL) {
105 if (ofile && (*ofile != '\0'))
106 dbg_file = ofile;
107 } else {
108 dbg_add_pid = 1;
111 if (dbg_file) {
112 char _file[MAXPATHLEN];
113 const char *file;
115 if (dbg_add_pid) {
116 file = _file;
117 (void) snprintf(_file, MAXPATHLEN,
118 MSG_ORIG(MSG_DBG_FILE), dbg_file, pid);
119 } else {
120 file = dbg_file;
122 dbg_fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
123 if (dbg_fd == -1) {
124 int err = errno;
126 eprintf(&lml_rtld, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
127 file, strerror(err));
128 dbp->d_class = 0;
129 return (0);
131 } else {
133 * The default is to direct debugging to the stderr.
135 dbg_fd = 2;
139 * Initialize the dev/inode pair to enable us to determine if
140 * the debugging file descriptor is still available once the
141 * application has been entered.
143 (void) rtld_fstat(dbg_fd, &status);
144 dbg_dev = status.st_dev;
145 dbg_ino = status.st_ino;
148 * Now that the output file is established, identify the linker
149 * package, and generate help output if the user specified the
150 * debug help token.
152 Dbg_version();
153 if (dbp->d_extra & DBG_E_HELP)
154 Dbg_help();
156 return (1);
160 * Return True (1) if dbg_print() should produce output for the
161 * specified link-map list, and False (0) otherwise.
163 static int
164 dbg_lmid_validate(Lm_list *lml)
166 const char *str;
167 Aliste idx;
170 * The LDSO link-map list is a special case, requiring
171 * an explicit user request.
173 if (lml->lm_flags & LML_FLG_RTLDLM)
174 return ((dbg_desc->d_extra & DBG_E_LMID_LDSO) != 0);
177 * Approve special cases:
178 * - The link-map list has no name
179 * - lmid=all was set
180 * - lmid=alt was set, and this is not the BASE linkmap
182 if ((lml->lm_lmidstr == NULL) ||
183 ((dbg_desc->d_extra & DBG_E_LMID_ALL) != 0) ||
184 (((dbg_desc->d_extra & DBG_E_LMID_ALT) != 0) &&
185 ((lml->lm_flags & LML_FLG_BASELM) == 0)))
186 return (1);
189 * If there is no list of specific link-map list names to check,
190 * then approval depends on lmid={ldso|alt} not being specified.
192 if (aplist_nitems(dbg_desc->d_list) == 0)
193 return ((dbg_desc->d_extra &
194 (DBG_E_LMID_LDSO | DBG_E_LMID_ALT)) == 0);
197 * Compare the link-map list name against the list of approved names
199 for (APLIST_TRAVERSE(dbg_desc->d_list, idx, str))
200 if (strcmp(lml->lm_lmidstr, str) == 0)
201 return (1);
203 /* Output for this linkmap is denied */
204 return (0);
208 * All diagnostic requests are funneled to this routine.
210 /* PRINTFLIKE2 */
211 void
212 dbg_print(Lm_list *lml, const char *format, ...)
214 va_list args;
215 char buffer[ERRSIZE + 1];
216 pid_t _pid;
217 rtld_stat_t status;
218 Prfbuf prf;
221 * Knock off any newline indicator to signify that a diagnostic has
222 * been processed.
224 dbg_desc->d_extra &= ~DBG_E_STDNL;
227 * If debugging has been isolated to individual link-map lists,
228 * determine whether this request originates from a link-map list that
229 * is being monitored.
231 if (lml && (dbg_lmid_validate(lml) == 0))
232 return;
235 * If we're in the application make sure the debugging file descriptor
236 * is still available (ie, the user hasn't closed and/or reused the
237 * same descriptor).
239 if (rtld_flags & RT_FL_APPLIC) {
240 if ((rtld_fstat(dbg_fd, &status) == -1) ||
241 (status.st_dev != dbg_dev) ||
242 (status.st_ino != dbg_ino)) {
243 if (dbg_file) {
245 * If the user specified output file has been
246 * disconnected try and reconnect to it.
248 char _file[MAXPATHLEN];
249 const char *file;
251 if (dbg_add_pid) {
252 file = _file;
253 (void) snprintf(_file, MAXPATHLEN,
254 MSG_ORIG(MSG_DBG_FILE), dbg_file,
255 pid);
256 } else {
257 file = dbg_file;
259 if ((dbg_fd = open(file, (O_RDWR | O_APPEND),
260 0)) == -1) {
261 dbg_desc->d_class = 0;
262 return;
264 (void) rtld_fstat(dbg_fd, &status);
265 dbg_dev = status.st_dev;
266 dbg_ino = status.st_ino;
267 } else {
269 * If stderr has been stolen from us simply
270 * turn debugging off.
272 dbg_desc->d_class = 0;
273 return;
278 prf.pr_fd = dbg_fd;
281 * Obtain the process id.
283 _pid = getpid();
286 * Each time ld.so.1 is entered, the diagnostic times are reset. It is
287 * useful to convey this reset as part of our diagnostics, but only if
288 * other diagnostics will follow. If a reset has preceded this
289 * diagnostic, print a division line.
291 if (DBG_ISRESET()) {
292 DBG_OFFRESET();
294 prf.pr_buf = prf.pr_cur = buffer;
295 prf.pr_len = ERRSIZE;
297 if (lml)
298 (void) bufprint(&prf, MSG_ORIG(MSG_DBG_PID), _pid);
299 else
300 (void) bufprint(&prf, MSG_ORIG(MSG_DBG_UNDEF));
301 prf.pr_cur--;
303 (void) bufprint(&prf, MSG_ORIG(MSG_DBG_RESET));
304 (void) dowrite(&prf);
308 * Reestablish the buffer for standard printing.
310 prf.pr_buf = prf.pr_cur = buffer;
311 prf.pr_len = ERRSIZE;
314 * Establish any diagnostic prefix strings.
316 if (lml)
317 (void) bufprint(&prf, MSG_ORIG(MSG_DBG_PID), _pid);
318 else
319 (void) bufprint(&prf, MSG_ORIG(MSG_DBG_UNDEF));
320 prf.pr_cur--;
322 if (DBG_ISLMID() && lml && lml->lm_lmidstr) {
323 (void) bufprint(&prf, MSG_ORIG(MSG_DBG_LMID), lml->lm_lmidstr);
324 prf.pr_cur--;
326 if (DBG_ISTIME()) {
327 struct timeval new;
329 if (gettimeofday(&new, NULL) == 0) {
330 Conv_time_buf_t buf;
332 if (DBG_ISTTIME()) {
333 (void) bufprint(&prf,
334 conv_time(&DBG_TOTALTIME, &new, &buf));
335 prf.pr_cur--;
337 if (DBG_ISDTIME()) {
338 (void) bufprint(&prf,
339 conv_time(&DBG_DELTATIME, &new, &buf));
340 prf.pr_cur--;
342 DBG_DELTATIME = new;
345 if (rtld_flags & RT_FL_THREADS) {
346 (void) bufprint(&prf, MSG_ORIG(MSG_DBG_THREAD), rt_thr_self());
347 prf.pr_cur--;
351 * Format the message and print it.
353 va_start(args, format);
354 (void) doprf(format, args, &prf);
355 *(prf.pr_cur - 1) = '\n';
356 (void) dowrite(&prf);
357 va_end(args);