Attaching debugger to JVM created by JNI_CreateJavaVM - java

This is on Ubuntu 12.04/ Java 7, 64bit
Working on project to create java bindings for WebkitGtk Version 2. My primary interest in WebkitGTK is DOM access and manipulation as against just displaying web pages. For V2, WebkitGtk team changed architecture which allows DOM access via extensions. Extensions are loaded by WebKitWebProcess which is a separate process fired when webkit is started.
I got most of it under control. My extension is getting loaded and JVM is started and my java classes are getting loaded and mostly working as expected. Right now my debugging technique is basically println statements and/or log statements.
Wondering if I can attach a java debugger so that I can debug java code more easily. If I run jps command, PID of WebKitWebProcess does show up. So someone is aware that this process has JVM. Preferred debugger will be eclipse.
Basically it boils down to how to attach java debugger where VM is stared using JNI_CreateJavaVM and process is already running.

Turned out to be very simple. When you create VM, just pass the debugger option as follows. Connect debugger to port 9836 and you in the debugger.
JavaVMOption options[3];
options[0].optionString = <your classpath>;
options[1].optionString = "-Xdebug";
options[2].optionString = "-agentlib:jdwp=transport=dt_socket,server=y,address=9836,suspend=n";

Related

IntelliJ idea: Show process id

I'm starting a java program from IntelliJ Idea which uses a .dll which is written by me in C++. After the startup of the application I can attach to the process using Microsoft Visual Studio (Debug / Attach to Process...) which allows me to debug the C++ part of the running application.
The name of the process is simply java. It is always a pain to select the right one from all the java processes. The simple Task Manager is not sufficient. The Process Explorer is good, but I still need to inspect multiple processes until I find the right one. It would be much easier for me if Idea would just tell the PID of the application it started.
Does Idea have such a feature?
(Win 7 64bit fresh # 2017-04-19, Idea 2017.1.1)
You can list all java processes with the jps utility (Can be executed from Idea terminal window) which makes it extremely easy to identify your process. Example scenario:
D:\projects\git\CENSORED>jps
12084
5476 WorkerMain
8772 WebSocketProxyMain
9444 Launcher
12920 RemoteMavenServer
13400 Bootstrap
7752 Jps
If you have a profiling application like "JProfiler" you can also very easily find the pid in the "quick attach" dialog.

How to enable Memory monitors in Eclipse (Java project)?

I am trying to inspect the memory contents (bytes) of an object in a Java project.
The program is paused in the Eclipse IDE.
How ever: the Memory view is disabled - as in: the Add Memory monitor + button is greyed out.
Why?
This question seems to be unanswered for quite a while and the one above sure doesn't help. This is the only post about it I could find.
In the Eclipse Help platform information about Memory view can be found in the C/C++ Development User Guide > Reference > Debug Views > Memory View but that isn't useful when were on Java. In the Java Development Guide it isn't as easy to find. Adding just Java Development Guide to the scope and searching for memory returns information about the Memory View in Running and Debugging but doesn't help very much. It is a view but isn't found in the same > Reference > Debug Views > as the C/C++ Guide.
When running a program in Debug mode in Eclipse C\C++ the + in the Memory view lights up and you can add addresses. However, running your program in Debug view doesn't seem to do the same in Eclipse Java. I'm assuming it can't be used in Java Eclipse or has a special case use that isn't mentioned.
Using JConsole
The JConsole graphical user interface is a monitoring tool that complies to the Java Management Extensions (JMX) specification. JConsole uses the extensive instrumentation of the Java Virtual Machine (Java VM) to provide information about the performance and resource consumption of applications running on the Java platform.
In the Java Platform, Standard Edition (Java SE platform) 6, JConsole has been updated to present the look and feel of the Windows and GNOME desktops (other platforms will present the standard Java graphical look and feel). The screen captures presented in this document were taken from an instance of the interface running on Windows XP.
Starting JConsole
The jconsole executable can be found in JDK_HOME/bin, where JDK_HOME is the directory in which the Java Development Kit (JDK) is installed. If this directory is in your system path, you can start JConsole by simply typing jconsole in a command (shell) prompt. Otherwise, you have to type the full path to the executable file.
Command Syntax
You can use JConsole to monitor both local applications, namely those running on the same system as JConsole, as well as remote applications, namely those running on other systems.
source : http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html

Find and kill a specific Java process from another Java App

I have several java processes running on a windows machine. I have a Java process which is supposed to monitor the other processes and periodically kill or restart new ones.
If I have a java process running com.foo.Main1 and one running com.foo.Main2 - how can my monitoring process find and kill just the Main2 process?
Update: I have some code that can execute a command line tasklist.exe and parse it, but no matter what I do, I only see the java.exe process, not which class is executing
Update 2: I do not have the ability to install non-java programs.
It's probably going to be a lot simpler using OS-specific tools and using Runtime.exec() to run them, but I'll try and give a platform independent answer:
It might be possible to do this platform independently using the Attach API. This comes with the JDK, so to use it just include tools.jar from your JDK on your program's classpath.
To get a list of virtual machines on the system, use VirtualMachine.list(). You can get/parse arguments from the virtual machine descriptor objects that are returned from this.
The attach API also allows you to load agents into already-running Java processes. Since you want to kill a Java process, you can write a Java agent that simply runs System.exit() (or if you really want it dead use Runtime.halt() instead) when the agent loads.
Once you identify the one you want to kill, attach to it and load the killer agent (the agent has to be built as a JAR file, accessible to the Java process it needs to be loaded into). Shortly after the agent is attached that process should die.
These links might help also:
An Oracle blog on the attach API
Package documentation for java.lang.instrument (has detailed instructions on how to build an agent JAR)
This is specific to Windows.
I was facing the same issue where I have to kill the specific java program using taskkill. When I run the java program, tasklist was showing the same program with Image name set as java.exe. But killing it using taskkill /F java.exe will stop all other java applications other than intended one which is not required.
So I run the same java program using:
start "MyProgramName" java java-program..
Here start command will open a new window and run the java program with window's title set to MyProgramName.
Now to kil this java-program use the following taskkill command:
taskkill /fi "MyProgramName"
Your Java program will be killed only. Rest will be unaffected.

How to attach a a Java agent to an already running VM instance

I am trying to attach a Java agent to a running Java instance using the attach API. The problem I am getting is when I check a process id of an already running Java process in Windows Task Manager and hard code it into the program attaching the java agent. Nothing happens. Is it so that we cannot attach a Java agent to an already running process.
I went through the dynamic agent loading tutorial it works fine but the only problem I see is, it detects the VM instance and dynamically attaches the Java agent to it. This instance none other than the one in which the current program for doing so is being launched.
Here requirement is to attach a Java agent to a already running VM instance like JMonitor and other analysis tools does. How to proceed kindly provide some direction.

Java JVMTI doesn't work alongside -Xdebug -Xrunjdwp

I spent the last 4 hours trying to set up Eclipse TPTP memory
profiling on a Tomcat instance that must be run remotely (i.e. not in
Eclipse). This should be possible according to the TPTP and Agent
Controller docs.
I installed the TPTP components (4.6.0) into my Eclipse (Galileo)
workbench, along with the Agent Controller according to the
instructions on the website. To enable the agent, I added the
following options to the command line that starts the Tomcat instance:
-agentlib:JPIBootLoader=JPIAgent:server=enabled;HeapProf:allocsites=true
and added the following directories to the front of the PATH:
D:\dev\tools\ac\plugins\org.eclipse.tptp.javaprofiler
D:\dev\tools\ac\bin
When attempting to start Tomcat I consistently got the following error
message:
ERROR: JDWP unable to get necessary JVMTI capabilities. ["debugInit.c",L279]
I did a lot of Googling but found nothing relevant; I tried
reinstalling TPTP and various versions of the Agent Controller.
In the end the problem turned out to be that I was starting Tomcat
with the "jpda" option, which catalina.bat translates into
-Xdebug -Xrunjdwp:transport=.....
Removing the "jpda" command argument caused JVMTI to start working.
SO, the question is: I found nothing during any of my searches to
indicate that a JVMTI agent is incompatible with debugging. Can
someone explain what is going on and why JVMTI + JDWP is not a valid
setup?
None of the answers so far are correct and this is the first hit that comes up on Google if you query the error mentioned, so I feel some clarification is needed.
JVMTI and JDWP do work together, in fact they generally must be used together. You will get ERROR: JDWP unable to get necessary JVMTI capabilities if -Xrunjdwp (and/or possibly -agentlib:jdwp) is specified more than once on the command line. To fix it, make sure you only have one of -Xrunjdwp or -agentlib:jdwp in your command line.
For more details, read on...
JVMTI (Java Virtual Machine Tool Interface) is the successor to JVMDI (Java Virtual Machine Debug Interface) and JVMPI (Java Virtual Machine Profiling Interface). It incorporates the functionality of both JVMDI and JVMPI, both of which were deprecated in Java 5 and removed in Java 6. It is the API that exposes the internals of the JVM for the purposes of debugging and profiling.
JDWP (Java Debug Wire Protocol) is a protocol that describes a simple mechanism for transmitting commands and responses. As far as I know, it is the only way for a debugger sitting outside the JVM to communicate with it and to interface with the JVMTI.
JDI (Java Debugger Interface) is a client-side (debugger-side) API which exposes some of the features of JVMTI while making use of JDWP more or less transparently.
The bug mentioned in Bob Dobbs's answer concerns the misleading error message, and the fact that the JVM will try to load JDWP once for every time it is specified on the command line. It doesn't state anywhere that JDWP and JVMTI cannot be used together.
More info here: https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzaha/jpdebuga.htm
I ran into the same problem as you, but I came up with a JVM bug report (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6354345) that cast some light on the issue. It basically comes down to the Java agent library not ever being intended to be loaded twice into the same VM. Sucks, but seems like it's basic limitation of the agent system that you can't do both at the same time.
For me it was the same issue as Code Bling post, they were duplicate -Xrunjdwp didn't realize there were a second -Xrunjdwp as it was hidden in the variable %JAVA_OPTIONS%, check your Application Server start script.

Categories

Resources