-
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
d01b1d5
commit 494679f
Showing
10 changed files
with
183 additions
and
143 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
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,69 @@ | ||
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 | ||
} | ||
mods[string(fields[2])] = string(bytes.Trim(fields[3], "[]")) | ||
} | ||
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,34 @@ | ||
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`) | ||
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
Oops, something went wrong.