1Testing LLDB using QEMU 2======================= 3 4QEMU system mode emulation 5-------------------------- 6 7QEMU can be used to test LLDB in an emulation environment in the absence of 8actual hardware. This page describes instructions to help setup a QEMU emulation 9environment for testing LLDB. 10 11The scripts under llvm-project/lldb/scripts/lldb-test-qemu can quickly help 12setup a virtual LLDB testing environment using QEMU. The scripts currently work 13with Arm or AArch64, but support for other architectures can be added easily. 14 15* **setup.sh** is used to build the Linux kernel image and QEMU system emulation executable(s) from source. 16* **rootfs.sh** is used to generate Ubuntu root file system images to be used for QEMU system mode emulation. 17* **run-qemu.sh** utilizes QEMU to boot a Linux kernel image with a root file system image. 18 19Once we have booted our kernel we can run lldb-server in emulation environment. 20Ubuntu Bionic/Focal x86_64 host was used to test these scripts instructions in this 21document. Please update it according to your host distribution/architecture. 22 23.. note:: 24 Instructions on this page and QEMU helper scripts are verified on a Ubuntu Bionic/Focal (x86_64) host. Moreover, scripts require sudo/root permissions for installing dependencies and setting up QEMU host/guest network. 25 26Given below are some examples of common use-cases of LLDB QEMU testing 27helper scripts: 28 29Create Ubuntu root file system image for QEMU system emulation with rootfs.sh 30-------------------------------------------------------------------------------- 31 32**Example:** generate Ubuntu Bionic (armhf) rootfs image of size 1 GB 33:: 34 35 $ bash rootfs.sh --arch armhf --distro bionic --size 1G 36 37**Example:** generate Ubuntu Focal (arm64) rootfs image of size 2 GB 38:: 39 40 $ bash rootfs.sh --arch arm64 --distro focal --size 2G 41 42rootfs.sh has been tested for generating Ubuntu Bionic and Focal images but they can be used to generate rootfs images of other Debian Linux distribution. 43 44rootfs.sh defaults username of generated image to your current username on host computer. 45 46 47Build QEMU or cross compile Linux kernel from source using setup.sh 48----------------------------------------------------------------------- 49 50**Example:** Build QEMU binaries and Arm/AArch64 Linux kernel image 51:: 52 53$ bash setup.sh --qemu --kernel arm 54$ bash setup.sh --qemu --kernel arm64 55 56**Example:** Build Linux kernel image only 57:: 58 59$ bash setup.sh --kernel arm 60$ bash setup.sh --kernel arm64 61 62**Example:** Build qemu-system-arm and qemu-system-aarch64 binaries. 63:: 64 65$ bash setup.sh --qemu 66 67**Example:** Remove qemu.git, linux.git and linux.build from working directory 68:: 69 70$ bash setup.sh --clean 71 72 73Run QEMU Arm or AArch64 system emulation using run-qemu.sh 74---------------------------------------------------------- 75run-qemu.sh has following dependencies: 76 77* Follow https://wiki.qemu.org/Documentation/Networking/NAT and set up bridge 78 networking for QEMU. 79 80* Make sure /etc/qemu-ifup script is available with executable permissions. 81 82* QEMU binaries must be built from source using setup.sh or provided via --qemu 83 commandline argument. 84 85* Linux kernel image must be built from source using setup.sh or provided via 86 --kernel commandline argument. 87 88* linux.build and qemu.git folder must be present in current directory if 89 setup.sh was used to build Linux kernel and QEMU binaries. 90 91* --sve option will enable AArch64 SVE mode. 92 93* --sme option will enable AArch64 SME mode (SME requires SVE, so this will also 94 be enabled). 95 96* --mte option will enable AArch64 MTE (memory tagging) mode 97 (can be used on its own or in addition to --sve). 98 99 100**Example:** Run QEMU Arm or AArch64 system emulation using run-qemu.sh 101:: 102 103 $ sudo bash run-qemu.sh --arch arm --rootfs <path of rootfs image> 104 $ sudo bash run-qemu.sh --arch arm64 --rootfs <path of rootfs image> 105 106**Example:** Run QEMU with kernel image and qemu binary provided using commandline 107:: 108 109 $ sudo bash run-qemu.sh --arch arm64 --rootfs <path of rootfs image> \ 110 --kernel <path of Linux kernel image> --qemu <path of QEMU binary> 111 112 113Steps for running lldb-server in QEMU system emulation environment 114------------------------------------------------------------------ 115 116Using Bridge Networking 117*********************** 118 119* Make sure bridge networking is enabled between host machine and QEMU VM 120 121* Find out ip address assigned to eth0 in emulation environment 122 123* Setup ssh access between host machine and emulation environment 124 125* Login emulation environment and install dependencies 126 127:: 128 129 $ sudo apt install python-dev libedit-dev libncurses5-dev libexpat1-dev 130 131* Cross compile LLDB server for AArch64 Linux: Please visit https://lldb.llvm.org/resources/build.html for instructions on how to cross compile LLDB server. 132 133* Transfer LLDB server executable to emulation environment 134 135:: 136 137 $ scp lldb-server username@ip-address-of-emulation-environment:/home/username 138 139* Run lldb-server inside QEMU VM 140 141* Try connecting to lldb-server running inside QEMU VM with selected ip:port 142 143Without Bridge Networking 144************************* 145 146Without bridge networking you will have to forward individual ports from the VM 147to the host (refer to QEMU's manuals for the specific options). 148 149* At least one to connect to the intial ``lldb-server``. 150* One more if you want to use ``lldb-server`` in ``platform mode``, and have it 151 start a ``gdbserver`` instance for you. 152 153If you are doing either of the latter 2 you should also restrict what ports 154``lldb-server tries`` to use, otherwise it will randomly pick one that is almost 155certainly not forwarded. An example of this is shown below. 156 157:: 158 159 $ lldb-server plaform --server --listen 0.0.0.0:54321 --gdbserver-port 49140 160 161The result of this is that: 162 163* ``lldb-server`` platform mode listens externally on port ``54321``. 164 165* When it is asked to start a new gdbserver mode instance, it will use the port 166 ``49140``. 167 168Your VM configuration should have ports ``54321`` and ``49140`` forwarded for 169this to work. 170