Bug 24953 - foreach (v = v1,v2) syntax not behaving correctly in stapbpf
Summary: foreach (v = v1,v2) syntax not behaving correctly in stapbpf
Status: RESOLVED FIXED
Alias: None
Product: systemtap
Classification: Unclassified
Component: bpf (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Sagar Patel
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-08-30 15:59 UTC by Sagar Patel
Modified: 2019-10-25 18:52 UTC (History)
2 users (show)

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 Sagar Patel 2019-08-30 15:59:24 UTC
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.
Comment 1 William Cohen 2019-08-30 18:41:06 UTC
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?
Comment 2 Sagar Patel 2019-08-30 18:52:11 UTC
(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.
Comment 3 Serhei Makarov 2019-09-03 14:06:45 UTC
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.
Comment 4 Sagar Patel 2019-09-26 20:01:48 UTC
Fixed in commit 724b7014a.
Comment 5 Sagar Patel 2019-10-25 18:52:47 UTC
Further fix introduced in commit b54a0507e.