Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | /*
* Copyright (c) 2010, 2013-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Low-level debug output
*
* Low-level debugging output. Platform installs a character output routine at
* init time. If no routine is installed, a nop routine is called.
*/
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <stdarg.h>
#include <zephyr/toolchain.h>
#include <zephyr/linker/sections.h>
#include <zephyr/syscall_handler.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/cbprintf.h>
#include <sys/types.h>
/* Option present only when CONFIG_USERSPACE enabled. */
#ifndef CONFIG_PRINTK_BUFFER_SIZE
#define CONFIG_PRINTK_BUFFER_SIZE 0
#endif
#if defined(CONFIG_PRINTK_SYNC)
static struct k_spinlock lock;
#endif
#ifdef CONFIG_PRINTK
/**
* @brief Default character output routine that does nothing
* @param c Character to swallow
*
* Note this is defined as a weak symbol, allowing architecture code
* to override it where possible to enable very early logging.
*
* @return 0
*/
/* LCOV_EXCL_START */
__attribute__((weak)) int arch_printk_char_out(int c)
{
ARG_UNUSED(c);
/* do nothing */
return 0;
}
/* LCOV_EXCL_STOP */
int (*_char_out)(int c) = arch_printk_char_out;
/**
* @brief Install the character output routine for printk
*
* To be called by the platform's console driver at init time. Installs a
* routine that outputs one ASCII character at a time.
* @param fn putc routine to install
*/
void __printk_hook_install(int (*fn)(int c))
{
_char_out = fn;
}
/**
* @brief Get the current character output routine for printk
*
* To be called by any console driver that would like to save
* current hook - if any - for later re-installation.
*
* @return a function pointer or NULL if no hook is set
*/
void *__printk_get_hook(void)
{
return _char_out;
}
struct buf_out_context {
#ifdef CONFIG_PICOLIBC
FILE file;
#endif
unsigned int buf_count;
char buf[CONFIG_PRINTK_BUFFER_SIZE];
};
static void buf_flush(struct buf_out_context *ctx)
{
k_str_out(ctx->buf, ctx->buf_count);
ctx->buf_count = 0U;
}
static int buf_char_out(int c, void *ctx_p)
{
struct buf_out_context *ctx = ctx_p;
ctx->buf[ctx->buf_count++] = c;
if (ctx->buf_count == CONFIG_PRINTK_BUFFER_SIZE) {
buf_flush(ctx);
}
return c;
}
static int char_out(int c, void *ctx_p)
{
(void) ctx_p;
return _char_out(c);
}
void vprintk(const char *fmt, va_list ap)
{
if (IS_ENABLED(CONFIG_LOG_PRINTK)) {
z_log_vprintk(fmt, ap);
return;
}
if (k_is_user_context()) {
struct buf_out_context ctx = {
#ifdef CONFIG_PICOLIBC
.file = FDEV_SETUP_STREAM((int(*)(char, FILE *))buf_char_out,
NULL, NULL, _FDEV_SETUP_WRITE),
#else
0
#endif
};
#ifdef CONFIG_PICOLIBC
(void) vfprintf(&ctx.file, fmt, ap);
#else
cbvprintf(buf_char_out, &ctx, fmt, ap);
#endif
if (ctx.buf_count) {
buf_flush(&ctx);
}
} else {
#ifdef CONFIG_PRINTK_SYNC
k_spinlock_key_t key = k_spin_lock(&lock);
#endif
#ifdef CONFIG_PICOLIBC
FILE console = FDEV_SETUP_STREAM((int(*)(char, FILE *))char_out,
NULL, NULL, _FDEV_SETUP_WRITE);
(void) vfprintf(&console, fmt, ap);
#else
cbvprintf(char_out, NULL, fmt, ap);
#endif
#ifdef CONFIG_PRINTK_SYNC
k_spin_unlock(&lock, key);
#endif
}
}
void z_impl_k_str_out(char *c, size_t n)
{
size_t i;
#ifdef CONFIG_PRINTK_SYNC
k_spinlock_key_t key = k_spin_lock(&lock);
#endif
for (i = 0; i < n; i++) {
_char_out(c[i]);
}
#ifdef CONFIG_PRINTK_SYNC
k_spin_unlock(&lock, key);
#endif
}
#ifdef CONFIG_USERSPACE
static inline void z_vrfy_k_str_out(char *c, size_t n)
{
Z_OOPS(Z_SYSCALL_MEMORY_READ(c, n));
z_impl_k_str_out((char *)c, n);
}
#include <syscalls/k_str_out_mrsh.c>
#endif /* CONFIG_USERSPACE */
/**
* @brief Output a string
*
* Output a string on output installed by platform at init time. Some
* printf-like formatting is available.
*
* Available formatting:
* - %x/%X: outputs a number in hexadecimal format
* - %s: outputs a null-terminated string
* - %p: pointer, same as %x with a 0x prefix
* - %u: outputs a number in unsigned decimal format
* - %d/%i: outputs a number in signed decimal format
*
* Field width (with or without leading zeroes) is supported.
* Length attributes h, hh, l, ll and z are supported. However, integral
* values with %lld and %lli are only printed if they fit in a long
* otherwise 'ERR' is printed. Full 64-bit values may be printed with %llx.
*
* @param fmt formatted string to output
*/
void printk(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintk(fmt, ap);
va_end(ap);
}
#endif /* defined(CONFIG_PRINTK) */
#ifndef CONFIG_PICOLIBC
struct str_context {
char *str;
int max;
int count;
};
static int str_out(int c, struct str_context *ctx)
{
if (ctx->str == NULL || ctx->count >= ctx->max) {
ctx->count++;
return c;
}
if (ctx->count == ctx->max - 1) {
ctx->str[ctx->count++] = '\0';
} else {
ctx->str[ctx->count++] = c;
}
return c;
}
int snprintk(char *str, size_t size, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vsnprintk(str, size, fmt, ap);
va_end(ap);
return ret;
}
int vsnprintk(char *str, size_t size, const char *fmt, va_list ap)
{
struct str_context ctx = { str, size, 0 };
cbvprintf(str_out, &ctx, fmt, ap);
if (ctx.count < ctx.max) {
str[ctx.count] = '\0';
}
return ctx.count;
}
#endif
|