// Copyright 2019 The Prometheus Authors// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//go:build (aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris) && !js// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris// +build !jspackage procfsimport ()// ProcMapPermissions contains permission settings read from `/proc/[pid]/maps`.typeProcMapPermissionsstruct {// mapping has the [R]ead flag set Read bool// mapping has the [W]rite flag set Write bool// mapping has the [X]ecutable flag set Execute bool// mapping has the [S]hared flag set Shared bool// mapping is marked as [P]rivate (copy on write) Private bool}// ProcMap contains the process memory-mappings of the process// read from `/proc/[pid]/maps`.typeProcMapstruct {// The start address of current mapping. StartAddr uintptr// The end address of the current mapping EndAddr uintptr// The permissions for this mapping Perms *ProcMapPermissions// The current offset into the file/fd (e.g., shared libs) Offset int64// Device owner of this mapping (major:minor) in Mkdev format. Dev uint64// The inode of the device above Inode uint64// The file or psuedofile (or empty==anonymous) Pathname string}// parseDevice parses the device token of a line and converts it to a dev_t// (mkdev) like structure.func parseDevice( string) (uint64, error) { := strings.Index(, ":")if == -1 {return0, fmt.Errorf("%w: expected separator `:` in %s", ErrFileParse, ) } , := strconv.ParseUint([0:], 16, 0)if != nil {return0, } , := strconv.ParseUint([+1:], 16, 0)if != nil {return0, }returnunix.Mkdev(uint32(), uint32()), nil}// parseAddress converts a hex-string to a uintptr.func parseAddress( string) (uintptr, error) { , := strconv.ParseUint(, 16, 0)if != nil {return0, }returnuintptr(), nil}// parseAddresses parses the start-end address.func parseAddresses( string) (uintptr, uintptr, error) { := strings.Index(, "-")if == -1 {return0, 0, fmt.Errorf("%w: expected separator `-` in %s", ErrFileParse, ) } , := parseAddress([0:])if != nil {return0, 0, } , := parseAddress([+1:])if != nil {return0, 0, }return , , nil}// parsePermissions parses a token and returns any that are set.func parsePermissions( string) (*ProcMapPermissions, error) {iflen() < 4 {returnnil, fmt.Errorf("%w: invalid permissions token", ErrFileParse) } := ProcMapPermissions{}for , := range {switch {case'r': .Read = truecase'w': .Write = truecase'x': .Execute = truecase'p': .Private = truecase's': .Shared = true } }return &, nil}// parseProcMap will attempt to parse a single line within a proc/[pid]/maps// buffer.func parseProcMap( string) (*ProcMap, error) { := strings.Fields()iflen() < 5 {returnnil, fmt.Errorf("%w: truncated procmap entry", ErrFileParse) } , , := parseAddresses([0])if != nil {returnnil, } , := parsePermissions([1])if != nil {returnnil, } , := strconv.ParseInt([2], 16, 0)if != nil {returnnil, } , := parseDevice([3])if != nil {returnnil, } , := strconv.ParseUint([4], 10, 0)if != nil {returnnil, } := ""iflen() >= 5 { = strings.Join([5:], " ") }return &ProcMap{StartAddr: ,EndAddr: ,Perms: ,Offset: ,Dev: ,Inode: ,Pathname: , }, nil}// ProcMaps reads from /proc/[pid]/maps to get the memory-mappings of the// process.func ( Proc) () ([]*ProcMap, error) { , := os.Open(.path("maps"))if != nil {returnnil, }defer .Close() := []*ProcMap{} := bufio.NewScanner()for .Scan() { , := parseProcMap(.Text())if != nil {returnnil, } = append(, ) }return , nil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.