Commit c318a884 authored by Brendan Gregg's avatar Brendan Gregg Committed by GitHub

Merge pull request #126 from iovisor/tracepoint-adjust-integer-types

Tracepoint adjust integer types
parents 12815210 24c5248d
# This script lists all the types in the kernel's tracepoint format files
# which appear with more than one size. This script's output should be
# compared to the code in TracepointFormatParser::adjust_integer_types()
import glob
field_types = {}
for format_file in glob.iglob("/sys/kernel/debug/tracing/events/*/*/format"):
for line in open(format_file):
if not line.startswith("\tfield:"):
continue
size_section = line.split(";")[2].split(":")
if size_section[0] != "\tsize":
continue
size_val = size_section[1]
field_section = line.split(";")[0].split(":")
if field_section[0] != "\tfield":
continue
field_val = field_section[1]
if "[" in field_val or "*" in field_val:
continue
field_type = " ".join(field_val.split()[:-1])
if field_type not in field_types:
field_types[field_type] = set()
field_types[field_type].add(size_val)
for t in sorted(field_types):
sizes = field_types[t]
if len(sizes) > 1:
sizes_str = ",".join(sorted(sizes))
print(f"{t}: {sizes_str}")
......@@ -98,29 +98,13 @@ std::string TracepointFormatParser::adjust_integer_types(const std::string &fiel
{
std::string new_type = field_type;
// Adjust integer fields to correctly sized types
if (size == 2)
if (size == 8)
{
if (new_type == "char" || new_type == "int8_t")
new_type = "s16";
if (new_type == "unsigned char" || new_type == "uint8_t")
new_type = "u16";
} else if (size == 4)
{
if (new_type == "char" || new_type == "short" ||
new_type == "int8_t" || new_type == "int16_t")
new_type = "s32";
if (new_type == "unsigned char" || new_type == "unsigned short" ||
new_type == "uint8_t" || new_type == "uint16_t")
new_type = "u32";
} else if (size == 8)
{
if (new_type == "char" || new_type == "short" || new_type == "int" ||
new_type == "int8_t" || new_type == "int16_t" ||
new_type == "int32_t")
if (field_type == "int")
new_type = "s64";
if (new_type == "unsigned char" || new_type == "unsigned short" ||
new_type == "unsigned int" || new_type == "uint8_t" ||
new_type == "uint16_t" || new_type == "uint32_t")
if (field_type == "unsigned int" || field_type == "unsigned" ||
field_type == "u32" || field_type == "pid_t" ||
field_type == "uid_t" || field_type == "gid_t")
new_type = "u64";
}
......
......@@ -89,6 +89,66 @@ TEST(tracepoint_format_parser, data_loc)
EXPECT_EQ(expected, result);
}
TEST(tracepoint_format_parser, adjust_integer_types)
{
std::string input =
" field:int arr[8]; offset:0; size:32; signed:1;\n"
" field:int int_a; offset:0; size:4; signed:1;\n"
" field:int int_b; offset:0; size:8; signed:1;\n"
" field:u32 u32_a; offset:0; size:4; signed:0;\n"
" field:u32 u32_b; offset:0; size:8; signed:0;\n"
" field:unsigned int uint_a; offset:0; size:4; signed:0;\n"
" field:unsigned int uint_b; offset:0; size:8; signed:0;\n"
" field:unsigned unsigned_a; offset:0; size:4; signed:0;\n"
" field:unsigned unsigned_b; offset:0; size:8; signed:0;\n"
" field:uid_t uid_a; offset:0; size:4; signed:0;\n"
" field:uid_t uid_b; offset:0; size:8; signed:0;\n"
" field:gid_t gid_a; offset:0; size:4; signed:0;\n"
" field:gid_t gid_b; offset:0; size:8; signed:0;\n"
" field:pid_t pid_a; offset:0; size:4; signed:1;\n"
" field:pid_t pid_b; offset:0; size:8; signed:0;\n";
std::string expected =
"struct _tracepoint_syscalls_sys_enter_read\n"
"{\n"
" int arr[8];\n"
" int int_a;\n"
" s64 int_b;\n"
" u32 u32_a;\n"
" u64 u32_b;\n"
" unsigned int uint_a;\n"
" u64 uint_b;\n"
" unsigned unsigned_a;\n"
" u64 unsigned_b;\n"
" uid_t uid_a;\n"
" u64 uid_b;\n"
" gid_t gid_a;\n"
" u64 gid_b;\n"
" pid_t pid_a;\n"
" u64 pid_b;\n"
"};\n";
std::istringstream format_file(input);
std::string result = MockTracepointFormatParser::get_tracepoint_struct_public(format_file, "syscalls", "sys_enter_read");
EXPECT_EQ(expected, result);
}
} // namespace tracepoint_format_parser
} // namespace test
} // namespace bpftrace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment