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

How to get the value of go arguments interface{} in bpf? #1209

Open
minimAluminiumalism opened this issue Oct 22, 2024 · 5 comments
Open

How to get the value of go arguments interface{} in bpf? #1209

minimAluminiumalism opened this issue Oct 22, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@minimAluminiumalism
Copy link
Contributor

I'm trying to a new instrument probe for a package, the target function attached the probe is:

func (c cmdable) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd

I successfully obtained the arg key with following code:

u64 key_ptr_pos = 4;
u64 key_len_ptr_pos = 5;
void *set_key_ptr = get_argument(ctx, key_ptr_pos);
u64 key_str_len = (u64)get_argument(ctx, key_len_ptr_pos);
u64 query_size = MAX_QUERY_SIZE < key_str_len ? MAX_QUERY_SIZE : key_str_len;
bpf_probe_read(key_str, query_size, set_key_ptr);

But I I encountered difficulties in obtaining another arg value which is interface{} type. I searched through all the code in your project but not found any related functions like the above function. Could you provide some guidance?

@minimAluminiumalism minimAluminiumalism added the enhancement New feature or request label Oct 22, 2024
@damemi
Copy link
Contributor

damemi commented Oct 22, 2024

Does #1176 help?

@minimAluminiumalism
Copy link
Contributor Author

@damemi The difficulty is how can I get the type of the interface param?

@minimAluminiumalism
Copy link
Contributor Author

@MrAlias Can you give some guidance about obtaining the interface{} type args?
Here is the function I want to add a probe to.
I can get the forth arg value when I ensure the data type is string, but the actual type is interface{}.
So I need the get the arg type first and then get the data? I have tried but failed to obtain the type.

Or do I need to first obtain the address of the data in kernel mode, then send it(bpf_perf_event_output) to user mode for parsing?

@MrAlias
Copy link
Contributor

MrAlias commented Oct 28, 2024

@minimAluminiumalism I am not sure how to achieve this.

You will need to parse the itab._type and dynamically reconstruct the type pointed to by the interface's data field. Essentially, you will need to recreate the Go runtime implementation in C.

This is something that has blocked prior tasks and caused us to search alternate solutions.

For what its worth, https://cmc.gitbook.io/go-internals/chapter-ii-interfaces provides a good overview of how the runtime package produces assembly code that will do the thing you need to do. Hopefully it is a decent jumping off point. Good luck.

@minimAluminiumalism
Copy link
Contributor Author

minimAluminiumalism commented Dec 14, 2024

@minimAluminiumalism I am not sure how to achieve this.

You will need to parse the itab._type and dynamically reconstruct the type pointed to by the interface's data field. Essentially, you will need to recreate the Go runtime implementation in C.

This is something that has blocked prior tasks and caused us to search alternate solutions.

For what its worth, https://cmc.gitbook.io/go-internals/chapter-ii-interfaces provides a good overview of how the runtime package produces assembly code that will do the thing you need to do. Hopefully it is a decent jumping off point. Good luck.

I have initially resolved this problem (how to obtain the actual value of a variable of type interface{}), although I have not yet completed adaptation for all types(int, string, bool are already finished) of variables, I have implemented the most critical part. If you think this project requires this functionality, I can submit a PR next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants