-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from newbrough/feature/use-agent
agent and config changes
- Loading branch information
Showing
43 changed files
with
737 additions
and
432 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
gumshoe-hooks/src/main/java/com/dell/gumshoe/IoTraceUtil.java
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
gumshoe-hooks/src/main/java/com/dell/gumshoe/hook/Agent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.dell.gumshoe.hook; | ||
|
||
import sun.misc.IoTrace; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.instrument.ClassDefinition; | ||
import java.lang.instrument.Instrumentation; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarInputStream; | ||
|
||
/** install our version of IoTrace to capture socket and file I/O activity | ||
*/ | ||
public class Agent { | ||
private static final String CLASS_FILENAME = "sun/misc/IoTrace.class"; | ||
|
||
public static void premain(String args, Instrumentation inst) throws Exception { | ||
final byte[] alternate = getAlternate(); | ||
if(alternate==null) { | ||
System.out.println("GUMSHOE ERROR: failed to locate IoTrace hook"); | ||
return; | ||
} | ||
try { | ||
inst.redefineClasses(new ClassDefinition(IoTrace.class, alternate)); | ||
if(Boolean.getBoolean("gumshoe.verbose")) { | ||
System.out.println("GUMSHOE: installed IoTrace hook"); | ||
} | ||
} catch(Exception e) { | ||
System.out.println("GUMSHOE ERROR: failed to install IoTrace hook"); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** loop over classpath */ | ||
private static byte[] getAlternate() throws IOException { | ||
final String[] classpath = System.getProperty("java.class.path").split(System.getProperty("path.separator")); | ||
for(String entry : classpath) { | ||
final File file = new File(entry); | ||
if( ! file.canRead()) continue; | ||
|
||
if(file.isFile()) { | ||
final JarInputStream in = new JarInputStream(new FileInputStream(file)); | ||
try { | ||
final byte[] contents = getFromJar(in); | ||
if(contents!=null) { return contents; } | ||
} finally { | ||
in.close(); | ||
} | ||
} else { | ||
// this case happens during development | ||
if(file.isDirectory()) { | ||
final File classFile = new File(file, CLASS_FILENAME); | ||
if(classFile.isFile()) { | ||
return getFromFile(classFile); | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static byte[] getFromJar(JarInputStream jarIn) throws IOException { | ||
JarEntry entry; | ||
while((entry = jarIn.getNextJarEntry())!=null) { | ||
if(CLASS_FILENAME.equals(entry.getName())) { | ||
return getFromStream(jarIn, entry.getSize()); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static byte[] getFromFile(File file) throws IOException { | ||
final InputStream in = new BufferedInputStream(new FileInputStream(file)); | ||
try { | ||
return getFromStream(in, file.length()); | ||
} finally { | ||
in.close(); | ||
} | ||
} | ||
|
||
private static byte[] getFromStream(InputStream in, long size) throws IOException { | ||
final byte[] contents = new byte[(int)size]; | ||
int pos = 0; | ||
int len; | ||
while(size-pos>0 && (len=in.read(contents, pos, (int)size-pos))!=-1) { | ||
pos+=len; | ||
} | ||
return contents; | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...java/com/dell/gumshoe/IoTraceAdapter.java → ...com/dell/gumshoe/hook/IoTraceAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
gumshoe-hooks/src/main/java/com/dell/gumshoe/hook/IoTraceHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.dell.gumshoe.hook; | ||
|
||
import java.net.InetAddress; | ||
import java.net.SocketAddress; | ||
|
||
public class IoTraceHandler { | ||
private static IoTraceListener NULL_OBJECT = new IoTraceAdapter(); | ||
private static IoTraceListener DELEGATE = NULL_OBJECT; | ||
|
||
public synchronized static void addTrace(IoTraceListener delegate) throws Exception { | ||
if(DELEGATE==NULL_OBJECT) { | ||
DELEGATE = delegate; | ||
} else if(DELEGATE instanceof IoTraceMultiplexer) { | ||
final IoTraceMultiplexer multi = (IoTraceMultiplexer) DELEGATE; | ||
multi.addDelegate(delegate); | ||
} else { | ||
final IoTraceMultiplexer multi = new IoTraceMultiplexer(); | ||
multi.addDelegate(DELEGATE); | ||
multi.addDelegate(delegate); | ||
DELEGATE = multi; | ||
} | ||
} | ||
|
||
public static void removeTrace(IoTraceListener delegate) throws Exception { | ||
if(DELEGATE==delegate) { | ||
DELEGATE = NULL_OBJECT; | ||
} else if(DELEGATE instanceof IoTraceMultiplexer) { | ||
final IoTraceMultiplexer multi = (IoTraceMultiplexer) DELEGATE; | ||
multi.removeDelegate(delegate); | ||
} else { | ||
throw new IllegalArgumentException("unable to remove, that IoTraceListener was not installed: " + delegate); | ||
} | ||
} | ||
|
||
public static IoTraceListener getTrace() { | ||
return DELEGATE; | ||
} | ||
|
||
///// | ||
|
||
public static Object socketReadBegin() { | ||
return DELEGATE.socketReadBegin(); | ||
} | ||
|
||
public static void socketReadEnd(Object context, InetAddress address, int port, | ||
int timeout, long bytesRead) { | ||
DELEGATE.socketReadEnd(context, address, port, timeout, bytesRead); | ||
} | ||
|
||
public static Object socketWriteBegin() { | ||
return DELEGATE.socketWriteBegin(); | ||
} | ||
|
||
public static void socketWriteEnd(Object context, InetAddress address, int port, | ||
long bytesWritten) { | ||
DELEGATE.socketWriteEnd(context, address, port, bytesWritten); | ||
} | ||
|
||
public static void socketReadEnd(Object context, SocketAddress address, | ||
long bytesRead) { | ||
DELEGATE.socketReadEnd(context, address, bytesRead); | ||
} | ||
|
||
public static void socketWriteEnd(Object context, SocketAddress address, | ||
long bytesWritten) { | ||
DELEGATE.socketWriteEnd(context, address, bytesWritten); | ||
} | ||
|
||
public static Object datagramReadBegin() { | ||
return DELEGATE.datagramReadBegin(); | ||
} | ||
|
||
public static void datagramReadEnd(Object context, SocketAddress address, | ||
long bytesRead) { | ||
DELEGATE.datagramReadEnd(context, address, bytesRead); | ||
} | ||
|
||
public static Object datagramWriteBegin() { | ||
return DELEGATE.datagramWriteBegin(); | ||
} | ||
|
||
public static void datagramWriteEnd(Object context, SocketAddress address, | ||
long bytesWritten) { | ||
DELEGATE.datagramWriteEnd(context, address, bytesWritten); | ||
} | ||
|
||
public static Object fileReadBegin(String path) { | ||
return DELEGATE.fileReadBegin(path); | ||
} | ||
|
||
public static void fileReadEnd(Object context, long bytesRead) { | ||
DELEGATE.fileReadEnd(context, bytesRead); | ||
} | ||
|
||
public static Object fileWriteBegin(String path) { | ||
return DELEGATE.fileWriteBegin(path); | ||
} | ||
|
||
public static void fileWriteEnd(Object context, long bytesWritten) { | ||
DELEGATE.fileWriteEnd(context, bytesWritten); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.