In-depth Java debugging system, part 2nd: JVMTI and agent implementation

Source: Internet
Author: User
Tags memory usage

JPDA (Java Platform Debugger architecture) is an abbreviation for the Java Platform Debug architecture. With the API provided by JPDA, developers can easily and flexibly build Java debugging applications. JPDA consists of three components: Java Virtual Machine Tool Interface (JVMTI), Java Debug Line Protocol (JDWP), and Java Debugging Interface (JDI). This series will detail the internal details of these three modules and uncover the JPDA for readers through an example.

The 1th part of this series describes the various components of the JPDA as a whole, as well as their intrinsic relationship with each other. This article is the 2nd of the series, highlighting the powerful virtual machine interface-JVMTI, and how to write user-defined Java debugging and diagnostics using JVMTI.

Diagnostics and debugging of Java programs

Developers have many different kinds and levels of requirements for diagnosing and debugging Java programs, which makes it possible for developers to use different tools to solve problems. For example, in the process of running a Java program, programmers want to master its overall health, at which point programmers can directly use the Jconsole program provided by JDK. Developers can use a variety of Java Profiler if they want to improve the execution efficiency of programs. This type of tool is very many, each has the merit, can help the developer to find the program bottleneck, thus enhances the program the speed. Developers will also experience memory-related problems, such as excessive memory usage, a large amount of memory not being released, even a memory overflow error (OUTOFMEMORYERROR), and so on, so that the current memory can be exported to the dump file, then the heap analyzer or dump Tools such as File Analyzer to examine the overall state of the instance in the current runtime heap (HEAP) to diagnose the problem. All of these tools have a common feature that ultimately they all need to interact with virtual machines to discover problems with Java programs running.

While these tools are powerful and easy to use, in some advanced application environments, developers often have special requirements that require custom tools to achieve goals. The JDK itself defines a well-defined and full-featured API to interact directly with virtual machines, and these APIs can be easily extended to meet the needs of developers. In this article, you will describe jvmti in more detail and how to write a custom Agent using JVMTI.

Agent
The Agent is the JVMTI client, it runs on the same process as the virtual machines that execute Java programs, so usually their implementations are compact, and they are usually controlled by another independent process, acting as intermediaries between this stand-alone process and the current virtual machine, interacting with the interface provided by JVMTI and the virtual machine , which is responsible for obtaining and returning the status of the current virtual machine or forwarding control commands.

Introduction to JVMTI

JVMTI (JVM Tool Interface) is the native programming interface provided by the Java Virtual machine, JVMPI (Java Virtual Machine Profiler) and Interface Updated version of Achine Debug Interface. From the historical trajectory of this API, we can see that JVMTI provides interfaces that can be used for debug and profiler, and that in Java 5/6, virtual machine interfaces also add listening (monitoring), threading profiling (thread analysis), and Coverage Analysis (Coverage) and other functions. Thanks to the power of JVMTI, it is the basis for implementing the Java debugger and other Java Runtime test and analysis tools.

JVMTI are not necessarily implemented on all Java virtual machines, and the implementations of different virtual machines are not the same. However, in some of the mainstream virtual machines, such as Sun and IBM, as well as some open source such as Apache Harmony DRLVM, have provided a standard JVMTI implementation.

JVMTI is a set of local code interfaces, so using JVMTI requires that we deal with C/s + + and JNI. In fact, the development of the general adoption of an Agent to use the JVMTI, it uses the JVMTI function, set some callback functions, and from the Java virtual machine to get the current running state information, and make their own judgments, and finally may operate the virtual machine running state. After the Agent has been compiled into a dynamic link library, we can load it (boot load mode) When the Java program is started, or use runtime loading after Java 5 (active load mode).

-agentlib:agent-lib-name=options

-agentpath:path-to-agent=options

The working process of the Agent

Start

The Agent is loaded at the start of the Java virtual machine, which is in the early stages of virtual machine initialization, at this point in time:

All Java classes have not been initialized;

All object instances have not been created;

Thus, no Java code is executed;

But at this time, we can already:

The Capability parameters of the operation Jvmti;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.