You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
arthas/en/groovy.html

429 lines
14 KiB
HTML

6 years ago
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5 years ago
<title>groovy &mdash; Arthas 3.1.5 documentation</title>
6 years ago
<link rel="shortcut icon" href="_static/favicon.ico"/>
5 years ago
<script type="text/javascript" src="_static/js/modernizr.min.js"></script>
6 years ago
5 years ago
<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/js/theme.js"></script>
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
<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="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<script src="_static/center_page.js"></script>
6 years ago
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/github.min.css">
6 years ago
</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">
5 years ago
<div class="wy-side-nav-search" >
6 years ago
<a href="index.html" class="icon icon-home"> Arthas
</a>
<div class="version">
5 years ago
3.1.5
6 years ago
</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>
6 years ago
<li class="toctree-l1"><a class="reference external" href="https://alibaba.github.io/arthas/arthas-tutorials?language=en">Online tutorials(Recommend)</a></li>
6 years ago
<li class="toctree-l1"><a class="reference internal" href="install-detail.html">Installation</a></li>
5 years ago
<li class="toctree-l1"><a class="reference internal" href="download.html">Download</a></li>
6 years ago
<li class="toctree-l1"><a class="reference internal" href="quick-start.html">Quick start</a></li>
<li class="toctree-l1"><a class="reference internal" href="advanced-use.html">Advanced usage</a></li>
<li class="toctree-l1"><a class="reference internal" href="commands.html">Commands</a></li>
5 years ago
<li class="toctree-l1"><a class="reference internal" href="web-console.html">WebConsole</a></li>
6 years ago
<li class="toctree-l1"><a class="reference internal" href="docker.html">Docker</a></li>
6 years ago
<li class="toctree-l1"><a class="reference external" href="https://github.com/alibaba/arthas/issues?q=label%3Auser-case">User cases</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/alibaba/arthas/issues?q=label%3Aquestion-answered">Questions and answers</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/alibaba/arthas">Fork me at GitHub</a></li>
5 years ago
<li class="toctree-l1"><a class="reference external" href="https://github.com/alibaba/arthas/blob/master/CONTRIBUTING.md#">Compile and debug/CONTRIBUTING</a></li>
5 years ago
<li class="toctree-l1"><a class="reference external" href="https://github.com/alibaba/arthas/releases">Release Notes</a></li>
6 years ago
</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> &raquo;</li>
<li>groovy</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/alibaba/arthas/blob/master/site/src/site/sphinx/en/groovy.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="groovy">
5 years ago
<h1>groovy<a class="headerlink" href="#groovy" title="Permalink to this headline"></a></h1>
6 years ago
<blockquote>
5 years ago
<div><p>Arthas support groovy scripting to allow user to use script like BTrace. It is possible to use if/for/switch/while in groovy scripting, but has more limitations compared to BTrace.</p>
</div></blockquote>
6 years ago
<div class="section" id="limitations">
5 years ago
<h2>Limitations<a class="headerlink" href="#limitations" title="Permalink to this headline"></a></h2>
6 years ago
<ol class="simple">
5 years ago
<li><p>Prohibit from alternating the original logic. Like <code class="docutils literal notranslate"><span class="pre">watch</span></code> command, The major purpose of scripting is monitoring and observing.</p></li>
<li><p>Only allow to monitor at the stages of before/success/exception/finish on one method.</p></li>
6 years ago
</ol>
</div>
<div class="section" id="parameters">
5 years ago
<h2>Parameters<a class="headerlink" href="#parameters" title="Permalink to this headline"></a></h2>
6 years ago
<table border="1" class="docutils">
<thead>
<tr>
<th align="right">Parameter</th>
<th align="left">Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right"><em>class-pattern</em></td>
<td align="left">class name pattern</td>
</tr>
<tr>
<td align="right"><em>method-pattern</em></td>
<td align="left">method name pattern</td>
</tr>
<tr>
<td align="right"><em>script-filepath</em></td>
<td align="left">the absolute path of the groovy script</td>
</tr>
<tr>
<td align="right">[S]</td>
<td align="left">match all sub classes</td>
</tr>
<tr>
<td align="right">[E]</td>
<td align="left">enable regex match, the default is wildcard match</td>
</tr>
</tbody>
5 years ago
</table><p>Note: the third parameter <code class="docutils literal notranslate"><span class="pre">script-filepath</span></code> must be the absolute path of the groovy script, for example <code class="docutils literal notranslate"><span class="pre">/tmp/test.groovy</span></code>. It is not recommended to use relative path, e.g. <code class="docutils literal notranslate"><span class="pre">./test.groovy</span></code>.</p>
6 years ago
</div>
<div class="section" id="explanation-on-the-important-callbacks">
5 years ago
<h2>Explanation on the important callbacks<a class="headerlink" href="#explanation-on-the-important-callbacks" title="Permalink to this headline"></a></h2>
6 years ago
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">/**
* Listeners for script to enhance the class
*/
interface ScriptListener {
/**
* When the script is created
*
* @param output Output
*/
void create(Output output);
/**
* When the script is destroyed
*
* @param output Output
*/
void destroy(Output output);
/**
* Before the method executes
*
* @param output Output
* @param advice Advice
*/
void before(Output output, Advice advice);
/**
* After the method returns
*
* @param output Output
* @param advice Advice
*/
void afterReturning(Output output, Advice advice);
/**
* After the method throws exceptions
*
* @param output Output
* @param advice Advice
*/
void afterThrowing(Output output, Advice advice);
5 years ago
}</pre></div>
6 years ago
</div>
</div>
<div class="section" id="advice-parameter">
5 years ago
<h2><code class="docutils literal notranslate"><span class="pre">Advice</span></code> parameter<a class="headerlink" href="#advice-parameter" title="Permalink to this headline"></a></h2>
<p><code class="docutils literal notranslate"><span class="pre">Advice</span></code> contains all information necessary for notification. Refer to <a class="reference internal" href="advice-class.html"><span class="doc">expression core parameters</span></a> for more details.</p>
6 years ago
</div>
<div class="section" id="output-parameter">
5 years ago
<h2><code class="docutils literal notranslate"><span class="pre">Output</span></code> parameter<a class="headerlink" href="#output-parameter" title="Permalink to this headline"></a></h2>
<p>There are three methods in <code class="docutils literal notranslate"><span class="pre">Output</span></code>, used for outputting the corresponding text.</p>
6 years ago
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">/**
* Output
*/
interface Output {
/**
* Output text without line break
*
* @param string Text to output
* @return this
*/
Output print(String string);
/**
* Output text with line break
*
* @param string Text to output
* @return this
*/
Output println(String string);
/**
* Finish outputting from the script
*
* @return this
*/
Output finish();
5 years ago
}</pre></div>
6 years ago
</div>
</div>
<div class="section" id="a-groovy-sample-script-to-output-logs">
5 years ago
<h2>A groovy sample script to output logs<a class="headerlink" href="#a-groovy-sample-script-to-output-logs" title="Permalink to this headline"></a></h2>
6 years ago
<div class="highlight-groovy notranslate"><div class="highlight hljs"><pre class="groovy">import com.taobao.arthas.core.command.ScriptSupportCommand
import com.taobao.arthas.core.util.Advice
import static java.lang.String.format
/**
* Output method logs
*/
public class Logger implements ScriptSupportCommand.ScriptListener {
@Override
void create(ScriptSupportCommand.Output output) {
output.println(&quot;script create.&quot;);
}
@Override
void destroy(ScriptSupportCommand.Output output) {
output.println(&quot;script destroy.&quot;);
}
@Override
void before(ScriptSupportCommand.Output output, Advice advice) {
output.println(format(&quot;before:class=%s;method=%s;paramslen=%d;%s;&quot;,
advice.getClazz().getSimpleName(),
advice.getMethod().getName(),
advice.getParams().length, advice.getParams()))
}
@Override
void afterReturning(ScriptSupportCommand.Output output, Advice advice) {
output.println(format(&quot;returning:class=%s;method=%s;&quot;,
advice.getClazz().getSimpleName(),
advice.getMethod().getName()))
}
@Override
void afterThrowing(ScriptSupportCommand.Output output, Advice advice) {
output.println(format(&quot;throwing:class=%s;method=%s;&quot;,
advice.getClazz().getSimpleName(),
advice.getMethod().getName()))
}
5 years ago
}</pre></div>
6 years ago
</div>
<p>Run the script like this:</p>
6 years ago
<div class="highlight-bash notranslate"><div class="highlight hljs"><pre class="bash">$ groovy com.alibaba.sample.petstore.dal.dao.ProductDao getProductById /Users/zhuyong/middleware/arthas/scripts/Logger.groovy -S
6 years ago
script create.
Press Ctrl+C to abort.
6 years ago
Affect(class-cnt:1 , method-cnt:1) cost in 102 ms.
before:class=IbatisProductDao;method=getProductById;paramslen=1;[Ljava.lang.Object;@45df64fc;
returning:class=IbatisProductDao;method=getProductById;
before:class=IbatisProductDao;method=getProductById;paramslen=1;[Ljava.lang.Object;@5b0e2d00;
5 years ago
returning:class=IbatisProductDao;method=getProductById;</pre></div>
6 years ago
</div>
</div>
</div>
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>
5 years ago
&copy; Copyright 2018-2019, Alibaba Middleware Group, and contributors
6 years ago
</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>
5 years ago
6 years ago
<div class="github-fork-ribbon-wrapper right">
<div class="github-fork-ribbon">
6 years ago
<a href="https://github.com/alibaba/arthas" target="_blank">Fork me at GitHub</a>
6 years ago
</div>
</div>
<script type="text/javascript" src="_static/add_badges.js"></script>
6 years ago
<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>
6 years ago
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?d5c5e25b100f0eb51a4c35c8a86ea9b4";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</body>
</html>