From 66b56bdbc0d3a42785802f13be39430e6dbc9605 Mon Sep 17 00:00:00 2001 From: a Date: Wed, 15 Mar 2023 16:44:40 +0100 Subject: [PATCH] proc/native: support core dumping on FreeBSD --- pkg/proc/native/dump_freebsd.go | 48 +++++++++++++++++++++++++++++++++ pkg/proc/native/dump_other.go | 4 +-- pkg/proc/native/proc.go | 2 +- pkg/proc/proc_test.go | 2 +- 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 pkg/proc/native/dump_freebsd.go diff --git a/pkg/proc/native/dump_freebsd.go b/pkg/proc/native/dump_freebsd.go new file mode 100644 index 0000000000..c4e1a97623 --- /dev/null +++ b/pkg/proc/native/dump_freebsd.go @@ -0,0 +1,48 @@ +package native + +import ( + "errors" + "unsafe" + + "github.com/go-delve/delve/pkg/elfwriter" + "github.com/go-delve/delve/pkg/proc" +) + +/* +#include +#include +#include +#include +*/ +import "C" + +func (p *nativeProcess) MemoryMap() ([]proc.MemoryMapEntry, error) { + var cnt C.int + vmentries := C.kinfo_getvmmap(C.int(p.pid), &cnt) + if vmentries == nil { + return nil, errors.New("kinfo_getvmmap call failed") + } + defer C.free(unsafe.Pointer(vmentries)) + r := make([]proc.MemoryMapEntry, 0, int(cnt)) + base := uintptr(unsafe.Pointer(vmentries)) + sz := unsafe.Sizeof(C.struct_kinfo_vmentry{}) + for i := 0; i < int(cnt); i++ { + vmentry := (*C.struct_kinfo_vmentry)(unsafe.Pointer(base + sz*uintptr(i))) + switch vmentry.kve_type { + case C.KVME_TYPE_DEFAULT, C.KVME_TYPE_VNODE, C.KVME_TYPE_SWAP, C.KVME_TYPE_PHYS: + r = append(r, proc.MemoryMapEntry{ + Addr: uint64(vmentry.kve_start), + Size: uint64(vmentry.kve_end - vmentry.kve_start), + + Read: vmentry.kve_protection&C.KVME_PROT_READ != 0, + Write: vmentry.kve_protection&C.KVME_PROT_WRITE != 0, + Exec: vmentry.kve_protection&C.KVME_PROT_EXEC != 0, + }) + } + } + return r, nil +} + +func (p *nativeProcess) DumpProcessNotes(notes []elfwriter.Note, threadDone func()) (threadsDone bool, notesout []elfwriter.Note, err error) { + return false, notes, nil +} diff --git a/pkg/proc/native/dump_other.go b/pkg/proc/native/dump_other.go index e4ebf44217..9422ff296a 100644 --- a/pkg/proc/native/dump_other.go +++ b/pkg/proc/native/dump_other.go @@ -1,5 +1,5 @@ -//go:build (freebsd && amd64) || darwin || (windows && arm64) -// +build freebsd,amd64 darwin windows,arm64 +//go:build darwin || (windows && arm64) +// +build darwin windows,arm64 package native diff --git a/pkg/proc/native/proc.go b/pkg/proc/native/proc.go index c3d6c86433..69423f29dc 100644 --- a/pkg/proc/native/proc.go +++ b/pkg/proc/native/proc.go @@ -318,7 +318,7 @@ func (dbp *nativeProcess) initialize(path string, debugInfoDirs []string) (*proc DisableAsyncPreempt: runtime.GOOS == "windows" || (runtime.GOOS == "linux" && runtime.GOARCH == "arm64"), StopReason: stopReason, - CanDump: runtime.GOOS == "linux" || (runtime.GOOS == "windows" && runtime.GOARCH == "amd64"), + CanDump: runtime.GOOS == "linux" || runtime.GOOS == "freebsd" || (runtime.GOOS == "windows" && runtime.GOARCH == "amd64"), }) procgrp.addTarget = addTarget tgt, err := procgrp.add(dbp, dbp.pid, dbp.memthread, path, stopReason) diff --git a/pkg/proc/proc_test.go b/pkg/proc/proc_test.go index 91c85fd9fd..a16a5ae9bd 100644 --- a/pkg/proc/proc_test.go +++ b/pkg/proc/proc_test.go @@ -5160,7 +5160,7 @@ func TestIssue2319(t *testing.T) { } func TestDump(t *testing.T) { - if runtime.GOOS == "freebsd" || (runtime.GOOS == "darwin" && testBackend == "native") || (runtime.GOOS == "windows" && runtime.GOARCH != "amd64") { + if (runtime.GOOS == "darwin" && testBackend == "native") || (runtime.GOOS == "windows" && runtime.GOARCH != "amd64") { t.Skip("not supported") }