Skip to content

Commit be156ac

Browse files
committed
modules
1 parent 68b1039 commit be156ac

File tree

8 files changed

+249
-94
lines changed

8 files changed

+249
-94
lines changed

README

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ Traces execution paths in a JVM.
33
Build
44
-----
55

6-
$ cd agent
7-
$ mvn clean install
8-
$ cd ../client
96
$ mvn clean install
107

118
Run
129
---
1310

14-
After Build, from within client subfolder:
15-
16-
$ cd target
17-
$ java -jar tracer-client.jar <pid>
11+
$ cd demo/target
12+
$ java -jar tracer-demo.jar <target-pid>

client/pom.xml

+11-83
Original file line numberDiff line numberDiff line change
@@ -15,88 +15,16 @@
1515
<artifactId>tracer-client</artifactId>
1616
<version>0.1-SNAPSHOT</version>
1717

18-
<build>
19-
<plugins>
20-
<plugin>
21-
<!--
22-
Build a jar-with-dependencies (of course, exclude the agent from the jar).
23-
-->
24-
<artifactId>maven-assembly-plugin</artifactId>
25-
<executions>
26-
<execution>
27-
<id>jar-with-dependencies</id>
28-
<phase>package</phase>
29-
<goals><goal>single</goal></goals>
30-
<configuration>
31-
<archive>
32-
<manifest>
33-
<mainClass>com.jmolly.tracer.client.Client</mainClass>
34-
</manifest>
35-
</archive>
36-
<descriptors>
37-
<descriptor>assembly.xml</descriptor>
38-
</descriptors>
39-
<attach>false</attach>
40-
<finalName>${project.artifactId}</finalName>
41-
<appendAssemblyId>false</appendAssemblyId>
42-
</configuration>
43-
</execution>
44-
</executions>
45-
</plugin>
46-
<!--
47-
Copy the agent jar to target build directory.
48-
-->
49-
<plugin>
50-
<artifactId>maven-dependency-plugin</artifactId>
51-
<executions>
52-
<execution>
53-
<id>copy-agent</id>
54-
<phase>package</phase>
55-
<goals>
56-
<goal>copy</goal>
57-
</goals>
58-
<configuration>
59-
<artifactItems>
60-
<artifactItem>
61-
<groupId>com.jmolly.tracer</groupId>
62-
<artifactId>tracer-agent</artifactId>
63-
<version>${project.version}</version>
64-
</artifactItem>
65-
</artifactItems>
66-
<outputDirectory>${project.build.directory}</outputDirectory>
67-
<stripVersion>true</stripVersion>
68-
<overWriteReleases>false</overWriteReleases>
69-
<overWriteSnapshots>true</overWriteSnapshots>
70-
</configuration>
71-
</execution>
72-
</executions>
73-
</plugin>
74-
</plugins>
75-
</build>
76-
77-
<profiles>
78-
<profile>
79-
<id>windows-tools-jar</id>
80-
<activation>
81-
<os>
82-
<family>Windows</family>
83-
</os>
84-
</activation>
85-
<dependencies>
86-
<dependency>
87-
<groupId>com.sun</groupId>
88-
<artifactId>tools</artifactId>
89-
<version>1.4.2</version>
90-
<scope>system</scope>
91-
<systemPath>${java.home}/../lib/tools.jar</systemPath>
92-
<!--
93-
osx: <systemPath>${java.home}/../Classes/classes.jar</systemPath>
94-
But on osx, jdi/jps dependencies seem to be available through
95-
standard jdk classpath.
96-
-->
97-
</dependency>
98-
</dependencies>
99-
</profile>
100-
</profiles>
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.jmolly.tracer</groupId>
21+
<artifactId>tracer-model</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.google.code.gson</groupId>
26+
<artifactId>gson</artifactId>
27+
</dependency>
28+
</dependencies>
10129

10230
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.jmolly.tracer.client;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonStreamParser;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.io.UnsupportedEncodingException;
12+
13+
public final class EventStream extends InputStream {
14+
15+
private final Gson gson = new GsonBuilder().create();
16+
private final InputStream bin;
17+
private final JsonStreamParser parser;
18+
19+
public EventStream(InputStream bin) {
20+
this(bin, "UTF-8");
21+
}
22+
23+
private EventStream(InputStream bin, String charsetName) {
24+
this.bin = bin;
25+
this.parser = new JsonStreamParser(toReader(bin, charsetName));
26+
}
27+
28+
public boolean hasNext() {
29+
return parser.hasNext();
30+
}
31+
32+
public <T> T readType(Class<T> type) {
33+
final JsonElement element = parser.next();
34+
return gson.fromJson(element, type);
35+
}
36+
37+
public Object readEvent() {
38+
final JsonElement element = parser.next();
39+
return gson.fromJson(element,
40+
TracerTypes.toType(getTypeProperty(element)));
41+
}
42+
43+
@Override
44+
public int read() throws IOException {
45+
return bin.read();
46+
}
47+
48+
@Override
49+
public void close() throws IOException {
50+
bin.close();
51+
}
52+
53+
private static String getTypeProperty(JsonElement element) {
54+
return element.getAsJsonObject().get("type").getAsString();
55+
}
56+
57+
private static InputStreamReader toReader(InputStream bin, String charsetName) {
58+
try {
59+
return new InputStreamReader(bin, charsetName);
60+
} catch (UnsupportedEncodingException e) {
61+
throw new IllegalStateException(e);
62+
}
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.jmolly.tracer.client;
2+
3+
import com.jmolly.tracer.agent.model.CT;
4+
import com.jmolly.tracer.agent.model.EO;
5+
import com.jmolly.tracer.agent.model.ME;
6+
import com.jmolly.tracer.agent.model.MX;
7+
8+
final class TracerTypes {
9+
10+
private TracerTypes() {}
11+
12+
public static Class<?> toType(String type) {
13+
if (ME.type.equals(type)) {
14+
return ME.class;
15+
}
16+
if (MX.type.equals(type)) {
17+
return MX.class;
18+
}
19+
if (CT.type.equals(type)) {
20+
return CT.class;
21+
}
22+
if (EO.type.equals(type)) {
23+
return EO.class;
24+
}
25+
throw new IllegalStateException("no such type : " + type);
26+
}
27+
28+
}
File renamed without changes.

demo/pom.xml

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.jmolly.tracer</groupId>
9+
<artifactId>tracer-parent</artifactId>
10+
<version>0.1-SNAPSHOT</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<groupId>com.jmolly.tracer</groupId>
15+
<artifactId>tracer-demo</artifactId>
16+
<version>0.1-SNAPSHOT</version>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.jmolly.tracer</groupId>
21+
<artifactId>tracer-client</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
24+
</dependencies>
25+
26+
<profiles>
27+
<profile>
28+
<id>windows-tools-jar</id>
29+
<activation>
30+
<os>
31+
<family>Windows</family>
32+
</os>
33+
</activation>
34+
<dependencies>
35+
<dependency>
36+
<groupId>com.sun</groupId>
37+
<artifactId>tools</artifactId>
38+
<version>1.4.2</version>
39+
<scope>system</scope>
40+
<systemPath>${java.home}/../lib/tools.jar</systemPath>
41+
<!--
42+
osx: <systemPath>${java.home}/../Classes/classes.jar</systemPath>
43+
But on osx, jdi/jps dependencies seem to be available through
44+
standard jdk classpath.
45+
-->
46+
</dependency>
47+
</dependencies>
48+
</profile>
49+
</profiles>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<!--
55+
Build a jar-with-dependencies (of course, exclude the agent from the jar).
56+
-->
57+
<artifactId>maven-assembly-plugin</artifactId>
58+
<executions>
59+
<execution>
60+
<id>jar-with-dependencies</id>
61+
<phase>package</phase>
62+
<goals><goal>single</goal></goals>
63+
<configuration>
64+
<archive>
65+
<manifest>
66+
<mainClass>com.jmolly.tracer.demo.Demo</mainClass>
67+
</manifest>
68+
</archive>
69+
<descriptors>
70+
<descriptor>assembly.xml</descriptor>
71+
</descriptors>
72+
<attach>false</attach>
73+
<finalName>${project.artifactId}</finalName>
74+
<appendAssemblyId>false</appendAssemblyId>
75+
</configuration>
76+
</execution>
77+
</executions>
78+
</plugin>
79+
<!--
80+
Copy the agent jar to target build directory.
81+
-->
82+
<plugin>
83+
<artifactId>maven-dependency-plugin</artifactId>
84+
<executions>
85+
<execution>
86+
<id>copy-agent</id>
87+
<phase>package</phase>
88+
<goals>
89+
<goal>copy</goal>
90+
</goals>
91+
<configuration>
92+
<artifactItems>
93+
<artifactItem>
94+
<groupId>com.jmolly.tracer</groupId>
95+
<artifactId>tracer-agent</artifactId>
96+
<version>${project.version}</version>
97+
</artifactItem>
98+
</artifactItems>
99+
<outputDirectory>${project.build.directory}</outputDirectory>
100+
<stripVersion>true</stripVersion>
101+
<overWriteReleases>false</overWriteReleases>
102+
<overWriteSnapshots>true</overWriteSnapshots>
103+
</configuration>
104+
</execution>
105+
</executions>
106+
</plugin>
107+
</plugins>
108+
</build>
109+
110+
</project>

client/src/main/java/com/jmolly/tracer/client/Client.java renamed to demo/src/main/java/com/jmolly/tracer/demo/Demo.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
1-
package com.jmolly.tracer.client;
1+
package com.jmolly.tracer.demo;
22

3+
import com.jmolly.tracer.client.EventStream;
34
import com.sun.tools.attach.AgentInitializationException;
45
import com.sun.tools.attach.AgentLoadException;
56
import com.sun.tools.attach.AttachNotSupportedException;
67
import com.sun.tools.attach.VirtualMachine;
78

9+
import java.io.BufferedReader;
810
import java.io.File;
911
import java.io.IOException;
12+
import java.io.InputStreamReader;
13+
import java.io.PrintWriter;
14+
import java.net.Socket;
1015

11-
public class Client {
16+
public class Demo {
1217

1318
private static final String AGENT_JARNAME = "tracer-agent.jar";
1419

15-
public static void main(String[] args) {
16-
File agent = new File(new File("."), AGENT_JARNAME);
20+
public static void main(String[] args) throws Exception {
21+
File agent = new File(AGENT_JARNAME);
1722
if (!agent.isFile()) {
1823
throw new RuntimeException("Expected " + AGENT_JARNAME + " in working directoy.");
1924
}
2025
loadAgent(agent, args[0]);
26+
trace();
27+
}
28+
29+
private static void trace() throws Exception {
30+
Socket socket = new Socket("localhost", 5555);
31+
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
32+
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
33+
out.println("CONF");
34+
String line = in.readLine();
35+
if (!line.startsWith("OK")) {
36+
throw new RuntimeException("unexpected: " + line);
37+
}
38+
out.println("GO");
39+
EventStream events = new EventStream(socket.getInputStream());
40+
while (events.hasNext()) {
41+
System.out.println("[from agent] " + events.readEvent());
42+
}
43+
in.close();
2144
}
2245

2346
private static void loadAgent(File file, String pid) {

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<module>model</module>
1414
<module>agent</module>
1515
<module>client</module>
16+
<module>demo</module>
1617
</modules>
1718

1819
<url>https://github.com/jmolly/tracer</url>
@@ -89,6 +90,11 @@
8990
<artifactId>maven-surefire-plugin</artifactId>
9091
<version>2.12</version>
9192
</plugin>
93+
<plugin>
94+
<groupId>org.codehaus.mojo</groupId>
95+
<artifactId>exec-maven-plugin</artifactId>
96+
<version>1.2.1</version>
97+
</plugin>
9298
</plugins>
9399
<pluginManagement>
94100
<plugins>

0 commit comments

Comments
 (0)