Skip to content

Commit

Permalink
proc/native: support core dumping on FreeBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
a committed Mar 15, 2023
1 parent a9d699b commit ec9288e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
46 changes: 46 additions & 0 deletions pkg/proc/native/dump_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package native

import (
"errors"
"unsafe"

"github.com/go-delve/delve/pkg/elfwriter"
"github.com/go-delve/delve/pkg/proc"
)

/*
#include <sys/types.h>
#include <sys/user.h>
#include <libutil.h>
*/
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")
}
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
}
4 changes: 2 additions & 2 deletions pkg/proc/native/dump_other.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion pkg/proc/native/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/proc/proc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5147,7 +5147,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")
}

Expand Down

0 comments on commit ec9288e

Please sign in to comment.