-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bryce Kahle <bryce.kahle@datadoghq.com>
- Loading branch information
1 parent
ff37506
commit ce7b452
Showing
8 changed files
with
295 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package kallsyms | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"io" | ||
"os" | ||
"sync" | ||
) | ||
|
||
var kernelModules struct { | ||
sync.RWMutex | ||
// function to kernel module mapping | ||
kmods map[string]string | ||
} | ||
|
||
// KernelModule returns the kernel module, if any, a probe-able function is contained in. | ||
func KernelModule(fn string) (string, error) { | ||
kernelModules.RLock() | ||
kmods := kernelModules.kmods | ||
kernelModules.RUnlock() | ||
|
||
if kmods == nil { | ||
kernelModules.Lock() | ||
defer kernelModules.Unlock() | ||
kmods = kernelModules.kmods | ||
} | ||
|
||
if kmods != nil { | ||
return kmods[fn], nil | ||
} | ||
|
||
f, err := os.Open("/proc/kallsyms") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
kmods, err = loadKernelModuleMapping(f) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
kernelModules.kmods = kmods | ||
return kmods[fn], nil | ||
} | ||
|
||
// FlushKernelModuleCache removes any cached information about function to kernel module mapping. | ||
func FlushKernelModuleCache() { | ||
kernelModules.Lock() | ||
defer kernelModules.Unlock() | ||
|
||
kernelModules.kmods = nil | ||
} | ||
|
||
func loadKernelModuleMapping(f io.Reader) (map[string]string, error) { | ||
mods := make(map[string]string) | ||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
fields := bytes.Fields(scanner.Bytes()) | ||
if len(fields) < 4 { | ||
continue | ||
} | ||
switch string(fields[1]) { | ||
case "t", "T": | ||
mods[string(fields[2])] = string(bytes.Trim(fields[3], "[]")) | ||
default: | ||
continue | ||
} | ||
} | ||
if scanner.Err() != nil { | ||
return nil, scanner.Err() | ||
} | ||
return mods, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package kallsyms | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/go-quicktest/qt" | ||
) | ||
|
||
func TestKernelModule(t *testing.T) { | ||
kallsyms := []byte(`0000000000000000 t hid_generic_probe [hid_generic] | ||
0000000000000000 T tcp_connect | ||
0000000000000000 B empty_zero_page | ||
0000000000000000 D kimage_vaddr | ||
0000000000000000 R __start_pci_fixups_early | ||
0000000000000000 V hv_root_partition | ||
0000000000000000 W calibrate_delay_is_known | ||
0000000000000000 a nft_counter_seq [nft_counter] | ||
0000000000000000 b bootconfig_found | ||
0000000000000000 d __func__.10 | ||
0000000000000000 r __ksymtab_LZ4_decompress_fast`) | ||
krdr := bytes.NewBuffer(kallsyms) | ||
kmods, err := loadKernelModuleMapping(krdr) | ||
qt.Assert(t, qt.IsNil(err)) | ||
|
||
// present and in module | ||
kmod := kmods["hid_generic_probe"] | ||
if kmod != "hid_generic" { | ||
t.Errorf("expected %q got %q", "hid_generic", kmod) | ||
} | ||
|
||
// present but not kernel module | ||
kmod = kmods["tcp_connect"] | ||
if kmod != "" { | ||
t.Errorf("expected %q got %q", "", kmod) | ||
} | ||
|
||
// not present | ||
kmod = kmods["asdfasdf"] | ||
if kmod != "" { | ||
t.Errorf("expected %q got %q", "", kmod) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ebpf | ||
|
||
import "github.com/cilium/ebpf/internal/kallsyms" | ||
|
||
// FlushKernelModuleCache removes any cached information about function to kernel module mapping. | ||
func FlushKernelModuleCache() { | ||
kallsyms.FlushKernelModuleCache() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.