Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2010, 2013-2014 Wind River Systems, Inc.
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : /**
8 : : * @file
9 : : * @brief Low-level debug output
10 : : *
11 : : * Low-level debugging output. Platform installs a character output routine at
12 : : * init time. If no routine is installed, a nop routine is called.
13 : : */
14 : :
15 : : #include <zephyr/kernel.h>
16 : : #include <zephyr/sys/printk.h>
17 : : #include <zephyr/sys/printk-hooks.h>
18 : : #include <stdarg.h>
19 : : #include <zephyr/toolchain.h>
20 : : #include <zephyr/linker/sections.h>
21 : : #include <zephyr/internal/syscall_handler.h>
22 : : #include <zephyr/logging/log.h>
23 : : #include <zephyr/sys/cbprintf.h>
24 : : #include <zephyr/llext/symbol.h>
25 : :
26 : : /* Option present only when CONFIG_USERSPACE enabled. */
27 : : #ifndef CONFIG_PRINTK_BUFFER_SIZE
28 : : #define CONFIG_PRINTK_BUFFER_SIZE 0
29 : : #endif
30 : :
31 : : #if defined(CONFIG_PRINTK_SYNC)
32 : : static struct k_spinlock lock;
33 : : #endif
34 : :
35 : : #ifdef CONFIG_PRINTK
36 : : /**
37 : : * @brief Default character output routine that does nothing
38 : : * @param c Character to swallow
39 : : *
40 : : * Note this is defined as a weak symbol, allowing architecture code
41 : : * to override it where possible to enable very early logging.
42 : : *
43 : : * @return 0
44 : : */
45 : : /* LCOV_EXCL_START */
46 : : __attribute__((weak)) int arch_printk_char_out(int c)
47 : : {
48 : : ARG_UNUSED(c);
49 : :
50 : : /* do nothing */
51 : : return 0;
52 : : }
53 : : /* LCOV_EXCL_STOP */
54 : :
55 : : static printk_hook_fn_t _char_out = arch_printk_char_out;
56 : :
57 : 1 : void __printk_hook_install(printk_hook_fn_t fn)
58 : : {
59 : 1 : _char_out = fn;
60 : 1 : }
61 : :
62 : 0 : printk_hook_fn_t __printk_get_hook(void)
63 : : {
64 : 0 : return _char_out;
65 : : }
66 : :
67 : : #if defined(CONFIG_PRINTK_SYNC)
68 : : /*
69 : : * Latched by printk_panic(). Plain bool, not an atomic: one way only, and
70 : : * a reader that misses the update just takes the lock as before.
71 : : */
72 : : static bool panic_mode;
73 : : #endif
74 : :
75 : 0 : void printk_panic(void)
76 : : {
77 : : #if defined(CONFIG_PRINTK_SYNC)
78 : : panic_mode = true;
79 : : #endif
80 : 0 : }
81 : :
82 : : struct buf_out_context {
83 : : #ifdef CONFIG_PICOLIBC
84 : : FILE file;
85 : : #endif
86 : : unsigned int buf_count;
87 : : char buf[CONFIG_PRINTK_BUFFER_SIZE];
88 : : };
89 : :
90 : 0 : static void buf_flush(struct buf_out_context *ctx)
91 : : {
92 : 0 : k_str_out(ctx->buf, ctx->buf_count);
93 : 0 : ctx->buf_count = 0U;
94 : 0 : }
95 : :
96 : 0 : static int buf_char_out(int c, void *ctx_p)
97 : : {
98 : 0 : struct buf_out_context *ctx = ctx_p;
99 : :
100 : 0 : ctx->buf[ctx->buf_count] = c;
101 : 0 : ++ctx->buf_count;
102 [ # # ]: 0 : if (ctx->buf_count == CONFIG_PRINTK_BUFFER_SIZE) {
103 : 0 : buf_flush(ctx);
104 : : }
105 : :
106 : 0 : return c;
107 : : }
108 : :
109 : 0 : static int char_out(int c, void *ctx_p)
110 : : {
111 : 0 : ARG_UNUSED(ctx_p);
112 : 0 : return _char_out(c);
113 : : }
114 : :
115 : : /*
116 : : * Format straight to the platform's character output hook, through a
117 : : * private unbuffered stream. Never stdout, never the logging subsystem,
118 : : * so no shared buffer and no lock of our own.
119 : : */
120 : 0 : static void vprintk_core(const char *fmt, va_list ap)
121 : : {
122 : : #ifdef CONFIG_PICOLIBC
123 : : FILE console = FDEV_SETUP_STREAM((int(*)(char, FILE *))char_out,
124 : : NULL, NULL, _FDEV_SETUP_WRITE);
125 : : (void) vfprintf(&console, fmt, ap);
126 : : #else
127 : 0 : cbvprintf(char_out, NULL, fmt, ap);
128 : : #endif
129 : 0 : }
130 : :
131 : : /*
132 : : * Format from user mode. The character output hook lives in kernel memory
133 : : * and cannot be reached from a user thread, so buffer here and hand the
134 : : * result to the kernel through k_str_out().
135 : : */
136 : 0 : static void vprintk_user(const char *fmt, va_list ap)
137 : : {
138 : 0 : struct buf_out_context ctx = {
139 : : #ifdef CONFIG_PICOLIBC
140 : : .file = FDEV_SETUP_STREAM((int(*)(char, FILE *))buf_char_out,
141 : : NULL, NULL, _FDEV_SETUP_WRITE),
142 : : #else
143 : : 0
144 : : #endif
145 : : };
146 : :
147 : : #ifdef CONFIG_PICOLIBC
148 : : (void) vfprintf(&ctx.file, fmt, ap);
149 : : #else
150 : 0 : cbvprintf(buf_char_out, &ctx, fmt, ap);
151 : : #endif
152 [ # # ]: 0 : if (ctx.buf_count) {
153 : 0 : buf_flush(&ctx);
154 : : }
155 : 0 : }
156 : :
157 : 0 : void vprintk_unlocked(const char *fmt, va_list ap)
158 : : {
159 [ # # ]: 0 : if (k_is_user_context()) {
160 : 0 : vprintk_user(fmt, ap);
161 : 0 : return;
162 : : }
163 : :
164 : 0 : vprintk_core(fmt, ap);
165 : : }
166 : : EXPORT_SYMBOL(vprintk_unlocked);
167 : :
168 : 0 : void printk_unlocked(const char *fmt, ...)
169 : : {
170 : 0 : va_list ap;
171 : :
172 : 0 : va_start(ap, fmt);
173 : :
174 : 0 : vprintk_unlocked(fmt, ap);
175 : :
176 : 0 : va_end(ap);
177 : 0 : }
178 : : EXPORT_SYMBOL(printk_unlocked);
179 : :
180 : 209 : void vprintk(const char *fmt, va_list ap)
181 : : {
182 : 209 : if (IS_ENABLED(CONFIG_LOG_PRINTK)) {
183 : 209 : z_log_vprintk(fmt, ap);
184 : 209 : return;
185 : : }
186 : :
187 : : if (k_is_user_context()) {
188 : : vprintk_user(fmt, ap);
189 : : } else {
190 : : compiler_barrier();
191 : : #ifdef CONFIG_PRINTK_SYNC
192 : : if (!panic_mode) {
193 : : k_spinlock_key_t key = k_spin_lock(&lock);
194 : :
195 : : vprintk_core(fmt, ap);
196 : : k_spin_unlock(&lock, key);
197 : : } else {
198 : : vprintk_core(fmt, ap);
199 : : }
200 : : #else
201 : : vprintk_core(fmt, ap);
202 : : #endif
203 : : }
204 : : }
205 : : EXPORT_SYMBOL(vprintk);
206 : :
207 : 0 : static void str_out_chars(const char *c, size_t n)
208 : : {
209 [ # # ]: 0 : for (size_t i = 0; i < n; i++) {
210 : 0 : _char_out(c[i]);
211 : : }
212 : 0 : }
213 : :
214 : 0 : void z_impl_k_str_out(char *c, size_t n)
215 : : {
216 : : #ifdef CONFIG_PRINTK_SYNC
217 : : if (!panic_mode) {
218 : : k_spinlock_key_t key = k_spin_lock(&lock);
219 : :
220 : : str_out_chars(c, n);
221 : : k_spin_unlock(&lock, key);
222 : : } else {
223 : : str_out_chars(c, n);
224 : : }
225 : : #else
226 : 0 : str_out_chars(c, n);
227 : : #endif
228 : 0 : }
229 : :
230 : : #ifdef CONFIG_USERSPACE
231 : : static inline void z_vrfy_k_str_out(char *c, size_t n)
232 : : {
233 : : K_OOPS(K_SYSCALL_MEMORY_READ(c, n));
234 : : z_impl_k_str_out((char *)c, n);
235 : : }
236 : : #include <zephyr/syscalls/k_str_out_mrsh.c>
237 : : #endif /* CONFIG_USERSPACE */
238 : :
239 : : /**
240 : : * @brief Output a string
241 : : *
242 : : * Output a string on output installed by platform at init time. Some
243 : : * printf-like formatting is available.
244 : : *
245 : : * Available formatting:
246 : : * - %x/%X: outputs a number in hexadecimal format
247 : : * - %s: outputs a null-terminated string
248 : : * - %p: pointer, same as %x with a 0x prefix
249 : : * - %u: outputs a number in unsigned decimal format
250 : : * - %d/%i: outputs a number in signed decimal format
251 : : *
252 : : * Field width (with or without leading zeroes) is supported.
253 : : * Length attributes h, hh, l, ll and z are supported. However, integral
254 : : * values with %lld and %lli are only printed if they fit in a long
255 : : * otherwise 'ERR' is printed. Full 64-bit values may be printed with %llx.
256 : : *
257 : : * @param fmt formatted string to output
258 : : */
259 : :
260 : : #ifndef CONFIG_LOG_PRINTK_STATIC
261 : 209 : void printk(const char *fmt, ...)
262 : : {
263 : 209 : va_list ap;
264 : :
265 : 209 : va_start(ap, fmt);
266 : :
267 : 209 : vprintk(fmt, ap);
268 : :
269 : 209 : va_end(ap);
270 : 209 : }
271 : : EXPORT_SYMBOL(printk);
272 : : #endif
273 : :
274 : : #endif /* defined(CONFIG_PRINTK) */
275 : :
276 : : #ifndef CONFIG_PICOLIBC
277 : :
278 : : struct str_context {
279 : : char *str;
280 : : int max;
281 : : int count;
282 : : };
283 : :
284 : 0 : static int str_out(int c, void *context)
285 : : {
286 : 0 : struct str_context *ctx = context;
287 [ # # # # ]: 0 : if ((ctx->str == NULL) || (ctx->count >= ctx->max)) {
288 : 0 : ++ctx->count;
289 : 0 : return c;
290 : : }
291 : :
292 [ # # ]: 0 : if (ctx->count == (ctx->max - 1)) {
293 : 0 : ctx->str[ctx->count] = '\0';
294 : : } else {
295 : 0 : ctx->str[ctx->count] = c;
296 : : }
297 : 0 : ++ctx->count;
298 : :
299 : 0 : return c;
300 : : }
301 : :
302 : 0 : int snprintk(char *str, size_t size, const char *fmt, ...)
303 : : {
304 : 0 : va_list ap;
305 : 0 : int ret;
306 : :
307 : 0 : va_start(ap, fmt);
308 : 0 : ret = vsnprintk(str, size, fmt, ap);
309 : 0 : va_end(ap);
310 : :
311 : 0 : return ret;
312 : : }
313 : :
314 : 0 : int vsnprintk(char *str, size_t size, const char *fmt, va_list ap)
315 : : {
316 : 0 : struct str_context ctx = { str, size, 0 };
317 : :
318 : 0 : cbvprintf(str_out, &ctx, fmt, ap);
319 : :
320 [ # # ]: 0 : if (ctx.count < ctx.max) {
321 : 0 : str[ctx.count] = '\0';
322 : : }
323 : :
324 : 0 : return ctx.count;
325 : : }
326 : :
327 : : #endif
|