10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51618Srie * Common Development and Distribution License (the "License").
61618Srie * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211618Srie
220Sstevel@tonic-gate /*
23*11734SAli.Bahrami@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/stat.h>
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <fcntl.h>
320Sstevel@tonic-gate #include <stdarg.h>
330Sstevel@tonic-gate #include <dlfcn.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <thread.h>
371618Srie #include <debug.h>
389577SRod.Evans@Sun.COM #include <conv.h>
390Sstevel@tonic-gate #include "_rtld.h"
400Sstevel@tonic-gate #include "_elf.h"
410Sstevel@tonic-gate #include "msg.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate
448394SAli.Bahrami@Sun.COM static int dbg_fd; /* debugging output file descriptor */
458394SAli.Bahrami@Sun.COM static dev_t dbg_dev;
468394SAli.Bahrami@Sun.COM static rtld_ino_t dbg_ino;
479406SAli.Bahrami@Sun.COM static int dbg_add_pid; /* True to add pid to debug file name */
488394SAli.Bahrami@Sun.COM static pid_t pid;
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * Enable diagnostic output. All debugging functions reside in the linker
520Sstevel@tonic-gate * debugging library liblddbg.so which is lazy loaded when required.
530Sstevel@tonic-gate */
549406SAli.Bahrami@Sun.COM int
dbg_setup(const char * options,Dbg_desc * dbp)551618Srie dbg_setup(const char *options, Dbg_desc *dbp)
560Sstevel@tonic-gate {
578394SAli.Bahrami@Sun.COM rtld_stat_t status;
589406SAli.Bahrami@Sun.COM const char *ofile;
590Sstevel@tonic-gate
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate * If we're running secure, only allow debugging if ld.so.1 itself is
620Sstevel@tonic-gate * owned by root and has its mode setuid. Fail silently.
630Sstevel@tonic-gate */
647668SRod.Evans@Sun.COM if ((rtld_flags & RT_FL_SECURE) && (is_rtld_setuid() == 0))
659406SAli.Bahrami@Sun.COM return (1);
660Sstevel@tonic-gate
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate * As Dbg_setup() will effectively lazy load the necessary support
690Sstevel@tonic-gate * libraries, make sure ld.so.1 is initialized for plt relocations.
700Sstevel@tonic-gate */
710Sstevel@tonic-gate if (elf_rtld_load() == 0)
729406SAli.Bahrami@Sun.COM return (1);
730Sstevel@tonic-gate
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * Call the debugging setup routine. This function verifies the
760Sstevel@tonic-gate * debugging tokens provided and returns a mask indicating the debugging
770Sstevel@tonic-gate * categories selected. The mask effectively enables calls to the
780Sstevel@tonic-gate * debugging library.
790Sstevel@tonic-gate */
809406SAli.Bahrami@Sun.COM if (Dbg_setup(DBG_CALLER_RTLD, options, dbp, &ofile) == 0)
819406SAli.Bahrami@Sun.COM return (0);
820Sstevel@tonic-gate
830Sstevel@tonic-gate /*
849340SRod.Evans@Sun.COM * Obtain the process id.
859340SRod.Evans@Sun.COM */
869340SRod.Evans@Sun.COM pid = getpid();
879340SRod.Evans@Sun.COM
889340SRod.Evans@Sun.COM /*
890Sstevel@tonic-gate * If an LD_DEBUG_OUTPUT file was specified then we need to direct all
900Sstevel@tonic-gate * diagnostics to the specified file. Add the process id as a file
910Sstevel@tonic-gate * suffix so that multiple processes that inherit the same debugging
920Sstevel@tonic-gate * environment variable don't fight over the same file.
939406SAli.Bahrami@Sun.COM *
949406SAli.Bahrami@Sun.COM * If LD_DEBUG_OUTPUT is not specified, and the output=file token
959406SAli.Bahrami@Sun.COM * was, then we direct all diagnostics to that file. Unlike
969406SAli.Bahrami@Sun.COM * LD_DEBUG_OUTPUT, we do not add the process id suffix. This
979406SAli.Bahrami@Sun.COM * is more convenient for interactive use.
989406SAli.Bahrami@Sun.COM *
999406SAli.Bahrami@Sun.COM * If neither redirection option is present, we send debugging
1009406SAli.Bahrami@Sun.COM * output to stderr. Note that the caller will not be able
1019406SAli.Bahrami@Sun.COM * to pipe or redirect this output at the shell level. libc
1029406SAli.Bahrami@Sun.COM * has not yet initialized things to make that possible.
1030Sstevel@tonic-gate */
1049406SAli.Bahrami@Sun.COM if (dbg_file == NULL) {
1059406SAli.Bahrami@Sun.COM if (ofile && (*ofile != '\0'))
1069406SAli.Bahrami@Sun.COM dbg_file = ofile;
1079406SAli.Bahrami@Sun.COM } else {
1089406SAli.Bahrami@Sun.COM dbg_add_pid = 1;
1099406SAli.Bahrami@Sun.COM }
1100Sstevel@tonic-gate
1119406SAli.Bahrami@Sun.COM if (dbg_file) {
1129406SAli.Bahrami@Sun.COM char _file[MAXPATHLEN];
1139406SAli.Bahrami@Sun.COM const char *file;
1149406SAli.Bahrami@Sun.COM
1159406SAli.Bahrami@Sun.COM if (dbg_add_pid) {
1169406SAli.Bahrami@Sun.COM file = _file;
1179406SAli.Bahrami@Sun.COM (void) snprintf(_file, MAXPATHLEN,
1189406SAli.Bahrami@Sun.COM MSG_ORIG(MSG_DBG_FILE), dbg_file, pid);
1199406SAli.Bahrami@Sun.COM } else {
1209406SAli.Bahrami@Sun.COM file = dbg_file;
1219406SAli.Bahrami@Sun.COM }
1229406SAli.Bahrami@Sun.COM dbg_fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
1239406SAli.Bahrami@Sun.COM if (dbg_fd == -1) {
1240Sstevel@tonic-gate int err = errno;
1250Sstevel@tonic-gate
1261618Srie eprintf(&lml_rtld, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
1271618Srie file, strerror(err));
1281618Srie dbp->d_class = 0;
1290Sstevel@tonic-gate return (0);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate } else {
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate * The default is to direct debugging to the stderr.
1340Sstevel@tonic-gate */
1350Sstevel@tonic-gate dbg_fd = 2;
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate * Initialize the dev/inode pair to enable us to determine if
1400Sstevel@tonic-gate * the debugging file descriptor is still available once the
1410Sstevel@tonic-gate * application has been entered.
1420Sstevel@tonic-gate */
1438394SAli.Bahrami@Sun.COM (void) rtld_fstat(dbg_fd, &status);
1440Sstevel@tonic-gate dbg_dev = status.st_dev;
1450Sstevel@tonic-gate dbg_ino = status.st_ino;
1460Sstevel@tonic-gate
1479406SAli.Bahrami@Sun.COM /*
148*11734SAli.Bahrami@Sun.COM * Now that the output file is established, identify the linker
149*11734SAli.Bahrami@Sun.COM * package, and generate help output if the user specified the
150*11734SAli.Bahrami@Sun.COM * debug help token.
1519406SAli.Bahrami@Sun.COM */
152*11734SAli.Bahrami@Sun.COM Dbg_version();
1539406SAli.Bahrami@Sun.COM if (dbp->d_extra & DBG_E_HELP)
1549406SAli.Bahrami@Sun.COM Dbg_help();
1559406SAli.Bahrami@Sun.COM
1569406SAli.Bahrami@Sun.COM return (1);
1571618Srie }
1581618Srie
1599406SAli.Bahrami@Sun.COM /*
1609406SAli.Bahrami@Sun.COM * Return True (1) if dbg_print() should produce output for the
1619406SAli.Bahrami@Sun.COM * specified link-map list, and False (0) otherwise.
1629406SAli.Bahrami@Sun.COM */
1631618Srie static int
dbg_lmid_validate(Lm_list * lml)1649406SAli.Bahrami@Sun.COM dbg_lmid_validate(Lm_list *lml)
1651618Srie {
1665892Sab196087 const char *str;
1675892Sab196087 Aliste idx;
1681618Srie
1699406SAli.Bahrami@Sun.COM /*
1709406SAli.Bahrami@Sun.COM * The LDSO link-map list is a special case, requiring
1719406SAli.Bahrami@Sun.COM * an explicit user request.
1729406SAli.Bahrami@Sun.COM */
1739406SAli.Bahrami@Sun.COM if (lml->lm_flags & LML_FLG_RTLDLM)
1749406SAli.Bahrami@Sun.COM return ((dbg_desc->d_extra & DBG_E_LMID_LDSO) != 0);
1759406SAli.Bahrami@Sun.COM
1769406SAli.Bahrami@Sun.COM /*
1779406SAli.Bahrami@Sun.COM * Approve special cases:
1789406SAli.Bahrami@Sun.COM * - The link-map list has no name
1799406SAli.Bahrami@Sun.COM * - lmid=all was set
1809406SAli.Bahrami@Sun.COM * - lmid=alt was set, and this is not the BASE linkmap
1819406SAli.Bahrami@Sun.COM */
1829406SAli.Bahrami@Sun.COM if ((lml->lm_lmidstr == NULL) ||
1839406SAli.Bahrami@Sun.COM ((dbg_desc->d_extra & DBG_E_LMID_ALL) != 0) ||
1849406SAli.Bahrami@Sun.COM (((dbg_desc->d_extra & DBG_E_LMID_ALT) != 0) &&
1859406SAli.Bahrami@Sun.COM ((lml->lm_flags & LML_FLG_BASELM) == 0)))
1869406SAli.Bahrami@Sun.COM return (1);
1879406SAli.Bahrami@Sun.COM
1889406SAli.Bahrami@Sun.COM /*
1899406SAli.Bahrami@Sun.COM * If there is no list of specific link-map list names to check,
1909406SAli.Bahrami@Sun.COM * then approval depends on lmid={ldso|alt} not being specified.
1919406SAli.Bahrami@Sun.COM */
1929406SAli.Bahrami@Sun.COM if (aplist_nitems(dbg_desc->d_list) == 0)
1939406SAli.Bahrami@Sun.COM return ((dbg_desc->d_extra &
1949406SAli.Bahrami@Sun.COM (DBG_E_LMID_LDSO | DBG_E_LMID_ALT)) == 0);
1959406SAli.Bahrami@Sun.COM
1969406SAli.Bahrami@Sun.COM /*
1979406SAli.Bahrami@Sun.COM * Compare the link-map list name against the list of approved names
1989406SAli.Bahrami@Sun.COM */
1999406SAli.Bahrami@Sun.COM for (APLIST_TRAVERSE(dbg_desc->d_list, idx, str))
2005892Sab196087 if (strcmp(lml->lm_lmidstr, str) == 0)
2011618Srie return (1);
2029406SAli.Bahrami@Sun.COM
2039406SAli.Bahrami@Sun.COM /* Output for this linkmap is denied */
2041618Srie return (0);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate * All diagnostic requests are funneled to this routine.
2090Sstevel@tonic-gate */
2101618Srie /* PRINTFLIKE2 */
2110Sstevel@tonic-gate void
dbg_print(Lm_list * lml,const char * format,...)2121618Srie dbg_print(Lm_list *lml, const char *format, ...)
2130Sstevel@tonic-gate {
2141618Srie va_list args;
2151618Srie char buffer[ERRSIZE + 1];
2161618Srie pid_t _pid;
2178394SAli.Bahrami@Sun.COM rtld_stat_t status;
2181618Srie Prfbuf prf;
2191618Srie
2201618Srie /*
2211618Srie * Knock off any newline indicator to signify that a diagnostic has
2221618Srie * been processed.
2231618Srie */
2241618Srie dbg_desc->d_extra &= ~DBG_E_STDNL;
2251618Srie
2261618Srie /*
2271618Srie * If debugging has been isolated to individual link-map lists,
2281618Srie * determine whether this request originates from a link-map list that
2299406SAli.Bahrami@Sun.COM * is being monitored.
2301618Srie */
2319406SAli.Bahrami@Sun.COM if (lml && (dbg_lmid_validate(lml) == 0))
2321618Srie return;
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate * If we're in the application make sure the debugging file descriptor
2360Sstevel@tonic-gate * is still available (ie, the user hasn't closed and/or reused the
2370Sstevel@tonic-gate * same descriptor).
2380Sstevel@tonic-gate */
2390Sstevel@tonic-gate if (rtld_flags & RT_FL_APPLIC) {
2408394SAli.Bahrami@Sun.COM if ((rtld_fstat(dbg_fd, &status) == -1) ||
2410Sstevel@tonic-gate (status.st_dev != dbg_dev) ||
2420Sstevel@tonic-gate (status.st_ino != dbg_ino)) {
2430Sstevel@tonic-gate if (dbg_file) {
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate * If the user specified output file has been
2460Sstevel@tonic-gate * disconnected try and reconnect to it.
2470Sstevel@tonic-gate */
2489406SAli.Bahrami@Sun.COM char _file[MAXPATHLEN];
2499406SAli.Bahrami@Sun.COM const char *file;
2500Sstevel@tonic-gate
2519406SAli.Bahrami@Sun.COM if (dbg_add_pid) {
2529406SAli.Bahrami@Sun.COM file = _file;
2539406SAli.Bahrami@Sun.COM (void) snprintf(_file, MAXPATHLEN,
2549406SAli.Bahrami@Sun.COM MSG_ORIG(MSG_DBG_FILE), dbg_file,
2559406SAli.Bahrami@Sun.COM pid);
2569406SAli.Bahrami@Sun.COM } else {
2579406SAli.Bahrami@Sun.COM file = dbg_file;
2589406SAli.Bahrami@Sun.COM }
2590Sstevel@tonic-gate if ((dbg_fd = open(file, (O_RDWR | O_APPEND),
2600Sstevel@tonic-gate 0)) == -1) {
2611618Srie dbg_desc->d_class = 0;
2620Sstevel@tonic-gate return;
2630Sstevel@tonic-gate }
2648394SAli.Bahrami@Sun.COM (void) rtld_fstat(dbg_fd, &status);
2650Sstevel@tonic-gate dbg_dev = status.st_dev;
2660Sstevel@tonic-gate dbg_ino = status.st_ino;
2670Sstevel@tonic-gate } else {
2680Sstevel@tonic-gate /*
2690Sstevel@tonic-gate * If stderr has been stolen from us simply
2700Sstevel@tonic-gate * turn debugging off.
2710Sstevel@tonic-gate */
2721618Srie dbg_desc->d_class = 0;
2730Sstevel@tonic-gate return;
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate
2781618Srie prf.pr_fd = dbg_fd;
2791618Srie
2800Sstevel@tonic-gate /*
2819340SRod.Evans@Sun.COM * Obtain the process id.
2820Sstevel@tonic-gate */
2839340SRod.Evans@Sun.COM _pid = getpid();
2840Sstevel@tonic-gate
2859577SRod.Evans@Sun.COM /*
2869577SRod.Evans@Sun.COM * Each time ld.so.1 is entered, the diagnostic times are reset. It is
2879577SRod.Evans@Sun.COM * useful to convey this reset as part of our diagnostics, but only if
2889577SRod.Evans@Sun.COM * other diagnostics will follow. If a reset has preceded this
2899577SRod.Evans@Sun.COM * diagnostic, print a division line.
2909577SRod.Evans@Sun.COM */
2919577SRod.Evans@Sun.COM if (DBG_ISRESET()) {
2929577SRod.Evans@Sun.COM DBG_OFFRESET();
2939577SRod.Evans@Sun.COM
2949577SRod.Evans@Sun.COM prf.pr_buf = prf.pr_cur = buffer;
2959577SRod.Evans@Sun.COM prf.pr_len = ERRSIZE;
2969577SRod.Evans@Sun.COM
2979577SRod.Evans@Sun.COM if (lml)
2989577SRod.Evans@Sun.COM (void) bufprint(&prf, MSG_ORIG(MSG_DBG_PID), _pid);
2999577SRod.Evans@Sun.COM else
3009577SRod.Evans@Sun.COM (void) bufprint(&prf, MSG_ORIG(MSG_DBG_UNDEF));
3019577SRod.Evans@Sun.COM prf.pr_cur--;
3029577SRod.Evans@Sun.COM
3039577SRod.Evans@Sun.COM (void) bufprint(&prf, MSG_ORIG(MSG_DBG_RESET));
3049577SRod.Evans@Sun.COM (void) dowrite(&prf);
3059577SRod.Evans@Sun.COM }
3069577SRod.Evans@Sun.COM
3079577SRod.Evans@Sun.COM /*
3089577SRod.Evans@Sun.COM * Reestablish the buffer for standard printing.
3099577SRod.Evans@Sun.COM */
3109577SRod.Evans@Sun.COM prf.pr_buf = prf.pr_cur = buffer;
3119577SRod.Evans@Sun.COM prf.pr_len = ERRSIZE;
3129577SRod.Evans@Sun.COM
3139577SRod.Evans@Sun.COM /*
3149577SRod.Evans@Sun.COM * Establish any diagnostic prefix strings.
3159577SRod.Evans@Sun.COM */
3161618Srie if (lml)
3171618Srie (void) bufprint(&prf, MSG_ORIG(MSG_DBG_PID), _pid);
3181618Srie else
3191618Srie (void) bufprint(&prf, MSG_ORIG(MSG_DBG_UNDEF));
3201618Srie prf.pr_cur--;
3210Sstevel@tonic-gate
3221618Srie if (DBG_ISLMID() && lml && lml->lm_lmidstr) {
3231618Srie (void) bufprint(&prf, MSG_ORIG(MSG_DBG_LMID), lml->lm_lmidstr);
3241618Srie prf.pr_cur--;
3251618Srie }
3269577SRod.Evans@Sun.COM if (DBG_ISTIME()) {
3279577SRod.Evans@Sun.COM struct timeval new;
3289577SRod.Evans@Sun.COM
3299577SRod.Evans@Sun.COM if (gettimeofday(&new, NULL) == 0) {
3309577SRod.Evans@Sun.COM Conv_time_buf_t buf;
3319577SRod.Evans@Sun.COM
3329577SRod.Evans@Sun.COM if (DBG_ISTTIME()) {
3339577SRod.Evans@Sun.COM (void) bufprint(&prf,
3349577SRod.Evans@Sun.COM conv_time(&DBG_TOTALTIME, &new, &buf));
3359577SRod.Evans@Sun.COM prf.pr_cur--;
3369577SRod.Evans@Sun.COM }
3379577SRod.Evans@Sun.COM if (DBG_ISDTIME()) {
3389577SRod.Evans@Sun.COM (void) bufprint(&prf,
3399577SRod.Evans@Sun.COM conv_time(&DBG_DELTATIME, &new, &buf));
3409577SRod.Evans@Sun.COM prf.pr_cur--;
3419577SRod.Evans@Sun.COM }
3429577SRod.Evans@Sun.COM DBG_DELTATIME = new;
3439577SRod.Evans@Sun.COM }
3449577SRod.Evans@Sun.COM }
3451618Srie if (rtld_flags & RT_FL_THREADS) {
3461618Srie (void) bufprint(&prf, MSG_ORIG(MSG_DBG_THREAD), rt_thr_self());
3471618Srie prf.pr_cur--;
3481618Srie }
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate * Format the message and print it.
3520Sstevel@tonic-gate */
3530Sstevel@tonic-gate va_start(args, format);
3540Sstevel@tonic-gate (void) doprf(format, args, &prf);
3550Sstevel@tonic-gate *(prf.pr_cur - 1) = '\n';
3560Sstevel@tonic-gate (void) dowrite(&prf);
3570Sstevel@tonic-gate va_end(args);
3580Sstevel@tonic-gate }
359