From be09486663a28673fca95557d1470a8d98c53f68 Mon Sep 17 00:00:00 2001 From: Katherine Cox-Buday Date: Thu, 2 Dec 2021 15:45:31 -0600 Subject: [PATCH] fix: When extracting PCs from stack frames, try the `PC` field Some error types may return stack traces already defined as `[]runtime.Frame`. In this case, the program counter will be in the `PC` field. fixes: #391 --- stacktrace.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/stacktrace.go b/stacktrace.go index 1c3320f5..0bf5bc3d 100644 --- a/stacktrace.go +++ b/stacktrace.go @@ -114,17 +114,20 @@ func extractPcs(method reflect.Value) []uintptr { for i := 0; i < stacktrace.Len(); i++ { pc := stacktrace.Index(i) - if pc.Kind() == reflect.Uintptr { + switch pc.Kind() { + case reflect.Uintptr: pcs = append(pcs, uintptr(pc.Uint())) - continue - } - - if pc.Kind() == reflect.Struct { - field := pc.FieldByName("ProgramCounter") - if field.IsValid() && field.Kind() == reflect.Uintptr { - pcs = append(pcs, uintptr(field.Uint())) - continue - } + case reflect.Struct: + for _, fieldName := range []string{"ProgramCounter", "PC"} { + field := pc.FieldByName(fieldName) + if !field.IsValid() { + continue + } + if field.Kind() == reflect.Uintptr { + pcs = append(pcs, uintptr(field.Uint())) + break + } + } } }