| 1 |
// Copyright 2023 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package pprof |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/byteorder" |
| 9 |
"os" |
| 10 |
"unsafe" |
| 11 |
) |
| 12 |
|
| 13 |
func isExecutable(protection int32) bool { |
| 14 |
return (protection&_VM_PROT_EXECUTE) != 0 && (protection&_VM_PROT_READ) != 0 |
| 15 |
} |
| 16 |
|
| 17 |
// machVMInfo uses the mach_vm_region region system call to add mapping entries |
| 18 |
// for the text region of the running process. |
| 19 |
func machVMInfo(addMapping func(lo, hi, offset uint64, file, buildID string)) bool { |
| 20 |
added := false |
| 21 |
var addr uint64 = 0x1 |
| 22 |
for { |
| 23 |
var memRegionSize uint64 |
| 24 |
var info machVMRegionBasicInfoData |
| 25 |
// Get the first address and page size. |
| 26 |
kr := mach_vm_region( |
| 27 |
&addr, |
| 28 |
&memRegionSize, |
| 29 |
unsafe.Pointer(&info)) |
| 30 |
if kr != 0 { |
| 31 |
if kr == _MACH_SEND_INVALID_DEST { |
| 32 |
// No more memory regions. |
| 33 |
return true |
| 34 |
} |
| 35 |
return added // return true if at least one mapping was added |
| 36 |
} |
| 37 |
if isExecutable(info.Protection) { |
| 38 |
// NOTE: the meaning/value of Offset is unclear. However, |
| 39 |
// this likely doesn't matter as the text segment's file |
| 40 |
// offset is usually 0. |
| 41 |
addMapping(addr, |
| 42 |
addr+memRegionSize, |
| 43 |
byteorder.LEUint64(info.Offset[:]), |
| 44 |
regionFilename(addr), |
| 45 |
"") |
| 46 |
added = true |
| 47 |
} |
| 48 |
addr += memRegionSize |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func regionFilename(address uint64) string { |
| 53 |
buf := make([]byte, _MAXPATHLEN) |
| 54 |
r := proc_regionfilename( |
| 55 |
os.Getpid(), |
| 56 |
address, |
| 57 |
unsafe.SliceData(buf), |
| 58 |
int64(cap(buf))) |
| 59 |
if r == 0 { |
| 60 |
return "" |
| 61 |
} |
| 62 |
return string(buf[:r]) |
| 63 |
} |
| 64 |
|
| 65 |
// mach_vm_region and proc_regionfilename are implemented by |
| 66 |
// the runtime package (runtime/sys_darwin.go). |
| 67 |
// |
| 68 |
//go:noescape |
| 69 |
func mach_vm_region(address, region_size *uint64, info unsafe.Pointer) int32 |
| 70 |
|
| 71 |
//go:noescape |
| 72 |
func proc_regionfilename(pid int, address uint64, buf *byte, buflen int64) int32 |