Skip to content

Commit

Permalink
agent asm buf fix & end user script error gathering
Browse files Browse the repository at this point in the history
  • Loading branch information
gunlee01 committed Dec 7, 2015
1 parent b7385ae commit 5bc8dc2
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 197 deletions.
277 changes: 157 additions & 120 deletions scouter.agent.java/src/scouter/agent/AgentTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/
package scouter.agent;

import scouter.agent.asm.IASM;
import scouter.agent.asm.ScouterClassWriter;
import scouter.agent.asm.*;
import scouter.agent.asm.util.AsmUtil;
import scouter.agent.util.AsyncRunner;
import scouter.lang.conf.ConfObserver;
Expand All @@ -30,123 +29,161 @@
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.List;

public class AgentTransformer implements ClassFileTransformer {
public static ThreadLocal<ClassLoader> hookingCtx = new ThreadLocal<ClassLoader>();
private static List<IASM> asms = new ArrayList<IASM>();
// hook 관련 설정이 변경되면 자동으로 변경된다.
private static int hook_signature;
static {
final Configure conf = Configure.getInstance();
reload();
hook_signature = conf.getHookSignature();
ConfObserver.add("AgentTransformer", new Runnable() {
public void run() {
if (conf.getHookSignature() != hook_signature) {
reload();
}
hook_signature = conf.getHookSignature();
}
});
}
public static void reload() {
Configure conf = Configure.getInstance();
List<IASM> temp = new ArrayList<IASM>();
asms = temp;
}
// //////////////////////////////////////////////////////////////
// boot class이지만 Hooking되어야하는 클래스를 등록한다.
private static IntSet asynchook = new IntSet();
static {
asynchook.add("sun/net/www/protocol/http/HttpURLConnection".hashCode());
asynchook.add("sun/net/www/http/HttpClient".hashCode());
asynchook.add("java/net/Socket".hashCode());
asynchook.add("javax/naming/InitialContext".hashCode());
}
private Configure conf = Configure.getInstance();
private Logger.FileLog bciOut;
public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
try {
hookingCtx.set(loader);
if (className == null)
return null;
if (classBeingRedefined == null) {
if (asynchook.contains(className.hashCode())) {
AsyncRunner.getInstance().add(loader, className, classfileBuffer);
return null;
}
if (loader == null) {
return null;
}
}
if (className.startsWith("scouter/")) {
return null;
}
//
classfileBuffer = DirectPatch.patch(className, classfileBuffer);
ObjTypeDetector.check(className);
final ClassDesc classDesc = new ClassDesc();
ClassReader cr = new ClassReader(classfileBuffer);
cr.accept(new ClassVisitor(Opcodes.ASM4) {
public void visit(int version, int access, String name, String signature, String superName,
String[] interfaces) {
classDesc.set(version, access, name, signature, superName, interfaces);
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
classDesc.anotation += desc;
return super.visitAnnotation(desc, visible);
}
}, 0);
if (AsmUtil.isInterface(classDesc.access)) {
return null;
}
classDesc.classBeingRedefined = classBeingRedefined;
ClassWriter cw = getClassWriter(classDesc);
ClassVisitor cv = cw;
List<IASM> workAsms = asms;
for (int i = 0, max = workAsms.size(); i < max; i++) {
cv = workAsms.get(i).transform(cv, className, classDesc);
if (cv != cw) {
cr = new ClassReader(classfileBuffer);
cr.accept(cv, ClassReader.EXPAND_FRAMES);
classfileBuffer = cw.toByteArray();
cv = cw = getClassWriter(classDesc);
if (conf.log_asm_enabled) {
if (this.bciOut == null) {
this.bciOut = new Logger.FileLog("./scouter.bci");
}
this.bciOut.println(className + "\t\t[" + loader + "]");
}
}
}
return classfileBuffer;
} catch (Throwable t) {
Logger.println("A101", "Transformer Error", t);
t.printStackTrace();
} finally {
hookingCtx.set(null);
}
return null;
}
private ClassWriter getClassWriter(final ClassDesc classDesc) {
ClassWriter cw;
switch (classDesc.version) {
case Opcodes.V1_1:
case Opcodes.V1_2:
case Opcodes.V1_3:
case Opcodes.V1_4:
case Opcodes.V1_5:
case Opcodes.V1_6:
cw = new ScouterClassWriter(ClassWriter.COMPUTE_MAXS);
break;
default:
cw = new ScouterClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
}
return cw;
}
private void dump(String className, byte[] bytes) {
String fname = "/tmp/" + className.replace('/', '_');
FileUtil.save(fname, bytes);
}
public static ThreadLocal<ClassLoader> hookingCtx = new ThreadLocal<ClassLoader>();
private static List<IASM> asms = new ArrayList<IASM>();
// hook 관련 설정이 변경되면 자동으로 변경된다.
private static int hook_signature;

static {
final Configure conf = Configure.getInstance();
reload();
hook_signature = conf.getHookSignature();
ConfObserver.add("AgentTransformer", new Runnable() {
public void run() {
if (conf.getHookSignature() != hook_signature) {
reload();
}
hook_signature = conf.getHookSignature();
}
});
}

public static void reload() {
Configure conf = Configure.getInstance();
List<IASM> temp = new ArrayList<IASM>();
temp.add(new HttpServiceASM());
temp.add(new ServiceASM());

temp.add(new JDBCPreparedStatementASM());
temp.add(new JDBCResultSetASM());
temp.add(new JDBCStatementASM());
temp.add(new SqlMapASM());
temp.add(new UserTxASM());

temp.add(new JDBCConnectionOpenASM());
temp.add(new JDBCDriverASM());
temp.add(new InitialContextASM());

temp.add(new CapArgsASM());
temp.add(new CapReturnASM());
temp.add(new CapThisASM());

temp.add(new MethodASM());
temp.add(new ApicallASM());
temp.add(new ApicallInfoASM());
temp.add(new SpringReqMapASM());

temp.add(new SocketASM());

temp.add(new JspServletASM());

temp.add(new AddFieldASM());

asms = temp;
}

// //////////////////////////////////////////////////////////////
// boot class이지만 Hooking되어야하는 클래스를 등록한다.
private static IntSet asynchook = new IntSet();

static {
asynchook.add("sun/net/www/protocol/http/HttpURLConnection".hashCode());
asynchook.add("sun/net/www/http/HttpClient".hashCode());
asynchook.add("java/net/Socket".hashCode());
asynchook.add("javax/naming/InitialContext".hashCode());
}

private Configure conf = Configure.getInstance();
private Logger.FileLog bciOut;

public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
try {
hookingCtx.set(loader);
if (className == null)
return null;
if (classBeingRedefined == null) {
if (asynchook.contains(className.hashCode())) {
AsyncRunner.getInstance().add(loader, className, classfileBuffer);
return null;
}
if (loader == null) {
return null;
}
}
if (className.startsWith("scouter/")) {
return null;
}
//
classfileBuffer = DirectPatch.patch(className, classfileBuffer);
ObjTypeDetector.check(className);
final ClassDesc classDesc = new ClassDesc();
ClassReader cr = new ClassReader(classfileBuffer);
cr.accept(new ClassVisitor(Opcodes.ASM4) {
public void visit(int version, int access, String name, String signature, String superName,
String[] interfaces) {
classDesc.set(version, access, name, signature, superName, interfaces);
}

@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
classDesc.anotation += desc;
return super.visitAnnotation(desc, visible);
}
}, 0);
if (AsmUtil.isInterface(classDesc.access)) {
return null;
}
classDesc.classBeingRedefined = classBeingRedefined;
ClassWriter cw = getClassWriter(classDesc);
ClassVisitor cv = cw;
List<IASM> workAsms = asms;
for (int i = 0, max = workAsms.size(); i < max; i++) {
cv = workAsms.get(i).transform(cv, className, classDesc);
if (cv != cw) {
cr = new ClassReader(classfileBuffer);
cr.accept(cv, ClassReader.EXPAND_FRAMES);
classfileBuffer = cw.toByteArray();
cv = cw = getClassWriter(classDesc);
if (conf.log_asm_enabled) {
if (this.bciOut == null) {
this.bciOut = new Logger.FileLog("./scouter.bci");
}
this.bciOut.println(className + "\t\t[" + loader + "]");
}
}
}
return classfileBuffer;
} catch (Throwable t) {
Logger.println("A101", "Transformer Error", t);
t.printStackTrace();
} finally {
hookingCtx.set(null);
}
return null;
}

private ClassWriter getClassWriter(final ClassDesc classDesc) {
ClassWriter cw;
switch (classDesc.version) {
case Opcodes.V1_1:
case Opcodes.V1_2:
case Opcodes.V1_3:
case Opcodes.V1_4:
case Opcodes.V1_5:
case Opcodes.V1_6:
cw = new ScouterClassWriter(ClassWriter.COMPUTE_MAXS);
break;
default:
cw = new ScouterClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
}
return cw;
}

private void dump(String className, byte[] bytes) {
String fname = "/tmp/" + className.replace('/', '_');
FileUtil.save(fname, bytes);
}
}
2 changes: 1 addition & 1 deletion scouter.agent.java/src/scouter/agent/Configure.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public final static synchronized Configure getInstance() {
public int summary_enduser_error_max_count = 5000;

//EndUser
public String enduser_trace_endpoint_url = "_scouter_browser.jsp";
public String enduser_trace_endpoint_url = "/_scouter_browser.jsp";

//internal variables
private int objHash;
Expand Down
31 changes: 24 additions & 7 deletions scouter.agent.java/src/scouter/agent/summary/EndUserErrorData.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,31 @@
package scouter.agent.summary;

public class EndUserErrorData {
public int stacktrace;
public int count;

public int host;
public int uri;
public int stacktrace;
public int userAgent;
public int count;
//
public int uri;
public int name;
public int message;
public int file;
public int lineNumber;// :1
public int columnNumber;// :26
public int payloadVersion;// :2
public int lineNumber;
public int columnNumber;

@Override
public String toString() {
return "EndUserErrorData{" +
"count=" + count +
", host=" + host +
", uri=" + uri +
", stacktrace=" + stacktrace +
", userAgent=" + userAgent +
", name=" + name +
", message=" + message +
", file=" + file +
", lineNumber=" + lineNumber +
", columnNumber=" + columnNumber +
'}';
}
}
Loading

0 comments on commit 5bc8dc2

Please sign in to comment.