LinuxCommandLibrary

arthas-trace

Trace method execution details and timings

TLDR

Trace method invoke chain

$ trace [class-pattern] [method-pattern]
copy

Trace method invoke chains and only display invoke information longer than 10 ms
$ trace [class-pattern] [method-pattern] '#cost > [10]'
copy

Trace the invoke chain of multiple classes or multiple methods
$ trace -E [class-pattern1]|[class-patter2] [method-pattern1]|[method-pattern2]|[method-pattern3]
copy

Track method invoke chains, only display invoke information that exceeds 10 ms, and exit after 5 times
$ trace [class-pattern] [method-pattern] '#cost > [10]' -n 5
copy

SYNOPSIS

arthas-trace [-E] [-c <classloader-hash>] [<class-pattern>] [<method-pattern>]

PARAMETERS

-E
    Enable regex/glob pattern matching for class/method (default exact match)

-c
    Use classloader hashcode as class name (e.g., -c 18a8b for specific ClassLoader)

<class-pattern>
    Class name pattern (regex if -E; optional, defaults to all)

<method-pattern>
    Method name pattern (regex if -E; optional, defaults to all)

DESCRIPTION

arthas-trace is a key command in Arthas, an open-source Java diagnostic tool developed by Alibaba for runtime analysis of JVM processes. Similar to strace for system calls or perf for performance profiling, it traces method invocations by matching class and method names with regular expressions, outputting detailed timing information for each call.

This helps identify performance bottlenecks, slow methods, and execution paths in live applications without restarting. When executed within an attached Arthas session (via as.sh PID), it displays entry and exit timestamps, total elapsed time, and percentage relative to parent calls. Output example:
`ts=2023-01-01 12:00:00.123 | `[1:206] | id=1 cost=0.632ms ` -> doSleep()#Demo
ts=2023-01-01 12:00:00.123 | `[1:206] | id=2 cost=626.419ms (99.90%) ` -> doSleep()#Demo`

Supports conditional tracing (e.g., #{params[0] > 100}) for filtered output. Ideal for microservices, high-load systems, or debugging latency issues. Non-interactive use via shell scripts enables automation in CI/CD or monitoring pipelines.

Attach Arthas first with java -jar arthas-boot.jar, select PID, then run trace. Requires no code changes, leveraging Java instrumentation (JVMTI). Widely used in production for its low overhead and intuitive output.

CAVEATS

Requires Arthas attachment to running JVM (PID); high-traffic methods may flood output; use conditions to filter; not for native code; overhead ~5-10% on traced methods.

EXAMPLE USAGE

Interactive: trace Demo *
Non-interactive: ./as.sh 12345 "trace -E '*Exception' '*'"
Conditional: trace Demo doSum #{params[0]>100}

OUTPUT FORMAT

Each line shows: timestamp | [thread:id] | id=callId cost=time(ms) (%parent) ` -> method#class

HISTORY

Arthas released by Alibaba in March 2018 (v3.1.0); trace command core since inception, enhanced with regex (-E) in v3.2.0 and classloader support (-c) in v3.4.0. Active development, v3.7.x as of 2023.

SEE ALSO

strace(1), ltrace(1), bpftrace(1), perf(1)

Copied to clipboard