summaryrefslogtreecommitdiffstats
path: root/contrib/go/_std_1.25/src/runtime/set_vma_name_linux.go
blob: 792d6ad34d9102daca8b3d317e79bfd69da0de7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build linux

package runtime

import (
	"internal/runtime/atomic"
	"internal/runtime/syscall"
	"unsafe"
)

var prSetVMAUnsupported atomic.Bool

func setVMANameSupported() bool {
	return !prSetVMAUnsupported.Load()
}

// setVMAName calls prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, start, len, name)
func setVMAName(start unsafe.Pointer, length uintptr, name string) {
	if debug.decoratemappings == 0 || !setVMANameSupported() {
		return
	}

	var sysName [80]byte
	n := copy(sysName[:], " Go: ")
	copy(sysName[n:79], name) // leave final byte zero

	_, _, err := syscall.Syscall6(syscall.SYS_PRCTL, syscall.PR_SET_VMA, syscall.PR_SET_VMA_ANON_NAME, uintptr(start), length, uintptr(unsafe.Pointer(&sysName[0])), 0)
	if err == _EINVAL {
		prSetVMAUnsupported.Store(true)
	}
	// ignore other errors
}