|
| 1 | +package ebpftracer |
| 2 | + |
| 3 | +import ( |
| 4 | + "debug/elf" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + |
| 8 | + "github.com/cilium/ebpf" |
| 9 | + "github.com/cilium/ebpf/link" |
| 10 | + "golang.org/x/arch/arm64/arm64asm" |
| 11 | + "golang.org/x/arch/x86/x86asm" |
| 12 | +) |
| 13 | + |
| 14 | +type Symbol struct { |
| 15 | + s *elf.Symbol |
| 16 | + f *ELFFile |
| 17 | + address uint64 |
| 18 | +} |
| 19 | + |
| 20 | +func (s *Symbol) Name() string { |
| 21 | + return s.s.Name |
| 22 | +} |
| 23 | + |
| 24 | +func (s *Symbol) Address() uint64 { |
| 25 | + if s.address == 0 { |
| 26 | + s.address = s.s.Value |
| 27 | + for _, p := range s.f.elf.Progs { |
| 28 | + if p.Type != elf.PT_LOAD || (p.Flags&elf.PF_X) == 0 { |
| 29 | + continue |
| 30 | + } |
| 31 | + if p.Vaddr <= s.s.Value && s.s.Value < (p.Vaddr+p.Memsz) { |
| 32 | + s.address = s.s.Value - p.Vaddr + p.Off |
| 33 | + break |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + return s.address |
| 38 | +} |
| 39 | + |
| 40 | +func (s *Symbol) ReturnOffsets() ([]int, error) { |
| 41 | + text, reader, err := s.f.getTextSectionAndReader() |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + sStart := s.s.Value - text.Addr |
| 47 | + _, err = reader.Seek(int64(sStart), io.SeekStart) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + sBytes := make([]byte, s.s.Size) |
| 52 | + _, err = reader.Read(sBytes) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + offsets := getReturnOffsets(s.f.elf.Machine, sBytes) |
| 58 | + if len(offsets) == 0 { |
| 59 | + return nil, fmt.Errorf("no offsets found") |
| 60 | + } |
| 61 | + return offsets, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (s *Symbol) AttachUprobe(exe *link.Executable, prog *ebpf.Program, pid uint32) (link.Link, error) { |
| 65 | + return exe.Uprobe(s.Name(), prog, &link.UprobeOptions{Address: s.Address(), PID: int(pid)}) |
| 66 | +} |
| 67 | + |
| 68 | +func (s *Symbol) AttachUretprobes(exe *link.Executable, prog *ebpf.Program, pid uint32) ([]link.Link, error) { |
| 69 | + returnOffsets, err := s.ReturnOffsets() |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + var links []link.Link |
| 74 | + for _, offset := range returnOffsets { |
| 75 | + l, err := exe.Uprobe("pthread_cond_timedwait", prog, &link.UprobeOptions{Address: s.Address(), Offset: uint64(offset), PID: int(pid)}) |
| 76 | + if err != nil { |
| 77 | + return links, err |
| 78 | + } |
| 79 | + links = append(links, l) |
| 80 | + } |
| 81 | + |
| 82 | + return links, nil |
| 83 | +} |
| 84 | + |
| 85 | +type ELFFile struct { |
| 86 | + elf *elf.File |
| 87 | + symbols []elf.Symbol |
| 88 | + textSection *elf.Section |
| 89 | + textSectionReader io.ReadSeeker |
| 90 | +} |
| 91 | + |
| 92 | +func OpenELFFile(path string) (*ELFFile, error) { |
| 93 | + file, err := elf.Open(path) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + return &ELFFile{elf: file}, nil |
| 98 | +} |
| 99 | + |
| 100 | +func (f *ELFFile) readSymbols() error { |
| 101 | + symbols, _ := f.elf.Symbols() |
| 102 | + dyn, _ := f.elf.DynamicSymbols() |
| 103 | + |
| 104 | + if len(symbols) == 0 && len(dyn) == 0 { |
| 105 | + return fmt.Errorf("no symbols found") |
| 106 | + } |
| 107 | + f.symbols = append(symbols, dyn...) |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func (f *ELFFile) GetSymbol(name string) (*Symbol, error) { |
| 112 | + if f.symbols == nil { |
| 113 | + if err := f.readSymbols(); err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + } |
| 117 | + var es *elf.Symbol |
| 118 | + for _, s := range f.symbols { |
| 119 | + if elf.ST_TYPE(s.Info) != elf.STT_FUNC || s.Size == 0 || s.Value == 0 { |
| 120 | + continue |
| 121 | + } |
| 122 | + if s.Name == name && s.VersionIndex&0x8000 == 0 { |
| 123 | + es = &s |
| 124 | + break |
| 125 | + } |
| 126 | + } |
| 127 | + if es == nil { |
| 128 | + return nil, fmt.Errorf("symbol %s not found", name) |
| 129 | + } |
| 130 | + return &Symbol{s: es, f: f}, nil |
| 131 | +} |
| 132 | + |
| 133 | +func (f *ELFFile) getTextSectionAndReader() (*elf.Section, io.ReadSeeker, error) { |
| 134 | + if f.textSection == nil { |
| 135 | + f.textSection = f.elf.Section(".text") |
| 136 | + if f.textSection == nil { |
| 137 | + return nil, nil, fmt.Errorf("no .text") |
| 138 | + } |
| 139 | + f.textSectionReader = f.textSection.Open() |
| 140 | + } |
| 141 | + return f.textSection, f.textSectionReader, nil |
| 142 | +} |
| 143 | + |
| 144 | +func (f *ELFFile) Close() error { |
| 145 | + return f.elf.Close() |
| 146 | +} |
| 147 | + |
| 148 | +func getReturnOffsets(machine elf.Machine, instructions []byte) []int { |
| 149 | + var res []int |
| 150 | + switch machine { |
| 151 | + case elf.EM_X86_64: |
| 152 | + for i := 0; i < len(instructions); { |
| 153 | + ins, err := x86asm.Decode(instructions[i:], 64) |
| 154 | + if err == nil && ins.Op == x86asm.RET { |
| 155 | + res = append(res, i) |
| 156 | + } |
| 157 | + i += ins.Len |
| 158 | + } |
| 159 | + case elf.EM_AARCH64: |
| 160 | + for i := 0; i < len(instructions); { |
| 161 | + ins, err := arm64asm.Decode(instructions[i:]) |
| 162 | + if err == nil && ins.Op == arm64asm.RET { |
| 163 | + res = append(res, i) |
| 164 | + } |
| 165 | + i += 4 |
| 166 | + } |
| 167 | + } |
| 168 | + return res |
| 169 | +} |
0 commit comments