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 built-in function for formatting MAC addresses #1647

Merged
merged 1 commit into from
Jan 31, 2021

Conversation

acj
Copy link
Contributor

@acj acj commented Nov 22, 2020

This adds a helper function named macaddr (tentative name) that formats MAC addresses in the canonical hex style, e.g. 00:1C:42:B5:20:09. Resolves #1575.

WIP, but feedback is welcome and appreciated.

Checklist
  • Language changes are updated in docs/reference_guide.md
  • User-visible and non-trivial changes updated in CHANGELOG.md
  • The new behaviour is covered by tests

@acj
Copy link
Contributor Author

acj commented Nov 22, 2020

The codegen validator is currently failing with an error that I don't understand:

4: /usr/bin/llvm-as-7: /home/runner/work/bpftrace/bpftrace/tests/codegen/llvm/call_macaddr.ll:24:9: error: stored value and pointer type do not match
4: store i64* %macaddr, [6 x i8]* %"@x_val"
4:         ^

Fiddling with the codegen types, I can even get this:

4: /usr/bin/llvm-as-7: /vagrant/tests/codegen/llvm/call_macaddr.ll:24:9: error: stored value and pointer type do not match
4:   store [8 x i8]* %macaddr, [8 x i8]* %"@x_val"
4:         ^

... which seems odd. If I remove the @ = assignment from the codegen test, then everything passes, but I think it should be possible for the current test to pass. Any tips?

Copy link
Member

@fbs fbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! Some comments.

Don't really like the name, don't really know anything better either.

src/ast/codegen_llvm.cpp Outdated Show resolved Hide resolved
src/bpftrace.cpp Outdated Show resolved Hide resolved
tests/semantic_analyser.cpp Show resolved Hide resolved
@acj acj force-pushed the mac-address-printer branch 2 times, most recently from 4060588 to dcd57db Compare November 23, 2020 00:28
Copy link
Member

@fbs fbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but I think it will have some issues with less common types

Changelog and refguide entries need to be added at some point too

src/ast/semantic_analyser.cpp Outdated Show resolved Hide resolved
auto macaddr = call.vargs->front();
auto scoped_del = accept(macaddr);

b_.CreateProbeRead(ctx_,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC arrays can end up on stack already so this would probe read from the stack in that case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should I check for that case? I see that shouldBeOnStackAlready doesn't include arrays, so it wouldn't be enough to branch on that. But I'm thinking that we need to branch on something and then store (instead of probe read) if the arg is already on the stack.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it would have to load/memcpy instead of probe reading.

I dont know the best way to check, arrays need some love. I'd be ok with this probe reading for now too, can always fix it in the future.

src/ast/codegen_llvm.cpp Outdated Show resolved Hide resolved
{
std::string structs = "struct mac { char addr[6]; }; ";

test(structs + "kprobe:f { macaddr((struct mac*)arg0); }", 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need some tests (and runtime tests) for the other supported cases too, like

macaddr(arg0)
macaddr((struct X)y)
macaddr(somekindofarray)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you meant for these two:

macaddr((struct X)y)

How would it differ from this line? Do you mean a cast to a non-pointer struct type?

macaddr(somekindofarray)

The line below this one is testing an array, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh woops, codeblock makes the difference

macaddr(arg0)
macaddr(*(struct mac*)y)
macaddr(somekindofarray)

macaddr(somekindofarray)

The line below this one is testing an array, no?

I'm not sure, I just want to make sure it works for data that has already been loaded onto the stack (so no probe_read required).

@acj
Copy link
Contributor Author

acj commented Dec 14, 2020

@fbs ping

@fbs
Copy link
Member

fbs commented Dec 14, 2020

Sorry, missed this one, busy times :(

@acj
Copy link
Contributor Author

acj commented Dec 14, 2020

All good! I should have time this week to finish testing the different cases, update the docs, etc.

@acj acj force-pushed the mac-address-printer branch 3 times, most recently from e7120f1 to 87f1ea2 Compare December 22, 2020 02:48
@acj acj marked this pull request as ready for review December 22, 2020 03:37
@acj
Copy link
Contributor Author

acj commented Dec 22, 2020

@fbs ready for another look when you have a moment

@fbs
Copy link
Member

fbs commented Jan 24, 2021

@acj sorry for all the delays. Can you rebase this so we can get it in?

@acj
Copy link
Contributor Author

acj commented Jan 24, 2021

@fbs All good. Here's the diff addressing the type changes and fixing a logging typo:

--- src/ast/semantic_analyser.cpp
+++ src/ast/semantic_analyser.cpp
@@ -1194,14 +1194,14 @@ void SemanticAnalyser::visit(Call &call)

     auto &arg = call.vargs->at(0);

-    if (!arg->type.IsIntTy() && !arg->type.IsArray() && !arg->type.IsPtrTy())
+    if (!arg->type.IsIntTy() && !arg->type.IsArrayTy() &&
+        !arg->type.IsByteArray() && !arg->type.IsPtrTy() &&
+        !arg->type.IsRecordTy())
       LOG(ERROR, call.loc, err_)
-          << "() only supports array or pointer arguments"
+          << call.func << "() only supports array or pointer arguments"
           << " (" << arg->type.type << " provided)";

     auto type = arg->type;
-
-    if (arg->type.IsArray() && type.GetSize() != 6)
+    if ((type.IsArrayTy() || type.IsByteArray()) && type.GetSize() != 6)
       LOG(ERROR, call.loc, err_)
           << call.func << "() argument must be 6 bytes in size";


if (!arg->type.IsIntTy() && !arg->type.IsArrayTy() &&
!arg->type.IsByteArray() && !arg->type.IsPtrTy() &&
!arg->type.IsRecordTy())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type::record should only be structs not struct pointers so this should be removed right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good catch. Fixed in latest commit

@fbs fbs merged commit 04dd8d4 into bpftrace:master Jan 31, 2021
@acj acj deleted the mac-address-printer branch January 31, 2021 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mac address printer
2 participants