content: package com.taobao.arthas.mcp.server.tool.function.monitor200;
  
  import com.taobao.arthas.mcp.server.tool.ToolContext;
  import com.taobao.arthas.mcp.server.tool.annotation.Tool;
  import com.taobao.arthas.mcp.server.tool.annotation.ToolParam;
  import com.taobao.arthas.mcp.server.tool.function.AbstractArthasTool;
  
  public class TraceTool extends AbstractArthasTool {
  
      public static final int DEFAULT_NUMBER_OF_EXECUTIONS = 3;
      public static final int DEFAULT_POLL_INTERVAL_MS = 100;
      public static final int DEFAULT_MAX_MATCH_COUNT = 50;
  
      /**
       * trace 方法内部调用路径跟踪工具
       * 追踪方法内部调用路径，输出每个节点的耗时信息
       */
      @Tool(
          name = "trace",
          description = "Trace 方法内部调用路径跟踪工具: 追踪方法内部调用路径，输出每个节点的耗时信息，对应 Arthas 的 trace 命令。",
          streamable = true
      )
      public String trace(
              @ToolParam(description = "类名表达式匹配，支持通配符，如demo.MathGame")
              String classPattern,
  
              @ToolParam(description = "方法名表达式匹配，支持通配符，如primeFactors", required = false)
              String methodPattern,
  
              @ToolParam(description = "OGNL条件表达式，包括#cost耗时过滤，如'#cost>100'表示耗时超过100ms", required = false)
              String condition,
  
              @ToolParam(description = "执行次数限制，默认值为3。达到指定次数后自动停止", required = false)
              Integer numberOfExecutions,
  
              @ToolParam(description = "开启正则表达式匹配，默认为通配符匹配，默认false", required = false)
              Boolean regex,
  
              @ToolParam(description = "指定Class最大匹配数量，默认50", required = false)
              Integer maxMatchCount,
  
              ToolContext toolContext
      ) {
          int execCount = getDefaultValue(numberOfExecutions, DEFAULT_NUMBER_OF_EXECUTIONS);
          int maxMatch = getDefaultValue(maxMatchCount, DEFAULT_MAX_MATCH_COUNT);
  
          StringBuilder cmd = buildCommand("trace");
  
          cmd.append(" -n ").append(execCount);
          cmd.append(" -m ").append(maxMatch);
  
          addFlag(cmd, "-E", regex);
  
          addParameter(cmd, classPattern);
  
          if (methodPattern != null && !methodPattern.trim().isEmpty()) {
              cmd.append(" ").append(methodPattern.trim());
          }
  
          addQuotedParameter(cmd, condition);
  
          return executeStreamable(toolContext, cmd.toString(), execCount, DEFAULT_POLL_INTERVAL_MS, 
                                  "Trace execution completed successfully");
      }
  }
  