Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: specific types in tuples #3029

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,7 @@ std::string Output::value_to_str(BPFtrace &bpftrace,
}
return struct_to_str(elems);
} else if (type.IsTupleTy()) {
std::vector<std::string> elems;
for (auto &field : type.GetFields()) {
std::vector<uint8_t> elem_data(value.begin() + field.offset,
value.begin() + field.offset +
field.type.GetSize());
elems.push_back(
value_to_str(bpftrace, field.type, elem_data, is_per_cpu, div));
}
return tuple_to_str(elems);
return tuple_to_str(bpftrace, type, value, is_per_cpu, div);
} else if (type.IsCountTy())
return std::to_string(reduce_value<uint64_t>(value, nvalues) / div);
else if (type.IsIntTy()) {
Expand Down Expand Up @@ -651,9 +643,24 @@ std::string TextOutput::field_to_str(const std::string &name,
return "." + name + " = " + value;
}

std::string TextOutput::tuple_to_str(
const std::vector<std::string> &elems) const
std::string TextOutput::tuple_to_str(BPFtrace &bpftrace,
const SizedType &type,
std::vector<uint8_t> &value,
bool is_per_cpu,
uint32_t div) const
{
std::vector<std::string> elems;
for (const auto &field : type.GetFields()) {
std::vector<uint8_t> elem_data(value.begin() + field.offset,
value.begin() + field.offset +
field.type.GetSize());
auto p = value_to_str(bpftrace, field.type, elem_data, is_per_cpu, div);
if (is_quoted_type(field.type)) {
elems.push_back("\"" + p + "\"");
} else {
elems.push_back(p);
}
}
return "(" + str_join(elems, ", ") + ")";
}

Expand Down Expand Up @@ -948,9 +955,20 @@ std::string JsonOutput::field_to_str(const std::string &name,
return "\"" + name + "\": " + value;
}

std::string JsonOutput::tuple_to_str(
const std::vector<std::string> &elems) const
std::string JsonOutput::tuple_to_str(BPFtrace &bpftrace,
const SizedType &type,
std::vector<uint8_t> &value,
bool is_per_cpu,
uint32_t div) const
{
std::vector<std::string> elems;
for (const auto &field : type.GetFields()) {
std::vector<uint8_t> elem_data(value.begin() + field.offset,
value.begin() + field.offset +
field.type.GetSize());
elems.push_back(
value_to_str(bpftrace, field.type, elem_data, is_per_cpu, div));
}
return "[" + str_join(elems, ",") + "]";
}

Expand Down
21 changes: 15 additions & 6 deletions src/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,11 @@ class Output {
virtual std::string field_to_str(const std::string &name,
const std::string &value) const = 0;
// Convert tuple to string
virtual std::string tuple_to_str(
const std::vector<std::string> &elems) const = 0;
virtual std::string tuple_to_str(BPFtrace &bpftrace,
const SizedType &type,
std::vector<uint8_t> &value,
bool is_per_cpu,
uint32_t div) const = 0;
// Convert a vector of (key, value) pairs into string
// keys are strings
// values are 64-bit integers
Expand Down Expand Up @@ -267,8 +270,11 @@ class TextOutput : public Output {
std::string map_elem_delim_to_str(const SizedType &map_type) const override;
std::string field_to_str(const std::string &name,
const std::string &value) const override;
std::string tuple_to_str(
const std::vector<std::string> &elems) const override;
std::string tuple_to_str(BPFtrace &bpftrace,
const SizedType &type,
std::vector<uint8_t> &value,
bool is_per_cpu,
uint32_t div) const override;
std::string key_value_pairs_to_str(
std::vector<std::pair<std::string, int64_t>> &keyvals) const override;
};
Expand Down Expand Up @@ -346,8 +352,11 @@ class JsonOutput : public Output {
std::string map_elem_delim_to_str(const SizedType &map_type) const override;
std::string field_to_str(const std::string &name,
const std::string &value) const override;
std::string tuple_to_str(
const std::vector<std::string> &elems) const override;
std::string tuple_to_str(BPFtrace &bpftrace,
const SizedType &type,
std::vector<uint8_t> &value,
bool is_per_cpu,
uint32_t div) const override;
std::string key_value_pairs_to_str(
std::vector<std::pair<std::string, int64_t>> &keyvals) const override;
};
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/call
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ TIMEOUT 1

NAME print_non_map_tuple
PROG BEGIN { $t = (1, 2, "string"); print($t); exit() }
EXPECT (1, 2, string)
EXPECT (1, 2, "string")
TIMEOUT 1

NAME print_non_map_array
Expand Down Expand Up @@ -359,7 +359,7 @@ TIMEOUT 1

NAME print_map_item_tuple
PROG BEGIN { @x[1] = "hi"; print((1, 2, @x[1])); exit() }
EXPECT (1, 2, hi)
EXPECT (1, 2, "hi")
TIMEOUT 1

NAME strftime
Expand Down
6 changes: 3 additions & 3 deletions tests/runtime/tuples
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TIMEOUT 5

NAME complex tuple 1
PROG BEGIN { print(((int8)-100, (int8) 100, "abcdef", 3, (int32) 1, (int64)-10, (int8)10, (int16)-555)); exit(); }
EXPECT (-100, 100, abcdef, 3, 1, -10, 10, -555)
EXPECT (-100, 100, "abcdef", 3, 1, -10, 10, -555)
TIMEOUT 5

NAME tuple struct sizing 1
Expand Down Expand Up @@ -80,8 +80,8 @@ TIMEOUT 5
# Careful with '(' and ')', they are read by the test engine as a regex group,
# so make sure to escape them.
NAME tuple print
PROG BEGIN { @ = (1, 2, "string", (4, 5)); exit(); }
EXPECT @: (1, 2, string, (4, 5))
PROG BEGIN { @ = (1, 2, "string", (4, 5), strerror(22)); exit(); }
EXPECT @: (1, 2, "string", (4, 5), "Invalid argument")
TIMEOUT 5

NAME tuple strftime type is packed
Expand Down
2 changes: 1 addition & 1 deletion tests/runtime/variable
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ TIMEOUT 2

NAME tuple string resize
PROG BEGIN { @ = ("hello", 0); } i:ms:1 { @ = ("hi", 1); exit(); }
EXPECT @: (hi, 1)
EXPECT @: ("hi", 1)
TIMEOUT 2
Loading