Get sizeof internal go struct

97 views Asked by At

I've been playing with uprobes. In order to probe a specific object in runtime, I need to know the size of internal go struct. In my case, the poll.FD. I could count each nested struct manually, but this could increase the complexity if we are working with a lot nested struct.

My first attempt was to use dlv expression , len <variable>. Didn't work Command failed: command not available

My second attempt was to create a program to extract this information:

package main

import (
    "fmt"
    "internal/poll"
    "unsafe"
)

func main() {
    fmt.Println("size of internal/poll FD struct:", unsafe.Sizeof(poll.FD{}))
}

When I compile the code above, the following message is shown:

 main.go:7:2: use of internal package internal/poll not allowed

Am I missing something? Is there a better way get that information?

2

There are 2 answers

0
Zhangir Mahmutov On

You can try do it by Testing in same package, in order to pass this limitation.

0
Niranjan M On

unsafe.Sizeof(reflect.ValueOf(<nested struct>)) is this what you're looking for to get the size of nested struct?

PS:This may not be answer but I can't comment.