xref: /aosp_15_r20/external/pigweed/pw_emu/guide.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_emu-guide:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker====================
4*61c4878aSAndroid Build Coastguard WorkerGet started & guides
5*61c4878aSAndroid Build Coastguard Worker====================
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module-subpage::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_emu
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard Worker.. _module-pw_emu-get-started:
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard Worker-----------
12*61c4878aSAndroid Build Coastguard WorkerGet started
13*61c4878aSAndroid Build Coastguard Worker-----------
14*61c4878aSAndroid Build Coastguard Worker.. tab-set::
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: Bazel
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Worker      ``pw_emu`` is currently only supported for GN-based projects.
19*61c4878aSAndroid Build Coastguard Worker
20*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: GN
21*61c4878aSAndroid Build Coastguard Worker
22*61c4878aSAndroid Build Coastguard Worker      Include the desired emulator target files :ref:`pigweed.json
23*61c4878aSAndroid Build Coastguard Worker      <seed-0101>`. For example:
24*61c4878aSAndroid Build Coastguard Worker
25*61c4878aSAndroid Build Coastguard Worker      .. code-block:: json
26*61c4878aSAndroid Build Coastguard Worker
27*61c4878aSAndroid Build Coastguard Worker         "pw_emu": {
28*61c4878aSAndroid Build Coastguard Worker            "target_files": [
29*61c4878aSAndroid Build Coastguard Worker              "pw_emu/qemu-lm3s6965evb.json",
30*61c4878aSAndroid Build Coastguard Worker              "pw_emu/qemu-stm32vldiscovery.json",
31*61c4878aSAndroid Build Coastguard Worker              "pw_emu/qemu-netduinoplus2.json",
32*61c4878aSAndroid Build Coastguard Worker              "renode-stm32f4_discovery.json"
33*61c4878aSAndroid Build Coastguard Worker            ]
34*61c4878aSAndroid Build Coastguard Worker          }
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard Worker      See :ref:`module-pw_emu-config-fragments` for examples of target files.
37*61c4878aSAndroid Build Coastguard Worker
38*61c4878aSAndroid Build Coastguard Worker      Build a target and then use ``pw emu run`` to run the target binaries on
39*61c4878aSAndroid Build Coastguard Worker      the host. Continuing with the example:
40*61c4878aSAndroid Build Coastguard Worker
41*61c4878aSAndroid Build Coastguard Worker      .. code-block:: console
42*61c4878aSAndroid Build Coastguard Worker
43*61c4878aSAndroid Build Coastguard Worker         ninja -C out qemu_gcc
44*61c4878aSAndroid Build Coastguard Worker         pw emu run --args=-no-reboot qemu-lm3s6965evb \
45*61c4878aSAndroid Build Coastguard Worker             out/lm3s6965evb_qemu_gcc_size_optimized/obj/pw_snapshot/test/cpp_compile_test
46*61c4878aSAndroid Build Coastguard Worker
47*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: CMake
48*61c4878aSAndroid Build Coastguard Worker
49*61c4878aSAndroid Build Coastguard Worker      ``pw_emu`` is not currently supported for CMake-based projects.
50*61c4878aSAndroid Build Coastguard Worker
51*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: Zephyr
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard Worker      ``pw_emu`` is not currently supported for Zephyr-based projects.
54*61c4878aSAndroid Build Coastguard Worker
55*61c4878aSAndroid Build Coastguard Worker   .. tab-item:: Android host
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard Worker      Create a :ref:`pigweed.json <seed-0101>` file and include the
58*61c4878aSAndroid Build Coastguard Worker      desired emulator targets.
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker      Build the ``pw emu`` standalone Android Python executable:
61*61c4878aSAndroid Build Coastguard Worker
62*61c4878aSAndroid Build Coastguard Worker      .. code-block:: console
63*61c4878aSAndroid Build Coastguard Worker
64*61c4878aSAndroid Build Coastguard Worker         m pw_emu_py
65*61c4878aSAndroid Build Coastguard Worker
66*61c4878aSAndroid Build Coastguard Worker      Run ``pw emu``:
67*61c4878aSAndroid Build Coastguard Worker
68*61c4878aSAndroid Build Coastguard Worker      .. code-block:: console
69*61c4878aSAndroid Build Coastguard Worker
70*61c4878aSAndroid Build Coastguard Worker         pw_emu_py -c <path to pigweed.json> start <emulator target>
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard Worker------------------------
73*61c4878aSAndroid Build Coastguard WorkerSet up emulation targets
74*61c4878aSAndroid Build Coastguard Worker------------------------
75*61c4878aSAndroid Build Coastguard WorkerAn emulator target can be defined directly in the ``pw.pw_emu`` namespace of
76*61c4878aSAndroid Build Coastguard Worker:ref:`pigweed.json <seed-0101>`, like this:
77*61c4878aSAndroid Build Coastguard Worker
78*61c4878aSAndroid Build Coastguard Worker.. code-block::
79*61c4878aSAndroid Build Coastguard Worker
80*61c4878aSAndroid Build Coastguard Worker   {
81*61c4878aSAndroid Build Coastguard Worker     "pw": {
82*61c4878aSAndroid Build Coastguard Worker       "pw_emu": {
83*61c4878aSAndroid Build Coastguard Worker         "targets": {
84*61c4878aSAndroid Build Coastguard Worker           "qemu-lm3s6965evb": {
85*61c4878aSAndroid Build Coastguard Worker             "...": "..."
86*61c4878aSAndroid Build Coastguard Worker           }
87*61c4878aSAndroid Build Coastguard Worker         }
88*61c4878aSAndroid Build Coastguard Worker       }
89*61c4878aSAndroid Build Coastguard Worker     }
90*61c4878aSAndroid Build Coastguard Worker   }
91*61c4878aSAndroid Build Coastguard Worker
92*61c4878aSAndroid Build Coastguard WorkerOr it can be defined elsewhere and then imported into ``pigweed.json``, like
93*61c4878aSAndroid Build Coastguard Workerthis:
94*61c4878aSAndroid Build Coastguard Worker
95*61c4878aSAndroid Build Coastguard Worker.. code-block::
96*61c4878aSAndroid Build Coastguard Worker
97*61c4878aSAndroid Build Coastguard Worker   {
98*61c4878aSAndroid Build Coastguard Worker     "pw": {
99*61c4878aSAndroid Build Coastguard Worker       "pw_emu": {
100*61c4878aSAndroid Build Coastguard Worker         "target_files": [
101*61c4878aSAndroid Build Coastguard Worker           "qemu-lm3s6965evb.json"
102*61c4878aSAndroid Build Coastguard Worker         ]
103*61c4878aSAndroid Build Coastguard Worker       }
104*61c4878aSAndroid Build Coastguard Worker     }
105*61c4878aSAndroid Build Coastguard Worker   }
106*61c4878aSAndroid Build Coastguard Worker
107*61c4878aSAndroid Build Coastguard WorkerRelative paths are interpreted relative to the root of your project directory.
108*61c4878aSAndroid Build Coastguard Worker
109*61c4878aSAndroid Build Coastguard WorkerYou can configure default options at the emulator level and then override
110*61c4878aSAndroid Build Coastguard Workerthose options at the target or channel level. See :ref:`module-pw_emu-config`
111*61c4878aSAndroid Build Coastguard Workerfor details.
112*61c4878aSAndroid Build Coastguard Worker
113*61c4878aSAndroid Build Coastguard WorkerQEMU targets
114*61c4878aSAndroid Build Coastguard Worker============
115*61c4878aSAndroid Build Coastguard WorkerWhen defining a QEMU emulation target the following keys must be defined
116*61c4878aSAndroid Build Coastguard Workerunder ``<target>.qemu`` (where ``<target>`` is a placeholder for a real target
117*61c4878aSAndroid Build Coastguard Workername):
118*61c4878aSAndroid Build Coastguard Worker
119*61c4878aSAndroid Build Coastguard Worker* ``executable`` - The name of the QEMU executable, e.g. ``qemu-system-arm``,
120*61c4878aSAndroid Build Coastguard Worker  ``qemusystem-riscv64``, etc.
121*61c4878aSAndroid Build Coastguard Worker* ``machine`` - The QEMU machine name. See ``qemu-system-<arch> -machine help``
122*61c4878aSAndroid Build Coastguard Worker  for a list of supported machines names.
123*61c4878aSAndroid Build Coastguard Worker
124*61c4878aSAndroid Build Coastguard WorkerThe following example is a :ref:`config fragment <module-pw_emu-config-fragments>`
125*61c4878aSAndroid Build Coastguard Workerfor a target that runs on QEMU:
126*61c4878aSAndroid Build Coastguard Worker
127*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: qemu-lm3s6965evb.json
128*61c4878aSAndroid Build Coastguard Worker
129*61c4878aSAndroid Build Coastguard Worker.. note::
130*61c4878aSAndroid Build Coastguard Worker
131*61c4878aSAndroid Build Coastguard Worker   Since this is an Arm machine the QEMU executable is defined as
132*61c4878aSAndroid Build Coastguard Worker   ``qemu-system-arm``.
133*61c4878aSAndroid Build Coastguard Worker
134*61c4878aSAndroid Build Coastguard WorkerQEMU chardevs can be exposed as host channels under
135*61c4878aSAndroid Build Coastguard Worker``<target>.qemu.channels.chardevs.<chan-name>`` where ``<chan-name>`` is
136*61c4878aSAndroid Build Coastguard Workerthe name that the channel can be accessed with (e.g. ``pw emu term
137*61c4878aSAndroid Build Coastguard Worker<chan-name>``). The ``id`` option is mandatory; it should match a valid
138*61c4878aSAndroid Build Coastguard Workerchardev ``id`` for this particular QEMU machine.
139*61c4878aSAndroid Build Coastguard Worker
140*61c4878aSAndroid Build Coastguard WorkerThe example target above emulates a `Stellaris EEVB
141*61c4878aSAndroid Build Coastguard Worker<https://www.ti.com/product/LM3S6965>`_ and is compatible with the
142*61c4878aSAndroid Build Coastguard Worker:ref:`target-lm3s6965evb-qemu` Pigweed target. The configuration defines a
143*61c4878aSAndroid Build Coastguard Worker``serial0`` channel to be the QEMU ``chardev`` with an ``id`` of ``serial0``.
144*61c4878aSAndroid Build Coastguard WorkerThe default channel type (``tcp``) is used, which is supported by all platforms.
145*61c4878aSAndroid Build Coastguard WorkerYou can change the type by adding a ``type`` key set to the desired type, e.g.
146*61c4878aSAndroid Build Coastguard Worker``pty``.
147*61c4878aSAndroid Build Coastguard Worker
148*61c4878aSAndroid Build Coastguard WorkerRenode targets
149*61c4878aSAndroid Build Coastguard Worker==============
150*61c4878aSAndroid Build Coastguard WorkerThe following example is a :ref:`config fragment <module-pw_emu-config-fragments>`
151*61c4878aSAndroid Build Coastguard Workerfor a target that runs on Renode:
152*61c4878aSAndroid Build Coastguard Worker
153*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: renode-stm32f4_discovery.json
154*61c4878aSAndroid Build Coastguard Worker
155*61c4878aSAndroid Build Coastguard WorkerThis target emulates the `ST 32F429I Discovery Kit
156*61c4878aSAndroid Build Coastguard Worker<https://www.st.com/en/evaluation-tools/32f429idiscovery.html>`_ and is
157*61c4878aSAndroid Build Coastguard Workercompatible with the :ref:`target-stm32f429i-disc1` Pigweed target.  ``machine``
158*61c4878aSAndroid Build Coastguard Workeridentifies which Renode script to use for the machine definitions. ``terminals``
159*61c4878aSAndroid Build Coastguard Workerdefines which UART devices to expose to the host. ``serial0`` exposes the serial
160*61c4878aSAndroid Build Coastguard Workerport identified as ``sysbus.usart1`` in the Renode machine script.
161*61c4878aSAndroid Build Coastguard Worker
162*61c4878aSAndroid Build Coastguard Worker-------------------
163*61c4878aSAndroid Build Coastguard WorkerRun target binaries
164*61c4878aSAndroid Build Coastguard Worker-------------------
165*61c4878aSAndroid Build Coastguard WorkerUse ``pw emu run`` to quickly run target binaries on the host. ``pw emu run``
166*61c4878aSAndroid Build Coastguard Workerstarts an emulator instance, connects to a given serial port, and then loads
167*61c4878aSAndroid Build Coastguard Workerand runs the given binary.
168*61c4878aSAndroid Build Coastguard Worker
169*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
170*61c4878aSAndroid Build Coastguard Worker
171*61c4878aSAndroid Build Coastguard Worker   $ pw emu run --args=-no-reboot qemu-lm3s6965evb \
172*61c4878aSAndroid Build Coastguard Worker         out/lm3s6965evb_qemu_gcc_size_optimized/obj/pw_snapshot/test/cpp_compile_test
173*61c4878aSAndroid Build Coastguard Worker
174*61c4878aSAndroid Build Coastguard Worker   --- Miniterm on serial0 ---
175*61c4878aSAndroid Build Coastguard Worker   --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
176*61c4878aSAndroid Build Coastguard Worker   INF  [==========] Running all tests.
177*61c4878aSAndroid Build Coastguard Worker   INF  [ RUN      ] Status.CompileTest
178*61c4878aSAndroid Build Coastguard Worker   INF  [       OK ] Status.CompileTest
179*61c4878aSAndroid Build Coastguard Worker   INF  [==========] Done running all tests.
180*61c4878aSAndroid Build Coastguard Worker   INF  [  PASSED  ] 1 test(s).
181*61c4878aSAndroid Build Coastguard Worker   --- exit ---
182*61c4878aSAndroid Build Coastguard Worker
183*61c4878aSAndroid Build Coastguard Worker.. note::
184*61c4878aSAndroid Build Coastguard Worker
185*61c4878aSAndroid Build Coastguard Worker   The ``-no-reboot`` option is passed directly to QEMU and instructs the
186*61c4878aSAndroid Build Coastguard Worker   emulator to quit instead of rebooting.
187*61c4878aSAndroid Build Coastguard Worker
188*61c4878aSAndroid Build Coastguard Worker---------
189*61c4878aSAndroid Build Coastguard WorkerDebugging
190*61c4878aSAndroid Build Coastguard Worker---------
191*61c4878aSAndroid Build Coastguard WorkerUse ``pw emu gdb`` to debug target binaries.
192*61c4878aSAndroid Build Coastguard Worker
193*61c4878aSAndroid Build Coastguard Worker.. note::
194*61c4878aSAndroid Build Coastguard Worker
195*61c4878aSAndroid Build Coastguard Worker   You always need to run an emulator instance (``pw emu start``) before
196*61c4878aSAndroid Build Coastguard Worker   starting a debug session.
197*61c4878aSAndroid Build Coastguard Worker
198*61c4878aSAndroid Build Coastguard WorkerIn the following example, ``status_test`` from ``pw_status`` is debugged. The
199*61c4878aSAndroid Build Coastguard Workerbinary was compiled for :ref:`target-lm3s6965evb-qemu`. First an emulator
200*61c4878aSAndroid Build Coastguard Workerinstance is started using the ``qemu-lm3s6965evb`` emulation target definition
201*61c4878aSAndroid Build Coastguard Workerand then the test file is loaded:
202*61c4878aSAndroid Build Coastguard Worker
203*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
204*61c4878aSAndroid Build Coastguard Worker
205*61c4878aSAndroid Build Coastguard Worker   $ pw emu start qemu-lm3s6965evb \
206*61c4878aSAndroid Build Coastguard Worker       --file out/lm3s6965evb_qemu_gcc_size_optimized/obj/pw_status/test/status_test
207*61c4878aSAndroid Build Coastguard Worker
208*61c4878aSAndroid Build Coastguard WorkerNext, a ``gdb`` session is started and connected to the emulator instance:
209*61c4878aSAndroid Build Coastguard Worker
210*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
211*61c4878aSAndroid Build Coastguard Worker
212*61c4878aSAndroid Build Coastguard Worker   $ pw emu gdb -e out/lm3s6965evb_qemu_gcc_size_optimized/obj/pw_status/test/status_test
213*61c4878aSAndroid Build Coastguard Worker   GNU gdb (Arm GNU Toolchain 12.2.MPACBTI-Rel1 ...
214*61c4878aSAndroid Build Coastguard Worker   ...
215*61c4878aSAndroid Build Coastguard Worker   Reading symbols from out/stm32f429i_disc1_debug/obj/pw_status/test/status_test.elf...
216*61c4878aSAndroid Build Coastguard Worker   Remote debugging using ::1:32979
217*61c4878aSAndroid Build Coastguard Worker   pw::sys_io::WriteByte (b=(unknown: 0x20)) at pw_sys_io_baremetal_lm3s6965evb/sys_io_baremetal.cc:117
218*61c4878aSAndroid Build Coastguard Worker   117    uart0.data_register = static_cast<uint32_t>(b);
219*61c4878aSAndroid Build Coastguard Worker   (gdb) bt
220*61c4878aSAndroid Build Coastguard Worker   #0  pw::sys_io::WriteByte (b=(unknown: 0x20)) at pw_sys_io_baremetal_lm3s6965evb/sys_io_baremetal.cc:117
221*61c4878aSAndroid Build Coastguard Worker   #1  0x00002f6a in pw::sys_io::WriteBytes (src=...) at pw_span/public/pw_span/internal/span_impl.h:408
222*61c4878aSAndroid Build Coastguard Worker   #2  0x00002eca in pw::sys_io::WriteLine (s=...) at pw_span/public/pw_span/internal/span_impl.h:264
223*61c4878aSAndroid Build Coastguard Worker   #3  0x00002f92 in operator() (log=..., __closure=0x0 <vector_table>) at pw_log_basic/log_basic.cc:87
224*61c4878aSAndroid Build Coastguard Worker   #4  _FUN () at pw_log_basic/log_basic.cc:89
225*61c4878aSAndroid Build Coastguard Worker   #5  0x00002fec in pw::log_basic::pw_Log (level=<optimized out>, flags=<optimized out>, module_name=<optimized out>, file_name=<optimized out>, line_number=95,
226*61c4878aSAndroid Build Coastguard Worker    function_name=0x6e68 "TestCaseStart", message=0x6e55 "[ RUN      ] %s.%s") at pw_log_basic/log_basic.cc:155
227*61c4878aSAndroid Build Coastguard Worker   #6  0x00002b0a in pw::unit_test::LoggingEventHandler::TestCaseStart (this=<optimized out>, test_case=...) at pw_unit_test/logging_event_handler.cc:95
228*61c4878aSAndroid Build Coastguard Worker   #7  0x00000f54 in pw::unit_test::internal::Framework::CreateAndRunTest<pw::(anonymous namespace)::Status_Strings_Test> (test_info=...)
229*61c4878aSAndroid Build Coastguard Worker    at pw_unit_test/public/pw_unit_test/internal/framework.h:266
230*61c4878aSAndroid Build Coastguard Worker   #8  0x0000254a in pw::unit_test::internal::TestInfo::run (this=0x20000280 <_pw_unit_test_Info_Status_Strings>)
231*61c4878aSAndroid Build Coastguard Worker    at pw_unit_test/public/pw_unit_test/internal/framework.h:413
232*61c4878aSAndroid Build Coastguard Worker   #9  pw::unit_test::internal::Framework::RunAllTests (this=0x20000350 <pw::unit_test::internal::Framework::framework_>) at pw_unit_test/framework.cc:64
233*61c4878aSAndroid Build Coastguard Worker   #10 0x000022b0 in main (argc=<optimized out>, argv=<optimized out>) at pw_unit_test/public/pw_unit_test/internal/framework.h:218
234*61c4878aSAndroid Build Coastguard Worker
235*61c4878aSAndroid Build Coastguard WorkerAt this point you can debug the program with ``gdb`` commands.
236*61c4878aSAndroid Build Coastguard Worker
237*61c4878aSAndroid Build Coastguard WorkerTo stop the debugging session:
238*61c4878aSAndroid Build Coastguard Worker
239*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
240*61c4878aSAndroid Build Coastguard Worker
241*61c4878aSAndroid Build Coastguard Worker   $ pw emu stop
242*61c4878aSAndroid Build Coastguard Worker
243*61c4878aSAndroid Build Coastguard Worker--------------
244*61c4878aSAndroid Build Coastguard WorkerBoot debugging
245*61c4878aSAndroid Build Coastguard Worker--------------
246*61c4878aSAndroid Build Coastguard WorkerUse the ``-p`` or ``--pause`` option when starting an emulator to debug
247*61c4878aSAndroid Build Coastguard Workerbootstrapping code:
248*61c4878aSAndroid Build Coastguard Worker
249*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
250*61c4878aSAndroid Build Coastguard Worker
251*61c4878aSAndroid Build Coastguard Worker   $ pw emu start -p qemu-lm3s6965evb \
252*61c4878aSAndroid Build Coastguard Worker         --file out/lm3s6965evb_qemu_gcc_size_optimized/obj/pw_status/test/status_test
253*61c4878aSAndroid Build Coastguard Worker
254*61c4878aSAndroid Build Coastguard WorkerThe given program loads but the emulator doesn't start executing. Next, start
255*61c4878aSAndroid Build Coastguard Workera debugging session with ``pw emu gdb``:
256*61c4878aSAndroid Build Coastguard Worker
257*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
258*61c4878aSAndroid Build Coastguard Worker
259*61c4878aSAndroid Build Coastguard Worker   $ pw emu gdb -e out/lm3s6965evb_qemu_gcc_size_optimized/obj/pw_status/test/status_test
260*61c4878aSAndroid Build Coastguard Worker   GNU gdb (Arm GNU Toolchain 12.2.MPACBTI-Rel1 ...
261*61c4878aSAndroid Build Coastguard Worker   ...
262*61c4878aSAndroid Build Coastguard Worker   Reading symbols from out/lm3s6965evb_qemu_gcc_size_optimized//obj/pw_status/test/status_test...
263*61c4878aSAndroid Build Coastguard Worker   Remote debugging using ::1:38723
264*61c4878aSAndroid Build Coastguard Worker   pw_boot_Entry () at pw_boot_cortex_m/core_init.c:122
265*61c4878aSAndroid Build Coastguard Worker   122    asm volatile("cpsid i");
266*61c4878aSAndroid Build Coastguard Worker   (gdb)
267*61c4878aSAndroid Build Coastguard Worker
268*61c4878aSAndroid Build Coastguard WorkerThe program stops at the :ref:`pw_boot_Entry() <module-pw_boot>` function. From
269*61c4878aSAndroid Build Coastguard Workerhere you can add breakpoints or step through the program with ``gdb`` commands.
270*61c4878aSAndroid Build Coastguard Worker
271*61c4878aSAndroid Build Coastguard Worker-------------------------------
272*61c4878aSAndroid Build Coastguard WorkerRun multiple emulator instances
273*61c4878aSAndroid Build Coastguard Worker-------------------------------
274*61c4878aSAndroid Build Coastguard WorkerUse the ``-i`` or ``--instance`` option to run multiple emulator instances.
275*61c4878aSAndroid Build Coastguard Worker
276*61c4878aSAndroid Build Coastguard Worker.. note::
277*61c4878aSAndroid Build Coastguard Worker
278*61c4878aSAndroid Build Coastguard Worker   Internally each emulator instance is identified by a working directory. The
279*61c4878aSAndroid Build Coastguard Worker   working directory for ``pw_emu`` is ``$PROJECT_ROOT/.pw_emu/<instance-id>``.
280*61c4878aSAndroid Build Coastguard Worker
281*61c4878aSAndroid Build Coastguard WorkerThe next example attempts to start two emulators at the same time:
282*61c4878aSAndroid Build Coastguard Worker
283*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
284*61c4878aSAndroid Build Coastguard Worker
285*61c4878aSAndroid Build Coastguard Worker   $ pw emu start qemu-lm3s6965evb --file out/lm3s6965evb_qemu_gcc_size_optimized/obj/pw_status/test/status_test
286*61c4878aSAndroid Build Coastguard Worker   $ pw emu start qemu-lm3s6965evb --file out/lm3s6965evb_qemu_gcc_size_optimized/obj/pw_status/test/status_test
287*61c4878aSAndroid Build Coastguard Worker   pigweed/.pw_emu/default: emulator already started
288*61c4878aSAndroid Build Coastguard Worker
289*61c4878aSAndroid Build Coastguard Worker
290*61c4878aSAndroid Build Coastguard WorkerThis fails because ``pw emu`` attempts to assign the same default instance
291*61c4878aSAndroid Build Coastguard WorkerID to each instance. The default ID is ``default``. To fix this, assign the
292*61c4878aSAndroid Build Coastguard Workersecond instance a custom ID:
293*61c4878aSAndroid Build Coastguard Worker
294*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
295*61c4878aSAndroid Build Coastguard Worker
296*61c4878aSAndroid Build Coastguard Worker   $ pw emu start qemu-lm3s6965evb --file out/lm3s6965evb_qemu_gcc_size_optimized/obj/pw_status/test/status_test
297*61c4878aSAndroid Build Coastguard Worker   $ pw emu -i instance2 start qemu-lm3s6965evb --file out/lm3s6965evb_qemu_gcc_size_optimized/obj/pw_status/test/status_test
298*61c4878aSAndroid Build Coastguard Worker
299*61c4878aSAndroid Build Coastguard WorkerTo stop both emulator instances:
300*61c4878aSAndroid Build Coastguard Worker
301*61c4878aSAndroid Build Coastguard Worker.. code-block:: console
302*61c4878aSAndroid Build Coastguard Worker
303*61c4878aSAndroid Build Coastguard Worker   $ pw emu stop
304*61c4878aSAndroid Build Coastguard Worker   $ pw emu stop -i instance2
305*61c4878aSAndroid Build Coastguard Worker
306*61c4878aSAndroid Build Coastguard Worker-------------------------
307*61c4878aSAndroid Build Coastguard WorkerAdding new emulator types
308*61c4878aSAndroid Build Coastguard Worker-------------------------
309*61c4878aSAndroid Build Coastguard Worker``pw_emu`` can be extended to support new emulator types by providing
310*61c4878aSAndroid Build Coastguard Workerimplementations for :py:class:`pw_emu.core.Launcher` and
311*61c4878aSAndroid Build Coastguard Worker:py:class:`pw_emu.core.Connector` in a dedicated ``pw_emu`` Python module
312*61c4878aSAndroid Build Coastguard Worker(e.g. :py:mod:`pw_emu.myemu`) or in an external Python module.
313*61c4878aSAndroid Build Coastguard Worker
314*61c4878aSAndroid Build Coastguard WorkerInternal ``pw_emu`` modules must register the connector and launcher
315*61c4878aSAndroid Build Coastguard Workerclasses by updating :py:obj:`pw_emu.pigweed_emulators.pigweed_emulators`. For
316*61c4878aSAndroid Build Coastguard Workerexample, the QEMU implementation sets the following values:
317*61c4878aSAndroid Build Coastguard Worker
318*61c4878aSAndroid Build Coastguard Worker.. code-block:: py
319*61c4878aSAndroid Build Coastguard Worker
320*61c4878aSAndroid Build Coastguard Worker   pigweed_emulators: dict[str, dict[str, str]] = {
321*61c4878aSAndroid Build Coastguard Worker     ...
322*61c4878aSAndroid Build Coastguard Worker     'qemu': {
323*61c4878aSAndroid Build Coastguard Worker       'connector': 'pw_emu.qemu.QemuConnector',
324*61c4878aSAndroid Build Coastguard Worker       'launcher': 'pw_emu.qemu.QemuLauncher',
325*61c4878aSAndroid Build Coastguard Worker      },
326*61c4878aSAndroid Build Coastguard Worker     ...
327*61c4878aSAndroid Build Coastguard Worker   }
328*61c4878aSAndroid Build Coastguard Worker
329*61c4878aSAndroid Build Coastguard WorkerFor external emulator frontend modules, ``pw_emu`` uses
330*61c4878aSAndroid Build Coastguard Worker:ref:`pigweed.json <seed-0101>` to determine the connector and launcher
331*61c4878aSAndroid Build Coastguard Workerclasses under ``pw_emu.emulators.<emulator-name>.connector`` and
332*61c4878aSAndroid Build Coastguard Worker``pw_emu.emulators:<emulator-name>.launcher``.
333*61c4878aSAndroid Build Coastguard Worker
334*61c4878aSAndroid Build Coastguard WorkerConfiguration example:
335*61c4878aSAndroid Build Coastguard Worker
336*61c4878aSAndroid Build Coastguard Worker.. code-block::
337*61c4878aSAndroid Build Coastguard Worker
338*61c4878aSAndroid Build Coastguard Worker   {
339*61c4878aSAndroid Build Coastguard Worker     "pw": {
340*61c4878aSAndroid Build Coastguard Worker       "pw_emu": {
341*61c4878aSAndroid Build Coastguard Worker         "emulators": [
342*61c4878aSAndroid Build Coastguard Worker           "myemu": {
343*61c4878aSAndroid Build Coastguard Worker             "launcher": "mypkg.mymod.mylauncher",
344*61c4878aSAndroid Build Coastguard Worker             "connector": "mypkg.mymod.myconnector",
345*61c4878aSAndroid Build Coastguard Worker           }
346*61c4878aSAndroid Build Coastguard Worker         ]
347*61c4878aSAndroid Build Coastguard Worker       }
348*61c4878aSAndroid Build Coastguard Worker     }
349*61c4878aSAndroid Build Coastguard Worker   }
350*61c4878aSAndroid Build Coastguard Worker
351*61c4878aSAndroid Build Coastguard WorkerThe :py:class:`pw_emu.core.Launcher` implementation must implement the following
352*61c4878aSAndroid Build Coastguard Workermethods:
353*61c4878aSAndroid Build Coastguard Worker
354*61c4878aSAndroid Build Coastguard Worker* :py:meth:`pw_emu.core.Launcher._pre_start`
355*61c4878aSAndroid Build Coastguard Worker* :py:class:`pw_emu.core.Launcher._post_start`
356*61c4878aSAndroid Build Coastguard Worker* :py:class:`pw_emu.core.Launcher._get_connector`
357*61c4878aSAndroid Build Coastguard Worker
358*61c4878aSAndroid Build Coastguard WorkerThere are several abstract methods that need to be implemented for the
359*61c4878aSAndroid Build Coastguard Workerconnector, like :py:meth:`pw_emu.core.Connector.reset` and
360*61c4878aSAndroid Build Coastguard Worker:py:meth:`pw_emu.core.Connector.cont`.  These are typically implemented using
361*61c4878aSAndroid Build Coastguard Workerinternal channels and :py:class:`pw_emu.core.Connector.get_channel_stream`. See
362*61c4878aSAndroid Build Coastguard Worker:py:class:`pw_emu.core.Connector` for a complete list of the abstract methods
363*61c4878aSAndroid Build Coastguard Workerthat need to be implemented.
364*61c4878aSAndroid Build Coastguard Worker
365*61c4878aSAndroid Build Coastguard Worker----------------
366*61c4878aSAndroid Build Coastguard WorkerMore pw_emu docs
367*61c4878aSAndroid Build Coastguard Worker----------------
368*61c4878aSAndroid Build Coastguard Worker.. include:: docs.rst
369*61c4878aSAndroid Build Coastguard Worker   :start-after: .. pw_emu-nav-start
370*61c4878aSAndroid Build Coastguard Worker   :end-before: .. pw_emu-nav-end
371