-
I think it's strange that the size unit of Int BTF type is bit, but size unit of Struct/Union BTF type is byte. For example, here's some BTF info of
But the
Here's
Should the size unit of Int BTF type be byte? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The package pwru
import (
"fmt"
"log"
"os"
"strings"
"github.com/cilium/ebpf/btf"
)
func main() {
kernelSpec, err := btf.LoadKernelSpec()
if err != nil {
log.Fatalf("failed to load kernel spec: %v", err)
}
iter := kernelSpec.Iterate()
for iter.Next() {
s, ok := iter.Type.(*btf.Struct)
if !ok {
continue
}
if s.Name == "sk_buff" {
printBtf(s, 0, s.Name)
fmt.Println()
}
}
os.Exit(0)
}
var seen = make(map[string]bool)
func init() {
seen["net_device"] = true
seen["sock"] = true
}
func indent(n int) string {
return strings.Repeat(" ", n)
}
func printBtf(t btf.Type, depth int, name string) {
// if depth > 10 {
// return
// }
if name == "" {
name = t.TypeName()
}
fmt.Printf("%s%s: %v\n", indent(depth), name, t)
switch t := t.(type) {
case *btf.Int:
// nothing
case *btf.Float:
// nothing
case *btf.Struct:
if seen[t.TypeName()] {
return
}
seen[t.TypeName()] = true
fmt.Printf("%s size: %d\n", indent(depth), t.Size)
for _, m := range t.Members {
if m.BitfieldSize != 0 {
fmt.Printf("%s %s: offset=%d bitfieldSize=%d\n", indent(depth), m.Name, m.Offset, m.BitfieldSize)
} else {
fmt.Printf("%s %s: offset=%d\n", indent(depth), m.Name, m.Offset)
}
printBtf(m.Type, depth+1, m.Name)
}
case *btf.Pointer:
printBtf(t.Target, depth+1, "")
case *btf.Typedef:
printBtf(t.Type, depth+1, t.Name)
case *btf.Const:
printBtf(t.Type, depth+1, "")
case *btf.Volatile:
printBtf(t.Type, depth+1, "")
case *btf.Fwd:
// nothing
case *btf.Void:
// nothing
case *btf.Enum:
for _, v := range t.Values {
fmt.Printf("%s %s: %d\n", indent(depth+1), v.Name, v.Value)
}
case *btf.Union:
fmt.Printf("%s size: %d\n", indent(depth), t.Size)
for _, m := range t.Members {
if m.BitfieldSize != 0 {
fmt.Printf("%s %s: offset=%d bitfieldSize=%d\n", indent(depth), m.Name, m.Offset, m.BitfieldSize)
} else {
fmt.Printf("%s %s: offset=%d\n", indent(depth), m.Name, m.Offset)
}
printBtf(m.Type, depth+1, m.Name)
}
case *btf.Array:
printBtf(t.Type, depth+1, "")
}
} |
Beta Was this translation helpful? Give feedback.
-
Looks like the 'size' field in the BTF is in bytes as well. What are you suggesting? |
Beta Was this translation helpful? Give feedback.
Thanks. I corrected it by the PR #1182