516341f714e3a31db8b83fe7e270eb46dad5259a
[deliverable/binutils-gdb.git] / gdb / doc / gdbint.texinfo
1 \input texinfo
2 @setfilename gdb-internals
3 @c $Id$
4 @ifinfo
5 This file documents the internals of the GNU debugger GDB.
6
7 Copyright (C) 1990, 1991 Free Software Foundation, Inc.
8 Contributed by Cygnus Support. Written by John Gilmore.
9
10 Permission is granted to make and distribute verbatim copies of
11 this manual provided the copyright notice and this permission notice
12 are preserved on all copies.
13
14 @ignore
15 Permission is granted to process this file through Tex and print the
16 results, provided the printed document carries copying permission
17 notice identical to this one except for the removal of this paragraph
18 (this paragraph not being relevant to the printed manual).
19
20 @end ignore
21 Permission is granted to copy or distribute modified versions of this
22 manual under the terms of the GPL (for which purpose this text may be
23 regarded as a program in the language TeX).
24 @end ifinfo
25
26 @setchapternewpage odd
27 @settitle GDB Internals
28 @titlepage
29 @title{Working in GDB}
30 @subtitle{A guide to the internals of the GNU debugger}
31 @author John Gilmore
32 @author Cygnus Support
33 @page
34 @tex
35 \def\$#1${{#1}} % Kluge: collect RCS revision info without $...$
36 \xdef\manvers{\$Revision$} % For use in headers, footers too
37 {\parskip=0pt
38 \hfill Cygnus Support\par
39 \hfill \manvers\par
40 \hfill \TeX{}info \texinfoversion\par
41 }
42 @end tex
43
44 @vskip 0pt plus 1filll
45 Copyright @copyright{} 1990, 1991 Free Software Foundation, Inc.
46
47 Permission is granted to make and distribute verbatim copies of
48 this manual provided the copyright notice and this permission notice
49 are preserved on all copies.
50
51 @end titlepage
52
53 @node Top, Cleanups, (dir), (dir)
54
55 @menu
56 * Cleanups:: Cleanups
57 * Wrapping:: Wrapping output lines
58 * Releases:: Configuring GDB for release
59 * README:: The README file
60 * New Architectures:: Defining a new host or target architecture
61 * BFD support for GDB:: How BFD and GDB interface
62 * Host versus Target:: What features are in which files
63 * Symbol Reading:: Defining new symbol readers
64 @end menu
65
66 @node Cleanups, Wrapping, Top, Top
67 @chapter Cleanups
68
69 Cleanups are a structured way to deal with things that need to be done
70 later. When your code does something (like malloc some memory, or open
71 a file) that needs to be undone later (e.g. free the memory or close
72 the file), it can make a cleanup. The cleanup will be done at some
73 future point: when the command is finished, when an error occurs, or
74 when your code decides it's time to do cleanups.
75
76 You can also discard cleanups, that is, throw them away without doing
77 what they say. This is only done if you ask that it be done.
78
79 Syntax:
80
81 @table @code
82 @item @var{old_chain} = make_cleanup (@var{function}, @var{arg});
83 Make a cleanup which will cause @var{function} to be called with @var{arg}
84 (a @code{char *}) later. The result, @var{old_chain}, is a handle that can be
85 passed to @code{do_cleanups} or @code{discard_cleanups} later. Unless you are
86 going to call @code{do_cleanups} or @code{discard_cleanups} yourself,
87 you can ignore the result from @code{make_cleanup}.
88
89
90 @item do_cleanups (@var{old_chain});
91 Perform all cleanups done since @code{make_cleanup} returned @var{old_chain}.
92 E.g.:
93 @example
94 make_cleanup (a, 0);
95 old = make_cleanup (b, 0);
96 do_cleanups (old);
97 @end example
98 @noindent
99 will call @code{b()} but will not call @code{a()}. The cleanup that calls @code{a()} will remain
100 in the cleanup chain, and will be done later unless otherwise discarded.@refill
101
102 @item discard_cleanups (@var{old_chain});
103 Same as @code{do_cleanups} except that it just removes the cleanups from the
104 chain and does not call the specified functions.
105
106 @end table
107
108 Some functions, e.g. @code{fputs_filtered()} or @code{error()}, specify that they
109 ``should not be called when cleanups are not in place''. This means
110 that any actions you need to reverse in the case of an error or
111 interruption must be on the cleanup chain before you call these functions,
112 since they might never return to your code (they @samp{longjmp} instead).
113
114
115 @node Wrapping, Releases, Cleanups, Top
116 @chapter Wrapping output lines
117
118 Output that goes through printf_filtered or fputs_filtered or
119 fputs_demangled needs only to have calls to wrap_here() added
120 in places that would be good breaking points. The utility routines
121 will take care of actually wrapping if the line width is exceeded.
122
123 The argument to wrap_here() is an indentation string which is printed
124 ONLY if the line breaks there. This argument is saved away and used
125 later. It must remain valid until the next call to wrap_here() or
126 until a newline has been printed through the *_filtered functions.
127 Don't pass in a local variable and then return!
128
129 It is usually best to call wrap_here() after printing a comma or space.
130 If you call it before printing a space, make sure that your indentation
131 properly accounts for the leading space that will print if the line wraps
132 there.
133
134 Any function or set of functions that produce filtered output must finish
135 by printing a newline, to flush the wrap buffer, before switching to
136 unfiltered ("printf") output. Symbol reading routines that print
137 warnings are a good example.
138
139
140 @node Releases, README, Wrapping, Top
141 @chapter Configuring GDB for release
142
143
144 GDB should be released after doing @samp{./configure none} in the top level
145 directory. This will leave a makefile there, but no tm- or xm- files.
146 The makefile is needed, for example, for @samp{make gdb.tar.Z}@dots{} If you
147 have tm- or xm-files in the main source directory, C's include rules
148 cause them to be used in preference to tm- and xm-files in the
149 subdirectories where the user will actually configure and build the
150 binaries.
151
152 @samp{./configure none} is also a good way to rebuild the top level Makefile
153 after changing Makefile.in, alldeps.mak, etc.
154
155 @emph{TEMPORARY RELEASE PROCEDURE FOR DOCUMENTATION}
156 @file{gdb.texinfo} is currently marked up using the texinfo-2 macros,
157 which are not yet a default for anything (but we have to start using
158 them sometime).
159
160 For making paper, the only thing this implies is the right generation of
161 texinfo.tex needs to be included in the distribution.
162
163 For making info files, however, rather than duplicating the texinfo2
164 distribution, generate gdb.texinfo locally, and include the files
165 gdb.info* in the distribution. Note the plural;
166 @samp{M-x texinfo-format-buffer} will split the document into one overall file
167 and five or so include files.
168
169 @node README, New Architectures, Releases, Top
170 @chapter The README file
171
172
173 Check the README file, it often has useful information that does not
174 appear anywhere else in the directory.
175
176
177
178 @node New Architectures, BFD support for GDB, README, Top
179 @chapter Defining a new host or target architecture
180
181
182 When building support for a new host and/or target, this will help you
183 organize where to put the various parts. @var{ARCH} stands for the
184 architecture involved.
185
186 Object files needed when the host system is an @var{ARCH} are listed in
187 the file @file{xconfig/@var{ARCH}}, in the Makefile macro @samp{XDEPFILES
188 = }@dots{}. The header file that defines the host system should be
189 called xm-@var{ARCH}.h, and should be specified as the value of @samp{XM_FILE=}
190 in @file{xconfig/@var{ARCH}}.
191 You can also define @samp{CC}, @samp{REGEX} and @samp{REGEX1},
192 @samp{SYSV_DEFINE}, @samp{XM_CFLAGS}, etc in there; see @file{Makefile.in}.
193
194 There are some ``generic'' versions of routines that can be used by
195 various host systems. If these routines work for the @var{ARCH} host,
196 you can just include the generic file's name (with .o, not .c) in
197 @code{XDEPFILES}. Otherwise, you will need to write routines that
198 perform the same functions as the generic file, put them into
199 @code{@var{ARCH}-xdep.c}, and put @code{@var{ARCH}-xdep.o} into
200 @code{XDEPFILES}. These generic host support files include:
201
202 @example
203 coredep.c, coredep.o
204 @end example
205
206 @table @code
207 @item fetch_core_registers()
208 Support for reading registers out of a core file. This routine calls
209 @code{register_addr(}), see below.
210
211 @item register_addr()
212 If your @code{xm-@var{ARCH}.h} file defines the macro
213 @code{REGISTER_U_ADDR(reg)} to be the offset within the @samp{user}
214 struct of a register (represented as a GDB register number),
215 @file{coredep.c} will define the @code{register_addr()} function and use
216 the macro in it. If you do not define @code{REGISTER_U_ADDR}, but you
217 are using the standard @code{fetch_core_registers}, you will need to
218 define your own version of @code{register_addr}, put it into your
219 @code{@var{ARCH}-xdep.c} file, and be sure @code{@var{ARCH}-xdep.o} is
220 in the @code{XDEPFILES} list. If you have your own
221 @code{fetch_core_registers}, you only need to define
222 @code{register_addr} if your @code{fetch_core_registers} calls it. Many
223 custom @code{fetch_core_registers} implementations simply locate the
224 registers themselves.@refill
225 @end table
226
227 Object files needed when the target system is an @var{ARCH} are listed in
228 the file @file{tconfig/@var{ARCH}}, in the Makefile macro @samp{TDEPFILES
229 = }@dots{}. The header file that defines the target system should be
230 called tm-@var{ARCH}.h, and should be specified as the value of @samp{TM_FILE}
231 in @file{tconfig/@var{ARCH}}.
232 You can also define @samp{TM_CFLAGS}, @samp{TM_CLIBS}, and @samp{TM_CDEPS}
233 in there; see @file{Makefile.in}.
234
235 Similar generic support files for target systems are:
236
237 @example
238 exec.c, exec.o:
239 @end example
240
241 This file defines functions for accessing files that are executable
242 on the target system. These functions open and examine an exec file,
243 extract data from one, write data to one, print information about one,
244 etc. Now that executable files are handled with BFD, every architecture
245 should be able to use the generic exec.c rather than its own custom code.
246
247 @node BFD support for GDB, Host versus Target, New Architectures, Top
248 @chapter Binary File Descriptor library support for GDB
249
250 BFD provides support for GDB in several ways:
251
252 @table @emph
253 @item identifying executable and core files
254 BFD will identify a variety of file types, including a.out, coff, and
255 several variants thereof, as well as several kinds of core files.
256
257 @item access to sections of files
258 BFD parses the file headers to determine the names, virtual addresses,
259 sizes, and file locations of all the various named sections in files
260 (such as the text section or the data section). GDB simply calls
261 BFD to read or write section X at byte offset Y for length Z.
262
263 @item specialized core file support
264 BFD provides routines to determine the failing command name stored
265 in a core file, the signal with which the program failed, and whether
266 a core file matches (i.e. could be a core dump of) a particular executable
267 file.
268
269 @item locating the symbol information
270 GDB uses an internal interface of BFD to determine where to find the
271 symbol information in an executable file or symbol-file. GDB itself
272 handles the reading of symbols, since BFD does not ``understand'' debug
273 symbols, but GDB uses BFD's cached information to find the symbols,
274 string table, etc.
275 @end table
276
277 The interface for symbol reading is described in @xref{Symbol Reading}.
278
279 @node Host versus Target, Symbol Reading, BFD support for GDB, Top
280 @chapter What is considered ``host-dependent'' versus ``target-dependent''?
281
282 The xconfig/*, xm-*.h and *-xdep.c files are for host support. The
283 question is, what features or aspects of a debugging or cross-debugging
284 environment are considered to be ``host'' support.
285
286 Defines and include files needed to build on the host are host support.
287 Examples are tty support, system defined types, host byte order, host
288 float format.
289
290 Unix child process support is considered an aspect of the host. Since
291 when you fork on the host you are still on the host, the various macros
292 needed for finding the registers in the upage, running ptrace, and such
293 are all in the host-dependent files.
294
295 This is still somewhat of a grey area; I (John Gilmore) didn't do the
296 xm- and tm- split for gdb (it was done by Jim Kingdon) so I have had to
297 figure out the grounds on which it was split, and make my own choices
298 as I evolve it. I have moved many things out of the xdep files
299 actually, partly as a result of BFD and partly by removing duplicated
300 code.
301
302 @menu
303 * New Host:: Adding a New Host
304 * New Target:: Adding a New Target
305 * New Config:: Extending @code{configure}
306 @end menu
307
308 @node New Host, New Target, Host versus Target, Host versus Target
309 @section Adding a New Host
310
311 There are two halves to making GDB work on a new machine. First,
312 you have to make it host on the new machine (compile there, handle
313 that machine's terminals properly, etc). If you will be cross-debugging
314 to some other kind of system, you are done.
315
316 (If you want to use GDB to debug programs that run on the new machine,
317 you have to get it to understand the machine's object files, symbol
318 files, and interfaces to processes. @pxref{New Target}.)
319
320 Most of the work in making GDB compile on a new machine is in specifying
321 the configuration of the machine. This is done in a dizzying variety
322 of header files and configuration scripts, which we hope to make more
323 sensible soon. Let's say your new host is called an XXX (e.g. sun4),
324 and its full three-part configuration name is XARCH-XVEND-XOS (e.g.
325 sparc-sun-sunos4). In particular:
326
327 At the top level, edit @file{config.sub} and add XARCH, XVEND, and
328 XOS to the lists of supported architectures, vendors, and operating systems
329 near the bottom of the file. Also, add XXX as an alias that maps to
330 XARCH-XVEND-XOS. You can test your changes by running
331
332 ./config.sub XXX
333 and ./config.sub XARCH-XVEND-XOS
334
335 which should both respond with XARCH-XVEND-XOS and no error messages.
336
337 Then edit @file{include/sysdep.h}. Add a new #define for XXX_SYS, with
338 a numeric value not already in use. Add a new section that says
339
340 #if HOST_SYS==XXX_SYS
341 #include <sys/h-XXX.h>
342 #endif
343
344 Now create a new file @file{include/sys/h-XXX.h}. Examine the other
345 h-*.h files as templates, and create one that brings in the right include
346 files for your system, and defines any host-specific macros needed by
347 GDB.
348
349 Now, go to the bfd directory and edit @file{bfd/configure.in}. Add shell
350 script code to recognize your XARCH-XVEND-XOS configuration, and set
351 bfd_host to XXX when you recognize it. Now create a file
352 @file{bfd/config/hmake-XXX}, which includes the line:
353
354 HDEFINES=-DHOST_SYS=XXX_SYS
355
356 (If you have the binutils in the same tree, you'll have to do the same
357 thing to in the binutils directory as you've done in the bfd directory.)
358
359 It's likely that the libiberty and readline directories won't need any
360 changes for your configuration, but if they do, you can change the
361 @file{configure.in} file there to recognize your system and map to an
362 hmake-XXX file. Then add @file{hmake-XXX} to the @file{config/} subdirectory,
363 to set any makefile variables you need. The only current options
364 in there are things like -DSYSV.
365
366 Aha! Now to configure GDB itself! Modify @file{gdb/configure.in} to
367 recognize your system and set gdb_host to XXX. Add a file
368 @file{gdb/xconfig/XXX} which specifies XDEPFILES=(whatever is needed),
369 and XM_FILE= xm-XXX.h. Create @file{gdb/xm-XXX.h} with the appropriate
370 #define's for your system (crib from existing xm-*.h files).
371 If your machine needs custom support routines, you can put them in
372 a file @file{gdb/XXX-xdep.c}, and add XXX-xdep.o to the XDEPFILES=
373 line. If not, you can use the generic routines for ptrace support
374 (infptrace.o) and core files (coredep.o). These can be customized
375 in various ways by macros defined in your @file{xm-XXX.h} file.
376
377 Now, from the top level (above bfd, gdb, etc), run:
378
379 ./configure -template=./configure
380
381 This will rebuild all your configure scripts, using the new
382 configure.in files that you modified. (You can also run this command
383 at any subdirectory level.) You are now ready to try configuring
384 GDB to compile for your system. Do:
385
386 ./configure XXX +target=vxworks960
387
388 This will configure your system to cross-compile for VxWorks on
389 the Intel 960, which is probably not what you really want, but it's
390 a test case that works at this stage. (You haven't set up to be
391 able to debug programs that run @emph{on} XXX yet.)
392
393 If this succeeds, you can try building it all with:
394
395 make
396
397 Good luck! Comments and suggestions about this section are particularly
398 welcome; send them to bug-gdb@@prep.ai.mit.edu.
399
400 When hosting GDB on a new operating system, to make it possible
401 to debug core files, you will need to either
402 write specific code for parsing your OS's core files, or customize
403 bfd/trad-core.c. First, use whatever #include files your machine uses
404 to define the struct of registers that is accessible (possibly in the
405 upage) in a core file (rather than <machine/reg.h>), and an include
406 file that defines whatever header exists on a core file (e.g. the
407 u-area or a "struct core"). Then modify @samp{trad_unix_core_file_p}
408 to use these values to set up the section information for the data
409 segment, stack segment, any other segments in the core file (perhaps
410 shared library contents or control information), "registers" segment,
411 and if there are two discontiguous sets of registers (e.g. integer and
412 float), the "reg2" segment. This section information basically
413 delimits areas in the core file in a standard way, which the
414 section-reading routines in BFD know how to seek around in.
415
416 Then back in GDB, you need a matching routine called fetch_core_registers.
417 If you can use the generic one, it's in core-dep.c; if not, it's in
418 your foobar-xdep.c file. It will be passed a char pointer
419 to the entire "registers" segment, its length, and a zero; or a char
420 pointer to the entire "regs2" segment, its length, and a 2. The
421 routine should suck out the supplied register values and install them into
422 gdb's "registers" array. (@xref{New Architectures}
423 for more info about this.)
424
425 @node New Target, New Config, New Host, Host versus Target
426 @section Adding a New Target
427
428 When adding support for a new target machine, there are various areas
429 of support that might need change, or might be OK.
430
431 If you are using an existing object file format (a.out or COFF),
432 there is probably little to be done. See @file{bfd/doc/bfd.texinfo}
433 for more information on writing new a.out or COFF versions.
434
435 If you need to add a new object file format, you are beyond the scope
436 of this document right now. Look at the structure of the a.out
437 and COFF support, build a transfer vector (xvec) for your new format,
438 and start populating it with routines. Add it to the list in
439 @file{bfd/targets.c}.
440
441 If you are adding a new existing CPU chip (e.g. m68k family), you'll
442 need to define an XARCH-opcode.h file, a tm-XARCH.h file that gives
443 the basic layout of the chip (registers, stack, etc), probably
444 an XARCH-tdep.c file that has support routines for tm-XARCH.h, etc.
445
446 If you are adding a new operating system for an existing CPU chip,
447 add a tm-XOS.h file that describes the operating system facilities
448 that are unusual (extra symbol table info; the breakpoint
449 instruction needed; etc). Then write a @file{tm-XARCH-XOS.h}
450 that just #include's tm-XARCH.h and tm-XOS.h. (Now that we have
451 three-part configuration names, this will probably get revised to
452 separate the OS configuration from the ARCH configuration. FIXME.)
453
454 @node New Config, , New Target, Host versus Target
455 @section Extending @code{configure}
456 Once you have added a new host, target, or both, you'll also need to
457 extend the @code{configure} script to recognize the new configuration
458 possibilities.
459
460 You shouldn't edit the @code{configure} script itself to add hosts or
461 targets; instead, edit the script fragments in the file
462 @code{configure.in}. To handle new hosts, modify the segment after the
463 comment @samp{# per-host}; to handle new targets, modify after @samp{#
464 per-target}.
465 @c Would it be simpler to just use different per-host and per-target
466 @c *scripts*, and call them from {configure} ?
467
468 Then fold your changes into the @code{configure} script by using the
469 @code{+template} option, and specifying @code{configure} itself as the
470 template:
471 @example
472 configure +template=configure
473 @end example
474 @c If "configure" is the only workable option value for +template, it's
475 @c kind of silly to insist that it be provided. If it isn't, somebody
476 @c please fill in here what are others... (then delete this comment!)
477
478 @node Symbol Reading, , Host versus Target, Top
479 @chapter Symbol Reading
480
481 GDB reads symbols from "symbol files". The usual symbol file is the
482 file containing the program which gdb is debugging. GDB can be directed
483 to use a different file for symbols (with the ``symbol-file''
484 command), and it can also read more symbols via the ``add-file'' and ``load''
485 commands, or while reading symbols from shared libraries.
486
487 Symbol files are initially opened by @file{symfile.c} using the BFD
488 library. BFD identifies the type of the file by examining its header.
489 @code{symfile_init} then uses this identification to locate a
490 set of symbol-reading functions.
491
492 Symbol reading modules identify themselves to GDB by calling
493 @code{add_symtab_fns} during their module initialization. The argument
494 to @code{add_symtab_fns} is a @code{struct sym_fns} which contains
495 the name (or name prefix) of the symbol format, the length of the prefix,
496 and pointers to four functions. These functions are called at various
497 times to process symbol-files whose identification matches the specified
498 prefix.
499
500 The functions supplied by each module are:
501
502 @table @code
503 @item XXX_symfile_init(struct sym_fns *sf)
504
505 Called from @code{symbol_file_add} when we are about to read a new
506 symbol file. This function should clean up any internal state
507 (possibly resulting from half-read previous files, for example)
508 and prepare to read a new symbol file. Note that the symbol file
509 which we are reading might be a new "main" symbol file, or might
510 be a secondary symbol file whose symbols are being added to the
511 existing symbol table.
512
513 The argument to @code{XXX_symfile_init} is a newly allocated
514 @code{struct sym_fns} whose @code{bfd} field contains the BFD
515 for the new symbol file being read. Its @code{private} field
516 has been zeroed, and can be modified as desired. Typically,
517 a struct of private information will be @code{malloc}'d, and
518 a pointer to it will be placed in the @code{private} field.
519
520 There is no result from @code{XXX_symfile_init}, but it can call
521 @code{error} if it detects an unavoidable problem.
522
523 @item XXX_new_init()
524
525 Called from @code{symbol_file_add} when discarding existing symbols.
526 This function need only handle
527 the symbol-reading module's internal state; the symbol table data
528 structures visible to the rest of GDB will be discarded by
529 @code{symbol_file_add}. It has no arguments and no result.
530 It may be called after @code{XXX_symfile_init}, if a new symbol
531 table is being read, or may be called alone if all symbols are
532 simply being discarded.
533
534 @item XXX_symfile_read(struct sym_fns *sf, CORE_ADDR addr, int mainline)
535
536 Called from @code{symbol_file_add} to actually read the symbols from a
537 symbol-file into a set of psymtabs or symtabs.
538
539 @code{sf} points to the struct sym_fns originally passed to
540 @code{XXX_sym_init} for possible initialization. @code{addr} is the
541 offset between the file's specified start address and its true address
542 in memory. @code{mainline} is 1 if this is the main symbol table being
543 read, and 0 if a secondary symbol file (e.g. shared library or
544 dynamically loaded file) is being read.@refill
545 @end table
546
547 In addition, if a symbol-reading module creates psymtabs when
548 XXX_symfile_read is called, these psymtabs will contain a pointer to
549 a function @code{XXX_psymtab_to_symtab}, which can be called from
550 any point in the GDB symbol-handling code.
551
552 @table @code
553 @item XXX_psymtab_to_symtab (struct partial_symtab *pst)
554
555 Called from @code{psymtab_to_symtab} (or the PSYMTAB_TO_SYMTAB
556 macro) if the psymtab has not already been read in and had its
557 @code{pst->symtab} pointer set. The argument is the psymtab
558 to be fleshed-out into a symtab. Upon return, pst->readin
559 should have been set to 1, and pst->symtab should contain a
560 pointer to the new corresponding symtab, or zero if there
561 were no symbols in that part of the symbol file.
562 @end table
563
564 @contents
565 @bye
566
This page took 0.04328 seconds and 4 git commands to generate.