Skip to content

Commit

Permalink
Fixes #132 - Add a rudimentary templating support for the scriptOutpu…
Browse files Browse the repository at this point in the history
…tFile argument
  • Loading branch information
jbachorik committed Jun 25, 2015
1 parent d42baad commit 11c002c
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src/share/classes/com/sun/btrace/agent/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.util.jar.JarFile;
import com.sun.btrace.BTraceRuntime;
import com.sun.btrace.comm.ErrorCommand;
import com.sun.btrace.comm.ExitCommand;
import com.sun.btrace.comm.OkayCommand;
import com.sun.btrace.runtime.OnProbe;
import com.sun.btrace.runtime.OnMethod;
Expand Down Expand Up @@ -90,6 +89,7 @@ public Thread newThread(Runnable r) {
private static void registerExitHook(final Client c) {
Runtime.getRuntime().addShutdownHook(new Thread(
new Runnable() {
@Override
public void run() {
BTraceRuntime rt = c.getRuntime();
if (rt != null) rt.handleExit(0);
Expand Down Expand Up @@ -162,6 +162,7 @@ private static synchronized void main(final String args, final Instrumentation i
return;
}
Thread agentThread = new Thread(new Runnable() {
@Override
public void run() {
BTraceRuntime.enter();
try {
Expand Down Expand Up @@ -224,7 +225,7 @@ private static void parseArgs(String args) {
args = "";
}
String[] pairs = args.split(",");
argMap = new HashMap<String, String>();
argMap = new HashMap<>();
for (String s : pairs) {
int i = s.indexOf('=');
String key, value = "";
Expand Down Expand Up @@ -347,14 +348,15 @@ private static void loadBTraceScript(String filename, boolean traceToStdOut) {
if (traceToStdOut) {
traceWriter = new PrintWriter(System.out);
} else {
String agentName = System.getProperty("btrace.agent", null);
String agentName = System.getProperty("btrace.agent", "default");
String currentBtraceScriptOutput = scriptOutputFile;

if (currentBtraceScriptOutput == null || currentBtraceScriptOutput.length() == 0) {
currentBtraceScriptOutput = filename + (agentName != null ? "." + agentName : "") + ".btrace";
currentBtraceScriptOutput = filename + (agentName != null ? "." + agentName : "") + ".${ts}.btrace";
if (isDebug()) debugPrint("scriptOutputFile not specified. defaulting to " + currentBtraceScriptOutput);
}
if (fileRollMilliseconds != null && fileRollMilliseconds.longValue() > 0) {
currentBtraceScriptOutput = templateFileName(currentBtraceScriptOutput);
if (fileRollMilliseconds != null && fileRollMilliseconds > 0) {
traceWriter = new PrintWriter(new BufferedWriter(TraceOutputWriter.rollingFileWriter(new File(currentBtraceScriptOutput), 100, fileRollMilliseconds, TimeUnit.MILLISECONDS)));
} else {
traceWriter = new PrintWriter(new BufferedWriter(TraceOutputWriter.fileWriter(new File(currentBtraceScriptOutput))));
Expand All @@ -364,13 +366,18 @@ private static void loadBTraceScript(String filename, boolean traceToStdOut) {
Client client = new FileClient(inst, traceScript, traceWriter);

handleNewClient(client);
} catch (RuntimeException re) {
} catch (RuntimeException | IOException re) {
if (isDebug()) debugPrint(re);
} catch (IOException ioexp) {
if (isDebug()) debugPrint(ioexp);
}
}

private static String templateFileName(String fName) {
if (fName != null) {
fName = fName.replace("${ts}", String.valueOf(System.currentTimeMillis()));
}
return fName;
}

public static final int BTRACE_DEFAULT_PORT = 2020;

//-- Internals only below this point
Expand Down Expand Up @@ -405,38 +412,33 @@ private static void startServer() {
Client client = new RemoteClient(inst, sock);
registerExitHook(client);
handleNewClient(client);
} catch (RuntimeException re) {
} catch (RuntimeException | IOException re) {
if (isDebug()) debugPrint(re);
} catch (IOException ioexp) {
if (isDebug()) debugPrint(ioexp);
}
}
}

private static void handleNewClient(byte[] code, PrintWriter traceWriter) {
try {
handleNewClient(new FileClient(inst, code, traceWriter));
} catch (RuntimeException re) {
} catch (RuntimeException | IOException re) {
if (isDebug()) {
debugPrint(re);
}
} catch (IOException ioexp) {
if (isDebug()) {
debugPrint(ioexp);
}
}
}

private static void handleNewClient(final Client client) {
serializedExecutor.submit(new Runnable() {

@Override
public void run() {
try {
if (isDebug()) debugPrint("new Client created " + client);
if (client.shouldAddTransformer()) {
client.registerTransformer();
Class[] classes = inst.getAllLoadedClasses();
ArrayList<Class> list = new ArrayList<Class>();
ArrayList<Class> list = new ArrayList<>();
if (isDebug()) debugPrint("filtering loaded classes");
for (Class c : classes) {
if (inst.isModifiableClass(c) &&
Expand Down Expand Up @@ -506,11 +508,8 @@ static void dumpClass(String btraceClassName, String targetClassName, byte[] cod
file += ".class";
new File(dir).mkdirs();
File out = new File(dir, file);
FileOutputStream fos = new FileOutputStream(out);
try {
try (FileOutputStream fos = new FileOutputStream(out)) {
fos.write(code);
} finally {
fos.close();
}
} catch (Exception exp) {
exp.printStackTrace();
Expand All @@ -523,7 +522,7 @@ static void dumpClass(String btraceClassName, String targetClassName, byte[] cod
* probe descriptor XML files.
*/
static List<OnMethod> mapOnProbes(List<OnProbe> onProbes) {
List<OnMethod> res = new ArrayList<OnMethod>();
List<OnMethod> res = new ArrayList<>();
for (OnProbe op : onProbes) {
String ns = op.getNamespace();
if (isDebug()) debugPrint("about to load probe descriptor for " + ns);
Expand Down

0 comments on commit 11c002c

Please sign in to comment.