<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Martin Schaaf's Blog &#187; jvm</title>
	<atom:link href="http://infram.wordpress.com/tag/jvm/feed/" rel="self" type="application/rss+xml" />
	<link>http://infram.wordpress.com</link>
	<description>programming languages and surroundings</description>
	<lastBuildDate>Thu, 10 Dec 2009 12:00:11 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='infram.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/c1326df8a34133d328ade8208e97c5b6?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Martin Schaaf's Blog &#187; jvm</title>
		<link>http://infram.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://infram.wordpress.com/osd.xml" title="Martin Schaaf&#8217;s Blog" />
		<item>
		<title>Eclipse Test &amp; Performance Tools Platform Project troubles</title>
		<link>http://infram.wordpress.com/2008/03/18/eclipse-test-performance-tools-platform-project-troubles/</link>
		<comments>http://infram.wordpress.com/2008/03/18/eclipse-test-performance-tools-platform-project-troubles/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 10:58:30 +0000</pubDate>
		<dc:creator>mascha</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[BachelorArbeit]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java debugging interface]]></category>
		<category><![CDATA[jdi]]></category>
		<category><![CDATA[jvm]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[single step event]]></category>

		<guid isPermaLink="false">http://infram.wordpress.com/?p=157</guid>
		<description><![CDATA[After using TPTP for about a half year for my bachelor I am at a point where I would remove it. But I don&#8217;t have to decide it. I reported so far 6 bugs. Everyone of them detains me on profiling. I can create single traces but if I repeat tracing I am in trouble. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=infram.wordpress.com&blog=979786&post=157&subd=infram&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>After using TPTP for about a half year for my bachelor I am at a point where I would remove it. But I don&#8217;t have to decide it. I reported so far <a href="https://bugs.eclipse.org/bugs/buglist.cgi?bug_status=NEW&amp;bug_status=ASSIGNED&amp;bug_status=REOPENED&amp;email1=mascha%40ma-scha.de&amp;emailtype1=exact&amp;emailassigned_to1=1&amp;emailreporter1=1">6 bugs</a>. Everyone of them detains me on profiling. I can create single traces but if I repeat tracing I am in trouble. JVM crashes, 100% CPU usages and so on. I have now written my own &#8220;profiler&#8221; two classes that give me the covered lines back of a profiled program. It uses the Java debugging interface (JDI).</p>
<p>The first class is called Agent that starts a event catcher thread (the debugging agent) if it is called without any arguments. This debugging agent waits for a connecting JVM. With a argument it calls a sample method to test profiling. You have to supply the debugging agent port and host like <i>-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:55555</i>. The other class is EventCatcherThread which enables the single step event and waits for a connecting JVM to profile a program. Only 278 lines of java code are necessary.</p>
<p><b>Agent.java</b></p>
<pre>import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.sun.jdi.connect.IllegalConnectorArgumentsException;
import com.sun.jdi.connect.VMStartException;

public class Agent {

	private ArrayList&lt;String&gt; _linesPassed = new ArrayList&lt;String&gt;();

	public void addSourceCodeLine() {
		_linesPassed.add("bla");
	}

	public List&lt;String&gt; getLinesPassed() {
		return _linesPassed;
	}

	public static void debug() throws Exception {
		List&lt;String&gt; passedLine = new ArrayList&lt;String&gt;();
		EventCatcherThread eventThread = EventCatcherThread.connectToVm(55555);
		eventThread.setPassedCodeLines(passedLine);
		eventThread.start();
		eventThread.resumeVM();
		eventThread.join();

		for (Iterator&lt;String&gt; iterator = passedLine.iterator(); iterator
				.hasNext();) {
			String lineKey = (String) iterator.next();
			System.out.println(lineKey);
		}
	}

	public static void main(String[] args) throws Exception,
			IllegalConnectorArgumentsException, VMStartException {
		if (args.length != 0) {
			debug();
		} else {
			Agent agent = new Agent();
			agent.traceTest();
		}
	}

	public void traceTest() {
		String string = new String();
		string = string + " fdgvdf ";
		string = string.concat("bla");
		System.out.println(string);
	}
}</pre>
<p><b>EventCatcherThread.java<br />
</b></p>
<pre>package org.projectory.ezunit;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jdi.Bootstrap;

import com.sun.jdi.AbsentInformationException;
import com.sun.jdi.ThreadReference;
import com.sun.jdi.VMDisconnectedException;
import com.sun.jdi.VirtualMachine;
import com.sun.jdi.connect.AttachingConnector;
import com.sun.jdi.connect.IllegalConnectorArgumentsException;
import com.sun.jdi.connect.Connector.Argument;
import com.sun.jdi.event.Event;
import com.sun.jdi.event.EventIterator;
import com.sun.jdi.event.EventQueue;
import com.sun.jdi.event.EventSet;
import com.sun.jdi.event.StepEvent;
import com.sun.jdi.event.VMDeathEvent;
import com.sun.jdi.event.VMDisconnectEvent;
import com.sun.jdi.event.VMStartEvent;
import com.sun.jdi.request.EventRequest;
import com.sun.jdi.request.EventRequestManager;
import com.sun.jdi.request.StepRequest;

public class EventCatcherThread extends Thread {

	private final VirtualMachine _vm;

	private String[] _excludes;

	private boolean _connected = true;

	private String[] _includes;

	private Map&lt;String, Location&gt; _lineStat = new HashMap&lt;String, Location&gt;();

	private List&lt;String&gt; _passedCodeLines;

	EventCatcherThread(VirtualMachine vm, String[] excludes, String[] includes) {
		super("event-handler");
		_vm = vm;
		_excludes = excludes;
		_includes = includes;
	}

	public void run() {
		EventQueue queue = _vm.eventQueue();
		List&lt;ThreadReference&gt; allThreads = _vm.allThreads();
		for (ThreadReference thread : allThreads) {
			if (thread.uniqueID() == 1) {
				enableSingleStepEvent(thread);
			}
		}

		while (_connected) {
			try {
				EventSet eventSet = queue.remove();
				EventIterator it = eventSet.eventIterator();
				while (it.hasNext()) {
					Event nextEvent = it.nextEvent();
					handleEvent(nextEvent);
				}
				eventSet.resume();
			} catch (InterruptedException exc) {
				// Ignore
			} catch (VMDisconnectedException discExc) {
				handleDisconnectedException();
				break;
			}
		}
	}

	private void enableSingleStepEvent(ThreadReference thread) {
		EventRequestManager mgr = _vm.eventRequestManager();
		StepRequest req = mgr.createStepRequest(thread, StepRequest.STEP_LINE,
				StepRequest.STEP_INTO);
		req.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
		for (int i = 0; i &lt; _excludes.length; ++i) {
			req.addClassExclusionFilter(_excludes[i]);
		}
		for (int i = 0; i &lt; _includes.length; ++i) {
			req.addClassFilter(_includes[i]);
		}
		req.enable();
	}

	private void singleStepEvent(StepEvent event) {
		if (_passedCodeLines != null) {
			try {
				final String sourcePath = event.location().sourcePath();
				final int lineNumber = event.location().lineNumber();
				final String method = event.location().method().toString();
				final String lineKey = sourcePath + ":" + method  + ":" + lineNumber;
				if (_lineStat.containsKey(lineKey)) {
					Location location = _lineStat.get(lineKey);
					location.countCall();
				} else {
					Location location = new Location(sourcePath, method, lineNumber);
					location.countCall();
					_lineStat.put(lineKey, location);
				}
				if (!_passedCodeLines.contains(lineKey)) {
					_passedCodeLines.add(lineKey);
				}
			} catch (AbsentInformationException e) {
				e.printStackTrace();
			}
		} else {
			System.err
					.println("Cannot set line numbers, because the container is null!");
		}
	}

	/**
	 * Dispatch incoming events
	 */
	private void handleEvent(Event event) {
		if (event instanceof StepEvent) {
			singleStepEvent((StepEvent) event);
		} else if (event instanceof VMStartEvent) {
			vmStartEvent((VMStartEvent) event);
		} else if (event instanceof VMDisconnectEvent) {
			vmDisconnectEvent((VMDisconnectEvent) event);
		} else if (event instanceof VMDeathEvent) {
			// application exit
		} else {
			System.out.println("Unhandled event type: " + event.getClass());
		}
	}

	private void handleDisconnectedException() {
		EventQueue queue = _vm.eventQueue();
		while (_connected) {
			try {
				EventSet eventSet = queue.remove();
				EventIterator iter = eventSet.eventIterator();
				while (iter.hasNext()) {
					Event event = iter.nextEvent();
					if (event instanceof VMDisconnectEvent) {
						vmDisconnectEvent((VMDisconnectEvent) event);
					}
				}
				eventSet.resume();
			} catch (InterruptedException exc) {
				// ignore
			}
		}
	}

	/**
	 * Enable the SingleStepEvent if the VM was started in suspend mode.
	 * @param event
	 */
	private void vmStartEvent(VMStartEvent event) {
		EventRequestManager mgr = _vm.eventRequestManager();
		StepRequest req = mgr.createStepRequest(event.thread(),
				StepRequest.STEP_LINE, StepRequest.STEP_INTO);
		req.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
		for (int i = 0; i &lt; _excludes.length; ++i) {
			req.addClassExclusionFilter(_excludes[i]);
		}
		for (int i = 0; i &lt; _includes.length; ++i) {
			req.addClassFilter(_includes[i]);
		}
		req.enable();
	}

	private void vmDisconnectEvent(VMDisconnectEvent event) {
		_connected = false;
	}

	@SuppressWarnings("unchecked")
	public static EventCatcherThread connectToVm(int port) throws Error,
			IOException, InterruptedException,
			IllegalConnectorArgumentsException {
		AttachingConnector c = getConnector();
		Map&lt;String, Argument&gt; arguments = c.defaultArguments();
		Argument portArg = arguments.get("port");
		portArg.setValue("" + port);
		// FIXME: localhost doesn't work
		Argument hostArg = arguments.get("hostname");
		hostArg.setValue("elaste");
		VirtualMachine myVM = c.attach(arguments);
		myVM.setDebugTraceMode(0);
		final String[] excludes = { "org.hibernate.*", "net.sf.cglib.*",
				"org.junit.*", "java.*", "javax.*", "sun.*", "com.sun.*" };
		final String[] includes = { };
		EventCatcherThread eventThread = new EventCatcherThread(myVM, excludes,
				includes);

		return eventThread;
	}

	private static AttachingConnector getConnector() {
		AttachingConnector result = null;
		List&lt;AttachingConnector&gt; allConnectors = Bootstrap
				.virtualMachineManager().attachingConnectors();
		if (allConnectors.size() &gt; 0) {
			result = allConnectors.get(0);
		}
		return result;
	}

	public void resumeVM() {
		_vm.resume();
	}

	public List&lt;String&gt; getPassedCodeLines() {
		return _passedCodeLines;
	}

	public void setPassedCodeLines(List&lt;String&gt; codeLines) {
		_passedCodeLines = codeLines;
	}

	public Location getLineStat(String lineKey) {
		return _lineStat.get(lineKey);
	}
}</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/infram.wordpress.com/157/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/infram.wordpress.com/157/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/infram.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/infram.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/infram.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/infram.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/infram.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/infram.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/infram.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/infram.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/infram.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/infram.wordpress.com/157/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=infram.wordpress.com&blog=979786&post=157&subd=infram&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://infram.wordpress.com/2008/03/18/eclipse-test-performance-tools-platform-project-troubles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">mascha</media:title>
		</media:content>
	</item>
		<item>
		<title>A Collection of JVM Options</title>
		<link>http://infram.wordpress.com/2008/02/15/a-collection-of-jvm-options/</link>
		<comments>http://infram.wordpress.com/2008/02/15/a-collection-of-jvm-options/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 23:36:27 +0000</pubDate>
		<dc:creator>mascha</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jvm]]></category>
		<category><![CDATA[jvm options]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://infram.wordpress.com/2008/02/15/a-collection-of-jvm-options/</guid>
		<description><![CDATA[A Collection of JVM Options Show also in what version the option exists.
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=infram.wordpress.com&blog=979786&post=45&subd=infram&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://blogs.sun.com/watt/resource/jvm-options-list.html">A Collection of JVM Options</a> Show also in what version the option exists.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/infram.wordpress.com/45/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/infram.wordpress.com/45/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/infram.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/infram.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/infram.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/infram.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/infram.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/infram.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/infram.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/infram.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/infram.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/infram.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=infram.wordpress.com&blog=979786&post=45&subd=infram&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://infram.wordpress.com/2008/02/15/a-collection-of-jvm-options/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">mascha</media:title>
		</media:content>
	</item>
	</channel>
</rss>