<!DOCTYPE html> <!--[if IE 8]><html class="no-js lt-ie9" lang="zh-CN" > <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js" lang="zh-CN" > <!--<![endif]--> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>tt — Arthas 3.3.6 文档</title> <link rel="shortcut icon" href="_static/favicon.ico"/> <script type="text/javascript" src="_static/js/modernizr.min.js"></script> <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <script type="text/javascript" src="_static/language_data.js"></script> <script type="text/javascript" src="_static/translations.js"></script> <script type="text/javascript" src="_static/js/theme.js"></script> <link rel="stylesheet" href="_static/css/theme.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <link rel="stylesheet" href="_static/overrides.css" type="text/css" /> <link rel="index" title="索引" href="genindex.html" /> <link rel="search" title="搜索" href="search.html" /> <link rel="next" title="profiler" href="profiler.html" /> <link rel="prev" title="stack" href="stack.html" /> <script src="_static/center_page.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/github.min.css"> </head> <body class="wy-body-for-nav"> <div class="wy-grid-for-nav"> <nav data-toggle="wy-nav-shift" class="wy-nav-side"> <div class="wy-side-scroll"> <div class="wy-side-nav-search" > <a href="index.html" class="icon icon-home"> Arthas </a> <div class="version"> 3.3.6 </div> <div role="search"> <form id="rtd-search-form" class="wy-form" action="search.html" method="get"> <input type="text" name="q" placeholder="Search docs" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> <ul class="current"> <li class="toctree-l1"><a class="reference external" href="https://mp.weixin.qq.com/s/VVSc_a3mLELW35lMH7rR0Q">实习生招聘</a></li> <li class="toctree-l1"><a class="reference external" href="https://mp.weixin.qq.com/s/677GO-3kYx5ygG9rlOlfQg">社招信息</a></li> <li class="toctree-l1"><a class="reference external" href="https://developer.aliyun.com/article/751641">技术征文</a></li> <li class="toctree-l1"><a class="reference external" href="https://alibaba.github.io/arthas/en/">English Docs</a></li> <li class="toctree-l1"><a class="reference external" href="https://alibaba.github.io/arthas/arthas-tutorials?language=cn">在线教程(推荐)</a></li> <li class="toctree-l1"><a class="reference internal" href="install-detail.html">安装</a></li> <li class="toctree-l1"><a class="reference internal" href="download.html">下载</a></li> <li class="toctree-l1"><a class="reference internal" href="quick-start.html">快速入门</a></li> <li class="toctree-l1"><a class="reference internal" href="advanced-use.html">进阶使用</a></li> <li class="toctree-l1 current"><a class="reference internal" href="commands.html">命令列表</a><ul class="current"> <li class="toctree-l2"><a class="reference internal" href="dashboard.html">dashboard</a></li> <li class="toctree-l2"><a class="reference internal" href="thread.html">thread</a></li> <li class="toctree-l2"><a class="reference internal" href="jvm.html">jvm</a></li> <li class="toctree-l2"><a class="reference internal" href="sysprop.html">sysprop</a></li> <li class="toctree-l2"><a class="reference internal" href="sysenv.html">sysenv</a></li> <li class="toctree-l2"><a class="reference internal" href="vmoption.html">vmoption</a></li> <li class="toctree-l2"><a class="reference internal" href="perfcounter.html">perfcounter</a></li> <li class="toctree-l2"><a class="reference internal" href="logger.html">logger</a></li> <li class="toctree-l2"><a class="reference internal" href="mbean.html">mbean</a></li> <li class="toctree-l2"><a class="reference internal" href="getstatic.html">getstatic</a></li> <li class="toctree-l2"><a class="reference internal" href="ognl.html">ognl</a></li> <li class="toctree-l2"><a class="reference internal" href="sc.html">sc</a></li> <li class="toctree-l2"><a class="reference internal" href="sm.html">sm</a></li> <li class="toctree-l2"><a class="reference internal" href="dump.html">dump</a></li> <li class="toctree-l2"><a class="reference internal" href="heapdump.html">heapdump</a></li> <li class="toctree-l2"><a class="reference internal" href="jad.html">jad</a></li> <li class="toctree-l2"><a class="reference internal" href="classloader.html">classloader</a></li> <li class="toctree-l2"><a class="reference internal" href="mc.html">mc</a></li> <li class="toctree-l2"><a class="reference internal" href="redefine.html">redefine</a></li> <li class="toctree-l2"><a class="reference internal" href="monitor.html">monitor</a></li> <li class="toctree-l2"><a class="reference internal" href="watch.html">watch</a></li> <li class="toctree-l2"><a class="reference internal" href="trace.html">trace</a></li> <li class="toctree-l2"><a class="reference internal" href="stack.html">stack</a></li> <li class="toctree-l2 current"><a class="current reference internal" href="#">tt</a><ul> <li class="toctree-l3"><a class="reference internal" href="#id1">使用参考</a><ul> <li class="toctree-l4"><a class="reference internal" href="#demo">启动 Demo</a></li> <li class="toctree-l4"><a class="reference internal" href="#id2">记录调用</a></li> <li class="toctree-l4"><a class="reference internal" href="#id3">检索调用记录</a></li> <li class="toctree-l4"><a class="reference internal" href="#id4">查看调用信息</a></li> <li class="toctree-l4"><a class="reference internal" href="#id5">重做一次调用</a></li> </ul> </li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="profiler.html">profiler</a></li> <li class="toctree-l2"><a class="reference internal" href="cat.html">cat</a></li> <li class="toctree-l2"><a class="reference internal" href="echo.html">echo</a></li> <li class="toctree-l2"><a class="reference internal" href="grep.html">grep</a></li> <li class="toctree-l2"><a class="reference internal" href="tee.html">tee</a></li> <li class="toctree-l2"><a class="reference internal" href="pwd.html">pwd</a></li> <li class="toctree-l2"><a class="reference internal" href="options.html">options</a></li> <li class="toctree-l2"><a class="reference internal" href="commands.html#arthas">Arthas 基础命令</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="web-console.html">WebConsole</a></li> <li class="toctree-l1"><a class="reference internal" href="docker.html">Docker</a></li> <li class="toctree-l1"><a class="reference internal" href="spring-boot-starter.html">Arthas Spring Boot Starter</a></li> <li class="toctree-l1"><a class="reference external" href="https://github.com/alibaba/arthas/issues?q=label%3Auser-case">用户案例</a></li> <li class="toctree-l1"><a class="reference external" href="https://github.com/alibaba/arthas/issues?q=label%3Aquestion-answered">常见问题</a></li> <li class="toctree-l1"><a class="reference external" href="https://github.com/alibaba/arthas">Fork me at GitHub</a></li> <li class="toctree-l1"><a class="reference external" href="https://github.com/alibaba/arthas/blob/master/CONTRIBUTING.md#">编译调试/参与贡献</a></li> <li class="toctree-l1"><a class="reference external" href="https://github.com/alibaba/arthas/releases">Release Notes</a></li> <li class="toctree-l1"><a class="reference internal" href="contact-us.html">QQ群/钉钉群</a></li> <li class="toctree-l1"><a class="reference external" href="https://arthas.gitee.io/">国内镜像站</a></li> </ul> </div> </div> </nav> <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> <nav class="wy-nav-top" aria-label="top navigation"> <i data-toggle="wy-nav-top" class="fa fa-bars"></i> <a href="index.html">Arthas</a> </nav> <div class="wy-nav-content"> <div class="rst-content"> <div role="navigation" aria-label="breadcrumbs navigation"> <ul class="wy-breadcrumbs"> <li><a href="index.html">Docs</a> »</li> <li><a href="commands.html">命令列表</a> »</li> <li>tt</li> <li class="wy-breadcrumbs-aside"> <a href="https://github.com/alibaba/arthas/blob/master/site/src/site/sphinx/tt.md" class="fa fa-github"> Edit on GitHub</a> </li> </ul> <hr/> </div> <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> <div itemprop="articleBody"> <div class="section" id="tt"> <h1>tt<a class="headerlink" href="#tt" title="永久链接至标题">¶</a></h1> <blockquote> <div><p>方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测</p> </div></blockquote> <p><code class="docutils literal notranslate"><span class="pre">watch</span></code> 虽然很方便和灵活,但需要提前想清楚观察表达式的拼写,这对排查问题而言要求太高,因为很多时候我们并不清楚问题出自于何方,只能靠蛛丝马迹进行猜测。</p> <p>这个时候如果能记录下当时方法调用的所有入参和返回值、抛出的异常会对整个问题的思考与判断非常有帮助。</p> <p>于是乎,TimeTunnel 命令就诞生了。</p> <div class="section" id="id1"> <h2>使用参考<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2> <div class="section" id="demo"> <h3>启动 Demo<a class="headerlink" href="#demo" title="永久链接至标题">¶</a></h3> <p>启动<a class="reference internal" href="quick-start.html"><span class="doc">快速入门</span></a>里的<code class="docutils literal notranslate"><span class="pre">arthas-demo</span></code>。</p> </div> <div class="section" id="id2"> <h3>记录调用<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h3> <p>对于一个最基本的使用来说,就是记录下当前方法的每次调用环境现场。</p> <div class="highlight-bash notranslate"><div class="highlight hljs"><pre class="bash">$ tt -t demo.MathGame primeFactors Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 66 ms. INDEX TIMESTAMP COST(ms) IS-RET IS-EXP OBJECT CLASS METHOD ------------------------------------------------------------------------------------------------------------------------------------- 1000 2018-12-04 11:15:38 1.096236 false true 0x4b67cf4d MathGame primeFactors 1001 2018-12-04 11:15:39 0.191848 false true 0x4b67cf4d MathGame primeFactors 1002 2018-12-04 11:15:40 0.069523 false true 0x4b67cf4d MathGame primeFactors 1003 2018-12-04 11:15:41 0.186073 false true 0x4b67cf4d MathGame primeFactors 1004 2018-12-04 11:15:42 17.76437 true false 0x4b67cf4d MathGame primeFactors</pre></div> </div> <ul> <li><p>命令参数解析</p> <ul> <li><p><code class="docutils literal notranslate"><span class="pre">-t</span></code></p> <p>tt 命令有很多个主参数,<code class="docutils literal notranslate"><span class="pre">-t</span></code> 就是其中之一。这个参数的表明希望记录下类 <code class="docutils literal notranslate"><span class="pre">*Test</span></code> 的 <code class="docutils literal notranslate"><span class="pre">print</span></code> 方法的每次执行情况。</p> </li> <li><p><code class="docutils literal notranslate"><span class="pre">-n</span> <span class="pre">3</span></code></p> <p>当你执行一个调用量不高的方法时可能你还能有足够的时间用 <code class="docutils literal notranslate"><span class="pre">CTRL+C</span></code> 中断 tt 命令记录的过程,但如果遇到调用量非常大的方法,瞬间就能将你的 JVM 内存撑爆。</p> <p>此时你可以通过 <code class="docutils literal notranslate"><span class="pre">-n</span></code> 参数指定你需要记录的次数,当达到记录次数时 Arthas 会主动中断tt命令的记录过程,避免人工操作无法停止的情况。</p> </li> </ul> </li> <li><p>表格字段说明</p></li> </ul> <table border="1" class="docutils"> <thead> <tr> <th>表格字段</th> <th>字段解释</th> </tr> </thead> <tbody> <tr> <td>INDEX</td> <td>时间片段记录编号,每一个编号代表着一次调用,后续tt还有很多命令都是基于此编号指定记录操作,非常重要。</td> </tr> <tr> <td>TIMESTAMP</td> <td>方法执行的本机时间,记录了这个时间片段所发生的本机时间</td> </tr> <tr> <td>COST(ms)</td> <td>方法执行的耗时</td> </tr> <tr> <td>IS-RET</td> <td>方法是否以正常返回的形式结束</td> </tr> <tr> <td>IS-EXP</td> <td>方法是否以抛异常的形式结束</td> </tr> <tr> <td>OBJECT</td> <td>执行对象的<code>hashCode()</code>,注意,曾经有人误认为是对象在JVM中的内存地址,但很遗憾他不是。但他能帮助你简单的标记当前执行方法的类实体</td> </tr> <tr> <td>CLASS</td> <td>执行的类名</td> </tr> <tr> <td>METHOD</td> <td>执行的方法名</td> </tr> </tbody> </table><ul> <li><p>条件表达式</p> <p>不知道大家是否有在使用过程中遇到以下困惑</p> <ul class="simple"> <li><p>Arthas 似乎很难区分出重载的方法</p></li> <li><p>我只需要观察特定参数,但是 tt 却全部都给我记录了下来</p></li> </ul> <p>条件表达式也是用 <code class="docutils literal notranslate"><span class="pre">OGNL</span></code> 来编写,核心的判断对象依然是 <code class="docutils literal notranslate"><span class="pre">Advice</span></code> 对象。除了 <code class="docutils literal notranslate"><span class="pre">tt</span></code> 命令之外,<code class="docutils literal notranslate"><span class="pre">watch</span></code>、<code class="docutils literal notranslate"><span class="pre">trace</span></code>、<code class="docutils literal notranslate"><span class="pre">stack</span></code> 命令也都支持条件表达式。</p> </li> <li><p>解决方法重载</p> <p><code class="docutils literal notranslate"><span class="pre">tt</span> <span class="pre">-t</span> <span class="pre">*Test</span> <span class="pre">print</span> <span class="pre">params.length==1</span></code></p> <p>通过制定参数个数的形式解决不同的方法签名,如果参数个数一样,你还可以这样写</p> <p><code class="docutils literal notranslate"><span class="pre">tt</span> <span class="pre">-t</span> <span class="pre">*Test</span> <span class="pre">print</span> <span class="pre">'params[1]</span> <span class="pre">instanceof</span> <span class="pre">Integer'</span></code></p> </li> <li><p>解决指定参数</p> <p><code class="docutils literal notranslate"><span class="pre">tt</span> <span class="pre">-t</span> <span class="pre">*Test</span> <span class="pre">print</span> <span class="pre">params[0].mobile=="13989838402"</span></code></p> </li> <li><p>构成条件表达式的 <code class="docutils literal notranslate"><span class="pre">Advice</span></code> 对象</p> <p>前边看到了很多条件表达式中,都使用了 <code class="docutils literal notranslate"><span class="pre">params[0]</span></code>,有关这个变量的介绍,请参考<a class="reference internal" href="advice-class.html"><span class="doc">表达式核心变量</span></a></p> </li> </ul> </div> <div class="section" id="id3"> <h3>检索调用记录<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h3> <p>当你用 <code class="docutils literal notranslate"><span class="pre">tt</span></code> 记录了一大片的时间片段之后,你希望能从中筛选出自己需要的时间片段,这个时候你就需要对现有记录进行检索。</p> <p>假设我们有这些记录</p> <div class="highlight-bash notranslate"><div class="highlight hljs"><pre class="bash">$ tt -l INDEX TIMESTAMP COST(ms) IS-RET IS-EXP OBJECT CLASS METHOD ------------------------------------------------------------------------------------------------------------------------------------- 1000 2018-12-04 11:15:38 1.096236 false true 0x4b67cf4d MathGame primeFactors 1001 2018-12-04 11:15:39 0.191848 false true 0x4b67cf4d MathGame primeFactors 1002 2018-12-04 11:15:40 0.069523 false true 0x4b67cf4d MathGame primeFactors 1003 2018-12-04 11:15:41 0.186073 false true 0x4b67cf4d MathGame primeFactors 1004 2018-12-04 11:15:42 17.76437 true false 0x4b67cf4d MathGame primeFactors 9 1005 2018-12-04 11:15:43 0.4776 false true 0x4b67cf4d MathGame primeFactors Affect(row-cnt:6) cost in 4 ms.</pre></div> </div> <p>我需要筛选出 <code class="docutils literal notranslate"><span class="pre">primeFactors</span></code> 方法的调用信息</p> <div class="highlight-bash notranslate"><div class="highlight hljs"><pre class="bash">$ tt -s 'method.name=="primeFactors"' INDEX TIMESTAMP COST(ms) IS-RET IS-EXP OBJECT CLASS METHOD ------------------------------------------------------------------------------------------------------------------------------------- 1000 2018-12-04 11:15:38 1.096236 false true 0x4b67cf4d MathGame primeFactors 1001 2018-12-04 11:15:39 0.191848 false true 0x4b67cf4d MathGame primeFactors 1002 2018-12-04 11:15:40 0.069523 false true 0x4b67cf4d MathGame primeFactors 1003 2018-12-04 11:15:41 0.186073 false true 0x4b67cf4d MathGame primeFactors 1004 2018-12-04 11:15:42 17.76437 true false 0x4b67cf4d MathGame primeFactors 9 1005 2018-12-04 11:15:43 0.4776 false true 0x4b67cf4d MathGame primeFactors Affect(row-cnt:6) cost in 607 ms.</pre></div> </div> <p>你需要一个 <code class="docutils literal notranslate"><span class="pre">-s</span></code> 参数。<span style="color:red;">同样的,搜索表达式的核心对象依旧是 <code class="docutils literal notranslate"><span class="pre">Advice</span></code> 对象。</span></p> </div> <div class="section" id="id4"> <h3>查看调用信息<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h3> <p>对于具体一个时间片的信息而言,你可以通过 <code class="docutils literal notranslate"><span class="pre">-i</span></code> 参数后边跟着对应的 <code class="docutils literal notranslate"><span class="pre">INDEX</span></code> 编号查看到他的详细信息。</p> <div class="highlight-bash notranslate"><div class="highlight hljs"><pre class="bash">$ tt -i 1003 INDEX 1003 GMT-CREATE 2018-12-04 11:15:41 COST(ms) 0.186073 OBJECT 0x4b67cf4d CLASS demo.MathGame METHOD primeFactors IS-RETURN false IS-EXCEPTION true PARAMETERS[0] @Integer[-564322413] THROW-EXCEPTION java.lang.IllegalArgumentException: number is: -564322413, need >= 2 at demo.MathGame.primeFactors(MathGame.java:46) at demo.MathGame.run(MathGame.java:24) at demo.MathGame.main(MathGame.java:16) Affect(row-cnt:1) cost in 11 ms.</pre></div> </div> </div> <div class="section" id="id5"> <h3>重做一次调用<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h3> <p>当你稍稍做了一些调整之后,你可能需要前端系统重新触发一次你的调用,此时得求爷爷告奶奶的需要前端配合联调的同学再次发起一次调用。而有些场景下,这个调用不是这么好触发的。</p> <p><code class="docutils literal notranslate"><span class="pre">tt</span></code> 命令由于保存了当时调用的所有现场信息,所以我们可以自己主动对一个 <code class="docutils literal notranslate"><span class="pre">INDEX</span></code> 编号的时间片自主发起一次调用,从而解放你的沟通成本。此时你需要 <code class="docutils literal notranslate"><span class="pre">-p</span></code> 参数。通过 <code class="docutils literal notranslate"><span class="pre">--replay-times</span></code> 指定 调用次数,通过 <code class="docutils literal notranslate"><span class="pre">--replay-interval</span></code> 指定多次调用间隔(单位ms, 默认1000ms)</p> <div class="highlight-bash notranslate"><div class="highlight hljs"><pre class="bash">$ tt -i 1004 -p RE-INDEX 1004 GMT-REPLAY 2018-12-04 11:26:00 OBJECT 0x4b67cf4d CLASS demo.MathGame METHOD primeFactors PARAMETERS[0] @Integer[946738738] IS-RETURN true IS-EXCEPTION false COST(ms) 0.186073 RETURN-OBJ @ArrayList[ @Integer[2], @Integer[11], @Integer[17], @Integer[2531387], ] Time fragment[1004] successfully replayed. Affect(row-cnt:1) cost in 14 ms.</pre></div> </div> <p>你会发现结果虽然一样,但调用的路径发生了变化,有原来的程序发起变成了 Arthas 自己的内部线程发起的调用了。</p> <ul> <li><p>需要强调的点</p> <ol> <li><p><strong>ThreadLocal 信息丢失</strong></p> <p>很多框架偷偷的将一些环境变量信息塞到了发起调用线程的 ThreadLocal 中,由于调用线程发生了变化,这些 ThreadLocal 线程信息无法通过 Arthas 保存,所以这些信息将会丢失。</p> <p>一些常见的 CASE 比如:鹰眼的 TraceId 等。</p> </li> <li><p><strong>引用的对象</strong></p> <p>需要强调的是,<code class="docutils literal notranslate"><span class="pre">tt</span></code> 命令是将当前环境的对象引用保存起来,但仅仅也只能保存一个引用而已。如果方法内部对入参进行了变更,或者返回的对象经过了后续的处理,那么在 <code class="docutils literal notranslate"><span class="pre">tt</span></code> 查看的时候将无法看到当时最准确的值。这也是为什么 <code class="docutils literal notranslate"><span class="pre">watch</span></code> 命令存在的意义。</p> </li> </ol> </li> </ul> </div> </div> </div> </div> </div> <footer> <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation"> <a href="profiler.html" class="btn btn-neutral float-right" title="profiler" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a> <a href="stack.html" class="btn btn-neutral float-left" title="stack" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a> </div> <hr/> <div role="contentinfo"> <p> © Copyright 2018-2020, Alibaba Middleware Group, and contributors </p> </div> Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. </footer> </div> </div> </section> </div> <script type="text/javascript"> jQuery(function () { SphinxRtdTheme.Navigation.enable(true); }); </script> <div class="github-fork-ribbon-wrapper right"> <div class="github-fork-ribbon"> <a href="https://github.com/alibaba/arthas" target="_blank">Fork me at GitHub</a> </div> </div> <script type="text/javascript" src="_static/add_badges.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.5.0/highlightjs-line-numbers.min.js"></script> <script> document.querySelectorAll('div.hljs > pre').forEach(function(block) { hljs.highlightBlock(block); hljs.lineNumbersBlock(block); }); </script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); if (window.location.hostname == "arthas.gitee.io") { hm.src = "https://hm.baidu.com/hm.js?ecc1b0d9aeb73da1671876133211d4d3"; } else { // https://alibaba.github.io/arthas/ hm.src = "https://hm.baidu.com/hm.js?d5c5e25b100f0eb51a4c35c8a86ea9b4"; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>