This document provides an introduction and overview of Byteman, a tool for injecting faults into Java applications. It covers how to activate and load Byteman scripts, the ECA (event-condition-action) model used to define rules, common locators and expressions, options for compiling rules, helpers for debugging and tracing, examples of rules, and integrating Byteman with testing frameworks. Configuration details are also provided for using Byteman with WildFly.
3. Resolvendo o problema
RULE setRequestsRecovery
CLASS org.jbpm.process.core.timer.impl.QuartzSchedulerService
METHOD internalSchedule
AT WRITE $jobq
IF TRUE
DO
traceln("[BYTEMAN] Current Value for requestsRecovery: " + $jobq.requestsRecovery() +
" ---------- UPDATING");
$jobq.setRequestsRecovery(true);
traceln("[BYTEMAN] New value for requestsRecovery: " + $jobq.requestsRecovery());
ENDRULE
4. Primeiros Passos
• Como Ativar o Byteman?
• -javaagent:/$BYTEMAN_HOME/lib/byteman.jar=script:$BYTEMAN_HOME/scripts/MyCo
olScript.btm
• sys - Adiciona o byteman ao System classloader.
• boot - Adiciona o byteman ao bootstrap classloader.
• Load/Unload Byteman em Runtime
• bminstall.sh - Cuidado!
• bmsubmit.sh - Load/Unload de Scripts Byteman.
• Integrações
• Principais framework de testes: Junit e TestNG.
• Maven/ant (Annotations).
• Validando o script
• bmcheck.sh
5. ECA - Event Conditions Action
• Where (onde)
• CLASS java.lang.Thread
• METHOD start()
• AT ENTRY
• Whether (se)
• IF true
• What (Qual)
• DO traceln("*** Hello TDC POA ***")
6. Esqueleto do Script Byteman
RULE @X regra feliz
COMPILE|NOCOMPILE (Opcional)
CLASS|INTERFACE br.com.tdcpoa.MinhaClasseLegal
METHOD NaoMeModifique (Para construtores <init>)
HELPER br.com.tdcpoa.MeuAjudanteNota10 (Opcional)
AT WRITE $objetoA (Opcional)
BIND nome:type = valor (Opcional)
feliz:boolean = $objetoA.Feliz();
IF feliz == true
DO //faça algo legal
minhaOperacaoBuiltInParaImprimir(“Feliz“);
ENDRULE - Fim da definição da regra
7. Especificadores de Localização
AT ENTRY AFTER INVOKE [ type .] method [ ( argtypes ) ][count | ALL ]
AT EXIT AT SYNCHRONIZE [count | ALL ]
AT LINE number AFTER SYNCHRONIZE [count | ALL ]
AT READ [type .] field [count | ALL ] AT THROW [count | ALL ]
AT READ $var-or-idx [count | ALL ] AT EXCEPTION EXIT
AFTER READ [ type .] field [count | ALL ]
AFTER READ $var-or-idx [count | ALL ]
AT WRITE [ type .] field [count | ALL ]
AT WRITE $var-or-idx [count | ALL ]
AFTER WRITE [ type .] field [count | ALL ]
AFTER WRITE $var-or-idx [count | ALL ]
AT INVOKE [ type .] method [ ( argtypes ) ] [count | ALL ]
8. O que é um Helper?
• Auxiliar
• Helper.java
• Subdivido em três categorias:
• coordenação de threads
• Gerenciamento do ciclo de vida das regras
• Debug e Tracing
import org.jboss.byteman.rule.helper.Helper;
public class MeuAjudanteNota10 extends Helper {
public void printHello(){
System.out.println(“Hello TDC POA);
}
}
9. Expressões de Referência
$!, $^, $#, $*, $@, $CLASS e $METHOD
$0, $1, $x, $this
CLASS ^javax.naming.InitialContext
Operadores
OR (||), LE (< =), NE (!=) TIMES (*) MINUS (-)
AND (&&), LT (<) GE (>=) DIVIDE (/) MOD (%)
NOT (!), EQ (==), GT (>) PLUS (+)
Principais Expressões
10. Compilar ou Interpretar as regras?
● Interpretadas
● Vale a pena?
● Quando usar regras compiladas?
● org.jboss.byteman.compile.to.bytecode
ClassNotFound?
● IMPORTS
● JBoss Modules
● OSGI e JigSaw (java 9)
12. Verificando Regras
• Sempre verifique
• Livre de erros e warnings
• bmcheck
• bmcheck.sh -p br.com.tdcpoa -cp .
../../src/test/resources/check.btm
• maven
• configurar plugin byteman-rulecheck-maven-plugin
13. Ativando o Byteman no WildFly 10!
• Arquivos standalone.conf e domain.conf
if [ "x$JBOSS_MODULES_SYSTEM_PKGS" = "x" ]; then
JBOSS_MODULES_SYSTEM_PKGS="org.jboss.byteman"
fi
Modo Standalone:
JAVA_OPTS="$JAVA_OPTS
-javaagent:$BYTEMAN_HOME/lib/byteman.jar=script:/dados/presentations/byteman/demo/byteman-web-app/src/resources/th
read.btm,boot:$BYTEMAN_HOME/lib/byteman.jar,boot:/dados/presentations/byteman/demo/byteman-helper/target/byteman-
helper-1.0-SNAPSHOT.jar -Dorg.jboss.byteman.transform.all"
Domain:
<jvm-options>
<option
value="-javaagent:/${jboss.home.dir}/byteman/lib/byteman.jar=script:${jboss.home.dir}/byteman/scripts/org.jboss.re
moting3.EndpointImpl.btm,boot:${jboss.home.dir}/byteman/lib/byteman.jar"/>
<option value="-Dorg.jboss.byteman.transform.all=true"/>
</jvm-options>
14. Trabalhando com Regras em Runtime
● Instalando o byteman em uma JVM em execução:
> bminstall.sh 13101
> bminstall.sh -b -Dorg.jboss.byteman.transform.all
$JBOSS_HOME/jboss-modules.jar
● Carregando regras em runtime:
> bmsubmit.sh -l thread.btm
Descarregando regras:
> bmsubmit.sh -u thread.btm