...

Source file src/runtime/pprof/proto_windows.go

Documentation: runtime/pprof

     1  // Copyright 2022 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  	"errors"
     9  	"internal/syscall/windows"
    10  	"os"
    11  	"syscall"
    12  )
    13  
    14  // readMapping adds memory mapping information to the profile.
    15  func (b *profileBuilder) readMapping() {
    16  	snap, err := createModuleSnapshot()
    17  	if err != nil {
    18  		// pprof expects a map entry, so fake one, when we haven't added anything yet.
    19  		b.addMappingEntry(0, 0, 0, "", "", true)
    20  		return
    21  	}
    22  	defer func() { _ = syscall.CloseHandle(snap) }()
    23  
    24  	var module windows.ModuleEntry32
    25  	module.Size = uint32(windows.SizeofModuleEntry32)
    26  	err = windows.Module32First(snap, &module)
    27  	if err != nil {
    28  		// pprof expects a map entry, so fake one, when we haven't added anything yet.
    29  		b.addMappingEntry(0, 0, 0, "", "", true)
    30  		return
    31  	}
    32  	for err == nil {
    33  		exe := syscall.UTF16ToString(module.ExePath[:])
    34  		b.addMappingEntry(
    35  			uint64(module.ModBaseAddr),
    36  			uint64(module.ModBaseAddr)+uint64(module.ModBaseSize),
    37  			0,
    38  			exe,
    39  			peBuildID(exe),
    40  			false,
    41  		)
    42  		err = windows.Module32Next(snap, &module)
    43  	}
    44  }
    45  
    46  func readMainModuleMapping() (start, end uint64, exe, buildID string, err error) {
    47  	exe, err = os.Executable()
    48  	if err != nil {
    49  		return 0, 0, "", "", err
    50  	}
    51  	snap, err := createModuleSnapshot()
    52  	if err != nil {
    53  		return 0, 0, "", "", err
    54  	}
    55  	defer func() { _ = syscall.CloseHandle(snap) }()
    56  
    57  	var module windows.ModuleEntry32
    58  	module.Size = uint32(windows.SizeofModuleEntry32)
    59  	err = windows.Module32First(snap, &module)
    60  	if err != nil {
    61  		return 0, 0, "", "", err
    62  	}
    63  
    64  	return uint64(module.ModBaseAddr), uint64(module.ModBaseAddr) + uint64(module.ModBaseSize), exe, peBuildID(exe), nil
    65  }
    66  
    67  func createModuleSnapshot() (syscall.Handle, error) {
    68  	for {
    69  		snap, err := syscall.CreateToolhelp32Snapshot(windows.TH32CS_SNAPMODULE|windows.TH32CS_SNAPMODULE32, uint32(syscall.Getpid()))
    70  		var errno syscall.Errno
    71  		if err != nil && errors.As(err, &errno) && errno == windows.ERROR_BAD_LENGTH {
    72  			// When CreateToolhelp32Snapshot(SNAPMODULE|SNAPMODULE32, ...) fails
    73  			// with ERROR_BAD_LENGTH then it should be retried until it succeeds.
    74  			continue
    75  		}
    76  		return snap, err
    77  	}
    78  }
    79  

View as plain text