鏂规璁捐锛氬熀浜嶪DEA鎻掍欢寮€鍙戝拰瀛楄妭鐮佹彃妗╂妧鏈紝瀹炵幇鐮斿彂浜や粯璐ㄩ噺鑷姩鍒嗘瀽
Posted 灏忓倕鍝?,
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏂规璁捐锛氬熀浜嶪DEA鎻掍欢寮€鍙戝拰瀛楄妭鐮佹彃妗╂妧鏈紝瀹炵幇鐮斿彂浜や粯璐ㄩ噺鑷姩鍒嗘瀽相关的知识,希望对你有一定的参考价值。
浣滆€咃細灏忓倕鍝?/p>
娌夋穩銆佸垎浜€佹垚闀匡紝璁╄嚜宸卞拰浠栦汉閮借兘鏈夋墍鏀惰幏锛侌煒?/blockquote>涓€銆佸墠瑷€
濡備綍淇濊瘉浠g爜璐ㄩ噺锛?/code>
涓氬姟鎻愰渶姹傦紝浜у搧瀹氭柟妗堬紝鐮斿彂鍋氬疄鐜帮紝娴嬭瘯楠屾祦绋嬨€傚洓绉嶈鑹茬殑鐩镐簰閰嶅悎鏄‘淇濅竴涓渶姹備笂绾跨殑蹇呭鏉′欢銆傚湪鏁翠釜闇€姹傜殑浜や粯璐ㄩ噺绾у埆鍒掑垎涓紝鐮斿彂涓庢祴璇曟槸闈炲父閲嶇殑涓€鐜紝濡傛灉鐮斿彂鎻愭祴鐨勪唬鐮佽川閲忎笉楂橈紝灏变細鍑虹幇涓嶅悓绾у埆鐨勪慨BUG銆佽繑宸ョ敋鑷抽噸鍋氱殑椋庨櫓銆?/p>
閭d箞锛屾€庝箞鏉ユ彁楂樹唬鐮佽川閲忓憿锛熶竴鑸垜浠兘浼氳姹傜爺鍙戝湪寮€鍙戜唬鐮佺殑杩囩▼涓紪鍐欏崟鍏冩祴璇曪紝楠岃瘉鑷繁鐨勪唬鐮侀€昏緫銆傚鏋滄渶缁堝崟鍏冩祴璇曡鐩栧害涓嶈冻锛屽彲浠ョ敱娴嬭瘯鎷掔粷鐮斿彂鎻愭祴銆?/p>
浣嗘槸锛屾暣涓渶姹傚疄鐜扮殑浠g爜鏄湪鍏ㄩ儴寮€鍙戝畬鎴愬悗鎻愭祴鐨勶紝涔熷氨鏄复杩戜笂绾跨殑鏈€鍚庝竴鐜紝澶у鎵嶇煡閬撴煇涓爺鍙戠殑鏌愪釜鍔熻兘鍩熺殑瀹炵幇鏄惁鍏峰鎻愭祴鏉′欢銆傚鏋滆繖涓椂鍊欎唬鐮佽川閲忎笉楂橈紝閭d箞鎺ヤ笅鏉ュ氨鏄」鐩闄╃殑鏃跺€欍€?code>鍘嬫祴璇曟椂闂?/code>銆?code>璋冧笂绾挎椂闂?/code>锛屾€讳箣鏈夌梾鎷栫潃鏈€鍚庢垚澶х梾浜嗭紒
褰撶劧锛屼綘鍙互鍦ㄩ」鐩紑鍙戞湡闂村畾鏈熸帓鏌ヤ唬鐮侊紝鎴栬€呭湪鏃ヤ細杩涘害鍙嶉绛夌瓑鎵嬫銆傚彲杩欐牱闇€瑕佽€楄垂澶ч噺鏃堕棿1鎷?鐨勫紑鍙戞帓鏌ユ柟寮忓緢闅炬弧瓒冲鏉傛祦绋嬬殑杈冨ぇ鍨嬮」鐩紑鍙戯紝鑰屼笖瀵逛簬椤圭洰椋庨櫓鎶婃帶涔熸槸涓嶅彲棰勪及鐨勩€?/p>
鎵€浠?/strong>锛屾垜浠笇鏈涢噰闆嗙爺鍙戝湪寮€鍙戣繃绋嬩腑鐨勬墽琛屽姩浣滐紝鎶婇闄╁垽鏂彁鍓嶃€傚疄闄呮搷浣滀妇渚嬪氨鏄紝
褰撲綘寮€鍙戝畬鎴愪竴涓帴鍙o紝寮€濮嬫祴璇曡繍琛屾椂
锛屾垜浠殑鎻掍欢灏卞彲浠ラ噰闆嗗埌杩欎釜鎺ュ彛鐨勫叏閮ㄤ俊鎭紝鍖呮嫭锛氭帴鍙e悕绉般€佸叆鍙傜被鍨嬪拰鍐呭銆佸嚭鍙傜被鍨嬪拰鍐呭銆佸紓甯镐俊鎭€佽皟鐢ㄥ叧绯婚摼绛夈€傝€屽啀鎶婅繖浜涗俊鎭眹鎬绘彁浜ゅ埌鏈嶅姟绔紝鐢熸垚鏈闇€姹備唬鐮佸垎鏀笅鐨勫叏閮ㄦ帴鍙e姩浣滐紝浠ュ強鍚勭郴缁熼棿鐨勫叧绯婚摼璺紝骞堕檮甯﹂殢鏃剁敓鎴愭渶鏂扮殑鎺ュ彛鏂囨。鍜屼竴閿祴璇曢獙璇佸姛鑳姐€傚悗鏈熸祴璇曚汉鍛樹粙鍏ユ椂灏卞彲浠ュ弬鑰冪爺鍙戝湪缂栫爜杩囩▼涓殑鍏ㄩ儴娴嬭瘯鐢ㄤ緥锛屼篃鍙互鏌ョ湅鏁翠釜鍔熻兘鐨勮鐩栫▼搴︼紝姝ゅ娴嬭瘯浜哄憳娴嬭瘯杩囩▼涓殑鏁版嵁涔熶細琚繚鐣欎笅銆傜幇鍦ㄦ嫢鏈夎繖浜涙暟鎹俊鎭互鍚庯紝灏卞彲浠ュ畬鏁寸殑鐢熸垚涓€濂楃爺鍙戞祴璇曡川閲忎氦浠樺叏瑙堝浘锛岃鏁翠釜宸ョ▼寮€鍙戜氦浠樿川閲忚瘎浼伴€忔槑鍖栥€?/p>鎺ヤ笅鏉ユ垜浠氨鎸夌収浠ヤ笂鐨勬弿杩版€у唴瀹癸紝瀹炶返寮€鍙戜竴涓渚嬩綋浼氫笅銆?em>璧拌捣锛?/em>
浜屻€佹妧鏈疄鐜板噯澶?/h2>
- 瀛楄妭鐮佹彃妗╋紝鍥犱负鎴戜滑闇€瑕侀噰闆嗗埌鎺ュ彛鎵ц淇℃伅锛岄偅涔堝氨闇€瑕佷娇鐢ㄥ瓧鑺傜爜鎻掓々缁勪欢缁欐帴鍙f柟娉曞寮恒€傝繖涓疄鐜版湁鐐圭被浼艰胺姝岀殑Dapper锛屽ぇ瑙勬ā鍒嗗竷寮忔灦鏋勭殑闈炲叆渚电洃鎺с€傚彧涓嶈繃鎴戜滑闇€瑕侀噰闆嗙殑鎻忚堪鎬т俊鎭洿澶氥€傚叧浜庡瓧鑺傜爜鎻掓々锛屽彲浠ヤ簡瑙SM銆丣avassist銆丅yte-Buddy锛屽畠浠兘鍙互鍋氭椤瑰伐浣溿€?/li>
- IDEA 鎻掍欢寮€鍙戯紝鍥犱负鎴戜滑闇€瑕佸湪鐮斿彂浜哄憳寮€鍙戣繃绋嬩腑杩涜閲囬泦锛屼篃涓嶇牬鍧忕爺鍙戠殑鎿嶄綔涔犳儻銆傞偅涔堟渶濂界殑鏂瑰紡灏辨槸宓屽叆鍒?code>鍚姩杩愯涓紝鍙鍦ㄥ紑鍙戣繃绋嬩腑鏈夎繍琛屼唬鐮佺殑鍔ㄤ綔锛屽氨閲囬泦鐩稿簲鐨勬帴鍙d俊鎭€?/li>
- 鏈€鍚庡氨鏄暟鎹殑浼犺緭鍜屽鐞嗭紝浼犺緭鍙互浣跨敤MQ鎴栬€呯洿鎺ョ敤Netty銆傝€屽鐞嗘暟鎹殑杩囩▼浼氱浉瀵规瘮杈冨鏉傦紝鍦ㄨ繖涓繃绋嬮渶瑕佸垎鏋愬嚭鏈変环鍊肩殑鏁版嵁锛屽悓绫荤殑鏁版嵁锛屽悎骞朵竴鏉℃墽琛岄摼璺殑鏁版嵁锛屼互鍙婄敓鎴愮浉鍏崇殑鎺ュ彛鏂囨。鍜屽伐绋嬫湇鍔″湴鍥俱€?/li>
涓夈€佸瀛楄妭鐮佹彃妗?/h2>
杩欓噷鎴戜滑浣跨敤鐨勫瓧鑺傜爜鎻掓々缁勪欢鏄?
Byte-buddy
锛屽畠鏄竴涓唬鐮佺敓鎴愬拰鎿嶄綔搴擄紝鐢ㄤ簬鍦?Java
搴旂敤绋嬪簭杩愯鏃跺垱寤哄拰淇敼Java
绫伙紝鑰屾棤闇€缂栬瘧鍣ㄧ殑甯姪銆傞櫎浜?Java
绫诲簱闄勫甫鐨勪唬鐮佺敓鎴愬疄鐢ㄧ▼搴忓锛?code>Byte Buddy 杩樺厑璁稿垱寤轰换鎰忕被锛屽苟涓斾笉闄愪簬瀹炵幇鐢ㄤ簬鍒涘缓杩愯鏃朵唬鐞嗙殑鎺ュ彛銆傛澶栵紝Byte Buddy
鎻愪緵浜嗕竴绉嶆柟渚跨殑 API锛屽彲浠ヤ娇鐢?Java
浠g悊鎴栧湪鏋勫缓杩囩▼涓墜鍔ㄦ洿鏀圭被銆?/p>
- 鏃犻渶鐞嗚В瀛楄妭鐮佹寚浠わ紝鍗冲彲浣跨敤绠€鍗曠殑 API 灏辫兘寰堝鏄撴搷浣滃瓧鑺傜爜锛屾帶鍒剁被鍜屾柟娉曘€?/li>
- 宸叉敮鎸丣ava 11锛屽簱杞婚噺锛屼粎鍙栧喅浜嶫ava瀛楄妭浠g爜瑙f瀽鍣ㄥ簱ASM鐨勮闂€匒PI锛屽畠鏈韩涓嶉渶瑕佷换浣曞叾浠栦緷璧栭」銆?/li>
- 姣旇捣JDK鍔ㄦ€佷唬鐞嗐€乧glib銆丣avassist锛孊yte Buddy鍦ㄦ€ц兘涓婂叿鏈変竴瀹氱殑浼樺娍銆?/li>
1. 鏂规硶鍏ュ彛
public static void premain(String agentArgs, Instrumentation inst) { AgentBuilder.Transformer transformer = (builder, typeDescription, classLoader, javaModule) -> { return builder .method(ElementMatchers.any()) // 鎷︽埅浠绘剰鏂规硶 .intercept(MethodDelegation.to(MonitorMethod.class)); }; new AgentBuilder .Default() .type(ElementMatchers.nameStartsWith(agentArgs)) .transform(transformer) .installOn(inst); }
濡傛灉浣犳帴瑙﹁繃 Javaagent 寮€鍙戯紝閭d箞瀵逛簬 premain 浼氭瘮杈冪啛鎮夈€傚鏋滀笉娓呮浣犲彲浠ユ妸瀹冪悊瑙d负锛屽畠鏄▼搴忓惎鍔ㄧ殑鏃剁殑鏂规硶鍏ュ彛锛屼綘鍙互浠庤繖涓叆鍙d腑鎷︽埅鍒颁綘闇€瑕佺殑鏂规硶锛屼箣鍚庡瀹冭繘琛屽瓧鑺傜爜澧炲己銆傚叾瀹炰篃灏辨槸鍔ㄦ€佸啓浠g爜锛屽湪鏂规硶涓坊鍔犱綘鐨勪唬鐮侊紝鏉ユ敹闆嗘柟娉曚俊鎭€?/p>
2. 閲囬泦淇℃伅
@RuntimeType public static Object intercept(@Origin Method method, @SuperCall Callable<?> callable, @AllArguments Object[] args) throws Exception { long start = System.currentTimeMillis(); Object resObj = null; try { resObj = callable.call(); return resObj; } finally { System.out.println("鏂规硶鍚嶇О锛? + method.getName()); System.out.println("鍏ュ弬涓暟锛? + method.getParameterCount()); for (int i = 0; i < method.getParameterCount(); i++) { System.out.println("鍏ュ弬 Idx锛? + (i + 1) + " 绫诲瀷锛? + method.getParameterTypes()[i].getTypeName() + " 鍐呭锛? + args[i]); } System.out.println("鍑哄弬绫诲瀷锛? + method.getReturnType().getName()); System.out.println("鍑哄弬缁撴灉锛? + resObj); System.out.println("鏂规硶鑰楁椂锛? + (System.currentTimeMillis() - start) + "ms"); } }
杩欎釜灏辨槸浣跨敤 Byte-Buddy 鍙互閲囬泦鐨勪俊鎭紝浣犲彲浠ラ€氳繃娉ㄨВ鍏ュ弬锛岃幏鍙栧埌涓€涓柟娉曠殑鍏ㄩ儴淇℃伅銆傛柟娉曞悕绉般€佸叆鍙備釜鏁般€佸叆鍙傜被鍨嬪拰鍐呭銆佸嚭鍙傜被鍨嬪拰缁撴灉浠ュ強杩樿兘璁$畻鏂规硶鎵ц鑰楁椂銆?/p>
鍥涖€両DEA 鎻掍欢寮€鍙?/h2>
鍏充簬 IDEA 鎻掍欢寮€鍙戠殑鐭ヨ瘑鍐呭杈冨锛屽彲浠ヤ粠GitHub鎼滅储涓€浜涜祫鏂欏拰鏌ラ槄瀹樻柟鏂囨。锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fplugins.jetbrains.com%2Fdocs%2Fintellij%2Fgradle-build-system.html%3Ffrom%3Djetbrains.org" rel="nofollow">https://plugins.jetbrains.com/docs/intellij/gradle-build-system.html?from=jetbrains.org
姝ゅ婕旂ず妗堜緥鍏充簬鎻掍欢寮€鍙戠殑鍐呭姣旇緝绠€鍗曪紝涓昏鏄户鎵?
com.intellij.execution.impl.DefaultJavaProgramRunner
锛孫verridedoExecute
鏂规硶锛屾坊鍔犺嚜宸遍渶瑕佺殑鍐呭鍗冲彲銆?/p>杩欓儴鍒嗘坊鍔犵殑鍐呭鏍稿績灏辨槸鍦ㄧ▼搴忓惎鍔ㄦ椂娣诲姞鎴戜滑鐨勫瓧鑺傜爜鎻掓々绋嬪簭锛屽涓嬶細
@Override protected RunContentDescriptor doExecute(@NotNull RunProfileState state, @NotNull ExecutionEnvironment env) throws ExecutionException { JavaParameters parameters = ((JavaCommandLine) state).getJavaParameters(); // 淇℃伅鑾峰彇 PsiFile psiFile = env.getDataContext().getData(LangDataKeys.PSI_FILE); String packageName = ((PsiJavaFileImpl) psiFile).getPackageName(); // 娣诲姞瀛楄妭鐮佹彃瑁? ParametersList parametersList = parameters.getVMParametersList(); parametersList.add("-javaagent:" + this.getClass().getResource("/").getPath().substring(1) + "ProjectProbe.jar=" + packageName); return super.doExecute(state, env); }
姝ゅ鏈€鏍稿績鐨勫氨鏄?
-javaagent
鎶?ProjectProbe.jar
宸ョ▼鎺㈤拡绋嬪簭鐨凧ar鍖呭姞杞借繘鍘汇€傚叾浠栫殑灏辨槸涓€浜涘叧浜?PsiFile
API 鐨勪娇鐢紝鎰熷叴瓒e彲浠ラ槄璇诲畼鏂规枃妗d腑鐨勪粙缁嶃€?/p>浜斻€佹晥鏋滄紨绀?/h2>
瀹夎鎻掍欢
- 瀹夎鎻掍欢灏卞拰鎴戜滑姝e父瀹夎涓€鏍凤紝涓嶈繃鐩墠杩欎釜鎻掍欢鍦ㄥ紑鍙戦樁娈碉紝鎵€浠ラ渶瑕佹湰鍦板畨瑁呫€?/li>
杩愯鏁堟灉
- 涓婂浘灏辨槸杩愯鏁堟灉鐨勬渚嬫紨绀猴紝鎴戜滑鎶婅繍琛屾椂鎺ュ彛鐨勪俊鎭畬鏁寸殑杈撳嚭鍒版帶鍒跺彴銆?/li>
- 鍦ㄥ疄闄呬娇鐢ㄧ殑杩囩▼涓紝浼氭妸杩欓儴鍒嗕俊鎭紶鍥炴湇鍔$锛岀敱鏈嶅姟绔垎鏋愬鐞嗗悗锛屽睍绀哄湪椤甸潰涓娿€?/li>
鍏€佹€荤粨
- 鍩轰簬IDEA鎻掍欢鍜屽瓧鑺傜爜鎻掓々鎶€鏈紝鑳藉仛鐨勫姛鑳藉疄鐜拌繕鏈夊緢澶氥€傛湰鏂囦粎浠呮槸鍏朵腑涓€绉嶇爺鍙戝埌娴嬭瘯鐥涚偣鐨勮В鍐虫柟妗堬紝濡傛灉鎰熷叴瓒e彲浠ヤ竴璧锋繁鍏ョ爺绌躲€?/li>
- 褰撲綘鐪嬪埌杩欐牱鐨勬渚嬩互鍚庯紝甯屾湜鑳界粰浣犵殑鏄苟涓嶄竴瀹氭墍鏈夌殑鎶€鏈偣閮芥槸涓轰簡闈㈣瘯閫犵伀绠璇濈殑銆傚綋浣犵湡鐨勬妸瀹冭惤鍦颁互鍚庯紝鎵嶄細鎳傜殑鑷繁闇€瑕佸緢澶氱煡璇嗐€?/li>
- 鏈枃娌℃湁澶繃澶氱殑浠嬬粛鎻掍欢寮€鍙戝拰瀛楄妭鐮佹妧鏈紝濡傛灉瀵瑰瓧鑺傜爜缂栫▼鎰熷叴瓒o紝鍙互鍦ㄥ叕浼楀彿锛?code>bugstack铏礊鏍?/code>锛屽洖澶?code>瀛楄妭鐮佺紪绋?/code>銆傚叏涔?1涓?鍗冨瓧锛?0涓珷鑺傛兜鐩栦笁涓瓧鑺傜爜妗嗘灦(ASM銆丣avassist銆丅yte-budy)鍜孞avaAgent浣跨敤骞堕檮甯︽暣濂楁渚嬫簮鐮侊紒
涓冦€佺郴鍒楁帹鑽?/h2>
- 鏁板锛岀涓€涓▼搴忓憳鏈夊杩戯紵
- 涓€娆′唬鐮佽瘎瀹★紝宸偣杩囦笉浜嗚瘯鐢ㄦ湡锛?/a>
- 銆奐ava 闈㈢粡鎵嬪唽銆婸DF锛屽叏涔?绔?9鑺傦紝417椤?1.5涓囧瓧锛屽畬绋?amp;鍙戠増锛?/a>
- 宸ヤ綔涓ゅ勾绠€鍘嗗啓鎴愯繖鏍凤紝璋佽浣犲憖锛?/a>
- 宸ヤ綔3骞达紝鐪嬪暐璧勬枡鑳芥湀钖?0K锛?/a>
鍗氬锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fbugstack.cn" rel="nofollow">https://bugstack.cn
GitHub锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fgithub.com%2Ffuzhengwei%2FCodeGuide%2Fwiki" rel="nofollow">https://github.com/fuzhengwei/CodeGuide/wiki以上是关于鏂规璁捐锛氬熀浜嶪DEA鎻掍欢寮€鍙戝拰瀛楄妭鐮佹彃妗╂妧鏈紝瀹炵幇鐮斿彂浜や粯璐ㄩ噺鑷姩鍒嗘瀽的主要内容,如果未能解决你的问题,请参考以下文章