Common parts of GDB and GDBserver (a.k.a., The Stop the Duplication Madness! project)

This page describes the work to eliminate duplicate code of GDB and GDBserver.

1. Goal

There's a lot of code duplication between GDB and GDBserver. The project has been taking baby steps in reducing such duplication, by refactoring things, converging gdb and gdbserver's designs, and putting shared code in the shared directories. The goal of this project is to reduce the duplication as much as possible.

2. Where to put shared code?

See these past discussions:

(there were more)

The "common" moniker for a directory name isn't that great. It is a bit artificial and an artifact. It tells us that the code is used by more than one something, but it doesn't tell us really what's inside. It's not much different from saying the gdb/ is the new common and allowing gdb/gdbserver/ to refer to files under gdb/ . In the extreme, we could end up with half of gdb under common/ in a few years.

There are really two kinds of "common" stuff. There's the native target backends (ptrace and friends), but there's also the host side common stuff. If we stripped both gdb and gdbserver of their native target backends (e.g, in gdbserver, you end up with "main()", command line option processing, the event loop, the RSP marshalling, etc.., we'll still find things that are common between the two programs. The event loop is an obvious example. So we should take the opportunity to a establish a more meaningful directory structure.

So with that in mind, these are the subdirectories we're moving stuff to currently:

gdb/nat/

native target backend files. Code that interfaces with the host debug API. E.g., ptrace code, Windows debug API code, procfs code should go here.

gdb/target/

Host-independent, target vector specific code (target_ops). Currently GDB's and GDBserver's target_ops vectors are different, though ideally they'd converge.

gdb/common/

All other shared code.

3. Header files in common code (defs.h vs server.h, etc.)

There's some obvious duplication above.

The current plan is to factor out the core/base/common code to a common/common-defs.h file that declares/defines the core types and both utility functions / macros, and then both gdb's defs.h and GDBserver's server.h includes that new file as the first line. This new file will also include the two config.h files (the application's and gnulib's).

Then .c files in the shared directories include common-defs.h as the first line, files in gdb will include defs.h and files in gdbserver include server.h as the first line.

In end the number of "#ifdef GDBSERVER"s in the shared directories codebase will be precisely one (to select the appropriate gnulib config.h in common-defs.h). We'll consider eliminating that one at a later stage.

See these past discussions:

4. Where's the duplication then?

4.1. Target backends

These are the biggest duplication offenders. That is, the gdb/*-nat.c files and the gdb/gdbserver/*-low.c files accomplish pretty much the same. (Some targets do things differently enough, that they're not really duplication of code, but can be considered different implementations. The Linux support is one such case. Over the years we've been converging them though -- see the LocalRemoteFeatureParity page for more details.). GDBserver supports fewer target OSs than GDB does. Ideally, we'd finish the LocalRemoteFeatureParity project first, and then just dump the GDB-side backends. Presently, there's overlap/duplication in:

4.2. Arch-specific bits of the target backends

Independently of the local/remote parity project, however, there are bits of the backends, most prominently, the arch specific bits, that can be shared between the target backend implementations before then, as the interfaces are similar enough. E.g., code accessing hardware registers, debug registers/watchpoint support; code that detects which variant of a processor the program is running (the xml target description to send to GDB core), etc.. Sharing such arch specific code significantly reduces the effort for new ports (those usually don't need to touch common code). As is today most ports involve doing the same work twice. We should clean up some of the existing ports, laying grounds for good examples for new ports.

Move these duplicated part to nat/ directory, and come up with logically reasonable file names.

For example, the code for accessing x86 debug registers is duplicated in gdb/gdbserver/i386-low.c and gdb/i386-nat.c, so it can be moved to gdb/nat/ directory with file name i386-dregs.c and i386-dregs.h. See Refactor shared code in i386-{nat,low}.[ch]. Done in 7.9.

4.3. Terminal handling

Parts of terminal handling are duplicated.

GDB

GDBserver

comments

gdb/terminal.h

gdb/gdbserver/terminal.h

(parts)

4.4. Event loop machinery

GDB

GDBserver

comments

gdb/event-loop.c

gdb/gdbserver/event-loop.c

4.5. Build/Configure machinery

GDB

GDBserver

comments

gdb/configure.ac, etc.

gdb/gdbserver/configure.ac, etc.

Largely done

The current approach is to create Makefile fragments and m4 files in common/, which are then sourced by gdb and gdbserver's build machineries. See this thread (RFC: introduce common.m4).

Eventually we plan to move gnulib, the common code, and gdbserver to the top-level. Then the configury becomes straightforward.

Note that at one point, common/ had its own configure. See the thread here. The patches went into CVS, but were later reverted, because of various issues they caused.

4.6. Remote serial protocol

GDB

GDBserver

comments

gdb/remote.c

gdb/gdbserver/remote-utils.c

Factor out tohex/fromhex/getpkt/putpkt and more into rsp.c/h

4.7. Target stack

GDB

GDBserver

comments

gdb/target.[ch]

gdb/gdbserver/target.[ch]

Merging the entire target stack is a long-term goal.

5. Guidelines

5.1. File Naming

5.2. Split files if possible

When moving things to common/, take the opportunity to split them into smaller, more logically atomic, leaner units. E.g., that's how we ended up with ptid.h/ptid.c, instead of inferior-common.h (or some such). Avoid new kitchen sinks! Combining movement and refactoring is bad, but this is just movement: putting code into separate smaller new files is really no different from stuffing it all into one big new file.


OngoingWork

LocalRemoteFeatureParity

None: Common (last edited 2016-01-16 14:43:53 by PedroAlves)

All content (C) 2008 Free Software Foundation. For terms of use, redistribution, and modification, please see the WikiLicense page.