{"id":16178844,"url":"https://github.com/wangji92/remote-debug-example","last_synced_at":"2025-03-19T01:30:51.496Z","repository":{"id":44384789,"uuid":"237238121","full_name":"WangJi92/remote-debug-example","owner":"WangJi92","description":"Java 远程debug 示范例子","archived":false,"fork":false,"pushed_at":"2020-02-02T08:15:10.000Z","size":77,"stargazers_count":15,"open_issues_count":0,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-17T01:35:11.390Z","etag":null,"topics":["debug","java","java-remote-debug"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WangJi92.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-30T15:04:42.000Z","updated_at":"2024-05-11T08:11:28.000Z","dependencies_parsed_at":"2022-07-14T14:31:22.266Z","dependency_job_id":null,"html_url":"https://github.com/WangJi92/remote-debug-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WangJi92%2Fremote-debug-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WangJi92%2Fremote-debug-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WangJi92%2Fremote-debug-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WangJi92%2Fremote-debug-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WangJi92","download_url":"https://codeload.github.com/WangJi92/remote-debug-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244336124,"owners_count":20436772,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["debug","java","java-remote-debug"],"created_at":"2024-10-10T05:24:40.576Z","updated_at":"2025-03-19T01:30:51.232Z","avatar_url":"https://github.com/WangJi92.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Java 远程debug\n\n\u003ca name=\"b0lJr\"\u003e\u003c/a\u003e\n## 一、remote debug\n何为远程debug呢？通常我们在开发过程中，都会将代码部署到服务中，这个时候QA 提出了一个bug,通过查看代码的逻辑发现问题十分的困难？一般情况下都是想着本地能不能复现一下，本地debug 调试一下；或者通过arthas 进行相关逻辑的诊断也是可以的。但是今天我们讲的是远程debug，意思就是直接调试linux 服务器上面的代码，在IDEA中添加一个remote 然后填写远程服务器的开启JPDA（Java Platform Debugger Architecture)的端口号,只需要本地存在相同的代码即可。这样的使用场景非常多，比如本地启动代码真的特别的麻烦，有时候开发代码都是直接使用远程debug。\n\u003ca name=\"dJqr2\"\u003e\u003c/a\u003e\n## 二、实践\n\n\u003ca name=\"13ssx\"\u003e\u003c/a\u003e\n### 2.1 远程服务器java程序开启JPDA\n  java -agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n  -jar xxx.jar,就是开启了远程debug，自己本地想测试，可以使用下面这个脚本在当前项目目录创建一个，通过脚本启动jar。\n```bash\n#!/bin/sh -x\ndir=$(cd `dirname $0`;pwd)\necho $dir\nmvn clean install -Dmaven.test.skip=true -U \u0026\u0026\\\njava -Dspring.profiles.active=project \\\n-Dfile.encoding=UTF-8 \\\n-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n \\\n-jar $dir/target/remote-debug.jar\n```\n\n\u003ca name=\"04335\"\u003e\u003c/a\u003e\n### 2.2 本地启动remote-debug 客户端\n在IDEA中选择remote的这种方式，然后填写服务器端开启的ip 和JPDA的端口即可,，然后启动即可调试\n```bash\n-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005\n```\n![image.png](https://cdn.nlark.com/yuque/0/2020/png/171220/1580365740982-c50088b5-1ebc-49a8-9d68-e6a60a485600.png#align=left\u0026display=inline\u0026height=493\u0026name=image.png\u0026originHeight=986\u0026originWidth=2106\u0026size=169813\u0026status=done\u0026style=none\u0026width=1053)\n\n然后就可以像调试本地一样的进行调试了，非常的方便\n\n![image.png](https://cdn.nlark.com/yuque/0/2020/png/171220/1580365836357-7fd99654-b035-4af8-9f5b-6b1ea2f789db.png#align=left\u0026display=inline\u0026height=511\u0026name=image.png\u0026originHeight=1022\u0026originWidth=2110\u0026size=182466\u0026status=done\u0026style=none\u0026width=1055)\n\n\u003ca name=\"gBs3u\"\u003e\u003c/a\u003e\n## 三、理论\nJPDA（Java Platform Debugger Architecture）是Java平台调试体系结构的缩写。由3个规范组成，分别是JVMTI(JVM Tool Interface)，JDWP(Java Debug Wire Protocol)，JDI(Java Debug Interface) 。\u003cbr /\u003e![image.png](https://cdn.nlark.com/yuque/0/2020/png/171220/1580366114446-eecca308-f8f5-4a92-a908-ae3771686532.png#align=left\u0026display=inline\u0026height=323\u0026name=image.png\u0026originHeight=323\u0026originWidth=481\u0026size=40062\u0026status=done\u0026style=none\u0026width=481)\u003cbr /\u003e1.JVMTI定义了虚拟机应该提供的调试服务，包括调试信息（Information譬如栈信息）、调试行为（Action譬如客户端设置一个断点）和通知（Notification譬如到达某个断点时通知客户端），该接口由虚拟机实现者提供实现，并结合在虚拟机中。本质上JVMTI是一个programming interface，主要用在开发和监控上。而且它提供了接口去观察(inspect) 应用状态和控制应用的执行。提供了profiling、debuging、monitoring、thread analysis、coverage analysis等等，我们使用到的debug，[只是JVMTI提供的众从能力中的一种](https://mp.weixin.qq.com/s?__biz=MzI3MTEwODc5Ng==\u0026mid=2650859362\u0026idx=1\u0026sn=6d0a588da10ebf38e9d83cfa4e7e16fa\u0026chksm=f13298b1c64511a78e3a87df0f1ddebd87719a36a9335edd759c404cd5c53d65e9383da613f1\u0026scene=21#wechat_redirect)。Class的hotSwap，就是通过Instrument实现class的redefine和retransform实现。可以参考arthas实现、[或者这篇文章](https://www.cnblogs.com/goodAndyxublog/p/11880314.html)。\u003cbr /\u003e2.JDWP定义调试服务和调试器之间的通信，包括定义调试信息格式和调试请求机制\u003cbr /\u003e3.JDI在语言的高层次上定义了调试者可以使用的调试接口以能方便地与远程的调试服务进行交互，Java语言实现，调试器实现者可直接使用该接口访问虚拟机调试服务。 java调试工具jdb，就是sun公司提供的JDI实现。\n\n更多JPDA 可以参考\n\n- 第 1 部分，JPDA 体系概览  [https://www.ibm.com/developerworks/cn/java/j-lo-jpda1/](https://www.ibm.com/developerworks/cn/java/j-lo-jpda1/)\n- 第 2 部分  JVMTI 和 Agent 实现 [https://www.ibm.com/developerworks/cn/java/j-lo-jpda2/](https://www.ibm.com/developerworks/cn/java/j-lo-jpda2/)\n- 第 3 部分: JDWP 协议及实现   [https://www.ibm.com/developerworks/cn/java/j-lo-jpda3/](https://www.ibm.com/developerworks/cn/java/j-lo-jpda3/)\n- 第 4 部分: Java 调试接口（JDI）  [https://www.ibm.com/developerworks/cn/java/j-lo-jpda4/](https://www.ibm.com/developerworks/cn/java/j-lo-jpda4/)\n\n\u003ca name=\"xNT5y\"\u003e\u003c/a\u003e\n## 四、参考文档\n\n- JAVA远程debug   [https://blog.csdn.net/wz122330/article/details/82877839](https://blog.csdn.net/wz122330/article/details/82877839)\n- Tomcat Remote Debug操作和原理 [https://www.cnblogs.com/XuYankang/p/jpda.html](https://www.cnblogs.com/XuYankang/p/jpda.html)\n- 当我们谈Debug时，我们在谈什么(Debug实现原理) [https://mp.weixin.qq.com/s?__biz=MzI3MTEwODc5Ng==\u0026mid=2650859362\u0026idx=1\u0026sn=6d0a588da10ebf38e9d83cfa4e7e16fa\u0026chksm=f13298b1c64511a78e3a87df0f1ddebd87719a36a9335edd759c404cd5c53d65e9383da613f1\u0026scene=21#wechat_redirect](https://mp.weixin.qq.com/s?__biz=MzI3MTEwODc5Ng==\u0026mid=2650859362\u0026idx=1\u0026sn=6d0a588da10ebf38e9d83cfa4e7e16fa\u0026chksm=f13298b1c64511a78e3a87df0f1ddebd87719a36a9335edd759c404cd5c53d65e9383da613f1\u0026scene=21#wechat_redirect)\n- 手把手教你实现热更新功能，带你了解 Arthas 热更新背后的原理  [https://www.cnblogs.com/goodAndyxublog/p/11880314.html](https://www.cnblogs.com/goodAndyxublog/p/11880314.html)\n- javaagent使用指南  [https://www.cnblogs.com/rickiyang/p/11368932.html](https://www.cnblogs.com/rickiyang/p/11368932.html)\n\n## 五、更多\n* 源码地址 [https://github.com/WangJi92/remote-debug-example](https://github.com/WangJi92/remote-debug-example)\n* [remote debug video](https://www.bilibili.com/video/av86278550)\n\u003e 更多[汪小哥](https://wangji.blog.csdn.net/) or [语雀分享](https://www.yuque.com/docs/share/970bb79c-be5d-4895-96d5-2d10955e4b13?#)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangji92%2Fremote-debug-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwangji92%2Fremote-debug-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangji92%2Fremote-debug-example/lists"}