Skip to content

Commit

Permalink
Merge pull request #10 from jschwinger233/pr/gray/output-call-without…
Browse files Browse the repository at this point in the history
…-dbgsym

-i 'symbol+*' can work without dbgsym image
  • Loading branch information
jschwinger233 authored Jan 12, 2025
2 parents 2a83bd5 + 4ee2087 commit 220171c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
11 changes: 6 additions & 5 deletions kcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"debug/elf"
"fmt"
log "log/slog"
"os"
"sync"

Expand Down Expand Up @@ -59,7 +60,7 @@ func (k *Kcore) ParseInsns(symbol string) (insns map[uint64]*Instruction, err er
insns = make(map[uint64]*Instruction)
kdwarf, err := GetKdwarf()
if err != nil {
return
log.Debug("Failed to get kdwarf", "err", err)
}

ksym, err := KsymByName(symbol)
Expand All @@ -74,7 +75,8 @@ func (k *Kcore) ParseInsns(symbol string) (insns map[uint64]*Instruction, err er
if prog.Vaddr <= addr && prog.Vaddr+prog.Memsz >= addr {
bytes := make([]byte, leng)
if _, err = k.file.ReadAt(bytes, int64(prog.Off+addr-prog.Vaddr)); err != nil {
fmt.Println(err)
log.Debug("Failed to read kcore", "err", err)
continue
}
if len(bytes) == 0 {
continue
Expand Down Expand Up @@ -112,9 +114,8 @@ func (k *Kcore) ParseInsns(symbol string) (insns map[uint64]*Instruction, err er
break
}
}
lineInfo, err := kdwarf.GetLineInfo(symbol, uint64(off))
if err == nil {
insn.LineInfo = lineInfo
if kdwarf != nil {
insn.LineInfo, _ = kdwarf.GetLineInfo(symbol, uint64(off))
}
insns[uint64(off)] = &insn
}
Expand Down
49 changes: 30 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func main() {
}
defer k.Close()

var attachByLine bool
kdwarf, _ := GetKdwarf()
allInsns := map[string]map[uint64]*Instruction{}
for _, target := range config.Targets {
match := targetPattern.FindStringSubmatch(target)
Expand All @@ -124,8 +124,9 @@ func main() {
var ok bool
var err error
var address uint64
var attachByInsn bool
if result["addr"] == "*" {
attachByLine = true
attachByInsn = true
delete(result, "addr")
}
if result["addr"] != "" {
Expand Down Expand Up @@ -159,7 +160,7 @@ func main() {
}
symbol = ksym.Name

if attachByLine {
if attachByInsn {
kcore, err := NewKcore()
if err != nil {
log.Error("Failed to new kcore", "err", err)
Expand All @@ -184,6 +185,19 @@ func main() {
}

} else {
insns, ok := allInsns[symbol]
if !ok {
allInsns[symbol] = map[uint64]*Instruction{}
insns = allInsns[symbol]
}
insns[offset] = &Instruction{
Symbol: symbol,
Offset: offset,
}
if kdwarf != nil {
insns[offset].LineInfo, _ = kdwarf.GetLineInfo(symbol, offset)
}

k, err = link.Kprobe(symbol, objs.KprobeSkbBySearch, &link.KprobeOptions{Offset: offset})
if err != nil {
log.Error("Failed to attach", "target", target, "err", err)
Expand Down Expand Up @@ -217,7 +231,6 @@ func main() {
}
}
for _, skbBuildFunc := range skbBuildFuncs {

ksym, err := KsymByName(skbBuildFunc)
if err != nil {
log.Error("Failed to find ksym", "symbol", skbBuildFunc, "err", err)
Expand Down Expand Up @@ -295,11 +308,11 @@ func main() {
ksym, _ := NearestKsym(event.At)

var insn *Instruction
if attachByLine {
var ok bool
insn, ok = allInsns[ksym.Name][event.At-ksym.Addr-1]
insns, ok := allInsns[ksym.Name]
if ok {
insn, ok = insns[event.At-ksym.Addr-1]
if !ok {
insn, ok = allInsns[ksym.Name][event.At-ksym.Addr-4]
insn, ok = insns[event.At-ksym.Addr-4]
if !ok {
log.Error("Failed to find insn", "symbol", ksym.Name, "offset", event.At-ksym.Addr)
return
Expand All @@ -308,17 +321,15 @@ func main() {
}

fmt.Printf("%-4d %-18x %-10d %-18x %-16s", i, event.Skb, event.SkbLen, event.At, fmt.Sprintf("%s+%d", ksym.Name, event.At-ksym.Addr))
if attachByLine {
if insn.LineInfo != nil {
fmt.Printf(" %s:%d", insn.Filename, insn.Line)
}
if insn.Call {
ksym, err := KsymByAddr(event.Call)
if event.Call != 0 && err == nil {
fmt.Printf(" // CALL %s", ksym.Name)
} else {
fmt.Printf(" // CALL %s", insn.CallTarget)
}
if insn.LineInfo != nil {
fmt.Printf(" %s:%d", insn.Filename, insn.Line)
}
if insn.Call {
ksym, err := KsymByAddr(event.Call)
if event.Call != 0 && err == nil {
fmt.Printf(" // CALL %s", ksym.Name)
} else {
fmt.Printf(" // CALL %s", insn.CallTarget)
}
}
fmt.Println()
Expand Down

0 comments on commit 220171c

Please sign in to comment.