Bug 26844 - off-by-one error in the output of sprint_ubacktrace()
Summary: off-by-one error in the output of sprint_ubacktrace()
Status: RESOLVED FIXED
Alias: None
Product: systemtap
Classification: Unclassified
Component: runtime (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-11-04 21:59 UTC by agentzh
Modified: 2020-12-12 14:16 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
Off-by-one fix (1009 bytes, patch)
2020-12-09 18:24 UTC, Cosmin Tanislav
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description agentzh 2020-11-04 21:59:58 UTC
Consider the following C program for the target program:

```
int foo(void) {
    return 3;
}

int bar(void) {
    return foo();
}

int main(void) {
    bar();
    return 0;
}
```

Compile it to generate the executable file `./a.out`:

```
gcc -g a.c
```

And then run the following stap oneliner:

```
$ stap -e 'probe oneshot { printf("{%s}\n", sprint_ubacktrace()) }'
{foo+0x4 [a.out]
bar+0x9 [a.out]
main+0x9 [a.out]
0x7ffff7a3b11b [libc-2.27.so+0x2311b]}
WARNING: Missing unwind data for a module, rerun with 'stap -d /usr/lib64/libc-2.27.so'
```

The warning can be safely ignored. What's unexpected here is the lack of newline at the end of the last frame in the generated backtrace (between the bytes `]` and `}` in the output above).

The current master of the upstream systemtap git repo also has the same problem. We should commit any fixes to the upstream as well for this.
Comment 1 agentzh 2020-11-04 22:00:55 UTC
According to the code, it indeed appends a newline to each frame, but the buffer only copies the N - 1 bytes where N is the total byte count (excluding `\0`).
Comment 2 agentzh 2020-11-05 00:29:34 UTC
Using `sprint_backtrace()` is the same thing:

```
$ stap -d kernel -e 'probe oneshot { printf("{\%s}\n", sprint_backtrace()) }'
{0xffffffffc1d8994b [stap_f56de473ef304e53bfb082b091cabf8_15711+0x894b]
0xffffffffc1d898ca [stap_f56de473ef304e53bfb082b091cabf8_15711+0x88ca] (inexact)}
```
Comment 3 Cosmin Tanislav 2020-12-09 18:24:24 UTC
Created attachment 13032 [details]
Off-by-one fix
Comment 4 Frank Ch. Eigler 2020-12-12 14:16:57 UTC
commit fd93cf71df80f