Skip to content

Commit 988203b

Browse files
Jordan Romejordalgo
authored andcommitted
Fix json output for none type
For non-json output we print an empty string ("") for a none type, however for json output we need to "quote" this empty string so we don't get invalid json e.g. ``` bpftrace -e 'kprobe:vfs_write {print(*(struct file *)arg0); exit()}' -f json ``` Before snippet: ``` {"type": "value", "data": { "f_u": , "f_path": { "mnt": 18446612686587658272, ... ``` After snippet: ``` {"type": "value", "data": { "f_u": "", "f_path": { "mnt": 18446612686587658272, ... ``` This is ultimately caused by bpftrace treating anon union and structs as none types. Issue: bpftrace#3543
1 parent 3b0b470 commit 988203b

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ and this project adheres to
2020
#### Deprecated
2121
#### Removed
2222
#### Fixed
23+
- Fix json output for none type
24+
- [#3692](https://github.com/bpftrace/bpftrace/pull/3692)
2325
#### Security
2426
#### Docs
2527
#### Tools

src/output.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ bool is_quoted_type(const SizedType &ty)
2424
case Type::inet:
2525
case Type::kstack_t:
2626
case Type::ksym_t:
27+
case Type::none:
2728
case Type::strerror_t:
2829
case Type::string:
2930
case Type::timestamp:
@@ -40,7 +41,6 @@ bool is_quoted_type(const SizedType &ty)
4041
case Type::mac_address:
4142
case Type::max_t:
4243
case Type::min_t:
43-
case Type::none:
4444
case Type::pointer:
4545
case Type::reference:
4646
case Type::record:

tests/runtime/json-output

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,10 @@ EXPECT {"type": "value", "data": 333}
178178
NAME enum_symbolized_printf
179179
RUN {{BPFTRACE}} -q -f json -e 'enum Foo { ONE = 1, TWO = 2, OTHER = 99999 }; BEGIN { printf("%d %s %d %s %d %s\n", ONE, ONE, TWO, TWO, OTHER, OTHER); exit() }'
180180
EXPECT {"type": "printf", "data": "1 ONE 2 TWO 99999 OTHER\n"}
181+
182+
# Note: if we fix the underlying issue, which is that we don't support unions
183+
# or anon structs, then we will need to change/delete this test
184+
NAME none type
185+
RUN {{BPFTRACE}} -q -f json -e 'union N { int i; float f; }; struct Foo { int m; union N n; }; uprobe:./testprogs/struct_with_union:func { print(*((struct Foo *) arg0)); exit(); }'
186+
EXPECT {"type": "value", "data": { "m": 2, "n": { "i": 5, "f": "" } }}
187+
AFTER ./testprogs/struct_with_union
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
union N {
2+
int i;
3+
float f;
4+
};
5+
6+
struct Foo {
7+
int m;
8+
union N n;
9+
};
10+
11+
int func(struct Foo *foo)
12+
{
13+
return foo->m;
14+
}
15+
16+
int main()
17+
{
18+
struct Foo foo;
19+
foo.m = 2;
20+
foo.n.i = 5;
21+
func(&foo);
22+
return 0;
23+
}

0 commit comments

Comments
 (0)