|
| 1 | +# Interprocess debugging of UnitTestBot Java |
| 2 | + |
| 3 | +### Background |
| 4 | + |
| 5 | +We have split the UnitTestBot machinery into three processes. This approach has improved UnitTestBot capabilities, e.g. |
| 6 | +provided support for various JVMs and scenarios, but also complicated the debugging flow. |
| 7 | + |
| 8 | +These are UnitTestBot processes (according to the execution order): |
| 9 | + |
| 10 | +* IDE process |
| 11 | +* Engine process |
| 12 | +* Concrete execution process |
| 13 | + |
| 14 | +Usually, main problems happen in the Engine process, but it is not the process we run first. |
| 15 | +The most straightforward way to debug the Engine process is the following. |
| 16 | + |
| 17 | +### Enable debugging for the Engine process |
| 18 | + |
| 19 | +1. Open `org/utbot/framework/UtSettings.kt`. |
| 20 | +2. Set `runIdeaProcessWithDebug` property to _true_. This enables `EngineProcess.debugArgument`. |
| 21 | +3. Find `EngineProcess.debugArgument` at `org/utbot/intellij/plugin/process/EngineProcess` and check the parameters of the debug run: |
| 22 | + |
| 23 | + `"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,quiet=y,address=5005"` |
| 24 | + |
| 25 | +* The `suspend` mode is enabled. Modify it in the case of some tricky timeouts in your scenario. |
| 26 | +* The port that will be used for debugging (`address`) is set to `5005`. Modify it if the port is already in use on your system. |
| 27 | + |
| 28 | +### Create a new run configuration for debugging the Engine process |
| 29 | + |
| 30 | +In addition to the `runIde` Gradle task that is supposed to run a new IDE instance, we should create another run |
| 31 | +configuration. |
| 32 | + |
| 33 | +1. In your IntelliJ IDEA go to **Ru**n > **Edit configurations…**. |
| 34 | +2. In the **Run/Debug Configuration** dialog, click **`+`** on the toolbar. |
| 35 | +3. In the **Run/Debug Configuration Templates** dialog that opens, select a **Remote JVM Debug** configuration type. |
| 36 | +4. Check that **Port** has the same number as the `address` parameter from the `EngineProcess.debugArgument` mentioned above. |
| 37 | +5. Give the new run configuration a meaningful name and save the run configuration. |
| 38 | + |
| 39 | +### How to debug |
| 40 | + |
| 41 | +1. In your current IntelliJ IDEA, use breakpoints to define where the program needs to be stopped. For example, set the breakpoints at |
| 42 | + |
| 43 | + `EngineProcess.createTestGenerator()`<br> |
| 44 | + `engineModel().createTestGenerator.startSuspending()` |
| 45 | + |
| 46 | +2. Start the debugger session (**Shift+F9**) for the `runIde` Gradle task (wait for the debug IDE instance to open). |
| 47 | +3. Generate tests with UnitTestBot in the debug IDE instance. Make sure symbolic execution is turned on. |
| 48 | +4. The debug IDE instance will stop generation (if you have not changed the debug parameters). If you take no action, test generation will be cancelled by timeout. |
| 49 | +5. When the Engine process started (build processes have finished, and the progress bar says: _"Generate tests: read |
| 50 | + classes"_), start the debugger session (**Shift+F9**) for your newly created Remote JVM Debug run configuration. |
| 51 | +6. Wait for the program to be suspended upon reaching the first breakpoint. |
| 52 | + |
| 53 | +### Interprocess call mapping |
| 54 | + |
| 55 | +Now you are standing on a breakpoint in the IDE process, for example, the process stopped on: |
| 56 | + |
| 57 | + `EngineProcess.createTestGenerator()` |
| 58 | + |
| 59 | +If you resume the process it reaches the next breakpoint (you are still in the IDE process): |
| 60 | + |
| 61 | + `engineModel().createTestGenerator.startSuspending()` |
| 62 | + |
| 63 | +It seems that the test generation itself should occur in the Engine process and there should be an outbound point of the IDE process. How can we find it? An how can we reach the inbound point of the Engine process? |
| 64 | + |
| 65 | +Standing on the breakpoint` engineModel().createTestGenerator.startSuspending()`, you may **Go to Declaration or |
| 66 | +Usage** and navigate to the definition of `RdCall` (which is responsible for cross-process communication) in `EngineProcessModel.createTestGenerator`. |
| 67 | + |
| 68 | +Now **Find Usages** for `EngineProcessModel.createTestGenerator` and see the point where `RdCall` is passed to the next method: |
| 69 | + |
| 70 | + synchronizer.measureExecutionForTermination() |
| 71 | + |
| 72 | +This is the point where `RdCall` is called in the Engine process. |
| 73 | + |
| 74 | +Actually you could have skipped the previous step and used **Find Usages** right away, but it is useful to know where `RdCall` is defined. |
| 75 | + |
| 76 | +If you are interested in the trailing lambda of `synchronizer.measureExecutionForTermination()`, set the breakpoint here. |
| 77 | + |
| 78 | +#### Architectural notice |
| 79 | + |
| 80 | +We must place the outbound point of the IDE process and the inbound point of the Engine process as close as possible. |
| 81 | +They may be two lambda-parameters of the same function. In this case we hope that the developer will not spend time on straying around. |
| 82 | + |
0 commit comments