-
Notifications
You must be signed in to change notification settings - Fork 0
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: Viet Anh Duong <[email protected]>
- Loading branch information
1 parent
968878d
commit 4ee3795
Showing
3 changed files
with
119 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package bcc | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"math" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const KernelAddressSpace = 0x00ffffffffffffff | ||
|
||
type Symbol struct { | ||
Name string | ||
Module string | ||
Address uint64 | ||
} | ||
|
||
func (k *Symbol) String() string { | ||
if k == nil { | ||
return "" | ||
} | ||
return fmt.Sprintf("module=%s symbol=%s address=0x%016x", k.Module, k.Name, k.Address) | ||
} | ||
|
||
func LoadProcKallsym() ([]*Symbol, error) { | ||
var ret []*Symbol | ||
LoadProcKallsymWithCallback(func(sym *Symbol) { ret = append(ret, sym) }) | ||
return ret, nil | ||
} | ||
|
||
func LoadProcKallsymWithCallback(callback func(sym *Symbol)) error { | ||
f, err := os.Open("/proc/kallsyms") | ||
if err != nil { | ||
return fmt.Errorf("open /proc/kallsyms: %w", err) | ||
} | ||
defer f.Close() | ||
|
||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
parts := strings.Fields(scanner.Text()) | ||
if len(parts) < 3 { // This should never happen | ||
continue | ||
} | ||
sym := &Symbol{Name: parts[2], Module: "kernel"} | ||
|
||
if parts[1][0] == 'b' || parts[1][0] == 'B' || | ||
parts[1][0] == 'd' || parts[1][0] == 'D' || | ||
parts[1][0] == 'r' || parts[1][0] == 'R' { | ||
continue | ||
} | ||
|
||
sym.Address, _ = strconv.ParseUint(parts[0], 16, 0) | ||
if sym.Address == 0 || sym.Address == math.MaxUint64 || sym.Address < KernelAddressSpace { | ||
continue | ||
} | ||
|
||
parts = append(parts, "") | ||
if len(parts[3]) > 0 && parts[3][0] == '[' && parts[3][len(parts[3])-1] == ']' { | ||
sym.Module = parts[3][1 : len(parts[3])-1] | ||
} | ||
|
||
callback(sym) | ||
} | ||
|
||
if err = scanner.Err(); err != nil { | ||
return fmt.Errorf("scanner error: %w", err) | ||
} | ||
return 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,19 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/vietanhduong/go-bpf/bcc" | ||
) | ||
|
||
func main() { | ||
ksyms, err := bcc.LoadProcKallsym() | ||
if err != nil { | ||
log.Printf("Failed to load Kallsyms: %v", err) | ||
os.Exit(1) | ||
} | ||
for _, sym := range ksyms { | ||
log.Println(sym.String()) | ||
} | ||
} |