Bug 23816 - printf with multiple %s does not work on bpf
Summary: printf with multiple %s does not work on bpf
Alias: None
Product: systemtap
Classification: Unclassified
Component: bpf (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Serhei Makarov
Depends on: 22330
  Show dependency treegraph
Reported: 2018-10-24 20:01 UTC by Serhei Makarov
Modified: 2019-03-22 19:56 UTC (History)
0 users

See Also:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Serhei Makarov 2018-10-24 20:01:20 UTC
Try e.g.:

function foo() { return "test" }

probe kernel.function("vfs_read") {
  printf("K 1 %s\n", foo()) // <- this worked
  printf("K 2 %s\n", "test") // <- this worked
  printf("K 3 %s %s\n", foo(), "test") // <- this didn't
  printf("K 4 %s %s\n", "test", "test") // <- this didn't
  printf("K 5 %s %s\n", foo(), foo()) // <- this didn't
  printf("K 6 %s", "test") printf(" %s\n", "test") // <- this did
  printf("K %d %s\n", 7, "test") // <- this did

In kernel, many of the printf's will be swallowed.

In userspace, output might be incorrect (e.g. string3.stp newly committed to testsuite).
Comment 1 Serhei Makarov 2018-11-02 16:28:42 UTC
Turns out this limitation is hardcoded into the trace_printk() helper:

Comment 2 Serhei Makarov 2019-03-22 19:56:04 UTC
fixed in commit f3fe0bc82
tested by commit 2ec607a31 (string3.stp, string4.stp)

This adds support for format specifiers (e.g. '%3d') in-kernel, which was another thing trace_printk() didn't handle (it would drop the entire printf with an error code, quite a confusing behaviour for someone using stapbpf). Commit 14b90aebc updates cachestat.stp to take advantage of this.

BPF documentation informs me that trace_printk() appears to have been deliberately designed to have annoying limitations to push people into using perf_events: https://github.com/torvalds/linux/blob/master/include/uapi/linux/bpf.h#L528

Message received.
Comment 3 Serhei Makarov 2019-03-22 19:56:28 UTC
fixed, as explained above