arthas 高级玩法

简介: redefine的class不能修改、添加、删除类的field和method,包括方法参数、方法名称及返回值;redefine命令和jad/watch/trace/monitor/tt等命令会冲突。执行完redefine之后,如果再执行上面提到的命令,则会把redefine的字节码重置。

获取 spring context  然后为所欲为?


执行 tt 命令来记录 RequestMappingHandlerAdapter#invokeHandlerMethod 的请求


tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod


请求以后将看到


[arthas@38341]$ tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 60 ms, listenerId: 2
 INDEX      TIMESTAMP                  COST(ms)      IS-RET     IS-EXP     OBJECT              CLASS                                    METHOD
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1000       2021-08-03 15:34:31        473.709743    true       false      0x545d772e          RequestMappingHandlerAdapter             invokeHandlerMethod


可以用 tt 命令的 -i 参数来指定 index,并且用 -w 参数来执行 ognl 表达式来获取 spring context


[arthas@38341]$ tt -i 1000 -w 'target.getApplicationContext()'
@AnnotationConfigServletWebServerApplicationContext[
    reader=@AnnotatedBeanDefinitionReader[org.springframework.context.annotation.AnnotatedBeanDefinitionReader@28aa9b60],
    scanner=@ClassPathBeanDefinitionScanner[org.springframework.context.annotation.ClassPathBeanDefinitionScanner@1c138ae],
    annotatedClasses=@LinkedHashSet[isEmpty=true;size=0],
    basePackages=null,
    logger=@SLF4JLocationAwareLog[org.apache.commons.logging.impl.SLF4JLocationAwareLog@450746b3],
    DISPATCHER_SERVLET_NAME=@String[dispatcherServlet],
    webServer=@TomcatWebServer[org.springframework.boot.web.embedded.tomcat.TomcatWebServer@c5697fe],
    servletConfig=null,
    serverNamespace=null,
    servletContext=@ApplicationContextFacade[org.apache.catalina.core.ApplicationContextFacade@705b6c8f],
    themeSource=@ResourceBundleThemeSource[org.springframework.ui.context.support.ResourceBundleThemeSource@78d73e62],
    beanFactory=@DefaultListableBeanFactory[org.springframework.beans.factory.support.DefaultListableBeanFactory@4277127c: defining beans [org.springframework.context.annotatio
n.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnno
tationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,mealBootstrapApplication,bootstr
apApplicationListener.BootstrapMarkerConfiguration,org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory,glRocketMQConfig,globalConfig,mybatisPlusConfig,o
kHttpConfiguration,mealQueryBaseDataController,mealAppBannerController,appOrderController,mealBannerController,mealReturnCouponsTipsController,appletsOrderController,h5OrderCon
troller,thirdNoticeController,aliPayNoticeController,payController,wxPayNoticeController,mealGoodsExclusivePricePcController,mealManagerController,mealOrderManagerController,ba
seServiceClient,fileServiceClient,userCenterClient,mealGlobalConfigServiceImpl,mealGoodsCostServiceImpl,mealGoodsCostTableServiceImpl,mealGoodsExclusivePriceServiceImpl,mealBan
nerServiceImpl,mealManagerPcServiceImpl,mealOrderManagerPcServiceImpl,mealQueryBaseDataServiceImpl,mealReturnCouponsTipsServiceImpl,internalOrderMq,glCompensationFuncTM,glRocke
tMQLocalTM,orderPayResultServiceImpl,orderServiceImpl,orderJobServiceImpl,aliPayConfig,wxAppPayProperties,wxPayConfiguration,aliAppletCyPayServiceImpl,aliPayServiceImpl,mealPay
FlowServiceImpl,payProfitSharingFlowServiceImpl,payResultServiceImpl,weChatPayServiceImpl,payFlowHandler,payJobServiceImpl,refundServiceImpl,couponsUtil,idUtils,thirdConfig,goo
dsSyncServiceImpl,okHttpUtil,reqUtil,syncGoodsTaskHandler,costTableHandler,mealOrderHandler,payHandler,statisticTaskHandler,globalExceptionHandler,springWebMvcConfiguration,asy
ncUtils,feignHystrixConcurrencyStrategy,springSecurityConfiguration,authenticationCustomizeEntryPoint,authAccessDeniedHandler,RBACService,memberExtraInfoClientFallback,memberIn
foClientFallback,memberRiskClientFallback,memberWalletClientFallback,memberWalletFlowClientFallback,riskEventClientFallback,userCenterMemberInfoClientFallback,userCenterOperato
rClientFallback,constants,utils,captchaClientFallback,channelIsolationClientFallback,channelPriorityClientFallback,complexMessageClientFallback,JPushClientFallback,messageFeish
uBotClientFallback,messageFeiShuBotClientV2Fallback,stationLetterClientFallback,idGenerateClientFallback,couponClientFallBack,couponManagerClientFallBack,couponMemberClientFall
Back,normalFileClientFallback,transactionAutoConfiguration,webAutoConfiguration,authAutoConfiguration,cacheAutoConfiguration,transactionMQProducer,defaultMQPushConsumer,enumCus
tomizer,paginationInterceptor,okHttpClient,x509TrustManager,sslSocketFactory,pool,aliAppPayProperties,aliAppletPayProperties,appAlipayClient,appletAlipayClient,wxAppletPayServi
ce,wxAppPayService,org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor,org.springframework.boot.context.internalConfigurationPropertiesBinde
rFactory,org.springframework.boot.context.internalConfigurationPropertiesBinder,org.springframework.boot.context.properties.BoundConfigurationProperties,org.springframework.boo
t.context.properties.ConfigurationPropertiesBeanDefinitionValidator,org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata,wx.applet.pay-com.demo.meal.se
rvice.pay.config.WxAppletPayProperties,errorPageRegistrar,corsFilterRegistrationBean,org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfigurati
on,objectPostProcessor,org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration,authenticationManagerBuilder,enableGlobalAuthenti
cationAutowiredConfigurer,initializeUserDetailsBeanManagerConfigurer,initializeAuthenticationProviderBeanManagerConfigurer,org.springframework.security.config.annotation.web.co
nfiguration.WebSecurityConfiguration,delegatingApplicationListener,webSecurityExpressionHandler,springSecurityFilterChain,privilegeEvaluator,conversionServicePostProcessor,auto
wiredWebSecurityConfigurersIgnoreParents,org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration,requestDataValueProcessor,org.springframew
ork.scheduling.annotation.SchedulingConfiguration,org.springframework.context.annotation.internalScheduledAnnotationProcessor,org.springframework.boot.autoconfigure.AutoConfigu
rationPackages,com.demo.MealBootstrapApplication#MapperScannerRegistrar#0,default.com.demo.MealBootstrapApplication.FeignClientSpecification,memberExtraInfoClient.FeignClient
Specification,com.demo.user.common.client.MemberExtraInfoClient,MemberInfoClient.FeignClientSpecification,com.demo.user.common.client.MemberInfoClient,MemberRiskClient.FeignC
lientSpecification,com.demo.user.common.client.MemberRiskClient,MemberWalletClient.FeignClientSpecification,com.demo.user.common.client.MemberWalletClient,memberWalletFlowCli
ent.FeignClientSpecification,com.demo.user.common.client.MemberWalletFlowClient,riskEventClient.FeignClientSpecification,com.demo.user.common.client.RiskEventClient,userCenterMemberInfoClient.FeignClientSpecification,com.demo.user.common.client.UserCenterMemberInfoClient,userCenterOperatorClient.FeignClientSpecification,com.demo.user.common.client.UserCenterOperatorClient,captchaClient.FeignClientSpecification,com.demo.message.rest.CaptchaClient,channelIsolationClient.FeignClientSpecification,com.demo.message.rest.ChannelIsolationClient,channelPriorityClient.FeignClientSpecification,com.demo.message.rest.ChannelPriorityClient,complexMessageClient.FeignClientSpecification,com.demo.message.rest.ComplexMessageClient,jPushClient.FeignClientSpecification,com.demo.message.rest.JPushClient,messageFeishuBotClient.FeignClientSpecification,com.demo.message.rest.MessageFeishuBotClient,messageFeiShuBotClientV2.FeignClientSpecification,com.demo.message.rest.MessageFeiShuBotClientV2,stationLetterClient.FeignClientSpecification,com.demo.message.rest.StationLetterClient,idGenerateClient.FeignClientSpecification,com.demo.id.client.IdGenerateClient,couponClient.FeignClientSpecification,com.demo.marketing.client.coupon.CouponClient,couponManagerClient.FeignClientSpecification,com.demo.marketing.client.coupon.CouponManagerClient,couponMemberClient.FeignClientSpecification,com.demo.marketing.client.coupon.CouponMemberClient,normalFileClient.FeignClientSpecification,c],
    resourceLoader=null,
    customClassLoader=@Boolean[false],
    refreshed=@AtomicBoolean[true],
    MESSAGE_SOURCE_BEAN_NAME=@String[messageSource],
    LIFECYCLE_PROCESSOR_BEAN_NAME=@String[lifecycleProcessor],
    APPLICATION_EVENT_MULTICASTER_BEAN_NAME=@String[applicationEventMulticaster],
    logger=@SLF4JLocationAwareLog[org.apache.commons.logging.impl.SLF4JLocationAwareLog@71add15],
    id=@String[application-1],
    displayName=@String[org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@58740366],
    parent=@AnnotationConfigApplicationContext[org.springframework.context.annotation.AnnotationConfigApplicationContext@62d363ab, started on Tue Aug 03 15:13:22 CST 2021],
    environment=@StandardServletEnvironment[StandardServletEnvironment {activeProfiles=[local, common-local, common], defaultProfiles=[default], propertySources=[MapPropertySource {name='server.ports'}, ConfigurationPropertySourcesPropertySource {name='configurationProperties'}, EncryptablePropertySourceWrapper {name='servletConfigInitParams'}, EncryptablePropertySourceWrapper {name='servletContextInitParams'}, EncryptableMapPropertySourceWrapper {name='systemProperties'}, EncryptableSystemEnvironmentPropertySourceWrapper{name='systemEnvironment'}, EncryptablePropertySourceWrapper {name='random'}, EncryptableMapPropertySourceWrapper {name='springCloudClientHostInfo'}, EncryptableMapPropertySourceWrapper {name='applicationConfig: [classpath:/application-common.yml]'}, EncryptableMapPropertySourceWrapper {name='applicationConfig: [classpath:/application-common-local.yml]'}, EncryptableMapPropertySourceWrapper {name='applicationConfig: [classpath:/application-local.yml]'}, EncryptableMapPropertySourceWrapper {name='applicationConfig: [classpath:/application.yml]'}, EncryptableMapPropertySourceWrapper {name='springCloudDefaultProperties'}, EncryptablePropertySourceWrapper {name='cachedrandom'},  {name='Management Server'}]}],
    beanFactoryPostProcessors=@ArrayList[isEmpty=false;size=5],
    startupDate=@Long[1627974802713],
    active=@AtomicBoolean[true],
    closed=@AtomicBoolean[false],
    startupShutdownMonitor=@Object[java.lang.Object@553a67da],
    shutdownHook=@[Thread[SpringContextShutdownHook,5,main]],
    resourcePatternResolver=@ServletContextResourcePatternResolver[org.springframework.web.context.support.ServletContextResourcePatternResolver@499ea1d],
    lifecycleProcessor=@DefaultLifecycleProcessor[org.springframework.context.support.DefaultLifecycleProcessor@61ac01da],
    messageSource=@DelegatingMessageSource[Empty MessageSource],
    applicationEventMulticaster=@SimpleApplicationEventMulticaster[org.springframework.context.event.SimpleApplicationEventMulticaster@ee42df5],
    applicationListeners=@LinkedHashSet[isEmpty=false;size=38],
    earlyApplicationListeners=@LinkedHashSet[isEmpty=false;size=19],
    earlyApplicationEvents=null,
    classLoader=@AppClassLoader[jdk.internal.loader.ClassLoaders$AppClassLoader@9e89d68],
    protocolResolvers=@LinkedHashSet[isEmpty=true;size=0],
    resourceCaches=@ConcurrentHashMap[isEmpty=true;size=0],
]
Affect(row-cnt:1) cost in 4 ms.


利用 tt 命令通过 1000 这个 index 我们已经可以得到 spring context 了,得到后就可以利用 spring context 进行各种操作了。比如我们 get 一个 controllerbean 并执行一下方法


tt -i 1000 -w 'target.getApplicationContext().getBean("mealManagerController").listMealByCategoryId(1379999659854008320L)'


查看SQL语句


watch Connection


watch java.sql.Connection prepareStatement '{params,throwExp}'  -n 5  -x 3


watch BoundSql (mybatis)


watch org.apache.ibatis.mapping.BoundSql getSql '{params,returnObj,throwExp}'  -n 5  -x 3


热修复三板斧(生产慎用!)


反编译输出 .java 源码


jad --source-only com.demo.meal.pc.MealManagerController > /tmp/MealManagerController.java


修改代码


利用 vim 直接修改上面反编译出的文件内容


编译


搜索到对应类的classloader


[arthas@38341]$ sc -d *MealManagerController |grep classLoader
 classLoaderHash   9e89d68


mc(Memory Compiler) 命令来编译


mc -c  9e89d68 /tmp/MealManagerController.java -d /tmp


热加载


重新热加载(无需重启服务), 并且非侵入, 只是临时修改


[arthas@38341]$ redefine /tmp/com/demo/meal/pc/MealManagerController.class
redefine success, size: 1, classes:
com.demo.meal.pc.MealManagerController


限制


  • redefine的class不能修改、添加、删除类的field和method,包括方法参数、方法名称及返回值
  • redefine命令和jad/watch/trace/monitor/tt等命令会冲突。执行完redefine之后,如果再执行上面提到的命令,则会把redefine的字节码重置。


远程打断点 Debug?


虽然可以,但不建议 https://github.com/alibaba/arthas/issues/222

相关文章
|
8月前
|
Java 数据库连接 测试技术
进阶之路:高级Spring整合技术解析
进阶之路:高级Spring整合技术解析
|
人工智能 自然语言处理 Java
当代码遇见AI:IDEA开启ChatGPT插件,分分钟成为高效程序猿!
当代码遇见AI:IntelliJ IDEA开启ChatGPT插件,分分钟成为高效程序猿!
2423 0
|
消息中间件 NoSQL 算法
基础到高级涵盖11个技术,Alibaba最新出品711页Java面试神册真香
很多人都说八股文没用,这里聊一下我对八股文的一些看法吧:一个知识点,你能把使用以及原理说出来,我称之为八股,但是你能把底层关联以及业务使用,优化历程也能搞清楚,我称之为能力;这两点无疑是现在面试都会考察的。
|
Arthas Java 测试技术
Arthas 实践案例 | 学习笔记
快速学习 Arthas 实践案例
Arthas 实践案例 | 学习笔记
|
XML JSON Java
进阶体验:5个方面解锁Eolink高级玩法
进阶体验:5个方面解锁Eolink高级玩法
进阶体验:5个方面解锁Eolink高级玩法
|
机器学习/深度学习 人工智能 前端开发
黑科技:进阶必备,推荐两款自动生成代码神器
最近两年,在互联网行业各大技术峰会上,都能看到关于工程效能这个概念,从侧面也反映出了研发效能已经逐渐被各企业所重视!
494 0
黑科技:进阶必备,推荐两款自动生成代码神器
|
Arthas 监控 Java
早就听闻阿里开源的 Arthas 在做 Java 应用诊断上十分牛逼,没失望
早就听闻阿里开源的 Arthas 在做 Java 应用诊断上十分牛逼,身边也有很多同事在使用,因此决定开一个坑,自己从零学习下这个工具的使用,本系列使用的版本是当前最新版 3.4.5。
早就听闻阿里开源的 Arthas 在做 Java 应用诊断上十分牛逼,没失望
|
Arthas 监控 Java
Java线上问题排查神器Arthas快速上手与原理浅谈
当你兴冲冲地开始运行自己的Java项目时,你是否遇到过如下问题: 程序在稳定运行了,可是实现的功能点了没反应。 为了修复Bug而上线的新版本,上线后发现Bug依然在,却想不通哪里有问题? 想到可能出现问题的地方,却发现那里没打日志,没法在运行中看到问题,只能加了日志输出重新打包——部署——上线 程序功能正常了,可是为啥响应时间这么慢,在哪里出现了问题? 程序不但稳定运行,而且功能完美,但跑了几天或者几周过后,发现响应速度变慢了,是不是内存泄漏了?
662 0
|
Arthas 监控 Java
阿里巴巴Java应用在线诊断神器Arthas工具使用介绍
线上环境出现异常时,我们想立马查看下JVM内存使用情况,想看下我们的应用中的线程状态等等。那么阿里巴巴的在线诊断神器Arthas工具就是非常不错的选择,本文主要介绍Arthas的基本用法。 Arthas工具介绍 Arthas安装 常用命令
阿里巴巴Java应用在线诊断神器Arthas工具使用介绍
|
开发工具 图形学 Android开发
2021 友盟+移动应用性能挑战赛 -- Unity开发游戏启动慢问题解决方案
探索Unity制作游戏启动慢的问题原因分析和使用友盟+定位问题及处理问题的解决方案记录。
320 0
2021 友盟+移动应用性能挑战赛 -- Unity开发游戏启动慢问题解决方案

热门文章

最新文章