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

Change how printf parses its format string #1414

Merged
merged 1 commit into from
Jul 15, 2020

Conversation

suyuee
Copy link
Contributor

@suyuee suyuee commented Jul 9, 2020

Currently printf does not allow string format specifiers to be directly
followed by any alphabetic characters since it would take all the following
letters as part of the specifiers. As an example:

bpftrace -e 'BEGIN { @start = nsecs; } i:s:1 { printf("Elapsed: %ds\n", (nsecs - @start)/1000000000); }' 
stdin:1:35-95: ERROR: printf: Unknown format string token: %ds
BEGIN { @start = nsecs; } i:s:1 { printf("Elapsed: %ds\n", (nsecs - @start)/1000000000); }

I think it's good to fix it since I could not think of any obvious downsides.
This PR works to address this issue by changing the way printf parse the
format strings. It would also affect cat and system as well. With this PR:

bpftrace -e 'BEGIN { @start = nsecs; } i:s:1 { printf("Elapsed: %ds\n", (nsecs - @start)/1000000000); }'  
Attaching 2 probes...
Elapsed: 1s
Elapsed: 2s
Elapsed: 3s
Elapsed: 4s
Elapsed: 5s
^C

@start: 2976003477708

@suyuee suyuee force-pushed the improve_printf branch 2 times, most recently from 72bb7ab to 0434849 Compare July 9, 2020 19:59
…ctly

followed by any alphabetic characters since it would take all the following
letters as part of the specifiers. As an example:

```
bpftrace -e 'BEGIN { @start = nsecs; } i:s:1 { printf("Elapsed: %ds\n", (nsecs - @start)/1000000000); }'
stdin:1:35-95: ERROR: printf: Unknown format string token: %ds
BEGIN { @start = nsecs; } i:s:1 { printf("Elapsed: %ds\n", (nsecs - @start)/1000000000); }
```
I think it's good to fix it since I could not think of any obvious downsides.
This works to address this issue by changing the way `printf` parse the
format strings. It would also affect `cat` and `system` as well. With this PR:

```
bpftrace -e 'BEGIN { @start = nsecs; } i:s:1 { printf("Elapsed: %ds\n", (nsecs - @start)/1000000000); }'
Attaching 2 probes...
Elapsed: 1s
Elapsed: 2s
Elapsed: 3s
Elapsed: 4s
Elapsed: 5s
^C

@start: 2976003477708
```
@suyuee suyuee marked this pull request as ready for review July 9, 2020 20:37
Copy link
Member

@danobi danobi left a comment

Choose a reason for hiding this comment

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

Nice fix, lgtm.

Will merge after release tomorrow

@fbs fbs merged commit 6cf6857 into bpftrace:master Jul 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants