This file documents the internals of the GNU debugger GDB.

Copyright (C) 1990, 1991 Free Software Foundation, Inc. Contributed by Cygnus Support.  Written by John Gilmore.

Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy or distribute modified versions of this manual under the terms of the GPL (for which purpose this text may be regarded as a program in the language TeX).

Cleanups
********

Cleanups are a structured way to deal with things that need to be done later.  When your code does something (like malloc some memory, or open a file) that needs to be undone later (e.g. free the memory or close the file), it can make a cleanup.  The cleanup will be done at some future point:  when the command is finished, when an error occurs, or when your code decides it's time to do cleanups.

You can also discard cleanups, that is, throw them away without doing what they say.  This is only done if you ask that it be done.

Syntax:

OLD_CHAIN = make_cleanup (FUNCTION, ARG);
     Make a cleanup which will cause FUNCTION to be called with ARG (a char *) later.  The result, OLD_CHAIN, is a handle that can be passed to do_cleanups or discard_cleanups later.  Unless you are going to call do_cleanups or discard_cleanups yourself, you can ignore the result from make_cleanup.

do_cleanups (OLD_CHAIN);
     Perform all cleanups done since make_cleanup returned OLD_CHAIN. E.g.:

          make_cleanup (a, 0);
          old = make_cleanup (b, 0);
          do_cleanups (old);

     will call b() but will not call a().  The cleanup that calls a() will remain in the cleanup chain, and will be done later unless otherwise discarded.

discard_cleanups (OLD_CHAIN);
     Same as do_cleanups except that it just removes the cleanups from the chain and does not call the specified functions.

Some functions, e.g. fputs_filtered() or error(), specify that they ``should not be called when cleanups are not in place''.  This means that any actions you need to reverse in the case of an error or interruption must be on the cleanup chain before you call these functions, since they might never return to your code (they longjmp instead).

Wrapping output lines
*********************

Output that goes through printf_filtered or fputs_filtered or fputs_demangled needs only to have calls to wrap_here() added in places that would be good breaking points.  The utility routines will take care of actually wrapping if the line width is exceeded.

The argument to wrap_here() is an indentation string which is printed ONLY if the line breaks there.  This argument is saved away and used later.  It must remain valid until the next call to wrap_here() or until a newline has been printed through the *_filtered functions. Don't pass in a local variable and then return!

It is usually best to call wrap_here() after printing a comma or space. If you call it before printing a space, make sure that your indentation properly accounts for the leading space that will print if the line wraps there.

Any function or set of functions that produce filtered output must finish by printing a newline, to flush the wrap buffer, before switching to unfiltered ("printf") output.  Symbol reading routines that print warnings are a good example.

Configuring GDB for release
***************************

GDB should be released after doing ./configure none in the top level directory.  This will leave a makefile there, but no tm- or xm- files. The makefile is needed, for example, for make gdb.tar.Z...  If you have tm- or xm-files in the main source directory, C's include rules cause them to be used in preference to tm- and xm-files in the subdirectories where the user will actually configure and build the binaries.

./configure none is also a good way to rebuild the top level Makefile after changing Makefile.in, alldeps.mak, etc.

TEMPORARY RELEASE PROCEDURE FOR DOCUMENTATION gdb.texinfo is currently marked up using the texinfo-2 macros, which are not yet a default for anything (but we have to start using them sometime).

For making paper, the only thing this implies is the right generation of texinfo.tex needs to be included in the distribution.

For making info files, however, rather than duplicating the texinfo2 distribution, generate gdb.texinfo locally, and include the files gdb.info* in the distribution.  Note the plural; M-x texinfo-format-buffer will split the document into one overall file and five or so include files.

The README file
***************

Check the README file, it often has useful information that does not appear anywhere else in the directory.

Defining a new host or target architecture
******************************************

When building support for a new host and/or target, this will help you organize where to put the various parts.  ARCH stands for the architecture involved.

Object files needed when the host system is an ARCH are listed in the file xconfig/ARCH, in the Makefile macro XDEPFILES = ....  The header file that defines the host system should be called xm-ARCH.h, and should be specified as the value of XM_FILE= in xconfig/ARCH. You can also define CC, REGEX and REGEX1, SYSV_DEFINE, XM_CFLAGS, etc in there; see Makefile.in.

There are some ``generic'' versions of routines that can be used by various host systems.  If these routines work for the ARCH host, you can just include the generic file's name (with .o, not .c) in XDEPFILES.  Otherwise, you will need to write routines that perform the same functions as the generic file, put them into ARCH-xdep.c, and put ARCH-xdep.o into XDEPFILES.  These generic host support files include:

     	coredep.c, coredep.o

fetch_core_registers()
     Support for reading registers out of a core file.  This routine calls register_addr(), see below.

register_addr()
     If your xm-ARCH.h file defines the macro REGISTER_U_ADDR(reg) to be the offset within the user struct of a register (represented as a GDB register number), coredep.c will define the register_addr() function and use the macro in it.  If you do not define REGISTER_U_ADDR, but you are using the standard fetch_core_registers, you will need to define your own version of register_addr, put it into your ARCH-xdep.c file, and be sure ARCH-xdep.o is in the XDEPFILES list.  If you have your own fetch_core_registers, you only need to define register_addr if your fetch_core_registers calls it.  Many custom fetch_core_registers implementations simply locate the registers themselves.

Object files needed when the target system is an ARCH are listed in the file tconfig/ARCH, in the Makefile macro TDEPFILES = ....  The header file that defines the target system should be called tm-ARCH.h, and should be specified as the value of TM_FILE in tconfig/ARCH. You can also define TM_CFLAGS, TM_CLIBS, and TM_CDEPS in there; see Makefile.in.

Similar generic support files for target systems are:

     	exec.c, exec.o:

This file defines functions for accessing files that are executable on the target system.  These functions open and examine an exec file, extract data from one, write data to one, print information about one, etc.  Now that executable files are handled with BFD, every architecture should be able to use the generic exec.c rather than its own custom code.

Binary File Descriptor library support for GDB
**********************************************

BFD provides support for GDB in several ways:

identifying executable and core files
     BFD will identify a variety of file types, including a.out, coff, and several variants thereof, as well as several kinds of core files.

access to sections of files
     BFD parses the file headers to determine the names, virtual addresses, sizes, and file locations of all the various named sections in files (such as the text section or the data section).  GDB simply calls BFD to read or write section X at byte offset Y for length Z.

specialized core file support
     BFD provides routines to determine the failing command name stored in a core file, the signal with which the program failed, and whether a core file matches (i.e. could be a core dump of) a particular executable file.

locating the symbol information
     GDB uses an internal interface of BFD to determine where to find the symbol information in an executable file or symbol-file.  GDB itself handles the reading of symbols, since BFD does not ``understand'' debug symbols, but GDB uses BFD's cached information to find the symbols, string table, etc.

The interface for symbol reading is described in see Symbol Reading.

What is considered ``host-dependent'' versus ``target-dependent''?
******************************************************************

The xconfig/*, xm-*.h and *-xdep.c files are for host support.  The question is, what features or aspects of a debugging or cross-debugging environment are considered to be ``host'' support.

Defines and include files needed to build on the host are host support. Examples are tty support, system defined types, host byte order, host float format.

Unix child process support is considered an aspect of the host.  Since when you fork on the host you are still on the host, the various macros needed for finding the registers in the upage, running ptrace, and such are all in the host-dependent files.

This is still somewhat of a grey area; I (John Gilmore) didn't do the xm- and tm- split for gdb (it was done by Jim Kingdon) so I have had to figure out the grounds on which it was split, and make my own choices as I evolve it.  I have moved many things out of the xdep files actually, partly as a result of BFD and partly by removing duplicated code.

Adding a New Host
=================

There are two halves to making GDB work on a new machine.  First, you have to make it host on the new machine (compile there, handle that machine's terminals properly, etc).  If you will be cross-debugging to some other kind of system, you are done.

(If you want to use GDB to debug programs that run on the new machine, you have to get it to understand the machine's object files, symbol files, and interfaces to processes.  see New Target.)

Most of the work in making GDB compile on a new machine is in specifying the configuration of the machine.  This is done in a dizzying variety of header files and configuration scripts, which we hope to make more sensible soon.  Let's say your new host is called an XXX (e.g. sun4), and its full three-part configuration name is XARCH-XVEND-XOS (e.g. sparc-sun-sunos4).  In particular:

At the top level, edit config.sub and add XARCH, XVEND, and XOS to the lists of supported architectures, vendors, and operating systems near the bottom of the file.  Also, add XXX as an alias that maps to XARCH-XVEND-XOS.  You can test your changes by running

./config.sub XXX and	./config.sub XARCH-XVEND-XOS

which should both respond with XARCH-XVEND-XOS and no error messages.

Then edit include/sysdep.h.  Add a new #define for XXX_SYS, with a numeric value not already in use.  Add a new section that says

#if HOST_SYS==XXX_SYS #include <sys/h-XXX.h> #endif

Now create a new file include/sys/h-XXX.h.  Examine the other h-*.h files as templates, and create one that brings in the right include files for your system, and defines any host-specific macros needed by GDB.

Now, go to the bfd directory and edit bfd/configure.in.  Add shell script code to recognize your XARCH-XVEND-XOS configuration, and set bfd_host to XXX when you recognize it.  Now create a file bfd/config/hmake-XXX, which includes the line:

HDEFINES=-DHOST_SYS=XXX_SYS

(If you have the binutils in the same tree, you'll have to do the same thing to in the binutils directory as you've done in the bfd directory.)

It's likely that the libiberty and readline directories won't need any changes for your configuration, but if they do, you can change the configure.in file there to recognize your system and map to an hmake-XXX file.  Then add hmake-XXX to the config/ subdirectory, to set any makefile variables you need.  The only current options in there are things like -DSYSV.

Aha!  Now to configure GDB itself!  Modify gdb/configure.in to recognize your system and set gdb_host to XXX.  Add a file gdb/xconfig/XXX which specifies XDEPFILES=(whatever is needed), and XM_FILE= xm-XXX.h.  Create gdb/xm-XXX.h with the appropriate #define's for your system (crib from existing xm-*.h files). If your machine needs custom support routines, you can put them in a file gdb/XXX-xdep.c, and add XXX-xdep.o to the XDEPFILES= line.  If not, you can use the generic routines for ptrace support (infptrace.o) and core files (coredep.o).  These can be customized in various ways by macros defined in your xm-XXX.h file.

Now, from the top level (above bfd, gdb, etc), run:

./configure -template=./configure

This will rebuild all your configure scripts, using the new configure.in files that you modified.  (You can also run this command at any subdirectory level.)  You are now ready to try configuring GDB to compile for your system.  Do:

./configure XXX +target=vxworks960

This will configure your system to cross-compile for VxWorks on the Intel 960, which is probably not what you really want, but it's a test case that works at this stage.  (You haven't set up to be able to debug programs that run on XXX yet.)

If this succeeds, you can try building it all with:

make

Good luck!  Comments and suggestions about this section are particularly welcome; send them to bug-gdb@prep.ai.mit.edu.

When hosting GDB on a new operating system, to make it possible to debug core files, you will need to either write specific code for parsing your OS's core files, or customize bfd/trad-core.c.   First, use whatever #include files your machine uses to define the struct of registers that is accessible (possibly in the upage) in a core file (rather than <machine/reg.h>), and an include file that defines whatever header exists on a core file (e.g. the u-area or a "struct core").  Then modify trad_unix_core_file_p to use these values to set up the section information for the data segment, stack segment, any other segments in the core file (perhaps shared library contents or control information), "registers" segment, and if there are two discontiguous sets of registers (e.g.  integer and float), the "reg2" segment.  This section information basically delimits areas in the core file in a standard way, which the section-reading routines in BFD know how to seek around in.

Then back in GDB, you need a matching routine called fetch_core_registers. If you can use the generic one, it's in core-dep.c; if not, it's in your foobar-xdep.c file.  It will be passed a char pointer to the entire "registers" segment, its length, and a zero; or a char pointer to the entire "regs2" segment, its length, and a 2.  The routine should suck out the supplied register values and install them into gdb's "registers" array.  (see New Architectures for more info about this.)

Adding a New Target
===================

When adding support for a new target machine, there are various areas of support that might need change, or might be OK.

If you are using an existing object file format (a.out or COFF), there is probably little to be done.  See bfd/doc/bfd.texinfo for more information on writing new a.out or COFF versions.

If you need to add a new object file format, you are beyond the scope of this document right now.  Look at the structure of the a.out and COFF support, build a transfer vector (xvec) for your new format, and start populating it with routines.  Add it to the list in bfd/targets.c.

If you are adding a new existing CPU chip (e.g. m68k family), you'll need to define an XARCH-opcode.h file, a tm-XARCH.h file that gives the basic layout of the chip (registers, stack, etc), probably an XARCH-tdep.c file that has support routines for tm-XARCH.h, etc.

If you are adding a new operating system for an existing CPU chip, add a tm-XOS.h file that describes the operating system facilities that are unusual (extra symbol table info; the breakpoint instruction needed; etc).  Then write a tm-XARCH-XOS.h that just #include's tm-XARCH.h and tm-XOS.h.  (Now that we have three-part configuration names, this will probably get revised to separate the OS configuration from the ARCH configuration.  FIXME.)

Extending configure
===================

Once you have added a new host, target, or both, you'll also need to extend the configure script to recognize the new configuration possibilities.

You shouldn't edit the configure script itself to add hosts or targets; instead, edit the script fragments in the file configure.in.  To handle new hosts, modify the segment after the comment # per-host; to handle new targets, modify after # per-target.

Then fold your changes into the configure script by using the +template option, and specifying configure itself as the template:

     configure +template=configure

Symbol Reading
**************

GDB reads symbols from "symbol files".  The usual symbol file is the file containing the program which gdb is debugging.  GDB can be directed to use a different file for symbols (with the ``symbol-file'' command), and it can also read more symbols via the ``add-file'' and ``load'' commands, or while reading symbols from shared libraries.

Symbol files are initially opened by symfile.c using the BFD library.  BFD identifies the type of the file by examining its header. symfile_init then uses this identification to locate a set of symbol-reading functions.

Symbol reading modules identify themselves to GDB by calling add_symtab_fns during their module initialization.  The argument to add_symtab_fns is a struct sym_fns which contains the name (or name prefix) of the symbol format, the length of the prefix, and pointers to four functions.  These functions are called at various times to process symbol-files whose identification matches the specified prefix.

The functions supplied by each module are:

XXX_symfile_init(struct sym_fns *sf)

     Called from symbol_file_add when we are about to read a new symbol file.  This function should clean up any internal state (possibly resulting from half-read previous files, for example) and prepare to read a new symbol file. Note that the symbol file which we are reading might be a new "main" symbol file, or might be a secondary symbol file whose symbols are being added to the existing symbol table.

     The argument to XXX_symfile_init is a newly allocated struct sym_fns whose bfd field contains the BFD for the new symbol file being read.  Its private field has been zeroed, and can be modified as desired.  Typically, a struct of private information will be malloc'd, and a pointer to it will be placed in the private field.

     There is no result from XXX_symfile_init, but it can call error if it detects an unavoidable problem.

XXX_new_init()

     Called from symbol_file_add when discarding existing symbols. This function need only handle the symbol-reading module's internal state; the symbol table data structures visible to the rest of GDB will be discarded by symbol_file_add.  It has no arguments and no result. It may be called after XXX_symfile_init, if a new symbol table is being read, or may be called alone if all symbols are simply being discarded.

XXX_symfile_read(struct sym_fns *sf, CORE_ADDR addr, int mainline)

     Called from symbol_file_add to actually read the symbols from a symbol-file into a set of psymtabs or symtabs.

     sf points to the struct sym_fns originally passed to XXX_sym_init for possible initialization.  addr is the offset between the file's specified start address and its true address in memory.  mainline is 1 if this is the main symbol table being read, and 0 if a secondary symbol file (e.g. shared library or dynamically loaded file) is being read.

In addition, if a symbol-reading module creates psymtabs when XXX_symfile_read is called, these psymtabs will contain a pointer to a function XXX_psymtab_to_symtab, which can be called from any point in the GDB symbol-handling code.

XXX_psymtab_to_symtab (struct partial_symtab *pst)

     Called from psymtab_to_symtab (or the PSYMTAB_TO_SYMTAB macro) if the psymtab has not already been read in and had its pst->symtab pointer set.  The argument is the psymtab to be fleshed-out into a symtab.  Upon return, pst->readin should have been set to 1, and pst->symtab should contain a pointer to the new corresponding symtab, or zero if there were no symbols in that part of the symbol file.