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

btf.Array.TypeName() returns empty string #1672

Open
Ghostbaby opened this issue Feb 8, 2025 · 1 comment
Open

btf.Array.TypeName() returns empty string #1672

Ghostbaby opened this issue Feb 8, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@Ghostbaby
Copy link
Contributor

Describe the bug

Issue Description

When working with BTF type information in ebpf.VariableSpec, the TypeName() method may return an empty string for certain types (like arrays), requiring recursive type resolution to get the actual type name.

Current Behavior

Currently, when calling Type.TypeName() directly on BTF types, it may return an empty string for composite types. For example:

memberMeta := ExportedTypesStructMemberMeta{
    Name: member.Name,
    Type: member.Type.TypeName(), // This might return empty string
}

Consider this BTF type structure:

member = {btf.Member}
 ├── Name = "comm"
 └── Type = {btf.Type | *btf.Array}
     ├── Index = {btf.Type | *btf.Int}
     └── Type = {btf.Type | *btf.Int}
         ├── Name = "char"
         ├── Size = 1
         ├── Encoding = Signed (1)
         └── Nelems = 16

Direct call to TypeName() returns empty string, while the actual type should be char[16].

Expected Behavior

The type resolution should recursively traverse the type information until it finds a meaningful type name. For array types, it should include the array size in the format type[size].

How to reproduce

  1. Create a minimal eBPF program with a global variable:
// example.bpf.c
struct event {
    char comm[16];
    int pid;
};

struct event *unused_event SEC(".bss") = { 0 };
  1. Create a Go program to inspect the BTF information:
package main

import (
    "fmt"
    "log"
    
    "github.com/cilium/ebpf"
    "github.com/cilium/ebpf/btf"
)

func main() {
    // Load the eBPF program
    spec, err := ebpf.LoadCollectionSpec("example.bpf.o")
    if err != nil {
        log.Fatalf("loading spec: %v", err)
    }

    // Inspect global variables
    for name, varSpec := range spec.Variables {
        fmt.Printf("Variable: %s\n", name)
        
        // Get BTF type information
        btfType := varSpec.Type().Type
        
        // If it's a pointer type, try to get the target struct
        if ptr, isPtr := btfType.(*btf.Pointer); isPtr {
            if target, isStruct := ptr.Target.(*btf.Struct); isStruct {
                fmt.Printf("Struct: %s\n", target.Name)
                
                // Print struct members
                for _, member := range target.Members {
                    fmt.Printf("Member: %s, Type: %s\n", 
                        member.Name, 
                        member.Type.TypeName()) // This will be empty for arrays
                }
            }
        }
    }
}
  1. Build and run:
# Compile the eBPF program
clang -target bpf -g -O2 -c example.bpf.c -o example.bpf.o

# Run the Go program
go run main.go
  1. Expected output:
Variable: unused_event
Struct: event
Member: comm, Type:  # Empty string instead of "char[16]"
Member: pid, Type: int

This demonstrates that TypeName() returns an empty string for array types, making it difficult to get the complete type information without recursive type resolution.

Version information

github.com/cilium/ebpf v0.17.2

@Ghostbaby Ghostbaby added the bug Something isn't working label Feb 8, 2025
@ti-mo
Copy link
Collaborator

ti-mo commented Feb 26, 2025

Good observation! This is not related to VariableSpec, though. Would you be up for sending a patch? I likely won't get to this in the coming weeks, I'll be out on PTO.

@ti-mo ti-mo changed the title BTF Type Name Resolution Issue in ebpf.VariableSpec btf.Array.TypeName() returns empty string Feb 26, 2025
@ti-mo ti-mo changed the title btf.Array.TypeName() returns empty string btf.Array.TypeName() returns empty string Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants