Skip to content

Commit

Permalink
c/debug: func addr and info
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Nov 28, 2024
1 parent 38a7f4f commit e76fcc2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
27 changes: 27 additions & 0 deletions c/debug/_demo/funcinfo/main.go
Original file line number Diff line number Diff line change
@@ -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.Fbase, info.Saddr)
}

func main() {
t := &T{100}
t.Demo()
}
14 changes: 14 additions & 0 deletions c/debug/_wrap/debug.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#if defined(__linux__)
#define _GNU_SOURCE
#include <features.h>
#endif

#include <dlfcn.h>

void *llgo_address() {
return __builtin_return_address(0);
}

int llgo_addrinfo(void *addr, Dl_info *info) {
return dladdr(addr, info);
}
25 changes: 25 additions & 0 deletions c/debug/debug.go
Original file line number Diff line number Diff line change
@@ -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
Fbase 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
1 change: 1 addition & 0 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, linkArgs
"-lm",
"-latomic",
"-lpthread", // libpthread is built-in since glibc 2.34 (2021-08-01); we need to support earlier versions.
"-rdynamic", // support addrinfo
)
}
needRuntime := false
Expand Down

0 comments on commit e76fcc2

Please sign in to comment.