1b1994897Sopenharmony_ci# Runtime debug API
2b1994897Sopenharmony_ci
3b1994897Sopenharmony_ci### Requirements
4b1994897Sopenharmony_ci
5b1994897Sopenharmony_ci1. Runtime should support debugging on the platforms from low-end IoT devices to hi-end mobile phones.
6b1994897Sopenharmony_ci
7b1994897Sopenharmony_ci### Key Design Decisions
8b1994897Sopenharmony_ci
9b1994897Sopenharmony_ci1. Runtime doesn't patch apps' bytecode on the fly. Instead of it notification about changing bytecode pc is used.
10b1994897Sopenharmony_ci
11b1994897Sopenharmony_ci1. Runtime and debugger work in the same process. Debugger functionality is provided via shared library, that runtime loads when works in debugger mode. Debugger works in the own thread and responsibilies for thread management relies on it, runtime doesn't create/destroy threads.
12b1994897Sopenharmony_ci
13b1994897Sopenharmony_ci### Rationale
14b1994897Sopenharmony_ci
15b1994897Sopenharmony_ci1. As some low-end targets can store bytecode in ROM, runtime cannot patch app's bytecode on the fly. So it uses slower approach with interpreter instrumentation.
16b1994897Sopenharmony_ci
17b1994897Sopenharmony_ci1. To simplify communication beetween debugger and runtime (especially on microcontrollers) they are work in the same process. Debugger is loaded as shared library when it's necessary.
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_ci### Specification / Implementation
20b1994897Sopenharmony_ci
21b1994897Sopenharmony_ciTo start runtime in the debug mode a debugger [`LoadableAgent`](../runtime/include/loadable_agent.h) is loaded, which usually represents a debugger shared library. It is created by the VM (see `PandaVM::LoadDebuggerAgent()`) as it is language-specific.
22b1994897Sopenharmony_ci
23b1994897Sopenharmony_ciThe loaded agent starts a debug session when necessary (see `Runtime::StartDebugSession()`). When starting the session, the JIT is disabled, and the [`tooling::Debugger`](../runtime/tooling/debugger.h) object that implements [`tooling::DebugInterface`](../runtime/include/tooling/debug_interface.h) - the point of interaction with the runtime - is created. The agent may access this using the `Runtime::DebugSession::GetDebugger()` method.
24b1994897Sopenharmony_ci
25b1994897Sopenharmony_ciRuntime provides [`RuntimeNotificationManager`](../runtime/include/runtime_notification.h) class that allows to subscript for different events:
26b1994897Sopenharmony_ci* `LoadModule` - occurs when panda file is loaded by the runtime
27b1994897Sopenharmony_ci* `BytecodePcChanged` - occurs when bytecode pc is changed during interpretation (only if runtime works in debug mode)
28b1994897Sopenharmony_ci
29b1994897Sopenharmony_ci[`tooling::Debugger`](../runtime/tooling/debugger.h) subscribes to these events and notificates debugger via hooks.
30