Zephyr Project API  3.1.0
A Scalable Open Source RTOS
log_msg2.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef ZEPHYR_INCLUDE_LOGGING_LOG_MSG2_H_
7#define ZEPHYR_INCLUDE_LOGGING_LOG_MSG2_H_
8
11#include <zephyr/sys/cbprintf.h>
12#include <zephyr/sys/atomic.h>
13#include <zephyr/sys/util.h>
14#include <string.h>
15#include <zephyr/toolchain.h>
16
17#ifdef __GNUC__
18#ifndef alloca
19#define alloca __builtin_alloca
20#endif
21#else
22#include <alloca.h>
23#endif
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#define LOG_MSG2_DEBUG 0
30#define LOG_MSG2_DBG(...) IF_ENABLED(LOG_MSG2_DEBUG, (printk(__VA_ARGS__)))
31
32#ifdef CONFIG_LOG_TIMESTAMP_64BIT
34#else
36#endif
37
45#define Z_LOG_MSG2_LOG 0
46
47#define LOG_MSG2_GENERIC_HDR \
48 MPSC_PBUF_HDR;\
49 uint32_t type:1
50
58};
59
63 void *raw;
64};
65
68/* Attempting to keep best alignment. When address is 64 bit and timestamp 32
69 * swap the order to have 16 byte header instead of 24 byte.
70 */
71#if (INTPTR_MAX > INT32_MAX) && !CONFIG_LOG_TIMESTAMP_64BIT
73 const void *source;
74#else
75 const void *source;
77#endif
78};
79
80/* Messages are aligned to alignment required by cbprintf package. */
81#define Z_LOG_MSG2_ALIGNMENT CBPRINTF_PACKAGE_ALIGNMENT
82
83#define Z_LOG_MSG2_PADDING \
84 ((sizeof(struct log_msg2_hdr) % Z_LOG_MSG2_ALIGNMENT) > 0 ? \
85 (Z_LOG_MSG2_ALIGNMENT - (sizeof(struct log_msg2_hdr) % Z_LOG_MSG2_ALIGNMENT)) : \
86 0)
87
88struct log_msg2 {
90 /* Adding padding to ensure that cbprintf package that follows is
91 * properly aligned.
92 */
93 uint8_t padding[Z_LOG_MSG2_PADDING];
95};
96
99};
100
104 struct log_msg2 log;
105};
106
111enum z_log_msg2_mode {
112 /* Runtime mode is least efficient but supports all cases thus it is
113 * treated as a fallback method when others cannot be used.
114 */
115 Z_LOG_MSG2_MODE_RUNTIME,
116 /* Mode creates statically a string package on stack and calls a
117 * function for creating a message. It takes code size than
118 * Z_LOG_MSG2_MODE_ZERO_COPY but is a bit slower.
119 */
120 Z_LOG_MSG2_MODE_FROM_STACK,
121
122 /* Mode calculates size of the message and allocates it and writes
123 * directly to the message space. It is the fastest method but requires
124 * more code size.
125 */
126 Z_LOG_MSG2_MODE_ZERO_COPY,
127};
128
129#define Z_LOG_MSG_DESC_INITIALIZER(_domain_id, _level, _plen, _dlen) \
130{ \
131 .valid = 0, \
132 .busy = 0, \
133 .type = Z_LOG_MSG2_LOG, \
134 .domain = _domain_id, \
135 .level = _level, \
136 .package_len = _plen, \
137 .data_len = _dlen, \
138 .reserved = 0, \
139}
140
141#define Z_LOG_MSG2_CBPRINTF_FLAGS(_cstr_cnt) \
142 (CBPRINTF_PACKAGE_FIRST_RO_STR_CNT(_cstr_cnt) | \
143 (IS_ENABLED(CONFIG_LOG2_MSG_PKG_ALWAYS_ADD_RO_STRING_IDXS) ? \
144 CBPRINTF_PACKAGE_ADD_STRING_IDXS : 0))
145
146#ifdef CONFIG_LOG2_USE_VLA
147#define Z_LOG_MSG2_ON_STACK_ALLOC(ptr, len) \
148 long long _ll_buf[ceiling_fraction(len, sizeof(long long))]; \
149 long double _ld_buf[ceiling_fraction(len, sizeof(long double))]; \
150 ptr = (sizeof(long double) == Z_LOG_MSG2_ALIGNMENT) ? \
151 (struct log_msg2 *)_ld_buf : (struct log_msg2 *)_ll_buf; \
152 if (IS_ENABLED(CONFIG_LOG_TEST_CLEAR_MESSAGE_SPACE)) { \
153 /* During test fill with 0's to simplify message comparison */ \
154 memset(ptr, 0, len); \
155 }
156#else /* Z_LOG_MSG2_USE_VLA */
157/* When VLA cannot be used we need to trick compiler a bit and create multiple
158 * fixed size arrays and take the smallest one that will fit the message.
159 * Compiler will remove unused arrays and stack usage will be kept similar
160 * to vla case, rounded to the size of the used buffer.
161 */
162#define Z_LOG_MSG2_ON_STACK_ALLOC(ptr, len) \
163 long long _ll_buf32[32 / sizeof(long long)]; \
164 long long _ll_buf48[48 / sizeof(long long)]; \
165 long long _ll_buf64[64 / sizeof(long long)]; \
166 long long _ll_buf128[128 / sizeof(long long)]; \
167 long long _ll_buf256[256 / sizeof(long long)]; \
168 long double _ld_buf32[32 / sizeof(long double)]; \
169 long double _ld_buf48[48 / sizeof(long double)]; \
170 long double _ld_buf64[64 / sizeof(long double)]; \
171 long double _ld_buf128[128 / sizeof(long double)]; \
172 long double _ld_buf256[256 / sizeof(long double)]; \
173 if (sizeof(long double) == Z_LOG_MSG2_ALIGNMENT) { \
174 ptr = (len > 128) ? (struct log_msg2 *)_ld_buf256 : \
175 ((len > 64) ? (struct log_msg2 *)_ld_buf128 : \
176 ((len > 48) ? (struct log_msg2 *)_ld_buf64 : \
177 ((len > 32) ? (struct log_msg2 *)_ld_buf48 : \
178 (struct log_msg2 *)_ld_buf32)));\
179 } else { \
180 ptr = (len > 128) ? (struct log_msg2 *)_ll_buf256 : \
181 ((len > 64) ? (struct log_msg2 *)_ll_buf128 : \
182 ((len > 48) ? (struct log_msg2 *)_ll_buf64 : \
183 ((len > 32) ? (struct log_msg2 *)_ll_buf48 : \
184 (struct log_msg2 *)_ll_buf32)));\
185 } \
186 if (IS_ENABLED(CONFIG_LOG_TEST_CLEAR_MESSAGE_SPACE)) { \
187 /* During test fill with 0's to simplify message comparison */ \
188 memset(ptr, 0, len); \
189 }
190#endif /* Z_LOG_MSG2_USE_VLA */
191
192#define Z_LOG_MSG2_ALIGN_OFFSET \
193 offsetof(struct log_msg2, data)
194
195#define Z_LOG_MSG2_LEN(pkg_len, data_len) \
196 (offsetof(struct log_msg2, data) + pkg_len + (data_len))
197
198#define Z_LOG_MSG2_ALIGNED_WLEN(pkg_len, data_len) \
199 ceiling_fraction(ROUND_UP(Z_LOG_MSG2_LEN(pkg_len, data_len), \
200 Z_LOG_MSG2_ALIGNMENT), \
201 sizeof(uint32_t))
202
203#define Z_LOG_MSG2_STACK_CREATE(_cstr_cnt, _domain_id, _source, _level, _data, _dlen, ...) \
204do { \
205 int _plen; \
206 uint32_t flags = Z_LOG_MSG2_CBPRINTF_FLAGS(_cstr_cnt) | \
207 CBPRINTF_PACKAGE_ADD_RW_STR_POS; \
208 if (GET_ARG_N(1, __VA_ARGS__) == NULL) { \
209 _plen = 0; \
210 } else { \
211 CBPRINTF_STATIC_PACKAGE(NULL, 0, _plen, Z_LOG_MSG2_ALIGN_OFFSET, flags, \
212 __VA_ARGS__); \
213 } \
214 struct log_msg2 *_msg; \
215 Z_LOG_MSG2_ON_STACK_ALLOC(_msg, Z_LOG_MSG2_LEN(_plen, 0)); \
216 if (_plen) { \
217 CBPRINTF_STATIC_PACKAGE(_msg->data, _plen, \
218 _plen, Z_LOG_MSG2_ALIGN_OFFSET, flags, \
219 __VA_ARGS__);\
220 } \
221 struct log_msg2_desc _desc = \
222 Z_LOG_MSG_DESC_INITIALIZER(_domain_id, _level, \
223 (uint32_t)_plen, _dlen); \
224 LOG_MSG2_DBG("creating message on stack: package len: %d, data len: %d\n", \
225 _plen, (int)(_dlen)); \
226 z_log_msg2_static_create((void *)_source, _desc, _msg->data, _data); \
227} while (0)
228
229#ifdef CONFIG_LOG_SPEED
230#define Z_LOG_MSG2_SIMPLE_CREATE(_cstr_cnt, _domain_id, _source, _level, ...) do { \
231 int _plen; \
232 CBPRINTF_STATIC_PACKAGE(NULL, 0, _plen, Z_LOG_MSG2_ALIGN_OFFSET, \
233 Z_LOG_MSG2_CBPRINTF_FLAGS(_cstr_cnt), \
234 __VA_ARGS__); \
235 size_t _msg_wlen = Z_LOG_MSG2_ALIGNED_WLEN(_plen, 0); \
236 struct log_msg2 *_msg = z_log_msg2_alloc(_msg_wlen); \
237 struct log_msg2_desc _desc = \
238 Z_LOG_MSG_DESC_INITIALIZER(_domain_id, _level, (uint32_t)_plen, 0); \
239 LOG_MSG2_DBG("creating message zero copy: package len: %d, msg: %p\n", \
240 _plen, _msg); \
241 if (_msg) { \
242 CBPRINTF_STATIC_PACKAGE(_msg->data, _plen, _plen, \
243 Z_LOG_MSG2_ALIGN_OFFSET, \
244 Z_LOG_MSG2_CBPRINTF_FLAGS(_cstr_cnt), \
245 __VA_ARGS__); \
246 } \
247 z_log_msg2_finalize(_msg, (void *)_source, _desc, NULL); \
248} while (0)
249#else
250/* Alternative empty macro created to speed up compilation when LOG_SPEED is
251 * disabled (default).
252 */
253#define Z_LOG_MSG2_SIMPLE_CREATE(...)
254#endif
255
256/* Macro handles case when local variable with log message string is created. It
257 * replaces original string literal with that variable.
258 */
259#define Z_LOG_FMT_ARGS_2(_name, ...) \
260 COND_CODE_1(CONFIG_LOG2_FMT_SECTION, \
261 (COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
262 (_name), (_name, GET_ARGS_LESS_N(1, __VA_ARGS__)))), \
263 (__VA_ARGS__))
264
274#define Z_LOG_FMT_ARGS(_name, ...) \
275 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
276 (NULL), \
277 (Z_LOG_FMT_ARGS_2(_name, ##__VA_ARGS__)))
278
279/* Macro handles case when there is no string provided, in that case variable
280 * is not created.
281 */
282#define Z_LOG_MSG2_STR_VAR_IN_SECTION(_name, ...) \
283 COND_CODE_0(NUM_VA_ARGS_LESS_1(_, ##__VA_ARGS__), \
284 (/* No args provided, no variable */), \
285 (static const char _name[] \
286 __attribute__((__section__(".log_strings"))) = \
287 GET_ARG_N(1, __VA_ARGS__);))
288
296#define Z_LOG_MSG2_STR_VAR(_name, ...) \
297 IF_ENABLED(CONFIG_LOG2_FMT_SECTION, \
298 (Z_LOG_MSG2_STR_VAR_IN_SECTION(_name, ##__VA_ARGS__)))
299
339#if defined(CONFIG_LOG2_ALWAYS_RUNTIME) || \
340 (!defined(CONFIG_LOG) && \
341 (!TOOLCHAIN_HAS_PRAGMA_DIAG || !TOOLCHAIN_HAS_C_AUTO_TYPE))
342#define Z_LOG_MSG2_CREATE2(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source,\
343 _level, _data, _dlen, ...) \
344do {\
345 Z_LOG_MSG2_STR_VAR(_fmt, ##__VA_ARGS__) \
346 z_log_msg2_runtime_create(_domain_id, (void *)_source, \
347 _level, (uint8_t *)_data, _dlen,\
348 Z_LOG_MSG2_CBPRINTF_FLAGS(_cstr_cnt), \
349 Z_LOG_FMT_ARGS(_fmt, ##__VA_ARGS__));\
350 _mode = Z_LOG_MSG2_MODE_RUNTIME; \
351} while (0)
352#else /* CONFIG_LOG2_ALWAYS_RUNTIME */
353#define Z_LOG_MSG2_CREATE3(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source,\
354 _level, _data, _dlen, ...) \
355do { \
356 Z_LOG_MSG2_STR_VAR(_fmt, ##__VA_ARGS__); \
357 bool has_rw_str = CBPRINTF_MUST_RUNTIME_PACKAGE( \
358 Z_LOG_MSG2_CBPRINTF_FLAGS(_cstr_cnt), \
359 __VA_ARGS__); \
360 if (IS_ENABLED(CONFIG_LOG_SPEED) && _try_0cpy && ((_dlen) == 0) && !has_rw_str) {\
361 LOG_MSG2_DBG("create zero-copy message\n");\
362 Z_LOG_MSG2_SIMPLE_CREATE(_cstr_cnt, _domain_id, _source, \
363 _level, Z_LOG_FMT_ARGS(_fmt, ##__VA_ARGS__)); \
364 _mode = Z_LOG_MSG2_MODE_ZERO_COPY; \
365 } else { \
366 LOG_MSG2_DBG("create on stack message\n");\
367 Z_LOG_MSG2_STACK_CREATE(_cstr_cnt, _domain_id, _source, _level, _data, \
368 _dlen, Z_LOG_FMT_ARGS(_fmt, ##__VA_ARGS__)); \
369 _mode = Z_LOG_MSG2_MODE_FROM_STACK; \
370 } \
371 (void)_mode; \
372} while (0)
373
374#if defined(__cplusplus)
375#define Z_AUTO_TYPE auto
376#else
377#define Z_AUTO_TYPE __auto_type
378#endif
379
380/* Macro for getting name of a local variable with the exception of the first argument
381 * which is a formatted string in log message.
382 */
383#define Z_LOG_LOCAL_ARG_NAME(idx, arg) COND_CODE_0(idx, (arg), (_v##idx))
384
385/* Create local variable from input variable (expect for the first (fmt) argument). */
386#define Z_LOG_LOCAL_ARG_CREATE(idx, arg) \
387 COND_CODE_0(idx, (), (Z_AUTO_TYPE Z_LOG_LOCAL_ARG_NAME(idx, arg) = (arg) + 0))
388
389/* First level of processing creates stack variables to be passed for further processing.
390 * This is done to prevent multiple evaluations of input arguments (in case argument
391 * evaluation has side effects, e.g. it is a non-pure function call).
392 */
393#define Z_LOG_MSG2_CREATE2(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source, \
394 _level, _data, _dlen, ...) \
395do { \
396 _Pragma("GCC diagnostic push") \
397 _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"") \
398 FOR_EACH_IDX(Z_LOG_LOCAL_ARG_CREATE, (;), __VA_ARGS__); \
399 _Pragma("GCC diagnostic pop") \
400 Z_LOG_MSG2_CREATE3(_try_0cpy, _mode, _cstr_cnt, _domain_id, _source,\
401 _level, _data, _dlen, \
402 FOR_EACH_IDX(Z_LOG_LOCAL_ARG_NAME, (,), __VA_ARGS__)); \
403} while (0)
404#endif /* CONFIG_LOG2_ALWAYS_RUNTIME ||
405 * (!LOG && (!TOOLCHAIN_HAS_PRAGMA_DIAG || !TOOLCHAIN_HAS_C_AUTO_TYPE))
406 */
407
408
409#define Z_LOG_MSG2_CREATE(_try_0cpy, _mode, _domain_id, _source,\
410 _level, _data, _dlen, ...) \
411 Z_LOG_MSG2_CREATE2(_try_0cpy, _mode, UTIL_CAT(Z_LOG_FUNC_PREFIX_, _level), \
412 _domain_id, _source, _level, _data, _dlen, \
413 Z_LOG_STR(_level, __VA_ARGS__))
414
421struct log_msg2 *z_log_msg2_alloc(uint32_t wlen);
422
436void z_log_msg2_finalize(struct log_msg2 *msg, const void *source,
437 const struct log_msg2_desc desc, const void *data);
438
449__syscall void z_log_msg2_static_create(const void *source,
450 const struct log_msg2_desc desc,
451 uint8_t *package, const void *data);
452
474__syscall void z_log_msg2_runtime_vcreate(uint8_t domain_id, const void *source,
475 uint8_t level, const void *data,
476 size_t dlen, uint32_t package_flags,
477 const char *fmt,
478 va_list ap);
479
501static inline void z_log_msg2_runtime_create(uint8_t domain_id,
502 const void *source,
503 uint8_t level, const void *data,
504 size_t dlen, uint32_t package_flags,
505 const char *fmt, ...)
506{
507 va_list ap;
508
509 va_start(ap, fmt);
510 z_log_msg2_runtime_vcreate(domain_id, source, level,
511 data, dlen, package_flags, fmt, ap);
512 va_end(ap);
513}
514
515static inline bool z_log_item_is_msg(const union log_msg2_generic *msg)
516{
517 return msg->generic.type == Z_LOG_MSG2_LOG;
518}
519
526static inline uint32_t log_msg2_get_total_wlen(const struct log_msg2_desc desc)
527{
528 return Z_LOG_MSG2_ALIGNED_WLEN(desc.package_len, desc.data_len);
529}
530
538{
539 const union log_msg2_generic *generic_msg = (const union log_msg2_generic *)item;
540
541 if (z_log_item_is_msg(generic_msg)) {
542 const struct log_msg2 *msg = (const struct log_msg2 *)generic_msg;
543
544 return log_msg2_get_total_wlen(msg->hdr.desc);
545 }
546
547 return 0;
548}
549
557{
558 return msg->hdr.desc.domain;
559}
560
568{
569 return msg->hdr.desc.level;
570}
571
578static inline const void *log_msg2_get_source(struct log_msg2 *msg)
579{
580 return msg->hdr.source;
581}
582
590{
591 return msg->hdr.timestamp;
592}
593
602static inline uint8_t *log_msg2_get_data(struct log_msg2 *msg, size_t *len)
603{
604 *len = msg->hdr.desc.data_len;
605
606 return msg->data + msg->hdr.desc.package_len;
607}
608
617static inline uint8_t *log_msg2_get_package(struct log_msg2 *msg, size_t *len)
618{
619 *len = msg->hdr.desc.package_len;
620
621 return msg->data;
622}
623
628#include <syscalls/log_msg2.h>
629
630#ifdef __cplusplus
631}
632#endif
633
634#endif /* ZEPHYR_INCLUDE_LOGGING_LOG_MSG2_H_ */
static const void * log_msg2_get_source(struct log_msg2 *msg)
Get message source data.
Definition: log_msg2.h:578
static uint8_t log_msg2_get_level(struct log_msg2 *msg)
Get log message level.
Definition: log_msg2.h:567
static log_timestamp_t log_msg2_get_timestamp(struct log_msg2 *msg)
Get timestamp.
Definition: log_msg2.h:589
#define LOG_MSG2_GENERIC_HDR
Definition: log_msg2.h:47
static uint8_t * log_msg2_get_package(struct log_msg2 *msg, size_t *len)
Get string package.
Definition: log_msg2.h:617
static uint8_t * log_msg2_get_data(struct log_msg2 *msg, size_t *len)
Get data buffer.
Definition: log_msg2.h:602
static uint32_t log_msg2_generic_get_wlen(const union mpsc_pbuf_generic *item)
Get length of the log item.
Definition: log_msg2.h:537
static uint8_t log_msg2_get_domain(struct log_msg2 *msg)
Get log message domain ID.
Definition: log_msg2.h:556
static uint32_t log_msg2_get_total_wlen(const struct log_msg2_desc desc)
Get total length (in 32 bit words) of a log message.
Definition: log_msg2.h:526
uint32_t log_timestamp_t
Definition: log_msg2.h:35
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
Definition: log_msg2.h:51
uint32_t reserved
Definition: log_msg2.h:57
uint32_t domain
Definition: log_msg2.h:53
uint32_t level
Definition: log_msg2.h:54
uint32_t data_len
Definition: log_msg2.h:56
uint32_t package_len
Definition: log_msg2.h:55
Definition: log_msg2.h:97
Definition: log_msg2.h:66
log_timestamp_t timestamp
Definition: log_msg2.h:76
struct log_msg2_desc desc
Definition: log_msg2.h:67
const void * source
Definition: log_msg2.h:75
Definition: log_msg2.h:88
struct log_msg2_hdr hdr
Definition: log_msg2.h:89
uint8_t data[]
Definition: log_msg2.h:94
uint8_t padding[((sizeof(struct log_msg2_hdr) % CBPRINTF_PACKAGE_ALIGNMENT) > 0 ?(CBPRINTF_PACKAGE_ALIGNMENT -(sizeof(struct log_msg2_hdr) % CBPRINTF_PACKAGE_ALIGNMENT)) :0)]
Definition: log_msg2.h:93
Constant data associated with the source of log messages.
Definition: log_instance.h:16
Dynamic data associated with the source of log messages.
Definition: log_instance.h:29
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static void msg(uint64_t c64)
Definition: main.c:17
Macros to abstract toolchain specific capabilities.
Definition: log_msg2.h:101
struct log_msg2_generic_hdr generic
Definition: log_msg2.h:103
union mpsc_pbuf_generic buf
Definition: log_msg2.h:102
struct log_msg2 log
Definition: log_msg2.h:104
Definition: log_msg2.h:60
struct log_source_dynamic_data * dynamic
Definition: log_msg2.h:62
const struct log_source_const_data * fixed
Definition: log_msg2.h:61
void * raw
Definition: log_msg2.h:63
Generic packet header.
Definition: mpsc_packet.h:49
Misc utilities.