JVMTI Attach機制與核心原始碼分析

猿碼道發表於2018-05-29

0 前言

前面文章,我們已講述了《基於JVMTI的Agent實現》《基於Java Instrument的Agent實現》兩種Agent的實現方式,其中每種方式都會分為:啟動時Agent、執行時Agent

對於 啟動時Agent的觸發機制,在上一節《JVMTI Agent 工作原理及核心原始碼分析》中,已經在原始碼級進行了分析,具體如下:

載入Agent連結庫,觸發呼叫Agent_OnLoad方法

但是對於 執行時Agent的觸發機制,卻沒有進行詳細說明,本節的主要目標就是在原始碼級分析下JVMTI Attach 工作機制。

1 Attach是什麼

Attach機制是JVM提供一種JVM程式間通訊的能力,能讓一個程式傳命令給另外一個程式,並讓它執行內部的一些操作

比如:為了讓另外一個JVM程式把執行緒dump出來,那麼首先跑了一個jstack的程式,然後傳了個pid的引數,告訴它要哪個程式進行執行緒dump,既然是兩個程式,那肯定涉及到程式間通訊,以及傳輸協議的定義,比如:要執行什麼操作,傳了什麼引數等。

有時當我們感覺執行緒一直卡在某個地方,想知道卡在哪裡,首先想到的是進行 執行緒dump,而常用的命令是jstack,我們就可以看到如下執行緒棧:

2014-06-18 12:56:14 Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.51-b03 mixed mode):

"Attach Listener" daemon prio=5 tid=0x00007fb0c6800800 nid=0x440b waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Service Thread" daemon prio=5 tid=0x00007fb0c584d800 nid=0x5303 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" daemon prio=5 tid=0x00007fb0c482e000 nid=0x5103 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" daemon prio=5 tid=0x00007fb0c482c800 nid=0x4f03 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=5 tid=0x00007fb0c4815800 nid=0x4d03 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=5 tid=0x00007fb0c4813800 nid=0x3903 in Object.wait() [0x00000001187d2000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007aaa85568> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
    - locked <0x00000007aaa85568> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:189)

"Reference Handler" daemon prio=5 tid=0x00007fb0c4800000 nid=0x3703 in Object.wait() [0x00000001186cf000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007aaa850f0> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:503)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
    - locked <0x00000007aaa850f0> (a java.lang.ref.Reference$Lock)

"main" prio=5 tid=0x00007fb0c5800800 nid=0x1903 waiting on condition [0x0000000107962000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at Test.main(Test.java:5)

"VM Thread" prio=5 tid=0x00007fb0c583d800 nid=0x3503 runnable

"GC task thread#0 (ParallelGC)" prio=5 tid=0x00007fb0c401e000 nid=0x2503 runnable

"GC task thread#1 (ParallelGC)" prio=5 tid=0x00007fb0c401e800 nid=0x2703 runnable

"GC task thread#2 (ParallelGC)" prio=5 tid=0x00007fb0c401f800 nid=0x2903 runnable

"GC task thread#3 (ParallelGC)" prio=5 tid=0x00007fb0c4020000 nid=0x2b03 runnable

"GC task thread#4 (ParallelGC)" prio=5 tid=0x00007fb0c4020800 nid=0x2d03 runnable

"GC task thread#5 (ParallelGC)" prio=5 tid=0x00007fb0c4021000 nid=0x2f03 runnable

"GC task thread#6 (ParallelGC)" prio=5 tid=0x00007fb0c4022000 nid=0x3103 runnable

"GC task thread#7 (ParallelGC)" prio=5 tid=0x00007fb0c4022800 nid=0x3303 runnable

"VM Periodic Task Thread" prio=5 tid=0x00007fb0c5845000 nid=0x5503 waiting on condition
複製程式碼

在上面的Thread Dump日誌中,出現了兩個執行緒:“Attach Listener” 和 “Signal Dispatcher”,這兩個執行緒便是Attach機制的關鍵。

那麼JVM是如何啟動這兩個執行緒呢?JVM有很多執行緒主要在thread.cpp裡的create_vm方法體裡實現

JvmtiExport::enter_live_phase();  
  
// 1. Signal Dispatcher 需要在釋出VMInit事件之前啟動  
os::signal_init();  
  
// 2. Start Attach Listener 如果配置 +StartAttachListener; 否則會延遲啟動  
if (!DisableAttachMechanism) {  
  if (StartAttachListener || AttachListener::init_at_startup()) {  
    AttachListener::init();  
  }  
}
複製程式碼

其中JVM相關引數:DisableAttachMechanism,StartAttachListener ,ReduceSignalUsage 均預設是 false

product(bool, DisableAttachMechanism, false, "Disable mechanism that allows tools to Attach to this VM”);   
product(bool, StartAttachListener, false, "Always start Attach Listener at VM startup");
product(bool, ReduceSignalUsage, false, "Reduce the use of OS signals in Java and/or the VM”);  
複製程式碼

如上面create_vm原始碼所示,在啟動的時候有可能不會建立AttachListener執行緒,那麼 在上面Thread Stack日誌中看到的AttachListener執行緒是怎麼建立的呢,這個就要關注另外一個執行緒“Signal Dispatcher”了,顧名思義是處理訊號的,這個執行緒是在JVM啟動的時候肯定會建立的。

1.1 Signal Dispatcher 執行緒

在os.cpp中的 signal_init() 函式中,啟動了signal dispatcher 執行緒,對signal dispather執行緒主要是用於處理訊號,等待訊號並且分發處理,可以詳細看 signal_thread_entry 的方法:

// 該方法用於Signal Dispatcher執行緒處理接受到的訊號
static void signal_thread_entry(JavaThread* thread, TRAPS) {  
  os::set_priority(thread, NearMaxPriority);  
  while (true) {  
    int sig;  
    {  
      // FIXME : Currently we have not decieded what should be the status  
      //         for this java thread blocked here. Once we decide about  
      //         that we should fix this.  等待訊號
      sig = os::signal_wait();  
    }  
    if (sig == os::sigexitnum_pd()) {  
       // Terminate the signal thread  
       return;  
    }  
  
    switch (sig) {  
      case SIGBREAK: {  
        // Check if the signal is a trigger to start the Attach Listener - in that  
        // case don't print stack traces.  
        if (!DisableAttachMechanism && AttachListener::is_init_trigger()) {  
          continue;  
        }  
        // Print stack traces  
        // Any SIGBREAK operations added here should make sure to flush  
        // the output stream (e.g. tty->flush()) after output.  See 4803766.  
        // Each module also prints an extra carriage return after its output.  
        VM_PrintThreads op;  
        VMThread::execute(&op);  
        VM_PrintJNI jni_op;  
        VMThread::execute(&jni_op);  
        VM_FindDeadlocks op1(tty);  
        VMThread::execute(&op1);  
        Universe::print_heap_at_SIGBREAK();  
        if (PrintClassHistogram) {  
          VM_GC_HeapInspection op1(gclog_or_tty, true /* force full GC before heap inspection */,  
                                   true /* need_prologue */);  
          VMThread::execute(&op1);  
        }  
        if (JvmtiExport::should_post_data_dump()) {  
          JvmtiExport::post_data_dump();  
        }  
        break;  
      }  
      default: {  
        // Dispatch the signal to java  
        HandleMark hm(THREAD);  
        klassOop k = SystemDictionary::resolve_or_null(vmSymbolHandles::sun_misc_Signal(), THREAD);  
        KlassHandle klass (THREAD, k);  
        if (klass.not_null()) {  
          JavaValue result(T_VOID);  
          JavaCallArguments args;  
          args.push_int(sig);  
          JavaCalls::call_static(  
            &result,  
            klass,  
            vmSymbolHandles::dispatch_name(),  
            vmSymbolHandles::int_void_signature(),  
            &args,  
            THREAD  
          );  
        }  
        if (HAS_PENDING_EXCEPTION) {  
          // tty is initialized early so we don't expect it to be null, but  
          // if it is we can't risk doing an initialization that might  
          // trigger additional out-of-memory conditions  
          if (tty != NULL) {  
            char klass_name[256];  
            char tmp_sig_name[16];  
            const char* sig_name = "UNKNOWN";  
            instanceKlass::cast(PENDING_EXCEPTION->klass())->  
              name()->as_klass_external_name(klass_name, 256);  
            if (os::exception_name(sig, tmp_sig_name, 16) != NULL)  
              sig_name = tmp_sig_name;  
            warning("Exception %s occurred dispatching signal %s to handler"  
                    "- the VM may need to be forcibly terminated",  
                    klass_name, sig_name );  
          }  
          CLEAR_PENDING_EXCEPTION;  
        }  
      }  
    }  
  }  
}  
複製程式碼

可以看到通過 os::signal_wait(); 等待訊號,而在Linux裡是通過 sem_wait() 來實現,當接受到訊號是SIGBREAK(在JVM裡做了#define,其實就是SIGQUIT)的時候,就會觸發 AttachListener::is_init_trigger()的執行初始化attach listener執行緒

  1. 第一次收到訊號,會開始初始化,當初始化成功,將會直接返回,而且 不返回任何執行緒stack的資訊(通過socket file的操作返回),並且第二次將不在需要初始化。如果初始化不成功,將直接在控制檯的outputstream中列印執行緒棧資訊;
  2. 第二次收到訊號,如果已經初始化過,將直接在控制檯中列印執行緒的棧資訊。如果沒有初始化,繼續初始化,走和第一次相同的流程;

比如:我們經常會 使用 kill -3 pid的操作列印出執行緒棧資訊,可以看到具體的實現是在Signal Dispatcher 執行緒中完成的,因為kill -3 pid 並不會建立.attach_pid#pid檔案,所以一直初始化不成功,從而執行緒的棧資訊被列印到控制檯中。

1.2 Attach Listener 執行緒

Attach Listener 執行緒是負責接收到外部的命令,而對該命令進行執行的並且把結果返回給傳送者。在JVM啟動的時候,如果沒有指定 +StartAttachListener,該Attach Listener執行緒是不會啟動的。

在接受到 quit 訊號之後,會呼叫 AttachListener::is_init_trigger() 方法, AttachListener::is_init_trigger() 內會呼叫AttachListener::init() 啟動了Attach Listener 執行緒,在不同的作業系統下初始化實現是不同的,在linux中是在attachListener_Linux.cpp檔案中實現的。

AttachListener::is_init_trigger() 程式碼如下

bool AttachListener::is_init_trigger() {
  if (init_at_startup() || is_initialized()) {
    return false;               // initialized at startup or already initialized
  }
  char fn[PATH_MAX+1];
  sprintf(fn, ".Attach_pid%d", os::current_process_id());
  int ret;
  struct stat64 st;
  RESTARTABLE(::stat64(fn, &st), ret);
  if (ret == -1) {
    snprintf(fn, sizeof(fn), "%s/.Attach_pid%d",
             os::get_temp_directory(), os::current_process_id());
    RESTARTABLE(::stat64(fn, &st), ret);
  }
  if (ret == 0) {
    // simple check to avoid starting the Attach mechanism when
    // a bogus user creates the file
    if (st.st_uid == geteuid()) {
      // 建立AttachListener執行緒
      init();
      return true;
    }
  }
  return false;
}
複製程式碼

一開始會 判斷當前程式目錄下是否有個.Attach_pid檔案,如果沒有就會在/tmp下建立一個/tmp/.Attach_pid當那個檔案的uid和自己的uid是一致的情況下(為了安全)再呼叫init方法

// Starts the Attach Listener thread
void AttachListener::init() {
  EXCEPTION_MARK;
  klassOop k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK);
  instanceKlassHandle klass (THREAD, k);
  instanceHandle thread_oop = klass->allocate_instance_handle(CHECK);

  const char thread_name[] = "Attach Listener";
  Handle string = java_lang_String::create_from_str(thread_name, CHECK);

  // Initialize thread_oop to put it into the system threadGroup
  Handle thread_group (THREAD, Universe::system_thread_group());
  JavaValue result(T_VOID);
  JavaCalls::call_special(&result, thread_oop,
                       klass,
                       vmSymbols::object_initializer_name(),
                       vmSymbols::threadgroup_string_void_signature(),
                       thread_group,
                       string,
                       CHECK);

  KlassHandle group(THREAD, SystemDictionary::ThreadGroup_klass());
  JavaCalls::call_special(&result,
                        thread_group,
                        group,
                        vmSymbols::add_method_name(),
                        vmSymbols::thread_void_signature(),
                        thread_oop,             // ARG 1
                        CHECK);

  { MutexLocker mu(Threads_lock);
    JavaThread* listener_thread = new JavaThread(&Attach_listener_thread_entry);

    // Check that thread and osthread were created
    if (listener_thread == NULL || listener_thread->osthread() == NULL) {
      vm_exit_during_initialization("java.lang.OutOfMemoryError",
                                    "unable to create new native thread");
    }

    java_lang_Thread::set_thread(thread_oop(), listener_thread);
    java_lang_Thread::set_daemon(thread_oop());

    listener_thread->set_threadObj(thread_oop());
    Threads::add(listener_thread);
    Thread::start(listener_thread);
  }
}
複製程式碼

此時水落石出了,看到建立了一個執行緒,並且取名為Attach Listener。再看看Linux系統下其子類LinuxAttachListener的init方法

int LinuxAttachListener::init() {
  char path[UNIX_PATH_MAX];          // socket file
  char initial_path[UNIX_PATH_MAX];  // socket file during setup
  int listener;                      // listener socket (file descriptor)

  // register function to cleanup
  ::atexit(listener_cleanup);

  int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
                   os::get_temp_directory(), os::current_process_id());
  if (n < (int)UNIX_PATH_MAX) {
    n = snprintf(initial_path, UNIX_PATH_MAX, "%s.tmp", path);
  }
  if (n >= (int)UNIX_PATH_MAX) {
    return -1;
  }

  // create the listener socket
  listener = ::socket(PF_UNIX, SOCK_STREAM, 0);
  if (listener == -1) {
    return -1;
  }

  // bind socket
  struct sockaddr_un addr;
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, initial_path);
  ::unlink(initial_path);
  int res = ::bind(listener, (struct sockaddr*)&addr, sizeof(addr));
  if (res == -1) {
    RESTARTABLE(::close(listener), res);
    return -1;
  }

  // put in listen mode, set permissions, and rename into place
  res = ::listen(listener, 5);
  if (res == 0) {
      RESTARTABLE(::chmod(initial_path, S_IREAD|S_IWRITE), res);
      if (res == 0) {
          res = ::rename(initial_path, path);
      }
  }
  if (res == -1) {
    RESTARTABLE(::close(listener), res);
    ::unlink(initial_path);
    return -1;
  }
  set_path(path);
  set_listener(listener);

  return 0;
}
複製程式碼

看到其建立了一個監聽套接字,並建立了一個檔案/tmp/.java_pid,這個檔案就是客戶端之前一直在輪詢等待的檔案,隨著這個檔案的生成,意味著Attach的建立過程圓滿結束了。

Attach Listener執行緒接收到請求時,具體的請求處理在 attach_listener_thread_entry 方法體中實現

static void attach_listener_thread_entry(JavaThread* thread, TRAPS) {  
  os::set_priority(thread, NearMaxPriority);  
  
  if (AttachListener::pd_init() != 0) {  
    return;  
  }  
  AttachListener::set_initialized();  
  
  for (;;) {  
    AttachOperation* op = AttachListener::dequeue();    
     if (op == NULL) {  
      return;   // dequeue failed or shutdown  
    }  
  
    ResourceMark rm;  
    bufferedStream st;  
    jint res = JNI_OK;  
  
    // handle special detachall operation  
    if (strcmp(op->name(), AttachOperation::detachall_operation_name()) == 0) {  
      AttachListener::detachall();  
    } else {  
      // find the function to dispatch too  
      AttachOperationFunctionInfo* info = NULL;  
      for (int i=0; funcs[i].name != NULL; i++) {  
        const char* name = funcs[i].name;  
        assert(strlen(name) <= AttachOperation::name_length_max, "operation <= name_length_max");  
        if (strcmp(op->name(), name) == 0) {  
          info = &(funcs[i]);  
          break;  
        }  
      }  
  
      // check for platform dependent attach operation  
      if (info == NULL) {  
        info = AttachListener::pd_find_operation(op->name());  
      }  
  
      if (info != NULL) {  
        // dispatch to the function that implements this operation  
        res = (info->func)(op, &st);  
      } else {  
        st.print("Operation %s not recognized!", op->name());  
        res = JNI_ERR;  
      }  
    }  
  
    // operation complete - send result and output to client  
    op->complete(res, &st);  
  }  
}  
複製程式碼

從程式碼來看就是 從佇列裡不斷取AttachOperation,然後找到請求命令對應的方法進行執行,比如一開始說的jstack命令,找到 { “threaddump”, thread_dump }的對映關係,然後執行thread_dump方法。

AttachOperation有很多種類,比如:記憶體dump,執行緒dump,類資訊統計(比如載入的類及大小以及例項個數等),動態載入agent,動態設定vm flag(但是並不是所有的flag都可以設定的,因為有些flag是在jvm啟動過程中使用的,是一次性的),列印vm flag,獲取系統屬性等,這些對應的原始碼(AttachListener.cpp)如下:

static AttachOperationFunctionInfo funcs[] = {
  // 第二個引數是命令對應的處理函式
  { "agentProperties",  get_agent_properties },
  { "datadump",         data_dump },
  { "dumpheap",         dump_heap },
  { "load",             JvmtiExport::load_agent_library },
  { "properties",       get_system_properties },
  { "threaddump",       thread_dump },
  { "inspectheap",      heap_inspection },
  { "setflag",          set_flag },
  { "printflag",        print_flag },
  { "jcmd",             jcmd },
  { NULL,               NULL }
};
複製程式碼

再來看看其要呼叫的 AttachListener::dequeue();

AttachOperation* AttachListener::dequeue() {
  JavaThread* thread = JavaThread::current();
  ThreadBlockInVM tbivm(thread);

  thread->set_suspend_equivalent();
  // cleared by handle_special_suspend_equivalent_condition() or
  // java_suspend_self() via check_and_wait_while_suspended()

  AttachOperation* op = LinuxAttachListener::dequeue();

  // were we externally suspended while we were waiting?
  thread->check_and_wait_while_suspended();

  return op;
}
複製程式碼

最終會呼叫的是 LinuxAttachListener::dequeue()

LinuxAttachOperation* LinuxAttachListener::dequeue() {
  for (;;) {
    int s;

    // wait for client to connect
    struct sockaddr addr;
    socklen_t len = sizeof(addr);
    // 如果沒有請求的話,會一直accept在那裡
    RESTARTABLE(::accept(listener(), &addr, &len), s);
    if (s == -1) {
      return NULL;      // log a warning?
    }

    // get the credentials of the peer and check the effective uid/guid
    // - check with jeff on this.
    struct ucred cred_info;
    socklen_t optlen = sizeof(cred_info);
    if (::getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void*)&cred_info, &optlen) == -1) {
      int res;
      RESTARTABLE(::close(s), res);
      continue;
    }
    uid_t euid = geteuid();
    gid_t egid = getegid();

    if (cred_info.uid != euid || cred_info.gid != egid) {
      int res;
      RESTARTABLE(::close(s), res);
      continue;
    }

    // peer credential look okay so we read the request
    LinuxAttachOperation* op = read_request(s);
    if (op == NULL) {
      int res;
      RESTARTABLE(::close(s), res);
      continue;
    } else {
      return op;
    }
  }
}
複製程式碼

如上程式碼中可以看到,如果沒有請求的話,會一直accept在那裡,當來了請求,然後就會建立一個套接字,並讀取資料,構建出LinuxAttachOperation返回,找到請求對應的操作,呼叫操作得到結果並把結果寫到這個socket的檔案,如果你把socket的檔案刪除,jstack/jmap會出現錯誤資訊 unable to open socket file:........

1.3 jstack/jmap命令流程圖

以jstack的實現來說明觸發Attach這一機制進行的過程,jstack命令的實現其實是一個叫做JStack.java的類,jstack命令首先會attach到目標JVM程式,產生VirtualMachine類;Linux系統下,其實現類為LinuxVirtualMachine,呼叫其remoteDataDump方法,列印堆疊資訊;檢視JStack.java程式碼後會走到下面的方法裡:

private static void runThreadDump(String pid, String args[]) throws Exception {
        VirtualMachine vm = null;
        try {
            // jstack命令首先會attach到目標JVM程式
            vm = VirtualMachine.Attach(pid);
        } catch (Exception x) {
            String msg = x.getMessage();
            if (msg != null) {
                System.err.println(pid + ": " + msg);
            } else {
                x.printStackTrace();
            }
            if ((x instanceof AttachNotSupportedException) &&
                (loadSAClass() != null)) {
                System.err.println("The -F option can be used when the target " +
                    "process is not responding");
            }
            System.exit(1);
        }

        // Cast to HotSpotVirtualMachine as this is implementation specific
        // method.
        // 輸出堆疊資訊
        InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);

        // read to EOF and just print output
        byte b[] = new byte[256];
        int n;
        do {
            n = in.read(b);
            if (n > 0) {
                String s = new String(b, 0, n, "UTF-8");
                System.out.print(s);
            }
        } while (n > 0);
        in.close();
        vm.detach();
}
複製程式碼

那麼VirtualMachine是如何連線到目標JVM程式的呢?請注意 VirtualMachine.Attach(pid); 這行程式碼,觸發Attach pid的關鍵,如果是在Linux下具體的實現邏輯在 sun.tools.attach.LinuxVirtualMachine 的建構函式:

LinuxVirtualMachine(AttachProvider provider, String vmid) throws AttachNotSupportedException, IOException
    {
        super(provider, vmid);

        // This provider only understands pids
        int pid;
        try {
            pid = Integer.parseInt(vmid);
        } catch (NumberFormatException x) {
            throw new AttachNotSupportedException("Invalid process identifier");
        }

        // Find the socket file. If not found then we attempt to start the
        // Attach mechanism in the target VM by sending it a QUIT signal.
        // Then we attempt to find the socket file again.
        path = findSocketFile(pid);
        if (path == null) {
            File f = createAttachFile(pid);
            try {
                // On LinuxThreads each thread is a process and we don't have the
                // pid of the VMThread which has SIGQUIT unblocked. To workaround
                // this we get the pid of the "manager thread" that is created
                // by the first call to pthread_create. This is parent of all
                // threads (except the initial thread).
                if (isLinuxThreads) {
                    int mpid;
                    try {
                        mpid = getLinuxThreadsManager(pid);
                    } catch (IOException x) {
                        throw new AttachNotSupportedException(x.getMessage());
                    }
                    assert(mpid >= 1);
                    sendQuitToChildrenOf(mpid);
                } else {
                    sendQuitTo(pid);
                }

                // give the target VM time to start the Attach mechanism
                int i = 0;
                long delay = 200;
                int retries = (int)(AttachTimeout() / delay);
                do {
                    try {
                        Thread.sleep(delay);
                    } catch (InterruptedException x) { }
                    path = findSocketFile(pid);
                    i++;
                } while (i <= retries && path == null);
                if (path == null) {
                    throw new AttachNotSupportedException(
                        "Unable to open socket file: target process not responding " +
                        "or HotSpot VM not loaded");
                }
            } finally {
                f.delete();
            }
        }

        // Check that the file owner/permission to avoid Attaching to
        // bogus process
        checkPermissions(path);

        // Check that we can connect to the process
        // - this ensures we throw the permission denied error now rather than
        // later when we attempt to enqueue a command.
        int s = socket();
        try {
            connect(s, path);
        } finally {
            close(s);
        }
}
複製程式碼
  1. 查詢/tmp目錄下是否存在".java_pid"+pid檔案;
  2. 如果檔案不存在,則首先建立"/proc/" + pid + "/cwd/" + ".attach_pid" + pid檔案;
  3. 通過kill命令傳送SIGQUIT訊號給目標JVM程式,由於JVM裡除了訊號執行緒,其他執行緒都設定了對此訊號的遮蔽,因此收不到該訊號,於是該訊號就傳給了“Signal Dispatcher”
  4. 目標JVM程式接收到訊號之後,會在/tmp目錄下建立".java_pid"+pid檔案;
  5. 當發現/tmp目錄下存在".java_pid"+pid檔案,LinuxVirtualMachine會通過connect系統呼叫連線到該檔案描述符,後續通過該fd進行雙方的通訊;

JVM接受SIGQUIT訊號的相關邏輯處理,則是在前面 signal_thread_entry 方法中進行實現

jstack/jmap命令流程圖

前面JStack.java原始碼中,輸出堆疊資訊是通過呼叫remoteDataDump方法實現的,該方法就是通過往前面提到的fd中寫入threaddump指令,讀取返回結果,從而得到目標JVM的堆疊資訊

2 Java 程式碼實現動態 attach Agent

Java動態attach Agent與上面所講到的JStack.java實現基本類似,在 attach 的java程式碼中,使用sun自用的tool.jar中的VirtualMachine的attach的方式:

VirtualMachine vm = VirtualMachine.attach(processid);  
vm.loadAgent(agentpath, args) 
複製程式碼

HotSpotVirtualMachine.java中,loadAgent 方法原始碼如下:

public void loadAgent(String agent, String options) throws AgentLoadException, AgentInitializationException, IOException  
{  
    String args = agent;  
    if (options != null) {  
        args = args + "=" + options;  
    }  
    try {  
        loadAgentLibrary("instrument", args);  
    } .....  
}

private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options) throws AgentLoadException, AgentInitializationException, IOException  
{  
    InputStream in = execute("load", agentLibrary, isAbsolute ? "true" : "false", options);  
    try {  
        int result = readInt(in);  
        if (result != 0) {  
            throw new AgentInitializationException("Agent_OnAttach failed", result);  
        }  
    } finally {  
        in.close();  
    }  
}  
複製程式碼

LinuxVirtualMachine.java中的execute方法:

InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {  
    assert args.length <= 3;                // includes null  
    // did we detach?  
    String p;  
    synchronized (this) {  
        if (this.path == null) {  
            throw new IOException("Detached from target VM");  
        }  
        p = this.path;  
    }  
    // create UNIX socket  
    int s = socket();  
    // connect to target VM  
    try {  
        connect(s, p);  
    } catch (IOException x) {  
        close(s);  
        throw x;  
    }  
  
    IOException ioe = null;  
  
    // connected - write request  
    // <ver> <cmd> <args...>  
    try {  
        writeString(s, PROTOCOL_VERSION);  
        writeString(s, cmd);  
        for (int i=0; i<3; i++) {  
            if (i < args.length && args[i] != null) {  
                writeString(s, (String)args[i]);  
            } else {  
                writeString(s, "");  
            }  
        }  
    } catch (IOException x) {  
        ioe = x;  
    }  
    // Create an input stream to read reply  
    SocketInputStream sis = new SocketInputStream(s);  
  
    // Read the command completion status  
    int completionStatus;  
    try {  
        completionStatus = readInt(sis);  
    } catch (IOException x) {  
        sis.close();  
        if (ioe != null) {  
            throw ioe;  
        } else {  
            throw x;  
        }  
    }  
    ....  
}  
複製程式碼

也就是向socket的中寫入了,格式為:

<ver> <cmd> <args...> 
複製程式碼

具體內容為:

1 load instrument agentPath=path.jar
複製程式碼

既然Load Agent 往socket裡發了load指令,匹配到JVM的操作:

static AttachOperationFunctionInfo funcs[] = {  
  { "agentProperties",  get_agent_properties },  
  { "datadump",         data_dump },  
#ifndef SERVICES_KERNEL  
  { "dumpheap",         dump_heap },  
#endif  // SERVICES_KERNEL  
  { "load",             JvmtiExport::load_agent_library },  
  { "properties",       get_system_properties },  
  { "threaddump",       thread_dump },  
  { "inspectheap",      heap_inspection },  
  { "setflag",          set_flag },  
  { "printflag",        print_flag },  
  { NULL,               NULL }  
}; 
複製程式碼

"load", JvmtiExport::load_agent_library,具體原始碼如下:

jint JvmtiExport::load_agent_library(AttachOperation* op, outputStream* st) {  
  char ebuf[1024];  
  char buffer[JVM_MAXPATHLEN];  
  void* library;  
  jint result = JNI_ERR;  
  const char* agent = op->arg(0);  
  const char* absParam = op->arg(1);  
  const char* options = op->arg(2);  
  bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0);  
  if (is_absolute_path) {  
    library = os::dll_load(agent, ebuf, sizeof ebuf);  
  } else {  
    // Try to load the agent from the standard dll directory  
    os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), agent);  
    library = os::dll_load(buffer, ebuf, sizeof ebuf);  
    if (library == NULL) {  
      // not found - try local path  
      char ns[1] = {0};  
      os::dll_build_name(buffer, sizeof(buffer), ns, agent);  
      library = os::dll_load(buffer, ebuf, sizeof ebuf);  
    }  
  }  
  if (library != NULL) {  
    // Lookup the Agent_OnAttach function  
    OnAttachEntry_t on_attach_entry = NULL;  
    const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS;  
    for (uint symbol_index = 0; symbol_index < ARRAY_SIZE(on_attach_symbols); symbol_index++) {  
  
      on_attach_entry =  
  
        CAST_TO_FN_PTR(OnAttachEntry_t, os::dll_lookup(library, on_attach_symbols[symbol_index]));  
  
      if (on_attach_entry != NULL) break;  
  
    }  
    if (on_attach_entry == NULL) {  
      // Agent_OnAttach missing - unload library  
      os::dll_unload(library);  
    } else {  
      // Invoke the Agent_OnAttach function  
      JavaThread* THREAD = JavaThread::current();  
      {  
        extern struct JavaVM_ main_vm;  
        JvmtiThreadEventMark jem(THREAD);  
        JvmtiJavaThreadEventTransition jet(THREAD);  
        result = (*on_attach_entry)(&main_vm, (char*)options, NULL);  
  
      }  
      if (HAS_PENDING_EXCEPTION) {  
        CLEAR_PENDING_EXCEPTION;  
      }  
      if (result == JNI_OK) {  
        Arguments::add_loaded_agent(agent, (char*)options, is_absolute_path, library);  
      }  
      // Agent_OnAttach executed so completion status is JNI_OK  
      st->print_cr("%d", result);  
      result = JNI_OK;  
    }  
  }  
  return result;  
}  

#define AGENT_ONATTACH_SYMBOLS  {"Agent_OnAttach"} 
複製程式碼

3 執行 Instrument 的 Agent on attach

載入instrument的動態庫,並且呼叫方法instrument動態庫中的Agent_OnAttach方法:

JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char *args, void * reserved) {  
   .....  
    initerror = createNewJPLISAgent(vm, &agent);  
    if ( initerror == JPLIS_INIT_ERROR_NONE ) {  
        ......  
        if (parseArgumentTail(args, &jarfile, &options) != 0) {  
            return JNI_ENOMEM;  
        }  
        attributes = readAttributes( jarfile );  
        if (attributes == NULL) {  
            fprintf(stderr, "Error opening zip file or JAR manifest missing: %s\n", jarfile);  
            free(jarfile);  
            if (options != NULL) free(options);  
            return AGENT_ERROR_BADJAR;  
        }  
        agentClass = getAttribute(attributes, "Agent-Class");  
        if (agentClass == NULL) {  
            fprintf(stderr, "Failed to find Agent-Class manifest attribute from %s\n",  
                jarfile);  
            free(jarfile);  
            if (options != NULL) free(options);  
            freeAttributes(attributes);  
            return AGENT_ERROR_BADJAR;  
        }  
        if (appendClassPath(agent, jarfile)) {  
            fprintf(stderr, "Unable to add %s to system class path "  
                "- not supported by system class loader or configuration error!\n",  
                jarfile);  
            free(jarfile);  
            if (options != NULL) free(options);  
            freeAttributes(attributes);  
            return AGENT_ERROR_NOTONCP;  
        }  
        oldLen = strlen(agentClass);  
        newLen = modifiedUtf8LengthOfUtf8(agentClass, oldLen);  
        if (newLen == oldLen) {  
            agentClass = strdup(agentClass);  
        } else {  
            char* str = (char*)malloc( newLen+1 );  
            if (str != NULL) {  
                convertUtf8ToModifiedUtf8(agentClass, oldLen, str, newLen);  
            }  
            agentClass = str;  
        }  
  
        if (agentClass == NULL) {  
            free(jarfile);  
            if (options != NULL) free(options);  
            freeAttributes(attributes);  
            return JNI_ENOMEM;  
        }  
        bootClassPath = getAttribute(attributes, "Boot-Class-Path");  
        if (bootClassPath != NULL) {  
            appendBootClassPath(agent, jarfile, bootClassPath);  
        }  
        convertCapabilityAtrributes(attributes, agent);  
        success = createInstrumentationImpl(jni_env, agent);  
        jplis_assert(success);  
        /* 
         *  Turn on the ClassFileLoadHook. 
         */  
        if (success) {  
            success = setLivePhaseEventHandlers(agent);  
            jplis_assert(success);  
        }  
        if (success) {  
            success = startJavaAgent(agent,  
                                     jni_env,  
                                     agentClass,  
                                     options,  
                                     agent->mAgentmainCaller);  
        }  
        if (!success) {  
            fprintf(stderr, "Agent failed to start!\n");  
            result = AGENT_ERROR_STARTFAIL;  
        }  
  
        if (options != NULL) free(options);  
        free(agentClass);  
        freeAttributes(attributes);  
    }  
    return result;  
}  
複製程式碼

上面程式碼裡一開始的createNewJPLISAgenton_load是一樣的註冊了一些鉤子函式,具體詳情可參考:《JVMTI Agent 工作原理及核心原始碼分析》

在上面的Agent_OnAttach程式碼中我們也看到了,讀取載入的jar中MANIFEST Agent-Class的配置:

agentClass = getAttribute(attributes, "Agent-Class");
複製程式碼

建立生成sun.instrument.InstrumentationImpl物件:

success = createInstrumentationImpl(jni_env, agent);
複製程式碼

通過InstrumentationImpl物件中的loadClassAndCallAgentmain方法去初始化在Agent-Class中的類,並呼叫class裡的agentmain的方法:

success = startJavaAgent(agent, jni_env, agentClass, options, agent->mAgentmainCaller);
複製程式碼

也就是說定義的on_attach的class裡需要有agentmain的方法實現:

public class MyTransformer {  
    public static void agentmain(String agentArgs, Instrumentation inst) throws ClassNotFoundException, UnmodifiableClassException, NotFoundException, CannotCompileException, IOException{  
        ....  
    }  
}複製程式碼

相關文章