Skip to content

Commit

Permalink
error handling stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
carminecesarano committed Jan 13, 2025
1 parent 89843e1 commit 7cd9ba1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
13 changes: 12 additions & 1 deletion eBPFleash/stackanalyzer/stackanalyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
maxStackDepth = 20
maxStackDepth = 32
)

type ImportedPackages struct {
Expand Down Expand Up @@ -44,19 +44,30 @@ func LoadModuleCache(modManifest string) error {
}

func GetStackTrace(stacktraces *ebpf.Map, stackID uint32) ([]uint64, error) {

if stackID == 0 {
return nil, fmt.Errorf("Invalid stack ID")
}

var stackTrace [maxStackDepth]uint64
err := stacktraces.Lookup(stackID, &stackTrace)
if err != nil {
return nil, err
}

// Valid stack entries
var result []uint64
for _, addr := range stackTrace {
if addr == 0 {
break
}
result = append(result, addr)
}

if len(result) == 0 {
return nil, fmt.Errorf("Empty stack trace")
}

return result, nil
}

Expand Down
35 changes: 27 additions & 8 deletions eBPFleash/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ import (
)

func logEvent(event ebpfEvent, stackTrace []uint64) {

/*
if len(stackTrace) == 0 {
log.Printf("No valid stack trace available for syscall: %d", event.Syscall)
return
}
*/

resolvedStackTrace := stackanalyzer.ResolveSymbols(stackTrace)
/*
if resolvedStackTrace == "" {
log.Printf("Could not resolve symbols for stack trace")
}
*/

callerPackage, callerFunction, err := stackanalyzer.GetCallerPackageAndFunction(stackTrace)
if err != nil {
log.Printf("Error getting caller package: %v", err)
Expand All @@ -30,13 +44,14 @@ func logEvent(event ebpfEvent, stackTrace []uint64) {
log.Printf("Invoked syscall: %d\tpid: %d\tcomm: %s\n",
event.Syscall, event.Pid, unix.ByteSliceToString(event.Comm[:]))

if callerPackage != "" && callerFunction != "" {
log.Printf("Stack Trace:\n%s", resolvedStackTrace)
log.Printf("Go caller package: %s", callerPackage)
log.Printf("Go caller function: %s", callerFunction)
} else {
log.Printf("Go Runtime Invocation")
}
//if callerPackage != "" && callerFunction != "" {
log.Printf("Stack Trace:\n%s", resolvedStackTrace)
log.Printf("Go caller package: %s", callerPackage)
log.Printf("Go caller function: %s", callerFunction)
//} else {
// log.Printf("Go Runtime Invocation")
//}

}

func loadEBPF() (*ebpfObjects, *ringbuf.Reader, *link.Link, error) {
Expand Down Expand Up @@ -114,7 +129,11 @@ func setupAndRun(binaryPath string, modManifestPath string, processEvent func(eb
continue
}

stackTrace, _ := stackanalyzer.GetStackTrace(objs.Stacktraces, event.StackId)
stackTrace, err := stackanalyzer.GetStackTrace(objs.Stacktraces, event.StackId)
if err != nil {
log.Printf("Getting stack trace: %s", err)
continue
}
processEvent(event, stackTrace, objs)
}
}
Expand Down

0 comments on commit 7cd9ba1

Please sign in to comment.