Skip to content

Commit f82e7ca

Browse files
committed
tracing: Error if a trace event has an array for a __field()
A __field() in the TRACE_EVENT() macro is used to set up the fields of the trace event data. It is for single storage units (word, char, int, pointer, etc) and not for complex structures or arrays. Unfortunately, there's nothing preventing the build from accepting: __field(int, arr[5]); from building. It will turn into a array value. This use to work fine, as the offset and size use to be determined by the macro using the field name, but things have changed and the offset and size are now determined by the type. So the above would only be size 4, and the next field will be located 4 bytes from it (instead of 20). The proper way to declare static arrays is to use the __array() macro. Instead of __field(int, arr[5]) it should be __array(int, arr, 5). Add some macro tricks to the building of a trace event from the TRACE_EVENT() macro such that __field(int, arr[5]) will fail to build. A comment by the failure will explain why the build failed. Link: https://lore.kernel.org/lkml/[email protected]/ Link: https://lore.kernel.org/linux-trace-kernel/[email protected] Reported-by: Douglas RAILLARD <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]> Acked-by: Masami Hiramatsu (Google) <[email protected]>
1 parent d3cba7f commit f82e7ca

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

include/trace/stages/stage5_get_offsets.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,30 @@
99
#undef __entry
1010
#define __entry entry
1111

12+
/*
13+
* Fields should never declare an array: i.e. __field(int, arr[5])
14+
* If they do, it will cause issues in parsing and possibly corrupt the
15+
* events. To prevent that from happening, test the sizeof() a fictitious
16+
* type called "struct _test_no_array_##item" which will fail if "item"
17+
* contains array elements (like "arr[5]").
18+
*
19+
* If you hit this, use __array(int, arr, 5) instead.
20+
*/
1221
#undef __field
13-
#define __field(type, item)
22+
#define __field(type, item) \
23+
{ (void)sizeof(struct _test_no_array_##item *); }
1424

1525
#undef __field_ext
16-
#define __field_ext(type, item, filter_type)
26+
#define __field_ext(type, item, filter_type) \
27+
{ (void)sizeof(struct _test_no_array_##item *); }
1728

1829
#undef __field_struct
19-
#define __field_struct(type, item)
30+
#define __field_struct(type, item) \
31+
{ (void)sizeof(struct _test_no_array_##item *); }
2032

2133
#undef __field_struct_ext
22-
#define __field_struct_ext(type, item, filter_type)
34+
#define __field_struct_ext(type, item, filter_type) \
35+
{ (void)sizeof(struct _test_no_array_##item *); }
2336

2437
#undef __array
2538
#define __array(type, item, len)

0 commit comments

Comments
 (0)