Skip to content

Commit 160a504

Browse files
committed
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
1 parent 71cbb3f commit 160a504

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/traced/probes/ftrace/proto_translation_table.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,20 @@ bool InferFtraceType(const std::string& type_and_name,
256256
size_t size,
257257
bool is_signed,
258258
FtraceFieldType* out) {
259-
// Fixed length strings: e.g. "char foo[16]" we don't care about the number
260-
// since we get the size as it's own field. Somewhat awkwardly these fields
261-
// are both fixed size and null terminated meaning that we can't just drop
262-
// them directly into the protobuf (since if the string is shorter than 15
263-
// characters we want only the bit up to the null terminator).
259+
// Fixed length strings: e.g. "char foo[16]".
260+
//
261+
// We don't care about the number, since we get the size as it's own field and
262+
// since it can be a string defined elsewhere in a kernel header file.
263+
//
264+
// Somewhat awkwardly these fields are both fixed size and null terminated
265+
// meaning that we can't just drop them directly into the protobuf (since if
266+
// the string is shorter than 15 characters we want only the bit up to the
267+
// null terminator).
264268
//
265269
// In some rare cases (e.g. old kernel bugs) these strings might not be null
266270
// terminated (b/205763418).
267-
if (Match(type_and_name.c_str(), R"(char [a-zA-Z_]+\[[0-9]+\])")) {
271+
if (Match(type_and_name.c_str(),
272+
R"(char [a-zA-Z_][a-zA-Z_0-9]*\[[a-zA-Z_0-9]+\])")) {
268273
*out = kFtraceFixedCString;
269274
return true;
270275
}

src/traced/probes/ftrace/proto_translation_table_unittest.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ TEST(TranslationTableTest, InferFtraceType) {
319319
ASSERT_TRUE(InferFtraceType("char foo[16]", 16, false, &type));
320320
EXPECT_EQ(type, kFtraceFixedCString);
321321

322+
ASSERT_TRUE(InferFtraceType("char comm[TASK_COMM_LEN]", 16, false, &type));
323+
EXPECT_EQ(type, kFtraceFixedCString);
324+
325+
ASSERT_TRUE(InferFtraceType("char identifier22[16]", 16, false, &type));
326+
EXPECT_EQ(type, kFtraceFixedCString);
327+
328+
EXPECT_FALSE(InferFtraceType("char 2invalid[16]", 16, false, &type));
329+
322330
ASSERT_TRUE(InferFtraceType("char[] foo", 8, false, &type));
323331
EXPECT_EQ(type, kFtraceStringPtr);
324332

0 commit comments

Comments
 (0)