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) && !CONFIG_LOG_TIMESTAMP_64BIT
82#if CONFIG_LOG_THREAD_ID_PREFIX
88#define Z_LOG_MSG_ALIGNMENT CBPRINTF_PACKAGE_ALIGNMENT
90#define Z_LOG_MSG_PADDING \
91 ((sizeof(struct log_msg_hdr) % Z_LOG_MSG_ALIGNMENT) > 0 ? \
92 (Z_LOG_MSG_ALIGNMENT - (sizeof(struct log_msg_hdr) % Z_LOG_MSG_ALIGNMENT)) : \
107BUILD_ASSERT(
sizeof(
struct log_msg) % Z_LOG_MSG_ALIGNMENT == 0,
108 "Log msg size must aligned");
132 Z_LOG_MSG_MODE_RUNTIME,
137 Z_LOG_MSG_MODE_FROM_STACK,
143 Z_LOG_MSG_MODE_ZERO_COPY,
146#define Z_LOG_MSG_DESC_INITIALIZER(_domain_id, _level, _plen, _dlen) \
150 .type = Z_LOG_MSG_LOG, \
151 .domain = _domain_id, \
153 .package_len = _plen, \
157#define Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt) \
158 (CBPRINTF_PACKAGE_FIRST_RO_STR_CNT(_cstr_cnt) | \
159 (IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC) ? \
160 CBPRINTF_PACKAGE_ADD_STRING_IDXS : 0))
162#ifdef CONFIG_LOG_USE_VLA
163#define Z_LOG_MSG_ON_STACK_ALLOC(ptr, len) \
164 long long _ll_buf[DIV_ROUND_UP(len, sizeof(long long))]; \
165 long double _ld_buf[DIV_ROUND_UP(len, sizeof(long double))]; \
166 ptr = (sizeof(long double) == Z_LOG_MSG_ALIGNMENT) ? \
167 (struct log_msg *)_ld_buf : (struct log_msg *)_ll_buf; \
168 if (IS_ENABLED(CONFIG_LOG_TEST_CLEAR_MESSAGE_SPACE)) { \
170 memset(ptr, 0, len); \
178#define Z_LOG_MSG_ON_STACK_ALLOC(ptr, len) \
179 long long _ll_buf32[32 / sizeof(long long)]; \
180 long long _ll_buf48[48 / sizeof(long long)]; \
181 long long _ll_buf64[64 / sizeof(long long)]; \
182 long long _ll_buf128[128 / sizeof(long long)]; \
183 long long _ll_buf256[256 / sizeof(long long)]; \
184 long double _ld_buf32[32 / sizeof(long double)]; \
185 long double _ld_buf48[48 / sizeof(long double)]; \
186 long double _ld_buf64[64 / sizeof(long double)]; \
187 long double _ld_buf128[128 / sizeof(long double)]; \
188 long double _ld_buf256[256 / sizeof(long double)]; \
189 if (sizeof(long double) == Z_LOG_MSG_ALIGNMENT) { \
190 ptr = (len > 128) ? (struct log_msg *)_ld_buf256 : \
191 ((len > 64) ? (struct log_msg *)_ld_buf128 : \
192 ((len > 48) ? (struct log_msg *)_ld_buf64 : \
193 ((len > 32) ? (struct log_msg *)_ld_buf48 : \
194 (struct log_msg *)_ld_buf32)));\
196 ptr = (len > 128) ? (struct log_msg *)_ll_buf256 : \
197 ((len > 64) ? (struct log_msg *)_ll_buf128 : \
198 ((len > 48) ? (struct log_msg *)_ll_buf64 : \
199 ((len > 32) ? (struct log_msg *)_ll_buf48 : \
200 (struct log_msg *)_ll_buf32)));\
202 if (IS_ENABLED(CONFIG_LOG_TEST_CLEAR_MESSAGE_SPACE)) { \
204 memset(ptr, 0, len); \
208#define Z_LOG_MSG_ALIGN_OFFSET \
209 offsetof(struct log_msg, data)
211#define Z_LOG_MSG_LEN(pkg_len, data_len) \
212 (offsetof(struct log_msg, data) + pkg_len + (data_len))
214#define Z_LOG_MSG_ALIGNED_WLEN(pkg_len, data_len) \
215 DIV_ROUND_UP(ROUND_UP(Z_LOG_MSG_LEN(pkg_len, data_len), \
216 Z_LOG_MSG_ALIGNMENT), \
228#define Z_LOG_ARM64_VLA_PROTECT() compiler_barrier()
230#define Z_LOG_MSG_STACK_CREATE(_cstr_cnt, _domain_id, _source, _level, _data, _dlen, ...) \
233 uint32_t _options = Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt) | \
234 CBPRINTF_PACKAGE_ADD_RW_STR_POS; \
235 if (GET_ARG_N(1, __VA_ARGS__) == NULL) { \
238 CBPRINTF_STATIC_PACKAGE(NULL, 0, _plen, Z_LOG_MSG_ALIGN_OFFSET, _options, \
241 TOOLCHAIN_IGNORE_WSHADOW_BEGIN \
242 struct log_msg *_msg; \
243 TOOLCHAIN_IGNORE_WSHADOW_END \
244 Z_LOG_MSG_ON_STACK_ALLOC(_msg, Z_LOG_MSG_LEN(_plen, 0)); \
245 Z_LOG_ARM64_VLA_PROTECT(); \
247 CBPRINTF_STATIC_PACKAGE(_msg->data, _plen, \
248 _plen, Z_LOG_MSG_ALIGN_OFFSET, _options, \
251 struct log_msg_desc _desc = \
252 Z_LOG_MSG_DESC_INITIALIZER(_domain_id, _level, \
253 (uint32_t)_plen, _dlen); \
254 LOG_MSG_DBG("creating message on stack: package len: %d, data len: %d\n", \
255 _plen, (int)(_dlen)); \
256 z_log_msg_static_create((void *)_source, _desc, _msg->data, _data); \
259#ifdef CONFIG_LOG_SPEED
260#define Z_LOG_MSG_SIMPLE_CREATE(_cstr_cnt, _domain_id, _source, _level, ...) do { \
262 CBPRINTF_STATIC_PACKAGE(NULL, 0, _plen, Z_LOG_MSG_ALIGN_OFFSET, \
263 Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt), \
265 size_t _msg_wlen = Z_LOG_MSG_ALIGNED_WLEN(_plen, 0); \
266 struct log_msg *_msg = z_log_msg_alloc(_msg_wlen); \
267 struct log_msg_desc _desc = \
268 Z_LOG_MSG_DESC_INITIALIZER(_domain_id, _level, (uint32_t)_plen, 0); \
269 LOG_MSG_DBG("creating message zero copy: package len: %d, msg: %p\n", \
272 CBPRINTF_STATIC_PACKAGE(_msg->data, _plen, _plen, \
273 Z_LOG_MSG_ALIGN_OFFSET, \
274 Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt), \
277 z_log_msg_finalize(_msg, (void *)_source, _desc, NULL); \
283#define Z_LOG_MSG_SIMPLE_CREATE(...)
289#define Z_LOG_FMT_ARGS_2(_name, ...) \
290 COND_CODE_1(CONFIG_LOG_FMT_SECTION, \
291 (COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
292 (_name), (_name, GET_ARGS_LESS_N(1, __VA_ARGS__)))), \
304#define Z_LOG_FMT_ARGS(_name, ...) \
305 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
307 (Z_LOG_FMT_ARGS_2(_name, ##__VA_ARGS__)))
309#if defined(CONFIG_LOG_USE_TAGGED_ARGUMENTS)
311#define Z_LOG_FMT_TAGGED_ARGS_2(_name, ...) \
312 COND_CODE_1(CONFIG_LOG_FMT_SECTION, \
313 (_name, Z_CBPRINTF_TAGGED_ARGS(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
314 GET_ARGS_LESS_N(1, __VA_ARGS__))), \
315 (GET_ARG_N(1, __VA_ARGS__), \
316 Z_CBPRINTF_TAGGED_ARGS(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
317 GET_ARGS_LESS_N(1, __VA_ARGS__))))
329#define Z_LOG_FMT_TAGGED_ARGS(_name, ...) \
330 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
331 (Z_CBPRINTF_TAGGED_ARGS(0)), \
332 (Z_LOG_FMT_TAGGED_ARGS_2(_name, ##__VA_ARGS__)))
334#define Z_LOG_FMT_RUNTIME_ARGS(...) \
335 Z_LOG_FMT_TAGGED_ARGS(__VA_ARGS__)
339#define Z_LOG_FMT_RUNTIME_ARGS(...) \
340 Z_LOG_FMT_ARGS(__VA_ARGS__)
347#define Z_LOG_MSG_STR_VAR_IN_SECTION(_name, ...) \
348 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
350 (static const char _name[] \
351 __in_section(_log_strings, static, _CONCAT(_name, _)) __used __noasan = \
352 GET_ARG_N(1, __VA_ARGS__);))
361#define Z_LOG_MSG_STR_VAR(_name, ...) \
362 IF_ENABLED(CONFIG_LOG_FMT_SECTION, \
363 (Z_LOG_MSG_STR_VAR_IN_SECTION(_name, ##__VA_ARGS__)))
404#if defined(CONFIG_LOG_ALWAYS_RUNTIME) || \
405 (!defined(CONFIG_LOG) && \
406 (!TOOLCHAIN_HAS_PRAGMA_DIAG || !TOOLCHAIN_HAS_C_AUTO_TYPE))
407#define Z_LOG_MSG_CREATE2(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source,\
408 _level, _data, _dlen, ...) \
410 Z_LOG_MSG_STR_VAR(_fmt, ##__VA_ARGS__) \
411 z_log_msg_runtime_create(_domain_id, (void *)_source, \
412 _level, (uint8_t *)_data, _dlen,\
413 Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt) | \
414 (IS_ENABLED(CONFIG_LOG_USE_TAGGED_ARGUMENTS) ? \
415 CBPRINTF_PACKAGE_ARGS_ARE_TAGGED : 0), \
416 Z_LOG_FMT_RUNTIME_ARGS(_fmt, ##__VA_ARGS__));\
417 _mode = Z_LOG_MSG_MODE_RUNTIME; \
420#define Z_LOG_MSG_CREATE3(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source,\
421 _level, _data, _dlen, ...) \
423 Z_LOG_MSG_STR_VAR(_fmt, ##__VA_ARGS__); \
424 bool has_rw_str = CBPRINTF_MUST_RUNTIME_PACKAGE( \
425 Z_LOG_MSG_CBPRINTF_FLAGS(_cstr_cnt), \
427 if (IS_ENABLED(CONFIG_LOG_SPEED) && _try_0cpy && ((_dlen) == 0) && !has_rw_str) {\
428 LOG_MSG_DBG("create zero-copy message\n");\
429 Z_LOG_MSG_SIMPLE_CREATE(_cstr_cnt, _domain_id, _source, \
430 _level, Z_LOG_FMT_ARGS(_fmt, ##__VA_ARGS__)); \
431 _mode = Z_LOG_MSG_MODE_ZERO_COPY; \
433 LOG_MSG_DBG("create on stack message\n");\
434 Z_LOG_MSG_STACK_CREATE(_cstr_cnt, _domain_id, _source, _level, _data, \
435 _dlen, Z_LOG_FMT_ARGS(_fmt, ##__VA_ARGS__)); \
436 _mode = Z_LOG_MSG_MODE_FROM_STACK; \
441#if defined(__cplusplus)
442#define Z_AUTO_TYPE auto
444#define Z_AUTO_TYPE __auto_type
450#define Z_LOG_LOCAL_ARG_NAME(idx, arg) COND_CODE_0(idx, (arg), (_v##idx))
453#define Z_LOG_LOCAL_ARG_CREATE(idx, arg) \
454 COND_CODE_0(idx, (), (Z_AUTO_TYPE Z_LOG_LOCAL_ARG_NAME(idx, arg) = (arg) + 0))
460#define Z_LOG_MSG_CREATE2(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source, \
461 _level, _data, _dlen, ...) \
463 _Pragma("GCC diagnostic push") \
464 _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
465 FOR_EACH_IDX(Z_LOG_LOCAL_ARG_CREATE, (;), __VA_ARGS__); \
466 _Pragma("GCC diagnostic pop") \
467 Z_LOG_MSG_CREATE3(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source,\
468 _level, _data, _dlen, \
469 FOR_EACH_IDX(Z_LOG_LOCAL_ARG_NAME, (,), __VA_ARGS__)); \
476#define Z_LOG_MSG_CREATE(_try_0cpy, _mode, _domain_id, _source,\
477 _level, _data, _dlen, ...) \
478 Z_LOG_MSG_CREATE2(_try_0cpy, _mode, UTIL_CAT(Z_LOG_FUNC_PREFIX_, _level), \
479 _domain_id, _source, _level, _data, _dlen, \
480 Z_LOG_STR(_level, __VA_ARGS__))
503void z_log_msg_finalize(
struct log_msg *msg,
const void *source,
516__syscall
void z_log_msg_static_create(
const void *source,
541__syscall
void z_log_msg_runtime_vcreate(
uint8_t domain_id,
const void *source,
543 size_t dlen,
uint32_t package_flags,
568static inline void z_log_msg_runtime_create(
uint8_t domain_id,
571 size_t dlen,
uint32_t package_flags,
572 const char *fmt, ...)
577 z_log_msg_runtime_vcreate(domain_id, source, level,
578 data, dlen, package_flags, fmt, ap);
608 if (z_log_item_is_msg(generic_msg)) {
669#if CONFIG_LOG_THREAD_ID_PREFIX
static const void * log_msg_get_source(struct log_msg *msg)
Get message source data.
Definition: log_msg.h:645
static uint8_t log_msg_get_level(struct log_msg *msg)
Get log message level.
Definition: log_msg.h:634
static void * log_msg_get_tid(struct log_msg *msg)
Get Thread ID.
Definition: log_msg.h:667
static uint8_t log_msg_get_domain(struct log_msg *msg)
Get log message domain ID.
Definition: log_msg.h:623
static uint32_t log_msg_generic_get_wlen(const union mpsc_pbuf_generic *item)
Get length of the log item.
Definition: log_msg.h:604
#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:593
static uint8_t * log_msg_get_package(struct log_msg *msg, size_t *len)
Get string package.
Definition: log_msg.h:700
static log_timestamp_t log_msg_get_timestamp(struct log_msg *msg)
Get timestamp.
Definition: log_msg.h:656
static uint8_t * log_msg_get_data(struct log_msg *msg, size_t *len)
Get data buffer.
Definition: log_msg.h:685
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
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:114
uint32_t type
Definition: log_msg.h:115
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:101
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:100
struct log_msg_hdr hdr
Definition: log_msg.h:96
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:118
union mpsc_pbuf_generic buf
Definition: log_msg.h:119
struct log_msg log
Definition: log_msg.h:121
struct log_msg_generic_hdr generic
Definition: log_msg.h:120
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