From 15279d23b63b71845e50005ec0746d16c3b2cf5f Mon Sep 17 00:00:00 2001 From: alexkr000 Date: Mon, 7 Jul 2025 11:08:48 -0700 Subject: [PATCH] Update trex-emu.go: Printing stack frames upon hitting a panic Previously, once a panic was triggered, EMU stopped functioning without any diagnostic message (neither on the console nor in /var/log/trex/emu_daemon_server.log) With this fix, the stack traces will appear in the log file to speed up diagnostic --- src/cmd/trex-emu.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/cmd/trex-emu.go b/src/cmd/trex-emu.go index b062477..75f2f3c 100644 --- a/src/cmd/trex-emu.go +++ b/src/cmd/trex-emu.go @@ -11,6 +11,7 @@ import ( "emu/version" "errors" "fmt" + "runtime" "log" "math/rand" "os" @@ -249,6 +250,19 @@ func RunCoreZmq(args *MainArgs) { } } +func captureStackTrace() { + buf := make([]byte, 1024*10) // Allocate a buffer for the stack trace + n := runtime.Stack(buf, true) // true captures all goroutines + fmt.Printf("Stack trace:\n%s\n", buf[:n]) +} + func main() { + defer func() { + if r := recover(); r != nil { + fmt.Println("Recovered from panic:", r) + captureStackTrace() + } + }() + RunCoreZmq(parseMainArgs()) }