(Eclipse) is it possible to dump execution data programatically?

141 views
Skip to first unread message

lufi...@redhat.com

unread,
Mar 14, 2018, 10:32:49 AM3/14/18
to JaCoCo and EclEmma Users
Hello,

While debugging SWT, I have the situation where I'm interested in what happens during a particular event (e.g low level drawing issues).

Currently I do a Thread...sleep(..) manually dump the execution. Let the event run and at the end I do System.exit(..).
This gives me a picture of what ran during that event.

However, above method is sometimes hard to use with some events. (e.g Drag and Drop debugging).

Is there any way to programatically initiate a "Dump Execution Data" ? (with "reset data" enabled).

Thank you

Evgeny Mandrikov

unread,
Mar 16, 2018, 4:54:07 PM3/16/18
to JaCoCo and EclEmma Users
Hi Leo,

First of all - there is extensive documentation covering features of EclEmma ( http://www.eclemma.org/ ) and its underlying coverage engine JaCoCo ( http://www.jacoco.org/jacoco/ ). Please study.

In particular there is documentation about APIs that JaCoCo exposes - http://www.jacoco.org/jacoco/trunk/doc/api/index.html
The interesting part is "JaCoCo Runtime" package "org.jacoco.agent.rt" with description "API to access the JaCoCo agent from within the JVM under test",
which contains class
http://www.jacoco.org/jacoco/trunk/doc/api/org/jacoco/agent/rt/RT.html - "Entry point to access the JaCoCo agent runtime."
that provides access to

In a usual Java application this can be easily used via reflection, because agent is loaded into application classloader and so application can easily access these classes.
For example to call method "reset" which will clear already collected information:

public class Example {
  public static void main(String[] args) throws Exception {
    Object jacocoAgent = Example.class.getClassLoader().loadClass("org.jacoco.agent.rt.RT").getMethod("getAgent").invoke(null);
    jacocoAgent.getClass().getMethod("reset").invoke(jacocoAgent);
  }
}

But I assume that your tests are executed inside OSGi container? In this case access gets complicated.
Fortunately the exact same API can be exposed via MBean by setting agent option "jmx=true" - see http://www.jacoco.org/jacoco/trunk/doc/agent.html
However EclEmma exposes limited set of agent options and "jmx" is not one of them - see http://www.eclemma.org/userdoc/preferences.html
Luckily there is no validation of properties, so you can abuse a bit e.g. "includes" to set "jmx=true" :


but please don't tell anyone that I recommended to do this ;) :D we state on the same page: "Use these options with caution! Invalid entries might break the code coverage launcher."


For the example - below is screenshot of usage of EclEmma for the one of its own UI tests that use SWTBot and are executed inside OSGi container, pay attention on elements marked as covered:




Now let's add reset via MBean and execute this test again:




As you can see from screenshots: code above "reset" is not considered as covered anymore, as well as creation of "view" that is done by this code, but disposal of "view" is considered as covered because executes after "reset".

Hope this helps.


P.S. please take a bit of time to study documentation.


Regards,
Evgeny

lufi...@redhat.com

unread,
Mar 19, 2018, 2:29:18 PM3/19/18
to JaCoCo and EclEmma Users
On Friday, March 16, 2018 at 4:54:07 PM UTC-4, Evgeny Mandrikov wrote:
> Hi Leo,

Thank you very much, exactly what I was looking for :-D.

I updated my blog article and added how to programatically restart code coverage:
https://developers.redhat.com/blog/2017/10/06/java-code-coverage-eclipse/

The api is also quite interesting, thanks for pointing to the relevant sections.

I've been using the above technique to figure out some SWT bugs, very helpful!

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages