6#ifndef ZEPHYR_INCLUDE_LOGGING_LOG_MSG_H_
7#define ZEPHYR_INCLUDE_LOGGING_LOG_MSG_H_
20#define alloca __builtin_alloca
30#define LOG_MSG_DEBUG 0
31#define LOG_MSG_DBG(...) IF_ENABLED(LOG_MSG_DEBUG, (printk(__VA_ARGS__)))
33#ifdef CONFIG_LOG_TIMESTAMP_64BIT
46#define Z_LOG_MSG_LOG 0
48#define Z_LOG_MSG_PACKAGE_BITS 11
50#define Z_LOG_MSG_MAX_PACKAGE BIT_MASK(Z_LOG_MSG_PACKAGE_BITS)
52#define LOG_MSG_GENERIC_HDR \
75#if (INTPTR_MAX > INT32_MAX) && !defined(CONFIG_LOG_TIMESTAMP_64BIT)
82#if defined(CONFIG_LOG_THREAD_ID_PREFIX)
87#define Z_LOG_MSG_ALIGNMENT CBPRINTF_PACKAGE_ALIGNMENT
89#define Z_LOG_MSG_PADDING \
90 ((sizeof(struct log_msg_hdr) % Z_LOG_MSG_ALIGNMENT) > 0 ? \
91 (Z_LOG_MSG_ALIGNMENT - (sizeof(struct log_msg_hdr) % Z_LOG_MSG_ALIGNMENT)) : \
106BUILD_ASSERT(
sizeof(
struct log_msg) % Z_LOG_MSG_ALIGNMENT == 0,
107 "Log msg size must aligned");
131 Z_LOG_MSG_MODE_RUNTIME,
136 Z_LOG_MSG_MODE_FROM_STACK,
142 Z_LOG_MSG_MODE_ZERO_COPY,
145 Z_LOG_MSG_MODE_SIMPLE,
148#define Z_LOG_MSG_DESC_INITIALIZER(_domain_id, _level, _plen, _dlen) \
152 .type = Z_LOG_MSG_LOG, \
153 .domain = _domain_id, \
155 .package_len = _plen, \
159#define Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt) \
160 (CBPRINTF_PACKAGE_FIRST_RO_STR_CNT(_cstr_cnt) | \
161 (IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC) ? \
162 CBPRINTF_PACKAGE_ADD_STRING_IDXS : 0))
164#ifdef CONFIG_LOG_USE_VLA
165#define Z_LOG_MSG_ON_STACK_ALLOC(ptr, len) \
166 long long _ll_buf[DIV_ROUND_UP(len, sizeof(long long))]; \
167 long double _ld_buf[DIV_ROUND_UP(len, sizeof(long double))]; \
168 ptr = (sizeof(long double) == Z_LOG_MSG_ALIGNMENT) ? \
169 (struct log_msg *)_ld_buf : (struct log_msg *)_ll_buf; \
170 if (IS_ENABLED(CONFIG_LOG_TEST_CLEAR_MESSAGE_SPACE)) { \
172 memset(ptr, 0, len); \
180#define Z_LOG_MSG_ON_STACK_ALLOC(ptr, len) \
181 long long _ll_buf32[32 / sizeof(long long)]; \
182 long long _ll_buf48[48 / sizeof(long long)]; \
183 long long _ll_buf64[64 / sizeof(long long)]; \
184 long long _ll_buf128[128 / sizeof(long long)]; \
185 long long _ll_buf256[256 / sizeof(long long)]; \
186 long double _ld_buf32[32 / sizeof(long double)]; \
187 long double _ld_buf48[48 / sizeof(long double)]; \
188 long double _ld_buf64[64 / sizeof(long double)]; \
189 long double _ld_buf128[128 / sizeof(long double)]; \
190 long double _ld_buf256[256 / sizeof(long double)]; \
191 if (sizeof(long double) == Z_LOG_MSG_ALIGNMENT) { \
192 ptr = (len > 128) ? (struct log_msg *)_ld_buf256 : \
193 ((len > 64) ? (struct log_msg *)_ld_buf128 : \
194 ((len > 48) ? (struct log_msg *)_ld_buf64 : \
195 ((len > 32) ? (struct log_msg *)_ld_buf48 : \
196 (struct log_msg *)_ld_buf32)));\
198 ptr = (len > 128) ? (struct log_msg *)_ll_buf256 : \
199 ((len > 64) ? (struct log_msg *)_ll_buf128 : \
200 ((len > 48) ? (struct log_msg *)_ll_buf64 : \
201 ((len > 32) ? (struct log_msg *)_ll_buf48 : \
202 (struct log_msg *)_ll_buf32)));\
204 if (IS_ENABLED(CONFIG_LOG_TEST_CLEAR_MESSAGE_SPACE)) { \
206 memset(ptr, 0, len); \
210#define Z_LOG_MSG_ALIGN_OFFSET \
211 offsetof(struct log_msg, data)
213#define Z_LOG_MSG_LEN(pkg_len, data_len) \
214 (offsetof(struct log_msg, data) + pkg_len + (data_len))
216#define Z_LOG_MSG_ALIGNED_WLEN(pkg_len, data_len) \
217 DIV_ROUND_UP(ROUND_UP(Z_LOG_MSG_LEN(pkg_len, data_len), \
218 Z_LOG_MSG_ALIGNMENT), \
230#define Z_LOG_ARM64_VLA_PROTECT() compiler_barrier()
232#define _LOG_MSG_SIMPLE_XXXX0 1
233#define _LOG_MSG_SIMPLE_XXXX1 1
234#define _LOG_MSG_SIMPLE_XXXX2 1
237#define LOG_MSG_SIMPLE_ARG_CNT_CHECK(...) \
238 COND_CODE_1(UTIL_CAT(_LOG_MSG_SIMPLE_XXXX, NUM_VA_ARGS_LESS_1(__VA_ARGS__)), (1), (0))
241#define LOG_MSG_SIMPLE_ARG_TYPE_CHECK_0(fmt) 1
242#define LOG_MSG_SIMPLE_ARG_TYPE_CHECK_1(fmt, arg) Z_CBPRINTF_IS_WORD_NUM(arg)
243#define LOG_MSG_SIMPLE_ARG_TYPE_CHECK_2(fmt, arg0, arg1) \
244 Z_CBPRINTF_IS_WORD_NUM(arg0) && Z_CBPRINTF_IS_WORD_NUM(arg1)
250#define LOG_MSG_SIMPLE_ARG_TYPE_CHECK(...) \
251 UTIL_CAT(LOG_MSG_SIMPLE_ARG_TYPE_CHECK_, NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__)
265#define LOG_MSG_SIMPLE_CHECK(...) \
266 COND_CODE_1(CONFIG_64BIT, (0), (\
267 COND_CODE_1(LOG_MSG_SIMPLE_ARG_CNT_CHECK(__VA_ARGS__), ( \
268 LOG_MSG_SIMPLE_ARG_TYPE_CHECK(__VA_ARGS__)), (0))))
271#define Z_LOG_MSG_SIMPLE_CREATE_1(_source, _level, ...) \
272 z_log_msg_simple_create_1(_source, _level, GET_ARG_N(1, __VA_ARGS__), \
273 (uint32_t)(uintptr_t)GET_ARG_N(2, __VA_ARGS__))
277#define Z_LOG_MSG_SIMPLE_CREATE_2(_source, _level, ...) \
278 z_log_msg_simple_create_2(_source, _level, GET_ARG_N(1, __VA_ARGS__), \
279 (uint32_t)(uintptr_t)GET_ARG_N(2, __VA_ARGS__), \
280 (uint32_t)(uintptr_t)GET_ARG_N(3, __VA_ARGS__))
291#define Z_LOG_MSG_SIMPLE_FUNC2(arg_cnt, _source, _level, ...) \
292 COND_CODE_0(arg_cnt, \
293 (z_log_msg_simple_create_0(_source, _level, GET_ARG_N(1, __VA_ARGS__))), \
294 (COND_CODE_1(arg_cnt, ( \
295 Z_LOG_MSG_SIMPLE_CREATE_1(_source, _level, __VA_ARGS__, dummy) \
297 Z_LOG_MSG_SIMPLE_CREATE_2(_source, _level, __VA_ARGS__, dummy, dummy) \
310#define LOG_MSG_SIMPLE_FUNC(_source, _level, ...) \
311 Z_LOG_MSG_SIMPLE_FUNC2(NUM_VA_ARGS_LESS_1(__VA_ARGS__), _source, _level, __VA_ARGS__)
323#define Z_LOG_MSG_SIMPLE_ARGS_CREATE(_domain_id, _source, _level, ...) \
324 IF_ENABLED(LOG_MSG_SIMPLE_ARG_CNT_CHECK(__VA_ARGS__), (\
325 LOG_MSG_SIMPLE_FUNC(_source, _level, __VA_ARGS__); \
328#define Z_LOG_MSG_STACK_CREATE(_cstr_cnt, _domain_id, _source, _level, _data, _dlen, ...) \
331 uint32_t _options = Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt) | \
332 CBPRINTF_PACKAGE_ADD_RW_STR_POS; \
333 if (GET_ARG_N(1, __VA_ARGS__) == NULL) { \
336 CBPRINTF_STATIC_PACKAGE(NULL, 0, _plen, Z_LOG_MSG_ALIGN_OFFSET, _options, \
339 TOOLCHAIN_IGNORE_WSHADOW_BEGIN \
340 struct log_msg *_msg; \
341 TOOLCHAIN_IGNORE_WSHADOW_END \
342 Z_LOG_MSG_ON_STACK_ALLOC(_msg, Z_LOG_MSG_LEN(_plen, 0)); \
343 Z_LOG_ARM64_VLA_PROTECT(); \
345 CBPRINTF_STATIC_PACKAGE(_msg->data, _plen, \
346 _plen, Z_LOG_MSG_ALIGN_OFFSET, _options, \
349 struct log_msg_desc _desc = \
350 Z_LOG_MSG_DESC_INITIALIZER(_domain_id, _level, \
351 (uint32_t)_plen, _dlen); \
352 LOG_MSG_DBG("creating message on stack: package len: %d, data len: %d\n", \
353 _plen, (int)(_dlen)); \
354 z_log_msg_static_create((void *)_source, _desc, _msg->data, _data); \
357#ifdef CONFIG_LOG_SPEED
358#define Z_LOG_MSG_SIMPLE_CREATE(_cstr_cnt, _domain_id, _source, _level, ...) do { \
360 CBPRINTF_STATIC_PACKAGE(NULL, 0, _plen, Z_LOG_MSG_ALIGN_OFFSET, \
361 Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt), \
363 size_t _msg_wlen = Z_LOG_MSG_ALIGNED_WLEN(_plen, 0); \
364 struct log_msg *_msg = z_log_msg_alloc(_msg_wlen); \
365 struct log_msg_desc _desc = \
366 Z_LOG_MSG_DESC_INITIALIZER(_domain_id, _level, (uint32_t)_plen, 0); \
367 LOG_MSG_DBG("creating message zero copy: package len: %d, msg: %p\n", \
370 CBPRINTF_STATIC_PACKAGE(_msg->data, _plen, _plen, \
371 Z_LOG_MSG_ALIGN_OFFSET, \
372 Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt), \
375 z_log_msg_finalize(_msg, (void *)_source, _desc, NULL); \
381#define Z_LOG_MSG_SIMPLE_CREATE(...)
387#define Z_LOG_FMT_ARGS_2(_name, ...) \
388 COND_CODE_1(CONFIG_LOG_FMT_SECTION, \
389 (COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
390 (_name), (_name, GET_ARGS_LESS_N(1, __VA_ARGS__)))), \
402#define Z_LOG_FMT_ARGS(_name, ...) \
403 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
405 (Z_LOG_FMT_ARGS_2(_name, ##__VA_ARGS__)))
407#if defined(CONFIG_LOG_USE_TAGGED_ARGUMENTS)
409#define Z_LOG_FMT_TAGGED_ARGS_2(_name, ...) \
410 COND_CODE_1(CONFIG_LOG_FMT_SECTION, \
411 (_name, Z_CBPRINTF_TAGGED_ARGS(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
412 GET_ARGS_LESS_N(1, __VA_ARGS__))), \
413 (GET_ARG_N(1, __VA_ARGS__), \
414 Z_CBPRINTF_TAGGED_ARGS(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
415 GET_ARGS_LESS_N(1, __VA_ARGS__))))
427#define Z_LOG_FMT_TAGGED_ARGS(_name, ...) \
428 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
429 (Z_CBPRINTF_TAGGED_ARGS(0)), \
430 (Z_LOG_FMT_TAGGED_ARGS_2(_name, ##__VA_ARGS__)))
432#define Z_LOG_FMT_RUNTIME_ARGS(...) \
433 Z_LOG_FMT_TAGGED_ARGS(__VA_ARGS__)
437#define Z_LOG_FMT_RUNTIME_ARGS(...) \
438 Z_LOG_FMT_ARGS(__VA_ARGS__)
445#define Z_LOG_MSG_STR_VAR_IN_SECTION(_name, ...) \
446 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
448 (static const char _name[] \
449 __in_section(_log_strings, static, _CONCAT(_name, _)) __used __noasan = \
450 GET_ARG_N(1, __VA_ARGS__);))
459#define Z_LOG_MSG_STR_VAR(_name, ...) \
460 IF_ENABLED(CONFIG_LOG_FMT_SECTION, \
461 (Z_LOG_MSG_STR_VAR_IN_SECTION(_name, ##__VA_ARGS__)))
502#if defined(CONFIG_LOG_ALWAYS_RUNTIME) || \
503 (!defined(CONFIG_LOG) && \
504 (!TOOLCHAIN_HAS_PRAGMA_DIAG || !TOOLCHAIN_HAS_C_AUTO_TYPE))
505#define Z_LOG_MSG_CREATE2(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source,\
506 _level, _data, _dlen, ...) \
508 Z_LOG_MSG_STR_VAR(_fmt, ##__VA_ARGS__) \
509 z_log_msg_runtime_create(_domain_id, (void *)_source, \
510 _level, (uint8_t *)_data, _dlen,\
511 Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt) | \
512 (IS_ENABLED(CONFIG_LOG_USE_TAGGED_ARGUMENTS) ? \
513 CBPRINTF_PACKAGE_ARGS_ARE_TAGGED : 0), \
514 Z_LOG_FMT_RUNTIME_ARGS(_fmt, ##__VA_ARGS__));\
515 _mode = Z_LOG_MSG_MODE_RUNTIME; \
518#define Z_LOG_MSG_CREATE3(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source,\
519 _level, _data, _dlen, ...) \
521 Z_LOG_MSG_STR_VAR(_fmt, ##__VA_ARGS__); \
522 bool has_rw_str = CBPRINTF_MUST_RUNTIME_PACKAGE( \
523 Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt), \
525 if (IS_ENABLED(CONFIG_LOG_SPEED) && _try_0cpy && ((_dlen) == 0) && !has_rw_str) {\
526 LOG_MSG_DBG("create zero-copy message\n");\
527 Z_LOG_MSG_SIMPLE_CREATE(_cstr_cnt, _domain_id, _source, \
528 _level, Z_LOG_FMT_ARGS(_fmt, ##__VA_ARGS__)); \
529 _mode = Z_LOG_MSG_MODE_ZERO_COPY; \
531 IF_ENABLED(UTIL_AND(IS_ENABLED(CONFIG_LOG_SIMPLE_MSG_OPTIMIZE), \
532 UTIL_AND(UTIL_NOT(_domain_id), UTIL_NOT(_cstr_cnt))), \
534 bool can_simple = LOG_MSG_SIMPLE_CHECK(__VA_ARGS__); \
535 if (can_simple && ((_dlen) == 0) && !k_is_user_context()) { \
536 LOG_MSG_DBG("create fast message\n");\
537 Z_LOG_MSG_SIMPLE_ARGS_CREATE(_domain_id, _source, _level, \
538 Z_LOG_FMT_ARGS(_fmt, ##__VA_ARGS__)); \
539 _mode = Z_LOG_MSG_MODE_SIMPLE; \
544 LOG_MSG_DBG("create on stack message\n");\
545 Z_LOG_MSG_STACK_CREATE(_cstr_cnt, _domain_id, _source, _level, _data, \
546 _dlen, Z_LOG_FMT_ARGS(_fmt, ##__VA_ARGS__)); \
547 _mode = Z_LOG_MSG_MODE_FROM_STACK; \
552#if defined(__cplusplus)
553#define Z_AUTO_TYPE auto
555#define Z_AUTO_TYPE __auto_type
561#define Z_LOG_LOCAL_ARG_NAME(idx, arg) COND_CODE_0(idx, (arg), (_v##idx))
564#define Z_LOG_LOCAL_ARG_CREATE(idx, arg) \
565 COND_CODE_0(idx, (), (Z_AUTO_TYPE Z_LOG_LOCAL_ARG_NAME(idx, arg) = (arg) + 0))
571#define Z_LOG_MSG_CREATE2(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source, \
572 _level, _data, _dlen, ...) \
574 _Pragma("GCC diagnostic push") \
575 _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
576 FOR_EACH_IDX(Z_LOG_LOCAL_ARG_CREATE, (;), __VA_ARGS__); \
577 _Pragma("GCC diagnostic pop") \
578 Z_LOG_MSG_CREATE3(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source,\
579 _level, _data, _dlen, \
580 FOR_EACH_IDX(Z_LOG_LOCAL_ARG_NAME, (,), __VA_ARGS__)); \
587#define Z_LOG_MSG_CREATE(_try_0cpy, _mode, _domain_id, _source,\
588 _level, _data, _dlen, ...) \
589 Z_LOG_MSG_CREATE2(_try_0cpy, _mode, UTIL_CAT(Z_LOG_FUNC_PREFIX_, _level), \
590 _domain_id, _source, _level, _data, _dlen, \
591 Z_LOG_STR(_level, __VA_ARGS__))
614void z_log_msg_finalize(
struct log_msg *msg,
const void *source,
623__syscall
void z_log_msg_simple_create_0(
const void *source,
uint32_t level,
633__syscall
void z_log_msg_simple_create_1(
const void *source,
uint32_t level,
644__syscall
void z_log_msg_simple_create_2(
const void *source,
uint32_t level,
657__syscall
void z_log_msg_static_create(
const void *source,
682void z_log_msg_runtime_vcreate(
uint8_t domain_id,
const void *source,
684 size_t dlen,
uint32_t package_flags,
709static inline void z_log_msg_runtime_create(
uint8_t domain_id,
712 size_t dlen,
uint32_t package_flags,
713 const char *fmt, ...)
718 z_log_msg_runtime_vcreate(domain_id, source, level,
719 data, dlen, package_flags, fmt, ap);
749 if (z_log_item_is_msg(generic_msg)) {
818#if defined(CONFIG_LOG_THREAD_ID_PREFIX)
860#include <syscalls/log_msg.h>
static const void * log_msg_get_source(struct log_msg *msg)
Get message source data.
Definition: log_msg.h:786
int16_t log_msg_get_source_id(struct log_msg *msg)
Get log message source ID.
static uint8_t log_msg_get_level(struct log_msg *msg)
Get log message level.
Definition: log_msg.h:775
static void * log_msg_get_tid(struct log_msg *msg)
Get Thread ID.
Definition: log_msg.h:816
static uint8_t log_msg_get_domain(struct log_msg *msg)
Get log message domain ID.
Definition: log_msg.h:764
static uint32_t log_msg_generic_get_wlen(const union mpsc_pbuf_generic *item)
Get length of the log item.
Definition: log_msg.h:745
#define LOG_MSG_GENERIC_HDR
Definition: log_msg.h:52
static uint32_t log_msg_get_total_wlen(const struct log_msg_desc desc)
Get total length (in 32 bit words) of a log message.
Definition: log_msg.h:734
static uint8_t * log_msg_get_package(struct log_msg *msg, size_t *len)
Get string package.
Definition: log_msg.h:849
static log_timestamp_t log_msg_get_timestamp(struct log_msg *msg)
Get timestamp.
Definition: log_msg.h:805
static uint8_t * log_msg_get_data(struct log_msg *msg, size_t *len)
Get data buffer.
Definition: log_msg.h:834
uint32_t log_timestamp_t
Definition: log_msg.h:36
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__INT16_TYPE__ int16_t
Definition: stdint.h:73
uint32_t domain
Definition: log_msg.h:58
uint32_t package_len
Definition: log_msg.h:60
uint32_t level
Definition: log_msg.h:59
uint32_t data_len
Definition: log_msg.h:61
Definition: log_msg.h:113
uint32_t type
Definition: log_msg.h:114
struct log_msg_desc desc
Definition: log_msg.h:71
log_timestamp_t timestamp
Definition: log_msg.h:80
const void * source
Definition: log_msg.h:79
uint8_t data[]
Definition: log_msg.h:100
uint8_t padding[((sizeof(struct log_msg_hdr) % CBPRINTF_PACKAGE_ALIGNMENT) > 0 ?(CBPRINTF_PACKAGE_ALIGNMENT -(sizeof(struct log_msg_hdr) % CBPRINTF_PACKAGE_ALIGNMENT)) :0)]
Definition: log_msg.h:99
struct log_msg_hdr hdr
Definition: log_msg.h:95
Constant data associated with the source of log messages.
Definition: log_instance.h:17
Dynamic data associated with the source of log messages.
Definition: log_instance.h:30
Definition: log_msg.h:117
union mpsc_pbuf_generic buf
Definition: log_msg.h:118
struct log_msg log
Definition: log_msg.h:120
struct log_msg_generic_hdr generic
Definition: log_msg.h:119
const struct log_source_const_data * fixed
Definition: log_msg.h:65
struct log_source_dynamic_data * dynamic
Definition: log_msg.h:66
void * raw
Definition: log_msg.h:67
Generic packet header.
Definition: mpsc_packet.h:49