ftrace: Improve parsing for string fields
With this commit:
* The code now accepts string fields with an indentifier, instead of a
literal, for array size. This is important since commit
3087c61ed2c4("tools/testing/selftests/bpf: replace open-coded 16 with
TASK_COMM_LEN"), included in kernel 5.17, which exposes TASK_COMM_LEN
in the format.
* The code now accepts fields whose name include a digit (because that's
a valid C identifier.).
Tested: unit tests
Bug: 227677227
Change-Id: I6f0074607f6ce41880a235a00ee7a4fbc53a5bb9
diff --git a/src/traced/probes/ftrace/proto_translation_table.cc b/src/traced/probes/ftrace/proto_translation_table.cc
index a644390..90aed24 100644
--- a/src/traced/probes/ftrace/proto_translation_table.cc
+++ b/src/traced/probes/ftrace/proto_translation_table.cc
@@ -256,15 +256,20 @@
size_t size,
bool is_signed,
FtraceFieldType* out) {
- // Fixed length strings: e.g. "char foo[16]" we don't care about the number
- // since we get the size as it's own field. Somewhat awkwardly these fields
- // are both fixed size and null terminated meaning that we can't just drop
- // them directly into the protobuf (since if the string is shorter than 15
- // characters we want only the bit up to the null terminator).
+ // Fixed length strings: e.g. "char foo[16]".
+ //
+ // We don't care about the number, since we get the size as it's own field and
+ // since it can be a string defined elsewhere in a kernel header file.
+ //
+ // Somewhat awkwardly these fields are both fixed size and null terminated
+ // meaning that we can't just drop them directly into the protobuf (since if
+ // the string is shorter than 15 characters we want only the bit up to the
+ // null terminator).
//
// In some rare cases (e.g. old kernel bugs) these strings might not be null
// terminated (b/205763418).
- if (Match(type_and_name.c_str(), R"(char [a-zA-Z_]+\[[0-9]+\])")) {
+ if (Match(type_and_name.c_str(),
+ R"(char [a-zA-Z_][a-zA-Z_0-9]*\[[a-zA-Z_0-9]+\])")) {
*out = kFtraceFixedCString;
return true;
}
diff --git a/src/traced/probes/ftrace/proto_translation_table_unittest.cc b/src/traced/probes/ftrace/proto_translation_table_unittest.cc
index 0210f71..2748ac9 100644
--- a/src/traced/probes/ftrace/proto_translation_table_unittest.cc
+++ b/src/traced/probes/ftrace/proto_translation_table_unittest.cc
@@ -319,6 +319,14 @@
ASSERT_TRUE(InferFtraceType("char foo[16]", 16, false, &type));
EXPECT_EQ(type, kFtraceFixedCString);
+ ASSERT_TRUE(InferFtraceType("char comm[TASK_COMM_LEN]", 16, false, &type));
+ EXPECT_EQ(type, kFtraceFixedCString);
+
+ ASSERT_TRUE(InferFtraceType("char identifier22[16]", 16, false, &type));
+ EXPECT_EQ(type, kFtraceFixedCString);
+
+ EXPECT_FALSE(InferFtraceType("char 2invalid[16]", 16, false, &type));
+
ASSERT_TRUE(InferFtraceType("char[] foo", 8, false, &type));
EXPECT_EQ(type, kFtraceStringPtr);