Skip to content

Commit

Permalink
Add an ability to resolve addresses of kernel symbols
Browse files Browse the repository at this point in the history
This is convenient if you want to read the value of some global variable from
the data section. Note that you need `CONFIG_KALLSYMS_ALL` to make us of it.

Removing unused `ksyms` member of `Exporter` as well.
  • Loading branch information
bobrik committed Sep 24, 2021
1 parent 50bf3d5 commit a17be46
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,9 @@ perf_events:
cflags:
[ - -I/include/path
- -DMACRO_NAME=value ]
# Kernel symbol addresses to define as kaddr_{symbol} from /proc/kallsyms (consider CONFIG_KALLSYMS_ALL)
kaddrs:
[ - symbol_to_resolve ]
# Actual eBPF program code to inject in the kernel
code: [ code ]
```
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Program struct {
PerfEvents []PerfEvent `yaml:"perf_events"`
Code string `yaml:"code"`
Cflags []string `yaml:"cflags"`
Kaddrs []string `yaml:"kaddrs"`
}

// PerfEvent describes perf_event to attach to
Expand Down
66 changes: 63 additions & 3 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package exporter

import (
"bufio"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"

"github.com/cloudflare/ebpf_exporter/config"
"github.com/cloudflare/ebpf_exporter/decoder"
Expand All @@ -20,7 +23,7 @@ type Exporter struct {
config config.Config
modules map[string]*bcc.Module
perfMapCollectors []*PerfMapSink
ksyms map[uint64]string
kaddrs map[string]uint64
enabledProgramsDesc *prometheus.Desc
programInfoDesc *prometheus.Desc
programTags map[string]map[string]uint64
Expand Down Expand Up @@ -52,7 +55,7 @@ func New(cfg config.Config) (*Exporter, error) {
return &Exporter{
config: cfg,
modules: map[string]*bcc.Module{},
ksyms: map[uint64]string{},
kaddrs: map[string]uint64{},
enabledProgramsDesc: enabledProgramsDesc,
programInfoDesc: programInfoDesc,
programTags: map[string]map[string]uint64{},
Expand All @@ -68,7 +71,12 @@ func (e *Exporter) Attach() error {
return fmt.Errorf("multiple programs with name %q", program.Name)
}

module := bcc.NewModule(program.Code, program.Cflags)
code, err := e.code(program)
if err != nil {
return err
}

module := bcc.NewModule(code, program.Cflags)
if module == nil {
return fmt.Errorf("error compiling module for program %q", program.Name)
}
Expand Down Expand Up @@ -99,6 +107,58 @@ func (e *Exporter) Attach() error {
return nil
}

// code generates program code, augmented if necessary
func (e Exporter) code(program config.Program) (string, error) {
preamble := ""

if len(program.Kaddrs) > 0 && len(e.kaddrs) == 0 {
if err := e.populateKaddrs(); err != nil {
return "", err
}
}

defines := make([]string, 0, len(program.Kaddrs))
for _, kaddr := range program.Kaddrs {
defines = append(defines, fmt.Sprintf("#define kaddr_%s 0x%x", kaddr, e.kaddrs[kaddr]))
}

preamble = preamble + strings.Join(defines, "\n")

if preamble == "" {
return program.Code, nil
}

return preamble + "\n\n" + program.Code, nil
}

// populateKaddrs populates cache of ksym -> kaddr mappings
// TODO: move to github.com/iovisor/gobpf/pkg/ksym
func (e Exporter) populateKaddrs() error {
fd, err := os.Open("/proc/kallsyms")
if err != nil {
return err
}

defer fd.Close()

s := bufio.NewScanner(fd)
for s.Scan() {
parts := strings.Split(s.Text(), " ")
if len(parts) != 3 {
continue
}

addr, err := strconv.ParseUint(parts[0], 16, 64)
if err != nil {
return fmt.Errorf("error parsing addr %q from line %q: %s", parts[0], s.Text(), err)
}

e.kaddrs[parts[2]] = addr
}

return s.Err()
}

// Describe satisfies prometheus.Collector interface by sending descriptions
// for all metrics the exporter can possibly report
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
Expand Down

0 comments on commit a17be46

Please sign in to comment.