printf: Convert %T to %pT

In order to be more compliant to standards, and ultimately turn on
compile-time printf format validation, switch the non-standard %T
into %pT, which takes a pointer to a 64-bit timestamp as an argument.
For convenience, define PRINTF_TIMESTAMP_NOW, which will use the
current time as the timestamp value, rather than forcing everyone
to pass a pointer to get_time().val.

For a couple of instances, simply use CPRINTS instead.

BUG=chromium:984041
TEST=make -j buildall
BRANCH=None

Cq-Depend:chrome-internal:1473305
Change-Id: I83e45b55a95ea27256dc147544ae3f7e39acc5dd
Signed-off-by: Evan Green <evgreen@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1704216
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
This commit is contained in:
Evan Green 2019-07-19 16:20:45 -07:00 committed by Commit Bot
parent a41fea6b7d
commit 0128582fe6
17 changed files with 66 additions and 35 deletions

View File

@ -304,7 +304,7 @@ static void ccd_state_change_hook(void)
if (flags_now == flags_want)
return;
CPRINTF("[%T CCD state:");
CPRINTF("[%pT CCD state:", PRINTF_TIMESTAMP_NOW);
print_state_flags(CC_USB, flags_want);
CPRINTF("]\n");

View File

@ -111,7 +111,7 @@ static void print_packet(int head, uint32_t *payload)
const char *prole;
if (trace_mode == TRACE_MODE_RAW) {
ccprintf("%T[%04x]", head);
ccprintf("%pT[%04x]", PRINTF_TIMESTAMP_NOW, head);
for (i = 0; i < cnt; i++)
ccprintf(" %08x", payload[i]);
ccputs("\n");
@ -119,7 +119,8 @@ static void print_packet(int head, uint32_t *payload)
}
name = cnt ? data_msg_name[typ] : ctrl_msg_name[typ];
prole = head & (PD_ROLE_SOURCE << 8) ? "SRC" : "SNK";
ccprintf("%T %s/%d [%04x]%s", prole, id, head, name);
ccprintf("%pT %s/%d [%04x]%s",
PRINTF_TIMESTAMP_NOW, prole, id, head, name);
if (!cnt) { /* Control message : we are done */
ccputs("\n");
return;
@ -150,11 +151,11 @@ static void print_packet(int head, uint32_t *payload)
static void print_error(enum pd_rx_errors err)
{
if (err == PD_RX_ERR_INVAL)
ccprintf("%T TMOUT\n");
ccprintf("%pT TMOUT\n", PRINTF_TIMESTAMP_NOW);
else if (err == PD_RX_ERR_HARD_RESET)
ccprintf("%T HARD-RST\n");
ccprintf("%pT HARD-RST\n", PRINTF_TIMESTAMP_NOW);
else if (err == PD_RX_ERR_UNSUPPORTED_SOP)
ccprintf("%T SOP*\n");
ccprintf("%pT SOP*\n", PRINTF_TIMESTAMP_NOW);
else
ccprintf("ERR %d\n", err);
}

View File

@ -533,7 +533,8 @@ int pd_custom_vdm(int port, int cnt, uint32_t *payload,
if (PD_VDO_VID(payload[0]) != USB_VID_GOOGLE || !gfu_mode)
return 0;
debug_printf("%T] VDM/%d [%d] %08x\n", cnt, cmd, payload[0]);
debug_printf("%pT] VDM/%d [%d] %08x\n",
PRINTF_TIMESTAMP_NOW, cnt, cmd, payload[0]);
*rpayload = payload;
rsize = pd_custom_flash_vdm(port, cnt, payload);

View File

@ -313,7 +313,8 @@ int acpi_ap_to_ec(int is_cmd, uint8_t value, uint8_t *resultptr)
* does a lot of keyboard backlights and it scrolls the
* debug console.
*/
CPRINTF("\r[%T ACPI kblight %d]", data);
CPRINTF("\r[%pT ACPI kblight %d]",
PRINTF_TIMESTAMP_NOW, data);
kblight_set(data);
break;
#endif

View File

@ -350,7 +350,7 @@ DECLARE_HOST_COMMAND(EC_CMD_BATTERY_CUT_OFF, battery_command_cutoff,
static void check_pending_cutoff(void)
{
if (battery_cutoff_state == BATTERY_CUTOFF_STATE_PENDING) {
CPRINTF("[%T Cutting off battery in %d second(s)]\n",
CPRINTS("Cutting off battery in %d second(s)",
CONFIG_BATTERY_CUTOFF_DELAY_US / SECOND);
hook_call_deferred(&pending_cutoff_deferred_data,
CONFIG_BATTERY_CUTOFF_DELAY_US);

View File

@ -52,7 +52,8 @@ static void capsense_change_deferred(void)
new_val = capsense_read_bitmask();
if (new_val != cur_val) {
CPRINTF("[%T capsense 0x%02x: ", new_val);
CPRINTF("[%pT capsense 0x%02x: ",
PRINTF_TIMESTAMP_NOW, new_val);
for (i = 0; i < CAPSENSE_MASK_BITS; i++) {
/* See what changed */
n = (new_val >> i) & 0x01;

View File

@ -89,7 +89,7 @@ int cprints(enum console_channel channel, const char *format, ...)
return EC_SUCCESS;
#endif
rv = cprintf(channel, "[%T ");
rv = cprintf(channel, "[%pT ", PRINTF_TIMESTAMP_NOW);
va_start(args, format);
r = uart_vprintf(format, args);

View File

@ -604,7 +604,7 @@ static void dump_host_command_suppressed(int force)
if (!force && !timestamp_expired(suppressed_cmd_deadline, NULL))
return;
CPRINTF("[%T HC Suppressed:");
CPRINTF("[%pT HC Suppressed:", PRINTF_TIMESTAMP_NOW);
for (i = 0; i < ARRAY_SIZE(hc_suppressed_cmd); i++) {
CPRINTF(" 0x%x=%d", hc_suppressed_cmd[i], hc_suppressed_cnt[i]);
hc_suppressed_cnt[i] = 0;

View File

@ -151,7 +151,7 @@ static void print_state(const uint8_t *state, const char *msg)
{
int c;
CPRINTF("[%T KB %s:", msg);
CPRINTF("[%pT KB %s:", PRINTF_TIMESTAMP_NOW, msg);
for (c = 0; c < keyboard_cols; c++) {
if (state[c])
CPRINTF(" %02x", state[c]);
@ -529,7 +529,7 @@ static int check_keys_changed(uint8_t *state)
#ifdef CONFIG_KEYBOARD_PRINT_SCAN_TIMES
/* Print delta times from now back to each previous scan */
CPRINTF("[%T kb deltaT");
CPRINTF("[%pT kb deltaT", PRINTF_TIMESTAMP_NOW);
for (i = 0; i < SCAN_TIME_COUNT; i++) {
int tnew = scan_time[
(SCAN_TIME_COUNT + scan_time_index - i) %

View File

@ -288,7 +288,7 @@ void lb_init(int use_lock)
{
int i;
CPRINTF("[%T LB_init_vals ");
CPRINTF("[%pT LB_init_vals ", PRINTF_TIMESTAMP_NOW);
for (i = 0; i < ARRAY_SIZE(init_vals); i++) {
CPRINTF("%c", '0' + i % 10);
if (use_lock)

View File

@ -877,7 +877,8 @@ void motion_sense_task(void *u)
#endif
#ifdef CONFIG_CMD_ACCEL_INFO
if (accel_disp) {
CPRINTF("[%T event 0x%08x ", event);
CPRINTF("[%pT event 0x%08x ",
PRINTF_TIMESTAMP_NOW, event);
for (i = 0; i < motion_sensor_count; ++i) {
sensor = &motion_sensors[i];
CPRINTF("%s=%-5d, %-5d, %-5d ", sensor->name,

View File

@ -53,7 +53,8 @@ void port_80_write(int data)
* coming from the host.
*/
if (print_in_int)
CPRINTF("%c[%T Port 80: 0x%02x]", scroll ? '\n' : '\r', data);
CPRINTF("%c[%pT Port 80: 0x%02x]",
scroll ? '\n' : '\r', PRINTF_TIMESTAMP_NOW, data);
else if (data < 0x100)
hook_call_deferred(&port80_dump_buffer_data, 4 * SECOND);

View File

@ -210,6 +210,8 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context,
v = va_arg(args, uint32_t);
#else /* NO_UINT64_SUPPORT */
uint64_t v;
int ptrspec;
void *ptrval;
/* Handle length */
if (c == 'l') {
@ -217,16 +219,37 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context,
c = *format++;
}
/* Special-case: %T = current time */
if (c == 'T') {
v = get_time().val;
flags |= PF_64BIT;
#ifdef CONFIG_CONSOLE_VERBOSE
precision = 6;
#else
precision = 3;
v /= 1000;
#endif
if (c == 'p') {
ptrspec = *format++;
ptrval = va_arg(args, void *);
/* %pT - print a timestamp. */
if (ptrspec == 'T') {
flags |= PF_64BIT;
/* NULL uses the current time. */
if (ptrval == NULL)
v = get_time().val;
else
v = *(uint64_t *)ptrval;
if (IS_ENABLED(
CONFIG_CONSOLE_VERBOSE)) {
precision = 6;
} else {
precision = 3;
v /= 1000;
}
} else if (ptrspec == 'P') {
/* Print a raw pointer. */
v = (unsigned long)ptrval;
if (sizeof(unsigned long) ==
sizeof(uint64_t))
flags |= PF_64BIT;
} else {
return EC_ERROR_INVAL;
}
} else if (flags & PF_64BIT) {
v = va_arg(args, uint64_t);
} else {

View File

@ -18,6 +18,7 @@
#include "util.h"
#define CPRINTF(format, args...) cprintf(CC_ACCEL, format, ## args)
#define CPRINTS(format, args...) cprints(CC_ACCEL, format, ## args)
/* Only when configured as base accel sensor, fifo and interrupt
* are supported.
@ -463,7 +464,7 @@ static int read(const struct motion_sensor_t *s, intv3_t v)
LIS2DW12_OUT_X_L_ADDR, raw,
OUT_XYZ_SIZE);
if (ret != EC_SUCCESS) {
CPRINTF("[%T %s type:0x%X RD XYZ Error]", s->name, s->type);
CPRINTS("%s type:0x%X RD XYZ Error", s->name, s->type);
return ret;
}
@ -559,8 +560,7 @@ static int init(const struct motion_sensor_t *s)
err_unlock:
mutex_unlock(s->mutex);
CPRINTF("[%T %s: MS Init type:0x%X Error]\n", s->name, s->type);
CPRINTS("%s: MS Init type:0x%X Error", s->name, s->type);
return EC_ERROR_UNKNOWN;
}

View File

@ -818,8 +818,7 @@ static int init(const struct motion_sensor_t *s)
err_unlock:
mutex_unlock(s->mutex);
CPRINTF("[%T %s: MS Init type:0x%X Error]\n", s->name, s->type);
CPRINTS("%s: MS Init type:0x%X Error", s->name, s->type);
return ret;
}

View File

@ -10,6 +10,8 @@
#include "common.h"
#define PRINTF_TIMESTAMP_NOW NULL
/* Console command; used by DECLARE_CONSOLE_COMMAND macro. */
struct console_command {
/* Command name. Case-insensitive. */
@ -86,7 +88,7 @@ int cprintf(enum console_channel channel, const char *format, ...);
/**
* Print formatted output with timestamp. This is like:
* cprintf(channel, "[%T " + format + "]\n", ...)
* cprintf(channel, "[%pT " + format + "]\n", PRINTF_TIMESTAMP_NOW, ...)
*
* @param channel Output channel
* @param format Format string; see printf.h for valid formatting codes

View File

@ -52,8 +52,9 @@
* - 'b' - unsigned integer, print as binary
*
* Special format codes:
* - "%T" - current time in seconds - interpreted as "%.6T" for precision.
* This does NOT use up any arguments.
* - "%pT" - current time in seconds - interpreted as "%.6T" for precision.
* Supply PRINTF_TIMESTAMP_NOW to use the current time, or supply a
* pointer to a 64-bit timestamp to print.
*/
#ifndef HIDE_EC_STDLIB