Differences between revisions 29 and 30
Revision 29 as of 2014-07-25 11:43:14
Size: 9058
Editor: PedroAlves
Comment: Add 'Header files in common code' section
Revision 30 as of 2014-07-31 17:50:52
Size: 9064
Editor: TomTromey
Comment: rsp task done
Deletions are marked like this. Additions are marked like this.
Line 119: Line 119:
|| gdb/remote.c || gdb/gdbserver/remote-utils.c || Factor out tohex/fromhex/getpkt/putpkt and more into rsp.c/h || || gdb/remote.c || gdb/gdbserver/remote-utils.c || --(Factor out tohex/fromhex/getpkt/putpkt and more into rsp.c/h)-- ||

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:


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.


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


All other shared code.

We could certainly do better, but that's where we're at currently.

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

  • GDB has a defs.h file that includes declarations and definitions that end up necessary throughout most of GDB. This includes both core types and utility functions / macros, and GDB-the-application specific bits (that are of no interest to base/common code).
  • GDBserver has a server.h file that includes declarations and definitions that end up necessary throughout most of GDBserver. This includes both core types and utility functions / macros, and also GDBserver-the-application specific bits (that are of no interest to base/common code).

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:

  • Linux
    • See the LocalRemoteFeatureParity page for more details.









      ... and related arch specific files ...

  • Windows
    • GDB




      ... and related arch specific files ...

  • NTO
    • GDB




      ... and related arch specific files ...

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.







4.4. Event loop machinery






4.5. Build/Configure machinery




gdb/configure.ac, etc.

gdb/gdbserver/configure.ac, etc.

So far, we have three possible approaches to address this:

  1. Don't have its own build stuff in common/, let gdb or gdbserver pick the pieces they want, and duplicate the necessary autoconf checks, etc.. This is the status quo.
  2. Make common/ have its own configure. In March 2011, Yao gave a try to build common/ as a library (libcommon.a), for both GDB and GDBserver to link. See the thread here. The patches went into CVS, but were later reverted, because of various issues they caused.

  3. Create Makefile fragments and m4 files in common/, then sourced by gdb and gdbserver's build machineries. No new configure. This is the direction we're heading towards now. See this thread (RFC: introduce common.m4).

  4. Move gnulib, the common code, and gdbserver to the top-level. Then the configury becomes straightforward.

4.6. Remote serial protocol






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

5. Guidelines

5.1. File Naming

  • Avoid "common" in file names in files under common/. Instead, name the files for what they contain, not for the fact that they're "common" to two programs (gdb, gdbserver) presently. If the resulting file is just a dumping ground of miscellaneous things, then call it that. Say, foo-misc.h or foo-defs.h, not foo-common.h. But ... (see below)

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.



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.