简体   繁体   中英

How to use a struct for golang fuction args in bpftrace

I have following go-code:

package main

import "fmt"

type foo struct {
    bar  string
    some int
}

//go:noinline
func sum(a int, b int) {
    fmt.Printf("Sum: %d\n", (a + b))
}

//go:noinline
func (f *foo) baz(value string) string {
    return f.bar
}

//go:noinline
func (f *foo) nested(_f foo) foo {
    fmt.Printf("Arg value: %s and %d\n", _f.bar, _f.some)
    return _f
}

func main() {

    f := foo{
        bar:  "hello world!",
        some: 42,
    }
    fmt.Println(f.baz(f.bar))

    sum(1, 2)

    temp := f.nested(f)
    fmt.Println(temp.bar)

}

Following script or I have problems to dereference the string struct correctly when using a struct definition:

struct args
{
    uint64_t array;
    uint64_t length;
    uint64_t somevalue;
};

uprobe:/tmp/main:0x49aa00 {

  $s = (struct args *)(reg("sp") + 16);

  $length = *(reg("sp") + 24);
  $array  = reg("sp") + 16;

  printf("length: %d\n", $s->length);
  printf("array: %s\n", str(*($array), $length));
  printf("somevalue: %d\n", $s->somevalue);
  // this one will be empty
  printf("array: %s\n", str($s->array, $s->length));
}

Output:

Attaching 1 probe...
length: 12
array: hello world!
somevalue: 42
array: 

Removing $s->length prints 64 bytes (default) however:

hello world!host is downillegal seekinvalid slotlfstack.pushmad

Using reg("sp") works without any issues but when using a user-defined struct the array seems to be "empty". $s->length seems to be 12 but when assigning to an additional variable it becomes 2... Can someone help out here, please?

I have tested your code with bpftrace 0.9.4 in ubuntu 20.04, it seems the same issue.

After that I build bpftrace with latest version, all is ok. You can have a try.

Attaching 1 probe...
length: 12
array: hello world!
somevalue: 42
array: hello world!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM