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

language: support scientific notation for literals #1476

Merged
merged 2 commits into from
Aug 21, 2020
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ and this project adheres to
- [#1416](https://github.com/iovisor/bpftrace/pull/1416)
- Add an option to disable warning messages
- [#1444](https://github.com/iovisor/bpftrace/pull/1444)
- Support scientific notation for integer literals
- [#1476](https://github.com/iovisor/bpftrace/pull/1476)

#### Changed
- Warn if using `print` on `stats` maps with top and div arguments
Expand Down
26 changes: 14 additions & 12 deletions src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ static std::string buffer;
using namespace bpftrace;
%}

ident [_a-zA-Z][_a-zA-Z0-9]*
map @{ident}|@
var ${ident}
int [0-9]+|0[xX][0-9a-fA-F]+
hex (x|X)[0-9a-fA-F]{1,2}
oct [0-7]{1,3}
hspace [ \t]
vspace [\n\r]
space {hspace}|{vspace}
path :(\\.|[_\-\./a-zA-Z0-9#\*])*:
builtin arg[0-9]|args|cgroup|comm|cpid|cpu|ctx|curtask|elapsed|func|gid|nsecs|pid|probe|rand|retval|sarg[0-9]|tid|uid|username
call avg|buf|cat|cgroupid|clear|count|delete|exit|hist|join|kaddr|ksym|lhist|max|min|ntop|override|print|printf|reg|signal|sizeof|stats|str|strftime|strncmp|sum|system|time|uaddr|usym|zero
ident [_a-zA-Z][_a-zA-Z0-9]*
map @{ident}|@
var ${ident}
int [0-9]+|0[xX][0-9a-fA-F]+
exponent [0-9]+e[0-9]+
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the alingment in one go, this is the only added line

hex (x|X)[0-9a-fA-F]{1,2}
oct [0-7]{1,3}
hspace [ \t]
vspace [\n\r]
space {hspace}|{vspace}
path :(\\.|[_\-\./a-zA-Z0-9#\*])*:
builtin arg[0-9]|args|cgroup|comm|cpid|cpu|ctx|curtask|elapsed|func|gid|nsecs|pid|probe|rand|retval|sarg[0-9]|tid|uid|username
call avg|buf|cat|cgroupid|clear|count|delete|exit|hist|join|kaddr|ksym|lhist|max|min|ntop|override|print|printf|reg|signal|sizeof|stats|str|strftime|strncmp|sum|system|time|uaddr|usym|zero

/* Don't add to this! Use builtin OR call not both */
call_and_builtin kstack|ustack
Expand Down Expand Up @@ -59,6 +60,7 @@ bpftrace|perf { return Parser::make_STACK_MODE(yytext, loc); }
{call} { return Parser::make_CALL(yytext, loc); }
{call_and_builtin} { return Parser::make_CALL_BUILTIN(yytext, loc); }
{int} { return Parser::make_INT(strtoll(yytext, NULL, 0), loc); }
{exponent} { return Parser::make_INT(parse_exponent(yytext), loc); }
{path} { return Parser::make_PATH(yytext, loc); }
{map} { return Parser::make_MAP(yytext, loc); }
{var} { return Parser::make_VAR(yytext, loc); }
Expand Down
14 changes: 14 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cmath>
#include <cstring>

#include <algorithm>
Expand Down Expand Up @@ -778,4 +779,17 @@ std::unordered_set<std::string> get_traceable_funcs()
return result;
}

uint64_t parse_exponent(const char *str)
{
char *e_offset;
auto base = strtoll(str, &e_offset, 10);

if (*e_offset != 'e')
return base;

auto exp = strtoll(e_offset + 1, nullptr, 10);
auto num = base * std::pow(10, exp);
return num;
}

} // namespace bpftrace
2 changes: 2 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,6 @@ T read_data(const void *src)
return v;
}

uint64_t parse_exponent(const char *str);

} // namespace bpftrace
10 changes: 10 additions & 0 deletions tests/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,16 @@ TEST(Parser, empty_arguments)
test_parse_failure(":w:0x10000000:8:rw { 1 }");
}

TEST(Parser, scientific_notation)
{
test("k:f { print(1e6); }",
"Program\n kprobe:f\n call: print\n int: 1000000\n");
test("k:f { print(5e9); }",
"Program\n kprobe:f\n call: print\n int: 5000000000\n");

test_parse_failure("k:f { print(5e-9); }");
}

TEST(Parser, while_loop)
{
test("i:ms:100 { $a = 0; while($a < 10) { $a++ }}",
Expand Down
13 changes: 13 additions & 0 deletions tests/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ TEST(utils, resolve_binary_path)
exec_system(("rm -rf " + path).c_str());
}

TEST(utils, parse_exponent)
{
EXPECT_EQ(parse_exponent((const char*)"1e0"), 1e0);
EXPECT_EQ(parse_exponent((const char*)"1e1"), 1e1);
EXPECT_EQ(parse_exponent((const char*)"1e9"), 1e9);
EXPECT_EQ(parse_exponent((const char*)"2e1"), 2e1);
EXPECT_EQ(parse_exponent((const char*)"2e9"), 2e9);
EXPECT_EQ(parse_exponent((const char*)"2e9"), 2e9);
EXPECT_EQ(parse_exponent((const char*)"010e010"), 1e11);

EXPECT_EQ(parse_exponent((const char*)"2a9"), 2ULL);
}

} // namespace utils
} // namespace test
} // namespace bpftrace
4 changes: 2 additions & 2 deletions tools/biosnoop.bt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ kprobe:blk_account_io_done
{
$now = nsecs;
printf("%-12u %-16s %-6d %7d\n",
elapsed / 1000000, @iocomm[arg0], @iopid[arg0],
($now - @start[arg0]) / 1000000);
elapsed / 1e6, @iocomm[arg0], @iopid[arg0],
($now - @start[arg0]) / 1e6);

delete(@start[arg0]);
delete(@iopid[arg0]);
Expand Down
4 changes: 2 additions & 2 deletions tools/dcsnoop.bt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ BEGIN
kprobe:lookup_fast
{
$nd = (struct nameidata *)arg0;
printf("%-8d %-6d %-16s R %s\n", elapsed / 1000000, pid, comm,
printf("%-8d %-6d %-16s R %s\n", elapsed / 1e6, pid, comm,
str($nd->last.name));
}

Expand All @@ -48,7 +48,7 @@ kprobe:d_lookup
kretprobe:d_lookup
/@fname[tid]/
{
printf("%-8d %-6d %-16s M %s\n", elapsed / 1000000, pid, comm,
printf("%-8d %-6d %-16s M %s\n", elapsed / 1e6, pid, comm,
str(@fname[tid]));
delete(@fname[tid]);
}
2 changes: 1 addition & 1 deletion tools/execsnoop.bt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ BEGIN

tracepoint:syscalls:sys_enter_execve
{
printf("%-10u %-5d ", elapsed / 1000000, pid);
printf("%-10u %-5d ", elapsed / 1e6, pid);
join(args->argv);
}
2 changes: 1 addition & 1 deletion tools/gethostlatency.bt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ uretprobe:/lib/x86_64-linux-gnu/libc.so.6:gethostbyname,
uretprobe:/lib/x86_64-linux-gnu/libc.so.6:gethostbyname2
/@start[tid]/
{
$latms = (nsecs - @start[tid]) / 1000000;
$latms = (nsecs - @start[tid]) / 1e6;
time("%H:%M:%S ");
printf("%-6d %-16s %6d %s\n", pid, comm, $latms, str(@name[tid]));
delete(@start[tid]);
Expand Down
2 changes: 1 addition & 1 deletion tools/naptime.bt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ tracepoint:syscalls:sys_enter_nanosleep
time("%H:%M:%S ");
printf("%-6d %-16s %-6d %-16s %d.%03d\n", $task->real_parent->pid,
$task->real_parent->comm, pid, comm,
args->rqtp->tv_sec, (uint64)args->rqtp->tv_nsec / 1000000);
args->rqtp->tv_sec, (uint64)args->rqtp->tv_nsec / 1e6);
}
2 changes: 1 addition & 1 deletion tools/tcplife.bt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ kprobe:tcp_set_state

// session ended: calculate lifespan and print
if ($newstate == TCP_CLOSE && @birth[$sk]) {
$delta_ms = (nsecs - @birth[$sk]) / 1000000;
$delta_ms = (nsecs - @birth[$sk]) / 1e6;
$lport = $sk->__sk_common.skc_num;
$dport = $sk->__sk_common.skc_dport;
$dport = ($dport >> 8) | (($dport << 8) & 0xff00);
Expand Down
2 changes: 1 addition & 1 deletion tools/threadsnoop.bt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ BEGIN

uprobe:/lib/x86_64-linux-gnu/libpthread.so.0:pthread_create
{
printf("%-10u %-6d %-16s %s\n", elapsed / 1000000, pid, comm,
printf("%-10u %-6d %-16s %s\n", elapsed / 1e6, pid, comm,
usym(arg2));
}