From 04b8f8e8d3eacb2d3f5df51fe3ff08bd3d51326e Mon Sep 17 00:00:00 2001 From: visualfc Date: Thu, 28 Nov 2024 12:01:11 +0800 Subject: [PATCH] c/debug: func addr and info --- c/debug/_demo/funcinfo/main.go | 27 +++++++++++++++++++++++++++ c/debug/_wrap/debug.c | 9 +++++++++ c/debug/debug.go | 25 +++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 c/debug/_demo/funcinfo/main.go create mode 100644 c/debug/_wrap/debug.c create mode 100644 c/debug/debug.go diff --git a/c/debug/_demo/funcinfo/main.go b/c/debug/_demo/funcinfo/main.go new file mode 100644 index 000000000..5bfbcd91e --- /dev/null +++ b/c/debug/_demo/funcinfo/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/debug" +) + +type T struct { + n int +} + +func (t *T) Demo() { + println(t.n) + addr := debug.Address() + c.Printf(c.Str("addr:0x%x\n"), addr) + var info debug.Info + r := debug.Addrinfo(addr, &info) + if r == 0 { + panic("not found info") + } + c.Printf(c.Str("func file:%s name:%s base:0x%x addr:0x%x\n"), info.Fname, info.Sname, info.Base, info.Saddr) +} + +func main() { + t := &T{100} + t.Demo() +} diff --git a/c/debug/_wrap/debug.c b/c/debug/_wrap/debug.c new file mode 100644 index 000000000..7867227dd --- /dev/null +++ b/c/debug/_wrap/debug.c @@ -0,0 +1,9 @@ +#include + +void *llgo_address() { + return __builtin_return_address(0); +} + +int llgo_addrinfo(void *addr, Dl_info *info) { + return dladdr(addr, info); +} diff --git a/c/debug/debug.go b/c/debug/debug.go new file mode 100644 index 000000000..5a98eec40 --- /dev/null +++ b/c/debug/debug.go @@ -0,0 +1,25 @@ +package debug + +import ( + "unsafe" + + "github.com/goplus/llgo/c" +) + +const ( + LLGoPackage = "link" + LLGoFiles = "_wrap/debug.c" +) + +type Info struct { + Fname *c.Char + Base c.Pointer + Sname *c.Char + Saddr c.Pointer +} + +//go:linkname Address C.llgo_address +func Address() unsafe.Pointer + +//go:linkname Addrinfo C.llgo_addrinfo +func Addrinfo(addr unsafe.Pointer, info *Info) c.Int