jdb.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="generator" content="Asciidoctor 2.0.15">
  8. <meta name="author" content="pxzxj, pudge.zxj@gmail.com">
  9. <title>JDB(Java Debugger)</title>
  10. <link rel="stylesheet" href="css/site.css">
  11. <link href="css/custom.css" rel="stylesheet">
  12. <script src="js/setup.js"></script><script defer src="js/site.js"></script>
  13. </head>
  14. <body class="article toc2 toc-left"><div id="banner-container" class="container" role="banner">
  15. <div id="banner" class="contained" role="banner">
  16. <div id="switch-theme">
  17. <input type="checkbox" id="switch-theme-checkbox" />
  18. <label for="switch-theme-checkbox">Dark Theme</label>
  19. </div>
  20. </div>
  21. </div>
  22. <div id="tocbar-container" class="container" role="navigation">
  23. <div id="tocbar" class="contained" role="navigation">
  24. <button id="toggle-toc"></button>
  25. </div>
  26. </div>
  27. <div id="main-container" class="container">
  28. <div id="main" class="contained">
  29. <div id="doc" class="doc">
  30. <div id="header">
  31. <h1>JDB(Java Debugger)</h1>
  32. <div class="details">
  33. <span id="author" class="author">pxzxj</span><br>
  34. <span id="author2" class="author">pudge.zxj@gmail.com</span><br>
  35. <span id="revnumber">version v1.0,</span>
  36. <span id="revdate">2022/05/23</span>
  37. </div>
  38. <div id="toc" class="toc2">
  39. <div id="toctitle">Table of Contents</div>
  40. <span id="back-to-index"><a href="index.html">Back to index</a></span><ul class="sectlevel1">
  41. <li><a href="#_简介">1. 简介</a></li>
  42. <li><a href="#_用法">2. 用法</a></li>
  43. <li><a href="#option">3. 选项</a></li>
  44. <li><a href="#example">4. 示例</a>
  45. <ul class="sectlevel2">
  46. <li><a href="#_创建项目">4.1. 创建项目</a></li>
  47. <li><a href="#_启动项目">4.2. 启动项目</a></li>
  48. <li><a href="#_添加断点">4.3. 添加断点</a></li>
  49. <li><a href="#_断点调试">4.4. 断点调试</a>
  50. <ul class="sectlevel3">
  51. <li><a href="#_step">4.4.1. step</a></li>
  52. <li><a href="#_next">4.4.2. next</a></li>
  53. <li><a href="#_step_up">4.4.3. step up</a></li>
  54. <li><a href="#_stepi">4.4.4. stepi</a></li>
  55. <li><a href="#_查看变量和源码">4.4.5. 查看变量和源码</a></li>
  56. </ul>
  57. </li>
  58. <li><a href="#_清除断点">4.5. 清除断点</a></li>
  59. </ul>
  60. </li>
  61. </ul>
  62. </div>
  63. </div>
  64. <div id="content">
  65. <div id="preamble">
  66. <div class="sectionbody">
  67. <div class="paragraph">
  68. <p>调试器是软件开发领域最重要的工具之一,对Java开发来说,在开发环境可以使用Intellij IDEA进行debug从而快速解决问题,但在部署环境由于无法debug
  69. 经常需要耗时费力地添加打印语句并替换部署包。实际上JDK已经提供了一个命令行debug工具jdb,可以在任何环境使用它实现快速有效地问题定位。</p>
  70. </div>
  71. </div>
  72. </div>
  73. <div class="sect1">
  74. <h2 id="_简介"><a class="anchor" href="#_简介"></a>1. 简介</h2>
  75. <div class="sectionbody">
  76. <div class="paragraph">
  77. <p>The Java Debugger (<a href="https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jdb.html">JDB</a>) 是一个简单的命令行调试工具,
  78. 使用它可以像Intellij IDEA一样进行添加断点、单步执行、变量查看等操作,实际上jdb和Intellij IDEA底层都利用了
  79. Java Platform Debugger Architecture (<a href="http://docs.oracle.com/javase/8/docs/technotes/guides/jpda/index.html">JDBA</a>)
  80. 实现调试功能,只是展现方式不同而已。</p>
  81. </div>
  82. </div>
  83. </div>
  84. <div class="sect1">
  85. <h2 id="_用法"><a class="anchor" href="#_用法"></a>2. 用法</h2>
  86. <div class="sectionbody">
  87. <div class="paragraph">
  88. <p>jdk的bin目录下包含了jdb,jdb命令的基础语法是 <code><strong>jdb</strong> [options] [classname] [arguments]</code>,其中 <code>options</code> 是命令的选项,<code>classname</code> 和 <code>arguments</code> 是要调试的main类名和main方法参数。</p>
  89. </div>
  90. <div class="paragraph">
  91. <p>可以注意到jdb命令的 <code>classname</code> 和 <code>arguments</code> 都使用了中括号,表示它们都不是必须的,因为jdb还可以attache到一个已经在运行的JVM上,方法与普通的 <code>Remote Debug</code> 相同,调试Spring Boot应用时使用这种方法更方便。</p>
  92. </div>
  93. <div class="listingblock">
  94. <div class="content">
  95. <pre class="highlight"><code class="language-bash" data-lang="bash">#启动时添加debug参数
  96. java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar app.jar
  97. #attach到已经启动的JVM上
  98. jdb -attach 5005
  99. #与attach效果相同,attach失败时可以使用此命令
  100. jdb -connect com.sun.jdi.SocketAttach:hostname=127.0.0.1,port=5005</code></pre>
  101. </div>
  102. </div>
  103. <div class="paragraph">
  104. <p>jdb执行成功后会进入到一个交互式shell页面中,后续的调试操作都在此shell中进行,具体调试方法参考<a href="#example">示例</a>章节</p>
  105. </div>
  106. <div class="imageblock">
  107. <div class="content">
  108. <img src="images/jdb-connect.png" alt="jdb connect">
  109. </div>
  110. </div>
  111. </div>
  112. </div>
  113. <div class="sect1">
  114. <h2 id="option"><a class="anchor" href="#option"></a>3. 选项</h2>
  115. <div class="sectionbody">
  116. <div class="paragraph">
  117. <p>使用help可以查看jdb的选项,不同版本JDK的选项有所不同,Windows下jdk8支持如下选项</p>
  118. </div>
  119. <div class="imageblock">
  120. <div class="content">
  121. <img src="images/jdb-options.png" alt="jdb options">
  122. </div>
  123. </div>
  124. <div class="paragraph">
  125. <p>进入调试shell后可以查看debug相关命令,<a href="#example">示例</a>章节会演示主要的添加断点、单步执行等命令</p>
  126. </div>
  127. <div class="imageblock">
  128. <div class="content">
  129. <img src="images/jdb-options2.png" alt="jdb options2">
  130. </div>
  131. </div>
  132. </div>
  133. </div>
  134. <div class="sect1">
  135. <h2 id="example"><a class="anchor" href="#example"></a>4. 示例</h2>
  136. <div class="sectionbody">
  137. <div class="paragraph">
  138. <p>本节演示如下比较常用的调试命令</p>
  139. </div>
  140. <table class="tableblock frame-all grid-all stretch">
  141. <colgroup>
  142. <col style="width: 50%;">
  143. <col style="width: 50%;">
  144. </colgroup>
  145. <thead>
  146. <tr>
  147. <th class="tableblock halign-left valign-top">命令</th>
  148. <th class="tableblock halign-left valign-top">描述</th>
  149. </tr>
  150. </thead>
  151. <tbody>
  152. <tr>
  153. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>stop in &lt;class id&gt;.&lt;method&gt;[(argument_type,&#8230;&#8203;)]</code></p></td>
  154. <td class="tableblock halign-left valign-top"><p class="tableblock">在方法中设置断点</p></td>
  155. </tr>
  156. <tr>
  157. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>stop at &lt;class id&gt;:&lt;line&gt;</code></p></td>
  158. <td class="tableblock halign-left valign-top"><p class="tableblock">在行中设置断点</p></td>
  159. </tr>
  160. <tr>
  161. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>clear</code></p></td>
  162. <td class="tableblock halign-left valign-top"><p class="tableblock">查看所有断点</p></td>
  163. </tr>
  164. <tr>
  165. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>clear &lt;class id&gt;.&lt;method&gt;[(argument_type,&#8230;&#8203;)]</code></p></td>
  166. <td class="tableblock halign-left valign-top"><p class="tableblock">清除方法中的断点</p></td>
  167. </tr>
  168. <tr>
  169. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>clear &lt;class id&gt;:&lt;line&gt;</code></p></td>
  170. <td class="tableblock halign-left valign-top"><p class="tableblock">清除行中的断点</p></td>
  171. </tr>
  172. <tr>
  173. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>step</code></p></td>
  174. <td class="tableblock halign-left valign-top"><p class="tableblock">执行当前行</p></td>
  175. </tr>
  176. <tr>
  177. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>step up</code></p></td>
  178. <td class="tableblock halign-left valign-top"><p class="tableblock">一直执行, 直到当前方法返回到其调用方</p></td>
  179. </tr>
  180. <tr>
  181. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>stepi</code></p></td>
  182. <td class="tableblock halign-left valign-top"><p class="tableblock">执行当前指令</p></td>
  183. </tr>
  184. <tr>
  185. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>next</code></p></td>
  186. <td class="tableblock halign-left valign-top"><p class="tableblock">步进一行 (步过调用)</p></td>
  187. </tr>
  188. <tr>
  189. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>cont</code></p></td>
  190. <td class="tableblock halign-left valign-top"><p class="tableblock">从断点处继续执行</p></td>
  191. </tr>
  192. <tr>
  193. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>locals</code></p></td>
  194. <td class="tableblock halign-left valign-top"><p class="tableblock">输出当前堆栈帧中的所有本地变量</p></td>
  195. </tr>
  196. <tr>
  197. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>print &lt;expr&gt;</code></p></td>
  198. <td class="tableblock halign-left valign-top"><p class="tableblock">输出表达式的值</p></td>
  199. </tr>
  200. <tr>
  201. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>dump &lt;expr&gt;</code></p></td>
  202. <td class="tableblock halign-left valign-top"><p class="tableblock">输出所有对象信息</p></td>
  203. </tr>
  204. <tr>
  205. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>eval &lt;expr&gt;</code></p></td>
  206. <td class="tableblock halign-left valign-top"><p class="tableblock">对表达式求值 (与 print 相同)</p></td>
  207. </tr>
  208. <tr>
  209. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>list [line number|method]</code></p></td>
  210. <td class="tableblock halign-left valign-top"><p class="tableblock">输出源代码</p></td>
  211. </tr>
  212. <tr>
  213. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>methods &lt;class id&gt;</code></p></td>
  214. <td class="tableblock halign-left valign-top"><p class="tableblock">列出类的方法</p></td>
  215. </tr>
  216. <tr>
  217. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>fields &lt;class id&gt;</code></p></td>
  218. <td class="tableblock halign-left valign-top"><p class="tableblock">列出类的字段</p></td>
  219. </tr>
  220. <tr>
  221. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>!!</code></p></td>
  222. <td class="tableblock halign-left valign-top"><p class="tableblock">重复执行最后一个命令</p></td>
  223. </tr>
  224. <tr>
  225. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>&lt;n&gt; &lt;command&gt;</code></p></td>
  226. <td class="tableblock halign-left valign-top"><p class="tableblock">将命令重复执行 n 次</p></td>
  227. </tr>
  228. </tbody>
  229. </table>
  230. <div class="sect2">
  231. <h3 id="_创建项目"><a class="anchor" href="#_创建项目"></a>4.1. 创建项目</h3>
  232. <div class="paragraph">
  233. <p>创建一个Spring Boot项目添加 <code>starter-web</code> 依赖,并添加如下Controller</p>
  234. </div>
  235. <div class="listingblock">
  236. <div class="content">
  237. <pre class="highlight"><code class="language-java" data-lang="java"><span class="fold-block">package io.github.c;
  238. </span><span class="fold-block hide-when-folded">import org.springframework.web.bind.annotation.GetMapping;
  239. import org.springframework.web.bind.annotation.RestController;
  240. </span><span class="fold-block">@RestController
  241. public class A {
  242. private int counter = 0;
  243. @GetMapping("/s")
  244. public String s() {
  245. counter++;
  246. System.out.println("counter = " + counter);
  247. return a();
  248. }
  249. public String a() {
  250. String b = b();
  251. for(int i = 0; i &lt; 3; i++) {
  252. b += i;
  253. }
  254. return b;
  255. }
  256. public String b() {
  257. String s = "b";
  258. s += "d";
  259. return s;
  260. }
  261. }
  262. </span></code></pre>
  263. </div>
  264. </div>
  265. </div>
  266. <div class="sect2">
  267. <h3 id="_启动项目"><a class="anchor" href="#_启动项目"></a>4.2. 启动项目</h3>
  268. <div class="paragraph">
  269. <p>项目打包后使用如下命令启动</p>
  270. </div>
  271. <div class="listingblock">
  272. <div class="content">
  273. <pre class="highlight"><code class="language-bash" data-lang="bash">java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar app.jar</code></pre>
  274. </div>
  275. </div>
  276. <div class="paragraph">
  277. <p>在另一个shell中使用jdb命令attach到启动的JVM上,注意命令末尾使用 <code>-sourcepath</code> 参数指定了项目的源码位置,这样在调试时可以查看对应的源码</p>
  278. </div>
  279. <div class="listingblock">
  280. <div class="content">
  281. <pre class="highlight"><code class="language-bash" data-lang="bash">jdb -connect com.sun.jdi.SocketAttach:hostname=127.0.0.1,port=5005 -sourcepath D:\ideaprojects\boottest\src\main\java</code></pre>
  282. </div>
  283. </div>
  284. </div>
  285. <div class="sect2">
  286. <h3 id="_添加断点"><a class="anchor" href="#_添加断点"></a>4.3. 添加断点</h3>
  287. <div class="paragraph">
  288. <p>使用 <code>stop in io.github.c.A.s</code> 添加断点,然后调用 <code>/s</code> 接口可以看到jdb会话中出现断点命中的提升并自动进入
  289. 对应线程,使用 <code>cont</code> 可以继续执行</p>
  290. </div>
  291. <div class="imageblock">
  292. <div class="content">
  293. <img src="images/breakpoint1.gif" alt="breakpoint1">
  294. </div>
  295. </div>
  296. </div>
  297. <div class="sect2">
  298. <h3 id="_断点调试"><a class="anchor" href="#_断点调试"></a>4.4. 断点调试</h3>
  299. <div class="paragraph">
  300. <p>断点调试的命令有 <code>step</code>、<code>step up</code>、<code>next</code>、<code>stepi</code> 这几条,下面演示其不同效果</p>
  301. </div>
  302. <div class="sect3">
  303. <h4 id="_step"><a class="anchor" href="#_step"></a>4.4.1. step</h4>
  304. <div class="paragraph">
  305. <p>step命令调试的效果如下所示,可以看到如果存在方法调用,step会自动进入调用的方法一步步执行,这也是它与下面的next命令的主要区别</p>
  306. </div>
  307. <div class="imageblock">
  308. <div class="content">
  309. <img src="images/breakpoint3.gif" alt="breakpoint3">
  310. </div>
  311. </div>
  312. </div>
  313. <div class="sect3">
  314. <h4 id="_next"><a class="anchor" href="#_next"></a>4.4.2. next</h4>
  315. <div class="paragraph">
  316. <p>next命令调试效果如下所示,可以看到即使存在方法调用它也不会进入调用的方法</p>
  317. </div>
  318. <div class="imageblock">
  319. <div class="content">
  320. <img src="images/breakpoint4.gif" alt="breakpoint4">
  321. </div>
  322. </div>
  323. </div>
  324. <div class="sect3">
  325. <h4 id="_step_up"><a class="anchor" href="#_step_up"></a>4.4.3. step up</h4>
  326. <div class="paragraph">
  327. <p>step up命令会执行完成当前方法并返回,效果如下所示</p>
  328. </div>
  329. <div class="imageblock">
  330. <div class="content">
  331. <img src="images/breakpoint5.gif" alt="breakpoint5">
  332. </div>
  333. </div>
  334. </div>
  335. <div class="sect3">
  336. <h4 id="_stepi"><a class="anchor" href="#_stepi"></a>4.4.4. stepi</h4>
  337. <div class="paragraph">
  338. <p>stepi命令不是按照源码中的语句执行的,而是按照编译后的字节码指令执行的,所以会看到
  339. 连续多次都在执行 <code>counter++</code>,但后面的 <code>bci</code> 不断增加,bci是字节码序号(byte code index)的缩写</p>
  340. </div>
  341. <div class="imageblock">
  342. <div class="content">
  343. <img src="images/breakpoint6.gif" alt="breakpoint6">
  344. </div>
  345. </div>
  346. </div>
  347. <div class="sect3">
  348. <h4 id="_查看变量和源码"><a class="anchor" href="#_查看变量和源码"></a>4.4.5. 查看变量和源码</h4>
  349. <div class="paragraph">
  350. <p>print命令和list命令使用效果如下</p>
  351. </div>
  352. <div class="imageblock">
  353. <div class="content">
  354. <img src="images/breakpoint7.gif" alt="breakpoint7">
  355. </div>
  356. </div>
  357. </div>
  358. </div>
  359. <div class="sect2">
  360. <h3 id="_清除断点"><a class="anchor" href="#_清除断点"></a>4.5. 清除断点</h3>
  361. <div class="paragraph">
  362. <p>使用 <code>clear</code> 查看已经添加的断点,使用 <code>clear &lt;class&gt;:&lt;line_number&gt;</code> 或者 <code>clear &lt;class&gt;.&lt;method_name&gt;[(argument_type,&#8230;&#8203;)]</code>
  363. 删除已经添加的断点</p>
  364. </div>
  365. <div class="imageblock">
  366. <div class="content">
  367. <img src="images/breakpoint2.gif" alt="breakpoint2">
  368. </div>
  369. </div>
  370. </div>
  371. </div>
  372. </div>
  373. </div>
  374. <div id="footer">
  375. <div id="footer-text">
  376. Version v1.0<br>
  377. Last updated 2024-03-18 05:44:42 UTC
  378. </div>
  379. </div>
  380. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.3/highlight.min.js"></script>
  381. <script>
  382. if (!hljs.initHighlighting.called) {
  383. hljs.initHighlighting.called = true
  384. ;[].slice.call(document.querySelectorAll('pre.highlight > code')).forEach(function (el) { hljs.highlightBlock(el) })
  385. }
  386. </script>
  387. <script src="https://utteranc.es/client.js"
  388. repo="pxzxj/articles"
  389. issue-term="title"
  390. label="utteranc"
  391. theme="github-light"
  392. crossorigin="anonymous"
  393. async>
  394. </script>
  395. </div>
  396. </div>
  397. </div>
  398. </body>
  399. </html>