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/doc/redefine.html

76 lines
72 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="generator" content="VuePress 2.0.0-beta.49">
<style>
:root {
--c-bg: #fff;
}
html.dark {
--c-bg: #22272e;
}
html, body {
background-color: var(--c-bg);
}
</style>
<script>
const userMode = localStorage.getItem('vuepress-color-scheme');
const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
if (userMode === 'dark' || (userMode !== 'light' && systemDarkMode)) {
document.documentElement.classList.toggle('dark', true);
}
</script>
3 years ago
<link rel="icon" href="/images/favicon.ico"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta property="og:title" content="Arthas"><meta property="og:image:alt" content="Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas - alibaba/arthas: Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas"><meta property="og:image" content="/images/arthas_mate_image.png"><meta property="og:description" content="Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas - alibaba/arthas: Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas"><meta property="og:image:width" content="1200"><meta property="og:image:height" content="600"><meta property="twitter:image:src" content="/images/arthas_mate_image.png"><meta property="twitter:image:alt" content="Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas - alibaba/arthas: Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas"><meta itemprop="name" content="Arthas"><meta itemprop="image" content="/images/arthas_mate_image.png"><meta itemprop="description" content="Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas - alibaba/arthas: Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas"><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);
})();
3 years ago
</script><meta name="aes-config" content="pid=xux-opensource&user_type=101&uid=&username="><script src="//g.alicdn.com/alilog/mlog/aplus_v2.js" id="beacon-aplus" exparams="clog=o&aplus&sidx=aplusSidx&ckx=aplusCkx"></script><script src="//g.alicdn.com/aes/??tracker/1.0.34/index.js,tracker-plugin-pv/2.4.5/index.js,tracker-plugin-event/1.2.5/index.js,tracker-plugin-jserror/1.0.13/index.js,tracker-plugin-api/1.1.14/index.js,tracker-plugin-perf/1.1.8/index.js,tracker-plugin-eventTiming/1.0.4/index.js"></script><title>redefine | arthas</title><meta name="description" content="arthas 使用文档">
2 years ago
<link rel="modulepreload" href="/assets/app.e9adadd6.js"><link rel="modulepreload" href="/assets/redefine.html.83f2dd47.js"><link rel="modulepreload" href="/assets/redefine.html.7a72fe7b.js"><link rel="prefetch" href="/assets/index.html.fc6fe773.js"><link rel="prefetch" href="/assets/index.html.303ae9ec.js"><link rel="prefetch" href="/assets/index.html.5be10814.js"><link rel="prefetch" href="/assets/advanced-use.html.80c572ab.js"><link rel="prefetch" href="/assets/advice-class.html.30be8b5c.js"><link rel="prefetch" href="/assets/agent.html.478e63bb.js"><link rel="prefetch" href="/assets/arthas-properties.html.126a2442.js"><link rel="prefetch" href="/assets/arthas3.html.f866a915.js"><link rel="prefetch" href="/assets/async.html.7fc6690f.js"><link rel="prefetch" href="/assets/auth.html.7faa0702.js"><link rel="prefetch" href="/assets/base64.html.91916ad8.js"><link rel="prefetch" href="/assets/batch-support.html.6d8369c7.js"><link rel="prefetch" href="/assets/cat.html.49018237.js"><link rel="prefetch" href="/assets/classloader.html.e5597a48.js"><link rel="prefetch" href="/assets/cls.html.3d057407.js"><link rel="prefetch" href="/assets/commands.html.58e9e974.js"><link rel="prefetch" href="/assets/contact-us.html.e0d74259.js"><link rel="prefetch" href="/assets/dashboard.html.688b447f.js"><link rel="prefetch" href="/assets/docker.html.17503d14.js"><link rel="prefetch" href="/assets/download.html.19809e39.js"><link rel="prefetch" href="/assets/dump.html.35a47ccf.js"><link rel="prefetch" href="/assets/echo.html.81510109.js"><link rel="prefetch" href="/assets/faq.html.e22d74d2.js"><link rel="prefetch" href="/assets/getstatic.html.9ffb9ff2.js"><link rel="prefetch" href="/assets/grep.html.7747b85b.js"><link rel="prefetch" href="/assets/groovy.html.3c2a5669.js"><link rel="prefetch" href="/assets/heapdump.html.98d7b14d.js"><link rel="prefetch" href="/assets/help.html.67db59f7.js"><link rel="prefetch" href="/assets/history.html.f6e93473.js"><link rel="prefetch" href="/assets/http-api.html.8abd260a.js"><link rel="prefetch" href="/assets/idea-plugin.html.68b7d4f4.js"><link rel="prefetch" href="/assets/install-detail.html.666bc4d2.js"><link rel="prefetch" href="/assets/jad.html.a62cf748.js"><link rel="prefetch" href="/assets/jfr.html.fb32b04e.js"><link rel="prefetch" href="/assets/jvm.html.970a2dcd.js"><link rel="prefetch" href="/assets/keymap.html.79b77d01.js"><link rel="prefetch" href="/assets/logger.html.1c7e6fb8.js"><link rel="prefetch" href="/assets/manual-install.html.5992ea2f.js"><link rel="prefetch" href="/assets/mbean.html.07f7548d.js"><link rel="prefetch" href="/assets/mc.html.4a8fe9c1.js"><link rel="prefetch" href="/assets/memory.html.f3395aa5.js"><link rel="prefetch" href="/assets/monitor.html.f16f1733.js"><link rel="prefetch" href="/assets/ognl.html.e96e8461.js"><link rel="prefetch" href="/assets/options.html.f364e3b1.js"><link rel="prefetch" href="/assets/perfcounter.html.5ac9dd17.js"><link rel="prefetch" href="/assets/profiler.html.8b48b000.js"><link rel="prefetch" href="/assets/pwd.html.0881d938.js"><link rel="prefetch" href="/assets/quick-start.html.d22745bc.js"><link rel="prefetch" href="/assets/quit.html.7ac530c1.js"><link rel="prefetch" href="/assets/release-notes.html.dd1cd195.js"><link rel="prefetch" href="/assets/reset.html.452a35fa.js"><link rel="prefetch" href="/assets/retransform.html.fbef0997.js"><link rel="prefetch" href="/assets/save-log.html.c6a45ce2.js"><link rel="prefetch" href="/assets/sc.html.6441d65e.js"><link rel="prefetch" href="/assets/session.html.ebfd8261.js"><link rel="prefetch" href="/assets/sm.html.9e10db61.js"><link rel="prefetch" href="/assets/spring-boot-starter.html.c4118b0a.js"><link rel="prefetch" href="/assets/stack.html.9add4cf4.js"><link rel="prefetch" href="/assets/start-arthas.html.bb492515.js"><link rel="prefetch" href="/assets/stop.html.b66bc06b.js"><link rel="prefetch" href="/assets/sysenv.html.26a8e556.js"><link rel="prefetch" href="/assets/sysprop.html.b7e9f6f3.js"><link rel="prefetch" href="/assets/tee.html.ad9aa342.js"><link rel="prefetch" href="/assets/thread.html.e5d9019c
3 years ago
<link rel="stylesheet" href="/assets/style.3627c3cf.css">
</head>
<body>
2 years ago
<div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar"><div class="toggle-sidebar-button" title="切换侧边栏" aria-expanded="false" role="button" tabindex="0"><div class="icon" aria-hidden="true"><span></span><span></span><span></span></div></div><span><a href="/" class="" data-v-2aa8cc94><!----><span class="site-name can-hide" data-v-2aa8cc94>arthas</span><span class="navbar-version" data-v-2aa8cc94>v3.6.5</span></a></span><div class="navbar-items-wrapper" style=""><!--[--><!--]--><nav class="navbar-items can-hide"><!--[--><div class="navbar-item"><a href="/" class="" aria-label="首页"><!--[--><!--]--> 首页 <!--[--><!--]--></a></div><div class="navbar-item"><a class="external-link" href="/doc/arthas-tutorials.html?language=cn&amp;id=arthas-basics" rel="noopener noreferrer" target="_blank" aria-label="在线教程"><!--[--><!--]--><span>在线教程</span><span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">在新窗口打开</span></span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/doc/" class="router-link-active" aria-label="文档"><!--[--><!--]--> 文档 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/doc/commands.md" class="" aria-label="命令列表"><!--[--><!--]--> 命令列表 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/doc/download.md" class="" aria-label="下载"><!--[--><!--]--> 下载 <!--[--><!--]--></a></div><div class="navbar-item"><div class="navbar-dropdown-wrapper"><button class="navbar-dropdown-title" type="button" aria-label="解决方案"><span class="title">解决方案</span><span class="arrow down"></span></button><button class="navbar-dropdown-title-mobile" type="button" aria-label="解决方案"><span class="title">解决方案</span><span class="right arrow"></span></button><ul style="display:none;" class="navbar-dropdown"><!--[--><li class="navbar-dropdown-item"><a class="external-link" href="https://cn.aliyun.com/product/aliware/mse?spm=arthas.topbar.0.0.0" rel="noopener noreferrer" target="_blank" aria-label="微服务解决方案"><!--[--><!--]--><span>微服务解决方案</span><span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">在新窗口打开</span></span><!--[--><!--]--></a></li><li class="navbar-dropdown-item"><a class="external-link" href="https://www.aliyun.com/aliware/txc?spm=arthas.topbar.0.0.0" rel="noopener noreferrer" target="_blank" aria-label="分布式事务解决方案"><!--[--><!--]--><span>分布式事务解决方案</span><span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">在新窗口打开</span></span><!--[--><!--]--></a></li><li class="navbar-dropdown-item"><a class="external-link" href="https://www.aliyun.com/product/ahas?spm=artha
redefine -c 327a647b /tmp/Test.class /tmp/Test<span class="token punctuation">\</span><span class="token variable">$Inner</span>.class
redefine --classLoaderClass sun.misc.Launcher<span class="token variable">$AppClassLoader</span> /tmp/Test.class /tmp/Test<span class="token punctuation">\</span><span class="token variable">$Inner</span>.class
3 years ago
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="结合-jad-mc-命令使用" tabindex="-1"><a class="header-anchor" href="#结合-jad-mc-命令使用" aria-hidden="true">#</a> 结合 jad/mc 命令使用</h2><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>jad --source-only com.example.demo.arthas.user.UserController <span class="token operator">&gt;</span> /tmp/UserController.java
<span class="token function">mc</span> /tmp/UserController.java -d /tmp
redefine /tmp/com/example/demo/arthas/user/UserController.class
3 years ago
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><ul><li>jad 命令反编译,然后可以用其它编译器,比如 vim 来修改源码</li><li>mc 命令来内存编译修改过的代码</li><li>用 redefine 命令加载新的字节码</li></ul><h2 id="上传-class-文件到服务器的技巧" tabindex="-1"><a class="header-anchor" href="#上传-class-文件到服务器的技巧" aria-hidden="true">#</a> 上传 .class 文件到服务器的技巧</h2><p>使用<code>mc</code>命令来编译<code>jad</code>的反编译的代码有可能失败。可以在本地修改代码,编译好后再上传到服务器上。有的服务器不允许直接上传文件,可以使用<code>base64</code>命令来绕过。</p><ol><li><p>在本地先转换<code>.class</code>文件为 base64再保存为 result.txt</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>base64 <span class="token operator">&lt;</span> Test.class <span class="token operator">&gt;</span> result.txt
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div></li><li><p>到服务器上,新建并编辑<code>result.txt</code>,复制本地的内容,粘贴再保存</p></li><li><p>把服务器上的 <code>result.txt</code>还原为<code>.class</code></p><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>base64 -d &lt; result.txt &gt; Test.class
3 years ago
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div></li><li><p>用 md5 命令计算哈希值,校验是否一致</p></li></ol><h2 id="redefine-的限制" tabindex="-1"><a class="header-anchor" href="#redefine-的限制" aria-hidden="true">#</a> redefine 的限制</h2><ul><li>不允许新增加 field/method</li><li>正在跑的函数,没有退出不能生效,比如下面新增加的<code>System.out.println</code>,只有<code>run()</code>函数里的会生效</li></ul><div class="language-java ext-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MathGame</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>
<span class="token class-name">MathGame</span> game <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MathGame</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
game<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">TimeUnit</span><span class="token punctuation">.</span>SECONDS<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 这个不生效,因为代码一直跑在 while里</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;in loop&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>
<span class="token comment">// 这个生效因为run()函数每次都可以完整结束</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;call run()&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
<span class="token keyword">int</span> number <span class="token operator">=</span> random<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> primeFactors <span class="token operator">=</span> <span class="token function">primeFactors</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">print</span><span class="token punctuation">(</span>number<span class="token punctuation">,</span> primeFactors<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">&quot;illegalArgumentCount:%3d, &quot;</span><span class="token punctuation">,</span> illegalArgumentCount<span class="token punctuation">)</span> <span class="token operator">+</span> e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
3 years ago
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div><!--[--><!--]--></div><footer class="page-meta right-menu-padding" data-v-8e2a76de><div class="meta-item edit-link"><a class="external-link meta-item-label" href="https://github.com/alibaba/arthas/edit/master/site/docs/doc/redefine.md" rel="noopener noreferrer" target="_blank" aria-label="在 GitHub 上编辑此页"><!--[--><!--]--><span>在 GitHub 上编辑此页</span><span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">在新窗口打开</span></span><!--[--><!--]--></a></div><div class="meta-item last-updated"><span class="meta-item-label">Last Updated: </span><!----></div><div class="meta-item contributors"><span class="meta-item-label">贡献者: </span><span class="meta-item-info"><!--[--><!--[--><span class="contributor" title="email: hengyunabc@gmail.com">hengyunabc</span><!--[-->, <!--]--><!--]--><!--[--><span class="contributor" title="email: i@fatpandac.com">Fatpandac</span><!--[-->, <!--]--><!--]--><!--[--><span class="contributor" title="email: hollowman186@vip.qq.com">Hollow Man</span><!--[-->, <!--]--><!--]--><!--[--><span class="contributor" title="email: hollowman@hollowman.ml">Hollow Man</span><!----><!--]--><!--]--></span></div></footer><nav class="page-nav right-menu-padding" data-v-8e2a76de><p class="inner"><span class="prev"><a href="/doc/mc.html" class="" aria-label="mc"><!--[--><!--]--> mc <!--[--><!--]--></a></span><span class="next"><a href="/doc/retransform.html" class="" aria-label="retransform"><!--[--><!--]--> retransform <!--[--><!--]--></a></span></p></nav><!--[--><!--]--></main><!--]--></div><!----><!--]--></div>
2 years ago
<script type="module" src="/assets/app.e9adadd6.js" defer></script>
</body>
</html>