Search in sources :

Example 11 with Command

use of com.shulie.instrument.simulator.api.annotation.Command in project LinkAgent by shulieTech.

the class RuntimeModule method info.

@Command(value = "info", description = "获取JVM运行时信息")
public CommandResponse info(final Map<String, String> args) {
    try {
        final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        final ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
        RuntimeInfo runtimeInfo = new RuntimeInfo();
        runtimeInfo.setTotalLoadedClassCount(classLoadingMXBean.getTotalLoadedClassCount());
        runtimeInfo.setLoadedClassCount(classLoadingMXBean.getLoadedClassCount());
        runtimeInfo.setUnloadedClassCount(classLoadingMXBean.getUnloadedClassCount());
        runtimeInfo.setVerbose(classLoadingMXBean.isVerbose());
        runtimeInfo.setName(runtimeMXBean.getName());
        runtimeInfo.setStartTime(runtimeMXBean.getStartTime());
        runtimeInfo.setManagementSpecVersion(runtimeMXBean.getManagementSpecVersion());
        runtimeInfo.setSpecName(runtimeMXBean.getSpecName());
        runtimeInfo.setSpecVersion(runtimeMXBean.getSpecVersion());
        runtimeInfo.setSpecVendor(runtimeMXBean.getSpecVendor());
        runtimeInfo.setVmName(runtimeMXBean.getVmName());
        runtimeInfo.setVmVersion(runtimeMXBean.getVmVersion());
        runtimeInfo.setVmVendor(runtimeMXBean.getVmVendor());
        runtimeInfo.setInputArguments(runtimeMXBean.getInputArguments());
        runtimeInfo.setClassPath(runtimeMXBean.getClassPath());
        runtimeInfo.setBootClassPath(runtimeMXBean.getBootClassPath());
        runtimeInfo.setLibraryPath(runtimeMXBean.getLibraryPath());
        runtimeInfo.setOsName(System.getProperty("os.name"));
        runtimeInfo.setOsVersion(System.getProperty("os.version"));
        runtimeInfo.setJavaVersion(System.getProperty("java.version"));
        runtimeInfo.setJavaHome(System.getProperty("java.home"));
        runtimeInfo.setSystemLoadAverage(ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage());
        runtimeInfo.setProcessors(ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors());
        runtimeInfo.setArch(ManagementFactory.getOperatingSystemMXBean().getArch());
        runtimeInfo.setUptime(runtimeMXBean.getUptime() / 1000);
        return CommandResponse.success(runtimeInfo);
    } catch (Throwable e) {
        return CommandResponse.failure(e);
    }
}
Also used : RuntimeInfo(com.shulie.instrument.simulator.module.model.runtime.RuntimeInfo) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 12 with Command

use of com.shulie.instrument.simulator.api.annotation.Command in project LinkAgent by shulieTech.

the class InfoModule method modules.

@Command(value = "commands", description = "查看所有的命令")
public CommandResponse modules() {
    try {
        List<CommandInfo> commands = new ArrayList<CommandInfo>();
        for (ExtensionModule module : moduleManager.list()) {
            Class<?> classOfModule = module.getClass();
            // 判断模块是否实现了@Information标记
            if (!classOfModule.isAnnotationPresent(ModuleInfo.class)) {
                continue;
            }
            final ModuleInfo info = classOfModule.getAnnotation(ModuleInfo.class);
            if (info == null) {
                continue;
            }
            for (final Method method : getMethodsListWithAnnotation(module.getClass(), Command.class)) {
                final Command commandAnnotation = method.getAnnotation(Command.class);
                if (null == commandAnnotation) {
                    continue;
                }
                CommandInfo commandInfo = new CommandInfo();
                commandInfo.setModuleId(info.id());
                commandInfo.setCommand(commandAnnotation.value());
                commandInfo.setCommandDescription(commandAnnotation.description());
                commands.add(commandInfo);
            }
        }
        return CommandResponse.success(commands);
    } catch (Throwable e) {
        LOGGER.error("SIMULATOR: execute command info/commands error.", e);
        return CommandResponse.failure(e);
    }
}
Also used : CommandInfo(com.shulie.instrument.simulator.module.model.info.CommandInfo) ModuleInfo(com.shulie.instrument.simulator.api.ModuleInfo) Command(com.shulie.instrument.simulator.api.annotation.Command) ArrayList(java.util.ArrayList) ExtensionModule(com.shulie.instrument.simulator.api.ExtensionModule) Method(java.lang.reflect.Method) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 13 with Command

use of com.shulie.instrument.simulator.api.annotation.Command in project LinkAgent by shulieTech.

the class JadModule method jad.

@Command(value = "jad", description = "反编译")
public CommandResponse jad(final Map<String, String> param) {
    try {
        final String cnPattern = getParameter(param, "class");
        final String classloader = getParameter(param, "classloader");
        final boolean sourceOnly = getBooleanParameter(param, "source-only", false);
        if (logger.isInfoEnabled()) {
            logger.info("SIMULATOR: param.class={}", cnPattern);
            logger.info("SIMULATOR: param.classloader={}", classloader);
            logger.info("SIMULATOR: param.source-only={}", sourceOnly);
        }
        Set<Class<?>> matchedClasses = loadedClassDataSource.find(new NameRegexFilter(cnPattern, ".*", true, true));
        if (matchedClasses == null || matchedClasses.isEmpty()) {
            return processNoMatch(cnPattern);
        } else if (matchedClasses.size() > 1 && classloader == null) {
            return processMatches(matchedClasses, cnPattern);
        } else {
            // matchedClasses size is 1
            // find inner classes.
            Set<Class<?>> withInnerClasses = loadedClassDataSource.find(new NameRegexFilter(matchedClasses.iterator().next().getName() + "$*", "*", true, true));
            withInnerClasses = filter(withInnerClasses, classloader);
            if (withInnerClasses.isEmpty()) {
                withInnerClasses = matchedClasses;
            }
            return processExactMatch(matchedClasses, withInnerClasses, sourceOnly);
        }
    } catch (Throwable e) {
        return CommandResponse.failure(e);
    }
}
Also used : NameRegexFilter(com.shulie.instrument.simulator.api.filter.NameRegexFilter) Set(java.util.Set) HashSet(java.util.HashSet) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 14 with Command

use of com.shulie.instrument.simulator.api.annotation.Command in project LinkAgent by shulieTech.

the class TraceModule method trace.

@Command(value = "info", description = "方法追踪")
public CommandResponse trace(final Map<String, String> param) {
    final String classPattern = param.get("class");
    String methodPattern = param.get("method");
    final String condition = param.get("condition");
    final String express = param.get("express");
    /**
     * 如果 wait 和 count 都没有填写,则默认统计20条
     */
    final int wait = ParameterUtils.getInt(param, "wait", 5000);
    /**
     * 最多层数
     */
    final int level = ParameterUtils.getInt(param, "level", 0);
    /**
     * 条数限定
     */
    final int limits = ParameterUtils.getInt(param, "limits", 100);
    if (StringUtil.isEmpty(classPattern)) {
        return CommandResponse.failure("class must not be empty.");
    }
    if (StringUtil.isEmpty(methodPattern)) {
        methodPattern = "*";
    }
    Set<EventWatcher> childrenWatchers = new ConcurrentHashSet<EventWatcher>();
    List<EventWatcher> watchers = new ArrayList<EventWatcher>();
    try {
        if (wait > 10 * 60 * 1000) {
            return CommandResponse.failure("wait 最大等待时间不能超过10分钟");
        }
        if (limits > 5000) {
            return CommandResponse.failure("limits 最大不能超过5000");
        }
        /**
         * 多少毫秒以下停止
         */
        final int stopInMills = ParameterUtils.getInt(param, "stop", -1);
        Queue<Object> traceViews = new ConcurrentLinkedQueue<Object>();
        Set<String> traceMethods = new ConcurrentHashSet<String>();
        Set<Class<?>> classes = findClasses(classPattern);
        Set<Class<?>> instrumentClasses = new HashSet<Class<?>>();
        boolean foundInterface = false;
        boolean foundEnum = false;
        boolean foundAnnotation = false;
        for (Class clazz : classes) {
            if (clazz.isInterface()) {
                Set<Class<?>> implClasses = findImplClasses(clazz);
                if (!implClasses.isEmpty()) {
                    for (Class implClass : implClasses) {
                        instrumentClasses.addAll(getProxyInterfaceImplClasses(implClass, clazz));
                    }
                } else {
                    foundInterface = true;
                }
            } else if (!clazz.isEnum() && !clazz.isAnnotation()) {
                instrumentClasses.addAll(getProxyInterfaceImplClasses(clazz));
            } else if (clazz.isEnum()) {
                foundEnum = true;
            } else if (clazz.isAnnotation()) {
                foundAnnotation = true;
            }
        }
        if (instrumentClasses.isEmpty()) {
            String errorMsg = "can't found class:" + classPattern;
            if (foundInterface) {
                errorMsg = "can't found impl class with interface:" + classPattern;
            } else if (foundEnum) {
                errorMsg = "can't trace class because of it is a enum:" + classPattern;
            } else if (foundAnnotation) {
                errorMsg = "can't trace class because of it is a annotation:" + classPattern;
            }
            return CommandResponse.failure(errorMsg);
        }
        final CountDownLatch latch = new CountDownLatch(instrumentClasses.size());
        for (Class clazz : instrumentClasses) {
            EventWatcher watcher = new EventWatchBuilder(moduleEventWatcher).onClass(clazz.getName()).includeSubClasses().onAnyBehavior(methodPattern).withInvoke().withCall().onListener(Listeners.of(TraceListener.class, new Object[] { true, latch, traceViews, traceMethods, childrenWatchers, condition, express, level, limits, stopInMills, wait })).onClass().onWatch();
            watchers.add(watcher);
        }
        if (wait > 0) {
            latch.await(wait, TimeUnit.MILLISECONDS);
        } else if (limits > 0) {
            latch.await();
        }
        return CommandResponse.success(traceViews);
    } catch (Throwable e) {
        logger.error("SIMULATOR: trace module err! class={}, method={}, express={}, condition={}, limits={}, wait={}", classPattern, methodPattern, express, condition, limits, wait, e);
        return CommandResponse.failure(e);
    } finally {
        for (EventWatcher watcher : watchers) {
            try {
                watcher.onUnWatched();
            } catch (Throwable e) {
                logger.error("SIMULATOR: trace module unwatched failed! class={}, method={}, express={}, condition={}, limits={}, wait={}", classPattern, methodPattern, express, condition, limits, wait, e);
            }
        }
        for (EventWatcher eventWatcher : childrenWatchers) {
            try {
                eventWatcher.onUnWatched();
            } catch (Throwable e) {
                logger.error("SIMULATOR: trace module unwatched failed! class={}, method={}, express={}, condition={}, limits={}, wait={}", classPattern, methodPattern, express, condition, limits, wait, e);
            }
        }
    }
}
Also used : EventWatcher(com.shulie.instrument.simulator.api.listener.ext.EventWatcher) ModuleEventWatcher(com.shulie.instrument.simulator.api.resource.ModuleEventWatcher) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentHashSet(com.shulie.instrument.simulator.module.util.ConcurrentHashSet) EventWatchBuilder(com.shulie.instrument.simulator.api.listener.ext.EventWatchBuilder) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ConcurrentHashSet(com.shulie.instrument.simulator.module.util.ConcurrentHashSet) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 15 with Command

use of com.shulie.instrument.simulator.api.annotation.Command in project LinkAgent by shulieTech.

the class WatchModule method info.

@Command(value = "info", description = "watch 方法")
public CommandResponse info(final Map<String, String> args) {
    String classPattern = getParameter(args, "class");
    String methodPattern = getParameter(args, "method");
    String express = getParameter(args, "express");
    String condition = getParameter(args, "condition");
    final int limits = getIntParameter(args, "limits", 20);
    final int wait = getIntParameter(args, "wait", -1);
    if (StringUtil.isEmpty(classPattern)) {
        return CommandResponse.failure("class must not be empty.");
    }
    if (StringUtil.isEmpty(methodPattern)) {
        methodPattern = "*";
    }
    EventWatcher watcher = null;
    try {
        if (limits > 5000) {
            return CommandResponse.failure("limits 最大不能超过5000");
        }
        final CountDownLatch latch = new CountDownLatch(1);
        Queue<Object> watchViews = new ConcurrentLinkedQueue<Object>();
        watcher = new EventWatchBuilder(moduleEventWatcher).onClass(classPattern).includeSubClasses().onBehavior(methodPattern).withInvoke().withCall().onListener(Listeners.of(WatchListener.class, new Object[] { latch, watchViews, condition, express, wait != -1 ? -1 : limits })).onClass().onWatch();
        if (wait > 0) {
            latch.await(wait, TimeUnit.SECONDS);
        } else if (limits > 0) {
            latch.await();
        }
        return CommandResponse.success(watchViews);
    } catch (Throwable e) {
        logger.error("SIMULATOR: watch module err! class={}, method={}, express={}, condition={}, limits={}, wait={}", classPattern, methodPattern, express, condition, limits, wait, e);
        return CommandResponse.failure(e);
    } finally {
        if (watcher != null) {
            try {
                watcher.onUnWatched();
            } catch (Throwable e) {
                logger.error("SIMULATOR: watch module unwatched failed! class={}, method={}, express={}, condition={}, limits={}, wait={}", classPattern, methodPattern, express, condition, limits, wait, e);
            }
        }
    }
}
Also used : EventWatcher(com.shulie.instrument.simulator.api.listener.ext.EventWatcher) ModuleEventWatcher(com.shulie.instrument.simulator.api.resource.ModuleEventWatcher) EventWatchBuilder(com.shulie.instrument.simulator.api.listener.ext.EventWatchBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Command(com.shulie.instrument.simulator.api.annotation.Command)

Aggregations

Command (com.shulie.instrument.simulator.api.annotation.Command)27 ArrayList (java.util.ArrayList)10 EventWatchBuilder (com.shulie.instrument.simulator.api.listener.ext.EventWatchBuilder)5 EventWatcher (com.shulie.instrument.simulator.api.listener.ext.EventWatcher)5 ModuleEventWatcher (com.shulie.instrument.simulator.api.resource.ModuleEventWatcher)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 File (java.io.File)4 Method (java.lang.reflect.Method)3 List (java.util.List)3 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)3 PullLogResponse (com.shulie.instrument.module.log.data.pusher.log.PullLogResponse)2 NameRegexFilter (com.shulie.instrument.simulator.api.filter.NameRegexFilter)2 ModuleInf (com.shulie.instrument.simulator.module.mgr.model.ModuleInf)2 GcInfo (com.shulie.instrument.simulator.module.model.gc.GcInfo)2 MemoryInfo (com.shulie.instrument.simulator.module.model.memory.MemoryInfo)2 HotSpotDiagnosticMXBean (com.sun.management.HotSpotDiagnosticMXBean)2 VMOption (com.sun.management.VMOption)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 JSONObject (com.alibaba.fastjson.JSONObject)1