github.com/luchsh/agentlib.go@v0.0.0-20221115155834-ffd0caec4d72/jgo/load.go (about) 1 // 2 // Copyright 2020 chuanshenglu@gmail.com 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 package jgo 18 19 //#include "wrapper.h" 20 import "C" 21 22 // AgentLib defines the information of a agent library 23 type AgentLib struct { 24 // The unique instance of JavaVM 25 javaVM JVM 26 // command line options to this agent 27 Options string 28 // current jvmti env 29 jvmti jvmtiEnv 30 // Callbacks 31 callbacks JvmtiCallbacks 32 } 33 34 // GetCallbacks returns the callbacks of this lib 35 func (agent *AgentLib) GetCallbacks() *JvmtiCallbacks { 36 return &agent.callbacks 37 } 38 39 // The global instance of this agent lib 40 var ( 41 _lib *AgentLib 42 Onload AgentOnLoadCallback 43 Onunload AgentOnUnloadCallback 44 ) 45 46 type AgentOnLoadCallback func(*AgentLib) 47 type AgentOnUnloadCallback func() 48 49 //export OnAgentLoad 50 func OnAgentLoad(javaVM, jvmti uintptr, options *C.char) { 51 _lib = new(AgentLib) 52 _lib.javaVM = JVM(javaVM) 53 _lib.Options = C.GoString(options) 54 _lib.jvmti = jvmtiEnv(jvmti) 55 _lib.callbacks.init() 56 57 if Onload != nil { 58 Onload(_lib) 59 } 60 } 61 62 //export OnAgentUnload 63 func OnAgentUnload() int32 { 64 if Onunload != nil { 65 Onunload() 66 } 67 return 0 68 } 69 70 //export MainForwardLoop 71 func MainForwardLoop() { 72 // TODO: cross-runtime forwarding mechanism 73 }