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 (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
37 * dbg_setup() can be called a number of times. The typical use through
38 * LD_OPTIONS, results in dbg_setup() being called as the first argument to
39 * ld(1). It's also possible to pass debugging tokens through the compiler,
40 * for example -Wl,-Dlibs -Wl-Ddetail, in which case multiple dbg_setup()
43 * A distinction is also made between diagnostics being requested before any
44 * other ld(1) options are read, or whether the debugging options occur
45 * between other options on the command line. In the latter case, the
46 * debugging options can be used to isolate diagnostics around one or more
47 * input files. The "phase" argument allows us to select which phase of
48 * dbg_setup() processing we should isolate ourselves to.
50 * dbg_print() can require the output filename for use in the diagnostics
51 * created. Save the address of the output filename pointer for this use.
53 static const char **Name
= NULL
;
56 /* Debug file output state */
58 FILE *fptr
; /* File to send debug output */
59 int close_needed
; /* True if explicitly opened stream */
64 * If there is an explicitly opened debug file, close it and reset the state.
69 if (dbg_ofile
.close_needed
) {
70 (void) fclose(dbg_ofile
.fptr
);
71 dbg_ofile
.close_needed
= 0;
72 dbg_ofile
.fptr
= stderr
;
77 * Process debug tokens. Returns True (1) on success, and False (0)
81 dbg_setup(Ofl_desc
*ofl
, const char *options
, int phase
)
87 else if (Phase
!= phase
)
90 Name
= &ofl
->ofl_name
;
93 * Call the debugging setup routine to initialize the mask and
94 * debug function array.
96 if (Dbg_setup(DBG_CALLER_LD
, options
, dbg_desc
, &ofile
) == 0)
100 * If output= token was used, close the old file if necessary
101 * and open a new one if the file name is not NULL.
105 if (*ofile
!= '\0') {
106 FILE *fptr
= fopen(ofile
, MSG_ORIG(MSG_DBG_FOPEN_MODE
));
110 ld_eprintf(ofl
, ERR_FATAL
,
111 MSG_INTL(MSG_SYS_OPEN
), ofile
,
115 dbg_ofile
.fptr
= fptr
;
116 dbg_ofile
.close_needed
= 1;
122 * Now that the output file is established, identify the linker
123 * package, and generate help output if the user specified the
127 if (dbg_desc
->d_extra
& DBG_E_HELP
)
135 dbg_print(Lm_list
*lml
, const char *format
, ...)
137 static char *prestr
= NULL
;
140 if (dbg_ofile
.fptr
== NULL
)
141 dbg_ofile
.fptr
= stderr
;
144 * Knock off any newline indicator to signify that a diagnostic has
147 dbg_desc
->d_extra
&= ~DBG_E_STDNL
;
151 * If the debugging options have requested each diagnostic line
152 * be prepended by a name create a prefix string.
154 if ((prestr
== NULL
) && *Name
) {
155 const char *name
, *cls
;
159 * Select the fullname or basename of the output file
166 strrchr(*Name
, '/')) == NULL
)
172 strlen(MSG_INTL(MSG_DBG_NAME_FMT
)) + 1;
175 * Add the output file class if required.
179 len
+= MSG_DBG_CLS64_FMT_SIZE
;
180 cls
= MSG_ORIG(MSG_DBG_CLS64_FMT
);
182 len
+= MSG_DBG_CLS32_FMT_SIZE
;
183 cls
= MSG_ORIG(MSG_DBG_CLS32_FMT
);
188 * Allocate a string to build the prefix.
190 if ((prestr
= libld_malloc(len
)) == NULL
)
191 prestr
= (char *)MSG_INTL(MSG_DBG_DFLT_FMT
);
193 (void) snprintf(prestr
, len
,
194 MSG_INTL(MSG_DBG_NAME_FMT
), name
);
196 (void) strcat(prestr
, cls
);
199 (void) fputs(prestr
? prestr
: MSG_INTL(MSG_DBG_AOUT_FMT
),
202 (void) fputs(MSG_INTL(MSG_DBG_DFLT_FMT
), dbg_ofile
.fptr
);
208 if (gettimeofday(&new, NULL
) == 0) {
210 (void) fputs(conv_time(&DBG_TOTALTIME
, &new,
213 (void) fputs(conv_time(&DBG_DELTATIME
, &new,
220 va_start(args
, format
);
221 (void) vfprintf(dbg_ofile
.fptr
, format
, args
);
222 (void) fprintf(dbg_ofile
.fptr
, MSG_ORIG(MSG_STR_NL
));