libc: fix CONFIG_STDOUT_CONSOLE semantics

The intent of this Kconfig is to allow libc stdout
functions like printf() to send their output to the
active console driver instead of discarding it.

This somehow evolved into preferring to use
printf() instead of printk() for all test case output
if enabled. Libc printf() implementation for both
minimal libc and newlib use considerably more stack
space than printk(), with nothing gained by using
them.

Remove all instances where we are conditionally
sending test case output based on this config, enable
it by default, and adjust a few tests that disabled
this because they were blowing stack.

printk() and vprintk() now work as expected for
unit_testing targets, they are just wrappers for
host printf().

Fixes: #13701

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
This commit is contained in:
Andrew Boie 2019-02-24 13:17:31 -08:00 committed by Kumar Gala
parent 51a00cf790
commit feab37096b
18 changed files with 42 additions and 118 deletions

View file

@ -48,7 +48,7 @@ config NEWLIB_LIBC_FLOAT_SCANF
config STDOUT_CONSOLE
bool "Send stdout to console"
depends on CONSOLE_HAS_DRIVER
default NEWLIB_LIBC
default y
help
This option directs standard output (e.g. printf) to the console
device, rather than suppressing it entirely. See also EARLY_CONSOLE

View file

@ -72,13 +72,8 @@
#include "kernel.h"
#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#define MBEDTLS_PRINT printf
#else
#include <misc/printk.h>
#define MBEDTLS_PRINT ((int(*)(const char *, ...)) printk)
#endif /* CONFIG_STDOUT_CONSOLE */
static void my_debug(void *ctx, int level,
const char *file, int line, const char *str)

View file

@ -23,13 +23,6 @@ LOG_MODULE_REGISTER(net_google_iot_mqtt, LOG_LEVEL_INF);
#include <time.h>
#include <inttypes.h>
#ifdef CONFIG_STDOUT_CONSOLE
# include <stdio.h>
# define PRINT printf
#else
# define PRINT printk
#endif
s64_t time_base;
void do_sntp(struct addrinfo *addr)

View file

@ -26,13 +26,6 @@ LOG_MODULE_DECLARE(net_google_iot_mqtt, LOG_LEVEL_DBG);
#include <mbedtls/debug.h>
#ifdef CONFIG_STDOUT_CONSOLE
# include <stdio.h>
# define PRINT printf
#else
# define PRINT printk
#endif
extern s64_t time_base;
/* private key information */

View file

@ -85,23 +85,6 @@
#define STACK_SIZE 768
/*
* There are multiple threads doing printfs and they may conflict.
* Therefore use puts() instead of printf().
*/
#if defined(CONFIG_STDOUT_CONSOLE)
#define PRINTF(...) { char output[256]; \
sprintf(output, __VA_ARGS__); puts(output); }
#else
#define PRINTF(...) printk(__VA_ARGS__)
#endif
#if DEBUG_PRINTF
#define PR_DEBUG PRINTF
#else
#define PR_DEBUG(...)
#endif
#include "phil_obj_abstract.h"
#define fork(x) (forks[x])
@ -109,7 +92,7 @@
static void set_phil_state_pos(int id)
{
#if !DEBUG_PRINTF
PRINTF("\x1b[%d;%dH", id + 1, 1);
printk("\x1b[%d;%dH", id + 1, 1);
#endif
}
@ -120,18 +103,18 @@ static void print_phil_state(int id, const char *fmt, s32_t delay)
set_phil_state_pos(id);
PRINTF("Philosopher %d [%s:%s%d] ",
printk("Philosopher %d [%s:%s%d] ",
id, prio < 0 ? "C" : "P",
prio < 0 ? "" : " ",
prio);
if (delay) {
PRINTF(fmt, delay < 1000 ? " " : "", delay);
printk(fmt, delay < 1000 ? " " : "", delay);
} else {
PRINTF(fmt, "");
printk(fmt, "");
}
PRINTF("\n");
printk("\n");
}
static s32_t get_random_delay(int id, int period_in_ms)
@ -257,7 +240,7 @@ static void start_threads(void)
static void display_demo_description(void)
{
#if !DEBUG_PRINTF
PRINTF(DEMO_DESCRIPTION);
printk(DEMO_DESCRIPTION);
#endif
}

View file

@ -1,4 +1,3 @@
CONFIG_STDOUT_CONSOLE=n
CONFIG_SYS_CLOCK_TICKS_PER_SEC=100
CONFIG_ASSERT=y
CONFIG_ASSERT_LEVEL=2

View file

@ -74,19 +74,8 @@ osSemaphoreId forks[NUM_PHIL];
#define STACK_SIZE 512
/*
* There are multiple threads doing printfs and they may conflict.
* Therefore use puts() instead of printf().
*/
#if defined(CONFIG_STDOUT_CONSOLE)
#define PRINTF(...) { char output[256]; \
sprintf(output, __VA_ARGS__); puts(output); }
#else
#define PRINTF(...) printk(__VA_ARGS__)
#endif
#if DEBUG_PRINTF
#define PR_DEBUG PRINTF
#define PR_DEBUG printk
#else
#define PR_DEBUG(...)
#endif
@ -96,7 +85,7 @@ osSemaphoreId forks[NUM_PHIL];
static void set_phil_state_pos(int id)
{
#if !DEBUG_PRINTF
PRINTF("\x1b[%d;%dH", id + 1, 1);
printk("\x1b[%d;%dH", id + 1, 1);
#endif
}
@ -107,18 +96,18 @@ static void print_phil_state(int id, const char *fmt, s32_t delay)
set_phil_state_pos(id);
PRINTF("Philosopher %d [%s:%s%d] ",
printk("Philosopher %d [%s:%s%d] ",
id, prio < 0 ? "C" : "P",
prio < 0 ? "" : " ",
prio);
if (delay) {
PRINTF(fmt, delay < 1000 ? " " : "", delay);
printk(fmt, delay < 1000 ? " " : "", delay);
} else {
PRINTF(fmt, "");
printk(fmt, "");
}
PRINTF("\n");
printk("\n");
}
static s32_t get_random_delay(int id, int period_in_ms)
@ -215,7 +204,7 @@ static void start_threads(void)
static void display_demo_description(void)
{
#if !DEBUG_PRINTF
PRINTF(DEMO_DESCRIPTION);
printk(DEMO_DESCRIPTION);
#endif
}

View file

@ -1,4 +1,3 @@
CONFIG_STDOUT_CONSOLE=n
CONFIG_SYS_CLOCK_TICKS_PER_SEC=100
CONFIG_ASSERT=y
CONFIG_ASSERT_LEVEL=2

View file

@ -1,4 +1,3 @@
CONFIG_STDOUT_CONSOLE=n
CONFIG_SYS_CLOCK_TICKS_PER_SEC=100
CONFIG_ASSERT=y
CONFIG_ASSERT_LEVEL=2

View file

@ -31,12 +31,7 @@
#include <zephyr.h>
#include <kernel.h>
#include <cmsis_os2.h>
#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#else
#include <misc/printk.h>
#endif
#include <misc/__assert.h>
@ -115,19 +110,8 @@ static osThreadAttr_t thread_attr[] = {
};
/*
* There are multiple threads doing printfs and they may conflict.
* Therefore use puts() instead of printf().
*/
#if defined(CONFIG_STDOUT_CONSOLE)
#define PRINTF(...) { char output[256]; \
sprintf(output, __VA_ARGS__); puts(output); }
#else
#define PRINTF(...) printk(__VA_ARGS__)
#endif
#if DEBUG_PRINTF
#define PR_DEBUG PRINTF
#define PR_DEBUG printk
#else
#define PR_DEBUG(...)
#endif
@ -137,7 +121,7 @@ static osThreadAttr_t thread_attr[] = {
static void set_phil_state_pos(int id)
{
#if !DEBUG_PRINTF
PRINTF("\x1b[%d;%dH", id + 1, 1);
printk("\x1b[%d;%dH", id + 1, 1);
#endif
}
@ -148,18 +132,18 @@ static void print_phil_state(int id, const char *fmt, s32_t delay)
set_phil_state_pos(id);
PRINTF("Philosopher %d [%s:%s%d] ",
printk("Philosopher %d [%s:%s%d] ",
id, prio < 0 ? "C" : "P",
prio < 0 ? "" : " ",
prio);
if (delay) {
PRINTF(fmt, delay < 1000 ? " " : "", delay);
printk(fmt, delay < 1000 ? " " : "", delay);
} else {
PRINTF(fmt, "");
printk(fmt, "");
}
PRINTF("\n");
printk("\n");
}
static s32_t get_random_delay(int id, int period_in_ms)
@ -259,7 +243,7 @@ static void start_threads(void)
static void display_demo_description(void)
{
#if !DEBUG_PRINTF
PRINTF(DEMO_DESCRIPTION);
printk(DEMO_DESCRIPTION);
#endif
}

View file

@ -1,4 +1,3 @@
CONFIG_STDOUT_CONSOLE=n
CONFIG_SYS_CLOCK_TICKS_PER_SEC=100
CONFIG_ASSERT=y
CONFIG_ASSERT_LEVEL=2

View file

@ -13,14 +13,9 @@
#include <string.h>
#include <shell/shell.h>
#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#define PRINT_DATA(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#include <misc/printk.h>
#define PRINT_DATA(fmt, ...) printk(fmt, ##__VA_ARGS__)
#endif /* CONFIG_STDOUT_CONSOLE */
#if defined CONFIG_ARCH_POSIX
#include "posix_board_if.h"

View file

@ -43,18 +43,14 @@ extern "C" {
#define CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC 10000000
/* FIXME: Properly integrate with Zephyr's arch specific code */
#define CONFIG_X86 1
#define PRINT printf
#endif /* !KERNEL */
#define CONFIG_PRINTK 1
#endif
#include <misc/printk.h>
#define PRINT printk
#include <zephyr.h>
#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#define PRINT printf
#else /* !CONFIG_STDOUT_CONSOLE */
#include <misc/printk.h>
#define PRINT printk
#endif /* CONFIG_STDOUT_CONSOLE */
#include <ztest_assert.h>
#include <ztest_mock.h>

View file

@ -48,13 +48,8 @@ static inline void _zassert(int cond,
va_start(vargs, msg);
PRINT("\n Assertion failed at %s:%d: %s: %s\n",
file, line, func, default_msg);
#if defined(CONFIG_STDOUT_CONSOLE)
vprintf(msg, vargs);
printf("\n");
#else
vprintk(msg, vargs);
printk("\n");
#endif
va_end(vargs);
ztest_test_fail();
}

View file

@ -19,6 +19,7 @@ struct parameter {
#ifndef KERNEL
#include <stdlib.h>
#include <stdarg.h>
static void free_parameter(struct parameter *param)
{
@ -43,6 +44,19 @@ void _init_mock(void)
}
void printk(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
void vprintk(const char *fmt, va_list ap)
{
vprintf(fmt, ap);
}
#else
/*

View file

@ -7,13 +7,8 @@
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#define MBEDTLS_PRINT printf
#else
#include <misc/printk.h>
#define MBEDTLS_PRINT (int(*)(const char *, ...)) printk
#endif /* CONFIG_STDOUT_CONSOLE */
#include <string.h>
#include <stdio.h>

View file

@ -1,4 +1,2 @@
CONFIG_IRQ_OFFLOAD=y
CONFIG_ZTEST=y
# May fail without this
CONFIG_STDOUT_CONSOLE=n

View file

@ -1,5 +1,3 @@
CONFIG_IRQ_OFFLOAD=y
CONFIG_ZTEST=y
# May fail without this
CONFIG_STDOUT_CONSOLE=n
CONFIG_QEMU_TICKLESS_WORKAROUND=y
CONFIG_QEMU_TICKLESS_WORKAROUND=y