Bug 23407 - bpf: backend should support strings as first class values
Summary: bpf: backend should support strings as first class values
Status: RESOLVED FIXED
Alias: None
Product: systemtap
Classification: Unclassified
Component: bpf (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Serhei Makarov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-07-12 20:01 UTC by Serhei Makarov
Modified: 2018-10-12 00:55 UTC (History)
0 users

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Serhei Makarov 2018-07-12 20:01:04 UTC
PR created based on https://sourceware.org/ml/systemtap/2018-q3/msg00016.html

It should be possible to store string literals of reasonable size (BPF_MAXSTRINGLEN=64, matching the 'best practice' of other eBPF tracing tools) within local variables, associative array elements, and associative array keys, and to pass them as function arguments.

The tentative plan is to add a new value type STR to struct value in bpf-internal.h. In addition to doing register allocation, bpf-opt.cxx will also lower any STR value into a set of instructions that loads the string onto the eBPF stack (based on existing printf() code) or into memory (if writing to a global data structure), then returns the string address. Because more than one string can be written to the stack at once, strings may need to be written somewhere besides the start of the stack.

Suggested testcase:

global var
global tab1
global tab2

probe begin {
  printf("BEGIN\n")
  var = "str1"
  tab1[0] = "str2"
  tab2["key"] = "str3"
}

probe end {
  printf("%s\n", "str0")
  printf("%s\n", var)
  printf("%s\n", tab1[0])
  printf("%s\n", tab2["key"])
  printf("END\n")
}
Comment 1 Serhei Makarov 2018-08-17 15:42:09 UTC
The essential implementation has been completed. There is room to make the register allocator much, much more clever in terms of providing temporary space to store string literals, freeing it up to make room for other string literals, and so forth. Currently all string literals in a probe are stored on the stack throughout the lifetime of the probe.

To pass the string literals to a helper function, they must be zero-padded by an unrolled pseudo-loop. There are a couple of places (the ones writing map/global values rather than map keys) where this zero-padding code could be eliminated if we instead zeroed out a sufficient proportion of the stack before writing any string literals.

Leaving this PR open until I've come up with a more complex usage example, and it becomes clear whether the smarter storage allocator ideas are necessary.
Comment 2 Serhei Makarov 2018-10-12 00:55:36 UTC
Closing this PR. Smarter register allocation, concatenation support, etc. will be filed as separate PRs if the need comes up.