github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/agent/ebpfspy/symtab/gosym_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package symtab
     5  
     6  import (
     7  	"encoding/hex"
     8  	"reflect"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestGoSymSelfTest(t *testing.T) {
    14  	var ptr = reflect.ValueOf(TestGoSymSelfTest).Pointer()
    15  	mod := "/proc/self/exe"
    16  	symtab, err := NewGoSymbolTable(mod, nil)
    17  	if err != nil {
    18  		t.Fatalf("failed to create symtab %v", err)
    19  	}
    20  	sym := symtab.Resolve(uint64(ptr), false)
    21  	expectedSym := "github.com/pyroscope-io/pyroscope/pkg/agent/ebpfspy/symtab.TestGoSymSelfTest"
    22  	if sym.Name != expectedSym {
    23  		t.Fatalf("Expected %s got %s", expectedSym, sym.Name)
    24  	}
    25  	if sym.Module != mod {
    26  		t.Fatalf("Expected %s got %s", mod, sym.Module)
    27  	}
    28  	if sym.Offset != uint64(ptr) {
    29  		t.Fatalf("Expected %d got %d", ptr, sym.Offset)
    30  	}
    31  }
    32  
    33  func TestPclntab18(t *testing.T) {
    34  	s := "f0 ff ff ff 00 00 01 08 9a 05 00 00 00 00 00 00 " +
    35  		" bb 00 00 00 00 00 00 00 a0 23 40 00 00 00 00 00" +
    36  		" 60 00 00 00 00 00 00 00 c0 bb 00 00 00 00 00 00" +
    37  		" c0 c3 00 00 00 00 00 00 c0 df 00 00 00 00 00 00"
    38  	bs, _ := hex.DecodeString(strings.ReplaceAll(s, " ", ""))
    39  	textStart := parseRuntimeTextFromPclntab18(bs)
    40  	expected := uint64(0x4023a0)
    41  	if textStart != expected {
    42  		t.Fatalf("expected %d got %d", expected, textStart)
    43  	}
    44  }
    45  
    46  func BenchmarkGoSym(b *testing.B) {
    47  	gosym, _ := NewGoSymbolTable("/proc/self/exe", nil)
    48  	if len(gosym.tab.symbols) < 1000 {
    49  		b.FailNow()
    50  	}
    51  	b.ResetTimer()
    52  	for i := 0; i < b.N; i++ {
    53  		for _, symbol := range gosym.tab.symbols {
    54  			gosym.Resolve(symbol.Entry, false)
    55  		}
    56  	}
    57  }