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]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/types.h>
29 #include <sys/param.h>
44 static int dbg_fd
; /* debugging output file descriptor */
46 static rtld_ino_t dbg_ino
;
47 static int dbg_add_pid
; /* True to add pid to debug file name */
51 * Enable diagnostic output. All debugging functions reside in the linker
52 * debugging library liblddbg.so which is lazy loaded when required.
55 dbg_setup(const char *options
, Dbg_desc
*dbp
)
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))
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)
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
80 if (Dbg_setup(DBG_CALLER_RTLD
, options
, dbp
, &ofile
) == 0)
84 * Obtain the process id.
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'))
112 char _file
[MAXPATHLEN
];
117 (void) snprintf(_file
, MAXPATHLEN
,
118 MSG_ORIG(MSG_DBG_FILE
), dbg_file
, pid
);
122 dbg_fd
= open(file
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
126 eprintf(&lml_rtld
, ERR_FATAL
, MSG_INTL(MSG_SYS_OPEN
),
127 file
, strerror(err
));
133 * The default is to direct debugging to the stderr.
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
153 if (dbp
->d_extra
& DBG_E_HELP
)
160 * Return True (1) if dbg_print() should produce output for the
161 * specified link-map list, and False (0) otherwise.
164 dbg_lmid_validate(Lm_list
*lml
)
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
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)))
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)
203 /* Output for this linkmap is denied */
208 * All diagnostic requests are funneled to this routine.
212 dbg_print(Lm_list
*lml
, const char *format
, ...)
215 char buffer
[ERRSIZE
+ 1];
221 * Knock off any newline indicator to signify that a diagnostic has
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))
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
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
)) {
245 * If the user specified output file has been
246 * disconnected try and reconnect to it.
248 char _file
[MAXPATHLEN
];
253 (void) snprintf(_file
, MAXPATHLEN
,
254 MSG_ORIG(MSG_DBG_FILE
), dbg_file
,
259 if ((dbg_fd
= open(file
, (O_RDWR
| O_APPEND
),
261 dbg_desc
->d_class
= 0;
264 (void) rtld_fstat(dbg_fd
, &status
);
265 dbg_dev
= status
.st_dev
;
266 dbg_ino
= status
.st_ino
;
269 * If stderr has been stolen from us simply
270 * turn debugging off.
272 dbg_desc
->d_class
= 0;
281 * Obtain the process id.
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.
294 prf
.pr_buf
= prf
.pr_cur
= buffer
;
295 prf
.pr_len
= ERRSIZE
;
298 (void) bufprint(&prf
, MSG_ORIG(MSG_DBG_PID
), _pid
);
300 (void) bufprint(&prf
, MSG_ORIG(MSG_DBG_UNDEF
));
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.
317 (void) bufprint(&prf
, MSG_ORIG(MSG_DBG_PID
), _pid
);
319 (void) bufprint(&prf
, MSG_ORIG(MSG_DBG_UNDEF
));
322 if (DBG_ISLMID() && lml
&& lml
->lm_lmidstr
) {
323 (void) bufprint(&prf
, MSG_ORIG(MSG_DBG_LMID
), lml
->lm_lmidstr
);
329 if (gettimeofday(&new, NULL
) == 0) {
333 (void) bufprint(&prf
,
334 conv_time(&DBG_TOTALTIME
, &new, &buf
));
338 (void) bufprint(&prf
,
339 conv_time(&DBG_DELTATIME
, &new, &buf
));
345 if (rtld_flags
& RT_FL_THREADS
) {
346 (void) bufprint(&prf
, MSG_ORIG(MSG_DBG_THREAD
), rt_thr_self());
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
);