Merge pull request #891 from visualfc/debug
[WIP] c/debug: func addr and info
This commit is contained in:
27
c/debug/_demo/funcinfo/main.go
Normal file
27
c/debug/_demo/funcinfo/main.go
Normal file
@@ -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()
|
||||||
|
}
|
||||||
26
c/debug/_demo/stacktrace/main.go
Normal file
26
c/debug/_demo/stacktrace/main.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/goplus/llgo/c/debug"
|
||||||
|
)
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
n int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *T) Demo() {
|
||||||
|
println(t.n)
|
||||||
|
debug.StackTrace(0, func(fr *debug.Frame) bool {
|
||||||
|
var info debug.Info
|
||||||
|
debug.Addrinfo(unsafe.Pointer(fr.PC), &info)
|
||||||
|
println("[", fr.PC, "]", fr.Name, "+", fr.Offset, ", SP =", fr.SP)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
t := &T{100}
|
||||||
|
t.Demo()
|
||||||
|
}
|
||||||
38
c/debug/_wrap/debug.c
Normal file
38
c/debug/_wrap/debug.c
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#if defined(__linux__)
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
#include <features.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include <libunwind.h>
|
||||||
|
|
||||||
|
void *llgo_address() {
|
||||||
|
return __builtin_return_address(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int llgo_addrinfo(void *addr, Dl_info *info) {
|
||||||
|
return dladdr(addr, info);
|
||||||
|
}
|
||||||
|
|
||||||
|
void llgo_stacktrace(int skip, void *ctx, int (*fn)(void *ctx, void *pc, void *offset, void *sp, char *name)) {
|
||||||
|
unw_cursor_t cursor;
|
||||||
|
unw_context_t context;
|
||||||
|
unw_word_t offset, pc, sp;
|
||||||
|
char fname[256];
|
||||||
|
unw_getcontext(&context);
|
||||||
|
unw_init_local(&cursor, &context);
|
||||||
|
int depth = 0;
|
||||||
|
while (unw_step(&cursor) > 0) {
|
||||||
|
if (depth < skip) {
|
||||||
|
depth++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (unw_get_reg(&cursor, UNW_REG_IP, &pc) == 0) {
|
||||||
|
unw_get_proc_name(&cursor, fname, sizeof(fname), &offset);
|
||||||
|
unw_get_reg(&cursor, UNW_REG_SP, &sp);
|
||||||
|
if (fn(ctx, (void*)pc, (void*)offset, (void*)sp, fname) == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
45
c/debug/debug.go
Normal file
45
c/debug/debug.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
//go:linkname stacktrace C.llgo_stacktrace
|
||||||
|
func stacktrace(skip c.Int, ctx unsafe.Pointer, fn func(ctx, pc, offset, sp unsafe.Pointer, name *c.Char) c.Int)
|
||||||
|
|
||||||
|
type Frame struct {
|
||||||
|
PC uintptr
|
||||||
|
Offset uintptr
|
||||||
|
SP unsafe.Pointer
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func StackTrace(skip int, fn func(fr *Frame) bool) {
|
||||||
|
stacktrace(c.Int(1+skip), unsafe.Pointer(&fn), func(ctx, pc, offset, sp unsafe.Pointer, name *c.Char) c.Int {
|
||||||
|
fn := *(*func(fr *Frame) bool)(ctx)
|
||||||
|
if !fn(&Frame{uintptr(pc), uintptr(offset), sp, c.GoString(name)}) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return 1
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user