[V3,0/3] eBPF support

Message ID 20200703124650.15272-1-jose.marchesi@oracle.com
Headers show
Series
  • eBPF support
Related show

Message

Willgerodt, Felix via Gdb-patches July 3, 2020, 12:46 p.m.
[Changes from V2:
- Urgh, V2 contained a patch still too big for mailman.  In this
  version I am splitting the simulator part in two commits.  Thunks
  for fully generated configure files are still removed.]

Hi good peoples!

This patch series adds support for the eBPF virtual architecture to
GDB [1].

The first patch contains the basic bits to GDB in order to support the
bpf-unknown-none target.  Breakpointing and instruction
single-stepping works, but the debugging support in eBPF is still very
minimal.  This is mainly due to the many limitations imposed by the
architecture (disjoint stack, maximum stack size, etc).  We are
working to overcome these limitations, by introducing a variant called
xbpf, already supported in GCC with the -mxbpf option, whose purpose
is to ease debugging and to be used in other contexts different than
the Linux kernel, less restrictive.

The second patch adds a basic CGEN-based instruction simulator for
eBPF.  It can run many eBPF programs and works well with GDB.  A
testsuite covering the supported instructions is also included.  We
will be expanding it in order to emulate the several kernel contexts
in which eBPF programs can run, so eBPF developers can use GDB to
debug their programs without having to load them in a running kernel.
Currently the only kernel helper implemented in the simulator is
printk, which is used by the tests.

We of course commit to maintain and evolve this stuff :)

[1] Support for eBPF has been already added to both binutils and GCC.

Jose E. Marchesi (3):
  gdb: support for eBPF
  sim: eBPF simulator
  sim: generated files for the eBPF simulator

 gdb/ChangeLog                       |    10 +
 gdb/Makefile.in                     |     3 +
 gdb/bpf-tdep.c                      |   331 +
 gdb/bpf-tdep.h                      |    46 +
 gdb/configure.tgt                   |     6 +
 gdb/doc/ChangeLog                   |     6 +
 gdb/doc/gdb.texinfo                 |    21 +
 sim/ChangeLog                       |    42 +
 sim/MAINTAINERS                     |     1 +
 sim/bpf/Makefile.in                 |   205 +
 sim/bpf/aclocal.m4                  |   119 +
 sim/bpf/arch.c                      |    35 +
 sim/bpf/arch.h                      |    50 +
 sim/bpf/bpf-helpers.c               |   175 +
 sim/bpf/bpf-helpers.def             |   194 +
 sim/bpf/bpf-helpers.h               |    31 +
 sim/bpf/bpf-sim.h                   |    31 +
 sim/bpf/bpf.c                       |   327 +
 sim/bpf/config.in                   |   248 +
 sim/bpf/configure                   | 15942 ++++++++++++++++++++++++++
 sim/bpf/configure.ac                |    13 +
 sim/bpf/cpu.c                       |    69 +
 sim/bpf/cpu.h                       |    81 +
 sim/bpf/cpuall.h                    |    65 +
 sim/bpf/decode-be.c                 |  1129 ++
 sim/bpf/decode-be.h                 |    94 +
 sim/bpf/decode-le.c                 |  1129 ++
 sim/bpf/decode-le.h                 |    94 +
 sim/bpf/decode.h                    |    37 +
 sim/bpf/defs-be.h                   |   383 +
 sim/bpf/defs-le.h                   |   383 +
 sim/bpf/eng.h                       |    24 +
 sim/bpf/mloop.in                    |   165 +
 sim/bpf/sem-be.c                    |  3207 ++++++
 sim/bpf/sem-le.c                    |  3207 ++++++
 sim/bpf/sim-if.c                    |   216 +
 sim/bpf/sim-main.h                  |    51 +
 sim/bpf/traps.c                     |    33 +
 sim/configure                       |    22 +-
 sim/configure.tgt                   |     3 +
 sim/testsuite/ChangeLog             |    17 +
 sim/testsuite/configure             |    23 +-
 sim/testsuite/sim/bpf/allinsn.exp   |    26 +
 sim/testsuite/sim/bpf/alu.s         |   109 +
 sim/testsuite/sim/bpf/alu32.s       |    99 +
 sim/testsuite/sim/bpf/endbe.s       |    46 +
 sim/testsuite/sim/bpf/endle.s       |    43 +
 sim/testsuite/sim/bpf/jmp.s         |   120 +
 sim/testsuite/sim/bpf/jmp32.s       |   120 +
 sim/testsuite/sim/bpf/ldabs.s       |    87 +
 sim/testsuite/sim/bpf/mem.s         |    56 +
 sim/testsuite/sim/bpf/mov.s         |    54 +
 sim/testsuite/sim/bpf/testutils.inc |    38 +
 sim/testsuite/sim/bpf/xadd.s        |    44 +
 54 files changed, 29105 insertions(+), 5 deletions(-)
 create mode 100644 gdb/bpf-tdep.c
 create mode 100644 gdb/bpf-tdep.h
 create mode 100644 sim/bpf/Makefile.in
 create mode 100644 sim/bpf/aclocal.m4
 create mode 100644 sim/bpf/arch.c
 create mode 100644 sim/bpf/arch.h
 create mode 100644 sim/bpf/bpf-helpers.c
 create mode 100644 sim/bpf/bpf-helpers.def
 create mode 100644 sim/bpf/bpf-helpers.h
 create mode 100644 sim/bpf/bpf-sim.h
 create mode 100644 sim/bpf/bpf.c
 create mode 100644 sim/bpf/config.in
 create mode 100644 sim/bpf/configure
 create mode 100644 sim/bpf/configure.ac
 create mode 100644 sim/bpf/cpu.c
 create mode 100644 sim/bpf/cpu.h
 create mode 100644 sim/bpf/cpuall.h
 create mode 100644 sim/bpf/decode-be.c
 create mode 100644 sim/bpf/decode-be.h
 create mode 100644 sim/bpf/decode-le.c
 create mode 100644 sim/bpf/decode-le.h
 create mode 100644 sim/bpf/decode.h
 create mode 100644 sim/bpf/defs-be.h
 create mode 100644 sim/bpf/defs-le.h
 create mode 100644 sim/bpf/eng.h
 create mode 100644 sim/bpf/mloop.in
 create mode 100644 sim/bpf/sem-be.c
 create mode 100644 sim/bpf/sem-le.c
 create mode 100644 sim/bpf/sim-if.c
 create mode 100644 sim/bpf/sim-main.h
 create mode 100644 sim/bpf/traps.c
 create mode 100644 sim/testsuite/sim/bpf/allinsn.exp
 create mode 100644 sim/testsuite/sim/bpf/alu.s
 create mode 100644 sim/testsuite/sim/bpf/alu32.s
 create mode 100644 sim/testsuite/sim/bpf/endbe.s
 create mode 100644 sim/testsuite/sim/bpf/endle.s
 create mode 100644 sim/testsuite/sim/bpf/jmp.s
 create mode 100644 sim/testsuite/sim/bpf/jmp32.s
 create mode 100644 sim/testsuite/sim/bpf/ldabs.s
 create mode 100644 sim/testsuite/sim/bpf/mem.s
 create mode 100644 sim/testsuite/sim/bpf/mov.s
 create mode 100644 sim/testsuite/sim/bpf/testutils.inc
 create mode 100644 sim/testsuite/sim/bpf/xadd.s

-- 
2.25.0.2.g232378479e

Comments

Daniel Xu July 3, 2020, 8:37 p.m. | #1
Hi Jose,

On Fri Jul 3, 2020 at 5:46 AM PDT, Jose E. Marchesi via Gdb-patches wrote:
> [Changes from V2:

> - Urgh, V2 contained a patch still too big for mailman. In this

> version I am splitting the simulator part in two commits. Thunks

> for fully generated configure files are still removed.]

>

> Hi good peoples!

>

> This patch series adds support for the eBPF virtual architecture to

> GDB [1].

>

> The first patch contains the basic bits to GDB in order to support the

> bpf-unknown-none target. Breakpointing and instruction

> single-stepping works, but the debugging support in eBPF is still very

> minimal. This is mainly due to the many limitations imposed by the

> architecture (disjoint stack, maximum stack size, etc). We are

> working to overcome these limitations, by introducing a variant called

> xbpf, already supported in GCC with the -mxbpf option, whose purpose

> is to ease debugging and to be used in other contexts different than

> the Linux kernel, less restrictive.

>

> The second patch adds a basic CGEN-based instruction simulator for

> eBPF. It can run many eBPF programs and works well with GDB. A

> testsuite covering the supported instructions is also included. We

> will be expanding it in order to emulate the several kernel contexts

> in which eBPF programs can run, so eBPF developers can use GDB to

> debug their programs without having to load them in a running kernel.

> Currently the only kernel helper implemented in the simulator is

> printk, which is used by the tests.

>

> We of course commit to maintain and evolve this stuff :)

>

> [1] Support for eBPF has been already added to both binutils and GCC.


This is quite exciting for bpf developers.

Are there any instructions on how to test this out? Here's what I've
tried so far after I applied your patches:

    (gdb) file ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o
    Reading symbols from ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o...

    (gdb) run
    Starting program: /home/daniel/dev/libbpf-rs/target/bpf/runqslower.bpf.o
    zsh:1: permission denied: /home/daniel/dev/libbpf-rs/target/bpf/runqslower.bpf.o
    During startup program exited with code 126.

    (gdb) info file
    Symbols from "/home/daniel/dev/libbpf-rs/target/bpf/runqslower.bpf.o".
    Local exec file:
            `/home/daniel/dev/libbpf-rs/target/bpf/runqslower.bpf.o', file type elf64-little.
            Entry point: 0x0
            0x00000000 - 0x00000000 is .text
            0x00000000 - 0x00000080 is tp_btf/sched_wakeup
            0x00000080 - 0x00000100 is tp_btf/sched_wakeup_new
            0x00000100 - 0x00000348 is tp_btf/sched_switch
            0x00000348 - 0x000003a0 is .maps
            0x000003a0 - 0x000003a4 is license

The program I'm trying to "debug" can be found here:
https://github.com/libbpf/libbpf-rs/tree/master/example

[...]

Thanks,
Daniel
Willgerodt, Felix via Gdb-patches July 5, 2020, 12:27 a.m. | #2
Hi Daniel.

    On Fri Jul 3, 2020 at 5:46 AM PDT, Jose E. Marchesi via Gdb-patches wrote:
    > [Changes from V2:

    > - Urgh, V2 contained a patch still too big for mailman. In this

    > version I am splitting the simulator part in two commits. Thunks

    > for fully generated configure files are still removed.]

    >

    > Hi good peoples!

    >

    > This patch series adds support for the eBPF virtual architecture to

    > GDB [1].

    >

    > The first patch contains the basic bits to GDB in order to support the

    > bpf-unknown-none target. Breakpointing and instruction

    > single-stepping works, but the debugging support in eBPF is still very

    > minimal. This is mainly due to the many limitations imposed by the

    > architecture (disjoint stack, maximum stack size, etc). We are

    > working to overcome these limitations, by introducing a variant called

    > xbpf, already supported in GCC with the -mxbpf option, whose purpose

    > is to ease debugging and to be used in other contexts different than

    > the Linux kernel, less restrictive.

    >

    > The second patch adds a basic CGEN-based instruction simulator for

    > eBPF. It can run many eBPF programs and works well with GDB. A

    > testsuite covering the supported instructions is also included. We

    > will be expanding it in order to emulate the several kernel contexts

    > in which eBPF programs can run, so eBPF developers can use GDB to

    > debug their programs without having to load them in a running kernel.

    > Currently the only kernel helper implemented in the simulator is

    > printk, which is used by the tests.

    >

    > We of course commit to maintain and evolve this stuff :)

    >

    > [1] Support for eBPF has been already added to both binutils and GCC.

    
    This is quite exciting for bpf developers.

There is still a lot of work to do, but I am positive that eventually we
will achieve a good source-level debugging experience :)

    Are there any instructions on how to test this out? Here's what I've
    tried so far after I applied your patches:
    
        (gdb) file ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o
        Reading symbols from ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o...

Try with something like:

(gdb) target sim
(gdb) sim memory-size 4Mb
(gdb) load ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o
(gdb) file load ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o
Simon Marchi July 5, 2020, 1:34 a.m. | #3
On 2020-07-04 8:27 p.m., Jose E. Marchesi via Gdb-patches wrote:
> (gdb) target sim

> (gdb) sim memory-size 4Mb

> (gdb) load ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o

> (gdb) file load ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o


You probably meant `file ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o` for the last
command?

You can also save some typing by doing the `file blablabla` first, then `load` without
arguments.

Simon
Daniel Xu July 5, 2020, 3:12 a.m. | #4
On Sat Jul 4, 2020 at 6:34 PM PDT, Simon Marchi wrote:
> On 2020-07-04 8:27 p.m., Jose E. Marchesi via Gdb-patches wrote:

> > (gdb) target sim

> > (gdb) sim memory-size 4Mb

> > (gdb) load ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o

> > (gdb) file load ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o

>

> You probably meant `file ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o`

> for the last

> command?

>

> You can also save some typing by doing the `file blablabla` first, then

> `load` without

> arguments.

>

> Simon



Sorry if this is a silly question, but how do I build `sim` support into
gdb?

I've tried running the following against the top-level configure script:

```
$ ../configure --enable-sim --enable-unit-tests --enable-maintainer-mode
$ make -j4
$ make -C gdb run
make: Entering directory '/home/daniel/dev/gdb/build/gdb'
[...]

(gdb) target sim
Undefined target command: "sim".  Try "help target".

```

Thanks,
Daniel
Simon Marchi July 5, 2020, 3:20 a.m. | #5
On 2020-07-04 11:12 p.m., Daniel Xu wrote:
> Sorry if this is a silly question, but how do I build `sim` support into

> gdb?

> 

> I've tried running the following against the top-level configure script:

> 

> ```

> $ ../configure --enable-sim --enable-unit-tests --enable-maintainer-mode

> $ make -j4

> $ make -C gdb run

> make: Entering directory '/home/daniel/dev/gdb/build/gdb'

> [...]

> 

> (gdb) target sim

> Undefined target command: "sim".  Try "help target".


The sim is built when GDB & co are configured with a suitable --target (a target for
which there is a sim).  If you look at Jose's patches, they modify the configure.tgt
files of gdb/ and sim/ to recognize target triplets of the form `bpf-*-*`.  According
to this:

  https://lwn.net/Articles/800606/

the triplet to use is `bpf-unknown-none` (and there's even a nice picture!).  So, try
adding `--target=bpf-unknown-none` to your configure line.

Simon
Willgerodt, Felix via Gdb-patches July 5, 2020, 10:18 a.m. | #6
Hi Simon.

    On 2020-07-04 8:27 p.m., Jose E. Marchesi via Gdb-patches wrote:
    > (gdb) target sim

    > (gdb) sim memory-size 4Mb

    > (gdb) load ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o

    > (gdb) file load ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o

    
    You probably meant `file
    ~/dev/libbpf-rs/target/bpf/runqslower.bpf.o` for the last command?

Yeah that was a typo.
    
    You can also save some typing by doing the `file blablabla` first, then `load` without
    arguments.

Oh nice, I didn't know that! :D