Trace / Trace Multiple Classes Or Methods
Trace Multiple Classes Or Methods
Use 'trace -E' with multiple patterns to isolate method calls on specific classes in a Java app, utilizing wildcards for granular analysis.
trace -E <class-pattern1>|<class-pattern2> <method-pattern1>|<method-pattern2>|<method-pattern3> trace -E <class-pattern1>|<class-pattern2> <method-pattern1>|<method-pattern2>|<method-pattern3> #!/bin/bash
# Trace Multiple Classes Or Methods
trace -E {{class-pattern1}}|{{class-pattern2}} {{method-pattern1}}|{{method-pattern2}}|{{method-pattern3}} import subprocess
# Trace Multiple Classes Or Methods
# Make sure to replace <placeholders> with actual values
def run_command():
cmd = [
"trace",
"-E",
"<class-pattern1>|<class-pattern2>",
"<method-pattern1>|<method-pattern2>|<method-pattern3>"
]
try:
print(f"Executing: {' '.join(cmd)}")
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
print(f"Error: {e}")
except FileNotFoundError:
print("Error: trace not found. Please install it first.")
if __name__ == "__main__":
run_command() When To Use
Deploy this command in high-load environments where specific classes or methods are suspected as bottlenecks. It is especially crucial when traditional logging adds too much overhead or when intermittent issues occur, making reproducing them challenging.
Pro Tip
Beware of excessive verbosity. Tracing too many classes or methods simultaneously can exhaust JVM resources and affect application stability. Utilize filtering effectively to prevent performance degradation.
Command Builder
Tune the command before you copy it
trace -E <class-pattern1>|<class-pattern2> <method-pattern1>|<method-pattern2>|<method-pattern3> Terminal Output
Expected runtime feedback
Tracing started for patterns:
┌───────────────────────┬───────────────────────┐
│ Class Pattern │ Method Pattern │
├───────────────────────┼───────────────────────┤
│ com.example.ClassOne │ methodOne │
│ com.example.ClassTwo │ methodTwo │
│ │ methodThree │
└───────────────────────┴───────────────────────┘
Tracing... Done.
Results stored in /var/log/trace_results.log Anatomy of Output
Understanding the result
TRACE BEGIN: com.example.ClassA* Start Trace Indicates the beginning of the trace for specified class patterns.
Executing: com.example.ClassA.doSomething() Method Entry Logs entry into a method matching the defined pattern.
Returning from: com.example.ClassA.doSomething() Method Exit Logs exiting from a method, useful for identifying execution duration.
TRACE BEGIN: org.example.ClassB* Start Trace Begins tracing for another class pattern as per the command.
Executing: org.example.ClassB.calculate() Method Start Identifies method calls that fall under the traced patterns.
Returning from: org.example.ClassB.calculate() Return from Method Captures the exit, relevant for timing analysis.
Power User Variants
Optimized versions
trace -E com.example.ClassA* com.example.ClassB* Traces all methods within the specified class patterns without method pattern constraints. Uses more memory due to unrestricted scope.
trace -E -D com.example.ClassA* com.example.ClassB* Adds debug output for all classes matching the pattern, including verbose parameter and result printing, useful for detailed debugging but slower.
Unix Pipeline
Shell combinations
trace -E com.example.* | grep 'exception' | awk '{print $5}' Combines tracing with grep and awk to isolate and analyze exception occurrences in method calls.
trace -E com.* | xargs -I {} sh -c 'echo {}: Traced' Executes a shell command for each traced method output, useful for automated response actions or notifications.
Troubleshooting
Common pitfalls
OutOfMemoryError: Java heap space
Solution: Reduce the scope of tracing or increase JVM heap size.
java.io.IOException: No space left on device
Solution: Ensure sufficient disk space is available to store trace logs.
SecurityException: Could not find class
Solution: Check classpath configurations to ensure all classes are loaded correctly.
Command Breakdown
What each part is doing
-
trace - Base Command
- The executable that performs this operation. Here it runs Trace before the shell applies any redirect operators.
-
<class-pattern1> - class pattern1
- The value supplied for class pattern1.
-
<class-pattern2> - class pattern2
- The value supplied for class pattern2.
-
<method-pattern1> - method pattern1
- The value supplied for method pattern1.
-
<method-pattern2> - method pattern2
- The value supplied for method pattern2.
-
<method-pattern3> - method pattern3
- The value supplied for method pattern3.
-
-E - Command Option
- Tool-specific option used by this command invocation.
How To Run
Execution path
- Step 1
Run the command: `trace -E com\.example\.ClassOne|com\.example\.ClassTwo methodOne|methodTwo|methodThree`
- Step 2
Check for output in: `/var/log/trace_results.log`
- Step 3
Analyze the captured trace data for performance issues.
Alternative Approaches
Comparable commands in other tools
Alternative observability tools for the same job.
sudo trace-cmd report --cpu <cpu_number> Trace Cmd / Start Tracing With Plugin Linux sudo trace-cmd start -p <function|function_graph|preemptirqsoff|irqsoff|preemptoff|wakeup|...> Trace Cmd / Stop Tracing Retain Buffers Linux sudo trace-cmd stop Trace Cmd / Record Trace With Plugin Linux sudo trace-cmd record -p <plugin> Trace Cmd / Clear Trace Buffers Linux sudo trace-cmd clear