2015-04-11 01:44:37 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, 2013-2014 Wind River Systems, Inc.
|
|
|
|
*
|
2017-01-19 02:01:01 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-11 01:44:37 +02:00
|
|
|
*/
|
|
|
|
|
2015-07-16 20:07:57 +02:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Low-level debug output
|
|
|
|
*
|
2015-07-24 22:55:19 +02:00
|
|
|
* Low-level debugging output. Platform installs a character output routine at
|
2015-07-16 20:07:57 +02:00
|
|
|
* init time. If no routine is installed, a nop routine is called.
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2017-10-11 01:01:49 +02:00
|
|
|
#include <kernel.h>
|
2019-06-26 16:33:49 +02:00
|
|
|
#include <sys/printk.h>
|
2015-04-11 01:44:37 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <toolchain.h>
|
2017-06-17 17:30:47 +02:00
|
|
|
#include <linker/sections.h>
|
2017-10-11 01:01:49 +02:00
|
|
|
#include <syscall_handler.h>
|
2018-05-16 08:50:33 +02:00
|
|
|
#include <logging/log.h>
|
2019-06-14 04:44:07 +02:00
|
|
|
#include <sys/types.h>
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2016-11-28 08:55:19 +01:00
|
|
|
typedef int (*out_func_t)(int c, void *ctx);
|
|
|
|
|
misc: Support left-justification in printk()
This is supported by printf(), and is an important formatting feature
to print out some complex, nicely-formatted information. This is
accomplished by using a negative padding in the formatting string.
The following code:
printk("none: |%u| |%x|\n", 12345, 12345);
printk("zero_before: |%08u| |%08x|\n", 12345, 12345);
printk("space_before: |%8u| |%8x|\n", 12345, 12345);
printk("space_after: |%-8u| |%-8x|\n", 12345, 12345);
Will produce the following output:
none: |12345| |3039|
zero_before: |00012345| |0000000000003039|
space_before: | 12345| | 3039|
space_after: |12345 | |3039 |
Change-Id: I9c2d85a1790087d53b52b7713854adaf99282f09
Jira: ZEP-1599
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-04-14 21:47:58 +02:00
|
|
|
enum pad_type {
|
|
|
|
PAD_NONE,
|
|
|
|
PAD_ZERO_BEFORE,
|
|
|
|
PAD_SPACE_BEFORE,
|
|
|
|
PAD_SPACE_AFTER,
|
|
|
|
};
|
|
|
|
|
2020-06-23 00:54:23 +02:00
|
|
|
#ifdef CONFIG_PRINTK64
|
|
|
|
typedef uint64_t printk_val_t;
|
|
|
|
#else
|
|
|
|
typedef uint32_t printk_val_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Maximum number of digits in a printed decimal value (hex is always
|
|
|
|
* less, obviously). Funny formula produces 10 max digits for 32 bit,
|
|
|
|
* 21 for 64.
|
|
|
|
*/
|
|
|
|
#define DIGITS_BUFLEN (11 * (sizeof(printk_val_t) / 4) - 1)
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2020-06-03 00:07:28 +02:00
|
|
|
#ifdef CONFIG_PRINTK_SYNC
|
|
|
|
static struct k_spinlock lock;
|
|
|
|
#endif
|
|
|
|
|
2020-01-02 20:57:43 +01:00
|
|
|
#ifdef CONFIG_PRINTK
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Default character output routine that does nothing
|
2015-07-16 20:07:57 +02:00
|
|
|
* @param c Character to swallow
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2018-08-19 19:58:06 +02:00
|
|
|
* Note this is defined as a weak symbol, allowing architecture code
|
|
|
|
* to override it where possible to enable very early logging.
|
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return 0
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2019-06-29 00:48:54 +02:00
|
|
|
/* LCOV_EXCL_START */
|
2019-11-07 21:43:29 +01:00
|
|
|
__attribute__((weak)) int arch_printk_char_out(int c)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
|
|
|
ARG_UNUSED(c);
|
|
|
|
|
|
|
|
/* do nothing */
|
|
|
|
return 0;
|
|
|
|
}
|
2019-06-29 00:48:54 +02:00
|
|
|
/* LCOV_EXCL_STOP */
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2019-11-07 21:43:29 +01:00
|
|
|
int (*_char_out)(int) = arch_printk_char_out;
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Install the character output routine for printk
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2015-07-24 22:55:19 +02:00
|
|
|
* To be called by the platform's console driver at init time. Installs a
|
|
|
|
* routine that outputs one ASCII character at a time.
|
2015-07-16 20:07:57 +02:00
|
|
|
* @param fn putc routine to install
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return N/A
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-07-16 20:07:57 +02:00
|
|
|
void __printk_hook_install(int (*fn)(int))
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
|
|
|
_char_out = fn;
|
|
|
|
}
|
|
|
|
|
2017-01-19 15:37:20 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
2020-01-02 20:57:43 +01:00
|
|
|
#endif /* CONFIG_PRINTK */
|
2017-01-19 15:37:20 +01:00
|
|
|
|
2020-06-23 00:54:23 +02:00
|
|
|
static void print_digits(out_func_t out, void *ctx, printk_val_t num, int base,
|
|
|
|
bool pad_before, char pad_char, int min_width)
|
|
|
|
{
|
|
|
|
char buf[DIGITS_BUFLEN];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Print it backwards into the end of the buffer, low digits first */
|
|
|
|
for (i = DIGITS_BUFLEN - 1; num != 0; i--) {
|
|
|
|
buf[i] = "0123456789abcdef"[num % base];
|
|
|
|
num /= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == DIGITS_BUFLEN - 1) {
|
|
|
|
buf[i] = '0';
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pad = MAX(min_width - (DIGITS_BUFLEN - i), 0);
|
|
|
|
|
|
|
|
for (/**/; pad > 0 && pad_before; pad--) {
|
|
|
|
out(pad_char, ctx);
|
|
|
|
}
|
|
|
|
for (/**/; i < DIGITS_BUFLEN; i++) {
|
|
|
|
out(buf[i], ctx);
|
|
|
|
}
|
|
|
|
for (/**/; pad > 0; pad--) {
|
|
|
|
out(pad_char, ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_hex(out_func_t out, void *ctx, printk_val_t num,
|
|
|
|
enum pad_type padding, int min_width)
|
|
|
|
{
|
|
|
|
print_digits(out, ctx, num, 16, padding != PAD_SPACE_AFTER,
|
|
|
|
padding == PAD_ZERO_BEFORE ? '0' : ' ', min_width);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_dec(out_func_t out, void *ctx, printk_val_t num,
|
|
|
|
enum pad_type padding, int min_width)
|
2019-01-17 22:26:51 +01:00
|
|
|
{
|
2020-06-23 00:54:23 +02:00
|
|
|
print_digits(out, ctx, num, 10, padding != PAD_SPACE_AFTER,
|
|
|
|
padding == PAD_ZERO_BEFORE ? '0' : ' ', min_width);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ok64(out_func_t out, void *ctx, long long val)
|
|
|
|
{
|
|
|
|
if (sizeof(printk_val_t) < 8 && val != (long) val) {
|
|
|
|
out('E', ctx);
|
|
|
|
out('R', ctx);
|
|
|
|
out('R', ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool negative(printk_val_t val)
|
|
|
|
{
|
|
|
|
const printk_val_t hibit = ~(((printk_val_t) ~1) >> 1);
|
|
|
|
|
|
|
|
return (val & hibit) != 0;
|
2019-01-17 22:26:51 +01:00
|
|
|
}
|
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Printk internals
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
|
|
|
* See printk() for description.
|
2015-07-16 20:07:57 +02:00
|
|
|
* @param fmt Format string
|
|
|
|
* @param ap Variable parameters
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return N/A
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2019-03-15 02:42:51 +01:00
|
|
|
void z_vprintk(out_func_t out, void *ctx, const char *fmt, va_list ap)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
|
|
|
int might_format = 0; /* 1 if encountered a '%' */
|
misc: Support left-justification in printk()
This is supported by printf(), and is an important formatting feature
to print out some complex, nicely-formatted information. This is
accomplished by using a negative padding in the formatting string.
The following code:
printk("none: |%u| |%x|\n", 12345, 12345);
printk("zero_before: |%08u| |%08x|\n", 12345, 12345);
printk("space_before: |%8u| |%8x|\n", 12345, 12345);
printk("space_after: |%-8u| |%-8x|\n", 12345, 12345);
Will produce the following output:
none: |12345| |3039|
zero_before: |00012345| |0000000000003039|
space_before: | 12345| | 3039|
space_after: |12345 | |3039 |
Change-Id: I9c2d85a1790087d53b52b7713854adaf99282f09
Jira: ZEP-1599
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-04-14 21:47:58 +02:00
|
|
|
enum pad_type padding = PAD_NONE;
|
printk: Add basic support for width modifier and zero padding
This adds basic support for width modifier when printing integers.
Supported specifiers are u,i,d,x,X. Width '*' is not supported.
Flag '0' for left-pading number with zero is also supported.
examples:
printk("0x%x 0x%02x 0x%04x 0x%08x\n", 1, 1, 1, 1);
0x1 0x01 0x0001 0x00000001
printk("0x%x 0x%2x 0x%4x 0x%8x\n", 1, 1, 1, 1);
0x1 0x 1 0x 1 0x 1
This should make printk usable for pretty printing u8 and u16 integers.
Change-Id: I58fa869e9c295a052f97fbf052291ef4d132811e
Signed-off-by: Szymon Janc <szymon.janc@codecoup.pl>
2016-11-20 22:06:37 +01:00
|
|
|
int min_width = -1;
|
2019-06-14 04:44:07 +02:00
|
|
|
char length_mod = 0;
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
/* fmt has already been adjusted if needed */
|
|
|
|
|
|
|
|
while (*fmt) {
|
|
|
|
if (!might_format) {
|
|
|
|
if (*fmt != '%') {
|
2016-11-28 08:55:19 +01:00
|
|
|
out((int)*fmt, ctx);
|
2015-04-11 01:44:37 +02:00
|
|
|
} else {
|
|
|
|
might_format = 1;
|
printk: Add basic support for width modifier and zero padding
This adds basic support for width modifier when printing integers.
Supported specifiers are u,i,d,x,X. Width '*' is not supported.
Flag '0' for left-pading number with zero is also supported.
examples:
printk("0x%x 0x%02x 0x%04x 0x%08x\n", 1, 1, 1, 1);
0x1 0x01 0x0001 0x00000001
printk("0x%x 0x%2x 0x%4x 0x%8x\n", 1, 1, 1, 1);
0x1 0x 1 0x 1 0x 1
This should make printk usable for pretty printing u8 and u16 integers.
Change-Id: I58fa869e9c295a052f97fbf052291ef4d132811e
Signed-off-by: Szymon Janc <szymon.janc@codecoup.pl>
2016-11-20 22:06:37 +01:00
|
|
|
min_width = -1;
|
misc: Support left-justification in printk()
This is supported by printf(), and is an important formatting feature
to print out some complex, nicely-formatted information. This is
accomplished by using a negative padding in the formatting string.
The following code:
printk("none: |%u| |%x|\n", 12345, 12345);
printk("zero_before: |%08u| |%08x|\n", 12345, 12345);
printk("space_before: |%8u| |%8x|\n", 12345, 12345);
printk("space_after: |%-8u| |%-8x|\n", 12345, 12345);
Will produce the following output:
none: |12345| |3039|
zero_before: |00012345| |0000000000003039|
space_before: | 12345| | 3039|
space_after: |12345 | |3039 |
Change-Id: I9c2d85a1790087d53b52b7713854adaf99282f09
Jira: ZEP-1599
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-04-14 21:47:58 +02:00
|
|
|
padding = PAD_NONE;
|
2019-06-14 04:44:07 +02:00
|
|
|
length_mod = 0;
|
2015-04-11 01:44:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-21 14:42:01 +02:00
|
|
|
switch (*fmt) {
|
misc: Support left-justification in printk()
This is supported by printf(), and is an important formatting feature
to print out some complex, nicely-formatted information. This is
accomplished by using a negative padding in the formatting string.
The following code:
printk("none: |%u| |%x|\n", 12345, 12345);
printk("zero_before: |%08u| |%08x|\n", 12345, 12345);
printk("space_before: |%8u| |%8x|\n", 12345, 12345);
printk("space_after: |%-8u| |%-8x|\n", 12345, 12345);
Will produce the following output:
none: |12345| |3039|
zero_before: |00012345| |0000000000003039|
space_before: | 12345| | 3039|
space_after: |12345 | |3039 |
Change-Id: I9c2d85a1790087d53b52b7713854adaf99282f09
Jira: ZEP-1599
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-04-14 21:47:58 +02:00
|
|
|
case '-':
|
|
|
|
padding = PAD_SPACE_AFTER;
|
|
|
|
goto still_might_format;
|
printk: Add basic support for width modifier and zero padding
This adds basic support for width modifier when printing integers.
Supported specifiers are u,i,d,x,X. Width '*' is not supported.
Flag '0' for left-pading number with zero is also supported.
examples:
printk("0x%x 0x%02x 0x%04x 0x%08x\n", 1, 1, 1, 1);
0x1 0x01 0x0001 0x00000001
printk("0x%x 0x%2x 0x%4x 0x%8x\n", 1, 1, 1, 1);
0x1 0x 1 0x 1 0x 1
This should make printk usable for pretty printing u8 and u16 integers.
Change-Id: I58fa869e9c295a052f97fbf052291ef4d132811e
Signed-off-by: Szymon Janc <szymon.janc@codecoup.pl>
2016-11-20 22:06:37 +01:00
|
|
|
case '0':
|
misc: Support left-justification in printk()
This is supported by printf(), and is an important formatting feature
to print out some complex, nicely-formatted information. This is
accomplished by using a negative padding in the formatting string.
The following code:
printk("none: |%u| |%x|\n", 12345, 12345);
printk("zero_before: |%08u| |%08x|\n", 12345, 12345);
printk("space_before: |%8u| |%8x|\n", 12345, 12345);
printk("space_after: |%-8u| |%-8x|\n", 12345, 12345);
Will produce the following output:
none: |12345| |3039|
zero_before: |00012345| |0000000000003039|
space_before: | 12345| | 3039|
space_after: |12345 | |3039 |
Change-Id: I9c2d85a1790087d53b52b7713854adaf99282f09
Jira: ZEP-1599
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-04-14 21:47:58 +02:00
|
|
|
if (min_width < 0 && padding == PAD_NONE) {
|
|
|
|
padding = PAD_ZERO_BEFORE;
|
printk: Add basic support for width modifier and zero padding
This adds basic support for width modifier when printing integers.
Supported specifiers are u,i,d,x,X. Width '*' is not supported.
Flag '0' for left-pading number with zero is also supported.
examples:
printk("0x%x 0x%02x 0x%04x 0x%08x\n", 1, 1, 1, 1);
0x1 0x01 0x0001 0x00000001
printk("0x%x 0x%2x 0x%4x 0x%8x\n", 1, 1, 1, 1);
0x1 0x 1 0x 1 0x 1
This should make printk usable for pretty printing u8 and u16 integers.
Change-Id: I58fa869e9c295a052f97fbf052291ef4d132811e
Signed-off-by: Szymon Janc <szymon.janc@codecoup.pl>
2016-11-20 22:06:37 +01:00
|
|
|
goto still_might_format;
|
|
|
|
}
|
|
|
|
/* Fall through */
|
2019-03-05 18:13:30 +01:00
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
/* Fall through */
|
|
|
|
case '9':
|
printk: Add basic support for width modifier and zero padding
This adds basic support for width modifier when printing integers.
Supported specifiers are u,i,d,x,X. Width '*' is not supported.
Flag '0' for left-pading number with zero is also supported.
examples:
printk("0x%x 0x%02x 0x%04x 0x%08x\n", 1, 1, 1, 1);
0x1 0x01 0x0001 0x00000001
printk("0x%x 0x%2x 0x%4x 0x%8x\n", 1, 1, 1, 1);
0x1 0x 1 0x 1 0x 1
This should make printk usable for pretty printing u8 and u16 integers.
Change-Id: I58fa869e9c295a052f97fbf052291ef4d132811e
Signed-off-by: Szymon Janc <szymon.janc@codecoup.pl>
2016-11-20 22:06:37 +01:00
|
|
|
if (min_width < 0) {
|
|
|
|
min_width = *fmt - '0';
|
|
|
|
} else {
|
|
|
|
min_width = 10 * min_width + *fmt - '0';
|
|
|
|
}
|
misc: Support left-justification in printk()
This is supported by printf(), and is an important formatting feature
to print out some complex, nicely-formatted information. This is
accomplished by using a negative padding in the formatting string.
The following code:
printk("none: |%u| |%x|\n", 12345, 12345);
printk("zero_before: |%08u| |%08x|\n", 12345, 12345);
printk("space_before: |%8u| |%8x|\n", 12345, 12345);
printk("space_after: |%-8u| |%-8x|\n", 12345, 12345);
Will produce the following output:
none: |12345| |3039|
zero_before: |00012345| |0000000000003039|
space_before: | 12345| | 3039|
space_after: |12345 | |3039 |
Change-Id: I9c2d85a1790087d53b52b7713854adaf99282f09
Jira: ZEP-1599
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-04-14 21:47:58 +02:00
|
|
|
|
|
|
|
if (padding == PAD_NONE) {
|
|
|
|
padding = PAD_SPACE_BEFORE;
|
|
|
|
}
|
printk: Add basic support for width modifier and zero padding
This adds basic support for width modifier when printing integers.
Supported specifiers are u,i,d,x,X. Width '*' is not supported.
Flag '0' for left-pading number with zero is also supported.
examples:
printk("0x%x 0x%02x 0x%04x 0x%08x\n", 1, 1, 1, 1);
0x1 0x01 0x0001 0x00000001
printk("0x%x 0x%2x 0x%4x 0x%8x\n", 1, 1, 1, 1);
0x1 0x 1 0x 1 0x 1
This should make printk usable for pretty printing u8 and u16 integers.
Change-Id: I58fa869e9c295a052f97fbf052291ef4d132811e
Signed-off-by: Szymon Janc <szymon.janc@codecoup.pl>
2016-11-20 22:06:37 +01:00
|
|
|
goto still_might_format;
|
2019-06-14 04:44:07 +02:00
|
|
|
case 'h':
|
2016-08-18 21:57:59 +02:00
|
|
|
case 'l':
|
2017-05-30 23:38:11 +02:00
|
|
|
case 'z':
|
2019-06-14 04:44:07 +02:00
|
|
|
if (*fmt == 'h' && length_mod == 'h') {
|
|
|
|
length_mod = 'H';
|
|
|
|
} else if (*fmt == 'l' && length_mod == 'l') {
|
|
|
|
length_mod = 'L';
|
|
|
|
} else if (length_mod == 0) {
|
|
|
|
length_mod = *fmt;
|
|
|
|
} else {
|
|
|
|
out((int)'%', ctx);
|
|
|
|
out((int)*fmt, ctx);
|
|
|
|
break;
|
|
|
|
}
|
2016-08-18 21:57:59 +02:00
|
|
|
goto still_might_format;
|
2015-10-21 14:42:01 +02:00
|
|
|
case 'd':
|
2020-06-23 00:54:23 +02:00
|
|
|
case 'i':
|
|
|
|
case 'u': {
|
|
|
|
printk_val_t d;
|
2019-01-17 22:26:51 +01:00
|
|
|
|
2019-06-14 04:44:07 +02:00
|
|
|
if (length_mod == 'z') {
|
|
|
|
d = va_arg(ap, ssize_t);
|
|
|
|
} else if (length_mod == 'l') {
|
|
|
|
d = va_arg(ap, long);
|
|
|
|
} else if (length_mod == 'L') {
|
2019-01-17 22:26:51 +01:00
|
|
|
long long lld = va_arg(ap, long long);
|
2020-06-23 00:54:23 +02:00
|
|
|
if (!ok64(out, ctx, lld)) {
|
2019-01-17 22:26:51 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-06-23 00:54:23 +02:00
|
|
|
d = (printk_val_t) lld;
|
2019-06-14 04:44:07 +02:00
|
|
|
} else {
|
|
|
|
d = va_arg(ap, int);
|
2017-05-30 23:38:11 +02:00
|
|
|
}
|
2015-10-21 14:42:01 +02:00
|
|
|
|
2020-06-23 00:54:23 +02:00
|
|
|
if (*fmt != 'u' && negative(d)) {
|
2016-11-28 08:55:19 +01:00
|
|
|
out((int)'-', ctx);
|
2015-10-21 14:42:01 +02:00
|
|
|
d = -d;
|
printk: Add basic support for width modifier and zero padding
This adds basic support for width modifier when printing integers.
Supported specifiers are u,i,d,x,X. Width '*' is not supported.
Flag '0' for left-pading number with zero is also supported.
examples:
printk("0x%x 0x%02x 0x%04x 0x%08x\n", 1, 1, 1, 1);
0x1 0x01 0x0001 0x00000001
printk("0x%x 0x%2x 0x%4x 0x%8x\n", 1, 1, 1, 1);
0x1 0x 1 0x 1 0x 1
This should make printk usable for pretty printing u8 and u16 integers.
Change-Id: I58fa869e9c295a052f97fbf052291ef4d132811e
Signed-off-by: Szymon Janc <szymon.janc@codecoup.pl>
2016-11-20 22:06:37 +01:00
|
|
|
min_width--;
|
2015-04-11 01:44:37 +02:00
|
|
|
}
|
2020-06-23 00:54:23 +02:00
|
|
|
print_dec(out, ctx, d, padding, min_width);
|
2015-10-21 14:42:01 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-08-19 21:34:50 +02:00
|
|
|
case 'p':
|
2019-11-05 22:28:17 +01:00
|
|
|
out('0', ctx);
|
|
|
|
out('x', ctx);
|
|
|
|
/* left-pad pointers with zeros */
|
|
|
|
padding = PAD_ZERO_BEFORE;
|
2020-06-27 00:14:51 +02:00
|
|
|
min_width = sizeof(void *) * 2;
|
2019-11-05 22:28:17 +01:00
|
|
|
/* Fall through */
|
2015-10-21 14:42:01 +02:00
|
|
|
case 'x':
|
2016-08-19 21:34:50 +02:00
|
|
|
case 'X': {
|
2020-06-23 00:54:23 +02:00
|
|
|
printk_val_t x;
|
2017-05-30 23:38:11 +02:00
|
|
|
|
2019-06-14 04:44:07 +02:00
|
|
|
if (*fmt == 'p') {
|
|
|
|
x = (uintptr_t)va_arg(ap, void *);
|
|
|
|
} else if (length_mod == 'l') {
|
2017-05-30 23:38:11 +02:00
|
|
|
x = va_arg(ap, unsigned long);
|
2019-06-14 04:44:07 +02:00
|
|
|
} else if (length_mod == 'L') {
|
2019-01-11 23:45:15 +01:00
|
|
|
x = va_arg(ap, unsigned long long);
|
2019-06-14 04:44:07 +02:00
|
|
|
} else {
|
|
|
|
x = va_arg(ap, unsigned int);
|
2017-05-30 23:38:11 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 00:54:23 +02:00
|
|
|
print_hex(out, ctx, x, padding, min_width);
|
2015-10-21 14:42:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's': {
|
|
|
|
char *s = va_arg(ap, char *);
|
2017-11-14 13:11:42 +01:00
|
|
|
char *start = s;
|
2015-10-21 14:34:43 +02:00
|
|
|
|
2019-06-04 16:52:23 +02:00
|
|
|
while (*s) {
|
2016-11-28 08:55:19 +01:00
|
|
|
out((int)(*s++), ctx);
|
2019-06-04 16:52:23 +02:00
|
|
|
}
|
2017-11-14 13:11:42 +01:00
|
|
|
|
|
|
|
if (padding == PAD_SPACE_AFTER) {
|
|
|
|
int remaining = min_width - (s - start);
|
|
|
|
while (remaining-- > 0) {
|
|
|
|
out(' ', ctx);
|
|
|
|
}
|
|
|
|
}
|
2015-10-21 14:42:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'c': {
|
|
|
|
int c = va_arg(ap, int);
|
|
|
|
|
2016-11-28 08:55:19 +01:00
|
|
|
out(c, ctx);
|
2015-10-21 14:42:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '%': {
|
2016-11-28 08:55:19 +01:00
|
|
|
out((int)'%', ctx);
|
2015-10-21 14:42:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2016-11-28 08:55:19 +01:00
|
|
|
out((int)'%', ctx);
|
|
|
|
out((int)*fmt, ctx);
|
2015-10-21 14:42:01 +02:00
|
|
|
break;
|
2015-04-11 01:44:37 +02:00
|
|
|
}
|
2015-10-21 14:42:01 +02:00
|
|
|
might_format = 0;
|
2015-04-11 01:44:37 +02:00
|
|
|
}
|
2016-08-18 21:57:59 +02:00
|
|
|
still_might_format:
|
2015-04-11 01:44:37 +02:00
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 20:57:43 +01:00
|
|
|
#ifdef CONFIG_PRINTK
|
2017-10-11 01:01:49 +02:00
|
|
|
#ifdef CONFIG_USERSPACE
|
2017-10-13 19:24:30 +02:00
|
|
|
struct buf_out_context {
|
2017-10-11 01:01:49 +02:00
|
|
|
int count;
|
|
|
|
unsigned int buf_count;
|
|
|
|
char buf[CONFIG_PRINTK_BUFFER_SIZE];
|
|
|
|
};
|
|
|
|
|
2017-10-13 19:24:30 +02:00
|
|
|
static void buf_flush(struct buf_out_context *ctx)
|
2017-10-11 01:01:49 +02:00
|
|
|
{
|
|
|
|
k_str_out(ctx->buf, ctx->buf_count);
|
2018-11-29 20:15:18 +01:00
|
|
|
ctx->buf_count = 0U;
|
2017-10-11 01:01:49 +02:00
|
|
|
}
|
|
|
|
|
2017-10-13 19:24:30 +02:00
|
|
|
static int buf_char_out(int c, void *ctx_p)
|
2017-10-11 01:01:49 +02:00
|
|
|
{
|
2017-10-13 19:24:30 +02:00
|
|
|
struct buf_out_context *ctx = ctx_p;
|
|
|
|
|
2017-10-11 01:01:49 +02:00
|
|
|
ctx->count++;
|
|
|
|
ctx->buf[ctx->buf_count++] = c;
|
|
|
|
if (ctx->buf_count == CONFIG_PRINTK_BUFFER_SIZE) {
|
|
|
|
buf_flush(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
2017-10-13 19:24:30 +02:00
|
|
|
#endif /* CONFIG_USERSPACE */
|
|
|
|
|
2016-11-28 08:55:19 +01:00
|
|
|
struct out_context {
|
|
|
|
int count;
|
|
|
|
};
|
|
|
|
|
2017-10-13 19:24:30 +02:00
|
|
|
static int char_out(int c, void *ctx_p)
|
2016-11-28 08:55:19 +01:00
|
|
|
{
|
2017-10-13 19:24:30 +02:00
|
|
|
struct out_context *ctx = ctx_p;
|
|
|
|
|
2016-11-28 08:55:19 +01:00
|
|
|
ctx->count++;
|
|
|
|
return _char_out(c);
|
|
|
|
}
|
|
|
|
|
2017-10-13 19:24:30 +02:00
|
|
|
#ifdef CONFIG_USERSPACE
|
2018-09-12 00:57:04 +02:00
|
|
|
void vprintk(const char *fmt, va_list ap)
|
2017-04-20 21:31:37 +02:00
|
|
|
{
|
2017-10-13 19:24:30 +02:00
|
|
|
if (_is_user_context()) {
|
|
|
|
struct buf_out_context ctx = { 0 };
|
2017-04-20 21:31:37 +02:00
|
|
|
|
2019-03-15 02:42:51 +01:00
|
|
|
z_vprintk(buf_char_out, &ctx, fmt, ap);
|
2017-10-11 01:01:49 +02:00
|
|
|
|
2017-10-13 19:24:30 +02:00
|
|
|
if (ctx.buf_count) {
|
|
|
|
buf_flush(&ctx);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct out_context ctx = { 0 };
|
2020-06-03 00:07:28 +02:00
|
|
|
#ifdef CONFIG_PRINTK_SYNC
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
#endif
|
2017-10-13 19:24:30 +02:00
|
|
|
|
2019-03-15 02:42:51 +01:00
|
|
|
z_vprintk(char_out, &ctx, fmt, ap);
|
2020-06-03 00:07:28 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_PRINTK_SYNC
|
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
#endif
|
2017-10-11 01:01:49 +02:00
|
|
|
}
|
2017-10-13 19:24:30 +02:00
|
|
|
}
|
|
|
|
#else
|
2018-09-12 00:57:04 +02:00
|
|
|
void vprintk(const char *fmt, va_list ap)
|
2017-10-13 19:24:30 +02:00
|
|
|
{
|
|
|
|
struct out_context ctx = { 0 };
|
2020-06-03 00:07:28 +02:00
|
|
|
#ifdef CONFIG_PRINTK_SYNC
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
#endif
|
2017-10-13 19:24:30 +02:00
|
|
|
|
2019-03-15 02:42:51 +01:00
|
|
|
z_vprintk(char_out, &ctx, fmt, ap);
|
2020-06-03 00:07:28 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_PRINTK_SYNC
|
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
#endif
|
2017-04-20 21:31:37 +02:00
|
|
|
}
|
2020-01-02 20:57:43 +01:00
|
|
|
#endif /* CONFIG_USERSPACE */
|
2017-04-20 21:31:37 +02:00
|
|
|
|
2019-03-08 22:19:05 +01:00
|
|
|
void z_impl_k_str_out(char *c, size_t n)
|
2017-10-11 01:01:49 +02:00
|
|
|
{
|
2020-04-03 18:55:04 +02:00
|
|
|
size_t i;
|
2020-06-03 00:07:28 +02:00
|
|
|
#ifdef CONFIG_PRINTK_SYNC
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
#endif
|
2017-10-11 01:01:49 +02:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
_char_out(c[i]);
|
|
|
|
}
|
2020-06-03 00:07:28 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_PRINTK_SYNC
|
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
#endif
|
2017-10-11 01:01:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
userspace: Support for split 64 bit arguments
System call arguments, at the arch layer, are single words. So
passing wider values requires splitting them into two registers at
call time. This gets even more complicated for values (e.g
k_timeout_t) that may have different sizes depending on configuration.
This patch adds a feature to gen_syscalls.py to detect functions with
wide arguments and automatically generates code to split/unsplit them.
Unfortunately the current scheme of Z_SYSCALL_DECLARE_* macros won't
work with functions like this, because for N arguments (our current
maximum N is 10) there are 2^N possible configurations of argument
widths. So this generates the complete functions for each handler and
wrapper, effectively doing in python what was originally done in the
preprocessor.
Another complexity is that traditional the z_hdlr_*() function for a
system call has taken the raw list of word arguments, which does not
work when some of those arguments must be 64 bit types. So instead of
using a single Z_SYSCALL_HANDLER macro, this splits the job of
z_hdlr_*() into two steps: An automatically-generated unmarshalling
function, z_mrsh_*(), which then calls a user-supplied verification
function z_vrfy_*(). The verification function is typesafe, and is a
simple C function with exactly the same argument and return signature
as the syscall impl function. It is also not responsible for
validating the pointers to the extra parameter array or a wide return
value, that code gets automatically generated.
This commit includes new vrfy/msrh handling for all syscalls invoked
during CI runs. Future commits will port the less testable code.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-08-06 22:34:31 +02:00
|
|
|
static inline void z_vrfy_k_str_out(char *c, size_t n)
|
2017-10-11 01:01:49 +02:00
|
|
|
{
|
2018-05-05 00:57:57 +02:00
|
|
|
Z_OOPS(Z_SYSCALL_MEMORY_READ(c, n));
|
2019-03-08 22:19:05 +01:00
|
|
|
z_impl_k_str_out((char *)c, n);
|
2017-10-11 01:01:49 +02:00
|
|
|
}
|
userspace: Support for split 64 bit arguments
System call arguments, at the arch layer, are single words. So
passing wider values requires splitting them into two registers at
call time. This gets even more complicated for values (e.g
k_timeout_t) that may have different sizes depending on configuration.
This patch adds a feature to gen_syscalls.py to detect functions with
wide arguments and automatically generates code to split/unsplit them.
Unfortunately the current scheme of Z_SYSCALL_DECLARE_* macros won't
work with functions like this, because for N arguments (our current
maximum N is 10) there are 2^N possible configurations of argument
widths. So this generates the complete functions for each handler and
wrapper, effectively doing in python what was originally done in the
preprocessor.
Another complexity is that traditional the z_hdlr_*() function for a
system call has taken the raw list of word arguments, which does not
work when some of those arguments must be 64 bit types. So instead of
using a single Z_SYSCALL_HANDLER macro, this splits the job of
z_hdlr_*() into two steps: An automatically-generated unmarshalling
function, z_mrsh_*(), which then calls a user-supplied verification
function z_vrfy_*(). The verification function is typesafe, and is a
simple C function with exactly the same argument and return signature
as the syscall impl function. It is also not responsible for
validating the pointers to the extra parameter array or a wide return
value, that code gets automatically generated.
This commit includes new vrfy/msrh handling for all syscalls invoked
during CI runs. Future commits will port the less testable code.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-08-06 22:34:31 +02:00
|
|
|
#include <syscalls/k_str_out_mrsh.c>
|
2020-01-02 20:57:43 +01:00
|
|
|
#endif /* CONFIG_USERSPACE */
|
2017-10-11 01:01:49 +02:00
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Output a string
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2015-07-24 22:55:19 +02:00
|
|
|
* Output a string on output installed by platform at init time. Some
|
|
|
|
* printf-like formatting is available.
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
|
|
|
* Available formatting:
|
2019-06-14 04:44:07 +02:00
|
|
|
* - %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.
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2015-07-16 20:07:57 +02:00
|
|
|
* @param fmt formatted string to output
|
|
|
|
*
|
2018-09-12 00:57:04 +02:00
|
|
|
* @return N/A
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2018-09-12 00:57:04 +02:00
|
|
|
void printk(const char *fmt, ...)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
2015-04-15 00:15:52 +02:00
|
|
|
|
2015-04-11 01:44:37 +02:00
|
|
|
va_start(ap, fmt);
|
2018-05-16 08:50:33 +02:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_LOG_PRINTK)) {
|
2018-09-12 00:57:04 +02:00
|
|
|
log_printk(fmt, ap);
|
2018-05-16 08:50:33 +02:00
|
|
|
} else {
|
2018-09-12 00:57:04 +02:00
|
|
|
vprintk(fmt, ap);
|
2018-05-16 08:50:33 +02:00
|
|
|
}
|
2015-04-11 01:44:37 +02:00
|
|
|
va_end(ap);
|
|
|
|
}
|
2020-01-02 20:57:43 +01:00
|
|
|
#endif /* CONFIG_PRINTK */
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2016-11-28 09:31:18 +01:00
|
|
|
struct str_context {
|
|
|
|
char *str;
|
|
|
|
int max;
|
|
|
|
int count;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int str_out(int c, struct str_context *ctx)
|
|
|
|
{
|
2019-03-14 22:38:13 +01:00
|
|
|
if (ctx->str == NULL || ctx->count >= ctx->max) {
|
2016-11-28 09:31:18 +01:00
|
|
|
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;
|
2019-06-29 00:44:38 +02:00
|
|
|
int ret;
|
2016-11-28 09:31:18 +01:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2019-06-29 00:44:38 +02:00
|
|
|
ret = vsnprintk(str, size, fmt, ap);
|
2016-11-28 09:31:18 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
2019-06-29 00:44:38 +02:00
|
|
|
return ret;
|
2016-11-28 09:31:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int vsnprintk(char *str, size_t size, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
struct str_context ctx = { str, size, 0 };
|
|
|
|
|
2019-03-15 02:42:51 +01:00
|
|
|
z_vprintk((out_func_t)str_out, &ctx, fmt, ap);
|
2016-11-28 09:31:18 +01:00
|
|
|
|
|
|
|
if (ctx.count < ctx.max) {
|
|
|
|
str[ctx.count] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.count;
|
|
|
|
}
|