github.com/lemon-mint/libuseful@v1.3.1-0.20220724073654-ee73785d5aa0/proc.go (about)

     1  package libuseful
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  //go:linkname ProcPin runtime.procPin
     9  //go:linkname ProcUnpin runtime.procUnpin
    10  //go:linkname GetM runtime.getm
    11  
    12  func ProcPin() int
    13  func ProcUnpin()
    14  func GetM() unsafe.Pointer
    15  
    16  var gType reflect.Type = MustGetType("*runtime.g").Elem()
    17  var offset_g_goid uintptr = MustGetOffset(gType, "goid")
    18  var mType reflect.Type = MustGetType("*runtime.m").Elem()
    19  var offset_m_curg uintptr = MustGetOffset(mType, "curg")
    20  var offset_m_id uintptr = MustGetOffset(mType, "id")
    21  var offset_m_p uintptr = MustGetOffset(mType, "p")
    22  var pType reflect.Type = MustGetType("*runtime.p").Elem()
    23  var offset_p_id uintptr = MustGetOffset(pType, "id")
    24  
    25  func GetG() unsafe.Pointer {
    26  	m := GetM()
    27  	return *(*unsafe.Pointer)(unsafe.Add(m, offset_m_curg))
    28  }
    29  
    30  func GetP() unsafe.Pointer {
    31  	m := GetM()
    32  	return *(*unsafe.Pointer)(unsafe.Add(m, offset_m_p))
    33  }
    34  
    35  func GetMID() int64 {
    36  	m := GetM()
    37  	return *(*int64)(unsafe.Add(m, offset_m_id))
    38  }
    39  
    40  func GetGoID() int64 {
    41  	g := GetG()
    42  	return *(*int64)(unsafe.Add(g, offset_g_goid))
    43  }
    44  
    45  //go:linkname GetGID GetGoID
    46  func GetGID() int64
    47  
    48  func GetPID() int {
    49  	p := GetP()
    50  	return int(*(*int32)(unsafe.Add(p, offset_p_id)))
    51  }