]>
Commit | Line | Data |
---|---|---|
b693d75f | 1 | /* printf implementation for the dynamic loader. |
6d7e8eda | 2 | Copyright (C) 1997-2023 Free Software Foundation, Inc. |
b693d75f FW |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <https://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <_itoa.h> | |
20 | #include <assert.h> | |
21 | #include <dl-writev.h> | |
22 | #include <ldsodefs.h> | |
23 | #include <limits.h> | |
24 | #include <stdarg.h> | |
25 | #include <stdint.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <sys/uio.h> | |
29 | #include <unistd.h> | |
40c112ed | 30 | #include <intprops.h> |
b693d75f FW |
31 | |
32 | /* Bare-bones printf implementation. This function only knows about | |
33 | the formats and flags needed and can handle only up to 64 stripes in | |
34 | the output. */ | |
35 | static void | |
36 | _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg) | |
37 | { | |
38 | # define NIOVMAX 64 | |
39 | struct iovec iov[NIOVMAX]; | |
40c112ed AZ |
40 | /* Maximum size for 'd', 'u', and 'x' including padding. */ |
41 | enum { IFMTSIZE = INT_STRLEN_BOUND(void *) }; | |
42 | char ifmtbuf[NIOVMAX][IFMTSIZE]; | |
b693d75f FW |
43 | int niov = 0; |
44 | pid_t pid = 0; | |
45 | char pidbuf[12]; | |
46 | ||
47 | while (*fmt != '\0') | |
48 | { | |
49 | const char *startp = fmt; | |
50 | ||
51 | if (tag_p > 0) | |
52 | { | |
53 | /* Generate the tag line once. It consists of the PID and a | |
54 | colon followed by a tab. */ | |
55 | if (pid == 0) | |
56 | { | |
57 | char *p; | |
58 | pid = __getpid (); | |
59 | assert (pid >= 0 && sizeof (pid_t) <= 4); | |
60 | p = _itoa (pid, &pidbuf[10], 10, 0); | |
61 | while (p > pidbuf) | |
62 | *--p = ' '; | |
63 | pidbuf[10] = ':'; | |
64 | pidbuf[11] = '\t'; | |
65 | } | |
66 | ||
67 | /* Append to the output. */ | |
68 | assert (niov < NIOVMAX); | |
69 | iov[niov].iov_len = 12; | |
70 | iov[niov++].iov_base = pidbuf; | |
71 | ||
72 | /* No more tags until we see the next newline. */ | |
73 | tag_p = -1; | |
74 | } | |
75 | ||
76 | /* Skip everything except % and \n (if tags are needed). */ | |
77 | while (*fmt != '\0' && *fmt != '%' && (! tag_p || *fmt != '\n')) | |
78 | ++fmt; | |
79 | ||
80 | /* Append constant string. */ | |
81 | assert (niov < NIOVMAX); | |
82 | if ((iov[niov].iov_len = fmt - startp) != 0) | |
83 | iov[niov++].iov_base = (char *) startp; | |
84 | ||
85 | if (*fmt == '%') | |
86 | { | |
87 | /* It is a format specifier. */ | |
88 | char fill = ' '; | |
89 | int width = -1; | |
90 | int prec = -1; | |
91 | #if LONG_MAX != INT_MAX | |
92 | int long_mod = 0; | |
93 | #endif | |
94 | ||
95 | /* Recognize zero-digit fill flag. */ | |
96 | if (*++fmt == '0') | |
97 | { | |
98 | fill = '0'; | |
99 | ++fmt; | |
100 | } | |
101 | ||
102 | /* See whether with comes from a parameter. Note that no other | |
103 | way to specify the width is implemented. */ | |
104 | if (*fmt == '*') | |
105 | { | |
106 | width = va_arg (arg, int); | |
40c112ed AZ |
107 | /* The maximum padding accepted is up to pointer size. */ |
108 | assert (width < IFMTSIZE); | |
b693d75f FW |
109 | ++fmt; |
110 | } | |
111 | ||
112 | /* Handle precision. */ | |
113 | if (*fmt == '.' && fmt[1] == '*') | |
114 | { | |
115 | prec = va_arg (arg, int); | |
116 | fmt += 2; | |
117 | } | |
118 | ||
119 | /* Recognize the l modifier. It is only important on some | |
120 | platforms where long and int have a different size. We | |
121 | can use the same code for size_t. */ | |
de477abc | 122 | if (*fmt == 'l' || *fmt == 'z') |
b693d75f FW |
123 | { |
124 | #if LONG_MAX != INT_MAX | |
125 | long_mod = 1; | |
126 | #endif | |
127 | ++fmt; | |
128 | } | |
129 | ||
130 | switch (*fmt) | |
131 | { | |
132 | /* Integer formatting. */ | |
133 | case 'd': | |
134 | case 'u': | |
135 | case 'x': | |
136 | { | |
137 | /* We have to make a difference if long and int have a | |
138 | different size. */ | |
139 | #if LONG_MAX != INT_MAX | |
140 | unsigned long int num = (long_mod | |
141 | ? va_arg (arg, unsigned long int) | |
142 | : va_arg (arg, unsigned int)); | |
143 | #else | |
144 | unsigned long int num = va_arg (arg, unsigned int); | |
145 | #endif | |
146 | bool negative = false; | |
147 | if (*fmt == 'd') | |
148 | { | |
149 | #if LONG_MAX != INT_MAX | |
150 | if (long_mod) | |
151 | { | |
152 | if ((long int) num < 0) | |
153 | negative = true; | |
154 | } | |
155 | else | |
156 | { | |
157 | if ((int) num < 0) | |
158 | { | |
159 | num = (unsigned int) num; | |
160 | negative = true; | |
161 | } | |
162 | } | |
163 | #else | |
164 | if ((int) num < 0) | |
165 | negative = true; | |
166 | #endif | |
167 | } | |
168 | ||
40c112ed | 169 | char *endp = &ifmtbuf[niov][IFMTSIZE]; |
b693d75f FW |
170 | char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0); |
171 | ||
172 | /* Pad to the width the user specified. */ | |
173 | if (width != -1) | |
174 | while (endp - cp < width) | |
175 | *--cp = fill; | |
176 | ||
177 | if (negative) | |
178 | *--cp = '-'; | |
179 | ||
180 | iov[niov].iov_base = cp; | |
181 | iov[niov].iov_len = endp - cp; | |
182 | ++niov; | |
183 | } | |
184 | break; | |
185 | ||
186 | case 's': | |
187 | /* Get the string argument. */ | |
188 | iov[niov].iov_base = va_arg (arg, char *); | |
189 | iov[niov].iov_len = strlen (iov[niov].iov_base); | |
190 | if (prec != -1) | |
191 | iov[niov].iov_len = MIN ((size_t) prec, iov[niov].iov_len); | |
192 | ++niov; | |
193 | break; | |
194 | ||
195 | case '%': | |
196 | iov[niov].iov_base = (void *) fmt; | |
197 | iov[niov].iov_len = 1; | |
198 | ++niov; | |
199 | break; | |
200 | ||
201 | default: | |
202 | assert (! "invalid format specifier"); | |
203 | } | |
204 | ++fmt; | |
205 | } | |
206 | else if (*fmt == '\n') | |
207 | { | |
208 | /* See whether we have to print a single newline character. */ | |
209 | if (fmt == startp) | |
210 | { | |
211 | iov[niov].iov_base = (char *) startp; | |
212 | iov[niov++].iov_len = 1; | |
213 | } | |
214 | else | |
215 | /* No, just add it to the rest of the string. */ | |
216 | ++iov[niov - 1].iov_len; | |
217 | ||
218 | /* Next line, print a tag again. */ | |
219 | tag_p = 1; | |
220 | ++fmt; | |
221 | } | |
222 | } | |
223 | ||
224 | /* Finally write the result. */ | |
225 | _dl_writev (fd, iov, niov); | |
226 | } | |
227 | ||
228 | ||
229 | /* Write to debug file. */ | |
230 | void | |
231 | _dl_debug_printf (const char *fmt, ...) | |
232 | { | |
233 | va_list arg; | |
234 | ||
235 | va_start (arg, fmt); | |
236 | _dl_debug_vdprintf (GLRO(dl_debug_fd), 1, fmt, arg); | |
237 | va_end (arg); | |
238 | } | |
239 | ||
240 | ||
241 | /* Write to debug file but don't start with a tag. */ | |
242 | void | |
243 | _dl_debug_printf_c (const char *fmt, ...) | |
244 | { | |
245 | va_list arg; | |
246 | ||
247 | va_start (arg, fmt); | |
248 | _dl_debug_vdprintf (GLRO(dl_debug_fd), -1, fmt, arg); | |
249 | va_end (arg); | |
250 | } | |
251 | ||
252 | ||
253 | /* Write the given file descriptor. */ | |
254 | void | |
255 | _dl_dprintf (int fd, const char *fmt, ...) | |
256 | { | |
257 | va_list arg; | |
258 | ||
259 | va_start (arg, fmt); | |
260 | _dl_debug_vdprintf (fd, 0, fmt, arg); | |
261 | va_end (arg); | |
262 | } | |
263 | ||
264 | void | |
265 | _dl_printf (const char *fmt, ...) | |
266 | { | |
267 | va_list arg; | |
268 | ||
269 | va_start (arg, fmt); | |
270 | _dl_debug_vdprintf (STDOUT_FILENO, 0, fmt, arg); | |
271 | va_end (arg); | |
272 | } | |
273 | ||
274 | void | |
275 | _dl_error_printf (const char *fmt, ...) | |
276 | { | |
277 | va_list arg; | |
278 | ||
279 | va_start (arg, fmt); | |
280 | _dl_debug_vdprintf (STDERR_FILENO, 0, fmt, arg); | |
281 | va_end (arg); | |
282 | } | |
283 | ||
284 | void | |
285 | _dl_fatal_printf (const char *fmt, ...) | |
286 | { | |
287 | va_list arg; | |
288 | ||
289 | va_start (arg, fmt); | |
290 | _dl_debug_vdprintf (STDERR_FILENO, 0, fmt, arg); | |
291 | va_end (arg); | |
292 | _exit (127); | |
293 | } | |
294 | rtld_hidden_def (_dl_fatal_printf) |