github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/goid/goid.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build go1.12
    16  // +build !go1.18
    17  
    18  // Check type signatures when updating Go version.
    19  
    20  // Package goid provides the Get function.
    21  package goid
    22  
    23  // Get returns the ID of the current goroutine.
    24  func Get() int64 {
    25  	return getg().goid
    26  }
    27  
    28  // Structs from Go runtime. These may change in the future and require
    29  // updating. These structs are currently the same on both AMD64 and ARM64,
    30  // but may diverge in the future.
    31  
    32  type stack struct {
    33  	lo uintptr
    34  	hi uintptr
    35  }
    36  
    37  type gobuf struct {
    38  	sp   uintptr
    39  	pc   uintptr
    40  	g    uintptr
    41  	ctxt uintptr
    42  	ret  uint64
    43  	lr   uintptr
    44  	bp   uintptr
    45  }
    46  
    47  type g struct {
    48  	stack       stack
    49  	stackguard0 uintptr
    50  	stackguard1 uintptr
    51  
    52  	_panic       uintptr
    53  	_defer       uintptr
    54  	m            uintptr
    55  	sched        gobuf
    56  	syscallsp    uintptr
    57  	syscallpc    uintptr
    58  	stktopsp     uintptr
    59  	param        uintptr
    60  	atomicstatus uint32
    61  	stackLock    uint32
    62  	goid         int64
    63  
    64  	// More fields...
    65  	//
    66  	// We only use goid and the fields before it are only listed to
    67  	// calculate the correct offset.
    68  }
    69  
    70  // Defined in assembly. This can't use go:linkname since runtime.getg() isn't a
    71  // real function, it's a compiler intrinsic.
    72  func getg() *g