Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $# parameter #741

Closed
ajor opened this issue Jun 9, 2019 · 7 comments · Fixed by #791
Closed

Add $# parameter #741

ajor opened this issue Jun 9, 2019 · 7 comments · Fixed by #791
Labels
enhancement New feature or request, changes on existing features

Comments

@ajor
Copy link
Member

ajor commented Jun 9, 2019

To return the number of command line arguments passed to the script at runtime.

@danobi danobi added the enhancement New feature or request, changes on existing features label Jun 13, 2019
@markdrayton
Copy link
Contributor

What would example use cases for this be? Since there is no direct loop support and unroll doesn't yet (AFAICT) expose any way to do something with each positional parameter I can't see an immediate use for it. I'm sure I am missing something.

Nevertheless, I'm interested in trying to add this feature as an intro to bpftrace development. Is making PositionalParameter more generic (e.g. able to handle both positional params and the number of parameters via some additional state in the class) okay, or does this feature warrant an additional expression?

@ajor
Copy link
Member Author

ajor commented Jun 20, 2019

Making PositionalParameter able to handle this sounds like a good way to go.

Positional parameters are optional in bpftrace, and when they are not provided on the command line they get initialised to default values (0 for integers, "" for strings).

Two cases that $# would be useful for:

  • Making positional parameters required, not optional
BEGIN
{
  if ($# < 2)
  {
    printf("Two arguments required\n");
    exit();
  }
}
  • Setting different default values
kprobe:foo
{
  if ($# < 3)
  {
    // $3 will have the default value of 0 here
    $3 = -1; // instead initialise it to -1
  }
  ... use $3 ...
}

Scripts at the moment might be checking if a parameter is set by comparing to 0, but this would break if a user passes a value of 0 along through the command line.

@markdrayton
Copy link
Contributor

It looks like bpftrace refuses to run scripts that refer to missing positional parameters:

$ sudo ./src/bpftrace -e 'BEGIN { printf("%d\n", $1) }'
missing positional parameter $1

I have a PR more or less ready for $#. If it's acceptable we could decide if the missing positional behaviour needs to change.

@brendangregg
Copy link
Contributor

brendangregg commented Jun 21, 2019 via email

@ajor
Copy link
Member Author

ajor commented Jun 21, 2019

Yep sorry, I've just merged #742 which re-enabled optional positional parameters.

@markdrayton
Copy link
Contributor

At risk of derailing this PR, it looks like assigning to positionals doesn't work at the moment so setting default values won't work yet:

# bpftrace -e 'BEGIN { $1 = 1 }'
1.12: syntax error, unexpected =, expecting }

Maybe $pid = $# < 1 ? 12345 : $1 is an acceptable method of handing optional parameters with default values? Obviously positionals could be assignable, just wondering if this simple alternative is okay.

@ajor
Copy link
Member Author

ajor commented Jun 23, 2019

Ah yeah, I just didn't think through that example well enough. $pid = $# < 1 ? 12345 : $1 is a good way of doing it.

@ajor ajor closed this as completed in #791 Jul 10, 2019
ajor pushed a commit that referenced this issue Jul 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request, changes on existing features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants