Skip to content

Commit

Permalink
Use SLF4J SimpleLogger (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbachorik committed Jul 13, 2022
1 parent a07ed53 commit e615105
Show file tree
Hide file tree
Showing 47 changed files with 623 additions and 693 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

import java.util.concurrent.TimeUnit;
import org.openjdk.btrace.core.ArgsMap;
import org.openjdk.btrace.core.DebugSupport;
import org.openjdk.btrace.core.SharedSettings;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
Expand All @@ -40,7 +38,7 @@ public class OnMethodTemplateBenchmark {

@Setup
public void setup() {
argsMap = new ArgsMap(new String[] {"arg1=val1"}, new DebugSupport(SharedSettings.GLOBAL));
argsMap = new ArgsMap(new String[] {"arg1=val1"});
}

@Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS)
Expand Down
3 changes: 3 additions & 0 deletions btrace-agent/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
dependencies {
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.36'

implementation(group: 'org.ow2.asm', name: 'asm', version: "${rootProject.asmVersion}")

implementation files("${JAVA_8_HOME}/lib/tools.jar")
Expand Down
119 changes: 57 additions & 62 deletions btrace-agent/src/main/java/org/openjdk/btrace/agent/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,16 @@
import org.objectweb.asm.ClassWriter;
import org.openjdk.btrace.core.ArgsMap;
import org.openjdk.btrace.core.BTraceRuntime;
import org.openjdk.btrace.core.DebugSupport;
import org.openjdk.btrace.core.SharedSettings;
import org.openjdk.btrace.core.comm.*;
import org.openjdk.btrace.core.comm.Command;
import org.openjdk.btrace.core.comm.CommandListener;
import org.openjdk.btrace.core.comm.ErrorCommand;
import org.openjdk.btrace.core.comm.ExitCommand;
import org.openjdk.btrace.core.comm.InstrumentCommand;
import org.openjdk.btrace.core.comm.MessageCommand;
import org.openjdk.btrace.core.comm.RenameCommand;
import org.openjdk.btrace.core.comm.RetransformationStartNotification;
import org.openjdk.btrace.core.comm.StatusCommand;
import org.openjdk.btrace.instr.BTraceProbe;
import org.openjdk.btrace.instr.BTraceProbeFactory;
import org.openjdk.btrace.instr.BTraceProbePersisted;
Expand All @@ -66,6 +73,8 @@
import org.openjdk.btrace.instr.templates.impl.MethodTrackingExpander;
import org.openjdk.btrace.runtime.BTraceRuntimeAccess;
import org.openjdk.btrace.runtime.BTraceRuntimes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Abstract class that represents a BTrace client at the BTrace agent.
Expand All @@ -74,6 +83,8 @@
* @author J. Bachorik (j.bachorik@btrace.io)
*/
abstract class Client implements CommandListener {
private static final Logger log = LoggerFactory.getLogger(Client.class);

private static final Map<UUID, Client> CLIENTS = new ConcurrentHashMap<>();
private static final Map<String, PrintWriter> WRITER_MAP = new HashMap<>();
private static final Pattern SYSPROP_PTN = Pattern.compile("\\$\\{(.+?)}");
Expand All @@ -92,7 +103,6 @@ abstract class Client implements CommandListener {

private final Instrumentation inst;
final SharedSettings settings;
final DebugSupport debug;
final ArgsMap argsMap;
private final BTraceTransformer transformer;
volatile PrintWriter out;
Expand All @@ -113,7 +123,6 @@ private Client(Instrumentation inst, ArgsMap argsMap, SharedSettings s, BTraceTr
this.argsMap = argsMap;
settings = s != null ? s : SharedSettings.GLOBAL;
transformer = t;
debug = new DebugSupport(settings);

setupWriter();
CLIENTS.put(id, this);
Expand Down Expand Up @@ -144,7 +153,7 @@ private final void setupWriter() {
String outputDir = settings.getOutputDir();
String output = (outputDir != null ? outputDir + File.separator : "") + outputFile;
outputFile = templateOutputFileName(output);
infoPrint("Redirecting output to " + outputFile);
log.info("Redirecting output to {}", outputFile);
}
out = WRITER_MAP.get(outputFile);
if (out == null) {
Expand All @@ -159,7 +168,7 @@ private final void setupWriter() {
} else {
out =
new PrintWriter(
new BufferedWriter(TraceOutputWriter.fileWriter(new File(outputFile), settings)));
new BufferedWriter(TraceOutputWriter.fileWriter(new File(outputFile))));
}
}
WRITER_MAP.put(outputFile, out);
Expand Down Expand Up @@ -227,8 +236,8 @@ private String templateOutputFileName(String fName) {
.replace("[default]", "");

fName = replaceSysProps(fName);
if (dflt && settings.isDebug()) {
debugPrint("scriptOutputFile not specified. defaulting to " + fName);
if (dflt && log.isDebugEnabled()) {
log.debug("scriptOutputFile not specified. defaulting to {}", fName);
}
}
return fName;
Expand Down Expand Up @@ -282,23 +291,23 @@ synchronized void onExit(int exitCode) {

BTraceRuntime.leave();
try {
debugPrint("onExit:");
debugPrint("cleaning up transformers");
log.debug("onExit:");
log.debug("cleaning up transformers");
cleanupTransformers();
debugPrint("removing instrumentation");
log.debug("removing instrumentation");
retransformLoaded();
debugPrint("closing all I/O");
log.debug("closing all I/O");
Thread.sleep(300);
try {
closeAll();
} catch (IOException e) {
// ignore IOException when closing
}
debugPrint("done");
log.debug("done");
} catch (Throwable th) {
// ExitException is expected here
if (!th.getClass().getName().equals("ExitException")) {
debugPrint(th);
log.debug("Failed to gracefully exit BTrace probe", th);
BTraceRuntime.handleException(th);
}
} finally {
Expand All @@ -319,22 +328,22 @@ final Class<?> loadClass(InstrumentCommand instr, boolean canLoadPack) throws IO
try {
probe = load(btraceCode, ArgsMap.merge(argsMap, args), canLoadPack);
if (probe == null) {
debugPrint("Failed to load BTrace probe code");
log.debug("Failed to load BTrace probe code");
return null;
}

if (!settings.isTrusted()) {
probe.checkVerified();
}
} catch (Throwable th) {
debugPrint(th);
log.debug("Filed to load BTrace probe code", th);
errorExit(th);
return null;
}
if (isDebug()) {
debugPrint("creating BTraceRuntime instance for " + probe.getClassName());
if (log.isDebugEnabled()) {
log.debug("creating BTraceRuntime instance for {}", probe.getClassName());
}
runtime = BTraceRuntimes.getRuntime(probe.getClassName(), args, this, debug, inst);
runtime = BTraceRuntimes.getRuntime(probe.getClassName(), args, this, inst);
Runtime.getRuntime()
.addShutdownHook(
new Thread(
Expand All @@ -344,14 +353,14 @@ final Class<?> loadClass(InstrumentCommand instr, boolean canLoadPack) throws IO
}
}));
if (probe.isClassRenamed()) {
if (isDebug()) {
debugPrint("class renamed to " + probe.getClassName());
if (log.isDebugEnabled()) {
log.debug("class renamed to {}", probe.getClassName());
}
sendCommand(new RenameCommand(probe.getClassName()));
}
if (isDebug()) {
debugPrint("created BTraceRuntime instance for " + probe.getClassName());
debugPrint("sending Okay command");
if (log.isDebugEnabled()) {
log.debug("created BTraceRuntime instance for {}", probe.getClassName());
log.debug("sending Okay command");
}

sendCommand(new StatusCommand());
Expand All @@ -361,7 +370,7 @@ final Class<?> loadClass(InstrumentCommand instr, boolean canLoadPack) throws IO
entered = BTraceRuntimeAccess.enter(runtime);
return probe.register(runtime, transformer);
} catch (Throwable th) {
debugPrint(th);
log.debug("Failed to load BTrace probe", th);
errorExit(th);
return null;
} finally {
Expand All @@ -382,9 +391,9 @@ protected void closeAll() throws IOException {
}

private void errorExit(Throwable th) throws IOException {
debugPrint("sending error command");
log.debug("sending error command");
sendCommand(new ErrorCommand(th));
debugPrint("sending exit command");
log.debug("sending exit command");
sendCommand(new ExitCommand(1));
closeAll();
}
Expand All @@ -400,22 +409,6 @@ final boolean isInitialized() {
return initialized;
}

private void infoPrint(String msg) {
DebugSupport.info(msg);
}

final boolean isDebug() {
return settings.isDebug();
}

final void debugPrint(String msg) {
debug.debug(msg);
}

final void debugPrint(Throwable th) {
debug.debug(th);
}

final BTraceRuntime.Impl getRuntime() {
return runtime;
}
Expand All @@ -438,30 +431,32 @@ private final boolean isCandidate(Class<?> c) {

private final void startRetransformClasses(int numClasses) {
sendCommand(new RetransformationStartNotification(numClasses));
if (isDebug()) {
debugPrint("calling retransformClasses (" + numClasses + " classes to be retransformed)");
if (log.isDebugEnabled()) {
log.debug("calling retransformClasses ({} classes to be retransformed)", numClasses);
}
}

final void endRetransformClasses() {
sendCommand(new StatusCommand());
if (isDebug()) debugPrint("finished retransformClasses");
log.debug("finished retransformClasses");
}

// Internals only below this point
private BTraceProbe load(byte[] buf, ArgsMap args, boolean canLoadPack) {
BTraceProbeFactory f = new BTraceProbeFactory(settings, canLoadPack);
debugPrint("loading BTrace class");
log.debug("loading BTrace class");
BTraceProbe cn = f.createProbe(buf, args);

if (cn != null) {
if (isDebug()) {
if (cn.isVerified()) {
debugPrint("loaded '" + cn.getClassName() + "' successfully");
} else {
debugPrint(cn.getClassName() + " failed verification");
return null;
if (cn.isVerified()) {
if (log.isDebugEnabled()) {
log.debug("loaded '{}' successfully", cn.getClassName());
}
} else {
if (log.isDebugEnabled()) {
log.debug("{} failed verification", cn.getClassName());
}
return null;
}
}
return BTraceProbePersisted.from(cn);
Expand All @@ -473,13 +468,15 @@ boolean retransformLoaded() throws UnmodifiableClassException {
}
if (probe.isTransforming() && settings.isRetransformStartup()) {
ArrayList<Class<?>> list = new ArrayList<>();
debugPrint("retransforming loaded classes");
debugPrint("filtering loaded classes");
log.debug("retransforming loaded classes");
log.debug("filtering loaded classes");
ClassCache cc = ClassCache.getInstance();
for (Class<?> c : inst.getAllLoadedClasses()) {
if (c != null) {
if (inst.isModifiableClass(c) && isCandidate(c)) {
debugPrint("candidate " + c + " added");
if (log.isDebugEnabled()) {
log.debug("candidate {} added", c);
}
list.add(c);
}
}
Expand All @@ -490,14 +487,13 @@ boolean retransformLoaded() throws UnmodifiableClassException {
Class<?>[] classes = new Class[size];
list.toArray(classes);
startRetransformClasses(size);
if (isDebug()) {
if (log.isDebugEnabled()) {
for (Class<?> c : classes) {
try {
debugPrint("Attempting to retransform class: " + c.getName());
log.debug("Attempting to retransform class: {}", c.getName());
inst.retransformClasses(c);
} catch (ClassFormatError | VerifyError e) {
debugPrint("Class '" + c.getName() + "' verification failed");
debugPrint(e);
log.debug("Class '{}' verification failed", c.getName(), e);
sendCommand(
new MessageCommand(
"[BTRACE WARN] Class verification failed: "
Expand All @@ -520,8 +516,7 @@ boolean retransformLoaded() throws UnmodifiableClassException {
try {
inst.retransformClasses(c);
} catch (ClassFormatError | VerifyError e1) {
debugPrint("Class '" + c.getName() + "' verification failed");
debugPrint(e1);
log.debug("Class '{}' verification failed", c.getName(), e);
sendCommand(
new MessageCommand(
"[BTRACE WARN] Class verification failed: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.openjdk.btrace.core.DebugSupport;
import org.openjdk.btrace.core.comm.Command;
import org.openjdk.btrace.core.comm.ExitCommand;
import org.openjdk.btrace.core.comm.InstrumentCommand;
import org.openjdk.btrace.core.comm.PrintableCommand;
import org.openjdk.btrace.instr.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Represents a local client communicated by trace file. The trace script is specified as a File of
Expand All @@ -51,14 +52,16 @@
* @author J.Bachorik
*/
class FileClient extends Client {
private static final Logger log = LoggerFactory.getLogger(FileClient.class);

private final AtomicBoolean noOutputNotified = new AtomicBoolean(false);

private boolean canLoadPack = true;

FileClient(ClientContext ctx, File scriptFile) throws IOException {
super(ctx);
if (!init(readScript(scriptFile))) {
DebugSupport.warning("Unable to load BTrace script " + scriptFile);
log.warn("Unable to load BTrace script {}", scriptFile);
}
}

Expand All @@ -80,7 +83,7 @@ private static byte[] readAll(InputStream is, long size) throws IOException {
}

private boolean init(byte[] code) throws IOException {
InstrumentCommand cmd = new InstrumentCommand(code, argsMap, debug);
InstrumentCommand cmd = new InstrumentCommand(code, argsMap);
boolean ret = loadClass(cmd, canLoadPack) != null;
if (ret) {
initialize();
Expand All @@ -91,8 +94,8 @@ private boolean init(byte[] code) throws IOException {
@SuppressWarnings("RedundantThrows")
@Override
public void onCommand(Command cmd) throws IOException {
if (isDebug()) {
debugPrint("client " + getClassName() + ": got " + cmd);
if (log.isDebugEnabled()) {
log.debug("client {}: got {}", getClassName(), cmd);
}
switch (cmd.getType()) {
case Command.EXIT:
Expand All @@ -102,7 +105,7 @@ public void onCommand(Command cmd) throws IOException {
if (cmd instanceof PrintableCommand) {
if (out == null) {
if (noOutputNotified.compareAndSet(false, true)) {
DebugSupport.warning("No output stream. DataCommand output is ignored.");
log.debug("No output stream. DataCommand output is ignored.");
}
} else {
((PrintableCommand) cmd).print(out);
Expand Down
Loading

0 comments on commit e615105

Please sign in to comment.