java jdb remote debug - java遠(yuǎn)程調(diào)試

簡(jiǎn)介

  • 用log來(lái)做調(diào)試的方法低效茫目
  • 遠(yuǎn)程調(diào)試是jdk自帶的一個(gè)有利調(diào)試工具,可以快速定位問題

在工作機(jī)上使用eclipse來(lái)自帶的remote debug

問題

  • 運(yùn)維禁止使用remote debug, 因?yàn)槿艟W(wǎng)絡(luò)邊界被攻破,內(nèi)網(wǎng)開啟java debug的機(jī)器將面臨著巨大的風(fēng)險(xiǎn)。

使用端口轉(zhuǎn)發(fā)來(lái)實(shí)現(xiàn)eclipse遠(yuǎn)程調(diào)試

  • 給服務(wù)器的java進(jìn)程加入調(diào)試參數(shù):
    • -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=127.0.0.1:8090,suspend=n
    • 本文以8090為例
  • 用ssh工具的端口轉(zhuǎn)發(fā)功能將遠(yuǎn)程端口映射到本地端口:
    • 在xshell在tunneling(隧道)處設(shè)置
      • 類型為outgoing
      • 源主機(jī)及port為localhost:8090
      • 目標(biāo)主機(jī)及port為127.0.0.1:8090
    • 在scureCRT中在port forwarding處設(shè)置
      • remote host為localhost:8090
      • local address為127.0.0.1:8090
    • 若是mac或linux系統(tǒng),可以直接執(zhí)行下面的命令來(lái)進(jìn)行tcp的端口轉(zhuǎn)發(fā):
      • ssh -f -L 8092:localhost:8092 love@114.113.201.54 -p 16322 sleep 20
  • 在eclipse中新建一個(gè)remote debug
    • host為localhost
    • port為8090

在服務(wù)器上使用jdb

背景

  • eclipse remote debug(封裝了jdb):奇慢無(wú)比,attach上往往需要幾分鐘。原因不明。。。應(yīng)該是北京辦公室和機(jī)房之間網(wǎng)絡(luò)的問題

使用方法

  • 開端口——給待調(diào)試的java進(jìn)程增加啟動(dòng)參數(shù):
    • -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=127.0.0.1:8090,suspend=n
    • '''suspend=n/y指是否在進(jìn)程啟動(dòng)時(shí)等待調(diào)試器attach, 調(diào)試一個(gè)非server的java進(jìn)程時(shí)可以將suspend設(shè)為y,否則可能在attach上之前就已經(jīng)執(zhí)行完了'''
  • 連接:jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8090
    • 我在機(jī)群上加了個(gè)alias為jdbp,直接執(zhí)行jdbp 8090為同樣的命令

幫助說明

  • help(所有用法都能通過help看到)

我用過的幾個(gè)命令

  • 使用源代碼
    • use /disk1/love-audit/branch-2.2.5/src/main/java
  • 下斷點(diǎn)
    • stop at com.netease.love.TopicStore:19
  • step控制
    • step -- execute current line
    • step up -- execute until the current method returns to * its caller
    • stepi -- execute current instruction
    • next -- step one line (step OVER calls)
    • cont -- continue execution from breakpoint
  • 查看變量
    • locals
  • 打印變量
    • dump paramNames
    • dump paramNames.elementData
    • print paramNames
  • 查看棧
    • where
  • 查看所有線程
    • threads
  • 查看源代碼
    • list

實(shí)戰(zhàn)示例(部分)

resin-port-18082-45[1] next

\> 
Step completed: "thread=resin-port-18082-45", com.netease.love.topic.store.TopicDbStore.getTopics(), line=98 bci=41
98                sql = parseStatement(Topic.class, "selectWithLimit");

resin-port-18082-45[1] list
94                sql = parseStatement(Topic.class, "selectByStateWithLimit");
95                namedParameters.put("state", Const.TOPIC_STATE.ONLINE);  
96            }
97            else {
98 =>             sql = parseStatement(Topic.class, "selectWithLimit");
99            }
100            namedParameters.put("offset", offset); 
101            namedParameters.put("length", length);
102            List<Topic> topics = namedParameterJdbcTemplate.query(sql, namedParameters, Topic.ROW_MAPPER);
103            return topics;
resin-port-18082-45[1] next

\> 
Step completed: "thread=resin-port-18082-45", com.netease.love.topic.store.TopicDbStore.getTopics(), line=100 bci=52
100            namedParameters.put("offset", offset); 

resin-port-18082-45[1] next

\> 
Step completed: "thread=resin-port-18082-45", com.netease.love.topic.store.TopicDbStore.getTopics(), line=101 bci=66
101            namedParameters.put("length", length);

resin-port-18082-45[1] locals
Method arguments:
publishedOnly = false
offset = 0
length = 20
Local variables:
namedParameters = instance of java.util.HashMap(id=11628)
sql = "SELECT id, type, publish_id, title, context, pub_time, edit_time, editor, discuss_count, state FROM Topic_Topic  ORDER BY publish_id DESC LIMIT :limit OFFSET :offset"
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容