When using foreach statements with arrays on stapbpf, the incorrect values are retrieved. For example, the following script demonstrates this: global arr probe begin { arr[1] = 1; arr[2] = 2; exit() } probe end { foreach ( v = v1 in arr) { printf("%d ", v) } }' '1 2 ' should be printed, but rather '0 0 ' is printed.
Without the assignment in the foreach gets the values: [wcohen@localhost testsuite]$ more ~/foreach2.stp global arr probe begin { arr[1] = 1; arr[2] = 2; exit()} probe end { foreach ( v in arr) { printf("%d ", v) } } [wcohen@localhost testsuite]$ sudo stap --bpf ~/foreach2.stp 2 1 Trying to do things like the testsuite systemtap.base/array_slicing.exp or systemtap.base/foreach_value.stp?
(In reply to William Cohen from comment #1) > Without the assignment in the foreach gets the values: > > [wcohen@localhost testsuite]$ more ~/foreach2.stp > global arr > > probe begin { arr[1] = 1; arr[2] = 2; exit()} > > probe end { foreach ( v in arr) { printf("%d ", v) } } > [wcohen@localhost testsuite]$ sudo stap --bpf ~/foreach2.stp > 2 1 > > Trying to do things like the testsuite systemtap.base/array_slicing.exp or > systemtap.base/foreach_value.stp? Sorry, I should've used a clearer example. In the script above, it seems to "work" because the indices and the actual values in the array are the same. In the following script, you can observe that without the assignment, 'v' is referring to the indices and it will print "a b ": global arr probe begin { arr["a"] = 1; arr["b"] = 2; exit() } probe end {foreach ( v in arr) { printf("%s ", v) } } One possible alternative is to index into the array using 'v' and that works. I'm trying to do things like systemtap.base/foreach_value.stp and more specifically, things like tapset/prometheus.stpm where the indices and array values are both used in the foreach statement.
Returning the keys (not the values) is expected behaviour for a simple foreach statement. https://sourceware.org/systemtap/langref/6_Statement_types.html#SECTION00076000000000000000 > The foreach statement loops over each element of a named global array, assigning the current key to VAR. So that doesn't need to be changed. The current foreach (v = v1, v2 in arr) behaviour in stapbpf is not correct, however. If you look at foreach_loop in staptree.h https://sourceware.org/git/gitweb.cgi?p=systemtap.git;a=blob;f=staptree.h;h=27358085596b8e2bf2c6aa41785ffb067327a9c4;hb=HEAD#l758 there should be fields which specify where to store the value, and which are ignored in bpf-translate.cxx.
Fixed in commit 724b7014a.
Further fix introduced in commit b54a0507e.