Skip to content

Commit

Permalink
feat stack report
Browse files Browse the repository at this point in the history
  • Loading branch information
yoloyyh committed Nov 26, 2024
1 parent 9e37d8c commit 0ada207
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
22 changes: 19 additions & 3 deletions rasp/jvm/JVMProbe/src/main/java/com/security/smith/SmithProbe.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public void start() {
smithproxyTimer.schedule(
smithproxyTimerTask,
0,
TimeUnit.MINUTES.toMillis(1)
TimeUnit.MINUTES.toMillis(10)
);
smithProxy.setClient(client);
smithProxy.setDisruptor(disruptor);
Expand Down Expand Up @@ -737,6 +737,22 @@ public void onEvent(Trace trace, long sequence, boolean endOfBatch) {
client.write(Operate.TRACE, jsonElement);
}

public StackTraceElement[] formatCurrentStackTrace() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
if (stackTrace == null) {
return new StackTraceElement[0];
}
int startIndex = 0;
for (int i = 0; i < stackTrace.length; i++) {
StackTraceElement element = stackTrace[i];
if (element.getClassName().startsWith("com.security.smithloader.SmithAgent")) {
startIndex = i;
break;
}
}
return Arrays.copyOfRange(stackTrace, startIndex + 1, stackTrace.length);
}

public void printClassfilter(ClassFilter data) {
SmithLogger.logger.info("------------------------------------------------------------------------");
SmithLogger.logger.info("className:" + data.getClassName());
Expand Down Expand Up @@ -833,7 +849,7 @@ private void checkClassFilter(ClassLoader loader, String className, byte[] class

classFilter.setRuleId(rule_id);
classFilter.setTransId();
classFilter.setStackTrace(Thread.currentThread().getStackTrace());
classFilter.setStackTrace(formatCurrentStackTrace());

Gson gson = new GsonBuilder()
.registerTypeAdapter(ClassFilter.class, new ClassFilterSerializer())
Expand Down Expand Up @@ -1219,7 +1235,7 @@ public void onScanAllClass() {
}
classFilter.setTransId();
classFilter.setRuleId(rule_id);
classFilter.setStackTrace(Thread.currentThread().getStackTrace());
classFilter.setStackTrace(formatCurrentStackTrace());

Gson gson = new GsonBuilder()
.registerTypeAdapter(ClassFilter.class, new ClassFilterSerializer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void trace(int classID, int methodID, Object[] args, Object ret, boolean
trace.setBlocked(blocked);
trace.setRet(ret);
trace.setArgs(args);
trace.setStackTrace(Thread.currentThread().getStackTrace());
trace.setStackTrace(SmithProbeObj.formatCurrentStackTrace());
trace.setTypes(SmithProbeObj.getFuncTypes(classID, methodID));

ringBuffer.publish(sequence);
Expand Down Expand Up @@ -290,7 +290,7 @@ public void sendMetadataClass(Class<?> cla, int classID, int methodID) {
classFilter.setClassId(classID);
classFilter.setMethodId(methodID);
classFilter.setTypes(SmithProbeObj.getFuncTypes(classID, methodID));
classFilter.setStackTrace(Thread.currentThread().getStackTrace());
classFilter.setStackTrace(SmithProbeObj.formatCurrentStackTrace());
if (client != null) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(ClassFilter.class, new ClassFilterSerializer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ private String[] convertStackTrace(StackTraceElement[] stackTrace) {
return ret;

try {
StackTraceElement[] elements = Arrays.copyOfRange(stackTrace, 2, stackTrace.length);
String[] result = new String[elements.length];
String[] result = new String[stackTrace.length];

for (int i = 0; i < elements.length; i++) {
result[i] = elements[i].toString();
for (int i = 0; i < stackTrace.length; i++) {
result[i] = stackTrace[i].toString();
}

return result;
Expand Down

0 comments on commit 0ada207

Please sign in to comment.