Skip to content

Commit

Permalink
Fix: json formatting for hex values
Browse files Browse the repository at this point in the history
  • Loading branch information
amiremohamadi authored and jordalgo committed Oct 1, 2024
1 parent 1517d1c commit 6782899
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ and this project adheres to
- [#3443](https://github.com/bpftrace/bpftrace/pull/3443)
- Handle invalid BTF without crashing
- [#3453](https://github.com/bpftrace/bpftrace/pull/3453)
- Fix json formatting for hex values
- [#3475](https://github.com/bpftrace/bpftrace/pull/3475)
#### Security
#### Docs
- Remove mention of unsupported character literals
Expand Down
43 changes: 35 additions & 8 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,6 @@ std::string Output::value_to_str(BPFtrace &bpftrace,
case Type::strerror: {
return strerror(read_data<uint64_t>(value.data()));
}
case Type::pointer:
case Type::reference: {
std::ostringstream res;
res << "0x" << std::hex << read_data<uint64_t>(value.data());
return res.str();
}
case Type::none: {
return "";
}
Expand All @@ -428,6 +422,8 @@ std::string Output::value_to_str(BPFtrace &bpftrace,
case Type::lhist:
case Type::stack_mode:
case Type::stats:
case Type::pointer:
case Type::reference:
case Type::timestamp_mode: {
LOG(BUG) << "Invalid value type: " << type;
}
Expand Down Expand Up @@ -760,6 +756,27 @@ void TextOutput::value(BPFtrace &bpftrace,
out_ << value_to_str(bpftrace, ty, value, false, 1) << std::endl;
}

std::string TextOutput::value_to_str(BPFtrace &bpftrace,
const SizedType &type,
const std::vector<uint8_t> &value,
bool is_per_cpu,
uint32_t div,
bool is_map_key) const
{
switch (type.GetTy()) {
case Type::pointer:
case Type::reference: {
std::ostringstream res;
res << "0x" << std::hex << read_data<uint64_t>(value.data());
return res.str();
}
default: {
return Output::value_to_str(
bpftrace, type, value, is_per_cpu, div, is_map_key);
}
};
}

void TextOutput::message(MessageType type __attribute__((unused)),
const std::string &msg,
bool nl) const
Expand Down Expand Up @@ -1112,8 +1129,18 @@ std::string JsonOutput::value_to_str(BPFtrace &bpftrace,
uint32_t div,
bool is_map_key) const
{
auto str = Output::value_to_str(
bpftrace, type, value, is_per_cpu, div, is_map_key);
std::string str;

switch (type.GetTy()) {
case Type::pointer:
case Type::reference:
str = std::to_string(read_data<uint64_t>(value.data()));
break;
default:
str = Output::value_to_str(
bpftrace, type, value, is_per_cpu, div, is_map_key);
};

if (is_quoted_type(type)) {
if (is_map_key) {
return json_escape(str);
Expand Down
6 changes: 6 additions & 0 deletions src/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ class TextOutput : public Output {
const location &loc) const override;

protected:
std::string value_to_str(BPFtrace &bpftrace,
const SizedType &type,
const std::vector<uint8_t> &value,
bool is_per_cpu,
uint32_t div,
bool is_map_key = false) const override;
static std::string hist_index_label(uint32_t index, uint32_t bits);
static std::string lhist_index_label(int number, int step);
virtual std::string hist_to_str(const std::vector<uint64_t> &values,
Expand Down
4 changes: 4 additions & 0 deletions tests/runtime/json-output
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,7 @@ NAME strftime
RUN {{BPFTRACE}} -q -f json -e 'BEGIN { $t = (1, strftime("%m/%d/%y", nsecs)); print($t); exit() }' | python3 -c 'import sys,json; print(json.load(sys.stdin))'
EXPECT_REGEX ^{'type': 'value', 'data': \[1, '[0-9]{2}\/[0-9]{2}\/[0-9]{2}'\]}$
TIMEOUT 1

NAME print_hex_values
RUN {{BPFTRACE}} -q -f json -e 'BEGIN { @=(int16*) 0x32; exit(); }'
EXPECT {"type": "map", "data": {"@": 50}}

0 comments on commit 6782899

Please sign in to comment.