doc/GDB.md
author Jan Vrany <jan.vrany@labware.com>
Thu, 07 Dec 2023 12:33:31 +0000
changeset 322 1b26d0a9560c
parent 216 800057dbad30
permissions -rw-r--r--
Emit and handle (custom) `-register-changed` notification This commit adds new (custom) asynchronous notification about register value being changed. Standard GDB does not notify MI clients about register value being changed when debugging (for example, by CLI command `set $rax = 1` or via Python's `Value.assign()`). This caused libgdb's register value cache being out of sync. In the past, this was partially worked around by manually emiting the notification on `GDBRegisterWithValue` APIs, but this did not (and could not) handle the case register was changed from GDB command line. To solve this problem, this commit installs a custom Python event handler that emits new GDB/MI notification - `-register-changed` - whenever a register changes after debugee is stopped. This has been enabled by upstream GDB commit 4825fd "gdb/python: implement support for sending custom MI async notifications" On libgdbs side, complete inferior state is invalidated. In theory, one could carefully invalidate only the changed `GDBRegisterWithValue` but in certain cases this could also change the backtrace (for example, if one updates stack pointer) or position in code. So it seems safer to just invalidate everything.

# Building GDB for use with *libgdbs*

As of today, *libgdbs* require a very recent version of GDB. This is because
*libgdbs* requires various patches. Some of them are are already upstreamed
to [GDB's master branch][1], some of them are in process.

Therefore you may need to compile suitable GDB yourself. A git repository with
all the patches can be found at [https://github.com/janvrany/binutils-gdb][2]
*in branch `users/jv/vdb`* (note the branch!)

## Linux

### Preparing build environment

* On *Debian Buster* (tested on as of 2018-11-26)
  ```
  apt-get install build-essential texinfo python3-dev flex bison libexpat-dev libsource-highlight-dev
  ```

* On *Ubuntu 18.04* (tested on as of 2019-03-20)
  ```
  apt-get install g++  make texinfo python3-dev flex bison libfl2 libexpat1-dev
  ```

### Compiling GDB

    git clone --depth=1 https://github.com/janvrany/binutils-gdb
    cd binutils-gdb
    ./configure \
        --disable-werror \
        --with-guile=no \
        --with-python=/usr/bin/python3 \
        --enable-targets="i686-pc-linux-gnu,x86_64-pc-linux-gnu,i686-w64-mingw32,x86_64-w64-mingw32,riscv64-pc-linux-gnu,ppc-pc-linux-gnu"
    make -j4


You may want to tweak target list to fot your needs, the above list includes
both i686 and x86_64 Linux, both i686 and x86_64 Windows,PowerPC and RISC-V 64
Linux.

## Windows using MSYS2 and MINGW64

Following recipe has been tested on Windows 7 and Windows 10 using [MSYS2][3]
and `MinGW 64`.

### Preparing build environment

In order to build GDB from source, you need to install [MSYS2][3] and `MinGW 64`
toolchain:

 * Install [MSYS2][3] from [https://msys2.github.io][3]. Make sure to install
   it to `C:\MSYS64`. Once installer finishes, update packages:
   ```
   C:\msys64\usr\bin\pacman.exe -Sy pacman
   C:\msys64\usr\bin\pacman.exe -Syu
   C:\msys64\usr\bin\pacman.exe -Su
   ```
   Note: during `pacman -Syu` you maybe asked to close command prompt window.
   Don't forget to close it otherwise you will have to start from beginning.

 * Add `C:\msys64\usr\bin` to your `PATH`:
   ```
   setx PATH "%PATH%;c:\msys64\usr\bin"
   ```

 * Install `MinGW 64` toolchain and other dev tools:
   ```
   pacman -S git bison flex texinfo mingw-w64-x86_64-gcc mingw-w64-x86_64-SDL mingw-w64-x86_64-libexpat mingw-w64-x86_64-python3
   ```

### Compiling GDB

    git clone --depth=1 https://github.com/janvrany/binutils-gdb
    cd binutils-gdb
    set "PATH=%PATH%;C:\msys64\mingw64\bin"
    bash configure --build=x86_64-w64-mingw32 --disable-werror --with-system-readline --with-guile=no --with-python=C:\msys64\mingw64\bin\python3 --enable-targets="i686-w64-mingw32,x86_64-w64-mingw32"
    make -j4

If you don't have `C:\msys64\mingw64\bin` in your `PATH` (it is *NOT* by default),
then you may need to copy required libraries along `gdb.exe` otherwise GDB won't
start:

    set "PATH=%PATH%;C:\msys64\mingw64\bin"
    bash -c "for f in gdb/gdb.exe | grep MSYS | cut -d ' ' -f 3); do cp $f gdb ; done"

[1]: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=summary
[2]: https://github.com/janvrany/binutils-gdb
[3]: https://msys2.github.io/