Leanpub Header

Skip to main content

Learning JVMTI

Building native agents for JVM

Unlock the full power of the JVM! Learn to build native agents with JVMTI for monitoring, debugging, and extending Java applications—complete with hands-on examples and expert tips.

Minimum price

$12.99

$19.99

You pay

$19.99

Author earns

$15.99
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
WEB
About

About

About the Book

This practical guide introduces developers to the Java Virtual Machine Tool Interface (JVMTI), empowering you to build powerful native agents for the JVM. From the basics of agent architecture to advanced topics like thread inspection, heap analysis, and bytecode transformation, this book covers real-world use cases, hands-on examples, and best practices.

Whether you’re interested in performance monitoring, debugging, or extending the JVM, you’ll learn how to leverage JVMTI to gain deep insights and control over Java applications. Includes troubleshooting tips, code samples, and a comprehensive JNI reference.

Code examples can be found at - https://github.com/sachin-handiekar/jvmti-examples

Share this book

Categories

Author

About the Author

Contents

Table of Contents

Chapter 1: Introduction to JVMTI

  1. 1.1 Real-World Use Cases
  2. 1.2 Evolution: JVMPI, JVMDI → JVMTI
  3. 1.3 JVMTI vs Other Tooling Interfaces
  4. 1.4 Requirements and Environment Setup
  5. 1.5 A Note on C vs C++ Calling Convention

Chapter 2: Anatomy of a JVMTI Agent

  1. 2.1 What is a JVMTI Agent?
  2. 2.2 Lifecycle Entry Points
  3. 2.3 Error Handling: The Foundation
  4. 2.4 Minimum Viable Agent
  5. 2.5 Parsing Agent Options
  6. 2.6 Companion Java Application
  7. 2.7 Compiling with CMake
  8. 2.8 Running the Agent
  9. 2.9 Optional: File-Based Logging
  10. 2.10 Troubleshooting Tips
  11. 2.11 What’s Next?

Chapter 3: Capabilities and Events in JVMTI

  1. 3.1 Understanding JVMTI Capabilities
  2. 3.2 The Event Model
  3. 3.3 Event Categories
  4. 3.4 Writing Event Handlers
  5. 3.5 Controlling Events at Runtime
  6. 3.6 Complete Example: Thread and Method Tracker
  7. 3.7 Best Practices for Event Handling
  8. 3.8 Troubleshooting
  9. 3.9 What’s Next?

Chapter 4: Inspecting Threads, Stacks, and Methods

  1. 4.1 Enumerating All Threads
  2. 4.2 Walking the Stack
  3. 4.3 Retrieving Method Details
  4. 4.4 Inspecting Local Variables
  5. 4.5 Complete Example: Thread Dump Agent
  6. 4.6 Best Practices
  7. 4.7 Troubleshooting
  8. 4.8 What’s Next?

Chapter 5: Heap Inspection and Object Tagging

  1. 5.1 Required Capabilities
  2. 5.2 Iterating Over Heap Objects
  3. 5.3 Object Tagging
  4. 5.4 Getting Object Sizes
  5. 5.5 Monitoring Object Allocations
  6. 5.6 Heap Walking Safety
  7. 5.7 Combining Heap and Stack Data
  8. 5.8 Complete Example: Heap Histogram Agent
  9. 5.9 Best Practices
  10. 5.10 Troubleshooting
  11. 5.11 What’s Next?

Chapter 6: Class Transformation and Bytecode Instrumentation

  1. 6.1 How Class Transformation Works
  2. 6.2 The ClassFileLoadHook Callback
  3. 6.3 Filtering Classes
  4. 6.4 Modifying Bytecode
  5. 6.5 Bytecode Instrumentation Strategies
  6. 6.6 Retransformation vs. Redefinition
  7. 6.7 Complete Example: Class Load Monitor
  8. 6.8 Best Practices
  9. 6.9 Troubleshooting
  10. 6.10 What’s Next?

Chapter 7: Interacting with the JVM Runtime

  1. 7.1 Reading System Properties
  2. 7.2 JVM Phase and Timing
  3. 7.3 Controlling Garbage Collection
  4. 7.4 Object Size (Correct API)
  5. 7.5 Managing Native Resources
  6. 7.6 Thread-Local Storage
  7. 7.7 Complete Example: JVM Inspector Agent
  8. 7.8 Best Practices
  9. 7.9 Troubleshooting
  10. 7.10 What’s Next?

Chapter 8: Exception Handling and Debugging

  1. 8.1 Exception Events
  2. 8.2 The Exception Callback
  3. 8.3 The Exception Catch Callback
  4. 8.4 Capturing Exception Stack Traces
  5. 8.5 Complete Example: Exception Monitor Agent
  6. 8.6 Performance Considerations
  7. 8.7 Best Practices
  8. 8.8 Troubleshooting
  9. 8.9 What’s Next?

Chapter 9: Advanced JVMTI Techniques

  1. 9.1 Raw Monitors
  2. 9.2 Thread-Local Storage (TLS)
  3. 9.3 Agent Reentrancy
  4. 9.4 Native Method Binding
  5. 9.5 Multiple JVMTI Environments
  6. 9.6 Complete Example: Thread-Safe Method Counter
  7. 9.7 Best Practices
  8. 9.8 What’s Next?

Chapter 10: Building a Minimal Profiler

  1. 10.1 Sampling vs. Instrumentation
  2. 10.2 Core Architecture
  3. 10.3 Complete Example: Sampling Profiler Agent
  4. 10.4 Cross-Platform Timer Strategies
  5. 10.5 Production Profiler Architecture
  6. 10.6 Best Practices
  7. 10.7 Troubleshooting
  8. 10.8 What’s Next?

Chapter 11: Deploying and Testing JVMTI Agents

  1. 11.1 Agent Packaging
  2. 11.2 CMake Build Configuration
  3. 11.3 Debug vs. Release Builds
  4. 11.4 The Attach API
  5. 11.5 Measuring Agent Overhead
  6. 11.6 Best Practices
  7. 11.7 Troubleshooting
  8. 11.8 What’s Next?

Chapter 12: Security and Stability

  1. 12.1 Crash Handling
  2. 12.2 Thread Safety Rules
  3. 12.3 Container Deployment
  4. 12.4 JVM Vendor Compatibility
  5. 12.5 Least-Privilege Design
  6. 12.6 Best Practices
  7. 12.7 Troubleshooting
  8. 12.8 What’s Next?

Chapter 13: Case Study — Building an Allocation Tracker

  1. 13.1 Project Structure
  2. 13.2 Design Decisions
  3. 13.3 Complete Agent: alloc_tracker.c
  4. 13.4 Companion Test Application
  5. 13.5 Build and Run
  6. 13.6 Extending the Agent
  7. 13.7 Techniques Used (Chapter Cross-Reference)
  8. 13.8 Conclusion

Appendix A: Full JVMTI Capabilities Reference

  1. Querying Capabilities
  2. Debugging Capabilities
  3. Thread and Monitor Capabilities
  4. Class Transformation Capabilities
  5. Memory and Heap Capabilities
  6. Exception and Event Capabilities
  7. Miscellaneous Capabilities
  8. Quick Reference: Capabilities Used Per Chapter

Appendix B: JVMTI Error Codes Reference

  1. Universal Errors
  2. Capability Errors
  3. Argument Errors
  4. Thread State Errors
  5. Class and Method Errors
  6. Error Handling Pattern — Full Example

Appendix C: Open-Source Tools Using JVMTI

  1. Profiling and Monitoring Tools
  2. Diagnostic and Tracing Tools
  3. Bytecode Instrumentation Frameworks
  4. Commercial Tools (JVMTI-based)
  5. How to Learn from These Tools

Appendix D: Debugging Native Crashes in JVMTI Agents

  1. 1. Understanding the HotSpot Crash Log (hs_err_pid*.log)
  2. 2. Building with Debug Symbols
  3. 3. AddressSanitizer (ASan)
  4. 4. Valgrind (Linux/macOS)
  5. 5. Using GDB (Linux/macOS)
  6. 6. Using LLDB (macOS)
  7. 7. Windows Debugging
  8. 8. Common Crash Patterns in JVMTI Agents
  9. Debugging Checklist

Appendix E: JVM Compatibility Matrix

  1. Vendor Compatibility
  2. Capability Availability by Java Version
  3. Key Behavioral Differences
  4. Defensive Runtime Check
  5. Testing Across JVMs

Appendix F: JNI Cheat Sheet

  1. JNI Type Descriptors
  2. JNI Types and Handles
  3. Reference Types
  4. Common Operations
  5. Exception Handling in JNI
  6. Thread Attachment
  7. Common Pitfalls
  8. Additional Resources

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub