JaCoCo - Java Agent

# Java Agent

JaCoCo uses class file instrumentation to record execution coverage data. Class files are instrumented on-the-fly using a so called Java agent. This mechanism allows in-memory pre-processing of all class files during class loading independent of the application framework.

If you use the JaCoCo Ant tasks or JaCoCo Maven plug-in you don't have to care about the agent and its options directly. This is transparently handled by the them.

The JaCoCo agent collects execution information and dumps it on request or when the JVM exits. There are three different modes for execution data output:

- File System: At JVM termination execution data is written to a local file.
- TCP Socket Server: External tools can connect to the JVM and retrieve execution data over the socket connection. Optional execution data reset and execution data dump on VM exit is possible.
- TCP Socket Client: At startup the JaCoCo agent connects to a given TCP endpoint. Execution data is written to the socket connection on request. Optional execution data reset and execution data dump on VM exit is possible.

The agent `jacocoagent.jar` is part of the JaCoCo distribution and includes all required dependencies. A Java agent can be activated with the following JVM option:

      -javaagent:[yourpath/]jacocoagent.jar=[option1]=[value1],[option2]=[value2]

The JaCoCo agent accepts the following options:

Option | Description | Default
`destfile` | Path to the output file for execution data. | `jacoco.exec`
`append` | If set to `true` and the execution data file already exists, coverage data is appended to the existing file. If set to `false`, an existing execution data file will be replaced. | `true`
`includes` | A list of class names that should be included in execution analysis. The list entries are separated by a colon (`:`) and may use wildcard characters (`*` and `?`). Except for performance optimization or technical corner cases this option is normally not required. | `*` (all classes)
`excludes` | A list of class names that should be excluded from execution analysis. The list entries are separated by a colon (`:`) and may use wildcard characters (`*` and `?`). Except for performance optimization or technical corner cases this option is normally not required. If you want to exclude classes from the report please configure the respective report generation tool accordingly. | empty (no excluded classes)
`exclclassloader` | A list of class loader names that should be excluded from execution analysis. The list entries are separated by a colon (`:`) and may use wildcard characters (`*` and `?`). This option might be required in case of special frameworks that conflict with JaCoCo code instrumentation, in particular class loaders that do not have access to the Java runtime classes. | `sun.reflect.DelegatingClassLoader`
`inclbootstrapclasses` | Specifies whether also classes from the bootstrap classloader should be instrumented. Use this feature with caution, it needs heavy includes/excludes tuning. | `false`
`inclnolocationclasses` | Specifies whether also classes without a source location should be instrumented. Normally such classes are generated at runtime e.g. by mocking frameworks and are therefore excluded by default. | `false`
`sessionid` | A session identifier that is written with the execution data. Without this parameter a random identifier is created by the agent. | auto-generated
`dumponexit` | If set to `true` coverage data will be written on VM shutdown. The dump can only be written if either `file` is specified or the output is `tcpserver`/`tcpclient` and a connection is open at the time when the VM terminates. | `true`
`output` | Output method to use for writing coverage data. Valid options are: `file`: At VM termination execution data is written to the file specified in the `destfile` attribute. `tcpserver`: The agent listens for incoming connections on the TCP port specified by the `address` and `port` attribute. Execution data is written to this TCP connection. `tcpclient`: At startup the agent connects to the TCP port specified by the `address` and `port` attribute. Execution data is written to this TCP connection. `none`: Do not produce any output. Please see the security considerations below. | `file`
`address` | IP address or hostname to bind to when the output method is `tcpserver` or connect to when the output method is `tcpclient`. In `tcpserver` mode the value "`*`" causes the agent to accept connections on any local address. | loopback interface
`port` | Port to bind to when the output method is `tcpserver` or connect to when the output method is `tcpclient`. In `tcpserver` mode the port must be available, which means that if multiple JaCoCo agents should run on the same machine, different ports have to be specified. | `6300`
`classdumpdir` | Location relative to the working directory where all class files seen by the agent are dumped to. This can be useful for debugging purposes or in case of dynamically created classes for example when scripting engines are used. | no dumps
`jmx` | If set to `true` the agent exposes functionality via JMX under the name `org.jacoco:type=Runtime`. Please see the security considerations below. | `false`

## Security Consideration for Remote Agent Control

The ports and connections opened in `tcpserver` and `tcpclient` mode and the JMX interface do not provide any authentication mechanism. If you run JaCoCo on production systems make sure that no untrusted sources have access to the TCP server port, or JaCoCo TCP clients only connect to trusted targets. Otherwise internal information of the application might be revealed or DOS attacks are possible.