OSDN Git Service

0a40902e59be114e1443b8a3d1b88a347a822a6a
[pf3gnuchains/pf3gnuchains3x.git] / gdb / doc / gdbint.texinfo
1 \input texinfo   @c -*- texinfo -*-
2 @setfilename gdbint.info
3 @include gdb-cfg.texi
4 @settitle @value{GDBN} Internals
5 @setchapternewpage off
6 @dircategory Software development
7 @direntry
8 * Gdb-Internals: (gdbint).      The GNU debugger's internals.
9 @end direntry
10
11 @copying
12 Copyright @copyright{} 1990, 1991, 1992, 1993, 1994, 1996, 1998, 1999,
13 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010
14 Free Software Foundation, Inc.
15 Contributed by Cygnus Solutions.  Written by John Gilmore.
16 Second Edition by Stan Shebs.
17
18 Permission is granted to copy, distribute and/or modify this document
19 under the terms of the GNU Free Documentation License, Version 1.1 or
20 any later version published by the Free Software Foundation; with no
21 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
22 Texts.  A copy of the license is included in the section entitled ``GNU
23 Free Documentation License''.
24 @end copying
25
26 @ifnottex
27 This file documents the internals of the GNU debugger @value{GDBN}.
28
29 @insertcopying
30 @end ifnottex
31
32
33 @syncodeindex fn cp
34 @syncodeindex vr cp
35
36 @titlepage
37 @title @value{GDBN} Internals
38 @subtitle{A guide to the internals of the GNU debugger}
39 @author John Gilmore
40 @author Cygnus Solutions
41 @author Second Edition:
42 @author Stan Shebs
43 @author Cygnus Solutions
44 @page
45 @tex
46 \def\$#1${{#1}}  % Kluge: collect RCS revision info without $...$
47 \xdef\manvers{\$Revision$}  % For use in headers, footers too
48 {\parskip=0pt
49 \hfill Cygnus Solutions\par
50 \hfill \manvers\par
51 \hfill \TeX{}info \texinfoversion\par
52 }
53 @end tex
54
55 @vskip 0pt plus 1filll
56 @insertcopying
57 @end titlepage
58
59 @contents
60
61 @node Top
62 @c Perhaps this should be the title of the document (but only for info,
63 @c not for TeX).  Existing GNU manuals seem inconsistent on this point.
64 @top Scope of this Document
65
66 This document documents the internals of the GNU debugger, @value{GDBN}.  It
67 includes description of @value{GDBN}'s key algorithms and operations, as well
68 as the mechanisms that adapt @value{GDBN} to specific hosts and targets.
69
70 @menu
71 * Summary::
72 * Overall Structure::
73 * Algorithms::
74 * User Interface::
75 * libgdb::
76 * Values::
77 * Stack Frames::
78 * Symbol Handling::
79 * Language Support::
80 * Host Definition::
81 * Target Architecture Definition::
82 * Target Descriptions::
83 * Target Vector Definition::
84 * Native Debugging::
85 * Support Libraries::
86 * Coding::
87 * Porting GDB::
88 * Versions and Branches::
89 * Start of New Year Procedure::
90 * Releasing GDB::
91 * Testsuite::
92 * Hints::
93
94 * GDB Observers::  @value{GDBN} Currently available observers
95 * GNU Free Documentation License::  The license for this documentation
96 * Index::
97 @end menu
98
99 @node Summary
100 @chapter Summary
101
102 @menu
103 * Requirements::
104 * Contributors::
105 @end menu
106
107 @node Requirements
108 @section Requirements
109 @cindex requirements for @value{GDBN}
110
111 Before diving into the internals, you should understand the formal
112 requirements and other expectations for @value{GDBN}.  Although some
113 of these may seem obvious, there have been proposals for @value{GDBN}
114 that have run counter to these requirements.
115
116 First of all, @value{GDBN} is a debugger.  It's not designed to be a
117 front panel for embedded systems.  It's not a text editor.  It's not a
118 shell.  It's not a programming environment.
119
120 @value{GDBN} is an interactive tool.  Although a batch mode is
121 available, @value{GDBN}'s primary role is to interact with a human
122 programmer.
123
124 @value{GDBN} should be responsive to the user.  A programmer hot on
125 the trail of a nasty bug, and operating under a looming deadline, is
126 going to be very impatient of everything, including the response time
127 to debugger commands.
128
129 @value{GDBN} should be relatively permissive, such as for expressions.
130 While the compiler should be picky (or have the option to be made
131 picky), since source code lives for a long time usually, the
132 programmer doing debugging shouldn't be spending time figuring out to
133 mollify the debugger.
134
135 @value{GDBN} will be called upon to deal with really large programs.
136 Executable sizes of 50 to 100 megabytes occur regularly, and we've
137 heard reports of programs approaching 1 gigabyte in size.
138
139 @value{GDBN} should be able to run everywhere.  No other debugger is
140 available for even half as many configurations as @value{GDBN}
141 supports.
142
143 @node Contributors
144 @section Contributors
145
146 The first edition of this document was written by John Gilmore of
147 Cygnus Solutions. The current second edition was written by Stan Shebs
148 of Cygnus Solutions, who continues to update the manual.
149
150 Over the years, many others have made additions and changes to this
151 document. This section attempts to record the significant contributors
152 to that effort. One of the virtues of free software is that everyone
153 is free to contribute to it; with regret, we cannot actually
154 acknowledge everyone here.
155
156 @quotation
157 @emph{Plea:} This section has only been added relatively recently (four
158 years after publication of the second edition). Additions to this
159 section are particularly welcome.  If you or your friends (or enemies,
160 to be evenhanded) have been unfairly omitted from this list, we would
161 like to add your names!
162 @end quotation
163
164 A document such as this relies on being kept up to date by numerous
165 small updates by contributing engineers as they make changes to the
166 code base. The file @file{ChangeLog} in the @value{GDBN} distribution
167 approximates a blow-by-blow account. The most prolific contributors to
168 this important, but low profile task are Andrew Cagney (responsible
169 for over half the entries), Daniel Jacobowitz, Mark Kettenis, Jim
170 Blandy and Eli Zaretskii.
171
172 Eli Zaretskii and Daniel Jacobowitz wrote the sections documenting
173 watchpoints.
174
175 Jeremy Bennett updated the sections on initializing a new architecture
176 and register representation, and added the section on Frame Interpretation.
177
178
179 @node Overall Structure
180
181 @chapter Overall Structure
182
183 @value{GDBN} consists of three major subsystems: user interface,
184 symbol handling (the @dfn{symbol side}), and target system handling (the
185 @dfn{target side}).
186
187 The user interface consists of several actual interfaces, plus
188 supporting code.
189
190 The symbol side consists of object file readers, debugging info
191 interpreters, symbol table management, source language expression
192 parsing, type and value printing.
193
194 The target side consists of execution control, stack frame analysis, and
195 physical target manipulation.
196
197 The target side/symbol side division is not formal, and there are a
198 number of exceptions.  For instance, core file support involves symbolic
199 elements (the basic core file reader is in BFD) and target elements (it
200 supplies the contents of memory and the values of registers).  Instead,
201 this division is useful for understanding how the minor subsystems
202 should fit together.
203
204 @section The Symbol Side
205
206 The symbolic side of @value{GDBN} can be thought of as ``everything
207 you can do in @value{GDBN} without having a live program running''.
208 For instance, you can look at the types of variables, and evaluate
209 many kinds of expressions.
210
211 @section The Target Side
212
213 The target side of @value{GDBN} is the ``bits and bytes manipulator''.
214 Although it may make reference to symbolic info here and there, most
215 of the target side will run with only a stripped executable
216 available---or even no executable at all, in remote debugging cases.
217
218 Operations such as disassembly, stack frame crawls, and register
219 display, are able to work with no symbolic info at all.  In some cases,
220 such as disassembly, @value{GDBN} will use symbolic info to present addresses
221 relative to symbols rather than as raw numbers, but it will work either
222 way.
223
224 @section Configurations
225
226 @cindex host
227 @cindex target
228 @dfn{Host} refers to attributes of the system where @value{GDBN} runs.
229 @dfn{Target} refers to the system where the program being debugged
230 executes.  In most cases they are the same machine, in which case a
231 third type of @dfn{Native} attributes come into play.
232
233 Defines and include files needed to build on the host are host
234 support.  Examples are tty support, system defined types, host byte
235 order, host float format.  These are all calculated by @code{autoconf}
236 when the debugger is built.
237
238 Defines and information needed to handle the target format are target
239 dependent.  Examples are the stack frame format, instruction set,
240 breakpoint instruction, registers, and how to set up and tear down the stack
241 to call a function.
242
243 Information that is only needed when the host and target are the same,
244 is native dependent.  One example is Unix child process support; if the
245 host and target are not the same, calling @code{fork} to start the target
246 process is a bad idea.  The various macros needed for finding the
247 registers in the @code{upage}, running @code{ptrace}, and such are all
248 in the native-dependent files.
249
250 Another example of native-dependent code is support for features that
251 are really part of the target environment, but which require
252 @code{#include} files that are only available on the host system.  Core
253 file handling and @code{setjmp} handling are two common cases.
254
255 When you want to make @value{GDBN} work as the traditional native debugger
256 on a system, you will need to supply both target and native information.
257
258 @section Source Tree Structure
259 @cindex @value{GDBN} source tree structure
260
261 The @value{GDBN} source directory has a mostly flat structure---there
262 are only a few subdirectories.  A file's name usually gives a hint as
263 to what it does; for example, @file{stabsread.c} reads stabs,
264 @file{dwarf2read.c} reads @sc{DWARF 2}, etc.
265
266 Files that are related to some common task have names that share
267 common substrings.  For example, @file{*-thread.c} files deal with
268 debugging threads on various platforms; @file{*read.c} files deal with
269 reading various kinds of symbol and object files; @file{inf*.c} files
270 deal with direct control of the @dfn{inferior program} (@value{GDBN}
271 parlance for the program being debugged).
272
273 There are several dozens of files in the @file{*-tdep.c} family.
274 @samp{tdep} stands for @dfn{target-dependent code}---each of these
275 files implements debug support for a specific target architecture
276 (sparc, mips, etc).  Usually, only one of these will be used in a
277 specific @value{GDBN} configuration (sometimes two, closely related).
278
279 Similarly, there are many @file{*-nat.c} files, each one for native
280 debugging on a specific system (e.g., @file{sparc-linux-nat.c} is for
281 native debugging of Sparc machines running the Linux kernel).
282
283 The few subdirectories of the source tree are:
284
285 @table @file
286 @item cli
287 Code that implements @dfn{CLI}, the @value{GDBN} Command-Line
288 Interpreter.  @xref{User Interface, Command Interpreter}.
289
290 @item gdbserver
291 Code for the @value{GDBN} remote server.
292
293 @item gdbtk
294 Code for Insight, the @value{GDBN} TK-based GUI front-end.
295
296 @item mi
297 The @dfn{GDB/MI}, the @value{GDBN} Machine Interface interpreter.
298
299 @item signals
300 Target signal translation code.
301
302 @item tui
303 Code for @dfn{TUI}, the @value{GDBN} Text-mode full-screen User
304 Interface.  @xref{User Interface, TUI}.
305 @end table
306
307 @node Algorithms
308
309 @chapter Algorithms
310 @cindex algorithms
311
312 @value{GDBN} uses a number of debugging-specific algorithms.  They are
313 often not very complicated, but get lost in the thicket of special
314 cases and real-world issues.  This chapter describes the basic
315 algorithms and mentions some of the specific target definitions that
316 they use.
317
318 @section Prologue Analysis
319
320 @cindex prologue analysis
321 @cindex call frame information
322 @cindex CFI (call frame information)
323 To produce a backtrace and allow the user to manipulate older frames'
324 variables and arguments, @value{GDBN} needs to find the base addresses
325 of older frames, and discover where those frames' registers have been
326 saved.  Since a frame's ``callee-saves'' registers get saved by
327 younger frames if and when they're reused, a frame's registers may be
328 scattered unpredictably across younger frames.  This means that
329 changing the value of a register-allocated variable in an older frame
330 may actually entail writing to a save slot in some younger frame.
331
332 Modern versions of GCC emit Dwarf call frame information (``CFI''),
333 which describes how to find frame base addresses and saved registers.
334 But CFI is not always available, so as a fallback @value{GDBN} uses a
335 technique called @dfn{prologue analysis} to find frame sizes and saved
336 registers.  A prologue analyzer disassembles the function's machine
337 code starting from its entry point, and looks for instructions that
338 allocate frame space, save the stack pointer in a frame pointer
339 register, save registers, and so on.  Obviously, this can't be done
340 accurately in general, but it's tractable to do well enough to be very
341 helpful.  Prologue analysis predates the GNU toolchain's support for
342 CFI; at one time, prologue analysis was the only mechanism
343 @value{GDBN} used for stack unwinding at all, when the function
344 calling conventions didn't specify a fixed frame layout.
345
346 In the olden days, function prologues were generated by hand-written,
347 target-specific code in GCC, and treated as opaque and untouchable by
348 optimizers.  Looking at this code, it was usually straightforward to
349 write a prologue analyzer for @value{GDBN} that would accurately
350 understand all the prologues GCC would generate.  However, over time
351 GCC became more aggressive about instruction scheduling, and began to
352 understand more about the semantics of the prologue instructions
353 themselves; in response, @value{GDBN}'s analyzers became more complex
354 and fragile.  Keeping the prologue analyzers working as GCC (and the
355 instruction sets themselves) evolved became a substantial task.
356
357 @cindex @file{prologue-value.c}
358 @cindex abstract interpretation of function prologues
359 @cindex pseudo-evaluation of function prologues
360 To try to address this problem, the code in @file{prologue-value.h}
361 and @file{prologue-value.c} provides a general framework for writing
362 prologue analyzers that are simpler and more robust than ad-hoc
363 analyzers.  When we analyze a prologue using the prologue-value
364 framework, we're really doing ``abstract interpretation'' or
365 ``pseudo-evaluation'': running the function's code in simulation, but
366 using conservative approximations of the values registers and memory
367 would hold when the code actually runs.  For example, if our function
368 starts with the instruction:
369
370 @example
371 addi r1, 42     # add 42 to r1
372 @end example
373 @noindent
374 we don't know exactly what value will be in @code{r1} after executing
375 this instruction, but we do know it'll be 42 greater than its original
376 value.
377
378 If we then see an instruction like:
379
380 @example
381 addi r1, 22     # add 22 to r1
382 @end example
383 @noindent
384 we still don't know what @code{r1's} value is, but again, we can say
385 it is now 64 greater than its original value.
386
387 If the next instruction were:
388
389 @example
390 mov r2, r1      # set r2 to r1's value
391 @end example
392 @noindent
393 then we can say that @code{r2's} value is now the original value of
394 @code{r1} plus 64.
395
396 It's common for prologues to save registers on the stack, so we'll
397 need to track the values of stack frame slots, as well as the
398 registers.  So after an instruction like this:
399
400 @example
401 mov (fp+4), r2
402 @end example
403 @noindent
404 then we'd know that the stack slot four bytes above the frame pointer
405 holds the original value of @code{r1} plus 64.
406
407 And so on.
408
409 Of course, this can only go so far before it gets unreasonable.  If we
410 wanted to be able to say anything about the value of @code{r1} after
411 the instruction:
412
413 @example
414 xor r1, r3      # exclusive-or r1 and r3, place result in r1
415 @end example
416 @noindent
417 then things would get pretty complex.  But remember, we're just doing
418 a conservative approximation; if exclusive-or instructions aren't
419 relevant to prologues, we can just say @code{r1}'s value is now
420 ``unknown''.  We can ignore things that are too complex, if that loss of
421 information is acceptable for our application.
422
423 So when we say ``conservative approximation'' here, what we mean is an
424 approximation that is either accurate, or marked ``unknown'', but
425 never inaccurate.
426
427 Using this framework, a prologue analyzer is simply an interpreter for
428 machine code, but one that uses conservative approximations for the
429 contents of registers and memory instead of actual values.  Starting
430 from the function's entry point, you simulate instructions up to the
431 current PC, or an instruction that you don't know how to simulate.
432 Now you can examine the state of the registers and stack slots you've
433 kept track of.
434
435 @itemize @bullet
436
437 @item
438 To see how large your stack frame is, just check the value of the
439 stack pointer register; if it's the original value of the SP
440 minus a constant, then that constant is the stack frame's size.
441 If the SP's value has been marked as ``unknown'', then that means
442 the prologue has done something too complex for us to track, and
443 we don't know the frame size.
444
445 @item
446 To see where we've saved the previous frame's registers, we just
447 search the values we've tracked --- stack slots, usually, but
448 registers, too, if you want --- for something equal to the register's
449 original value.  If the calling conventions suggest a standard place
450 to save a given register, then we can check there first, but really,
451 anything that will get us back the original value will probably work.
452 @end itemize
453
454 This does take some work.  But prologue analyzers aren't
455 quick-and-simple pattern patching to recognize a few fixed prologue
456 forms any more; they're big, hairy functions.  Along with inferior
457 function calls, prologue analysis accounts for a substantial portion
458 of the time needed to stabilize a @value{GDBN} port.  So it's
459 worthwhile to look for an approach that will be easier to understand
460 and maintain.  In the approach described above:
461
462 @itemize @bullet
463
464 @item
465 It's easier to see that the analyzer is correct: you just see
466 whether the analyzer properly (albeit conservatively) simulates
467 the effect of each instruction.
468
469 @item
470 It's easier to extend the analyzer: you can add support for new
471 instructions, and know that you haven't broken anything that
472 wasn't already broken before.
473
474 @item
475 It's orthogonal: to gather new information, you don't need to
476 complicate the code for each instruction.  As long as your domain
477 of conservative values is already detailed enough to tell you
478 what you need, then all the existing instruction simulations are
479 already gathering the right data for you.
480
481 @end itemize
482
483 The file @file{prologue-value.h} contains detailed comments explaining
484 the framework and how to use it.
485
486
487 @section Breakpoint Handling
488
489 @cindex breakpoints
490 In general, a breakpoint is a user-designated location in the program
491 where the user wants to regain control if program execution ever reaches
492 that location.
493
494 There are two main ways to implement breakpoints; either as ``hardware''
495 breakpoints or as ``software'' breakpoints.
496
497 @cindex hardware breakpoints
498 @cindex program counter
499 Hardware breakpoints are sometimes available as a builtin debugging
500 features with some chips.  Typically these work by having dedicated
501 register into which the breakpoint address may be stored.  If the PC
502 (shorthand for @dfn{program counter})
503 ever matches a value in a breakpoint registers, the CPU raises an
504 exception and reports it to @value{GDBN}.
505
506 Another possibility is when an emulator is in use; many emulators
507 include circuitry that watches the address lines coming out from the
508 processor, and force it to stop if the address matches a breakpoint's
509 address.
510
511 A third possibility is that the target already has the ability to do
512 breakpoints somehow; for instance, a ROM monitor may do its own
513 software breakpoints.  So although these are not literally ``hardware
514 breakpoints'', from @value{GDBN}'s point of view they work the same;
515 @value{GDBN} need not do anything more than set the breakpoint and wait
516 for something to happen.
517
518 Since they depend on hardware resources, hardware breakpoints may be
519 limited in number; when the user asks for more, @value{GDBN} will
520 start trying to set software breakpoints.  (On some architectures,
521 notably the 32-bit x86 platforms, @value{GDBN} cannot always know
522 whether there's enough hardware resources to insert all the hardware
523 breakpoints and watchpoints.  On those platforms, @value{GDBN} prints
524 an error message only when the program being debugged is continued.)
525
526 @cindex software breakpoints
527 Software breakpoints require @value{GDBN} to do somewhat more work.
528 The basic theory is that @value{GDBN} will replace a program
529 instruction with a trap, illegal divide, or some other instruction
530 that will cause an exception, and then when it's encountered,
531 @value{GDBN} will take the exception and stop the program.  When the
532 user says to continue, @value{GDBN} will restore the original
533 instruction, single-step, re-insert the trap, and continue on.
534
535 Since it literally overwrites the program being tested, the program area
536 must be writable, so this technique won't work on programs in ROM.  It
537 can also distort the behavior of programs that examine themselves,
538 although such a situation would be highly unusual.
539
540 Also, the software breakpoint instruction should be the smallest size of
541 instruction, so it doesn't overwrite an instruction that might be a jump
542 target, and cause disaster when the program jumps into the middle of the
543 breakpoint instruction.  (Strictly speaking, the breakpoint must be no
544 larger than the smallest interval between instructions that may be jump
545 targets; perhaps there is an architecture where only even-numbered
546 instructions may jumped to.)  Note that it's possible for an instruction
547 set not to have any instructions usable for a software breakpoint,
548 although in practice only the ARC has failed to define such an
549 instruction.
550
551 Basic breakpoint object handling is in @file{breakpoint.c}.  However,
552 much of the interesting breakpoint action is in @file{infrun.c}.
553
554 @table @code
555 @cindex insert or remove software breakpoint
556 @findex target_remove_breakpoint
557 @findex target_insert_breakpoint
558 @item target_remove_breakpoint (@var{bp_tgt})
559 @itemx target_insert_breakpoint (@var{bp_tgt})
560 Insert or remove a software breakpoint at address
561 @code{@var{bp_tgt}->placed_address}.  Returns zero for success,
562 non-zero for failure.  On input, @var{bp_tgt} contains the address of the
563 breakpoint, and is otherwise initialized to zero.  The fields of the
564 @code{struct bp_target_info} pointed to by @var{bp_tgt} are updated
565 to contain other information about the breakpoint on output.  The field
566 @code{placed_address} may be updated if the breakpoint was placed at a
567 related address; the field @code{shadow_contents} contains the real
568 contents of the bytes where the breakpoint has been inserted,
569 if reading memory would return the breakpoint instead of the
570 underlying memory; the field @code{shadow_len} is the length of
571 memory cached in @code{shadow_contents}, if any; and the field
572 @code{placed_size} is optionally set and used by the target, if
573 it could differ from @code{shadow_len}.
574
575 For example, the remote target @samp{Z0} packet does not require
576 shadowing memory, so @code{shadow_len} is left at zero.  However,
577 the length reported by @code{gdbarch_breakpoint_from_pc} is cached in
578 @code{placed_size}, so that a matching @samp{z0} packet can be
579 used to remove the breakpoint.
580
581 @cindex insert or remove hardware breakpoint
582 @findex target_remove_hw_breakpoint
583 @findex target_insert_hw_breakpoint
584 @item target_remove_hw_breakpoint (@var{bp_tgt})
585 @itemx target_insert_hw_breakpoint (@var{bp_tgt})
586 Insert or remove a hardware-assisted breakpoint at address
587 @code{@var{bp_tgt}->placed_address}.  Returns zero for success,
588 non-zero for failure.  See @code{target_insert_breakpoint} for
589 a description of the @code{struct bp_target_info} pointed to by
590 @var{bp_tgt}; the @code{shadow_contents} and
591 @code{shadow_len} members are not used for hardware breakpoints,
592 but @code{placed_size} may be.
593 @end table
594
595 @section Single Stepping
596
597 @section Signal Handling
598
599 @section Thread Handling
600
601 @section Inferior Function Calls
602
603 @section Longjmp Support
604
605 @cindex @code{longjmp} debugging
606 @value{GDBN} has support for figuring out that the target is doing a
607 @code{longjmp} and for stopping at the target of the jump, if we are
608 stepping.  This is done with a few specialized internal breakpoints,
609 which are visible in the output of the @samp{maint info breakpoint}
610 command.
611
612 @findex gdbarch_get_longjmp_target
613 To make this work, you need to define a function called
614 @code{gdbarch_get_longjmp_target}, which will examine the
615 @code{jmp_buf} structure and extract the @code{longjmp} target address.
616 Since @code{jmp_buf} is target specific and typically defined in a
617 target header not available to @value{GDBN}, you will need to
618 determine the offset of the PC manually and return that; many targets
619 define a @code{jb_pc_offset} field in the tdep structure to save the
620 value once calculated.
621
622 @section Watchpoints
623 @cindex watchpoints
624
625 Watchpoints are a special kind of breakpoints (@pxref{Algorithms,
626 breakpoints}) which break when data is accessed rather than when some
627 instruction is executed.  When you have data which changes without
628 your knowing what code does that, watchpoints are the silver bullet to
629 hunt down and kill such bugs.
630
631 @cindex hardware watchpoints
632 @cindex software watchpoints
633 Watchpoints can be either hardware-assisted or not; the latter type is
634 known as ``software watchpoints.''  @value{GDBN} always uses
635 hardware-assisted watchpoints if they are available, and falls back on
636 software watchpoints otherwise.  Typical situations where @value{GDBN}
637 will use software watchpoints are:
638
639 @itemize @bullet
640 @item
641 The watched memory region is too large for the underlying hardware
642 watchpoint support.  For example, each x86 debug register can watch up
643 to 4 bytes of memory, so trying to watch data structures whose size is
644 more than 16 bytes will cause @value{GDBN} to use software
645 watchpoints.
646
647 @item
648 The value of the expression to be watched depends on data held in
649 registers (as opposed to memory).
650
651 @item
652 Too many different watchpoints requested.  (On some architectures,
653 this situation is impossible to detect until the debugged program is
654 resumed.)  Note that x86 debug registers are used both for hardware
655 breakpoints and for watchpoints, so setting too many hardware
656 breakpoints might cause watchpoint insertion to fail.
657
658 @item
659 No hardware-assisted watchpoints provided by the target
660 implementation.
661 @end itemize
662
663 Software watchpoints are very slow, since @value{GDBN} needs to
664 single-step the program being debugged and test the value of the
665 watched expression(s) after each instruction.  The rest of this
666 section is mostly irrelevant for software watchpoints.
667
668 When the inferior stops, @value{GDBN} tries to establish, among other
669 possible reasons, whether it stopped due to a watchpoint being hit.
670 It first uses @code{STOPPED_BY_WATCHPOINT} to see if any watchpoint
671 was hit.  If not, all watchpoint checking is skipped.
672
673 Then @value{GDBN} calls @code{target_stopped_data_address} exactly
674 once.  This method returns the address of the watchpoint which
675 triggered, if the target can determine it.  If the triggered address
676 is available, @value{GDBN} compares the address returned by this
677 method with each watched memory address in each active watchpoint.
678 For data-read and data-access watchpoints, @value{GDBN} announces
679 every watchpoint that watches the triggered address as being hit.
680 For this reason, data-read and data-access watchpoints
681 @emph{require} that the triggered address be available; if not, read
682 and access watchpoints will never be considered hit.  For data-write
683 watchpoints, if the triggered address is available, @value{GDBN}
684 considers only those watchpoints which match that address;
685 otherwise, @value{GDBN} considers all data-write watchpoints.  For
686 each data-write watchpoint that @value{GDBN} considers, it evaluates
687 the expression whose value is being watched, and tests whether the
688 watched value has changed.  Watchpoints whose watched values have
689 changed are announced as hit.
690
691 @c FIXME move these to the main lists of target/native defns
692
693 @value{GDBN} uses several macros and primitives to support hardware
694 watchpoints:
695
696 @table @code
697 @findex TARGET_CAN_USE_HARDWARE_WATCHPOINT
698 @item TARGET_CAN_USE_HARDWARE_WATCHPOINT (@var{type}, @var{count}, @var{other})
699 Return the number of hardware watchpoints of type @var{type} that are
700 possible to be set.  The value is positive if @var{count} watchpoints
701 of this type can be set, zero if setting watchpoints of this type is
702 not supported, and negative if @var{count} is more than the maximum
703 number of watchpoints of type @var{type} that can be set.  @var{other}
704 is non-zero if other types of watchpoints are currently enabled (there
705 are architectures which cannot set watchpoints of different types at
706 the same time).
707
708 @findex TARGET_REGION_OK_FOR_HW_WATCHPOINT
709 @item TARGET_REGION_OK_FOR_HW_WATCHPOINT (@var{addr}, @var{len})
710 Return non-zero if hardware watchpoints can be used to watch a region
711 whose address is @var{addr} and whose length in bytes is @var{len}.
712
713 @cindex insert or remove hardware watchpoint
714 @findex target_insert_watchpoint
715 @findex target_remove_watchpoint
716 @item target_insert_watchpoint (@var{addr}, @var{len}, @var{type})
717 @itemx target_remove_watchpoint (@var{addr}, @var{len}, @var{type})
718 Insert or remove a hardware watchpoint starting at @var{addr}, for
719 @var{len} bytes.  @var{type} is the watchpoint type, one of the
720 possible values of the enumerated data type @code{target_hw_bp_type},
721 defined by @file{breakpoint.h} as follows:
722
723 @smallexample
724  enum target_hw_bp_type
725    @{
726      hw_write   = 0, /* Common (write) HW watchpoint */
727      hw_read    = 1, /* Read    HW watchpoint */
728      hw_access  = 2, /* Access (read or write) HW watchpoint */
729      hw_execute = 3  /* Execute HW breakpoint */
730    @};
731 @end smallexample
732
733 @noindent
734 These two macros should return 0 for success, non-zero for failure.
735
736 @findex target_stopped_data_address
737 @item target_stopped_data_address (@var{addr_p})
738 If the inferior has some watchpoint that triggered, place the address
739 associated with the watchpoint at the location pointed to by
740 @var{addr_p} and return non-zero.  Otherwise, return zero.  This
741 is required for data-read and data-access watchpoints.  It is
742 not required for data-write watchpoints, but @value{GDBN} uses
743 it to improve handling of those also.
744
745 @value{GDBN} will only call this method once per watchpoint stop,
746 immediately after calling @code{STOPPED_BY_WATCHPOINT}.  If the
747 target's watchpoint indication is sticky, i.e., stays set after
748 resuming, this method should clear it.  For instance, the x86 debug
749 control register has sticky triggered flags.
750
751 @findex target_watchpoint_addr_within_range
752 @item target_watchpoint_addr_within_range (@var{target}, @var{addr}, @var{start}, @var{length})
753 Check whether @var{addr} (as returned by @code{target_stopped_data_address})
754 lies within the hardware-defined watchpoint region described by
755 @var{start} and @var{length}.  This only needs to be provided if the
756 granularity of a watchpoint is greater than one byte, i.e., if the
757 watchpoint can also trigger on nearby addresses outside of the watched
758 region.
759
760 @findex HAVE_STEPPABLE_WATCHPOINT
761 @item HAVE_STEPPABLE_WATCHPOINT
762 If defined to a non-zero value, it is not necessary to disable a
763 watchpoint to step over it.  Like @code{gdbarch_have_nonsteppable_watchpoint},
764 this is usually set when watchpoints trigger at the instruction
765 which will perform an interesting read or write.  It should be
766 set if there is a temporary disable bit which allows the processor
767 to step over the interesting instruction without raising the
768 watchpoint exception again.
769
770 @findex gdbarch_have_nonsteppable_watchpoint 
771 @item int gdbarch_have_nonsteppable_watchpoint (@var{gdbarch})
772 If it returns a non-zero value, @value{GDBN} should disable a
773 watchpoint to step the inferior over it.  This is usually set when
774 watchpoints trigger at the instruction which will perform an
775 interesting read or write.
776
777 @findex HAVE_CONTINUABLE_WATCHPOINT
778 @item HAVE_CONTINUABLE_WATCHPOINT
779 If defined to a non-zero value, it is possible to continue the
780 inferior after a watchpoint has been hit.  This is usually set
781 when watchpoints trigger at the instruction following an interesting
782 read or write.
783
784 @findex CANNOT_STEP_HW_WATCHPOINTS
785 @item CANNOT_STEP_HW_WATCHPOINTS
786 If this is defined to a non-zero value, @value{GDBN} will remove all
787 watchpoints before stepping the inferior.
788
789 @findex STOPPED_BY_WATCHPOINT
790 @item STOPPED_BY_WATCHPOINT (@var{wait_status})
791 Return non-zero if stopped by a watchpoint.  @var{wait_status} is of
792 the type @code{struct target_waitstatus}, defined by @file{target.h}.
793 Normally, this macro is defined to invoke the function pointed to by
794 the @code{to_stopped_by_watchpoint} member of the structure (of the
795 type @code{target_ops}, defined on @file{target.h}) that describes the
796 target-specific operations; @code{to_stopped_by_watchpoint} ignores
797 the @var{wait_status} argument.
798
799 @value{GDBN} does not require the non-zero value returned by
800 @code{STOPPED_BY_WATCHPOINT} to be 100% correct, so if a target cannot
801 determine for sure whether the inferior stopped due to a watchpoint,
802 it could return non-zero ``just in case''.
803 @end table
804
805 @subsection Watchpoints and Threads
806 @cindex watchpoints, with threads
807
808 @value{GDBN} only supports process-wide watchpoints, which trigger
809 in all threads.  @value{GDBN} uses the thread ID to make watchpoints
810 act as if they were thread-specific, but it cannot set hardware
811 watchpoints that only trigger in a specific thread.  Therefore, even
812 if the target supports threads, per-thread debug registers, and
813 watchpoints which only affect a single thread, it should set the
814 per-thread debug registers for all threads to the same value.  On
815 @sc{gnu}/Linux native targets, this is accomplished by using
816 @code{ALL_LWPS} in @code{target_insert_watchpoint} and
817 @code{target_remove_watchpoint} and by using
818 @code{linux_set_new_thread} to register a handler for newly created
819 threads.
820
821 @value{GDBN}'s @sc{gnu}/Linux support only reports a single event
822 at a time, although multiple events can trigger simultaneously for
823 multi-threaded programs.  When multiple events occur, @file{linux-nat.c}
824 queues subsequent events and returns them the next time the program
825 is resumed.  This means that @code{STOPPED_BY_WATCHPOINT} and
826 @code{target_stopped_data_address} only need to consult the current
827 thread's state---the thread indicated by @code{inferior_ptid}.  If
828 two threads have hit watchpoints simultaneously, those routines
829 will be called a second time for the second thread.
830
831 @subsection x86 Watchpoints
832 @cindex x86 debug registers
833 @cindex watchpoints, on x86
834
835 The 32-bit Intel x86 (a.k.a.@: ia32) processors feature special debug
836 registers designed to facilitate debugging.  @value{GDBN} provides a
837 generic library of functions that x86-based ports can use to implement
838 support for watchpoints and hardware-assisted breakpoints.  This
839 subsection documents the x86 watchpoint facilities in @value{GDBN}.
840
841 (At present, the library functions read and write debug registers directly, and are
842 thus only available for native configurations.)
843
844 To use the generic x86 watchpoint support, a port should do the
845 following:
846
847 @itemize @bullet
848 @findex I386_USE_GENERIC_WATCHPOINTS
849 @item
850 Define the macro @code{I386_USE_GENERIC_WATCHPOINTS} somewhere in the
851 target-dependent headers.
852
853 @item
854 Include the @file{config/i386/nm-i386.h} header file @emph{after}
855 defining @code{I386_USE_GENERIC_WATCHPOINTS}.
856
857 @item
858 Add @file{i386-nat.o} to the value of the Make variable
859 @code{NATDEPFILES} (@pxref{Native Debugging, NATDEPFILES}).
860
861 @item
862 Provide implementations for the @code{I386_DR_LOW_*} macros described
863 below.  Typically, each macro should call a target-specific function
864 which does the real work.
865 @end itemize
866
867 The x86 watchpoint support works by maintaining mirror images of the
868 debug registers.  Values are copied between the mirror images and the
869 real debug registers via a set of macros which each target needs to
870 provide:
871
872 @table @code
873 @findex I386_DR_LOW_SET_CONTROL
874 @item I386_DR_LOW_SET_CONTROL (@var{val})
875 Set the Debug Control (DR7) register to the value @var{val}.
876
877 @findex I386_DR_LOW_SET_ADDR
878 @item I386_DR_LOW_SET_ADDR (@var{idx}, @var{addr})
879 Put the address @var{addr} into the debug register number @var{idx}.
880
881 @findex I386_DR_LOW_RESET_ADDR
882 @item I386_DR_LOW_RESET_ADDR (@var{idx})
883 Reset (i.e.@: zero out) the address stored in the debug register
884 number @var{idx}.
885
886 @findex I386_DR_LOW_GET_STATUS
887 @item I386_DR_LOW_GET_STATUS
888 Return the value of the Debug Status (DR6) register.  This value is
889 used immediately after it is returned by
890 @code{I386_DR_LOW_GET_STATUS}, so as to support per-thread status
891 register values.
892 @end table
893
894 For each one of the 4 debug registers (whose indices are from 0 to 3)
895 that store addresses, a reference count is maintained by @value{GDBN},
896 to allow sharing of debug registers by several watchpoints.  This
897 allows users to define several watchpoints that watch the same
898 expression, but with different conditions and/or commands, without
899 wasting debug registers which are in short supply.  @value{GDBN}
900 maintains the reference counts internally, targets don't have to do
901 anything to use this feature.
902
903 The x86 debug registers can each watch a region that is 1, 2, or 4
904 bytes long.  The ia32 architecture requires that each watched region
905 be appropriately aligned: 2-byte region on 2-byte boundary, 4-byte
906 region on 4-byte boundary.  However, the x86 watchpoint support in
907 @value{GDBN} can watch unaligned regions and regions larger than 4
908 bytes (up to 16 bytes) by allocating several debug registers to watch
909 a single region.  This allocation of several registers per a watched
910 region is also done automatically without target code intervention.
911
912 The generic x86 watchpoint support provides the following API for the
913 @value{GDBN}'s application code:
914
915 @table @code
916 @findex i386_region_ok_for_watchpoint
917 @item i386_region_ok_for_watchpoint (@var{addr}, @var{len})
918 The macro @code{TARGET_REGION_OK_FOR_HW_WATCHPOINT} is set to call
919 this function.  It counts the number of debug registers required to
920 watch a given region, and returns a non-zero value if that number is
921 less than 4, the number of debug registers available to x86
922 processors.
923
924 @findex i386_stopped_data_address
925 @item i386_stopped_data_address (@var{addr_p})
926 The target function
927 @code{target_stopped_data_address} is set to call this function.
928 This
929 function examines the breakpoint condition bits in the DR6 Debug
930 Status register, as returned by the @code{I386_DR_LOW_GET_STATUS}
931 macro, and returns the address associated with the first bit that is
932 set in DR6.
933
934 @findex i386_stopped_by_watchpoint
935 @item i386_stopped_by_watchpoint (void)
936 The macro @code{STOPPED_BY_WATCHPOINT}
937 is set to call this function.  The
938 argument passed to @code{STOPPED_BY_WATCHPOINT} is ignored.  This
939 function examines the breakpoint condition bits in the DR6 Debug
940 Status register, as returned by the @code{I386_DR_LOW_GET_STATUS}
941 macro, and returns true if any bit is set.  Otherwise, false is
942 returned.
943
944 @findex i386_insert_watchpoint
945 @findex i386_remove_watchpoint
946 @item i386_insert_watchpoint (@var{addr}, @var{len}, @var{type})
947 @itemx i386_remove_watchpoint (@var{addr}, @var{len}, @var{type})
948 Insert or remove a watchpoint.  The macros
949 @code{target_insert_watchpoint} and @code{target_remove_watchpoint}
950 are set to call these functions.  @code{i386_insert_watchpoint} first
951 looks for a debug register which is already set to watch the same
952 region for the same access types; if found, it just increments the
953 reference count of that debug register, thus implementing debug
954 register sharing between watchpoints.  If no such register is found,
955 the function looks for a vacant debug register, sets its mirrored
956 value to @var{addr}, sets the mirrored value of DR7 Debug Control
957 register as appropriate for the @var{len} and @var{type} parameters,
958 and then passes the new values of the debug register and DR7 to the
959 inferior by calling @code{I386_DR_LOW_SET_ADDR} and
960 @code{I386_DR_LOW_SET_CONTROL}.  If more than one debug register is
961 required to cover the given region, the above process is repeated for
962 each debug register.
963
964 @code{i386_remove_watchpoint} does the opposite: it resets the address
965 in the mirrored value of the debug register and its read/write and
966 length bits in the mirrored value of DR7, then passes these new
967 values to the inferior via @code{I386_DR_LOW_RESET_ADDR} and
968 @code{I386_DR_LOW_SET_CONTROL}.  If a register is shared by several
969 watchpoints, each time a @code{i386_remove_watchpoint} is called, it
970 decrements the reference count, and only calls
971 @code{I386_DR_LOW_RESET_ADDR} and @code{I386_DR_LOW_SET_CONTROL} when
972 the count goes to zero.
973
974 @findex i386_insert_hw_breakpoint
975 @findex i386_remove_hw_breakpoint
976 @item i386_insert_hw_breakpoint (@var{bp_tgt})
977 @itemx i386_remove_hw_breakpoint (@var{bp_tgt})
978 These functions insert and remove hardware-assisted breakpoints.  The
979 macros @code{target_insert_hw_breakpoint} and
980 @code{target_remove_hw_breakpoint} are set to call these functions.
981 The argument is a @code{struct bp_target_info *}, as described in
982 the documentation for @code{target_insert_breakpoint}.
983 These functions work like @code{i386_insert_watchpoint} and
984 @code{i386_remove_watchpoint}, respectively, except that they set up
985 the debug registers to watch instruction execution, and each
986 hardware-assisted breakpoint always requires exactly one debug
987 register.
988
989 @findex i386_cleanup_dregs
990 @item i386_cleanup_dregs (void)
991 This function clears all the reference counts, addresses, and control
992 bits in the mirror images of the debug registers.  It doesn't affect
993 the actual debug registers in the inferior process.
994 @end table
995
996 @noindent
997 @strong{Notes:}
998 @enumerate 1
999 @item
1000 x86 processors support setting watchpoints on I/O reads or writes.
1001 However, since no target supports this (as of March 2001), and since
1002 @code{enum target_hw_bp_type} doesn't even have an enumeration for I/O
1003 watchpoints, this feature is not yet available to @value{GDBN} running
1004 on x86.
1005
1006 @item
1007 x86 processors can enable watchpoints locally, for the current task
1008 only, or globally, for all the tasks.  For each debug register,
1009 there's a bit in the DR7 Debug Control register that determines
1010 whether the associated address is watched locally or globally.  The
1011 current implementation of x86 watchpoint support in @value{GDBN}
1012 always sets watchpoints to be locally enabled, since global
1013 watchpoints might interfere with the underlying OS and are probably
1014 unavailable in many platforms.
1015 @end enumerate
1016
1017 @section Checkpoints
1018 @cindex checkpoints
1019 @cindex restart
1020 In the abstract, a checkpoint is a point in the execution history of
1021 the program, which the user may wish to return to at some later time.
1022
1023 Internally, a checkpoint is a saved copy of the program state, including
1024 whatever information is required in order to restore the program to that
1025 state at a later time.  This can be expected to include the state of 
1026 registers and memory, and may include external state such as the state
1027 of open files and devices.
1028
1029 There are a number of ways in which checkpoints may be implemented
1030 in gdb, e.g.@: as corefiles, as forked processes, and as some opaque
1031 method implemented on the target side.
1032
1033 A corefile can be used to save an image of target memory and register
1034 state, which can in principle be restored later --- but corefiles do
1035 not typically include information about external entities such as 
1036 open files.  Currently this method is not implemented in gdb.
1037
1038 A forked process can save the state of user memory and registers, 
1039 as well as some subset of external (kernel) state.  This method 
1040 is used to implement checkpoints on Linux, and in principle might
1041 be used on other systems.
1042
1043 Some targets, e.g.@: simulators, might have their own built-in 
1044 method for saving checkpoints, and gdb might be able to take
1045 advantage of that capability without necessarily knowing any
1046 details of how it is done.
1047
1048
1049 @section Observing changes in @value{GDBN} internals
1050 @cindex observer pattern interface
1051 @cindex notifications about changes in internals
1052
1053 In order to function properly, several modules need to be notified when
1054 some changes occur in the @value{GDBN} internals.  Traditionally, these
1055 modules have relied on several paradigms, the most common ones being
1056 hooks and gdb-events.  Unfortunately, none of these paradigms was
1057 versatile enough to become the standard notification mechanism in
1058 @value{GDBN}.  The fact that they only supported one ``client'' was also
1059 a strong limitation.
1060
1061 A new paradigm, based on the Observer pattern of the @cite{Design
1062 Patterns} book, has therefore been implemented.  The goal was to provide
1063 a new interface overcoming the issues with the notification mechanisms
1064 previously available.  This new interface needed to be strongly typed,
1065 easy to extend, and versatile enough to be used as the standard
1066 interface when adding new notifications.
1067
1068 See @ref{GDB Observers} for a brief description of the observers
1069 currently implemented in GDB. The rationale for the current
1070 implementation is also briefly discussed.
1071
1072 @node User Interface
1073
1074 @chapter User Interface
1075
1076 @value{GDBN} has several user interfaces, of which the traditional
1077 command-line interface is perhaps the most familiar.
1078
1079 @section Command Interpreter
1080
1081 @cindex command interpreter
1082 @cindex CLI
1083 The command interpreter in @value{GDBN} is fairly simple.  It is designed to
1084 allow for the set of commands to be augmented dynamically, and also
1085 has a recursive subcommand capability, where the first argument to
1086 a command may itself direct a lookup on a different command list.
1087
1088 For instance, the @samp{set} command just starts a lookup on the
1089 @code{setlist} command list, while @samp{set thread} recurses
1090 to the @code{set_thread_cmd_list}.
1091
1092 @findex add_cmd
1093 @findex add_com
1094 To add commands in general, use @code{add_cmd}.  @code{add_com} adds to
1095 the main command list, and should be used for those commands.  The usual
1096 place to add commands is in the @code{_initialize_@var{xyz}} routines at
1097 the ends of most source files.
1098
1099 @findex add_setshow_cmd
1100 @findex add_setshow_cmd_full
1101 To add paired @samp{set} and @samp{show} commands, use
1102 @code{add_setshow_cmd} or @code{add_setshow_cmd_full}.  The former is
1103 a slightly simpler interface which is useful when you don't need to
1104 further modify the new command structures, while the latter returns
1105 the new command structures for manipulation.
1106
1107 @cindex deprecating commands
1108 @findex deprecate_cmd
1109 Before removing commands from the command set it is a good idea to
1110 deprecate them for some time.  Use @code{deprecate_cmd} on commands or
1111 aliases to set the deprecated flag.  @code{deprecate_cmd} takes a
1112 @code{struct cmd_list_element} as it's first argument.  You can use the
1113 return value from @code{add_com} or @code{add_cmd} to deprecate the
1114 command immediately after it is created.
1115
1116 The first time a command is used the user will be warned and offered a
1117 replacement (if one exists). Note that the replacement string passed to
1118 @code{deprecate_cmd} should be the full name of the command, i.e., the
1119 entire string the user should type at the command line.
1120
1121 @anchor{UI-Independent Output}
1122 @section UI-Independent Output---the @code{ui_out} Functions
1123 @c This section is based on the documentation written by Fernando
1124 @c Nasser <fnasser@redhat.com>.
1125
1126 @cindex @code{ui_out} functions
1127 The @code{ui_out} functions present an abstraction level for the
1128 @value{GDBN} output code.  They hide the specifics of different user
1129 interfaces supported by @value{GDBN}, and thus free the programmer
1130 from the need to write several versions of the same code, one each for
1131 every UI, to produce output.
1132
1133 @subsection Overview and Terminology
1134
1135 In general, execution of each @value{GDBN} command produces some sort
1136 of output, and can even generate an input request.
1137
1138 Output can be generated for the following purposes:
1139
1140 @itemize @bullet
1141 @item
1142 to display a @emph{result} of an operation;
1143
1144 @item
1145 to convey @emph{info} or produce side-effects of a requested
1146 operation;
1147
1148 @item
1149 to provide a @emph{notification} of an asynchronous event (including
1150 progress indication of a prolonged asynchronous operation);
1151
1152 @item
1153 to display @emph{error messages} (including warnings);
1154
1155 @item
1156 to show @emph{debug data};
1157
1158 @item
1159 to @emph{query} or prompt a user for input (a special case).
1160 @end itemize
1161
1162 @noindent
1163 This section mainly concentrates on how to build result output,
1164 although some of it also applies to other kinds of output.
1165
1166 Generation of output that displays the results of an operation
1167 involves one or more of the following:
1168
1169 @itemize @bullet
1170 @item
1171 output of the actual data
1172
1173 @item
1174 formatting the output as appropriate for console output, to make it
1175 easily readable by humans
1176
1177 @item
1178 machine oriented formatting--a more terse formatting to allow for easy
1179 parsing by programs which read @value{GDBN}'s output
1180
1181 @item
1182 annotation, whose purpose is to help legacy GUIs to identify interesting
1183 parts in the output
1184 @end itemize
1185
1186 The @code{ui_out} routines take care of the first three aspects.
1187 Annotations are provided by separate annotation routines.  Note that use
1188 of annotations for an interface between a GUI and @value{GDBN} is
1189 deprecated.
1190
1191 Output can be in the form of a single item, which we call a @dfn{field};
1192 a @dfn{list} consisting of identical fields; a @dfn{tuple} consisting of
1193 non-identical fields; or a @dfn{table}, which is a tuple consisting of a
1194 header and a body.  In a BNF-like form:
1195
1196 @table @code
1197 @item <table> @expansion{}
1198 @code{<header> <body>}
1199 @item <header> @expansion{}
1200 @code{@{ <column> @}}
1201 @item <column> @expansion{}
1202 @code{<width> <alignment> <title>}
1203 @item <body> @expansion{}
1204 @code{@{<row>@}}
1205 @end table
1206
1207
1208 @subsection General Conventions
1209
1210 Most @code{ui_out} routines are of type @code{void}, the exceptions are
1211 @code{ui_out_stream_new} (which returns a pointer to the newly created
1212 object) and the @code{make_cleanup} routines.
1213
1214 The first parameter is always the @code{ui_out} vector object, a pointer
1215 to a @code{struct ui_out}.
1216
1217 The @var{format} parameter is like in @code{printf} family of functions.
1218 When it is present, there must also be a variable list of arguments
1219 sufficient used to satisfy the @code{%} specifiers in the supplied
1220 format.
1221
1222 When a character string argument is not used in a @code{ui_out} function
1223 call, a @code{NULL} pointer has to be supplied instead.
1224
1225
1226 @subsection Table, Tuple and List Functions
1227
1228 @cindex list output functions
1229 @cindex table output functions
1230 @cindex tuple output functions
1231 This section introduces @code{ui_out} routines for building lists,
1232 tuples and tables.  The routines to output the actual data items
1233 (fields) are presented in the next section.
1234
1235 To recap: A @dfn{tuple} is a sequence of @dfn{fields}, each field
1236 containing information about an object; a @dfn{list} is a sequence of
1237 fields where each field describes an identical object.
1238
1239 Use the @dfn{table} functions when your output consists of a list of
1240 rows (tuples) and the console output should include a heading.  Use this
1241 even when you are listing just one object but you still want the header.
1242
1243 @cindex nesting level in @code{ui_out} functions
1244 Tables can not be nested.  Tuples and lists can be nested up to a
1245 maximum of five levels.
1246
1247 The overall structure of the table output code is something like this:
1248
1249 @smallexample
1250   ui_out_table_begin
1251     ui_out_table_header
1252     @dots{}
1253     ui_out_table_body
1254       ui_out_tuple_begin
1255         ui_out_field_*
1256         @dots{}
1257       ui_out_tuple_end
1258       @dots{}
1259   ui_out_table_end
1260 @end smallexample
1261
1262 Here is the description of table-, tuple- and list-related @code{ui_out}
1263 functions:
1264
1265 @deftypefun void ui_out_table_begin (struct ui_out *@var{uiout}, int @var{nbrofcols}, int @var{nr_rows}, const char *@var{tblid})
1266 The function @code{ui_out_table_begin} marks the beginning of the output
1267 of a table.  It should always be called before any other @code{ui_out}
1268 function for a given table.  @var{nbrofcols} is the number of columns in
1269 the table. @var{nr_rows} is the number of rows in the table.
1270 @var{tblid} is an optional string identifying the table.  The string
1271 pointed to by @var{tblid} is copied by the implementation of
1272 @code{ui_out_table_begin}, so the application can free the string if it
1273 was @code{malloc}ed.
1274
1275 The companion function @code{ui_out_table_end}, described below, marks
1276 the end of the table's output.
1277 @end deftypefun
1278
1279 @deftypefun void ui_out_table_header (struct ui_out *@var{uiout}, int @var{width}, enum ui_align @var{alignment}, const char *@var{colhdr})
1280 @code{ui_out_table_header} provides the header information for a single
1281 table column.  You call this function several times, one each for every
1282 column of the table, after @code{ui_out_table_begin}, but before
1283 @code{ui_out_table_body}.
1284
1285 The value of @var{width} gives the column width in characters.  The
1286 value of @var{alignment} is one of @code{left}, @code{center}, and
1287 @code{right}, and it specifies how to align the header: left-justify,
1288 center, or right-justify it.  @var{colhdr} points to a string that
1289 specifies the column header; the implementation copies that string, so
1290 column header strings in @code{malloc}ed storage can be freed after the
1291 call.
1292 @end deftypefun
1293
1294 @deftypefun void ui_out_table_body (struct ui_out *@var{uiout})
1295 This function delimits the table header from the table body.
1296 @end deftypefun
1297
1298 @deftypefun void ui_out_table_end (struct ui_out *@var{uiout})
1299 This function signals the end of a table's output.  It should be called
1300 after the table body has been produced by the list and field output
1301 functions.
1302
1303 There should be exactly one call to @code{ui_out_table_end} for each
1304 call to @code{ui_out_table_begin}, otherwise the @code{ui_out} functions
1305 will signal an internal error.
1306 @end deftypefun
1307
1308 The output of the tuples that represent the table rows must follow the
1309 call to @code{ui_out_table_body} and precede the call to
1310 @code{ui_out_table_end}.  You build a tuple by calling
1311 @code{ui_out_tuple_begin} and @code{ui_out_tuple_end}, with suitable
1312 calls to functions which actually output fields between them.
1313
1314 @deftypefun void ui_out_tuple_begin (struct ui_out *@var{uiout}, const char *@var{id})
1315 This function marks the beginning of a tuple output.  @var{id} points
1316 to an optional string that identifies the tuple; it is copied by the
1317 implementation, and so strings in @code{malloc}ed storage can be freed
1318 after the call.
1319 @end deftypefun
1320
1321 @deftypefun void ui_out_tuple_end (struct ui_out *@var{uiout})
1322 This function signals an end of a tuple output.  There should be exactly
1323 one call to @code{ui_out_tuple_end} for each call to
1324 @code{ui_out_tuple_begin}, otherwise an internal @value{GDBN} error will
1325 be signaled.
1326 @end deftypefun
1327
1328 @deftypefun {struct cleanup *} make_cleanup_ui_out_tuple_begin_end (struct ui_out *@var{uiout}, const char *@var{id})
1329 This function first opens the tuple and then establishes a cleanup
1330 (@pxref{Coding, Cleanups}) to close the tuple.  It provides a convenient
1331 and correct implementation of the non-portable@footnote{The function
1332 cast is not portable ISO C.} code sequence:
1333 @smallexample
1334 struct cleanup *old_cleanup;
1335 ui_out_tuple_begin (uiout, "...");
1336 old_cleanup = make_cleanup ((void(*)(void *)) ui_out_tuple_end,
1337                             uiout);
1338 @end smallexample
1339 @end deftypefun
1340
1341 @deftypefun void ui_out_list_begin (struct ui_out *@var{uiout}, const char *@var{id})
1342 This function marks the beginning of a list output.  @var{id} points to
1343 an optional string that identifies the list; it is copied by the
1344 implementation, and so strings in @code{malloc}ed storage can be freed
1345 after the call.
1346 @end deftypefun
1347
1348 @deftypefun void ui_out_list_end (struct ui_out *@var{uiout})
1349 This function signals an end of a list output.  There should be exactly
1350 one call to @code{ui_out_list_end} for each call to
1351 @code{ui_out_list_begin}, otherwise an internal @value{GDBN} error will
1352 be signaled.
1353 @end deftypefun
1354
1355 @deftypefun {struct cleanup *} make_cleanup_ui_out_list_begin_end (struct ui_out *@var{uiout}, const char *@var{id})
1356 Similar to @code{make_cleanup_ui_out_tuple_begin_end}, this function
1357 opens a list and then establishes cleanup (@pxref{Coding, Cleanups})
1358 that will close the list.
1359 @end deftypefun
1360
1361 @subsection Item Output Functions
1362
1363 @cindex item output functions
1364 @cindex field output functions
1365 @cindex data output
1366 The functions described below produce output for the actual data
1367 items, or fields, which contain information about the object.
1368
1369 Choose the appropriate function accordingly to your particular needs.
1370
1371 @deftypefun void ui_out_field_fmt (struct ui_out *@var{uiout}, char *@var{fldname}, char *@var{format}, ...)
1372 This is the most general output function.  It produces the
1373 representation of the data in the variable-length argument list
1374 according to formatting specifications in @var{format}, a
1375 @code{printf}-like format string.  The optional argument @var{fldname}
1376 supplies the name of the field.  The data items themselves are
1377 supplied as additional arguments after @var{format}.
1378
1379 This generic function should be used only when it is not possible to
1380 use one of the specialized versions (see below).
1381 @end deftypefun
1382
1383 @deftypefun void ui_out_field_int (struct ui_out *@var{uiout}, const char *@var{fldname}, int @var{value})
1384 This function outputs a value of an @code{int} variable.  It uses the
1385 @code{"%d"} output conversion specification.  @var{fldname} specifies
1386 the name of the field.
1387 @end deftypefun
1388
1389 @deftypefun void ui_out_field_fmt_int (struct ui_out *@var{uiout}, int @var{width}, enum ui_align @var{alignment}, const char *@var{fldname}, int @var{value})
1390 This function outputs a value of an @code{int} variable.  It differs from
1391 @code{ui_out_field_int} in that the caller specifies the desired @var{width} and @var{alignment} of the output.
1392 @var{fldname} specifies
1393 the name of the field.
1394 @end deftypefun
1395
1396 @deftypefun void ui_out_field_core_addr (struct ui_out *@var{uiout}, const char *@var{fldname}, struct gdbarch *@var{gdbarch}, CORE_ADDR @var{address})
1397 This function outputs an address as appropriate for @var{gdbarch}.
1398 @end deftypefun
1399
1400 @deftypefun void ui_out_field_string (struct ui_out *@var{uiout}, const char *@var{fldname}, const char *@var{string})
1401 This function outputs a string using the @code{"%s"} conversion
1402 specification.
1403 @end deftypefun
1404
1405 Sometimes, there's a need to compose your output piece by piece using
1406 functions that operate on a stream, such as @code{value_print} or
1407 @code{fprintf_symbol_filtered}.  These functions accept an argument of
1408 the type @code{struct ui_file *}, a pointer to a @code{ui_file} object
1409 used to store the data stream used for the output.  When you use one
1410 of these functions, you need a way to pass their results stored in a
1411 @code{ui_file} object to the @code{ui_out} functions.  To this end,
1412 you first create a @code{ui_stream} object by calling
1413 @code{ui_out_stream_new}, pass the @code{stream} member of that
1414 @code{ui_stream} object to @code{value_print} and similar functions,
1415 and finally call @code{ui_out_field_stream} to output the field you
1416 constructed.  When the @code{ui_stream} object is no longer needed,
1417 you should destroy it and free its memory by calling
1418 @code{ui_out_stream_delete}.
1419
1420 @deftypefun {struct ui_stream *} ui_out_stream_new (struct ui_out *@var{uiout})
1421 This function creates a new @code{ui_stream} object which uses the
1422 same output methods as the @code{ui_out} object whose pointer is
1423 passed in @var{uiout}.  It returns a pointer to the newly created
1424 @code{ui_stream} object.
1425 @end deftypefun
1426
1427 @deftypefun void ui_out_stream_delete (struct ui_stream *@var{streambuf})
1428 This functions destroys a @code{ui_stream} object specified by
1429 @var{streambuf}.
1430 @end deftypefun
1431
1432 @deftypefun void ui_out_field_stream (struct ui_out *@var{uiout}, const char *@var{fieldname}, struct ui_stream *@var{streambuf})
1433 This function consumes all the data accumulated in
1434 @code{streambuf->stream} and outputs it like
1435 @code{ui_out_field_string} does.  After a call to
1436 @code{ui_out_field_stream}, the accumulated data no longer exists, but
1437 the stream is still valid and may be used for producing more fields.
1438 @end deftypefun
1439
1440 @strong{Important:} If there is any chance that your code could bail
1441 out before completing output generation and reaching the point where
1442 @code{ui_out_stream_delete} is called, it is necessary to set up a
1443 cleanup, to avoid leaking memory and other resources.  Here's a
1444 skeleton code to do that:
1445
1446 @smallexample
1447  struct ui_stream *mybuf = ui_out_stream_new (uiout);
1448  struct cleanup *old = make_cleanup (ui_out_stream_delete, mybuf);
1449  ...
1450  do_cleanups (old);
1451 @end smallexample
1452
1453 If the function already has the old cleanup chain set (for other kinds
1454 of cleanups), you just have to add your cleanup to it:
1455
1456 @smallexample
1457   mybuf = ui_out_stream_new (uiout);
1458   make_cleanup (ui_out_stream_delete, mybuf);
1459 @end smallexample
1460
1461 Note that with cleanups in place, you should not call
1462 @code{ui_out_stream_delete} directly, or you would attempt to free the
1463 same buffer twice.
1464
1465 @subsection Utility Output Functions
1466
1467 @deftypefun void ui_out_field_skip (struct ui_out *@var{uiout}, const char *@var{fldname})
1468 This function skips a field in a table.  Use it if you have to leave
1469 an empty field without disrupting the table alignment.  The argument
1470 @var{fldname} specifies a name for the (missing) filed.
1471 @end deftypefun
1472
1473 @deftypefun void ui_out_text (struct ui_out *@var{uiout}, const char *@var{string})
1474 This function outputs the text in @var{string} in a way that makes it
1475 easy to be read by humans.  For example, the console implementation of
1476 this method filters the text through a built-in pager, to prevent it
1477 from scrolling off the visible portion of the screen.
1478
1479 Use this function for printing relatively long chunks of text around
1480 the actual field data: the text it produces is not aligned according
1481 to the table's format.  Use @code{ui_out_field_string} to output a
1482 string field, and use @code{ui_out_message}, described below, to
1483 output short messages.
1484 @end deftypefun
1485
1486 @deftypefun void ui_out_spaces (struct ui_out *@var{uiout}, int @var{nspaces})
1487 This function outputs @var{nspaces} spaces.  It is handy to align the
1488 text produced by @code{ui_out_text} with the rest of the table or
1489 list.
1490 @end deftypefun
1491
1492 @deftypefun void ui_out_message (struct ui_out *@var{uiout}, int @var{verbosity}, const char *@var{format}, ...)
1493 This function produces a formatted message, provided that the current
1494 verbosity level is at least as large as given by @var{verbosity}.  The
1495 current verbosity level is specified by the user with the @samp{set
1496 verbositylevel} command.@footnote{As of this writing (April 2001),
1497 setting verbosity level is not yet implemented, and is always returned
1498 as zero.  So calling @code{ui_out_message} with a @var{verbosity}
1499 argument more than zero will cause the message to never be printed.}
1500 @end deftypefun
1501
1502 @deftypefun void ui_out_wrap_hint (struct ui_out *@var{uiout}, char *@var{indent})
1503 This function gives the console output filter (a paging filter) a hint
1504 of where to break lines which are too long.  Ignored for all other
1505 output consumers.  @var{indent}, if non-@code{NULL}, is the string to
1506 be printed to indent the wrapped text on the next line; it must remain
1507 accessible until the next call to @code{ui_out_wrap_hint}, or until an
1508 explicit newline is produced by one of the other functions.  If
1509 @var{indent} is @code{NULL}, the wrapped text will not be indented.
1510 @end deftypefun
1511
1512 @deftypefun void ui_out_flush (struct ui_out *@var{uiout})
1513 This function flushes whatever output has been accumulated so far, if
1514 the UI buffers output.
1515 @end deftypefun
1516
1517
1518 @subsection Examples of Use of @code{ui_out} functions
1519
1520 @cindex using @code{ui_out} functions
1521 @cindex @code{ui_out} functions, usage examples
1522 This section gives some practical examples of using the @code{ui_out}
1523 functions to generalize the old console-oriented code in
1524 @value{GDBN}.  The examples all come from functions defined on the
1525 @file{breakpoints.c} file.
1526
1527 This example, from the @code{breakpoint_1} function, shows how to
1528 produce a table.
1529
1530 The original code was:
1531
1532 @smallexample
1533  if (!found_a_breakpoint++)
1534    @{
1535      annotate_breakpoints_headers ();
1536
1537      annotate_field (0);
1538      printf_filtered ("Num ");
1539      annotate_field (1);
1540      printf_filtered ("Type           ");
1541      annotate_field (2);
1542      printf_filtered ("Disp ");
1543      annotate_field (3);
1544      printf_filtered ("Enb ");
1545      if (addressprint)
1546        @{
1547          annotate_field (4);
1548          printf_filtered ("Address    ");
1549        @}
1550      annotate_field (5);
1551      printf_filtered ("What\n");
1552
1553      annotate_breakpoints_table ();
1554    @}
1555 @end smallexample
1556
1557 Here's the new version:
1558
1559 @smallexample
1560   nr_printable_breakpoints = @dots{};
1561
1562   if (addressprint)
1563     ui_out_table_begin (ui, 6, nr_printable_breakpoints, "BreakpointTable");
1564   else
1565     ui_out_table_begin (ui, 5, nr_printable_breakpoints, "BreakpointTable");
1566
1567   if (nr_printable_breakpoints > 0)
1568     annotate_breakpoints_headers ();
1569   if (nr_printable_breakpoints > 0)
1570     annotate_field (0);
1571   ui_out_table_header (uiout, 3, ui_left, "number", "Num");             /* 1 */
1572   if (nr_printable_breakpoints > 0)
1573     annotate_field (1);
1574   ui_out_table_header (uiout, 14, ui_left, "type", "Type");             /* 2 */
1575   if (nr_printable_breakpoints > 0)
1576     annotate_field (2);
1577   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");              /* 3 */
1578   if (nr_printable_breakpoints > 0)
1579     annotate_field (3);
1580   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");    /* 4 */
1581   if (addressprint)
1582     @{
1583      if (nr_printable_breakpoints > 0)
1584        annotate_field (4);
1585      if (print_address_bits <= 32)
1586        ui_out_table_header (uiout, 10, ui_left, "addr", "Address");/* 5 */
1587      else
1588        ui_out_table_header (uiout, 18, ui_left, "addr", "Address");/* 5 */
1589     @}
1590   if (nr_printable_breakpoints > 0)
1591     annotate_field (5);
1592   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");  /* 6 */
1593   ui_out_table_body (uiout);
1594   if (nr_printable_breakpoints > 0)
1595     annotate_breakpoints_table ();
1596 @end smallexample
1597
1598 This example, from the @code{print_one_breakpoint} function, shows how
1599 to produce the actual data for the table whose structure was defined
1600 in the above example.  The original code was:
1601
1602 @smallexample
1603    annotate_record ();
1604    annotate_field (0);
1605    printf_filtered ("%-3d ", b->number);
1606    annotate_field (1);
1607    if ((int)b->type > (sizeof(bptypes)/sizeof(bptypes[0]))
1608        || ((int) b->type != bptypes[(int) b->type].type))
1609      internal_error ("bptypes table does not describe type #%d.",
1610                      (int)b->type);
1611    printf_filtered ("%-14s ", bptypes[(int)b->type].description);
1612    annotate_field (2);
1613    printf_filtered ("%-4s ", bpdisps[(int)b->disposition]);
1614    annotate_field (3);
1615    printf_filtered ("%-3c ", bpenables[(int)b->enable]);
1616    @dots{}
1617 @end smallexample
1618
1619 This is the new version:
1620
1621 @smallexample
1622    annotate_record ();
1623    ui_out_tuple_begin (uiout, "bkpt");
1624    annotate_field (0);
1625    ui_out_field_int (uiout, "number", b->number);
1626    annotate_field (1);
1627    if (((int) b->type > (sizeof (bptypes) / sizeof (bptypes[0])))
1628        || ((int) b->type != bptypes[(int) b->type].type))
1629      internal_error ("bptypes table does not describe type #%d.",
1630                      (int) b->type);
1631    ui_out_field_string (uiout, "type", bptypes[(int)b->type].description);
1632    annotate_field (2);
1633    ui_out_field_string (uiout, "disp", bpdisps[(int)b->disposition]);
1634    annotate_field (3);
1635    ui_out_field_fmt (uiout, "enabled", "%c", bpenables[(int)b->enable]);
1636    @dots{}
1637 @end smallexample
1638
1639 This example, also from @code{print_one_breakpoint}, shows how to
1640 produce a complicated output field using the @code{print_expression}
1641 functions which requires a stream to be passed.  It also shows how to
1642 automate stream destruction with cleanups.  The original code was:
1643
1644 @smallexample
1645     annotate_field (5);
1646     print_expression (b->exp, gdb_stdout);
1647 @end smallexample
1648
1649 The new version is:
1650
1651 @smallexample
1652   struct ui_stream *stb = ui_out_stream_new (uiout);
1653   struct cleanup *old_chain = make_cleanup_ui_out_stream_delete (stb);
1654   ...
1655   annotate_field (5);
1656   print_expression (b->exp, stb->stream);
1657   ui_out_field_stream (uiout, "what", local_stream);
1658 @end smallexample
1659
1660 This example, also from @code{print_one_breakpoint}, shows how to use
1661 @code{ui_out_text} and @code{ui_out_field_string}.  The original code
1662 was:
1663
1664 @smallexample
1665   annotate_field (5);
1666   if (b->dll_pathname == NULL)
1667     printf_filtered ("<any library> ");
1668   else
1669     printf_filtered ("library \"%s\" ", b->dll_pathname);
1670 @end smallexample
1671
1672 It became:
1673
1674 @smallexample
1675   annotate_field (5);
1676   if (b->dll_pathname == NULL)
1677     @{
1678       ui_out_field_string (uiout, "what", "<any library>");
1679       ui_out_spaces (uiout, 1);
1680     @}
1681   else
1682     @{
1683       ui_out_text (uiout, "library \"");
1684       ui_out_field_string (uiout, "what", b->dll_pathname);
1685       ui_out_text (uiout, "\" ");
1686     @}
1687 @end smallexample
1688
1689 The following example from @code{print_one_breakpoint} shows how to
1690 use @code{ui_out_field_int} and @code{ui_out_spaces}.  The original
1691 code was:
1692
1693 @smallexample
1694   annotate_field (5);
1695   if (b->forked_inferior_pid != 0)
1696     printf_filtered ("process %d ", b->forked_inferior_pid);
1697 @end smallexample
1698
1699 It became:
1700
1701 @smallexample
1702   annotate_field (5);
1703   if (b->forked_inferior_pid != 0)
1704     @{
1705       ui_out_text (uiout, "process ");
1706       ui_out_field_int (uiout, "what", b->forked_inferior_pid);
1707       ui_out_spaces (uiout, 1);
1708     @}
1709 @end smallexample
1710
1711 Here's an example of using @code{ui_out_field_string}.  The original
1712 code was:
1713
1714 @smallexample
1715   annotate_field (5);
1716   if (b->exec_pathname != NULL)
1717     printf_filtered ("program \"%s\" ", b->exec_pathname);
1718 @end smallexample
1719
1720 It became:
1721
1722 @smallexample
1723   annotate_field (5);
1724   if (b->exec_pathname != NULL)
1725     @{
1726       ui_out_text (uiout, "program \"");
1727       ui_out_field_string (uiout, "what", b->exec_pathname);
1728       ui_out_text (uiout, "\" ");
1729     @}
1730 @end smallexample
1731
1732 Finally, here's an example of printing an address.  The original code:
1733
1734 @smallexample
1735   annotate_field (4);
1736   printf_filtered ("%s ",
1737         hex_string_custom ((unsigned long) b->address, 8));
1738 @end smallexample
1739
1740 It became:
1741
1742 @smallexample
1743   annotate_field (4);
1744   ui_out_field_core_addr (uiout, "Address", b->address);
1745 @end smallexample
1746
1747
1748 @section Console Printing
1749
1750 @section TUI
1751
1752 @node libgdb
1753
1754 @chapter libgdb
1755
1756 @section libgdb 1.0
1757 @cindex @code{libgdb}
1758 @code{libgdb} 1.0 was an abortive project of years ago.  The theory was
1759 to provide an API to @value{GDBN}'s functionality.
1760
1761 @section libgdb 2.0
1762 @cindex @code{libgdb}
1763 @code{libgdb} 2.0 is an ongoing effort to update @value{GDBN} so that is
1764 better able to support graphical and other environments.
1765
1766 Since @code{libgdb} development is on-going, its architecture is still
1767 evolving.  The following components have so far been identified:
1768
1769 @itemize @bullet
1770 @item
1771 Observer - @file{gdb-events.h}.  
1772 @item
1773 Builder - @file{ui-out.h}
1774 @item
1775 Event Loop - @file{event-loop.h}
1776 @item
1777 Library - @file{gdb.h}
1778 @end itemize
1779
1780 The model that ties these components together is described below.
1781
1782 @section The @code{libgdb} Model
1783
1784 A client of @code{libgdb} interacts with the library in two ways.
1785
1786 @itemize @bullet
1787 @item
1788 As an observer (using @file{gdb-events}) receiving notifications from
1789 @code{libgdb} of any internal state changes (break point changes, run
1790 state, etc).
1791 @item
1792 As a client querying @code{libgdb} (using the @file{ui-out} builder) to
1793 obtain various status values from @value{GDBN}.
1794 @end itemize
1795
1796 Since @code{libgdb} could have multiple clients (e.g., a GUI supporting
1797 the existing @value{GDBN} CLI), those clients must co-operate when
1798 controlling @code{libgdb}.  In particular, a client must ensure that
1799 @code{libgdb} is idle (i.e.@: no other client is using @code{libgdb})
1800 before responding to a @file{gdb-event} by making a query.
1801
1802 @section CLI support
1803
1804 At present @value{GDBN}'s CLI is very much entangled in with the core of
1805 @code{libgdb}.  Consequently, a client wishing to include the CLI in
1806 their interface needs to carefully co-ordinate its own and the CLI's
1807 requirements.
1808
1809 It is suggested that the client set @code{libgdb} up to be bi-modal
1810 (alternate between CLI and client query modes).  The notes below sketch
1811 out the theory:
1812
1813 @itemize @bullet
1814 @item
1815 The client registers itself as an observer of @code{libgdb}.
1816 @item
1817 The client create and install @code{cli-out} builder using its own
1818 versions of the @code{ui-file} @code{gdb_stderr}, @code{gdb_stdtarg} and
1819 @code{gdb_stdout} streams.
1820 @item
1821 The client creates a separate custom @code{ui-out} builder that is only
1822 used while making direct queries to @code{libgdb}.
1823 @end itemize
1824
1825 When the client receives input intended for the CLI, it simply passes it
1826 along.  Since the @code{cli-out} builder is installed by default, all
1827 the CLI output in response to that command is routed (pronounced rooted)
1828 through to the client controlled @code{gdb_stdout} et.@: al.@: streams.
1829 At the same time, the client is kept abreast of internal changes by
1830 virtue of being a @code{libgdb} observer.
1831
1832 The only restriction on the client is that it must wait until
1833 @code{libgdb} becomes idle before initiating any queries (using the
1834 client's custom builder).
1835
1836 @section @code{libgdb} components
1837
1838 @subheading Observer - @file{gdb-events.h}
1839 @file{gdb-events} provides the client with a very raw mechanism that can
1840 be used to implement an observer.  At present it only allows for one
1841 observer and that observer must, internally, handle the need to delay
1842 the processing of any event notifications until after @code{libgdb} has
1843 finished the current command.
1844
1845 @subheading Builder - @file{ui-out.h}
1846 @file{ui-out} provides the infrastructure necessary for a client to
1847 create a builder.  That builder is then passed down to @code{libgdb}
1848 when doing any queries.
1849
1850 @subheading Event Loop - @file{event-loop.h}
1851 @c There could be an entire section on the event-loop
1852 @file{event-loop}, currently non-re-entrant, provides a simple event
1853 loop.  A client would need to either plug its self into this loop or,
1854 implement a new event-loop that @value{GDBN} would use.
1855
1856 The event-loop will eventually be made re-entrant.  This is so that
1857 @value{GDBN} can better handle the problem of some commands blocking
1858 instead of returning.
1859
1860 @subheading Library - @file{gdb.h}
1861 @file{libgdb} is the most obvious component of this system.  It provides
1862 the query interface.  Each function is parameterized by a @code{ui-out}
1863 builder.  The result of the query is constructed using that builder
1864 before the query function returns.
1865
1866 @node Values
1867 @chapter Values
1868 @section Values
1869
1870 @cindex values
1871 @cindex @code{value} structure
1872 @value{GDBN} uses @code{struct value}, or @dfn{values}, as an internal
1873 abstraction for the representation of a variety of inferior objects
1874 and @value{GDBN} convenience objects.
1875
1876 Values have an associated @code{struct type}, that describes a virtual
1877 view of the raw data or object stored in or accessed through the
1878 value.
1879
1880 A value is in addition discriminated by its lvalue-ness, given its
1881 @code{enum lval_type} enumeration type:
1882
1883 @cindex @code{lval_type} enumeration, for values.
1884 @table @code
1885 @item @code{not_lval}
1886 This value is not an lval.  It can't be assigned to.
1887
1888 @item @code{lval_memory}
1889 This value represents an object in memory.
1890
1891 @item @code{lval_register}
1892 This value represents an object that lives in a register.
1893
1894 @item @code{lval_internalvar}
1895 Represents the value of an internal variable.
1896
1897 @item @code{lval_internalvar_component}
1898 Represents part of a @value{GDBN} internal variable.  E.g., a
1899 structure field.
1900
1901 @cindex computed values
1902 @item @code{lval_computed}
1903 These are ``computed'' values.  They allow creating specialized value
1904 objects for specific purposes, all abstracted away from the core value
1905 support code.  The creator of such a value writes specialized
1906 functions to handle the reading and writing to/from the value's
1907 backend data, and optionally, a ``copy operator'' and a
1908 ``destructor''.
1909
1910 Pointers to these functions are stored in a @code{struct lval_funcs}
1911 instance (declared in @file{value.h}), and passed to the
1912 @code{allocate_computed_value} function, as in the example below.
1913
1914 @smallexample
1915 static void
1916 nil_value_read (struct value *v)
1917 @{
1918   /* This callback reads data from some backend, and stores it in V.
1919      In this case, we always read null data.  You'll want to fill in
1920      something more interesting.  */
1921
1922   memset (value_contents_all_raw (v),
1923           value_offset (v),
1924           TYPE_LENGTH (value_type (v)));
1925 @}
1926
1927 static void
1928 nil_value_write (struct value *v, struct value *fromval)
1929 @{
1930   /* Takes the data from FROMVAL and stores it in the backend of V.  */
1931
1932   to_oblivion (value_contents_all_raw (fromval),
1933                value_offset (v),
1934                TYPE_LENGTH (value_type (fromval)));
1935 @}
1936
1937 static struct lval_funcs nil_value_funcs =
1938   @{
1939     nil_value_read,
1940     nil_value_write
1941   @};
1942
1943 struct value *
1944 make_nil_value (void)
1945 @{
1946    struct type *type;
1947    struct value *v;
1948
1949    type = make_nils_type ();
1950    v = allocate_computed_value (type, &nil_value_funcs, NULL);
1951
1952    return v;
1953 @}
1954 @end smallexample
1955
1956 See the implementation of the @code{$_siginfo} convenience variable in
1957 @file{infrun.c} as a real example use of lval_computed.
1958
1959 @end table
1960
1961 @node Stack Frames
1962 @chapter Stack Frames
1963
1964 @cindex frame
1965 @cindex call stack frame
1966 A frame is a construct that @value{GDBN} uses to keep track of calling
1967 and called functions.
1968
1969 @cindex unwind frame
1970 @value{GDBN}'s frame model, a fresh design, was implemented with the
1971 need to support @sc{dwarf}'s Call Frame Information in mind.  In fact,
1972 the term ``unwind'' is taken directly from that specification.
1973 Developers wishing to learn more about unwinders, are encouraged to
1974 read the @sc{dwarf} specification, available from
1975 @url{http://www.dwarfstd.org}.
1976
1977 @findex frame_register_unwind
1978 @findex get_frame_register
1979 @value{GDBN}'s model is that you find a frame's registers by
1980 ``unwinding'' them from the next younger frame.  That is,
1981 @samp{get_frame_register} which returns the value of a register in
1982 frame #1 (the next-to-youngest frame), is implemented by calling frame
1983 #0's @code{frame_register_unwind} (the youngest frame).  But then the
1984 obvious question is: how do you access the registers of the youngest
1985 frame itself?
1986
1987 @cindex sentinel frame
1988 @findex get_frame_type
1989 @vindex SENTINEL_FRAME
1990 To answer this question, @value{GDBN} has the @dfn{sentinel} frame, the
1991 ``-1st'' frame.  Unwinding registers from the sentinel frame gives you
1992 the current values of the youngest real frame's registers.  If @var{f}
1993 is a sentinel frame, then @code{get_frame_type (@var{f}) @equiv{}
1994 SENTINEL_FRAME}.
1995
1996 @section Selecting an Unwinder
1997
1998 @findex frame_unwind_prepend_unwinder
1999 @findex frame_unwind_append_unwinder
2000 The architecture registers a list of frame unwinders (@code{struct
2001 frame_unwind}), using the functions
2002 @code{frame_unwind_prepend_unwinder} and
2003 @code{frame_unwind_append_unwinder}.  Each unwinder includes a
2004 sniffer.  Whenever @value{GDBN} needs to unwind a frame (to fetch the
2005 previous frame's registers or the current frame's ID), it calls
2006 registered sniffers in order to find one which recognizes the frame.
2007 The first time a sniffer returns non-zero, the corresponding unwinder
2008 is assigned to the frame.
2009
2010 @section Unwinding the Frame ID
2011 @cindex frame ID
2012
2013 Every frame has an associated ID, of type @code{struct frame_id}.
2014 The ID includes the stack base and function start address for
2015 the frame.  The ID persists through the entire life of the frame,
2016 including while other called frames are running; it is used to
2017 locate an appropriate @code{struct frame_info} from the cache.
2018
2019 Every time the inferior stops, and at various other times, the frame
2020 cache is flushed.  Because of this, parts of @value{GDBN} which need
2021 to keep track of individual frames cannot use pointers to @code{struct
2022 frame_info}.  A frame ID provides a stable reference to a frame, even
2023 when the unwinder must be run again to generate a new @code{struct
2024 frame_info} for the same frame.
2025
2026 The frame's unwinder's @code{this_id} method is called to find the ID.
2027 Note that this is different from register unwinding, where the next
2028 frame's @code{prev_register} is called to unwind this frame's
2029 registers.
2030
2031 Both stack base and function address are required to identify the
2032 frame, because a recursive function has the same function address for
2033 two consecutive frames and a leaf function may have the same stack
2034 address as its caller.  On some platforms, a third address is part of
2035 the ID to further disambiguate frames---for instance, on IA-64
2036 the separate register stack address is included in the ID.
2037
2038 An invalid frame ID (@code{outer_frame_id}) returned from the
2039 @code{this_id} method means to stop unwinding after this frame.
2040
2041 @code{null_frame_id} is another invalid frame ID which should be used
2042 when there is no frame.  For instance, certain breakpoints are attached
2043 to a specific frame, and that frame is identified through its frame ID
2044 (we use this to implement the "finish" command).  Using
2045 @code{null_frame_id} as the frame ID for a given breakpoint means
2046 that the breakpoint is not specific to any frame.  The @code{this_id}
2047 method should never return @code{null_frame_id}.
2048
2049 @section Unwinding Registers
2050
2051 Each unwinder includes a @code{prev_register} method.  This method
2052 takes a frame, an associated cache pointer, and a register number.
2053 It returns a @code{struct value *} describing the requested register,
2054 as saved by this frame.  This is the value of the register that is
2055 current in this frame's caller.
2056
2057 The returned value must have the same type as the register.  It may
2058 have any lvalue type.  In most circumstances one of these routines
2059 will generate the appropriate value:
2060
2061 @table @code
2062 @item frame_unwind_got_optimized
2063 @findex frame_unwind_got_optimized
2064 This register was not saved.
2065
2066 @item frame_unwind_got_register
2067 @findex frame_unwind_got_register
2068 This register was copied into another register in this frame.  This
2069 is also used for unchanged registers; they are ``copied'' into the
2070 same register.
2071
2072 @item frame_unwind_got_memory
2073 @findex frame_unwind_got_memory
2074 This register was saved in memory.
2075
2076 @item frame_unwind_got_constant
2077 @findex frame_unwind_got_constant
2078 This register was not saved, but the unwinder can compute the previous
2079 value some other way.
2080
2081 @item frame_unwind_got_address
2082 @findex frame_unwind_got_address
2083 Same as @code{frame_unwind_got_constant}, except that the value is a target
2084 address.  This is frequently used for the stack pointer, which is not
2085 explicitly saved but has a known offset from this frame's stack
2086 pointer.  For architectures with a flat unified address space, this is
2087 generally the same as @code{frame_unwind_got_constant}.
2088 @end table
2089
2090 @node Symbol Handling
2091
2092 @chapter Symbol Handling
2093
2094 Symbols are a key part of @value{GDBN}'s operation.  Symbols include
2095 variables, functions, and types.
2096
2097 Symbol information for a large program can be truly massive, and
2098 reading of symbol information is one of the major performance
2099 bottlenecks in @value{GDBN}; it can take many minutes to process it
2100 all.  Studies have shown that nearly all the time spent is
2101 computational, rather than file reading.
2102
2103 One of the ways for @value{GDBN} to provide a good user experience is
2104 to start up quickly, taking no more than a few seconds.  It is simply
2105 not possible to process all of a program's debugging info in that
2106 time, and so we attempt to handle symbols incrementally.  For instance,
2107 we create @dfn{partial symbol tables} consisting of only selected
2108 symbols, and only expand them to full symbol tables when necessary.
2109
2110 @section Symbol Reading
2111
2112 @cindex symbol reading
2113 @cindex reading of symbols
2114 @cindex symbol files
2115 @value{GDBN} reads symbols from @dfn{symbol files}.  The usual symbol
2116 file is the file containing the program which @value{GDBN} is
2117 debugging.  @value{GDBN} can be directed to use a different file for
2118 symbols (with the @samp{symbol-file} command), and it can also read
2119 more symbols via the @samp{add-file} and @samp{load} commands. In
2120 addition, it may bring in more symbols while loading shared
2121 libraries.
2122
2123 @findex find_sym_fns
2124 Symbol files are initially opened by code in @file{symfile.c} using
2125 the BFD library (@pxref{Support Libraries}).  BFD identifies the type
2126 of the file by examining its header.  @code{find_sym_fns} then uses
2127 this identification to locate a set of symbol-reading functions.
2128
2129 @findex add_symtab_fns
2130 @cindex @code{sym_fns} structure
2131 @cindex adding a symbol-reading module
2132 Symbol-reading modules identify themselves to @value{GDBN} by calling
2133 @code{add_symtab_fns} during their module initialization.  The argument
2134 to @code{add_symtab_fns} is a @code{struct sym_fns} which contains the
2135 name (or name prefix) of the symbol format, the length of the prefix,
2136 and pointers to four functions.  These functions are called at various
2137 times to process symbol files whose identification matches the specified
2138 prefix.
2139
2140 The functions supplied by each module are:
2141
2142 @table @code
2143 @item @var{xyz}_symfile_init(struct sym_fns *sf)
2144
2145 @cindex secondary symbol file
2146 Called from @code{symbol_file_add} when we are about to read a new
2147 symbol file.  This function should clean up any internal state (possibly
2148 resulting from half-read previous files, for example) and prepare to
2149 read a new symbol file.  Note that the symbol file which we are reading
2150 might be a new ``main'' symbol file, or might be a secondary symbol file
2151 whose symbols are being added to the existing symbol table.
2152
2153 The argument to @code{@var{xyz}_symfile_init} is a newly allocated
2154 @code{struct sym_fns} whose @code{bfd} field contains the BFD for the
2155 new symbol file being read.  Its @code{private} field has been zeroed,
2156 and can be modified as desired.  Typically, a struct of private
2157 information will be @code{malloc}'d, and a pointer to it will be placed
2158 in the @code{private} field.
2159
2160 There is no result from @code{@var{xyz}_symfile_init}, but it can call
2161 @code{error} if it detects an unavoidable problem.
2162
2163 @item @var{xyz}_new_init()
2164
2165 Called from @code{symbol_file_add} when discarding existing symbols.
2166 This function needs only handle the symbol-reading module's internal
2167 state; the symbol table data structures visible to the rest of
2168 @value{GDBN} will be discarded by @code{symbol_file_add}.  It has no
2169 arguments and no result.  It may be called after
2170 @code{@var{xyz}_symfile_init}, if a new symbol table is being read, or
2171 may be called alone if all symbols are simply being discarded.
2172
2173 @item @var{xyz}_symfile_read(struct sym_fns *sf, CORE_ADDR addr, int mainline)
2174
2175 Called from @code{symbol_file_add} to actually read the symbols from a
2176 symbol-file into a set of psymtabs or symtabs.
2177
2178 @code{sf} points to the @code{struct sym_fns} originally passed to
2179 @code{@var{xyz}_sym_init} for possible initialization.  @code{addr} is
2180 the offset between the file's specified start address and its true
2181 address in memory.  @code{mainline} is 1 if this is the main symbol
2182 table being read, and 0 if a secondary symbol file (e.g., shared library
2183 or dynamically loaded file) is being read.@refill
2184 @end table
2185
2186 In addition, if a symbol-reading module creates psymtabs when
2187 @var{xyz}_symfile_read is called, these psymtabs will contain a pointer
2188 to a function @code{@var{xyz}_psymtab_to_symtab}, which can be called
2189 from any point in the @value{GDBN} symbol-handling code.
2190
2191 @table @code
2192 @item @var{xyz}_psymtab_to_symtab (struct partial_symtab *pst)
2193
2194 Called from @code{psymtab_to_symtab} (or the @code{PSYMTAB_TO_SYMTAB} macro) if
2195 the psymtab has not already been read in and had its @code{pst->symtab}
2196 pointer set.  The argument is the psymtab to be fleshed-out into a
2197 symtab.  Upon return, @code{pst->readin} should have been set to 1, and
2198 @code{pst->symtab} should contain a pointer to the new corresponding symtab, or
2199 zero if there were no symbols in that part of the symbol file.
2200 @end table
2201
2202 @section Partial Symbol Tables
2203
2204 @value{GDBN} has three types of symbol tables:
2205
2206 @itemize @bullet
2207 @cindex full symbol table
2208 @cindex symtabs
2209 @item
2210 Full symbol tables (@dfn{symtabs}).  These contain the main
2211 information about symbols and addresses.
2212
2213 @cindex psymtabs
2214 @item
2215 Partial symbol tables (@dfn{psymtabs}).  These contain enough
2216 information to know when to read the corresponding part of the full
2217 symbol table.
2218
2219 @cindex minimal symbol table
2220 @cindex minsymtabs
2221 @item
2222 Minimal symbol tables (@dfn{msymtabs}).  These contain information
2223 gleaned from non-debugging symbols.
2224 @end itemize
2225
2226 @cindex partial symbol table
2227 This section describes partial symbol tables.
2228
2229 A psymtab is constructed by doing a very quick pass over an executable
2230 file's debugging information.  Small amounts of information are
2231 extracted---enough to identify which parts of the symbol table will
2232 need to be re-read and fully digested later, when the user needs the
2233 information.  The speed of this pass causes @value{GDBN} to start up very
2234 quickly.  Later, as the detailed rereading occurs, it occurs in small
2235 pieces, at various times, and the delay therefrom is mostly invisible to
2236 the user.
2237 @c (@xref{Symbol Reading}.)
2238
2239 The symbols that show up in a file's psymtab should be, roughly, those
2240 visible to the debugger's user when the program is not running code from
2241 that file.  These include external symbols and types, static symbols and
2242 types, and @code{enum} values declared at file scope.
2243
2244 The psymtab also contains the range of instruction addresses that the
2245 full symbol table would represent.
2246
2247 @cindex finding a symbol
2248 @cindex symbol lookup
2249 The idea is that there are only two ways for the user (or much of the
2250 code in the debugger) to reference a symbol:
2251
2252 @itemize @bullet
2253 @findex find_pc_function
2254 @findex find_pc_line
2255 @item
2256 By its address (e.g., execution stops at some address which is inside a
2257 function in this file).  The address will be noticed to be in the
2258 range of this psymtab, and the full symtab will be read in.
2259 @code{find_pc_function}, @code{find_pc_line}, and other
2260 @code{find_pc_@dots{}} functions handle this.
2261
2262 @cindex lookup_symbol
2263 @item
2264 By its name
2265 (e.g., the user asks to print a variable, or set a breakpoint on a
2266 function).  Global names and file-scope names will be found in the
2267 psymtab, which will cause the symtab to be pulled in.  Local names will
2268 have to be qualified by a global name, or a file-scope name, in which
2269 case we will have already read in the symtab as we evaluated the
2270 qualifier.  Or, a local symbol can be referenced when we are ``in'' a
2271 local scope, in which case the first case applies.  @code{lookup_symbol}
2272 does most of the work here.
2273 @end itemize
2274
2275 The only reason that psymtabs exist is to cause a symtab to be read in
2276 at the right moment.  Any symbol that can be elided from a psymtab,
2277 while still causing that to happen, should not appear in it.  Since
2278 psymtabs don't have the idea of scope, you can't put local symbols in
2279 them anyway.  Psymtabs don't have the idea of the type of a symbol,
2280 either, so types need not appear, unless they will be referenced by
2281 name.
2282
2283 It is a bug for @value{GDBN} to behave one way when only a psymtab has
2284 been read, and another way if the corresponding symtab has been read
2285 in.  Such bugs are typically caused by a psymtab that does not contain
2286 all the visible symbols, or which has the wrong instruction address
2287 ranges.
2288
2289 The psymtab for a particular section of a symbol file (objfile) could be
2290 thrown away after the symtab has been read in.  The symtab should always
2291 be searched before the psymtab, so the psymtab will never be used (in a
2292 bug-free environment).  Currently, psymtabs are allocated on an obstack,
2293 and all the psymbols themselves are allocated in a pair of large arrays
2294 on an obstack, so there is little to be gained by trying to free them
2295 unless you want to do a lot more work.
2296
2297 @section Types
2298
2299 @unnumberedsubsec Fundamental Types (e.g., @code{FT_VOID}, @code{FT_BOOLEAN}).
2300
2301 @cindex fundamental types
2302 These are the fundamental types that @value{GDBN} uses internally.  Fundamental
2303 types from the various debugging formats (stabs, ELF, etc) are mapped
2304 into one of these.  They are basically a union of all fundamental types
2305 that @value{GDBN} knows about for all the languages that @value{GDBN}
2306 knows about.
2307
2308 @unnumberedsubsec Type Codes (e.g., @code{TYPE_CODE_PTR}, @code{TYPE_CODE_ARRAY}).
2309
2310 @cindex type codes
2311 Each time @value{GDBN} builds an internal type, it marks it with one
2312 of these types.  The type may be a fundamental type, such as
2313 @code{TYPE_CODE_INT}, or a derived type, such as @code{TYPE_CODE_PTR}
2314 which is a pointer to another type.  Typically, several @code{FT_*}
2315 types map to one @code{TYPE_CODE_*} type, and are distinguished by
2316 other members of the type struct, such as whether the type is signed
2317 or unsigned, and how many bits it uses.
2318
2319 @unnumberedsubsec Builtin Types (e.g., @code{builtin_type_void}, @code{builtin_type_char}).
2320
2321 These are instances of type structs that roughly correspond to
2322 fundamental types and are created as global types for @value{GDBN} to
2323 use for various ugly historical reasons.  We eventually want to
2324 eliminate these.  Note for example that @code{builtin_type_int}
2325 initialized in @file{gdbtypes.c} is basically the same as a
2326 @code{TYPE_CODE_INT} type that is initialized in @file{c-lang.c} for
2327 an @code{FT_INTEGER} fundamental type.  The difference is that the
2328 @code{builtin_type} is not associated with any particular objfile, and
2329 only one instance exists, while @file{c-lang.c} builds as many
2330 @code{TYPE_CODE_INT} types as needed, with each one associated with
2331 some particular objfile.
2332
2333 @section Object File Formats
2334 @cindex object file formats
2335
2336 @subsection a.out
2337
2338 @cindex @code{a.out} format
2339 The @code{a.out} format is the original file format for Unix.  It
2340 consists of three sections: @code{text}, @code{data}, and @code{bss},
2341 which are for program code, initialized data, and uninitialized data,
2342 respectively.
2343
2344 The @code{a.out} format is so simple that it doesn't have any reserved
2345 place for debugging information.  (Hey, the original Unix hackers used
2346 @samp{adb}, which is a machine-language debugger!)  The only debugging
2347 format for @code{a.out} is stabs, which is encoded as a set of normal
2348 symbols with distinctive attributes.
2349
2350 The basic @code{a.out} reader is in @file{dbxread.c}.
2351
2352 @subsection COFF
2353
2354 @cindex COFF format
2355 The COFF format was introduced with System V Release 3 (SVR3) Unix.
2356 COFF files may have multiple sections, each prefixed by a header.  The
2357 number of sections is limited.
2358
2359 The COFF specification includes support for debugging.  Although this
2360 was a step forward, the debugging information was woefully limited.
2361 For instance, it was not possible to represent code that came from an
2362 included file.  GNU's COFF-using configs often use stabs-type info,
2363 encapsulated in special sections.
2364
2365 The COFF reader is in @file{coffread.c}.
2366
2367 @subsection ECOFF
2368
2369 @cindex ECOFF format
2370 ECOFF is an extended COFF originally introduced for Mips and Alpha
2371 workstations.
2372
2373 The basic ECOFF reader is in @file{mipsread.c}.
2374
2375 @subsection XCOFF
2376
2377 @cindex XCOFF format
2378 The IBM RS/6000 running AIX uses an object file format called XCOFF.
2379 The COFF sections, symbols, and line numbers are used, but debugging
2380 symbols are @code{dbx}-style stabs whose strings are located in the
2381 @code{.debug} section (rather than the string table).  For more
2382 information, see @ref{Top,,,stabs,The Stabs Debugging Format}.
2383
2384 The shared library scheme has a clean interface for figuring out what
2385 shared libraries are in use, but the catch is that everything which
2386 refers to addresses (symbol tables and breakpoints at least) needs to be
2387 relocated for both shared libraries and the main executable.  At least
2388 using the standard mechanism this can only be done once the program has
2389 been run (or the core file has been read).
2390
2391 @subsection PE
2392
2393 @cindex PE-COFF format
2394 Windows 95 and NT use the PE (@dfn{Portable Executable}) format for their
2395 executables.  PE is basically COFF with additional headers.
2396
2397 While BFD includes special PE support, @value{GDBN} needs only the basic
2398 COFF reader.
2399
2400 @subsection ELF
2401
2402 @cindex ELF format
2403 The ELF format came with System V Release 4 (SVR4) Unix.  ELF is
2404 similar to COFF in being organized into a number of sections, but it
2405 removes many of COFF's limitations.  Debugging info may be either stabs
2406 encapsulated in ELF sections, or more commonly these days, DWARF.
2407
2408 The basic ELF reader is in @file{elfread.c}.
2409
2410 @subsection SOM
2411
2412 @cindex SOM format
2413 SOM is HP's object file and debug format (not to be confused with IBM's
2414 SOM, which is a cross-language ABI).
2415
2416 The SOM reader is in @file{somread.c}.
2417
2418 @section Debugging File Formats
2419
2420 This section describes characteristics of debugging information that
2421 are independent of the object file format.
2422
2423 @subsection stabs
2424
2425 @cindex stabs debugging info
2426 @code{stabs} started out as special symbols within the @code{a.out}
2427 format.  Since then, it has been encapsulated into other file
2428 formats, such as COFF and ELF.
2429
2430 While @file{dbxread.c} does some of the basic stab processing,
2431 including for encapsulated versions, @file{stabsread.c} does
2432 the real work.
2433
2434 @subsection COFF
2435
2436 @cindex COFF debugging info
2437 The basic COFF definition includes debugging information.  The level
2438 of support is minimal and non-extensible, and is not often used.
2439
2440 @subsection Mips debug (Third Eye)
2441
2442 @cindex ECOFF debugging info
2443 ECOFF includes a definition of a special debug format.
2444
2445 The file @file{mdebugread.c} implements reading for this format.
2446
2447 @c mention DWARF 1 as a formerly-supported format
2448
2449 @subsection DWARF 2
2450
2451 @cindex DWARF 2 debugging info
2452 DWARF 2 is an improved but incompatible version of DWARF 1.
2453
2454 The DWARF 2 reader is in @file{dwarf2read.c}.
2455
2456 @subsection Compressed DWARF 2
2457
2458 @cindex Compressed DWARF 2 debugging info
2459 Compressed DWARF 2 is not technically a separate debugging format, but
2460 merely DWARF 2 debug information that has been compressed.  In this
2461 format, every object-file section holding DWARF 2 debugging
2462 information is compressed and prepended with a header.  (The section
2463 is also typically renamed, so a section called @code{.debug_info} in a
2464 DWARF 2 binary would be called @code{.zdebug_info} in a compressed
2465 DWARF 2 binary.)  The header is 12 bytes long:
2466
2467 @itemize @bullet
2468 @item
2469 4 bytes: the literal string ``ZLIB''
2470 @item
2471 8 bytes: the uncompressed size of the section, in big-endian byte
2472 order.
2473 @end itemize
2474
2475 The same reader is used for both compressed an normal DWARF 2 info.
2476 Section decompression is done in @code{zlib_decompress_section} in
2477 @file{dwarf2read.c}.
2478
2479 @subsection DWARF 3
2480
2481 @cindex DWARF 3 debugging info
2482 DWARF 3 is an improved version of DWARF 2.
2483
2484 @subsection SOM
2485
2486 @cindex SOM debugging info
2487 Like COFF, the SOM definition includes debugging information.
2488
2489 @section Adding a New Symbol Reader to @value{GDBN}
2490
2491 @cindex adding debugging info reader
2492 If you are using an existing object file format (@code{a.out}, COFF, ELF, etc),
2493 there is probably little to be done.
2494
2495 If you need to add a new object file format, you must first add it to
2496 BFD.  This is beyond the scope of this document.
2497
2498 You must then arrange for the BFD code to provide access to the
2499 debugging symbols.  Generally @value{GDBN} will have to call swapping
2500 routines from BFD and a few other BFD internal routines to locate the
2501 debugging information.  As much as possible, @value{GDBN} should not
2502 depend on the BFD internal data structures.
2503
2504 For some targets (e.g., COFF), there is a special transfer vector used
2505 to call swapping routines, since the external data structures on various
2506 platforms have different sizes and layouts.  Specialized routines that
2507 will only ever be implemented by one object file format may be called
2508 directly.  This interface should be described in a file
2509 @file{bfd/lib@var{xyz}.h}, which is included by @value{GDBN}.
2510
2511 @section Memory Management for Symbol Files
2512
2513 Most memory associated with a loaded symbol file is stored on
2514 its @code{objfile_obstack}.  This includes symbols, types,
2515 namespace data, and other information produced by the symbol readers.
2516
2517 Because this data lives on the objfile's obstack, it is automatically
2518 released when the objfile is unloaded or reloaded.  Therefore one
2519 objfile must not reference symbol or type data from another objfile;
2520 they could be unloaded at different times.
2521
2522 User convenience variables, et cetera, have associated types.  Normally
2523 these types live in the associated objfile.  However, when the objfile
2524 is unloaded, those types are deep copied to global memory, so that
2525 the values of the user variables and history items are not lost.
2526
2527
2528 @node Language Support
2529
2530 @chapter Language Support
2531
2532 @cindex language support
2533 @value{GDBN}'s language support is mainly driven by the symbol reader,
2534 although it is possible for the user to set the source language
2535 manually.
2536
2537 @value{GDBN} chooses the source language by looking at the extension
2538 of the file recorded in the debug info; @file{.c} means C, @file{.f}
2539 means Fortran, etc.  It may also use a special-purpose language
2540 identifier if the debug format supports it, like with DWARF.
2541
2542 @section Adding a Source Language to @value{GDBN}
2543
2544 @cindex adding source language
2545 To add other languages to @value{GDBN}'s expression parser, follow the
2546 following steps:
2547
2548 @table @emph
2549 @item Create the expression parser.
2550
2551 @cindex expression parser
2552 This should reside in a file @file{@var{lang}-exp.y}.  Routines for
2553 building parsed expressions into a @code{union exp_element} list are in
2554 @file{parse.c}.
2555
2556 @cindex language parser
2557 Since we can't depend upon everyone having Bison, and YACC produces
2558 parsers that define a bunch of global names, the following lines
2559 @strong{must} be included at the top of the YACC parser, to prevent the
2560 various parsers from defining the same global names:
2561
2562 @smallexample
2563 #define yyparse         @var{lang}_parse
2564 #define yylex           @var{lang}_lex
2565 #define yyerror         @var{lang}_error
2566 #define yylval          @var{lang}_lval
2567 #define yychar          @var{lang}_char
2568 #define yydebug         @var{lang}_debug
2569 #define yypact          @var{lang}_pact 
2570 #define yyr1            @var{lang}_r1   
2571 #define yyr2            @var{lang}_r2   
2572 #define yydef           @var{lang}_def  
2573 #define yychk           @var{lang}_chk  
2574 #define yypgo           @var{lang}_pgo  
2575 #define yyact           @var{lang}_act  
2576 #define yyexca          @var{lang}_exca
2577 #define yyerrflag       @var{lang}_errflag
2578 #define yynerrs         @var{lang}_nerrs
2579 @end smallexample
2580
2581 At the bottom of your parser, define a @code{struct language_defn} and
2582 initialize it with the right values for your language.  Define an
2583 @code{initialize_@var{lang}} routine and have it call
2584 @samp{add_language(@var{lang}_language_defn)} to tell the rest of @value{GDBN}
2585 that your language exists.  You'll need some other supporting variables
2586 and functions, which will be used via pointers from your
2587 @code{@var{lang}_language_defn}.  See the declaration of @code{struct
2588 language_defn} in @file{language.h}, and the other @file{*-exp.y} files,
2589 for more information.
2590
2591 @item Add any evaluation routines, if necessary
2592
2593 @cindex expression evaluation routines
2594 @findex evaluate_subexp
2595 @findex prefixify_subexp
2596 @findex length_of_subexp
2597 If you need new opcodes (that represent the operations of the language),
2598 add them to the enumerated type in @file{expression.h}.  Add support
2599 code for these operations in the @code{evaluate_subexp} function
2600 defined in the file @file{eval.c}.  Add cases
2601 for new opcodes in two functions from @file{parse.c}:
2602 @code{prefixify_subexp} and @code{length_of_subexp}.  These compute
2603 the number of @code{exp_element}s that a given operation takes up.
2604
2605 @item Update some existing code
2606
2607 Add an enumerated identifier for your language to the enumerated type
2608 @code{enum language} in @file{defs.h}.
2609
2610 Update the routines in @file{language.c} so your language is included.
2611 These routines include type predicates and such, which (in some cases)
2612 are language dependent.  If your language does not appear in the switch
2613 statement, an error is reported.
2614
2615 @vindex current_language
2616 Also included in @file{language.c} is the code that updates the variable
2617 @code{current_language}, and the routines that translate the
2618 @code{language_@var{lang}} enumerated identifier into a printable
2619 string.
2620
2621 @findex _initialize_language
2622 Update the function @code{_initialize_language} to include your
2623 language.  This function picks the default language upon startup, so is
2624 dependent upon which languages that @value{GDBN} is built for.
2625
2626 @findex allocate_symtab
2627 Update @code{allocate_symtab} in @file{symfile.c} and/or symbol-reading
2628 code so that the language of each symtab (source file) is set properly.
2629 This is used to determine the language to use at each stack frame level.
2630 Currently, the language is set based upon the extension of the source
2631 file.  If the language can be better inferred from the symbol
2632 information, please set the language of the symtab in the symbol-reading
2633 code.
2634
2635 @findex print_subexp
2636 @findex op_print_tab
2637 Add helper code to @code{print_subexp} (in @file{expprint.c}) to handle any new
2638 expression opcodes you have added to @file{expression.h}.  Also, add the
2639 printed representations of your operators to @code{op_print_tab}.
2640
2641 @item Add a place of call
2642
2643 @findex parse_exp_1
2644 Add a call to @code{@var{lang}_parse()} and @code{@var{lang}_error} in
2645 @code{parse_exp_1} (defined in @file{parse.c}).
2646
2647 @item Edit @file{Makefile.in}
2648
2649 Add dependencies in @file{Makefile.in}.  Make sure you update the macro
2650 variables such as @code{HFILES} and @code{OBJS}, otherwise your code may
2651 not get linked in, or, worse yet, it may not get @code{tar}red into the
2652 distribution!
2653 @end table
2654
2655
2656 @node Host Definition
2657
2658 @chapter Host Definition
2659
2660 With the advent of Autoconf, it's rarely necessary to have host
2661 definition machinery anymore.  The following information is provided,
2662 mainly, as an historical reference.
2663
2664 @section Adding a New Host
2665
2666 @cindex adding a new host
2667 @cindex host, adding
2668 @value{GDBN}'s host configuration support normally happens via Autoconf.
2669 New host-specific definitions should not be needed.  Older hosts
2670 @value{GDBN} still use the host-specific definitions and files listed
2671 below, but these mostly exist for historical reasons, and will
2672 eventually disappear.
2673
2674 @table @file
2675 @item gdb/config/@var{arch}/@var{xyz}.mh
2676 This file is a Makefile fragment that once contained both host and
2677 native configuration information (@pxref{Native Debugging}) for the
2678 machine @var{xyz}.  The host configuration information is now handled
2679 by Autoconf.
2680
2681 Host configuration information included definitions for @code{CC},
2682 @code{SYSV_DEFINE}, @code{XM_CFLAGS}, @code{XM_ADD_FILES},
2683 @code{XM_CLIBS}, @code{XM_CDEPS}, etc.; see @file{Makefile.in}.
2684
2685 New host-only configurations do not need this file.
2686
2687 @end table
2688
2689 (Files named @file{gdb/config/@var{arch}/xm-@var{xyz}.h} were once
2690 used to define host-specific macros, but were no longer needed and
2691 have all been removed.)
2692
2693 @subheading Generic Host Support Files
2694
2695 @cindex generic host support
2696 There are some ``generic'' versions of routines that can be used by
2697 various systems.
2698
2699 @table @file
2700 @cindex remote debugging support
2701 @cindex serial line support
2702 @item ser-unix.c
2703 This contains serial line support for Unix systems.  It is included by
2704 default on all Unix-like hosts.
2705
2706 @item ser-pipe.c
2707 This contains serial pipe support for Unix systems.  It is included by
2708 default on all Unix-like hosts.
2709
2710 @item ser-mingw.c
2711 This contains serial line support for 32-bit programs running under
2712 Windows using MinGW.
2713
2714 @item ser-go32.c
2715 This contains serial line support for 32-bit programs running under DOS,
2716 using the DJGPP (a.k.a.@: GO32) execution environment.
2717
2718 @cindex TCP remote support
2719 @item ser-tcp.c
2720 This contains generic TCP support using sockets.  It is included by
2721 default on all Unix-like hosts and with MinGW.
2722 @end table
2723
2724 @section Host Conditionals
2725
2726 When @value{GDBN} is configured and compiled, various macros are
2727 defined or left undefined, to control compilation based on the
2728 attributes of the host system.  While formerly they could be set in
2729 host-specific header files, at present they can be changed only by
2730 setting @code{CFLAGS} when building, or by editing the source code.
2731
2732 These macros and their meanings (or if the meaning is not documented
2733 here, then one of the source files where they are used is indicated)
2734 are:
2735
2736 @ftable @code
2737 @item @value{GDBN}INIT_FILENAME
2738 The default name of @value{GDBN}'s initialization file (normally
2739 @file{.gdbinit}).
2740
2741 @item SIGWINCH_HANDLER
2742 If your host defines @code{SIGWINCH}, you can define this to be the name
2743 of a function to be called if @code{SIGWINCH} is received.
2744
2745 @item SIGWINCH_HANDLER_BODY
2746 Define this to expand into code that will define the function named by
2747 the expansion of @code{SIGWINCH_HANDLER}.
2748
2749 @item CRLF_SOURCE_FILES
2750 @cindex DOS text files
2751 Define this if host files use @code{\r\n} rather than @code{\n} as a
2752 line terminator.  This will cause source file listings to omit @code{\r}
2753 characters when printing and it will allow @code{\r\n} line endings of files
2754 which are ``sourced'' by gdb.  It must be possible to open files in binary
2755 mode using @code{O_BINARY} or, for fopen, @code{"rb"}.
2756
2757 @item DEFAULT_PROMPT
2758 @cindex prompt
2759 The default value of the prompt string (normally @code{"(gdb) "}).
2760
2761 @item DEV_TTY
2762 @cindex terminal device
2763 The name of the generic TTY device, defaults to @code{"/dev/tty"}.
2764
2765 @item ISATTY
2766 Substitute for isatty, if not available.
2767
2768 @item FOPEN_RB
2769 Define this if binary files are opened the same way as text files.
2770
2771 @item CC_HAS_LONG_LONG
2772 @cindex @code{long long} data type
2773 Define this if the host C compiler supports @code{long long}.  This is set
2774 by the @code{configure} script.
2775
2776 @item PRINTF_HAS_LONG_LONG
2777 Define this if the host can handle printing of long long integers via
2778 the printf format conversion specifier @code{ll}.  This is set by the
2779 @code{configure} script.
2780
2781 @item LSEEK_NOT_LINEAR
2782 Define this if @code{lseek (n)} does not necessarily move to byte number
2783 @code{n} in the file.  This is only used when reading source files.  It
2784 is normally faster to define @code{CRLF_SOURCE_FILES} when possible.
2785
2786 @item NORETURN
2787 If defined, this should be one or more tokens, such as @code{volatile},
2788 that can be used in both the declaration and definition of functions to
2789 indicate that they never return.  The default is already set correctly
2790 if compiling with GCC.  This will almost never need to be defined.
2791
2792 @item ATTR_NORETURN
2793 If defined, this should be one or more tokens, such as
2794 @code{__attribute__ ((noreturn))}, that can be used in the declarations
2795 of functions to indicate that they never return.  The default is already
2796 set correctly if compiling with GCC.  This will almost never need to be
2797 defined.
2798
2799 @item lint
2800 Define this to help placate @code{lint} in some situations.
2801
2802 @item volatile
2803 Define this to override the defaults of @code{__volatile__} or
2804 @code{/**/}.
2805 @end ftable
2806
2807
2808 @node Target Architecture Definition
2809
2810 @chapter Target Architecture Definition
2811
2812 @cindex target architecture definition
2813 @value{GDBN}'s target architecture defines what sort of
2814 machine-language programs @value{GDBN} can work with, and how it works
2815 with them.
2816
2817 The target architecture object is implemented as the C structure
2818 @code{struct gdbarch *}.  The structure, and its methods, are generated
2819 using the Bourne shell script @file{gdbarch.sh}.
2820
2821 @menu
2822 * OS ABI Variant Handling::
2823 * Initialize New Architecture::
2824 * Registers and Memory::
2825 * Pointers and Addresses::
2826 * Address Classes::
2827 * Register Representation::
2828 * Frame Interpretation::
2829 * Inferior Call Setup::
2830 * Adding support for debugging core files::
2831 * Defining Other Architecture Features::
2832 * Adding a New Target::
2833 @end menu
2834
2835 @node  OS ABI Variant Handling
2836 @section Operating System ABI Variant Handling
2837 @cindex OS ABI variants
2838
2839 @value{GDBN} provides a mechanism for handling variations in OS
2840 ABIs.  An OS ABI variant may have influence over any number of
2841 variables in the target architecture definition.  There are two major
2842 components in the OS ABI mechanism: sniffers and handlers.
2843
2844 A @dfn{sniffer} examines a file matching a BFD architecture/flavour pair
2845 (the architecture may be wildcarded) in an attempt to determine the
2846 OS ABI of that file.  Sniffers with a wildcarded architecture are considered
2847 to be @dfn{generic}, while sniffers for a specific architecture are
2848 considered to be @dfn{specific}.  A match from a specific sniffer
2849 overrides a match from a generic sniffer.  Multiple sniffers for an
2850 architecture/flavour may exist, in order to differentiate between two
2851 different operating systems which use the same basic file format.  The
2852 OS ABI framework provides a generic sniffer for ELF-format files which
2853 examines the @code{EI_OSABI} field of the ELF header, as well as note
2854 sections known to be used by several operating systems.
2855
2856 @cindex fine-tuning @code{gdbarch} structure
2857 A @dfn{handler} is used to fine-tune the @code{gdbarch} structure for the
2858 selected OS ABI.  There may be only one handler for a given OS ABI
2859 for each BFD architecture.
2860
2861 The following OS ABI variants are defined in @file{defs.h}:
2862
2863 @table @code
2864
2865 @findex GDB_OSABI_UNINITIALIZED
2866 @item GDB_OSABI_UNINITIALIZED
2867 Used for struct gdbarch_info if ABI is still uninitialized.
2868
2869 @findex GDB_OSABI_UNKNOWN
2870 @item GDB_OSABI_UNKNOWN
2871 The ABI of the inferior is unknown.  The default @code{gdbarch}
2872 settings for the architecture will be used.
2873
2874 @findex GDB_OSABI_SVR4
2875 @item GDB_OSABI_SVR4
2876 UNIX System V Release 4.
2877
2878 @findex GDB_OSABI_HURD
2879 @item GDB_OSABI_HURD
2880 GNU using the Hurd kernel.
2881
2882 @findex GDB_OSABI_SOLARIS
2883 @item GDB_OSABI_SOLARIS
2884 Sun Solaris.
2885
2886 @findex GDB_OSABI_OSF1
2887 @item GDB_OSABI_OSF1
2888 OSF/1, including Digital UNIX and Compaq Tru64 UNIX.
2889
2890 @findex GDB_OSABI_LINUX
2891 @item GDB_OSABI_LINUX
2892 GNU using the Linux kernel.
2893
2894 @findex GDB_OSABI_FREEBSD_AOUT
2895 @item GDB_OSABI_FREEBSD_AOUT
2896 FreeBSD using the @code{a.out} executable format.
2897
2898 @findex GDB_OSABI_FREEBSD_ELF
2899 @item GDB_OSABI_FREEBSD_ELF
2900 FreeBSD using the ELF executable format.
2901
2902 @findex GDB_OSABI_NETBSD_AOUT
2903 @item GDB_OSABI_NETBSD_AOUT
2904 NetBSD using the @code{a.out} executable format.
2905
2906 @findex GDB_OSABI_NETBSD_ELF
2907 @item GDB_OSABI_NETBSD_ELF
2908 NetBSD using the ELF executable format.
2909
2910 @findex GDB_OSABI_OPENBSD_ELF
2911 @item GDB_OSABI_OPENBSD_ELF
2912 OpenBSD using the ELF executable format.
2913
2914 @findex GDB_OSABI_WINCE
2915 @item GDB_OSABI_WINCE
2916 Windows CE.
2917
2918 @findex GDB_OSABI_GO32
2919 @item GDB_OSABI_GO32
2920 DJGPP.
2921
2922 @findex GDB_OSABI_IRIX
2923 @item GDB_OSABI_IRIX
2924 Irix.
2925
2926 @findex GDB_OSABI_INTERIX
2927 @item GDB_OSABI_INTERIX
2928 Interix (Posix layer for MS-Windows systems).
2929
2930 @findex GDB_OSABI_HPUX_ELF
2931 @item GDB_OSABI_HPUX_ELF
2932 HP/UX using the ELF executable format.
2933
2934 @findex GDB_OSABI_HPUX_SOM
2935 @item GDB_OSABI_HPUX_SOM
2936 HP/UX using the SOM executable format.
2937
2938 @findex GDB_OSABI_QNXNTO
2939 @item GDB_OSABI_QNXNTO
2940 QNX Neutrino.
2941
2942 @findex GDB_OSABI_CYGWIN
2943 @item GDB_OSABI_CYGWIN
2944 Cygwin.
2945
2946 @findex GDB_OSABI_AIX
2947 @item GDB_OSABI_AIX
2948 AIX.
2949
2950 @end table
2951
2952 Here are the functions that make up the OS ABI framework:
2953
2954 @deftypefun {const char *} gdbarch_osabi_name (enum gdb_osabi @var{osabi})
2955 Return the name of the OS ABI corresponding to @var{osabi}.
2956 @end deftypefun
2957
2958 @deftypefun void gdbarch_register_osabi (enum bfd_architecture @var{arch}, unsigned long @var{machine}, enum gdb_osabi @var{osabi}, void (*@var{init_osabi})(struct gdbarch_info @var{info}, struct gdbarch *@var{gdbarch}))
2959 Register the OS ABI handler specified by @var{init_osabi} for the
2960 architecture, machine type and OS ABI specified by @var{arch},
2961 @var{machine} and @var{osabi}.  In most cases, a value of zero for the
2962 machine type, which implies the architecture's default machine type,
2963 will suffice.
2964 @end deftypefun
2965
2966 @deftypefun void gdbarch_register_osabi_sniffer (enum bfd_architecture @var{arch}, enum bfd_flavour @var{flavour}, enum gdb_osabi (*@var{sniffer})(bfd *@var{abfd}))
2967 Register the OS ABI file sniffer specified by @var{sniffer} for the
2968 BFD architecture/flavour pair specified by @var{arch} and @var{flavour}.
2969 If @var{arch} is @code{bfd_arch_unknown}, the sniffer is considered to
2970 be generic, and is allowed to examine @var{flavour}-flavoured files for
2971 any architecture.
2972 @end deftypefun
2973
2974 @deftypefun {enum gdb_osabi} gdbarch_lookup_osabi (bfd *@var{abfd})
2975 Examine the file described by @var{abfd} to determine its OS ABI.
2976 The value @code{GDB_OSABI_UNKNOWN} is returned if the OS ABI cannot
2977 be determined.
2978 @end deftypefun
2979
2980 @deftypefun void gdbarch_init_osabi (struct gdbarch info @var{info}, struct gdbarch *@var{gdbarch}, enum gdb_osabi @var{osabi})
2981 Invoke the OS ABI handler corresponding to @var{osabi} to fine-tune the
2982 @code{gdbarch} structure specified by @var{gdbarch}.  If a handler
2983 corresponding to @var{osabi} has not been registered for @var{gdbarch}'s
2984 architecture, a warning will be issued and the debugging session will continue
2985 with the defaults already established for @var{gdbarch}.
2986 @end deftypefun
2987
2988 @deftypefun void generic_elf_osabi_sniff_abi_tag_sections (bfd *@var{abfd}, asection *@var{sect}, void *@var{obj})
2989 Helper routine for ELF file sniffers.  Examine the file described by
2990 @var{abfd} and look at ABI tag note sections to determine the OS ABI
2991 from the note.  This function should be called via
2992 @code{bfd_map_over_sections}.
2993 @end deftypefun
2994
2995 @node Initialize New Architecture
2996 @section Initializing a New Architecture
2997
2998 @menu
2999 * How an Architecture is Represented::
3000 * Looking Up an Existing Architecture::
3001 * Creating a New Architecture::
3002 @end menu
3003
3004 @node How an Architecture is Represented
3005 @subsection How an Architecture is Represented
3006 @cindex architecture representation
3007 @cindex representation of architecture
3008
3009 Each @code{gdbarch} is associated with a single @sc{bfd} architecture,
3010 via a @code{bfd_arch_@var{arch}} in the @code{bfd_architecture}
3011 enumeration.  The @code{gdbarch} is registered by a call to
3012 @code{register_gdbarch_init}, usually from the file's
3013 @code{_initialize_@var{filename}} routine, which will be automatically
3014 called during @value{GDBN} startup.  The arguments are a @sc{bfd}
3015 architecture constant and an initialization function.
3016
3017 @findex _initialize_@var{arch}_tdep
3018 @cindex @file{@var{arch}-tdep.c}
3019 A @value{GDBN} description for a new architecture, @var{arch} is created by
3020 defining a global function @code{_initialize_@var{arch}_tdep}, by
3021 convention in the source file @file{@var{arch}-tdep.c}.  For example,
3022 in the case of the OpenRISC 1000, this function is called
3023 @code{_initialize_or1k_tdep} and is found in the file
3024 @file{or1k-tdep.c}.
3025
3026 @cindex @file{configure.tgt}
3027 @cindex @code{gdbarch}
3028 @findex gdbarch_register
3029 The resulting object files containing the implementation of the
3030 @code{_initialize_@var{arch}_tdep} function are specified in the @value{GDBN}
3031 @file{configure.tgt} file, which includes a large case statement
3032 pattern matching against the @code{--target} option of the
3033 @code{configure} script.  The new @code{struct gdbarch} is created
3034 within the @code{_initialize_@var{arch}_tdep} function by calling
3035 @code{gdbarch_register}:
3036
3037 @smallexample
3038 void gdbarch_register (enum bfd_architecture    @var{architecture},
3039                        gdbarch_init_ftype      *@var{init_func},
3040                        gdbarch_dump_tdep_ftype *@var{tdep_dump_func});
3041 @end smallexample
3042
3043 The @var{architecture} will identify the unique @sc{bfd} to be
3044 associated with this @code{gdbarch}.  The @var{init_func} funciton is
3045 called to create and return the new @code{struct gdbarch}.  The
3046 @var{tdep_dump_func} function will dump the target specific details
3047 associated with this architecture.
3048
3049 For example the function @code{_initialize_or1k_tdep} creates its
3050 architecture for 32-bit OpenRISC 1000 architectures by calling:
3051
3052 @smallexample
3053 gdbarch_register (bfd_arch_or32, or1k_gdbarch_init, or1k_dump_tdep);
3054 @end smallexample
3055
3056 @node Looking Up an Existing Architecture
3057 @subsection Looking Up an Existing Architecture
3058 @cindex @code{gdbarch} lookup
3059
3060 The initialization function has this prototype:
3061
3062 @smallexample
3063 static struct gdbarch *
3064 @var{arch}_gdbarch_init (struct gdbarch_info @var{info},
3065                          struct gdbarch_list *@var{arches})
3066 @end smallexample
3067
3068 The @var{info} argument contains parameters used to select the correct
3069 architecture, and @var{arches} is a list of architectures which
3070 have already been created with the same @code{bfd_arch_@var{arch}}
3071 value.
3072
3073 The initialization function should first make sure that @var{info}
3074 is acceptable, and return @code{NULL} if it is not.  Then, it should
3075 search through @var{arches} for an exact match to @var{info}, and
3076 return one if found.  Lastly, if no exact match was found, it should
3077 create a new architecture based on @var{info} and return it.
3078
3079 @findex gdbarch_list_lookup_by_info
3080 @cindex @code{gdbarch_info}
3081 The lookup is done using @code{gdbarch_list_lookup_by_info}.  It is
3082 passed the list of existing architectures, @var{arches}, and the
3083 @code{struct gdbarch_info}, @var{info}, and returns the first matching
3084 architecture it finds, or @code{NULL} if none are found.  If an
3085 architecture is found it can be returned as the result from the
3086 initialization function, otherwise a new @code{struct gdbach} will need
3087 to be created.
3088
3089 The struct gdbarch_info has the following components:
3090
3091 @smallexample
3092 struct gdbarch_info
3093 @{
3094    const struct bfd_arch_info *bfd_arch_info;
3095    int                         byte_order;
3096    bfd                        *abfd;
3097    struct gdbarch_tdep_info   *tdep_info;
3098    enum gdb_osabi              osabi;
3099    const struct target_desc   *target_desc;
3100 @};
3101 @end smallexample
3102
3103 @vindex bfd_arch_info
3104 The @code{bfd_arch_info} member holds the key details about the
3105 architecture.  The @code{byte_order} member is a value in an
3106 enumeration indicating the endianism.  The @code{abfd} member is a
3107 pointer to the full @sc{bfd}, the @code{tdep_info} member is
3108 additional custom target specific information, @code{osabi} identifies
3109 which (if any) of a number of operating specific ABIs are used by this
3110 architecture and the @code{target_desc} member is a set of name-value
3111 pairs with information about register usage in this target.
3112
3113 When the @code{struct gdbarch} initialization function is called, not
3114 all the fields are provided---only those which can be deduced from the
3115 @sc{bfd}.  The @code{struct gdbarch_info}, @var{info} is used as a
3116 look-up key with the list of existing architectures, @var{arches} to
3117 see if a suitable architecture already exists.  The @var{tdep_info},
3118 @var{osabi} and @var{target_desc} fields may be added before this
3119 lookup to refine the search.
3120
3121 Only information in @var{info} should be used to choose the new
3122 architecture.  Historically, @var{info} could be sparse, and
3123 defaults would be collected from the first element on @var{arches}.
3124 However, @value{GDBN} now fills in @var{info} more thoroughly,
3125 so new @code{gdbarch} initialization functions should not take
3126 defaults from @var{arches}.
3127
3128 @node Creating a New Architecture
3129 @subsection Creating a New Architecture
3130 @cindex @code{struct gdbarch} creation
3131
3132 @findex gdbarch_alloc
3133 @cindex @code{gdbarch_tdep} when allocating new @code{gdbarch}
3134 If no architecture is found, then a new architecture must be created,
3135 by calling @code{gdbarch_alloc} using the supplied @code{@w{struct
3136 gdbarch_info}} and any additional custom target specific
3137 information in a @code{struct gdbarch_tdep}.  The prototype for
3138 @code{gdbarch_alloc} is:
3139
3140 @smallexample
3141 struct gdbarch *gdbarch_alloc (const struct gdbarch_info *@var{info},
3142                                struct gdbarch_tdep       *@var{tdep}); 
3143 @end smallexample
3144
3145 @cindex @code{set_gdbarch} functions
3146 @cindex @code{gdbarch} accessor functions
3147 The newly created struct gdbarch must then be populated.  Although
3148 there are default values, in most cases they are not what is
3149 required.
3150
3151 For each element, @var{X}, there is are a pair of corresponding accessor
3152 functions, one to set the value of that element,
3153 @code{set_gdbarch_@var{X}}, the second to either get the value of an
3154 element (if it is a variable) or to apply the element (if it is a
3155 function), @code{gdbarch_@var{X}}.  Note that both accessor functions
3156 take a pointer to the @code{@w{struct gdbarch}} as first
3157 argument.  Populating the new @code{gdbarch} should use the
3158 @code{set_gdbarch} functions.
3159
3160 The following sections identify the main elements that should be set
3161 in this way.  This is not the complete list, but represents the
3162 functions and elements that must commonly be specified for a new
3163 architecture.  Many of the functions and variables are described in the
3164 header file @file{gdbarch.h}.
3165
3166 This is the main work in defining a new architecture.  Implementing the
3167 set of functions to populate the @code{struct gdbarch}.
3168
3169 @cindex @code{gdbarch_tdep} definition
3170 @code{struct gdbarch_tdep} is not defined within @value{GDBN}---it is up
3171 to the user to define this struct if it is needed to hold custom target
3172 information that is not covered by the standard @code{@w{struct
3173 gdbarch}}. For example with the OpenRISC 1000 architecture it is used to
3174 hold the number of matchpoints available in the target (along with other
3175 information).
3176
3177 If there is no additional target specific information, it can be set to
3178 @code{NULL}.
3179
3180 @node Registers and Memory
3181 @section Registers and Memory
3182
3183 @value{GDBN}'s model of the target machine is rather simple.
3184 @value{GDBN} assumes the machine includes a bank of registers and a
3185 block of memory.  Each register may have a different size.
3186
3187 @value{GDBN} does not have a magical way to match up with the
3188 compiler's idea of which registers are which; however, it is critical
3189 that they do match up accurately.  The only way to make this work is
3190 to get accurate information about the order that the compiler uses,
3191 and to reflect that in the @code{gdbarch_register_name} and related functions.
3192
3193 @value{GDBN} can handle big-endian, little-endian, and bi-endian architectures.
3194
3195 @node Pointers and Addresses
3196 @section Pointers Are Not Always Addresses
3197 @cindex pointer representation
3198 @cindex address representation
3199 @cindex word-addressed machines
3200 @cindex separate data and code address spaces
3201 @cindex spaces, separate data and code address
3202 @cindex address spaces, separate data and code
3203 @cindex code pointers, word-addressed
3204 @cindex converting between pointers and addresses
3205 @cindex D10V addresses
3206
3207 On almost all 32-bit architectures, the representation of a pointer is
3208 indistinguishable from the representation of some fixed-length number
3209 whose value is the byte address of the object pointed to.  On such
3210 machines, the words ``pointer'' and ``address'' can be used interchangeably.
3211 However, architectures with smaller word sizes are often cramped for
3212 address space, so they may choose a pointer representation that breaks this
3213 identity, and allows a larger code address space.
3214
3215 @c D10V is gone from sources - more current example?
3216
3217 For example, the Renesas D10V is a 16-bit VLIW processor whose
3218 instructions are 32 bits long@footnote{Some D10V instructions are
3219 actually pairs of 16-bit sub-instructions.  However, since you can't
3220 jump into the middle of such a pair, code addresses can only refer to
3221 full 32 bit instructions, which is what matters in this explanation.}.
3222 If the D10V used ordinary byte addresses to refer to code locations,
3223 then the processor would only be able to address 64kb of instructions.
3224 However, since instructions must be aligned on four-byte boundaries, the
3225 low two bits of any valid instruction's byte address are always
3226 zero---byte addresses waste two bits.  So instead of byte addresses,
3227 the D10V uses word addresses---byte addresses shifted right two bits---to
3228 refer to code.  Thus, the D10V can use 16-bit words to address 256kb of
3229 code space.
3230
3231 However, this means that code pointers and data pointers have different
3232 forms on the D10V.  The 16-bit word @code{0xC020} refers to byte address
3233 @code{0xC020} when used as a data address, but refers to byte address
3234 @code{0x30080} when used as a code address.
3235
3236 (The D10V also uses separate code and data address spaces, which also
3237 affects the correspondence between pointers and addresses, but we're
3238 going to ignore that here; this example is already too long.)
3239
3240 To cope with architectures like this---the D10V is not the only
3241 one!---@value{GDBN} tries to distinguish between @dfn{addresses}, which are
3242 byte numbers, and @dfn{pointers}, which are the target's representation
3243 of an address of a particular type of data.  In the example above,
3244 @code{0xC020} is the pointer, which refers to one of the addresses
3245 @code{0xC020} or @code{0x30080}, depending on the type imposed upon it.
3246 @value{GDBN} provides functions for turning a pointer into an address
3247 and vice versa, in the appropriate way for the current architecture.
3248
3249 Unfortunately, since addresses and pointers are identical on almost all
3250 processors, this distinction tends to bit-rot pretty quickly.  Thus,
3251 each time you port @value{GDBN} to an architecture which does
3252 distinguish between pointers and addresses, you'll probably need to
3253 clean up some architecture-independent code.
3254
3255 Here are functions which convert between pointers and addresses:
3256
3257 @deftypefun CORE_ADDR extract_typed_address (void *@var{buf}, struct type *@var{type})
3258 Treat the bytes at @var{buf} as a pointer or reference of type
3259 @var{type}, and return the address it represents, in a manner
3260 appropriate for the current architecture.  This yields an address
3261 @value{GDBN} can use to read target memory, disassemble, etc.  Note that
3262 @var{buf} refers to a buffer in @value{GDBN}'s memory, not the
3263 inferior's.
3264
3265 For example, if the current architecture is the Intel x86, this function
3266 extracts a little-endian integer of the appropriate length from
3267 @var{buf} and returns it.  However, if the current architecture is the
3268 D10V, this function will return a 16-bit integer extracted from
3269 @var{buf}, multiplied by four if @var{type} is a pointer to a function.
3270
3271 If @var{type} is not a pointer or reference type, then this function
3272 will signal an internal error.
3273 @end deftypefun
3274
3275 @deftypefun CORE_ADDR store_typed_address (void *@var{buf}, struct type *@var{type}, CORE_ADDR @var{addr})
3276 Store the address @var{addr} in @var{buf}, in the proper format for a
3277 pointer of type @var{type} in the current architecture.  Note that
3278 @var{buf} refers to a buffer in @value{GDBN}'s memory, not the
3279 inferior's.
3280
3281 For example, if the current architecture is the Intel x86, this function
3282 stores @var{addr} unmodified as a little-endian integer of the
3283 appropriate length in @var{buf}.  However, if the current architecture
3284 is the D10V, this function divides @var{addr} by four if @var{type} is
3285 a pointer to a function, and then stores it in @var{buf}.
3286
3287 If @var{type} is not a pointer or reference type, then this function
3288 will signal an internal error.
3289 @end deftypefun
3290
3291 @deftypefun CORE_ADDR value_as_address (struct value *@var{val})
3292 Assuming that @var{val} is a pointer, return the address it represents,
3293 as appropriate for the current architecture.
3294
3295 This function actually works on integral values, as well as pointers.
3296 For pointers, it performs architecture-specific conversions as
3297 described above for @code{extract_typed_address}.
3298 @end deftypefun
3299
3300 @deftypefun CORE_ADDR value_from_pointer (struct type *@var{type}, CORE_ADDR @var{addr})
3301 Create and return a value representing a pointer of type @var{type} to
3302 the address @var{addr}, as appropriate for the current architecture.
3303 This function performs architecture-specific conversions as described
3304 above for @code{store_typed_address}.
3305 @end deftypefun
3306
3307 Here are two functions which architectures can define to indicate the
3308 relationship between pointers and addresses.  These have default
3309 definitions, appropriate for architectures on which all pointers are
3310 simple unsigned byte addresses.
3311
3312 @deftypefun CORE_ADDR gdbarch_pointer_to_address (struct gdbarch *@var{gdbarch}, struct type *@var{type}, char *@var{buf})
3313 Assume that @var{buf} holds a pointer of type @var{type}, in the
3314 appropriate format for the current architecture.  Return the byte
3315 address the pointer refers to.
3316
3317 This function may safely assume that @var{type} is either a pointer or a
3318 C@t{++} reference type.
3319 @end deftypefun
3320
3321 @deftypefun void gdbarch_address_to_pointer (struct gdbarch *@var{gdbarch}, struct type *@var{type}, char *@var{buf}, CORE_ADDR @var{addr})
3322 Store in @var{buf} a pointer of type @var{type} representing the address
3323 @var{addr}, in the appropriate format for the current architecture.
3324
3325 This function may safely assume that @var{type} is either a pointer or a
3326 C@t{++} reference type.
3327 @end deftypefun
3328
3329 @node Address Classes
3330 @section Address Classes
3331 @cindex address classes
3332 @cindex DW_AT_byte_size
3333 @cindex DW_AT_address_class
3334
3335 Sometimes information about different kinds of addresses is available
3336 via the debug information.  For example, some programming environments
3337 define addresses of several different sizes.  If the debug information
3338 distinguishes these kinds of address classes through either the size
3339 info (e.g, @code{DW_AT_byte_size} in @w{DWARF 2}) or through an explicit
3340 address class attribute (e.g, @code{DW_AT_address_class} in @w{DWARF 2}), the
3341 following macros should be defined in order to disambiguate these
3342 types within @value{GDBN} as well as provide the added information to
3343 a @value{GDBN} user when printing type expressions.
3344
3345 @deftypefun int gdbarch_address_class_type_flags (struct gdbarch *@var{gdbarch}, int @var{byte_size}, int @var{dwarf2_addr_class})
3346 Returns the type flags needed to construct a pointer type whose size
3347 is @var{byte_size} and whose address class is @var{dwarf2_addr_class}.
3348 This function is normally called from within a symbol reader.  See
3349 @file{dwarf2read.c}.
3350 @end deftypefun
3351
3352 @deftypefun {char *} gdbarch_address_class_type_flags_to_name (struct gdbarch *@var{gdbarch}, int @var{type_flags})
3353 Given the type flags representing an address class qualifier, return
3354 its name.
3355 @end deftypefun
3356 @deftypefun int gdbarch_address_class_name_to_type_flags (struct gdbarch *@var{gdbarch}, int @var{name}, int *@var{type_flags_ptr})
3357 Given an address qualifier name, set the @code{int} referenced by @var{type_flags_ptr} to the type flags
3358 for that address class qualifier.
3359 @end deftypefun
3360
3361 Since the need for address classes is rather rare, none of
3362 the address class functions are defined by default.  Predicate
3363 functions are provided to detect when they are defined.
3364
3365 Consider a hypothetical architecture in which addresses are normally
3366 32-bits wide, but 16-bit addresses are also supported.  Furthermore,
3367 suppose that the @w{DWARF 2} information for this architecture simply
3368 uses a @code{DW_AT_byte_size} value of 2 to indicate the use of one
3369 of these "short" pointers.  The following functions could be defined
3370 to implement the address class functions:
3371
3372 @smallexample
3373 somearch_address_class_type_flags (int byte_size,
3374                                    int dwarf2_addr_class)
3375 @{
3376   if (byte_size == 2)
3377     return TYPE_FLAG_ADDRESS_CLASS_1;
3378   else
3379     return 0;
3380 @}
3381
3382 static char *
3383 somearch_address_class_type_flags_to_name (int type_flags)
3384 @{
3385   if (type_flags & TYPE_FLAG_ADDRESS_CLASS_1)
3386     return "short";
3387   else
3388     return NULL;
3389 @}
3390
3391 int
3392 somearch_address_class_name_to_type_flags (char *name,
3393                                            int *type_flags_ptr)
3394 @{
3395   if (strcmp (name, "short") == 0)
3396     @{
3397       *type_flags_ptr = TYPE_FLAG_ADDRESS_CLASS_1;
3398       return 1;
3399     @}
3400   else
3401     return 0;
3402 @}
3403 @end smallexample
3404
3405 The qualifier @code{@@short} is used in @value{GDBN}'s type expressions
3406 to indicate the presence of one of these ``short'' pointers.  For
3407 example if the debug information indicates that @code{short_ptr_var} is
3408 one of these short pointers, @value{GDBN} might show the following
3409 behavior:
3410
3411 @smallexample
3412 (gdb) ptype short_ptr_var
3413 type = int * @@short
3414 @end smallexample
3415
3416
3417 @node Register Representation
3418 @section Register Representation
3419
3420 @menu
3421 * Raw and Cooked Registers::
3422 * Register Architecture Functions & Variables::
3423 * Register Information Functions::
3424 * Register and Memory Data::
3425 * Register Caching::
3426 @end menu
3427
3428 @node Raw and Cooked Registers
3429 @subsection Raw and Cooked Registers
3430 @cindex raw register representation
3431 @cindex cooked register representation
3432 @cindex representations, raw and cooked registers
3433
3434 @value{GDBN} considers registers to be a set with members numbered
3435 linearly from 0 upwards.  The first part of that set corresponds to real
3436 physical registers, the second part to any @dfn{pseudo-registers}.
3437 Pseudo-registers have no independent physical existence, but are useful
3438 representations of information within the architecture.  For example the
3439 OpenRISC 1000 architecture has up to 32 general purpose registers, which
3440 are typically represented as 32-bit (or 64-bit) integers.  However the
3441 GPRs are also used as operands to the floating point operations, and it
3442 could be convenient to define a set of pseudo-registers, to show the
3443 GPRs represented as floating point values.
3444
3445 For any architecture, the implementer will decide on a mapping from
3446 hardware to @value{GDBN} register numbers.  The registers corresponding to real
3447 hardware are referred to as @dfn{raw} registers, the remaining registers are
3448 @dfn{pseudo-registers}.  The total register set (raw and pseudo) is called
3449 the @dfn{cooked} register set.
3450
3451
3452 @node Register Architecture Functions & Variables
3453 @subsection Functions and Variables Specifying the Register Architecture
3454 @cindex @code{gdbarch} register architecture functions
3455
3456 These @code{struct gdbarch} functions and variables specify the number
3457 and type of registers in the architecture.
3458
3459 @deftypefn {Architecture Function} CORE_ADDR read_pc (struct regcache *@var{regcache})
3460 @end deftypefn
3461 @deftypefn {Architecture Function} void write_pc (struct regcache *@var{regcache}, CORE_ADDR @var{val})
3462
3463 Read or write the program counter.  The default value of both
3464 functions is @code{NULL} (no function available).  If the program
3465 counter is just an ordinary register, it can be specified in
3466 @code{struct gdbarch} instead (see @code{pc_regnum} below) and it will
3467 be read or written using the standard routines to access registers.  This
3468 function need only be specified if the program counter is not an
3469 ordinary register.
3470
3471 Any register information can be obtained using the supplied register
3472 cache, @var{regcache}.  @xref{Register Caching, , Register Caching}.
3473
3474 @end deftypefn
3475
3476 @deftypefn {Architecture Function} void pseudo_register_read (struct gdbarch *@var{gdbarch}, struct regcache *@var{regcache}, int @var{regnum}, const gdb_byte *@var{buf})
3477 @end deftypefn
3478 @deftypefn {Architecture Function} void pseudo_register_write (struct gdbarch *@var{gdbarch}, struct regcache *@var{regcache}, int @var{regnum}, const gdb_byte *@var{buf})
3479
3480 These functions should be defined if there are any pseudo-registers.
3481 The default value is @code{NULL}.  @var{regnum} is the number of the
3482 register to read or write (which will be a @dfn{cooked} register
3483 number) and @var{buf} is the buffer where the value read will be
3484 placed, or from which the value to be written will be taken.  The
3485 value in the buffer may be converted to or from a signed or unsigned
3486 integral value using one of the utility functions (@pxref{Register and
3487 Memory Data, , Using Different Register and Memory Data
3488 Representations}).
3489
3490 The access should be for the specified architecture,
3491 @var{gdbarch}.  Any register information can be obtained using the
3492 supplied register cache, @var{regcache}.  @xref{Register Caching, ,
3493 Register Caching}.
3494
3495 @end deftypefn
3496
3497 @deftypevr {Architecture Variable} int sp_regnum
3498 @vindex sp_regnum
3499 @cindex stack pointer
3500 @cindex @kbd{$sp}
3501
3502 This specifies the register holding the stack pointer, which may be a
3503 raw or pseudo-register.  It defaults to -1 (not defined), but it is an
3504 error for it not to be defined.
3505
3506 The value of the stack pointer register can be accessed withing
3507 @value{GDBN} as the variable @kbd{$sp}.
3508
3509 @end deftypevr
3510
3511 @deftypevr {Architecture Variable} int pc_regnum
3512 @vindex pc_regnum
3513 @cindex program counter
3514 @cindex @kbd{$pc}
3515
3516 This specifies the register holding the program counter, which may be a
3517 raw or pseudo-register.  It defaults to -1 (not defined).  If
3518 @code{pc_regnum} is not defined, then the functions @code{read_pc} and
3519 @code{write_pc} (see above) must be defined.
3520
3521 The value of the program counter (whether defined as a register, or
3522 through @code{read_pc} and @code{write_pc}) can be accessed withing
3523 @value{GDBN} as the variable @kbd{$pc}.
3524
3525 @end deftypevr
3526
3527 @deftypevr {Architecture Variable} int ps_regnum
3528 @vindex ps_regnum
3529 @cindex processor status register
3530 @cindex status register
3531 @cindex @kbd{$ps}
3532
3533 This specifies the register holding the processor status (often called
3534 the status register), which may be a raw or pseudo-register.  It
3535 defaults to -1 (not defined).
3536
3537 If defined, the value of this register can be accessed withing
3538 @value{GDBN} as the variable @kbd{$ps}.
3539
3540 @end deftypevr
3541
3542 @deftypevr {Architecture Variable} int fp0_regnum
3543 @vindex fp0_regnum
3544 @cindex first floating point register
3545
3546 This specifies the first floating point register.  It defaults to
3547 0.  @code{fp0_regnum} is not needed unless the target offers support
3548 for floating point.
3549
3550 @end deftypevr
3551
3552 @node Register Information Functions
3553 @subsection Functions Giving Register Information
3554 @cindex @code{gdbarch} register information functions
3555
3556 These functions return information about registers.
3557
3558 @deftypefn {Architecture Function} {const char *} register_name (struct gdbarch *@var{gdbarch}, int @var{regnum})
3559
3560 This function should convert a register number (raw or pseudo) to a
3561 register name (as a C @code{const char *}).  This is used both to
3562 determine the name of a register for output and to work out the meaning
3563 of any register names used as input.  The function may also return
3564 @code{NULL}, to indicate that @var{regnum} is not a valid register.
3565
3566 For example with the OpenRISC 1000, @value{GDBN} registers 0-31 are the
3567 General Purpose Registers, register 32 is the program counter and
3568 register 33 is the supervision register (i.e.@: the processor status
3569 register), which map to the strings @code{"gpr00"} through
3570 @code{"gpr31"}, @code{"pc"} and @code{"sr"} respectively. This means
3571 that the @value{GDBN} command @kbd{print $gpr5} should print the value of
3572 the OR1K general purpose register 5@footnote{
3573 @cindex frame pointer
3574 @cindex @kbd{$fp}
3575 Historically, @value{GDBN} always had a concept of a frame pointer
3576 register, which could be accessed via the @value{GDBN} variable,
3577 @kbd{$fp}.  That concept is now deprecated, recognizing that not all
3578 architectures have a frame pointer.  However if an architecture does
3579 have a frame pointer register, and defines a register or
3580 pseudo-register with the name @code{"fp"}, then that register will be
3581 used as the value of the @kbd{$fp} variable.}.
3582
3583 The default value for this function is @code{NULL}, meaning
3584 undefined. It should always be defined.
3585
3586 The access should be for the specified architecture, @var{gdbarch}.
3587
3588 @end deftypefn
3589
3590 @deftypefn {Architecture Function} {struct type *} register_type (struct gdbarch *@var{gdbarch}, int @var{regnum})
3591
3592 Given a register number, this function identifies the type of data it
3593 may be holding, specified as a @code{struct type}.  @value{GDBN} allows
3594 creation of arbitrary types, but a number of built in types are
3595 provided (@code{builtin_type_void}, @code{builtin_type_int32} etc),
3596 together with functions to derive types from these.
3597
3598 Typically the program counter will have a type of ``pointer to
3599 function'' (it points to code), the frame pointer and stack pointer
3600 will have types of ``pointer to void'' (they point to data on the stack)
3601 and all other integer registers will have a type of 32-bit integer or
3602 64-bit integer.
3603
3604 This information guides the formatting when displaying register
3605 information.  The default value is @code{NULL} meaning no information is
3606 available to guide formatting when displaying registers.
3607
3608 @end deftypefn
3609
3610 @deftypefn {Architecture Function} void print_registers_info (struct gdbarch *@var{gdbarch}, struct ui_file *@var{file}, struct frame_info *@var{frame}, int @var{regnum}, int @var{all})
3611
3612 Define this function to print out one or all of the registers for the
3613 @value{GDBN} @kbd{info registers} command.  The default value is the
3614 function @code{default_print_registers_info}, which uses the register
3615 type information (see @code{register_type} above) to determine how each
3616 register should be printed.  Define a custom version of this function
3617 for fuller control over how the registers are displayed.
3618
3619 The access should be for the specified architecture, @var{gdbarch},
3620 with output to the the file specified by the User Interface
3621 Independent Output file handle, @var{file} (@pxref{UI-Independent
3622 Output, , UI-Independent Output---the @code{ui_out}
3623 Functions}).
3624
3625 The registers should show their values in the frame specified by
3626 @var{frame}.  If @var{regnum} is -1 and @var{all} is zero, then all
3627 the ``significant'' registers should be shown (the implementer should
3628 decide which registers are ``significant''). Otherwise only the value of
3629 the register specified by @var{regnum} should be output.  If
3630 @var{regnum} is -1 and @var{all} is non-zero (true), then the value of
3631 all registers should be shown.
3632
3633 By default @code{default_print_registers_info} prints one register per
3634 line, and if @var{all} is zero omits floating-point registers.
3635
3636 @end deftypefn
3637
3638 @deftypefn {Architecture Function} void print_float_info (struct gdbarch *@var{gdbarch}, struct ui_file *@var{file}, struct frame_info *@var{frame}, const char *@var{args})
3639
3640 Define this function to provide output about the floating point unit and
3641 registers for the @value{GDBN} @kbd{info float} command respectively.
3642 The default value is @code{NULL} (not defined), meaning no information
3643 will be provided.
3644
3645 The @var{gdbarch} and @var{file} and @var{frame} arguments have the same
3646 meaning as in the @code{print_registers_info} function above. The string
3647 @var{args} contains any supplementary arguments to the @kbd{info float}
3648 command.
3649
3650 Define this function if the target supports floating point operations.
3651
3652 @end deftypefn
3653
3654 @deftypefn {Architecture Function} void print_vector_info (struct gdbarch *@var{gdbarch}, struct ui_file *@var{file}, struct frame_info *@var{frame}, const char *@var{args})
3655
3656 Define this function to provide output about the vector unit and
3657 registers for the @value{GDBN} @kbd{info vector} command respectively.
3658 The default value is @code{NULL} (not defined), meaning no information
3659 will be provided.
3660
3661 The @var{gdbarch}, @var{file} and @var{frame} arguments have the
3662 same meaning as in the @code{print_registers_info} function above.  The
3663 string @var{args} contains any supplementary arguments to the @kbd{info
3664 vector} command.
3665
3666 Define this function if the target supports vector operations.
3667
3668 @end deftypefn
3669
3670 @deftypefn {Architecture Function} int register_reggroup_p (struct gdbarch *@var{gdbarch}, int @var{regnum}, struct reggroup *@var{group})
3671
3672 @value{GDBN} groups registers into different categories (general,
3673 vector, floating point etc).  This function, given a register,
3674 @var{regnum}, and group, @var{group}, returns 1 (true) if the register
3675 is in the group and 0 (false) otherwise.
3676
3677 The information should be for the specified architecture,
3678 @var{gdbarch}
3679
3680 The default value is the function @code{default_register_reggroup_p}
3681 which will do a reasonable job based on the type of the register (see
3682 the function @code{register_type} above), with groups for general
3683 purpose registers, floating point registers, vector registers and raw
3684 (i.e not pseudo) registers.
3685
3686 @end deftypefn
3687
3688 @node Register and Memory Data
3689 @subsection Using Different Register and Memory Data Representations
3690 @cindex register representation
3691 @cindex memory representation
3692 @cindex representations, register and memory
3693 @cindex register data formats, converting
3694 @cindex @code{struct value}, converting register contents to
3695
3696 Some architectures have different representations of data objects,
3697 depending whether the object is held in a register or memory.  For
3698 example:
3699
3700 @itemize @bullet
3701
3702 @item
3703 The Alpha architecture can represent 32 bit integer values in
3704 floating-point registers.
3705
3706 @item
3707 The x86 architecture supports 80-bit floating-point registers.  The
3708 @code{long double} data type occupies 96 bits in memory but only 80
3709 bits when stored in a register.
3710
3711 @end itemize
3712
3713 In general, the register representation of a data type is determined by
3714 the architecture, or @value{GDBN}'s interface to the architecture, while
3715 the memory representation is determined by the Application Binary
3716 Interface.
3717
3718 For almost all data types on almost all architectures, the two
3719 representations are identical, and no special handling is needed.
3720 However, they do occasionally differ.  An architecture may define the
3721 following @code{struct gdbarch} functions to request conversions
3722 between the register and memory representations of a data type:
3723
3724 @deftypefn {Architecture Function} int gdbarch_convert_register_p (struct gdbarch *@var{gdbarch}, int @var{reg})
3725
3726 Return non-zero (true) if the representation of a data value stored in
3727 this register may be different to the representation of that same data
3728 value when stored in memory.  The default value is @code{NULL}
3729 (undefined).
3730
3731 If this function is defined and returns non-zero, the @code{struct
3732 gdbarch} functions @code{gdbarch_register_to_value} and
3733 @code{gdbarch_value_to_register} (see below) should be used to perform
3734 any necessary conversion.
3735
3736 If defined, this function should return zero for the register's native
3737 type, when no conversion is necessary.
3738 @end deftypefn
3739
3740 @deftypefn {Architecture Function} void gdbarch_register_to_value (struct gdbarch *@var{gdbarch}, int @var{reg}, struct type *@var{type}, char *@var{from}, char *@var{to})
3741
3742 Convert the value of register number @var{reg} to a data object of
3743 type @var{type}.  The buffer at @var{from} holds the register's value
3744 in raw format; the converted value should be placed in the buffer at
3745 @var{to}.
3746
3747 @quotation
3748 @emph{Note:} @code{gdbarch_register_to_value} and
3749 @code{gdbarch_value_to_register} take their @var{reg} and @var{type}
3750 arguments in different orders.
3751 @end quotation
3752
3753 @code{gdbarch_register_to_value} should only be used with registers
3754 for which the @code{gdbarch_convert_register_p} function returns a
3755 non-zero value.
3756
3757 @end deftypefn
3758
3759 @deftypefn {Architecture Function} void gdbarch_value_to_register (struct gdbarch *@var{gdbarch}, struct type *@var{type}, int @var{reg}, char *@var{from}, char *@var{to})
3760
3761 Convert a data value of type @var{type} to register number @var{reg}'
3762 raw format.
3763
3764 @quotation
3765 @emph{Note:} @code{gdbarch_register_to_value} and
3766 @code{gdbarch_value_to_register} take their @var{reg} and @var{type}
3767 arguments in different orders.
3768 @end quotation
3769
3770 @code{gdbarch_value_to_register} should only be used with registers
3771 for which the @code{gdbarch_convert_register_p} function returns a
3772 non-zero value.
3773
3774 @end deftypefn
3775
3776 @node Register Caching
3777 @subsection Register Caching
3778 @cindex register caching
3779
3780 Caching of registers is used, so that the target does not need to be
3781 accessed and reanalyzed multiple times for each register in
3782 circumstances where the register value cannot have changed.
3783
3784 @cindex @code{struct regcache}
3785 @value{GDBN} provides @code{struct regcache}, associated with a
3786 particular @code{struct gdbarch} to hold the cached values of the raw
3787 registers.  A set of functions is provided to access both the raw
3788 registers (with @code{raw} in their name) and the full set of cooked
3789 registers (with @code{cooked} in their name).  Functions are provided
3790 to ensure the register cache is kept synchronized with the values of
3791 the actual registers in the target.
3792
3793 Accessing registers through the @code{struct regcache} routines will
3794 ensure that the appropriate @code{struct gdbarch} functions are called
3795 when necessary to access the underlying target architecture.  In general
3796 users should use the @dfn{cooked} functions, since these will map to the
3797 @dfn{raw} functions automatically as appropriate.
3798
3799 @findex regcache_cooked_read
3800 @findex regcache_cooked_write
3801 @cindex @code{gdb_byte}
3802 @findex regcache_cooked_read_signed
3803 @findex regcache_cooked_read_unsigned
3804 @findex regcache_cooked_write_signed
3805 @findex regcache_cooked_write_unsigned
3806 The two key functions are @code{regcache_cooked_read} and
3807 @code{regcache_cooked_write} which read or write a register from or to
3808 a byte buffer (type @code{gdb_byte *}).  For convenience the wrapper
3809 functions @code{regcache_cooked_read_signed},
3810 @code{regcache_cooked_read_unsigned},
3811 @code{regcache_cooked_write_signed} and
3812 @code{regcache_cooked_write_unsigned} are provided, which read or
3813 write the value using the buffer and convert to or from an integral
3814 value as appropriate.
3815
3816 @node Frame Interpretation
3817 @section Frame Interpretation
3818
3819 @menu
3820 * All About Stack Frames::
3821 * Frame Handling Terminology::
3822 * Prologue Caches::
3823 * Functions and Variable to Analyze Frames::
3824 * Functions to Access Frame Data::
3825 * Analyzing Stacks---Frame Sniffers::
3826 @end menu
3827
3828 @node All About Stack Frames
3829 @subsection All About Stack Frames
3830
3831 @value{GDBN} needs to understand the stack on which local (automatic)
3832 variables are stored.  The area of the stack containing all the local
3833 variables for a function invocation is known as the @dfn{stack frame}
3834 for that function (or colloquially just as the @dfn{frame}).  In turn the
3835 function that called the function will have its stack frame, and so on
3836 back through the chain of functions that have been called.
3837
3838 Almost all architectures have one register dedicated to point to the
3839 end of the stack (the @dfn{stack pointer}).  Many have a second register
3840 which points to the start of the currently active stack frame (the
3841 @dfn{frame pointer}).  The specific arrangements for an architecture are
3842 a key part of the ABI.
3843
3844 A diagram helps to explain this.  Here is a simple program to compute
3845 factorials:
3846
3847 @smallexample
3848 #include <stdio.h>
3849 int fact (int n)
3850 @{
3851   if (0 == n)
3852     @{
3853       return 1;
3854     @}
3855   else
3856     @{
3857       return n * fact (n - 1);
3858     @}
3859 @}
3860
3861 main ()
3862 @{
3863   int i;
3864
3865   for (i = 0; i < 10; i++)
3866     @{
3867       int   f = fact (i);
3868       printf ("%d! = %d\n", i, f);
3869     @}
3870 @}
3871 @end smallexample
3872
3873 Consider the state of the stack when the code reaches line 6 after the
3874 main program has called @code{fact@w{ }(3)}.  The chain of function
3875 calls will be @code{main ()}, @code{fact@w{ }(3)}, @code{fact@w{
3876 }(2)}, @code{@w{fact (1)}} and @code{fact@w{ }(0)}.
3877
3878 In this illustration the stack is falling (as used for example by the
3879 OpenRISC 1000 ABI).  The stack pointer (SP) is at the end of the stack
3880 (lowest address) and the frame pointer (FP) is at the highest address
3881 in the current stack frame.  The following diagram shows how the stack
3882 looks.
3883
3884 @center @image{stack_frame,14cm}
3885
3886 In each stack frame, offset 0 from the stack pointer is the frame
3887 pointer of the previous frame and offset 4 (this is illustrating a
3888 32-bit architecture) from the stack pointer is the return address.
3889 Local variables are indexed from the frame pointer, with negative
3890 indexes.  In the function @code{fact}, offset -4 from the frame
3891 pointer is the argument @var{n}.  In the @code{main} function, offset
3892 -4 from the frame pointer is the local variable @var{i} and offset -8
3893 from the frame pointer is the local variable @var{f}@footnote{This is
3894 a simplified example for illustrative purposes only.  Good optimizing
3895 compilers would not put anything on the stack for such simple
3896 functions.  Indeed they might eliminate the recursion and use of the
3897 stack entirely!}.
3898
3899 It is very easy to get confused when examining stacks.  @value{GDBN}
3900 has terminology it uses rigorously throughout.  The stack frame of the
3901 function currently executing, or where execution stopped is numbered
3902 zero.  In this example frame #0 is the stack frame of the call to
3903 @code{fact@w{ }(0)}.  The stack frame of its calling function
3904 (@code{fact@w{ }(1)} in this case) is numbered #1 and so on back
3905 through the chain of calls.
3906
3907 The main @value{GDBN} data structure describing frames is
3908  @code{@w{struct frame_info}}.  It is not used directly, but only via
3909 its accessor functions.  @code{frame_info} includes information about
3910 the registers in the frame and a pointer to the code of the function
3911 with which the frame is associated.  The entire stack is represented as
3912 a linked list of @code{frame_info} structs.
3913
3914 @node Frame Handling Terminology
3915 @subsection Frame Handling Terminology
3916
3917 It is easy to get confused when referencing stack frames.  @value{GDBN}
3918 uses some precise terminology.
3919
3920 @itemize @bullet
3921
3922 @item
3923 @cindex THIS frame
3924 @cindex stack frame, definition of THIS frame
3925 @cindex frame, definition of THIS frame
3926 @dfn{THIS} frame is the frame currently under consideration.
3927
3928 @item
3929 @cindex NEXT frame
3930 @cindex stack frame, definition of NEXT frame
3931 @cindex frame, definition of NEXT frame
3932 The @dfn{NEXT} frame, also sometimes called the inner or newer frame is the
3933 frame of the function called by the function of THIS frame.
3934
3935 @item
3936 @cindex PREVIOUS frame
3937 @cindex stack frame, definition of PREVIOUS frame
3938 @cindex frame, definition of PREVIOUS frame
3939 The @dfn{PREVIOUS} frame, also sometimes called the outer or older frame is
3940 the frame of the function which called the function of THIS frame.
3941
3942 @end itemize
3943
3944 So in the example in the previous section (@pxref{All About Stack
3945 Frames, , All About Stack Frames}), if THIS frame is #3 (the call to
3946 @code{fact@w{ }(3)}), the NEXT frame is frame #2 (the call to
3947 @code{fact@w{ }(2)}) and the PREVIOUS frame is frame #4 (the call to
3948 @code{main@w{ }()}).
3949
3950 @cindex innermost frame
3951 @cindex stack frame, definition of innermost frame
3952 @cindex frame, definition of innermost frame
3953 The @dfn{innermost} frame is the frame of the current executing
3954 function, or where the program stopped, in this example, in the middle
3955 of the call to @code{@w{fact (0))}}.  It is always numbered frame #0.
3956
3957 @cindex base of a frame
3958 @cindex stack frame, definition of base of a frame
3959 @cindex frame, definition of base of a frame
3960 The @dfn{base} of a frame is the address immediately before the start
3961 of the NEXT frame.  For a stack which grows down in memory (a
3962 @dfn{falling} stack) this will be the lowest address and for a stack
3963 which grows up in memory (a @dfn{rising} stack) this will be the
3964 highest address in the frame.
3965
3966 @value{GDBN} functions to analyze the stack are typically given a
3967 pointer to the NEXT frame to determine information about THIS
3968 frame.  Information about THIS frame includes data on where the
3969 registers of the PREVIOUS frame are stored in this stack frame.  In
3970 this example the frame pointer of the PREVIOUS frame is stored at
3971 offset 0 from the stack pointer of THIS frame.
3972
3973 @cindex unwinding
3974 @cindex stack frame, definition of unwinding
3975 @cindex frame, definition of unwinding
3976 The process whereby a function is given a pointer to the NEXT
3977 frame to work out information about THIS frame is referred to as
3978 @dfn{unwinding}.  The @value{GDBN} functions involved in this typically
3979 include unwind in their name.
3980
3981 @cindex sniffing
3982 @cindex stack frame, definition of sniffing
3983 @cindex frame, definition of sniffing
3984 The process of analyzing a target to determine the information that
3985 should go in struct frame_info is called @dfn{sniffing}.  The functions
3986 that carry this out are called sniffers and typically include sniffer
3987 in their name.  More than one sniffer may be required to extract all
3988 the information for a particular frame.
3989
3990 @cindex sentinel frame
3991 @cindex stack frame, definition of sentinel frame
3992 @cindex frame, definition of sentinel frame
3993 Because so many functions work using the NEXT frame, there is an issue
3994 about addressing the innermost frame---it has no NEXT frame.  To solve
3995 this @value{GDBN} creates a dummy frame #-1, known as the
3996 @dfn{sentinel} frame.
3997
3998 @node Prologue Caches
3999 @subsection Prologue Caches
4000
4001 @cindex function prologue
4002 @cindex prologue of a function
4003 All the frame sniffing functions typically examine the code at the
4004 start of the corresponding function, to determine the state of
4005 registers.  The ABI will save old values and set new values of key
4006 registers at the start of each function in what is known as the
4007 function @dfn{prologue}.
4008
4009 @cindex prologue cache
4010 For any particular stack frame this data does not change, so all the
4011 standard unwinding functions, in addition to receiving a pointer to
4012 the NEXT frame as their first argument, receive a pointer to a
4013 @dfn{prologue cache} as their second argument.  This can be used to store
4014 values associated with a particular frame, for reuse on subsequent
4015 calls involving the same frame.
4016
4017 It is up to the user to define the structure used (it is a
4018 @code{void@w{ }*} pointer) and arrange allocation and deallocation of
4019 storage.  However for general use, @value{GDBN} provides
4020 @code{@w{struct trad_frame_cache}}, with a set of accessor
4021 routines.  This structure holds the stack and code address of
4022 THIS frame, the base address of the frame, a pointer to the
4023 struct @code{frame_info} for the NEXT frame and details of
4024 where the registers of the PREVIOUS frame may be found in THIS
4025 frame.
4026
4027 Typically the first time any sniffer function is called with NEXT
4028 frame, the prologue sniffer for THIS frame will be @code{NULL}.  The
4029 sniffer will analyze the frame, allocate a prologue cache structure
4030 and populate it.  Subsequent calls using the same NEXT frame will
4031 pass in this prologue cache, so the data can be returned with no
4032 additional analysis.
4033
4034 @node Functions and Variable to Analyze Frames
4035 @subsection Functions and Variable to Analyze Frames
4036
4037 These struct @code{gdbarch} functions and variable should be defined
4038 to provide analysis of the stack frame and allow it to be adjusted as
4039 required.
4040
4041 @deftypefn {Architecture Function} CORE_ADDR skip_prologue (struct gdbarch *@var{gdbarch}, CORE_ADDR @var{pc})
4042
4043 The prologue of a function is the code at the beginning of the
4044 function which sets up the stack frame, saves the return address
4045 etc.  The code representing the behavior of the function starts after
4046 the prologue.
4047
4048 This function skips past the prologue of a function if the program
4049 counter, @var{pc}, is within the prologue of a function.  The result is
4050 the program counter immediately after the prologue.  With modern
4051 optimizing compilers, this may be a far from trivial exercise.  However
4052 the required information may be within the binary as DWARF2 debugging
4053 information, making the job much easier.
4054
4055 The default value is @code{NULL} (not defined).  This function should always
4056 be provided, but can take advantage of DWARF2 debugging information,
4057 if that is available.
4058
4059 @end deftypefn
4060
4061 @deftypefn {Architecture Function} int inner_than (CORE_ADDR @var{lhs}, CORE_ADDR @var{rhs})
4062 @findex core_addr_lessthan
4063 @findex core_addr_greaterthan
4064
4065 Given two frame or stack pointers, return non-zero (true) if the first
4066 represents the @dfn{inner} stack frame and 0 (false) otherwise.  This
4067 is used to determine whether the target has a stack which grows up in
4068 memory (rising stack) or grows down in memory (falling stack).
4069 @xref{All About Stack Frames, , All About Stack Frames}, for an
4070 explanation of @dfn{inner} frames.
4071
4072 The default value of this function is @code{NULL} and it should always
4073 be defined.  However for almost all architectures one of the built-in
4074 functions can be used: @code{core_addr_lessthan} (for stacks growing
4075 down in memory) or @code{core_addr_greaterthan} (for stacks growing up
4076 in memory).
4077
4078 @end deftypefn
4079
4080 @anchor{frame_align}
4081 @deftypefn {Architecture Function} CORE_ADDR frame_align (struct gdbarch *@var{gdbarch}, CORE_ADDR @var{address})
4082 @findex align_down
4083 @findex align_up
4084
4085 The architecture may have constraints on how its frames are
4086 aligned.  For example the OpenRISC 1000 ABI requires stack frames to be
4087 double-word aligned, but 32-bit versions of the architecture allocate
4088 single-word values to the stack.  Thus extra padding may be needed at
4089 the end of a stack frame.
4090
4091 Given a proposed address for the stack pointer, this function
4092 returns a suitably aligned address (by expanding the stack frame).
4093
4094 The default value is @code{NULL} (undefined).  This function should be defined
4095 for any architecture where it is possible the stack could become
4096 misaligned.  The utility functions @code{align_down} (for falling
4097 stacks) and @code{align_up} (for rising stacks) will facilitate the
4098 implementation of this function.
4099
4100 @end deftypefn
4101
4102 @deftypevr {Architecture Variable} int frame_red_zone_size
4103
4104 Some ABIs reserve space beyond the end of the stack for use by leaf
4105 functions without prologue or epilogue or by exception handlers (for
4106 example the OpenRISC 1000).
4107
4108 This is known as a @dfn{red zone} (AMD terminology).  The @sc{amd64}
4109 (nee x86-64) ABI documentation refers to the @dfn{red zone} when
4110 describing this scratch area.
4111
4112 The default value is 0.  Set this field if the architecture has such a
4113 red zone.  The value must be aligned as required by the ABI (see
4114 @code{frame_align} above for an explanation of stack frame alignment).
4115
4116 @end deftypevr
4117
4118 @node Functions to Access Frame Data
4119 @subsection Functions to Access Frame Data
4120
4121 These functions provide access to key registers and arguments in the
4122 stack frame.
4123
4124 @deftypefn {Architecture Function} CORE_ADDR unwind_pc (struct gdbarch *@var{gdbarch}, struct frame_info *@var{next_frame})
4125
4126 This function is given a pointer to the NEXT stack frame (@pxref{All
4127 About Stack Frames, , All About Stack Frames}, for how frames are
4128 represented) and returns the value of the program counter in the
4129 PREVIOUS frame (i.e.@: the frame of the function that called THIS
4130 one).  This is commonly referred to as the @dfn{return address}.
4131
4132 The implementation, which must be frame agnostic (work with any frame),
4133 is typically no more than:
4134
4135 @smallexample
4136 ULONGEST pc;
4137 pc = frame_unwind_register_unsigned (next_frame, @var{ARCH}_PC_REGNUM);
4138 return gdbarch_addr_bits_remove (gdbarch, pc);
4139 @end smallexample
4140
4141 @end deftypefn
4142
4143 @deftypefn {Architecture Function} CORE_ADDR unwind_sp (struct gdbarch *@var{gdbarch}, struct frame_info *@var{next_frame})
4144
4145 This function is given a pointer to the NEXT stack frame
4146 (@pxref{All About Stack Frames, , All About Stack Frames} for how
4147 frames are represented) and returns the value of the stack pointer in
4148 the PREVIOUS frame (i.e.@: the frame of the function that called
4149 THIS one).
4150
4151 The implementation, which must be frame agnostic (work with any frame),
4152 is typically no more than:
4153
4154 @smallexample
4155 ULONGEST sp;
4156 sp = frame_unwind_register_unsigned (next_frame, @var{ARCH}_SP_REGNUM);
4157 return gdbarch_addr_bits_remove (gdbarch, sp);
4158 @end smallexample
4159
4160 @end deftypefn
4161
4162 @deftypefn {Architecture Function} int frame_num_args (struct gdbarch *@var{gdbarch}, struct frame_info *@var{this_frame})
4163
4164 This function is given a pointer to THIS stack frame (@pxref{All
4165 About Stack Frames, , All About Stack Frames} for how frames are
4166 represented), and returns the number of arguments that are being
4167 passed, or -1 if not known.
4168
4169 The default value is @code{NULL} (undefined), in which case the number of
4170 arguments passed on any stack frame is always unknown.  For many
4171 architectures this will be a suitable default.
4172
4173 @end deftypefn
4174
4175 @node Analyzing Stacks---Frame Sniffers
4176 @subsection Analyzing Stacks---Frame Sniffers
4177
4178 When a program stops, @value{GDBN} needs to construct the chain of
4179 struct @code{frame_info} representing the state of the stack using
4180 appropriate @dfn{sniffers}.
4181
4182 Each architecture requires appropriate sniffers, but they do not form
4183 entries in @code{@w{struct gdbarch}}, since more than one sniffer may
4184 be required and a sniffer may be suitable for more than one
4185 @code{@w{struct gdbarch}}.  Instead sniffers are associated with
4186 architectures using the following functions.
4187
4188 @itemize @bullet
4189
4190 @item
4191 @findex frame_unwind_append_sniffer
4192 @code{frame_unwind_append_sniffer} is used to add a new sniffer to
4193 analyze THIS frame when given a pointer to the NEXT frame.
4194
4195 @item
4196 @findex frame_base_append_sniffer
4197 @code{frame_base_append_sniffer} is used to add a new sniffer
4198 which can determine information about the base of a stack frame.
4199
4200 @item
4201 @findex frame_base_set_default
4202 @code{frame_base_set_default} is used to specify the default base
4203 sniffer.
4204
4205 @end itemize
4206
4207 These functions all take a reference to @code{@w{struct gdbarch}}, so
4208 they are associated with a specific architecture.  They are usually
4209 called in the @code{gdbarch} initialization function, after the
4210 @code{gdbarch} struct has been set up.  Unless a default has been set, the
4211 most recently appended sniffer will be tried first.
4212
4213 The main frame unwinding sniffer (as set by
4214 @code{frame_unwind_append_sniffer)} returns a structure specifying
4215 a set of sniffing functions:
4216
4217 @cindex @code{frame_unwind}
4218 @smallexample
4219 struct frame_unwind
4220 @{
4221    enum frame_type            type;
4222    frame_this_id_ftype       *this_id;
4223    frame_prev_register_ftype *prev_register;
4224    const struct frame_data   *unwind_data;
4225    frame_sniffer_ftype       *sniffer;
4226    frame_prev_pc_ftype       *prev_pc;
4227    frame_dealloc_cache_ftype *dealloc_cache;
4228 @};
4229 @end smallexample
4230
4231 The @code{type} field indicates the type of frame this sniffer can
4232 handle: normal, dummy (@pxref{Functions Creating Dummy Frames, ,
4233 Functions Creating Dummy Frames}), signal handler or sentinel.  Signal
4234 handlers sometimes have their own simplified stack structure for
4235 efficiency, so may need their own handlers.
4236
4237 The @code{unwind_data} field holds additional information which may be
4238 relevant to particular types of frame.  For example it may hold
4239 additional information for signal handler frames.
4240
4241 The remaining fields define functions that yield different types of
4242 information when given a pointer to the NEXT stack frame.  Not all
4243 functions need be provided.  If an entry is @code{NULL}, the next sniffer will
4244 be tried instead.
4245
4246 @itemize @bullet
4247
4248 @item
4249 @code{this_id} determines the stack pointer and function (code
4250 entry point) for THIS stack frame.
4251
4252 @item
4253 @code{prev_register} determines where the values of registers for
4254 the PREVIOUS stack frame are stored in THIS stack frame.
4255
4256 @item
4257 @code{sniffer} takes a look at THIS frame's registers to
4258 determine if this is the appropriate unwinder.
4259
4260 @item
4261 @code{prev_pc} determines the program counter for THIS
4262 frame.  Only needed if the program counter is not an ordinary register
4263 (@pxref{Register Architecture Functions & Variables,
4264 , Functions and Variables Specifying the Register Architecture}).
4265
4266 @item
4267 @code{dealloc_cache} frees any additional memory associated with
4268 the prologue cache for this frame (@pxref{Prologue Caches, , Prologue
4269 Caches}).
4270
4271 @end itemize
4272
4273 In general it is only the @code{this_id} and @code{prev_register}
4274 fields that need be defined for custom sniffers.
4275
4276 The frame base sniffer is much simpler.  It is a @code{@w{struct
4277 frame_base}}, which refers to the corresponding @code{frame_unwind}
4278 struct and whose fields refer to functions yielding various addresses
4279 within the frame.
4280
4281 @cindex @code{frame_base}
4282 @smallexample
4283 struct frame_base
4284 @{
4285    const struct frame_unwind *unwind;
4286    frame_this_base_ftype     *this_base;
4287    frame_this_locals_ftype   *this_locals;
4288    frame_this_args_ftype     *this_args;
4289 @};
4290 @end smallexample
4291
4292 All the functions referred to take a pointer to the NEXT frame as
4293 argument. The function referred to by @code{this_base} returns the
4294 base address of THIS frame, the function referred to by
4295 @code{this_locals} returns the base address of local variables in THIS
4296 frame and the function referred to by @code{this_args} returns the
4297 base address of the function arguments in this frame.
4298
4299 As described above, the base address of a frame is the address
4300 immediately before the start of the NEXT frame.  For a falling
4301 stack, this is the lowest address in the frame and for a rising stack
4302 it is the highest address in the frame.  For most architectures the
4303 same address is also the base address for local variables and
4304 arguments, in which case the same function can be used for all three
4305 entries@footnote{It is worth noting that if it cannot be determined in any
4306 other way (for example by there being a register with the name
4307 @code{"fp"}), then the result of the @code{this_base} function will be
4308 used as the value of the frame pointer variable @kbd{$fp} in
4309 @value{GDBN}.  This is very often not correct (for example with the
4310 OpenRISC 1000, this value is the stack pointer, @kbd{$sp}).  In this
4311 case a register (raw or pseudo) with the name @code{"fp"} should be
4312 defined.  It will be used in preference as the value of @kbd{$fp}.}.
4313
4314 @node Inferior Call Setup
4315 @section Inferior Call Setup
4316 @cindex calls to the inferior
4317
4318 @menu
4319 * About Dummy Frames::
4320 * Functions Creating Dummy Frames::
4321 @end menu
4322
4323 @node About Dummy Frames
4324 @subsection About Dummy Frames
4325 @cindex dummy frames
4326
4327 @value{GDBN} can call functions in the target code (for example by
4328 using the @kbd{call} or @kbd{print} commands).  These functions may be
4329 breakpointed, and it is essential that if a function does hit a
4330 breakpoint, commands like @kbd{backtrace} work correctly.
4331
4332 This is achieved by making the stack look as though the function had
4333 been called from the point where @value{GDBN} had previously stopped.
4334 This requires that @value{GDBN} can set up stack frames appropriate for
4335 such function calls.
4336
4337 @node Functions Creating Dummy Frames
4338 @subsection Functions Creating Dummy Frames
4339
4340 The following functions provide the functionality to set up such
4341 @dfn{dummy} stack frames.
4342
4343 @deftypefn {Architecture Function} CORE_ADDR push_dummy_call (struct gdbarch *@var{gdbarch}, struct value *@var{function}, struct regcache *@var{regcache}, CORE_ADDR @var{bp_addr}, int  @var{nargs}, struct value **@var{args}, CORE_ADDR @var{sp}, int  @var{struct_return}, CORE_ADDR @var{struct_addr})
4344
4345 This function sets up a dummy stack frame for the function about to be
4346 called.  @code{push_dummy_call} is given the arguments to be passed
4347 and must copy them into registers or push them on to the stack as
4348 appropriate for the ABI.
4349
4350 @var{function} is a pointer to the function
4351 that will be called and @var{regcache} the register cache from which
4352 values should be obtained.  @var{bp_addr} is the address to which the
4353 function should return (which is breakpointed, so @value{GDBN} can
4354 regain control, hence the name).  @var{nargs} is the number of
4355 arguments to pass and @var{args} an array containing the argument
4356 values.  @var{struct_return} is non-zero (true) if the function returns
4357 a structure, and if so @var{struct_addr} is the address in which the
4358 structure should be returned.
4359
4360  After calling this function, @value{GDBN} will pass control to the
4361 target at the address of the function, which will find the stack and
4362 registers set up just as expected.
4363
4364 The default value of this function is @code{NULL} (undefined).  If the
4365 function is not defined, then @value{GDBN} will not allow the user to
4366 call functions within the target being debugged.
4367
4368 @end deftypefn
4369
4370 @deftypefn {Architecture Function} {struct frame_id} unwind_dummy_id (struct gdbarch *@var{gdbarch}, struct frame_info *@var{next_frame}) 
4371
4372 This is the inverse of @code{push_dummy_call} which restores the stack
4373 pointer and program counter after a call to evaluate a function using
4374 a dummy stack frame.  The result is a @code{@w{struct frame_id}}, which
4375 contains the value of the stack pointer and program counter to be
4376 used.
4377
4378 The NEXT frame pointer is provided as argument,
4379 @var{next_frame}.  THIS frame is the frame of the dummy function,
4380 which can be unwound, to yield the required stack pointer and program
4381 counter from the PREVIOUS frame.
4382
4383 The default value is @code{NULL} (undefined).  If @code{push_dummy_call} is
4384 defined, then this function should also be defined.
4385
4386 @end deftypefn
4387
4388 @deftypefn {Architecture Function} CORE_ADDR push_dummy_code (struct gdbarch *@var{gdbarch}, CORE_ADDR @var{sp}, CORE_ADDR @var{funaddr}, struct value **@var{args}, int  @var{nargs}, struct type *@var{value_type}, CORE_ADDR *@var{real_pc}, CORE_ADDR *@var{bp_addr}, struct regcache *@var{regcache})
4389
4390 If this function is not defined (its default value is @code{NULL}), a dummy
4391 call will use the entry point of the currently loaded code on the
4392 target as its return address.  A temporary breakpoint will be set
4393 there, so the location must be writable and have room for a
4394 breakpoint.
4395
4396 It is possible that this default is not suitable.  It might not be
4397 writable (in ROM possibly), or the ABI might require code to be
4398 executed on return from a call to unwind the stack before the
4399 breakpoint is encountered.
4400
4401 If either of these is the case, then push_dummy_code should be defined
4402 to push an instruction sequence onto the end of the stack to which the
4403 dummy call should return.
4404
4405 The arguments are essentially the same as those to
4406 @code{push_dummy_call}.  However the function is provided with the
4407 type of the function result, @var{value_type}, @var{bp_addr} is used
4408 to return a value (the address at which the breakpoint instruction
4409 should be inserted) and @var{real pc} is used to specify the resume
4410 address when starting the call sequence.  The function should return
4411 the updated innermost stack address.
4412
4413 @quotation
4414 @emph{Note:} This does require that code in the stack can be executed.
4415 Some Harvard architectures may not allow this.
4416 @end quotation
4417
4418 @end deftypefn
4419
4420 @node Adding support for debugging core files
4421 @section Adding support for debugging core files
4422 @cindex core files
4423
4424 The prerequisite for adding core file support in @value{GDBN} is to have
4425 core file support in BFD.
4426
4427 Once BFD support is available, writing the apropriate
4428 @code{regset_from_core_section} architecture function should be all
4429 that is needed in order to add support for core files in @value{GDBN}.
4430
4431 @node Defining Other Architecture Features 
4432 @section Defining Other Architecture Features 
4433
4434 This section describes other functions and values in @code{gdbarch},
4435 together with some useful macros, that you can use to define the
4436 target architecture.
4437
4438 @table @code
4439
4440 @item CORE_ADDR gdbarch_addr_bits_remove (@var{gdbarch}, @var{addr})
4441 @findex gdbarch_addr_bits_remove
4442 If a raw machine instruction address includes any bits that are not
4443 really part of the address, then this function is used to zero those bits in
4444 @var{addr}.  This is only used for addresses of instructions, and even then not
4445 in all contexts.
4446
4447 For example, the two low-order bits of the PC on the Hewlett-Packard PA
4448 2.0 architecture contain the privilege level of the corresponding
4449 instruction.  Since instructions must always be aligned on four-byte
4450 boundaries, the processor masks out these bits to generate the actual
4451 address of the instruction.  @code{gdbarch_addr_bits_remove} would then for
4452 example look like that:
4453 @smallexample
4454 arch_addr_bits_remove (CORE_ADDR addr)
4455 @{
4456   return (addr &= ~0x3);
4457 @}
4458 @end smallexample
4459
4460 @item int address_class_name_to_type_flags (@var{gdbarch}, @var{name}, @var{type_flags_ptr})
4461 @findex address_class_name_to_type_flags
4462 If @var{name} is a valid address class qualifier name, set the @code{int}
4463 referenced by @var{type_flags_ptr} to the mask representing the qualifier
4464 and return 1.  If @var{name} is not a valid address class qualifier name,
4465 return 0.
4466
4467 The value for @var{type_flags_ptr} should be one of
4468 @code{TYPE_FLAG_ADDRESS_CLASS_1}, @code{TYPE_FLAG_ADDRESS_CLASS_2}, or
4469 possibly some combination of these values or'd together.
4470 @xref{Target Architecture Definition, , Address Classes}.
4471
4472 @item int address_class_name_to_type_flags_p (@var{gdbarch})
4473 @findex address_class_name_to_type_flags_p
4474 Predicate which indicates whether @code{address_class_name_to_type_flags}
4475 has been defined.
4476
4477 @item int gdbarch_address_class_type_flags (@var{gdbarch}, @var{byte_size}, @var{dwarf2_addr_class})
4478 @findex gdbarch_address_class_type_flags
4479 Given a pointers byte size (as described by the debug information) and
4480 the possible @code{DW_AT_address_class} value, return the type flags
4481 used by @value{GDBN} to represent this address class.  The value
4482 returned should be one of @code{TYPE_FLAG_ADDRESS_CLASS_1},
4483 @code{TYPE_FLAG_ADDRESS_CLASS_2}, or possibly some combination of these
4484 values or'd together.
4485 @xref{Target Architecture Definition, , Address Classes}.
4486
4487 @item int gdbarch_address_class_type_flags_p (@var{gdbarch})
4488 @findex gdbarch_address_class_type_flags_p
4489 Predicate which indicates whether @code{gdbarch_address_class_type_flags_p} has
4490 been defined.
4491
4492 @item const char *gdbarch_address_class_type_flags_to_name (@var{gdbarch}, @var{type_flags})
4493 @findex gdbarch_address_class_type_flags_to_name
4494 Return the name of the address class qualifier associated with the type
4495 flags given by @var{type_flags}.
4496
4497 @item int gdbarch_address_class_type_flags_to_name_p (@var{gdbarch})
4498 @findex gdbarch_address_class_type_flags_to_name_p
4499 Predicate which indicates whether @code{gdbarch_address_class_type_flags_to_name} has been defined.
4500 @xref{Target Architecture Definition, , Address Classes}.
4501
4502 @item void gdbarch_address_to_pointer (@var{gdbarch}, @var{type}, @var{buf}, @var{addr})
4503 @findex gdbarch_address_to_pointer
4504 Store in @var{buf} a pointer of type @var{type} representing the address
4505 @var{addr}, in the appropriate format for the current architecture.
4506 This function may safely assume that @var{type} is either a pointer or a
4507 C@t{++} reference type.
4508 @xref{Target Architecture Definition, , Pointers Are Not Always Addresses}.
4509
4510 @item int gdbarch_believe_pcc_promotion (@var{gdbarch})
4511 @findex gdbarch_believe_pcc_promotion
4512 Used to notify if the compiler promotes a @code{short} or @code{char}
4513 parameter to an @code{int}, but still reports the parameter as its
4514 original type, rather than the promoted type.
4515
4516 @item gdbarch_bits_big_endian (@var{gdbarch})
4517 @findex gdbarch_bits_big_endian
4518 This is used if the numbering of bits in the targets does @strong{not} match
4519 the endianism of the target byte order.  A value of 1 means that the bits
4520 are numbered in a big-endian bit order, 0 means little-endian.
4521
4522 @item set_gdbarch_bits_big_endian (@var{gdbarch}, @var{bits_big_endian})
4523 @findex set_gdbarch_bits_big_endian
4524 Calling set_gdbarch_bits_big_endian with a value of 1 indicates that the
4525 bits in the target are numbered in a big-endian bit order, 0 indicates
4526 little-endian.
4527
4528 @item BREAKPOINT
4529 @findex BREAKPOINT
4530 This is the character array initializer for the bit pattern to put into
4531 memory where a breakpoint is set.  Although it's common to use a trap
4532 instruction for a breakpoint, it's not required; for instance, the bit
4533 pattern could be an invalid instruction.  The breakpoint must be no
4534 longer than the shortest instruction of the architecture.
4535
4536 @code{BREAKPOINT} has been deprecated in favor of
4537 @code{gdbarch_breakpoint_from_pc}.
4538
4539 @item BIG_BREAKPOINT
4540 @itemx LITTLE_BREAKPOINT
4541 @findex LITTLE_BREAKPOINT
4542 @findex BIG_BREAKPOINT
4543 Similar to BREAKPOINT, but used for bi-endian targets.
4544
4545 @code{BIG_BREAKPOINT} and @code{LITTLE_BREAKPOINT} have been deprecated in
4546 favor of @code{gdbarch_breakpoint_from_pc}.
4547
4548 @item const gdb_byte *gdbarch_breakpoint_from_pc (@var{gdbarch}, @var{pcptr}, @var{lenptr})
4549 @findex gdbarch_breakpoint_from_pc
4550 @anchor{gdbarch_breakpoint_from_pc} Use the program counter to determine the
4551 contents and size of a breakpoint instruction.  It returns a pointer to
4552 a static string of bytes that encode a breakpoint instruction, stores the
4553 length of the string to @code{*@var{lenptr}}, and adjusts the program
4554 counter (if necessary) to point to the actual memory location where the
4555 breakpoint should be inserted.  May return @code{NULL} to indicate that
4556 software breakpoints are not supported.
4557
4558 Although it is common to use a trap instruction for a breakpoint, it's
4559 not required; for instance, the bit pattern could be an invalid
4560 instruction.  The breakpoint must be no longer than the shortest
4561 instruction of the architecture.
4562
4563 Provided breakpoint bytes can be also used by @code{bp_loc_is_permanent} to
4564 detect permanent breakpoints.  @code{gdbarch_breakpoint_from_pc} should return
4565 an unchanged memory copy if it was called for a location with permanent
4566 breakpoint as some architectures use breakpoint instructions containing
4567 arbitrary parameter value.
4568
4569 Replaces all the other @var{BREAKPOINT} macros.
4570
4571 @item int gdbarch_memory_insert_breakpoint (@var{gdbarch}, @var{bp_tgt})
4572 @itemx gdbarch_memory_remove_breakpoint (@var{gdbarch}, @var{bp_tgt})
4573 @findex gdbarch_memory_remove_breakpoint
4574 @findex gdbarch_memory_insert_breakpoint
4575 Insert or remove memory based breakpoints.  Reasonable defaults
4576 (@code{default_memory_insert_breakpoint} and
4577 @code{default_memory_remove_breakpoint} respectively) have been
4578 provided so that it is not necessary to set these for most
4579 architectures.  Architectures which may want to set
4580 @code{gdbarch_memory_insert_breakpoint} and @code{gdbarch_memory_remove_breakpoint} will likely have instructions that are oddly sized or are not stored in a
4581 conventional manner.
4582
4583 It may also be desirable (from an efficiency standpoint) to define
4584 custom breakpoint insertion and removal routines if
4585 @code{gdbarch_breakpoint_from_pc} needs to read the target's memory for some
4586 reason.
4587
4588 @item CORE_ADDR gdbarch_adjust_breakpoint_address (@var{gdbarch}, @var{bpaddr})
4589 @findex gdbarch_adjust_breakpoint_address
4590 @cindex breakpoint address adjusted
4591 Given an address at which a breakpoint is desired, return a breakpoint
4592 address adjusted to account for architectural constraints on
4593 breakpoint placement.  This method is not needed by most targets.
4594
4595 The FR-V target (see @file{frv-tdep.c}) requires this method.
4596 The FR-V is a VLIW architecture in which a number of RISC-like
4597 instructions are grouped (packed) together into an aggregate
4598 instruction or instruction bundle.  When the processor executes
4599 one of these bundles, the component instructions are executed
4600 in parallel.
4601
4602 In the course of optimization, the compiler may group instructions
4603 from distinct source statements into the same bundle.  The line number
4604 information associated with one of the latter statements will likely
4605 refer to some instruction other than the first one in the bundle.  So,
4606 if the user attempts to place a breakpoint on one of these latter
4607 statements, @value{GDBN} must be careful to @emph{not} place the break
4608 instruction on any instruction other than the first one in the bundle.
4609 (Remember though that the instructions within a bundle execute
4610 in parallel, so the @emph{first} instruction is the instruction
4611 at the lowest address and has nothing to do with execution order.)
4612
4613 The FR-V's @code{gdbarch_adjust_breakpoint_address} method will adjust a
4614 breakpoint's address by scanning backwards for the beginning of
4615 the bundle, returning the address of the bundle.
4616
4617 Since the adjustment of a breakpoint may significantly alter a user's
4618 expectation, @value{GDBN} prints a warning when an adjusted breakpoint
4619 is initially set and each time that that breakpoint is hit.
4620
4621 @item int gdbarch_call_dummy_location (@var{gdbarch})
4622 @findex gdbarch_call_dummy_location
4623 See the file @file{inferior.h}.
4624
4625 This method has been replaced by @code{gdbarch_push_dummy_code}
4626 (@pxref{gdbarch_push_dummy_code}).
4627
4628 @item int gdbarch_cannot_fetch_register (@var{gdbarch}, @var{regum})
4629 @findex gdbarch_cannot_fetch_register
4630 This function should return nonzero if @var{regno} cannot be fetched
4631 from an inferior process.
4632
4633 @item int gdbarch_cannot_store_register (@var{gdbarch}, @var{regnum})
4634 @findex gdbarch_cannot_store_register
4635 This function should return nonzero if @var{regno} should not be
4636 written to the target.  This is often the case for program counters,
4637 status words, and other special registers.  This function returns 0 as
4638 default so that @value{GDBN} will assume that all registers may be written.
4639
4640 @item int gdbarch_convert_register_p (@var{gdbarch}, @var{regnum}, struct type *@var{type})
4641 @findex gdbarch_convert_register_p
4642 Return non-zero if register @var{regnum} represents data values of type
4643 @var{type} in a non-standard form.
4644 @xref{Target Architecture Definition, , Using Different Register and Memory Data Representations}.
4645
4646 @item int gdbarch_fp0_regnum (@var{gdbarch})
4647 @findex gdbarch_fp0_regnum
4648 This function returns the number of the first floating point register,
4649 if the machine has such registers.  Otherwise, it returns -1.
4650
4651 @item CORE_ADDR gdbarch_decr_pc_after_break (@var{gdbarch})
4652 @findex gdbarch_decr_pc_after_break
4653 This function shall return the amount by which to decrement the PC after the
4654 program encounters a breakpoint.  This is often the number of bytes in
4655 @code{BREAKPOINT}, though not always.  For most targets this value will be 0.
4656
4657 @item DISABLE_UNSETTABLE_BREAK (@var{addr})
4658 @findex DISABLE_UNSETTABLE_BREAK
4659 If defined, this should evaluate to 1 if @var{addr} is in a shared
4660 library in which breakpoints cannot be set and so should be disabled.
4661
4662 @item int gdbarch_dwarf2_reg_to_regnum (@var{gdbarch}, @var{dwarf2_regnr})
4663 @findex gdbarch_dwarf2_reg_to_regnum
4664 Convert DWARF2 register number @var{dwarf2_regnr} into @value{GDBN} regnum.
4665 If not defined, no conversion will be performed.
4666
4667 @item int gdbarch_ecoff_reg_to_regnum (@var{gdbarch}, @var{ecoff_regnr})
4668 @findex gdbarch_ecoff_reg_to_regnum
4669 Convert ECOFF register number  @var{ecoff_regnr} into @value{GDBN} regnum.  If
4670 not defined, no conversion will be performed.
4671
4672 @item GCC_COMPILED_FLAG_SYMBOL
4673 @itemx GCC2_COMPILED_FLAG_SYMBOL
4674 @findex GCC2_COMPILED_FLAG_SYMBOL
4675 @findex GCC_COMPILED_FLAG_SYMBOL
4676 If defined, these are the names of the symbols that @value{GDBN} will
4677 look for to detect that GCC compiled the file.  The default symbols
4678 are @code{gcc_compiled.} and @code{gcc2_compiled.},
4679 respectively.  (Currently only defined for the Delta 68.)
4680
4681 @item gdbarch_get_longjmp_target
4682 @findex gdbarch_get_longjmp_target
4683 This function determines the target PC address that @code{longjmp}
4684 will jump to, assuming that we have just stopped at a @code{longjmp}
4685 breakpoint.  It takes a @code{CORE_ADDR *} as argument, and stores the
4686 target PC value through this pointer.  It examines the current state
4687 of the machine as needed, typically by using a manually-determined
4688 offset into the @code{jmp_buf}.  (While we might like to get the offset
4689 from the target's @file{jmpbuf.h}, that header file cannot be assumed
4690 to be available when building a cross-debugger.)
4691
4692 @item DEPRECATED_IBM6000_TARGET
4693 @findex DEPRECATED_IBM6000_TARGET
4694 Shows that we are configured for an IBM RS/6000 system.  This
4695 conditional should be eliminated (FIXME) and replaced by
4696 feature-specific macros.  It was introduced in haste and we are
4697 repenting at leisure.
4698
4699 @item I386_USE_GENERIC_WATCHPOINTS
4700 An x86-based target can define this to use the generic x86 watchpoint
4701 support; see @ref{Algorithms, I386_USE_GENERIC_WATCHPOINTS}.
4702
4703 @item gdbarch_in_function_epilogue_p (@var{gdbarch}, @var{addr})
4704 @findex gdbarch_in_function_epilogue_p
4705 Returns non-zero if the given @var{addr} is in the epilogue of a function.
4706 The epilogue of a function is defined as the part of a function where
4707 the stack frame of the function already has been destroyed up to the
4708 final `return from function call' instruction.
4709
4710 @item int gdbarch_in_solib_return_trampoline (@var{gdbarch}, @var{pc}, @var{name})
4711 @findex gdbarch_in_solib_return_trampoline
4712 Define this function to return nonzero if the program is stopped in the
4713 trampoline that returns from a shared library.
4714
4715 @item target_so_ops.in_dynsym_resolve_code (@var{pc})
4716 @findex in_dynsym_resolve_code
4717 Define this to return nonzero if the program is stopped in the
4718 dynamic linker.
4719
4720 @item SKIP_SOLIB_RESOLVER (@var{pc})
4721 @findex SKIP_SOLIB_RESOLVER
4722 Define this to evaluate to the (nonzero) address at which execution
4723 should continue to get past the dynamic linker's symbol resolution
4724 function.  A zero value indicates that it is not important or necessary
4725 to set a breakpoint to get through the dynamic linker and that single
4726 stepping will suffice.
4727
4728 @item CORE_ADDR gdbarch_integer_to_address (@var{gdbarch}, @var{type}, @var{buf})
4729 @findex gdbarch_integer_to_address
4730 @cindex converting integers to addresses
4731 Define this when the architecture needs to handle non-pointer to address
4732 conversions specially.  Converts that value to an address according to
4733 the current architectures conventions.
4734
4735 @emph{Pragmatics: When the user copies a well defined expression from
4736 their source code and passes it, as a parameter, to @value{GDBN}'s
4737 @code{print} command, they should get the same value as would have been
4738 computed by the target program.  Any deviation from this rule can cause
4739 major confusion and annoyance, and needs to be justified carefully.  In
4740 other words, @value{GDBN} doesn't really have the freedom to do these
4741 conversions in clever and useful ways.  It has, however, been pointed
4742 out that users aren't complaining about how @value{GDBN} casts integers
4743 to pointers; they are complaining that they can't take an address from a
4744 disassembly listing and give it to @code{x/i}.  Adding an architecture
4745 method like @code{gdbarch_integer_to_address} certainly makes it possible for
4746 @value{GDBN} to ``get it right'' in all circumstances.}
4747
4748 @xref{Target Architecture Definition, , Pointers Are Not Always
4749 Addresses}.
4750
4751 @item CORE_ADDR gdbarch_pointer_to_address (@var{gdbarch}, @var{type}, @var{buf})
4752 @findex gdbarch_pointer_to_address
4753 Assume that @var{buf} holds a pointer of type @var{type}, in the
4754 appropriate format for the current architecture.  Return the byte
4755 address the pointer refers to.
4756 @xref{Target Architecture Definition, , Pointers Are Not Always Addresses}.
4757
4758 @item void gdbarch_register_to_value(@var{gdbarch}, @var{frame}, @var{regnum}, @var{type}, @var{fur})
4759 @findex gdbarch_register_to_value
4760 Convert the raw contents of register @var{regnum} into a value of type
4761 @var{type}.
4762 @xref{Target Architecture Definition, , Using Different Register and Memory Data Representations}.
4763
4764 @item REGISTER_CONVERT_TO_VIRTUAL(@var{reg}, @var{type}, @var{from}, @var{to})
4765 @findex REGISTER_CONVERT_TO_VIRTUAL
4766 Convert the value of register @var{reg} from its raw form to its virtual
4767 form.
4768 @xref{Target Architecture Definition, , Raw and Virtual Register Representations}.
4769
4770 @item REGISTER_CONVERT_TO_RAW(@var{type}, @var{reg}, @var{from}, @var{to})
4771 @findex REGISTER_CONVERT_TO_RAW
4772 Convert the value of register @var{reg} from its virtual form to its raw
4773 form.
4774 @xref{Target Architecture Definition, , Raw and Virtual Register Representations}.
4775
4776 @item const struct regset *regset_from_core_section (struct gdbarch * @var{gdbarch}, const char * @var{sect_name}, size_t @var{sect_size})
4777 @findex regset_from_core_section
4778 Return the appropriate register set for a core file section with name
4779 @var{sect_name} and size @var{sect_size}.
4780
4781 @item SOFTWARE_SINGLE_STEP_P()
4782 @findex SOFTWARE_SINGLE_STEP_P
4783 Define this as 1 if the target does not have a hardware single-step
4784 mechanism.  The macro @code{SOFTWARE_SINGLE_STEP} must also be defined.
4785
4786 @item SOFTWARE_SINGLE_STEP(@var{signal}, @var{insert_breakpoints_p})
4787 @findex SOFTWARE_SINGLE_STEP
4788 A function that inserts or removes (depending on
4789 @var{insert_breakpoints_p}) breakpoints at each possible destinations of
4790 the next instruction.  See @file{sparc-tdep.c} and @file{rs6000-tdep.c}
4791 for examples.
4792
4793 @item set_gdbarch_sofun_address_maybe_missing (@var{gdbarch}, @var{set})
4794 @findex set_gdbarch_sofun_address_maybe_missing
4795 Somebody clever observed that, the more actual addresses you have in the
4796 debug information, the more time the linker has to spend relocating
4797 them.  So whenever there's some other way the debugger could find the
4798 address it needs, you should omit it from the debug info, to make
4799 linking faster.
4800
4801 Calling @code{set_gdbarch_sofun_address_maybe_missing} with a non-zero
4802 argument @var{set} indicates that a particular set of hacks of this sort
4803 are in use, affecting @code{N_SO} and @code{N_FUN} entries in stabs-format
4804 debugging information.  @code{N_SO} stabs mark the beginning and ending
4805 addresses of compilation units in the text segment.  @code{N_FUN} stabs
4806 mark the starts and ends of functions.
4807
4808 In this case, @value{GDBN} assumes two things:
4809
4810 @itemize @bullet
4811 @item
4812 @code{N_FUN} stabs have an address of zero.  Instead of using those
4813 addresses, you should find the address where the function starts by
4814 taking the function name from the stab, and then looking that up in the
4815 minsyms (the linker/assembler symbol table).  In other words, the stab
4816 has the name, and the linker/assembler symbol table is the only place
4817 that carries the address.
4818
4819 @item
4820 @code{N_SO} stabs have an address of zero, too.  You just look at the
4821 @code{N_FUN} stabs that appear before and after the @code{N_SO} stab, and
4822 guess the starting and ending addresses of the compilation unit from them.
4823 @end itemize
4824
4825 @item int gdbarch_stabs_argument_has_addr (@var{gdbarch}, @var{type})
4826 @findex gdbarch_stabs_argument_has_addr
4827 @anchor{gdbarch_stabs_argument_has_addr} Define this function to return
4828 nonzero if a function argument of type @var{type} is passed by reference
4829 instead of value.
4830
4831 @item CORE_ADDR gdbarch_push_dummy_call (@var{gdbarch}, @var{function}, @var{regcache}, @var{bp_addr}, @var{nargs}, @var{args}, @var{sp}, @var{struct_return}, @var{struct_addr})
4832 @findex gdbarch_push_dummy_call
4833 @anchor{gdbarch_push_dummy_call} Define this to push the dummy frame's call to
4834 the inferior function onto the stack.  In addition to pushing @var{nargs}, the
4835 code should push @var{struct_addr} (when @var{struct_return} is non-zero), and
4836 the return address (@var{bp_addr}).
4837
4838 @var{function} is a pointer to a @code{struct value}; on architectures that use
4839 function descriptors, this contains the function descriptor value.
4840
4841 Returns the updated top-of-stack pointer.
4842
4843 @item CORE_ADDR gdbarch_push_dummy_code (@var{gdbarch}, @var{sp}, @var{funaddr}, @var{using_gcc}, @var{args}, @var{nargs}, @var{value_type}, @var{real_pc}, @var{bp_addr}, @var{regcache})
4844 @findex gdbarch_push_dummy_code
4845 @anchor{gdbarch_push_dummy_code} Given a stack based call dummy, push the
4846 instruction sequence (including space for a breakpoint) to which the
4847 called function should return.
4848
4849 Set @var{bp_addr} to the address at which the breakpoint instruction
4850 should be inserted, @var{real_pc} to the resume address when starting
4851 the call sequence, and return the updated inner-most stack address.
4852
4853 By default, the stack is grown sufficient to hold a frame-aligned
4854 (@pxref{frame_align}) breakpoint, @var{bp_addr} is set to the address
4855 reserved for that breakpoint, and @var{real_pc} set to @var{funaddr}.
4856
4857 This method replaces @w{@code{gdbarch_call_dummy_location (@var{gdbarch})}}.
4858
4859 @item int gdbarch_sdb_reg_to_regnum (@var{gdbarch}, @var{sdb_regnr})
4860 @findex gdbarch_sdb_reg_to_regnum
4861 Use this function to convert sdb register @var{sdb_regnr} into @value{GDBN}
4862 regnum.  If not defined, no conversion will be done.
4863
4864 @item enum return_value_convention gdbarch_return_value (struct gdbarch *@var{gdbarch}, struct type *@var{valtype}, struct regcache *@var{regcache}, void *@var{readbuf}, const void *@var{writebuf})
4865 @findex gdbarch_return_value
4866 @anchor{gdbarch_return_value} Given a function with a return-value of
4867 type @var{rettype}, return which return-value convention that function
4868 would use.
4869
4870 @value{GDBN} currently recognizes two function return-value conventions:
4871 @code{RETURN_VALUE_REGISTER_CONVENTION} where the return value is found
4872 in registers; and @code{RETURN_VALUE_STRUCT_CONVENTION} where the return
4873 value is found in memory and the address of that memory location is
4874 passed in as the function's first parameter.
4875
4876 If the register convention is being used, and @var{writebuf} is
4877 non-@code{NULL}, also copy the return-value in @var{writebuf} into
4878 @var{regcache}.
4879
4880 If the register convention is being used, and @var{readbuf} is
4881 non-@code{NULL}, also copy the return value from @var{regcache} into
4882 @var{readbuf} (@var{regcache} contains a copy of the registers from the
4883 just returned function).
4884
4885 @emph{Maintainer note: This method replaces separate predicate, extract,
4886 store methods.  By having only one method, the logic needed to determine
4887 the return-value convention need only be implemented in one place.  If
4888 @value{GDBN} were written in an @sc{oo} language, this method would
4889 instead return an object that knew how to perform the register
4890 return-value extract and store.}
4891
4892 @emph{Maintainer note: This method does not take a @var{gcc_p}
4893 parameter, and such a parameter should not be added.  If an architecture
4894 that requires per-compiler or per-function information be identified,
4895 then the replacement of @var{rettype} with @code{struct value}
4896 @var{function} should be pursued.}
4897
4898 @emph{Maintainer note: The @var{regcache} parameter limits this methods
4899 to the inner most frame.  While replacing @var{regcache} with a
4900 @code{struct frame_info} @var{frame} parameter would remove that
4901 limitation there has yet to be a demonstrated need for such a change.}
4902
4903 @item void gdbarch_skip_permanent_breakpoint (@var{gdbarch}, @var{regcache})
4904 @findex gdbarch_skip_permanent_breakpoint
4905 Advance the inferior's PC past a permanent breakpoint.  @value{GDBN} normally
4906 steps over a breakpoint by removing it, stepping one instruction, and
4907 re-inserting the breakpoint.  However, permanent breakpoints are
4908 hardwired into the inferior, and can't be removed, so this strategy
4909 doesn't work.  Calling @code{gdbarch_skip_permanent_breakpoint} adjusts the
4910 processor's state so that execution will resume just after the breakpoint.
4911 This function does the right thing even when the breakpoint is in the delay slot
4912 of a branch or jump.
4913
4914 @item CORE_ADDR gdbarch_skip_trampoline_code (@var{gdbarch}, @var{frame}, @var{pc})
4915 @findex gdbarch_skip_trampoline_code
4916 If the target machine has trampoline code that sits between callers and
4917 the functions being called, then define this function to return a new PC
4918 that is at the start of the real function.
4919
4920 @item int gdbarch_deprecated_fp_regnum (@var{gdbarch})
4921 @findex gdbarch_deprecated_fp_regnum
4922 If the frame pointer is in a register, use this function to return the
4923 number of that register.
4924
4925 @item int gdbarch_stab_reg_to_regnum (@var{gdbarch}, @var{stab_regnr})
4926 @findex gdbarch_stab_reg_to_regnum
4927 Use this function to convert stab register @var{stab_regnr} into @value{GDBN}
4928 regnum.  If not defined, no conversion will be done.
4929
4930 @item SYMBOL_RELOADING_DEFAULT
4931 @findex SYMBOL_RELOADING_DEFAULT
4932 The default value of the ``symbol-reloading'' variable.  (Never defined in
4933 current sources.)
4934
4935 @item TARGET_CHAR_BIT
4936 @findex TARGET_CHAR_BIT
4937 Number of bits in a char; defaults to 8.
4938
4939 @item int gdbarch_char_signed (@var{gdbarch})
4940 @findex gdbarch_char_signed
4941 Non-zero if @code{char} is normally signed on this architecture; zero if
4942 it should be unsigned.
4943
4944 The ISO C standard requires the compiler to treat @code{char} as
4945 equivalent to either @code{signed char} or @code{unsigned char}; any
4946 character in the standard execution set is supposed to be positive.
4947 Most compilers treat @code{char} as signed, but @code{char} is unsigned
4948 on the IBM S/390, RS6000, and PowerPC targets.
4949
4950 @item int gdbarch_double_bit (@var{gdbarch})
4951 @findex gdbarch_double_bit
4952 Number of bits in a double float; defaults to @w{@code{8 * TARGET_CHAR_BIT}}.
4953
4954 @item int gdbarch_float_bit (@var{gdbarch})
4955 @findex gdbarch_float_bit
4956 Number of bits in a float; defaults to @w{@code{4 * TARGET_CHAR_BIT}}.
4957
4958 @item int gdbarch_int_bit (@var{gdbarch})
4959 @findex gdbarch_int_bit
4960 Number of bits in an integer; defaults to @w{@code{4 * TARGET_CHAR_BIT}}.
4961
4962 @item int gdbarch_long_bit (@var{gdbarch})
4963 @findex gdbarch_long_bit
4964 Number of bits in a long integer; defaults to @w{@code{4 * TARGET_CHAR_BIT}}.
4965
4966 @item int gdbarch_long_double_bit (@var{gdbarch})
4967 @findex gdbarch_long_double_bit
4968 Number of bits in a long double float;
4969 defaults to @w{@code{2 * gdbarch_double_bit (@var{gdbarch})}}.
4970
4971 @item int gdbarch_long_long_bit (@var{gdbarch})
4972 @findex gdbarch_long_long_bit
4973 Number of bits in a long long integer; defaults to
4974 @w{@code{2 * gdbarch_long_bit (@var{gdbarch})}}.
4975
4976 @item int gdbarch_ptr_bit (@var{gdbarch})
4977 @findex gdbarch_ptr_bit
4978 Number of bits in a pointer; defaults to
4979 @w{@code{gdbarch_int_bit (@var{gdbarch})}}.
4980
4981 @item int gdbarch_short_bit (@var{gdbarch})
4982 @findex gdbarch_short_bit
4983 Number of bits in a short integer; defaults to @w{@code{2 * TARGET_CHAR_BIT}}.
4984
4985 @item void gdbarch_virtual_frame_pointer (@var{gdbarch}, @var{pc}, @var{frame_regnum}, @var{frame_offset})
4986 @findex gdbarch_virtual_frame_pointer
4987 Returns a @code{(@var{register}, @var{offset})} pair representing the virtual
4988 frame pointer in use at the code address @var{pc}.  If virtual frame
4989 pointers are not used, a default definition simply returns
4990 @code{gdbarch_deprecated_fp_regnum} (or @code{gdbarch_sp_regnum}, if
4991 no frame pointer is defined), with an offset of zero.
4992
4993 @c need to explain virtual frame pointers, they are recorded in agent
4994 @c expressions for tracepoints
4995
4996 @item TARGET_HAS_HARDWARE_WATCHPOINTS
4997 If non-zero, the target has support for hardware-assisted
4998 watchpoints.  @xref{Algorithms, watchpoints}, for more details and
4999 other related macros.
5000
5001 @item int gdbarch_print_insn (@var{gdbarch}, @var{vma}, @var{info})
5002 @findex gdbarch_print_insn
5003 This is the function used by @value{GDBN} to print an assembly
5004 instruction.  It prints the instruction at address @var{vma} in
5005 debugged memory and returns the length of the instruction, in bytes.
5006 This usually points to a function in the @code{opcodes} library
5007 (@pxref{Support Libraries, ,Opcodes}).  @var{info} is a structure (of
5008 type @code{disassemble_info}) defined in the header file
5009 @file{include/dis-asm.h}, and used to pass information to the
5010 instruction decoding routine.
5011
5012 @item frame_id gdbarch_dummy_id (@var{gdbarch}, @var{frame})
5013 @findex gdbarch_dummy_id
5014 @anchor{gdbarch_dummy_id} Given @var{frame} return a @w{@code{struct
5015 frame_id}} that uniquely identifies an inferior function call's dummy
5016 frame.  The value returned must match the dummy frame stack value
5017 previously saved by @code{call_function_by_hand}.
5018
5019 @item void gdbarch_value_to_register (@var{gdbarch}, @var{frame}, @var{type}, @var{buf})
5020 @findex gdbarch_value_to_register
5021 Convert a value of type @var{type} into the raw contents of a register.
5022 @xref{Target Architecture Definition, , Using Different Register and Memory Data Representations}.
5023
5024 @end table
5025
5026 Motorola M68K target conditionals.
5027
5028 @ftable @code
5029 @item BPT_VECTOR
5030 Define this to be the 4-bit location of the breakpoint trap vector.  If
5031 not defined, it will default to @code{0xf}.
5032
5033 @item REMOTE_BPT_VECTOR
5034 Defaults to @code{1}.
5035
5036 @end ftable
5037
5038 @node Adding a New Target
5039 @section Adding a New Target
5040
5041 @cindex adding a target
5042 The following files add a target to @value{GDBN}:
5043
5044 @table @file
5045 @cindex target dependent files
5046
5047 @item gdb/@var{ttt}-tdep.c
5048 Contains any miscellaneous code required for this target machine.  On
5049 some machines it doesn't exist at all.
5050
5051 @item gdb/@var{arch}-tdep.c
5052 @itemx gdb/@var{arch}-tdep.h
5053 This is required to describe the basic layout of the target machine's
5054 processor chip (registers, stack, etc.).  It can be shared among many
5055 targets that use the same processor architecture.
5056
5057 @end table
5058
5059 (Target header files such as
5060 @file{gdb/config/@var{arch}/tm-@var{ttt}.h},
5061 @file{gdb/config/@var{arch}/tm-@var{arch}.h}, and
5062 @file{config/tm-@var{os}.h} are no longer used.)
5063
5064 @findex _initialize_@var{arch}_tdep
5065 A @value{GDBN} description for a new architecture, arch is created by
5066 defining a global function @code{_initialize_@var{arch}_tdep}, by
5067 convention in the source file @file{@var{arch}-tdep.c}.  For
5068 example, in the case of the OpenRISC 1000, this function is called
5069 @code{_initialize_or1k_tdep} and is found in the file
5070 @file{or1k-tdep.c}.
5071
5072 The object file resulting from compiling this source file, which will
5073 contain the implementation of the
5074 @code{_initialize_@var{arch}_tdep} function is specified in the
5075 @value{GDBN} @file{configure.tgt} file, which includes a large case
5076 statement pattern matching against the @code{--target} option of the
5077 @kbd{configure} script.
5078
5079 @quotation
5080 @emph{Note:} If the architecture requires multiple source files, the
5081 corresponding binaries should be included in
5082 @file{configure.tgt}. However if there are header files, the
5083 dependencies on these will not be picked up from the entries in
5084 @file{configure.tgt}. The @file{Makefile.in} file will need extending to
5085 show these dependencies.
5086 @end quotation
5087
5088 @findex gdbarch_register
5089 A new struct gdbarch, defining the new architecture, is created within
5090 the @code{_initialize_@var{arch}_tdep} function by calling
5091 @code{gdbarch_register}:
5092
5093 @smallexample
5094 void gdbarch_register (enum bfd_architecture    architecture,
5095                        gdbarch_init_ftype      *init_func,
5096                        gdbarch_dump_tdep_ftype *tdep_dump_func);
5097 @end smallexample
5098
5099 This function has been described fully in an earlier
5100 section.  @xref{How an Architecture is Represented, , How an
5101 Architecture is Represented}.
5102
5103 The new @code{@w{struct gdbarch}} should contain implementations of
5104 the necessary functions (described in the previous sections) to
5105 describe the basic layout of the target machine's processor chip
5106 (registers, stack, etc.).  It can be shared among many targets that use
5107 the same processor architecture.
5108
5109 @node Target Descriptions
5110 @chapter Target Descriptions
5111 @cindex target descriptions
5112
5113 The target architecture definition (@pxref{Target Architecture Definition})
5114 contains @value{GDBN}'s hard-coded knowledge about an architecture.  For
5115 some platforms, it is handy to have more flexible knowledge about a specific
5116 instance of the architecture---for instance, a processor or development board.
5117 @dfn{Target descriptions} provide a mechanism for the user to tell @value{GDBN}
5118 more about what their target supports, or for the target to tell @value{GDBN}
5119 directly.
5120
5121 For details on writing, automatically supplying, and manually selecting
5122 target descriptions, see @ref{Target Descriptions, , , gdb,
5123 Debugging with @value{GDBN}}.  This section will cover some related
5124 topics about the @value{GDBN} internals.
5125
5126 @menu
5127 * Target Descriptions Implementation::
5128 * Adding Target Described Register Support::
5129 @end menu
5130
5131 @node Target Descriptions Implementation
5132 @section Target Descriptions Implementation
5133 @cindex target descriptions, implementation
5134
5135 Before @value{GDBN} connects to a new target, or runs a new program on
5136 an existing target, it discards any existing target description and
5137 reverts to a default gdbarch.  Then, after connecting, it looks for a
5138 new target description by calling @code{target_find_description}.
5139
5140 A description may come from a user specified file (XML), the remote
5141 @samp{qXfer:features:read} packet (also XML), or from any custom
5142 @code{to_read_description} routine in the target vector.  For instance,
5143 the remote target supports guessing whether a MIPS target is 32-bit or
5144 64-bit based on the size of the @samp{g} packet.
5145
5146 If any target description is found, @value{GDBN} creates a new gdbarch
5147 incorporating the description by calling @code{gdbarch_update_p}.  Any
5148 @samp{<architecture>} element is handled first, to determine which
5149 architecture's gdbarch initialization routine is called to create the
5150 new architecture.  Then the initialization routine is called, and has
5151 a chance to adjust the constructed architecture based on the contents
5152 of the target description.  For instance, it can recognize any
5153 properties set by a @code{to_read_description} routine.  Also
5154 see @ref{Adding Target Described Register Support}.
5155
5156 @node Adding Target Described Register Support
5157 @section Adding Target Described Register Support
5158 @cindex target descriptions, adding register support
5159
5160 Target descriptions can report additional registers specific to an
5161 instance of the target.  But it takes a little work in the architecture
5162 specific routines to support this.
5163
5164 A target description must either have no registers or a complete
5165 set---this avoids complexity in trying to merge standard registers
5166 with the target defined registers.  It is the architecture's
5167 responsibility to validate that a description with registers has
5168 everything it needs.  To keep architecture code simple, the same
5169 mechanism is used to assign fixed internal register numbers to
5170 standard registers.
5171
5172 If @code{tdesc_has_registers} returns 1, the description contains
5173 registers.  The architecture's @code{gdbarch_init} routine should:
5174
5175 @itemize @bullet
5176
5177 @item
5178 Call @code{tdesc_data_alloc} to allocate storage, early, before
5179 searching for a matching gdbarch or allocating a new one.
5180
5181 @item
5182 Use @code{tdesc_find_feature} to locate standard features by name.
5183
5184 @item
5185 Use @code{tdesc_numbered_register} and @code{tdesc_numbered_register_choices}
5186 to locate the expected registers in the standard features.
5187
5188 @item
5189 Return @code{NULL} if a required feature is missing, or if any standard
5190 feature is missing expected registers.  This will produce a warning that
5191 the description was incomplete.
5192
5193 @item
5194 Free the allocated data before returning, unless @code{tdesc_use_registers}
5195 is called.
5196
5197 @item
5198 Call @code{set_gdbarch_num_regs} as usual, with a number higher than any
5199 fixed number passed to @code{tdesc_numbered_register}.
5200
5201 @item
5202 Call @code{tdesc_use_registers} after creating a new gdbarch, before
5203 returning it.
5204
5205 @end itemize
5206
5207 After @code{tdesc_use_registers} has been called, the architecture's
5208 @code{register_name}, @code{register_type}, and @code{register_reggroup_p}
5209 routines will not be called; that information will be taken from
5210 the target description.  @code{num_regs} may be increased to account
5211 for any additional registers in the description.
5212
5213 Pseudo-registers require some extra care:
5214
5215 @itemize @bullet
5216
5217 @item
5218 Using @code{tdesc_numbered_register} allows the architecture to give
5219 constant register numbers to standard architectural registers, e.g.@:
5220 as an @code{enum} in @file{@var{arch}-tdep.h}.  But because
5221 pseudo-registers are always numbered above @code{num_regs},
5222 which may be increased by the description, constant numbers
5223 can not be used for pseudos.  They must be numbered relative to
5224 @code{num_regs} instead.
5225
5226 @item
5227 The description will not describe pseudo-registers, so the
5228 architecture must call @code{set_tdesc_pseudo_register_name},
5229 @code{set_tdesc_pseudo_register_type}, and
5230 @code{set_tdesc_pseudo_register_reggroup_p} to supply routines
5231 describing pseudo registers.  These routines will be passed
5232 internal register numbers, so the same routines used for the
5233 gdbarch equivalents are usually suitable.
5234
5235 @end itemize
5236
5237
5238 @node Target Vector Definition
5239
5240 @chapter Target Vector Definition
5241 @cindex target vector
5242
5243 The target vector defines the interface between @value{GDBN}'s
5244 abstract handling of target systems, and the nitty-gritty code that
5245 actually exercises control over a process or a serial port.
5246 @value{GDBN} includes some 30-40 different target vectors; however,
5247 each configuration of @value{GDBN} includes only a few of them.
5248
5249 @menu
5250 * Managing Execution State::
5251 * Existing Targets::
5252 @end menu
5253
5254 @node Managing Execution State
5255 @section Managing Execution State
5256 @cindex execution state
5257
5258 A target vector can be completely inactive (not pushed on the target
5259 stack), active but not running (pushed, but not connected to a fully
5260 manifested inferior), or completely active (pushed, with an accessible
5261 inferior).  Most targets are only completely inactive or completely
5262 active, but some support persistent connections to a target even
5263 when the target has exited or not yet started.
5264
5265 For example, connecting to the simulator using @code{target sim} does
5266 not create a running program.  Neither registers nor memory are
5267 accessible until @code{run}.  Similarly, after @code{kill}, the
5268 program can not continue executing.  But in both cases @value{GDBN}
5269 remains connected to the simulator, and target-specific commands
5270 are directed to the simulator.
5271
5272 A target which only supports complete activation should push itself
5273 onto the stack in its @code{to_open} routine (by calling
5274 @code{push_target}), and unpush itself from the stack in its
5275 @code{to_mourn_inferior} routine (by calling @code{unpush_target}).
5276
5277 A target which supports both partial and complete activation should
5278 still call @code{push_target} in @code{to_open}, but not call
5279 @code{unpush_target} in @code{to_mourn_inferior}.  Instead, it should
5280 call either @code{target_mark_running} or @code{target_mark_exited}
5281 in its @code{to_open}, depending on whether the target is fully active
5282 after connection.  It should also call @code{target_mark_running} any
5283 time the inferior becomes fully active (e.g.@: in
5284 @code{to_create_inferior} and @code{to_attach}), and
5285 @code{target_mark_exited} when the inferior becomes inactive (in
5286 @code{to_mourn_inferior}).  The target should also make sure to call
5287 @code{target_mourn_inferior} from its @code{to_kill}, to return the
5288 target to inactive state.
5289
5290 @node Existing Targets
5291 @section Existing Targets
5292 @cindex targets
5293
5294 @subsection File Targets
5295
5296 Both executables and core files have target vectors.
5297
5298 @subsection Standard Protocol and Remote Stubs
5299
5300 @value{GDBN}'s file @file{remote.c} talks a serial protocol to code that
5301 runs in the target system.  @value{GDBN} provides several sample
5302 @dfn{stubs} that can be integrated into target programs or operating
5303 systems for this purpose; they are named @file{@var{cpu}-stub.c}.  Many
5304 operating systems, embedded targets, emulators, and simulators already
5305 have a @value{GDBN} stub built into them, and maintenance of the remote
5306 protocol must be careful to preserve compatibility.
5307
5308 The @value{GDBN} user's manual describes how to put such a stub into
5309 your target code.  What follows is a discussion of integrating the
5310 SPARC stub into a complicated operating system (rather than a simple
5311 program), by Stu Grossman, the author of this stub.
5312
5313 The trap handling code in the stub assumes the following upon entry to
5314 @code{trap_low}:
5315
5316 @enumerate
5317 @item
5318 %l1 and %l2 contain pc and npc respectively at the time of the trap;
5319
5320 @item
5321 traps are disabled;
5322
5323 @item
5324 you are in the correct trap window.
5325 @end enumerate
5326
5327 As long as your trap handler can guarantee those conditions, then there
5328 is no reason why you shouldn't be able to ``share'' traps with the stub.
5329 The stub has no requirement that it be jumped to directly from the
5330 hardware trap vector.  That is why it calls @code{exceptionHandler()},
5331 which is provided by the external environment.  For instance, this could
5332 set up the hardware traps to actually execute code which calls the stub
5333 first, and then transfers to its own trap handler.
5334
5335 For the most point, there probably won't be much of an issue with
5336 ``sharing'' traps, as the traps we use are usually not used by the kernel,
5337 and often indicate unrecoverable error conditions.  Anyway, this is all
5338 controlled by a table, and is trivial to modify.  The most important
5339 trap for us is for @code{ta 1}.  Without that, we can't single step or
5340 do breakpoints.  Everything else is unnecessary for the proper operation
5341 of the debugger/stub.
5342
5343 From reading the stub, it's probably not obvious how breakpoints work.
5344 They are simply done by deposit/examine operations from @value{GDBN}.
5345
5346 @subsection ROM Monitor Interface
5347
5348 @subsection Custom Protocols
5349
5350 @subsection Transport Layer
5351
5352 @subsection Builtin Simulator
5353
5354
5355 @node Native Debugging
5356
5357 @chapter Native Debugging
5358 @cindex native debugging
5359
5360 Several files control @value{GDBN}'s configuration for native support:
5361
5362 @table @file
5363 @vindex NATDEPFILES
5364 @item gdb/config/@var{arch}/@var{xyz}.mh
5365 Specifies Makefile fragments needed by a @emph{native} configuration on
5366 machine @var{xyz}.  In particular, this lists the required
5367 native-dependent object files, by defining @samp{NATDEPFILES=@dots{}}.
5368 Also specifies the header file which describes native support on
5369 @var{xyz}, by defining @samp{NAT_FILE= nm-@var{xyz}.h}.  You can also
5370 define @samp{NAT_CFLAGS}, @samp{NAT_ADD_FILES}, @samp{NAT_CLIBS},
5371 @samp{NAT_CDEPS}, @samp{NAT_GENERATED_FILES}, etc.; see @file{Makefile.in}.
5372
5373 @emph{Maintainer's note: The @file{.mh} suffix is because this file
5374 originally contained @file{Makefile} fragments for hosting @value{GDBN}
5375 on machine @var{xyz}.  While the file is no longer used for this
5376 purpose, the @file{.mh} suffix remains.  Perhaps someone will
5377 eventually rename these fragments so that they have a @file{.mn}
5378 suffix.}
5379
5380 @item gdb/config/@var{arch}/nm-@var{xyz}.h
5381 (@file{nm.h} is a link to this file, created by @code{configure}).  Contains C
5382 macro definitions describing the native system environment, such as
5383 child process control and core file support.
5384
5385 @item gdb/@var{xyz}-nat.c
5386 Contains any miscellaneous C code required for this native support of
5387 this machine.  On some machines it doesn't exist at all.
5388 @end table
5389
5390 There are some ``generic'' versions of routines that can be used by
5391 various systems.  These can be customized in various ways by macros
5392 defined in your @file{nm-@var{xyz}.h} file.  If these routines work for
5393 the @var{xyz} host, you can just include the generic file's name (with
5394 @samp{.o}, not @samp{.c}) in @code{NATDEPFILES}.
5395
5396 Otherwise, if your machine needs custom support routines, you will need
5397 to write routines that perform the same functions as the generic file.
5398 Put them into @file{@var{xyz}-nat.c}, and put @file{@var{xyz}-nat.o}
5399 into @code{NATDEPFILES}.
5400
5401 @table @file
5402 @item inftarg.c
5403 This contains the @emph{target_ops vector} that supports Unix child
5404 processes on systems which use ptrace and wait to control the child.
5405
5406 @item procfs.c
5407 This contains the @emph{target_ops vector} that supports Unix child
5408 processes on systems which use /proc to control the child.
5409
5410 @item fork-child.c
5411 This does the low-level grunge that uses Unix system calls to do a ``fork
5412 and exec'' to start up a child process.
5413
5414 @item infptrace.c
5415 This is the low level interface to inferior processes for systems using
5416 the Unix @code{ptrace} call in a vanilla way.
5417 @end table
5418
5419 @section ptrace
5420
5421 @section /proc
5422
5423 @section win32
5424
5425 @section shared libraries
5426
5427 @section Native Conditionals
5428 @cindex native conditionals
5429
5430 When @value{GDBN} is configured and compiled, various macros are
5431 defined or left undefined, to control compilation when the host and
5432 target systems are the same.  These macros should be defined (or left
5433 undefined) in @file{nm-@var{system}.h}.
5434
5435 @table @code
5436
5437 @item I386_USE_GENERIC_WATCHPOINTS
5438 An x86-based machine can define this to use the generic x86 watchpoint
5439 support; see @ref{Algorithms, I386_USE_GENERIC_WATCHPOINTS}.
5440
5441 @item SOLIB_ADD (@var{filename}, @var{from_tty}, @var{targ}, @var{readsyms})
5442 @findex SOLIB_ADD
5443 Define this to expand into an expression that will cause the symbols in
5444 @var{filename} to be added to @value{GDBN}'s symbol table.  If
5445 @var{readsyms} is zero symbols are not read but any necessary low level
5446 processing for @var{filename} is still done.
5447
5448 @item SOLIB_CREATE_INFERIOR_HOOK
5449 @findex SOLIB_CREATE_INFERIOR_HOOK
5450 Define this to expand into any shared-library-relocation code that you
5451 want to be run just after the child process has been forked.
5452
5453 @item START_INFERIOR_TRAPS_EXPECTED
5454 @findex START_INFERIOR_TRAPS_EXPECTED
5455 When starting an inferior, @value{GDBN} normally expects to trap
5456 twice; once when
5457 the shell execs, and once when the program itself execs.  If the actual
5458 number of traps is something other than 2, then define this macro to
5459 expand into the number expected.
5460
5461 @end table
5462
5463 @node Support Libraries
5464
5465 @chapter Support Libraries
5466
5467 @section BFD
5468 @cindex BFD library
5469
5470 BFD provides support for @value{GDBN} in several ways:
5471
5472 @table @emph
5473 @item identifying executable and core files
5474 BFD will identify a variety of file types, including a.out, coff, and
5475 several variants thereof, as well as several kinds of core files.
5476
5477 @item access to sections of files
5478 BFD parses the file headers to determine the names, virtual addresses,
5479 sizes, and file locations of all the various named sections in files
5480 (such as the text section or the data section).  @value{GDBN} simply
5481 calls BFD to read or write section @var{x} at byte offset @var{y} for
5482 length @var{z}.
5483
5484 @item specialized core file support
5485 BFD provides routines to determine the failing command name stored in a
5486 core file, the signal with which the program failed, and whether a core
5487 file matches (i.e.@: could be a core dump of) a particular executable
5488 file.
5489
5490 @item locating the symbol information
5491 @value{GDBN} uses an internal interface of BFD to determine where to find the
5492 symbol information in an executable file or symbol-file.  @value{GDBN} itself
5493 handles the reading of symbols, since BFD does not ``understand'' debug
5494 symbols, but @value{GDBN} uses BFD's cached information to find the symbols,
5495 string table, etc.
5496 @end table
5497
5498 @section opcodes
5499 @cindex opcodes library
5500
5501 The opcodes library provides @value{GDBN}'s disassembler.  (It's a separate
5502 library because it's also used in binutils, for @file{objdump}).
5503
5504 @section readline
5505 @cindex readline library
5506 The @code{readline} library provides a set of functions for use by applications
5507 that allow users to edit command lines as they are typed in.
5508
5509 @section libiberty
5510 @cindex @code{libiberty} library
5511
5512 The @code{libiberty} library provides a set of functions and features
5513 that integrate and improve on functionality found in modern operating
5514 systems.  Broadly speaking, such features can be divided into three
5515 groups: supplemental functions (functions that may be missing in some
5516 environments and operating systems), replacement functions (providing
5517 a uniform and easier to use interface for commonly used standard
5518 functions), and extensions (which provide additional functionality
5519 beyond standard functions).
5520
5521 @value{GDBN} uses various features provided by the @code{libiberty}
5522 library, for instance the C@t{++} demangler, the @acronym{IEEE}
5523 floating format support functions, the input options parser
5524 @samp{getopt}, the @samp{obstack} extension, and other functions.
5525
5526 @subsection @code{obstacks} in @value{GDBN}
5527 @cindex @code{obstacks}
5528
5529 The obstack mechanism provides a convenient way to allocate and free
5530 chunks of memory.  Each obstack is a pool of memory that is managed
5531 like a stack.  Objects (of any nature, size and alignment) are
5532 allocated and freed in a @acronym{LIFO} fashion on an obstack (see
5533 @code{libiberty}'s documentation for a more detailed explanation of
5534 @code{obstacks}).
5535
5536 The most noticeable use of the @code{obstacks} in @value{GDBN} is in
5537 object files.  There is an obstack associated with each internal
5538 representation of an object file.  Lots of things get allocated on
5539 these @code{obstacks}: dictionary entries, blocks, blockvectors,
5540 symbols, minimal symbols, types, vectors of fundamental types, class
5541 fields of types, object files section lists, object files section
5542 offset lists, line tables, symbol tables, partial symbol tables,
5543 string tables, symbol table private data, macros tables, debug
5544 information sections and entries, import and export lists (som),
5545 unwind information (hppa), dwarf2 location expressions data.  Plus
5546 various strings such as directory names strings, debug format strings,
5547 names of types.
5548
5549 An essential and convenient property of all data on @code{obstacks} is
5550 that memory for it gets allocated (with @code{obstack_alloc}) at
5551 various times during a debugging session, but it is released all at
5552 once using the @code{obstack_free} function.  The @code{obstack_free}
5553 function takes a pointer to where in the stack it must start the
5554 deletion from (much like the cleanup chains have a pointer to where to
5555 start the cleanups).  Because of the stack like structure of the
5556 @code{obstacks}, this allows to free only a top portion of the
5557 obstack.  There are a few instances in @value{GDBN} where such thing
5558 happens.  Calls to @code{obstack_free} are done after some local data
5559 is allocated to the obstack.  Only the local data is deleted from the
5560 obstack.  Of course this assumes that nothing between the
5561 @code{obstack_alloc} and the @code{obstack_free} allocates anything
5562 else on the same obstack.  For this reason it is best and safest to
5563 use temporary @code{obstacks}.
5564
5565 Releasing the whole obstack is also not safe per se.  It is safe only
5566 under the condition that we know the @code{obstacks} memory is no
5567 longer needed.  In @value{GDBN} we get rid of the @code{obstacks} only
5568 when we get rid of the whole objfile(s), for instance upon reading a
5569 new symbol file.
5570
5571 @section gnu-regex
5572 @cindex regular expressions library
5573
5574 Regex conditionals.
5575
5576 @table @code
5577 @item C_ALLOCA
5578
5579 @item NFAILURES
5580
5581 @item RE_NREGS
5582
5583 @item SIGN_EXTEND_CHAR
5584
5585 @item SWITCH_ENUM_BUG
5586
5587 @item SYNTAX_TABLE
5588
5589 @item Sword
5590
5591 @item sparc
5592 @end table
5593
5594 @section Array Containers
5595 @cindex Array Containers
5596 @cindex VEC
5597
5598 Often it is necessary to manipulate a dynamic array of a set of
5599 objects.  C forces some bookkeeping on this, which can get cumbersome
5600 and repetitive.  The @file{vec.h} file contains macros for defining
5601 and using a typesafe vector type.  The functions defined will be
5602 inlined when compiling, and so the abstraction cost should be zero.
5603 Domain checks are added to detect programming errors.
5604
5605 An example use would be an array of symbols or section information.
5606 The array can be grown as symbols are read in (or preallocated), and
5607 the accessor macros provided keep care of all the necessary
5608 bookkeeping.  Because the arrays are type safe, there is no danger of
5609 accidentally mixing up the contents.  Think of these as C++ templates,
5610 but implemented in C.
5611
5612 Because of the different behavior of structure objects, scalar objects
5613 and of pointers, there are three flavors of vector, one for each of
5614 these variants.  Both the structure object and pointer variants pass
5615 pointers to objects around --- in the former case the pointers are
5616 stored into the vector and in the latter case the pointers are
5617 dereferenced and the objects copied into the vector.  The scalar
5618 object variant is suitable for @code{int}-like objects, and the vector
5619 elements are returned by value.
5620
5621 There are both @code{index} and @code{iterate} accessors.  The iterator
5622 returns a boolean iteration condition and updates the iteration
5623 variable passed by reference.  Because the iterator will be inlined,
5624 the address-of can be optimized away.
5625
5626 The vectors are implemented using the trailing array idiom, thus they
5627 are not resizeable without changing the address of the vector object
5628 itself.  This means you cannot have variables or fields of vector type
5629 --- always use a pointer to a vector.  The one exception is the final
5630 field of a structure, which could be a vector type.  You will have to
5631 use the @code{embedded_size} & @code{embedded_init} calls to create
5632 such objects, and they will probably not be resizeable (so don't use
5633 the @dfn{safe} allocation variants).  The trailing array idiom is used
5634 (rather than a pointer to an array of data), because, if we allow
5635 @code{NULL} to also represent an empty vector, empty vectors occupy
5636 minimal space in the structure containing them.
5637
5638 Each operation that increases the number of active elements is
5639 available in @dfn{quick} and @dfn{safe} variants.  The former presumes
5640 that there is sufficient allocated space for the operation to succeed
5641 (it dies if there is not).  The latter will reallocate the vector, if
5642 needed.  Reallocation causes an exponential increase in vector size.
5643 If you know you will be adding N elements, it would be more efficient
5644 to use the reserve operation before adding the elements with the
5645 @dfn{quick} operation.  This will ensure there are at least as many
5646 elements as you ask for, it will exponentially increase if there are
5647 too few spare slots.  If you want reserve a specific number of slots,
5648 but do not want the exponential increase (for instance, you know this
5649 is the last allocation), use a negative number for reservation.  You
5650 can also create a vector of a specific size from the get go.
5651
5652 You should prefer the push and pop operations, as they append and
5653 remove from the end of the vector.  If you need to remove several items
5654 in one go, use the truncate operation.  The insert and remove
5655 operations allow you to change elements in the middle of the vector.
5656 There are two remove operations, one which preserves the element
5657 ordering @code{ordered_remove}, and one which does not
5658 @code{unordered_remove}.  The latter function copies the end element
5659 into the removed slot, rather than invoke a memmove operation.  The
5660 @code{lower_bound} function will determine where to place an item in
5661 the array using insert that will maintain sorted order.
5662
5663 If you need to directly manipulate a vector, then the @code{address}
5664 accessor will return the address of the start of the vector.  Also the
5665 @code{space} predicate will tell you whether there is spare capacity in the
5666 vector.  You will not normally need to use these two functions.
5667
5668 Vector types are defined using a
5669 @code{DEF_VEC_@{O,P,I@}(@var{typename})} macro.  Variables of vector
5670 type are declared using a @code{VEC(@var{typename})} macro.  The
5671 characters @code{O}, @code{P} and @code{I} indicate whether
5672 @var{typename} is an object (@code{O}), pointer (@code{P}) or integral
5673 (@code{I}) type.  Be careful to pick the correct one, as you'll get an
5674 awkward and inefficient API if you use the wrong one.  There is a
5675 check, which results in a compile-time warning, for the @code{P} and
5676 @code{I} versions, but there is no check for the @code{O} versions, as
5677 that is not possible in plain C.
5678
5679 An example of their use would be,
5680
5681 @smallexample
5682 DEF_VEC_P(tree);   // non-managed tree vector.
5683
5684 struct my_struct @{
5685   VEC(tree) *v;      // A (pointer to) a vector of tree pointers.
5686 @};
5687
5688 struct my_struct *s;
5689
5690 if (VEC_length(tree, s->v)) @{ we have some contents @}
5691 VEC_safe_push(tree, s->v, decl); // append some decl onto the end
5692 for (ix = 0; VEC_iterate(tree, s->v, ix, elt); ix++)
5693   @{ do something with elt @}
5694
5695 @end smallexample
5696
5697 The @file{vec.h} file provides details on how to invoke the various
5698 accessors provided.  They are enumerated here:
5699
5700 @table @code
5701 @item VEC_length
5702 Return the number of items in the array,
5703
5704 @item VEC_empty
5705 Return true if the array has no elements.
5706
5707 @item VEC_last
5708 @itemx VEC_index
5709 Return the last or arbitrary item in the array.
5710
5711 @item VEC_iterate
5712 Access an array element and indicate whether the array has been
5713 traversed.
5714
5715 @item VEC_alloc
5716 @itemx VEC_free
5717 Create and destroy an array.
5718
5719 @item VEC_embedded_size
5720 @itemx VEC_embedded_init
5721 Helpers for embedding an array as the final element of another struct.
5722
5723 @item VEC_copy
5724 Duplicate an array.
5725
5726 @item VEC_space
5727 Return the amount of free space in an array.
5728
5729 @item VEC_reserve
5730 Ensure a certain amount of free space.
5731
5732 @item VEC_quick_push
5733 @itemx VEC_safe_push
5734 Append to an array, either assuming the space is available, or making
5735 sure that it is.
5736
5737 @item VEC_pop
5738 Remove the last item from an array.
5739
5740 @item VEC_truncate
5741 Remove several items from the end of an array.
5742
5743 @item VEC_safe_grow
5744 Add several items to the end of an array.
5745
5746 @item VEC_replace
5747 Overwrite an item in the array.
5748
5749 @item VEC_quick_insert
5750 @itemx VEC_safe_insert
5751 Insert an item into the middle of the array.  Either the space must
5752 already exist, or the space is created.
5753
5754 @item VEC_ordered_remove
5755 @itemx VEC_unordered_remove
5756 Remove an item from the array, preserving order or not.
5757
5758 @item VEC_block_remove
5759 Remove a set of items from the array.
5760
5761 @item VEC_address
5762 Provide the address of the first element.
5763
5764 @item VEC_lower_bound
5765 Binary search the array.
5766
5767 @end table
5768
5769 @section include
5770
5771 @node Coding
5772
5773 @chapter Coding
5774
5775 This chapter covers topics that are lower-level than the major
5776 algorithms of @value{GDBN}.
5777
5778 @section Cleanups
5779 @cindex cleanups
5780
5781 Cleanups are a structured way to deal with things that need to be done
5782 later.
5783
5784 When your code does something (e.g., @code{xmalloc} some memory, or
5785 @code{open} a file) that needs to be undone later (e.g., @code{xfree}
5786 the memory or @code{close} the file), it can make a cleanup.  The
5787 cleanup will be done at some future point: when the command is finished
5788 and control returns to the top level; when an error occurs and the stack
5789 is unwound; or when your code decides it's time to explicitly perform
5790 cleanups.  Alternatively you can elect to discard the cleanups you
5791 created.
5792
5793 Syntax:
5794
5795 @table @code
5796 @item struct cleanup *@var{old_chain};
5797 Declare a variable which will hold a cleanup chain handle.
5798
5799 @findex make_cleanup
5800 @item @var{old_chain} = make_cleanup (@var{function}, @var{arg});
5801 Make a cleanup which will cause @var{function} to be called with
5802 @var{arg} (a @code{char *}) later.  The result, @var{old_chain}, is a
5803 handle that can later be passed to @code{do_cleanups} or
5804 @code{discard_cleanups}.  Unless you are going to call
5805 @code{do_cleanups} or @code{discard_cleanups}, you can ignore the result
5806 from @code{make_cleanup}.
5807
5808 @findex do_cleanups
5809 @item do_cleanups (@var{old_chain});
5810 Do all cleanups added to the chain since the corresponding
5811 @code{make_cleanup} call was made.
5812
5813 @findex discard_cleanups
5814 @item discard_cleanups (@var{old_chain});
5815 Same as @code{do_cleanups} except that it just removes the cleanups from
5816 the chain and does not call the specified functions.
5817 @end table
5818
5819 Cleanups are implemented as a chain.  The handle returned by
5820 @code{make_cleanups} includes the cleanup passed to the call and any
5821 later cleanups appended to the chain (but not yet discarded or
5822 performed).  E.g.:
5823
5824 @smallexample
5825 make_cleanup (a, 0); 
5826 @{
5827   struct cleanup *old = make_cleanup (b, 0); 
5828   make_cleanup (c, 0)
5829   ...
5830   do_cleanups (old);
5831 @}
5832 @end smallexample
5833
5834 @noindent
5835 will call @code{c()} and @code{b()} but will not call @code{a()}.  The
5836 cleanup that calls @code{a()} will remain in the cleanup chain, and will
5837 be done later unless otherwise discarded.@refill
5838
5839 Your function should explicitly do or discard the cleanups it creates.
5840 Failing to do this leads to non-deterministic behavior since the caller
5841 will arbitrarily do or discard your functions cleanups.  This need leads
5842 to two common cleanup styles.
5843
5844 The first style is try/finally.  Before it exits, your code-block calls
5845 @code{do_cleanups} with the old cleanup chain and thus ensures that your
5846 code-block's cleanups are always performed.  For instance, the following
5847 code-segment avoids a memory leak problem (even when @code{error} is
5848 called and a forced stack unwind occurs) by ensuring that the
5849 @code{xfree} will always be called:
5850
5851 @smallexample
5852 struct cleanup *old = make_cleanup (null_cleanup, 0);
5853 data = xmalloc (sizeof blah);
5854 make_cleanup (xfree, data);
5855 ... blah blah ...
5856 do_cleanups (old);
5857 @end smallexample
5858
5859 The second style is try/except.  Before it exits, your code-block calls
5860 @code{discard_cleanups} with the old cleanup chain and thus ensures that
5861 any created cleanups are not performed.  For instance, the following
5862 code segment, ensures that the file will be closed but only if there is
5863 an error:
5864
5865 @smallexample
5866 FILE *file = fopen ("afile", "r");
5867 struct cleanup *old = make_cleanup (close_file, file);
5868 ... blah blah ...
5869 discard_cleanups (old);
5870 return file;
5871 @end smallexample
5872
5873 Some functions, e.g., @code{fputs_filtered()} or @code{error()}, specify
5874 that they ``should not be called when cleanups are not in place''.  This
5875 means that any actions you need to reverse in the case of an error or
5876 interruption must be on the cleanup chain before you call these
5877 functions, since they might never return to your code (they
5878 @samp{longjmp} instead).
5879
5880 @section Per-architecture module data
5881 @cindex per-architecture module data
5882 @cindex multi-arch data
5883 @cindex data-pointer, per-architecture/per-module
5884
5885 The multi-arch framework includes a mechanism for adding module
5886 specific per-architecture data-pointers to the @code{struct gdbarch}
5887 architecture object.
5888
5889 A module registers one or more per-architecture data-pointers using:
5890
5891 @deftypefn {Architecture Function} {struct gdbarch_data *} gdbarch_data_register_pre_init (gdbarch_data_pre_init_ftype *@var{pre_init})
5892 @var{pre_init} is used to, on-demand, allocate an initial value for a
5893 per-architecture data-pointer using the architecture's obstack (passed
5894 in as a parameter).  Since @var{pre_init} can be called during
5895 architecture creation, it is not parameterized with the architecture.
5896 and must not call modules that use per-architecture data.
5897 @end deftypefn
5898
5899 @deftypefn {Architecture Function} {struct gdbarch_data *} gdbarch_data_register_post_init (gdbarch_data_post_init_ftype *@var{post_init})
5900 @var{post_init} is used to obtain an initial value for a
5901 per-architecture data-pointer @emph{after}.  Since @var{post_init} is
5902 always called after architecture creation, it both receives the fully
5903 initialized architecture and is free to call modules that use
5904 per-architecture data (care needs to be taken to ensure that those
5905 other modules do not try to call back to this module as that will
5906 create in cycles in the initialization call graph).
5907 @end deftypefn
5908
5909 These functions return a @code{struct gdbarch_data} that is used to
5910 identify the per-architecture data-pointer added for that module.
5911
5912 The per-architecture data-pointer is accessed using the function:
5913
5914 @deftypefn {Architecture Function} {void *} gdbarch_data (struct gdbarch *@var{gdbarch}, struct gdbarch_data *@var{data_handle})
5915 Given the architecture @var{arch} and module data handle
5916 @var{data_handle} (returned by @code{gdbarch_data_register_pre_init}
5917 or @code{gdbarch_data_register_post_init}), this function returns the
5918 current value of the per-architecture data-pointer.  If the data
5919 pointer is @code{NULL}, it is first initialized by calling the
5920 corresponding @var{pre_init} or @var{post_init} method.
5921 @end deftypefn
5922
5923 The examples below assume the following definitions:
5924
5925 @smallexample
5926 struct nozel @{ int total; @};
5927 static struct gdbarch_data *nozel_handle;
5928 @end smallexample
5929
5930 A module can extend the architecture vector, adding additional
5931 per-architecture data, using the @var{pre_init} method.  The module's
5932 per-architecture data is then initialized during architecture
5933 creation.
5934
5935 In the below, the module's per-architecture @emph{nozel} is added.  An
5936 architecture can specify its nozel by calling @code{set_gdbarch_nozel}
5937 from @code{gdbarch_init}.
5938
5939 @smallexample
5940 static void *
5941 nozel_pre_init (struct obstack *obstack)
5942 @{
5943   struct nozel *data = OBSTACK_ZALLOC (obstack, struct nozel);
5944   return data;
5945 @}
5946 @end smallexample
5947
5948 @smallexample
5949 extern void
5950 set_gdbarch_nozel (struct gdbarch *gdbarch, int total)
5951 @{
5952   struct nozel *data = gdbarch_data (gdbarch, nozel_handle);
5953   data->total = nozel;
5954 @}
5955 @end smallexample
5956
5957 A module can on-demand create architecture dependent data structures
5958 using @code{post_init}.
5959
5960 In the below, the nozel's total is computed on-demand by
5961 @code{nozel_post_init} using information obtained from the
5962 architecture.
5963
5964 @smallexample
5965 static void *
5966 nozel_post_init (struct gdbarch *gdbarch)
5967 @{
5968   struct nozel *data = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct nozel);
5969   nozel->total = gdbarch@dots{} (gdbarch);
5970   return data;
5971 @}
5972 @end smallexample
5973
5974 @smallexample
5975 extern int
5976 nozel_total (struct gdbarch *gdbarch)
5977 @{
5978   struct nozel *data = gdbarch_data (gdbarch, nozel_handle);
5979   return data->total;
5980 @}
5981 @end smallexample
5982
5983 @section Wrapping Output Lines
5984 @cindex line wrap in output
5985
5986 @findex wrap_here
5987 Output that goes through @code{printf_filtered} or @code{fputs_filtered}
5988 or @code{fputs_demangled} needs only to have calls to @code{wrap_here}
5989 added in places that would be good breaking points.  The utility
5990 routines will take care of actually wrapping if the line width is
5991 exceeded.
5992
5993 The argument to @code{wrap_here} is an indentation string which is
5994 printed @emph{only} if the line breaks there.  This argument is saved
5995 away and used later.  It must remain valid until the next call to
5996 @code{wrap_here} or until a newline has been printed through the
5997 @code{*_filtered} functions.  Don't pass in a local variable and then
5998 return!
5999
6000 It is usually best to call @code{wrap_here} after printing a comma or
6001 space.  If you call it before printing a space, make sure that your
6002 indentation properly accounts for the leading space that will print if
6003 the line wraps there.
6004
6005 Any function or set of functions that produce filtered output must
6006 finish by printing a newline, to flush the wrap buffer, before switching
6007 to unfiltered (@code{printf}) output.  Symbol reading routines that
6008 print warnings are a good example.
6009
6010 @section @value{GDBN} Coding Standards
6011 @cindex coding standards
6012
6013 @value{GDBN} follows the GNU coding standards, as described in
6014 @file{etc/standards.texi}.  This file is also available for anonymous
6015 FTP from GNU archive sites.  @value{GDBN} takes a strict interpretation
6016 of the standard; in general, when the GNU standard recommends a practice
6017 but does not require it, @value{GDBN} requires it.
6018
6019 @value{GDBN} follows an additional set of coding standards specific to
6020 @value{GDBN}, as described in the following sections.
6021
6022
6023 @subsection ISO C
6024
6025 @value{GDBN} assumes an ISO/IEC 9899:1990 (a.k.a.@: ISO C90) compliant
6026 compiler.
6027
6028 @value{GDBN} does not assume an ISO C or POSIX compliant C library.
6029
6030
6031 @subsection Memory Management
6032
6033 @value{GDBN} does not use the functions @code{malloc}, @code{realloc},
6034 @code{calloc}, @code{free} and @code{asprintf}.
6035
6036 @value{GDBN} uses the functions @code{xmalloc}, @code{xrealloc} and
6037 @code{xcalloc} when allocating memory.  Unlike @code{malloc} et.al.@:
6038 these functions do not return when the memory pool is empty.  Instead,
6039 they unwind the stack using cleanups.  These functions return
6040 @code{NULL} when requested to allocate a chunk of memory of size zero.
6041
6042 @emph{Pragmatics: By using these functions, the need to check every
6043 memory allocation is removed.  These functions provide portable
6044 behavior.}
6045
6046 @value{GDBN} does not use the function @code{free}.
6047
6048 @value{GDBN} uses the function @code{xfree} to return memory to the
6049 memory pool.  Consistent with ISO-C, this function ignores a request to
6050 free a @code{NULL} pointer.
6051
6052 @emph{Pragmatics: On some systems @code{free} fails when passed a
6053 @code{NULL} pointer.}
6054
6055 @value{GDBN} can use the non-portable function @code{alloca} for the
6056 allocation of small temporary values (such as strings).
6057
6058 @emph{Pragmatics: This function is very non-portable.  Some systems
6059 restrict the memory being allocated to no more than a few kilobytes.}
6060
6061 @value{GDBN} uses the string function @code{xstrdup} and the print
6062 function @code{xstrprintf}.
6063
6064 @emph{Pragmatics: @code{asprintf} and @code{strdup} can fail.  Print
6065 functions such as @code{sprintf} are very prone to buffer overflow
6066 errors.}
6067
6068
6069 @subsection Compiler Warnings
6070 @cindex compiler warnings
6071
6072 With few exceptions, developers should avoid the configuration option
6073 @samp{--disable-werror} when building @value{GDBN}.  The exceptions
6074 are listed in the file @file{gdb/MAINTAINERS}.  The default, when
6075 building with @sc{gcc}, is @samp{--enable-werror}.
6076
6077 This option causes @value{GDBN} (when built using GCC) to be compiled
6078 with a carefully selected list of compiler warning flags.  Any warnings
6079 from those flags are treated as errors.
6080
6081 The current list of warning flags includes:
6082
6083 @table @samp
6084 @item -Wall
6085 Recommended @sc{gcc} warnings.
6086
6087 @item -Wdeclaration-after-statement
6088
6089 @sc{gcc} 3.x (and later) and @sc{c99} allow declarations mixed with
6090 code, but @sc{gcc} 2.x and @sc{c89} do not.
6091
6092 @item -Wpointer-arith
6093
6094 @item -Wformat-nonliteral
6095 Non-literal format strings, with a few exceptions, are bugs - they
6096 might contain unintended user-supplied format specifiers.
6097 Since @value{GDBN} uses the @code{format printf} attribute on all
6098 @code{printf} like functions this checks not just @code{printf} calls
6099 but also calls to functions such as @code{fprintf_unfiltered}.
6100
6101 @item -Wno-pointer-sign
6102 In version 4.0, GCC began warning about pointer argument passing or
6103 assignment even when the source and destination differed only in
6104 signedness.  However, most @value{GDBN} code doesn't distinguish
6105 carefully between @code{char} and @code{unsigned char}.  In early 2006
6106 the @value{GDBN} developers decided correcting these warnings wasn't
6107 worth the time it would take.
6108
6109 @item -Wno-unused-parameter
6110 Due to the way that @value{GDBN} is implemented many functions have
6111 unused parameters.  Consequently this warning is avoided.  The macro
6112 @code{ATTRIBUTE_UNUSED} is not used as it leads to false negatives ---
6113 it is not an error to have @code{ATTRIBUTE_UNUSED} on a parameter that
6114 is being used.
6115
6116 @item -Wno-unused
6117 @itemx -Wno-switch
6118 @itemx -Wno-char-subscripts
6119 These are warnings which might be useful for @value{GDBN}, but are
6120 currently too noisy to enable with @samp{-Werror}.
6121
6122 @end table
6123
6124 @subsection Formatting
6125
6126 @cindex source code formatting
6127 The standard GNU recommendations for formatting must be followed
6128 strictly.
6129
6130 A function declaration should not have its name in column zero.  A
6131 function definition should have its name in column zero.
6132
6133 @smallexample
6134 /* Declaration */
6135 static void foo (void);
6136 /* Definition */
6137 void
6138 foo (void)
6139 @{
6140 @}
6141 @end smallexample
6142
6143 @emph{Pragmatics: This simplifies scripting.  Function definitions can
6144 be found using @samp{^function-name}.}
6145
6146 There must be a space between a function or macro name and the opening
6147 parenthesis of its argument list (except for macro definitions, as
6148 required by C).  There must not be a space after an open paren/bracket
6149 or before a close paren/bracket.
6150
6151 While additional whitespace is generally helpful for reading, do not use
6152 more than one blank line to separate blocks, and avoid adding whitespace
6153 after the end of a program line (as of 1/99, some 600 lines had
6154 whitespace after the semicolon).  Excess whitespace causes difficulties
6155 for @code{diff} and @code{patch} utilities.
6156
6157 Pointers are declared using the traditional K&R C style:
6158
6159 @smallexample
6160 void *foo;
6161 @end smallexample
6162
6163 @noindent
6164 and not:
6165
6166 @smallexample
6167 void * foo;
6168 void* foo;
6169 @end smallexample
6170
6171 @subsection Comments
6172
6173 @cindex comment formatting
6174 The standard GNU requirements on comments must be followed strictly.
6175
6176 Block comments must appear in the following form, with no @code{/*}- or
6177 @code{*/}-only lines, and no leading @code{*}:
6178
6179 @smallexample
6180 /* Wait for control to return from inferior to debugger.  If inferior
6181    gets a signal, we may decide to start it up again instead of
6182    returning.  That is why there is a loop in this function.  When
6183    this function actually returns it means the inferior should be left
6184    stopped and @value{GDBN} should read more commands.  */
6185 @end smallexample
6186
6187 (Note that this format is encouraged by Emacs; tabbing for a multi-line
6188 comment works correctly, and @kbd{M-q} fills the block consistently.)
6189
6190 Put a blank line between the block comments preceding function or
6191 variable definitions, and the definition itself.
6192
6193 In general, put function-body comments on lines by themselves, rather
6194 than trying to fit them into the 20 characters left at the end of a
6195 line, since either the comment or the code will inevitably get longer
6196 than will fit, and then somebody will have to move it anyhow.
6197
6198 @subsection C Usage
6199
6200 @cindex C data types
6201 Code must not depend on the sizes of C data types, the format of the
6202 host's floating point numbers, the alignment of anything, or the order
6203 of evaluation of expressions.
6204
6205 @cindex function usage
6206 Use functions freely.  There are only a handful of compute-bound areas
6207 in @value{GDBN} that might be affected by the overhead of a function
6208 call, mainly in symbol reading.  Most of @value{GDBN}'s performance is
6209 limited by the target interface (whether serial line or system call).
6210
6211 However, use functions with moderation.  A thousand one-line functions
6212 are just as hard to understand as a single thousand-line function.
6213
6214 @emph{Macros are bad, M'kay.}
6215 (But if you have to use a macro, make sure that the macro arguments are
6216 protected with parentheses.)
6217
6218 @cindex types
6219
6220 Declarations like @samp{struct foo *} should be used in preference to
6221 declarations like @samp{typedef struct foo @{ @dots{} @} *foo_ptr}.
6222
6223
6224 @subsection Function Prototypes
6225 @cindex function prototypes
6226
6227 Prototypes must be used when both @emph{declaring} and @emph{defining}
6228 a function.  Prototypes for @value{GDBN} functions must include both the
6229 argument type and name, with the name matching that used in the actual
6230 function definition.
6231
6232 All external functions should have a declaration in a header file that
6233 callers include, except for @code{_initialize_*} functions, which must
6234 be external so that @file{init.c} construction works, but shouldn't be
6235 visible to random source files.
6236
6237 Where a source file needs a forward declaration of a static function,
6238 that declaration must appear in a block near the top of the source file.
6239
6240
6241 @subsection Internal Error Recovery
6242
6243 During its execution, @value{GDBN} can encounter two types of errors.
6244 User errors and internal errors.  User errors include not only a user
6245 entering an incorrect command but also problems arising from corrupt
6246 object files and system errors when interacting with the target.
6247 Internal errors include situations where @value{GDBN} has detected, at
6248 run time, a corrupt or erroneous situation.
6249
6250 When reporting an internal error, @value{GDBN} uses
6251 @code{internal_error} and @code{gdb_assert}.
6252
6253 @value{GDBN} must not call @code{abort} or @code{assert}.
6254
6255 @emph{Pragmatics: There is no @code{internal_warning} function.  Either
6256 the code detected a user error, recovered from it and issued a
6257 @code{warning} or the code failed to correctly recover from the user
6258 error and issued an @code{internal_error}.}
6259
6260 @subsection File Names
6261
6262 Any file used when building the core of @value{GDBN} must be in lower
6263 case.  Any file used when building the core of @value{GDBN} must be 8.3
6264 unique.  These requirements apply to both source and generated files.
6265
6266 @emph{Pragmatics: The core of @value{GDBN} must be buildable on many
6267 platforms including DJGPP and MacOS/HFS.  Every time an unfriendly file
6268 is introduced to the build process both @file{Makefile.in} and
6269 @file{configure.in} need to be modified accordingly.  Compare the
6270 convoluted conversion process needed to transform @file{COPYING} into
6271 @file{copying.c} with the conversion needed to transform
6272 @file{version.in} into @file{version.c}.}
6273
6274 Any file non 8.3 compliant file (that is not used when building the core
6275 of @value{GDBN}) must be added to @file{gdb/config/djgpp/fnchange.lst}.
6276
6277 @emph{Pragmatics: This is clearly a compromise.}
6278
6279 When @value{GDBN} has a local version of a system header file (ex
6280 @file{string.h}) the file name based on the POSIX header prefixed with
6281 @file{gdb_} (@file{gdb_string.h}).  These headers should be relatively
6282 independent: they should use only macros defined by @file{configure},
6283 the compiler, or the host; they should include only system headers; they
6284 should refer only to system types.  They may be shared between multiple
6285 programs, e.g.@: @value{GDBN} and @sc{gdbserver}.
6286
6287 For other files @samp{-} is used as the separator.
6288
6289
6290 @subsection Include Files
6291
6292 A @file{.c} file should include @file{defs.h} first.
6293
6294 A @file{.c} file should directly include the @code{.h} file of every
6295 declaration and/or definition it directly refers to.  It cannot rely on
6296 indirect inclusion.
6297
6298 A @file{.h} file should directly include the @code{.h} file of every
6299 declaration and/or definition it directly refers to.  It cannot rely on
6300 indirect inclusion.  Exception: The file @file{defs.h} does not need to
6301 be directly included.
6302
6303 An external declaration should only appear in one include file.
6304
6305 An external declaration should never appear in a @code{.c} file.
6306 Exception: a declaration for the @code{_initialize} function that
6307 pacifies @option{-Wmissing-declaration}.
6308
6309 A @code{typedef} definition should only appear in one include file.
6310
6311 An opaque @code{struct} declaration can appear in multiple @file{.h}
6312 files.  Where possible, a @file{.h} file should use an opaque
6313 @code{struct} declaration instead of an include.
6314
6315 All @file{.h} files should be wrapped in:
6316
6317 @smallexample
6318 #ifndef INCLUDE_FILE_NAME_H
6319 #define INCLUDE_FILE_NAME_H
6320 header body
6321 #endif
6322 @end smallexample
6323
6324
6325 @subsection Clean Design and Portable Implementation
6326
6327 @cindex design
6328 In addition to getting the syntax right, there's the little question of
6329 semantics.  Some things are done in certain ways in @value{GDBN} because long
6330 experience has shown that the more obvious ways caused various kinds of
6331 trouble.
6332
6333 @cindex assumptions about targets
6334 You can't assume the byte order of anything that comes from a target
6335 (including @var{value}s, object files, and instructions).  Such things
6336 must be byte-swapped using @code{SWAP_TARGET_AND_HOST} in
6337 @value{GDBN}, or one of the swap routines defined in @file{bfd.h},
6338 such as @code{bfd_get_32}.
6339
6340 You can't assume that you know what interface is being used to talk to
6341 the target system.  All references to the target must go through the
6342 current @code{target_ops} vector.
6343
6344 You can't assume that the host and target machines are the same machine
6345 (except in the ``native'' support modules).  In particular, you can't
6346 assume that the target machine's header files will be available on the
6347 host machine.  Target code must bring along its own header files --
6348 written from scratch or explicitly donated by their owner, to avoid
6349 copyright problems.
6350
6351 @cindex portability
6352 Insertion of new @code{#ifdef}'s will be frowned upon.  It's much better
6353 to write the code portably than to conditionalize it for various
6354 systems.
6355
6356 @cindex system dependencies
6357 New @code{#ifdef}'s which test for specific compilers or manufacturers
6358 or operating systems are unacceptable.  All @code{#ifdef}'s should test
6359 for features.  The information about which configurations contain which
6360 features should be segregated into the configuration files.  Experience
6361 has proven far too often that a feature unique to one particular system
6362 often creeps into other systems; and that a conditional based on some
6363 predefined macro for your current system will become worthless over
6364 time, as new versions of your system come out that behave differently
6365 with regard to this feature.
6366
6367 Adding code that handles specific architectures, operating systems,
6368 target interfaces, or hosts, is not acceptable in generic code.
6369
6370 @cindex portable file name handling
6371 @cindex file names, portability
6372 One particularly notorious area where system dependencies tend to
6373 creep in is handling of file names.  The mainline @value{GDBN} code
6374 assumes Posix semantics of file names: absolute file names begin with
6375 a forward slash @file{/}, slashes are used to separate leading
6376 directories, case-sensitive file names.  These assumptions are not
6377 necessarily true on non-Posix systems such as MS-Windows.  To avoid
6378 system-dependent code where you need to take apart or construct a file
6379 name, use the following portable macros:
6380
6381 @table @code
6382 @findex HAVE_DOS_BASED_FILE_SYSTEM
6383 @item HAVE_DOS_BASED_FILE_SYSTEM
6384 This preprocessing symbol is defined to a non-zero value on hosts
6385 whose filesystems belong to the MS-DOS/MS-Windows family.  Use this
6386 symbol to write conditional code which should only be compiled for
6387 such hosts.
6388
6389 @findex IS_DIR_SEPARATOR
6390 @item IS_DIR_SEPARATOR (@var{c})
6391 Evaluates to a non-zero value if @var{c} is a directory separator
6392 character.  On Unix and GNU/Linux systems, only a slash @file{/} is
6393 such a character, but on Windows, both @file{/} and @file{\} will
6394 pass.
6395
6396 @findex IS_ABSOLUTE_PATH
6397 @item IS_ABSOLUTE_PATH (@var{file})
6398 Evaluates to a non-zero value if @var{file} is an absolute file name.
6399 For Unix and GNU/Linux hosts, a name which begins with a slash
6400 @file{/} is absolute.  On DOS and Windows, @file{d:/foo} and
6401 @file{x:\bar} are also absolute file names.
6402
6403 @findex FILENAME_CMP
6404 @item FILENAME_CMP (@var{f1}, @var{f2})
6405 Calls a function which compares file names @var{f1} and @var{f2} as
6406 appropriate for the underlying host filesystem.  For Posix systems,
6407 this simply calls @code{strcmp}; on case-insensitive filesystems it
6408 will call @code{strcasecmp} instead.
6409
6410 @findex DIRNAME_SEPARATOR
6411 @item DIRNAME_SEPARATOR
6412 Evaluates to a character which separates directories in
6413 @code{PATH}-style lists, typically held in environment variables.
6414 This character is @samp{:} on Unix, @samp{;} on DOS and Windows.
6415
6416 @findex SLASH_STRING
6417 @item SLASH_STRING
6418 This evaluates to a constant string you should use to produce an
6419 absolute filename from leading directories and the file's basename.
6420 @code{SLASH_STRING} is @code{"/"} on most systems, but might be
6421 @code{"\\"} for some Windows-based ports.
6422 @end table
6423
6424 In addition to using these macros, be sure to use portable library
6425 functions whenever possible.  For example, to extract a directory or a
6426 basename part from a file name, use the @code{dirname} and
6427 @code{basename} library functions (available in @code{libiberty} for
6428 platforms which don't provide them), instead of searching for a slash
6429 with @code{strrchr}.
6430
6431 Another way to generalize @value{GDBN} along a particular interface is with an
6432 attribute struct.  For example, @value{GDBN} has been generalized to handle
6433 multiple kinds of remote interfaces---not by @code{#ifdef}s everywhere, but
6434 by defining the @code{target_ops} structure and having a current target (as
6435 well as a stack of targets below it, for memory references).  Whenever
6436 something needs to be done that depends on which remote interface we are
6437 using, a flag in the current target_ops structure is tested (e.g.,
6438 @code{target_has_stack}), or a function is called through a pointer in the
6439 current target_ops structure.  In this way, when a new remote interface
6440 is added, only one module needs to be touched---the one that actually
6441 implements the new remote interface.  Other examples of
6442 attribute-structs are BFD access to multiple kinds of object file
6443 formats, or @value{GDBN}'s access to multiple source languages.
6444
6445 Please avoid duplicating code.  For example, in @value{GDBN} 3.x all
6446 the code interfacing between @code{ptrace} and the rest of
6447 @value{GDBN} was duplicated in @file{*-dep.c}, and so changing
6448 something was very painful.  In @value{GDBN} 4.x, these have all been
6449 consolidated into @file{infptrace.c}.  @file{infptrace.c} can deal
6450 with variations between systems the same way any system-independent
6451 file would (hooks, @code{#if defined}, etc.), and machines which are
6452 radically different don't need to use @file{infptrace.c} at all.
6453
6454 All debugging code must be controllable using the @samp{set debug
6455 @var{module}} command.  Do not use @code{printf} to print trace
6456 messages.  Use @code{fprintf_unfiltered(gdb_stdlog, ...}.  Do not use
6457 @code{#ifdef DEBUG}.
6458
6459
6460 @node Porting GDB
6461
6462 @chapter Porting @value{GDBN}
6463 @cindex porting to new machines
6464
6465 Most of the work in making @value{GDBN} compile on a new machine is in
6466 specifying the configuration of the machine.  Porting a new
6467 architecture to @value{GDBN} can be broken into a number of steps.
6468
6469 @itemize @bullet
6470
6471 @item
6472 Ensure a @sc{bfd} exists for executables of the target architecture in
6473 the @file{bfd} directory.  If one does not exist, create one by
6474 modifying an existing similar one.
6475
6476 @item
6477 Implement a disassembler for the target architecture in the @file{opcodes}
6478 directory.
6479
6480 @item
6481 Define the target architecture in the @file{gdb} directory
6482 (@pxref{Adding a New Target, , Adding a New Target}).  Add the pattern
6483 for the new target to @file{configure.tgt} with the names of the files
6484 that contain the code.  By convention the target architecture
6485 definition for an architecture @var{arch} is placed in
6486 @file{@var{arch}-tdep.c}.
6487
6488 Within @file{@var{arch}-tdep.c} define the function
6489 @code{_initialize_@var{arch}_tdep} which calls
6490 @code{gdbarch_register} to create the new @code{@w{struct
6491 gdbarch}} for the architecture.
6492
6493 @item
6494 If a new remote target is needed, consider adding a new remote target
6495 by defining a function
6496 @code{_initialize_remote_@var{arch}}.  However if at all possible
6497 use the @value{GDBN} @emph{Remote Serial Protocol} for this and implement
6498 the server side protocol independently with the target.
6499
6500 @item
6501 If desired implement a simulator in the @file{sim} directory.  This
6502 should create the library @file{libsim.a} implementing the interface
6503 in @file{remote-sim.h} (found in the @file{include} directory).
6504
6505 @item
6506 Build and test.  If desired, lobby the @sc{gdb} steering group to
6507 have the new port included in the main distribution!
6508
6509 @item
6510 Add a description of the new architecture to the main @value{GDBN} user
6511 guide (@pxref{Configuration Specific Information, , Configuration
6512 Specific Information, gdb, Debugging with @value{GDBN}}).
6513
6514 @end itemize
6515
6516 @node Versions and Branches
6517 @chapter Versions and Branches
6518
6519 @section Versions
6520
6521 @value{GDBN}'s version is determined by the file
6522 @file{gdb/version.in} and takes one of the following forms:
6523
6524 @table @asis
6525 @item @var{major}.@var{minor}
6526 @itemx @var{major}.@var{minor}.@var{patchlevel}
6527 an official release (e.g., 6.2 or 6.2.1)
6528 @item @var{major}.@var{minor}.@var{patchlevel}.@var{YYYY}@var{MM}@var{DD}
6529 a snapshot taken at @var{YYYY}-@var{MM}-@var{DD}-gmt (e.g.,
6530 6.1.50.20020302, 6.1.90.20020304, or 6.1.0.20020308)
6531 @item @var{major}.@var{minor}.@var{patchlevel}.@var{YYYY}@var{MM}@var{DD}-cvs
6532 a @sc{cvs} check out drawn on @var{YYYY}-@var{MM}-@var{DD} (e.g.,
6533 6.1.50.20020302-cvs, 6.1.90.20020304-cvs, or 6.1.0.20020308-cvs)
6534 @item @var{major}.@var{minor}.@var{patchlevel}.@var{YYYY}@var{MM}@var{DD} (@var{vendor})
6535 a vendor specific release of @value{GDBN}, that while based on@*
6536 @var{major}.@var{minor}.@var{patchlevel}.@var{YYYY}@var{MM}@var{DD},
6537 may include additional changes
6538 @end table
6539
6540 @value{GDBN}'s mainline uses the @var{major} and @var{minor} version
6541 numbers from the most recent release branch, with a @var{patchlevel}
6542 of 50.  At the time each new release branch is created, the mainline's
6543 @var{major} and @var{minor} version numbers are updated.
6544
6545 @value{GDBN}'s release branch is similar.  When the branch is cut, the
6546 @var{patchlevel} is changed from 50 to 90.  As draft releases are
6547 drawn from the branch, the @var{patchlevel} is incremented.  Once the
6548 first release (@var{major}.@var{minor}) has been made, the
6549 @var{patchlevel} is set to 0 and updates have an incremented
6550 @var{patchlevel}.
6551
6552 For snapshots, and @sc{cvs} check outs, it is also possible to
6553 identify the @sc{cvs} origin:
6554
6555 @table @asis
6556 @item @var{major}.@var{minor}.50.@var{YYYY}@var{MM}@var{DD}
6557 drawn from the @sc{head} of mainline @sc{cvs} (e.g., 6.1.50.20020302)
6558 @item @var{major}.@var{minor}.90.@var{YYYY}@var{MM}@var{DD}
6559 @itemx @var{major}.@var{minor}.91.@var{YYYY}@var{MM}@var{DD} @dots{}
6560 drawn from a release branch prior to the release (e.g.,
6561 6.1.90.20020304)
6562 @item @var{major}.@var{minor}.0.@var{YYYY}@var{MM}@var{DD}
6563 @itemx @var{major}.@var{minor}.1.@var{YYYY}@var{MM}@var{DD} @dots{}
6564 drawn from a release branch after the release (e.g., 6.2.0.20020308)
6565 @end table
6566
6567 If the previous @value{GDBN} version is 6.1 and the current version is
6568 6.2, then, substituting 6 for @var{major} and 1 or 2 for @var{minor},
6569 here's an illustration of a typical sequence:
6570
6571 @smallexample
6572      <HEAD>
6573         |
6574 6.1.50.20020302-cvs
6575         |
6576         +--------------------------.
6577         |                    <gdb_6_2-branch>
6578         |                          |
6579 6.2.50.20020303-cvs        6.1.90 (draft #1)
6580         |                          |
6581 6.2.50.20020304-cvs        6.1.90.20020304-cvs
6582         |                          |
6583 6.2.50.20020305-cvs        6.1.91 (draft #2)
6584         |                          |
6585 6.2.50.20020306-cvs        6.1.91.20020306-cvs
6586         |                          |
6587 6.2.50.20020307-cvs        6.2 (release)
6588         |                          |
6589 6.2.50.20020308-cvs        6.2.0.20020308-cvs
6590         |                          |
6591 6.2.50.20020309-cvs        6.2.1 (update)
6592         |                          |
6593 6.2.50.20020310-cvs         <branch closed>
6594         |
6595 6.2.50.20020311-cvs
6596         |
6597         +--------------------------.
6598         |                     <gdb_6_3-branch>
6599         |                          |
6600 6.3.50.20020312-cvs        6.2.90 (draft #1)
6601         |                          |
6602 @end smallexample
6603
6604 @section Release Branches
6605 @cindex Release Branches
6606
6607 @value{GDBN} draws a release series (6.2, 6.2.1, @dots{}) from a
6608 single release branch, and identifies that branch using the @sc{cvs}
6609 branch tags:
6610
6611 @smallexample
6612 gdb_@var{major}_@var{minor}-@var{YYYY}@var{MM}@var{DD}-branchpoint
6613 gdb_@var{major}_@var{minor}-branch
6614 gdb_@var{major}_@var{minor}-@var{YYYY}@var{MM}@var{DD}-release
6615 @end smallexample
6616
6617 @emph{Pragmatics: To help identify the date at which a branch or
6618 release is made, both the branchpoint and release tags include the
6619 date that they are cut (@var{YYYY}@var{MM}@var{DD}) in the tag.  The
6620 branch tag, denoting the head of the branch, does not need this.}
6621
6622 @section Vendor Branches
6623 @cindex vendor branches
6624
6625 To avoid version conflicts, vendors are expected to modify the file
6626 @file{gdb/version.in} to include a vendor unique alphabetic identifier
6627 (an official @value{GDBN} release never uses alphabetic characters in
6628 its version identifier).  E.g., @samp{6.2widgit2}, or @samp{6.2 (Widgit
6629 Inc Patch 2)}.
6630
6631 @section Experimental Branches
6632 @cindex experimental branches
6633
6634 @subsection Guidelines
6635
6636 @value{GDBN} permits the creation of branches, cut from the @sc{cvs}
6637 repository, for experimental development.  Branches make it possible
6638 for developers to share preliminary work, and maintainers to examine
6639 significant new developments.
6640
6641 The following are a set of guidelines for creating such branches:
6642
6643 @table @emph
6644
6645 @item a branch has an owner
6646 The owner can set further policy for a branch, but may not change the
6647 ground rules.  In particular, they can set a policy for commits (be it
6648 adding more reviewers or deciding who can commit).
6649
6650 @item all commits are posted
6651 All changes committed to a branch shall also be posted to
6652 @email{gdb-patches@@sourceware.org, the @value{GDBN} patches
6653 mailing list}.  While commentary on such changes are encouraged, people
6654 should remember that the changes only apply to a branch.
6655
6656 @item all commits are covered by an assignment
6657 This ensures that all changes belong to the Free Software Foundation,
6658 and avoids the possibility that the branch may become contaminated.
6659
6660 @item a branch is focused
6661 A focused branch has a single objective or goal, and does not contain
6662 unnecessary or irrelevant changes.  Cleanups, where identified, being
6663 be pushed into the mainline as soon as possible.
6664
6665 @item a branch tracks mainline
6666 This keeps the level of divergence under control.  It also keeps the
6667 pressure on developers to push cleanups and other stuff into the
6668 mainline.
6669
6670 @item a branch shall contain the entire @value{GDBN} module
6671 The @value{GDBN} module @code{gdb} should be specified when creating a
6672 branch (branches of individual files should be avoided).  @xref{Tags}.
6673
6674 @item a branch shall be branded using @file{version.in}
6675 The file @file{gdb/version.in} shall be modified so that it identifies
6676 the branch @var{owner} and branch @var{name}, e.g.,
6677 @samp{6.2.50.20030303_owner_name} or @samp{6.2 (Owner Name)}.
6678
6679 @end table
6680
6681 @subsection Tags
6682 @anchor{Tags}
6683
6684 To simplify the identification of @value{GDBN} branches, the following
6685 branch tagging convention is strongly recommended:
6686
6687 @table @code
6688
6689 @item @var{owner}_@var{name}-@var{YYYYMMDD}-branchpoint
6690 @itemx @var{owner}_@var{name}-@var{YYYYMMDD}-branch
6691 The branch point and corresponding branch tag.  @var{YYYYMMDD} is the
6692 date that the branch was created.  A branch is created using the
6693 sequence: @anchor{experimental branch tags}
6694 @smallexample
6695 cvs rtag @var{owner}_@var{name}-@var{YYYYMMDD}-branchpoint gdb
6696 cvs rtag -b -r @var{owner}_@var{name}-@var{YYYYMMDD}-branchpoint \
6697    @var{owner}_@var{name}-@var{YYYYMMDD}-branch gdb
6698 @end smallexample
6699
6700 @item @var{owner}_@var{name}-@var{yyyymmdd}-mergepoint
6701 The tagged point, on the mainline, that was used when merging the branch
6702 on @var{yyyymmdd}.  To merge in all changes since the branch was cut,
6703 use a command sequence like:
6704 @smallexample
6705 cvs rtag @var{owner}_@var{name}-@var{yyyymmdd}-mergepoint gdb
6706 cvs update \
6707    -j@var{owner}_@var{name}-@var{YYYYMMDD}-branchpoint
6708    -j@var{owner}_@var{name}-@var{yyyymmdd}-mergepoint
6709 @end smallexample
6710 @noindent
6711 Similar sequences can be used to just merge in changes since the last
6712 merge.
6713
6714 @end table
6715
6716 @noindent
6717 For further information on @sc{cvs}, see
6718 @uref{http://www.gnu.org/software/cvs/, Concurrent Versions System}.
6719  
6720 @node Start of New Year Procedure
6721 @chapter Start of New Year Procedure
6722 @cindex new year procedure
6723
6724 At the start of each new year, the following actions should be performed:
6725
6726 @itemize @bullet
6727 @item
6728 Rotate the ChangeLog file
6729
6730 The current @file{ChangeLog} file should be renamed into
6731 @file{ChangeLog-YYYY} where YYYY is the year that has just passed.
6732 A new @file{ChangeLog} file should be created, and its contents should
6733 contain a reference to the previous ChangeLog.  The following should
6734 also be preserved at the end of the new ChangeLog, in order to provide
6735 the appropriate settings when editing this file with Emacs:
6736 @smallexample
6737 Local Variables:
6738 mode: change-log
6739 left-margin: 8
6740 fill-column: 74
6741 version-control: never
6742 coding: utf-8
6743 End:
6744 @end smallexample
6745
6746 @item
6747 Add an entry for the newly created ChangeLog file (@file{ChangeLog-YYYY})
6748 in @file{gdb/config/djgpp/fnchange.lst}.
6749
6750 @item
6751 Update the copyright year in the startup message
6752
6753 Update the copyright year in:
6754 @itemize @bullet
6755 @item file @file{top.c}, function @code{print_gdb_version}
6756 @item file @file{gdbserver/server.c}, function @code{gdbserver_version}
6757 @item file @file{gdbserver/gdbreplay.c}, function @code{gdbreplay_version}
6758 @end itemize
6759
6760 @item
6761 Add the new year in the copyright notices of all source and documentation
6762 files.  This can be done semi-automatically by running the @code{copyright.sh}
6763 script.  This script requires Emacs 22 or later to be installed.
6764
6765 @end itemize
6766
6767 @node Releasing GDB
6768
6769 @chapter Releasing @value{GDBN}
6770 @cindex making a new release of gdb
6771
6772 @section Branch Commit Policy
6773
6774 The branch commit policy is pretty slack.  @value{GDBN} releases 5.0,
6775 5.1 and 5.2 all used the below:
6776
6777 @itemize @bullet
6778 @item
6779 The @file{gdb/MAINTAINERS} file still holds.
6780 @item
6781 Don't fix something on the branch unless/until it is also fixed in the
6782 trunk.  If this isn't possible, mentioning it in the @file{gdb/PROBLEMS}
6783 file is better than committing a hack.
6784 @item
6785 When considering a patch for the branch, suggested criteria include:
6786 Does it fix a build?  Does it fix the sequence @kbd{break main; run}
6787 when debugging a static binary?
6788 @item
6789 The further a change is from the core of @value{GDBN}, the less likely
6790 the change will worry anyone (e.g., target specific code).
6791 @item
6792 Only post a proposal to change the core of @value{GDBN} after you've
6793 sent individual bribes to all the people listed in the
6794 @file{MAINTAINERS} file @t{;-)}
6795 @end itemize
6796
6797 @emph{Pragmatics: Provided updates are restricted to non-core
6798 functionality there is little chance that a broken change will be fatal.
6799 This means that changes such as adding a new architectures or (within
6800 reason) support for a new host are considered acceptable.}
6801
6802
6803 @section Obsoleting code
6804
6805 Before anything else, poke the other developers (and around the source
6806 code) to see if there is anything that can be removed from @value{GDBN}
6807 (an old target, an unused file).
6808
6809 Obsolete code is identified by adding an @code{OBSOLETE} prefix to every
6810 line.  Doing this means that it is easy to identify something that has
6811 been obsoleted when greping through the sources.
6812
6813 The process is done in stages --- this is mainly to ensure that the
6814 wider @value{GDBN} community has a reasonable opportunity to respond.
6815 Remember, everything on the Internet takes a week.
6816
6817 @enumerate
6818 @item
6819 Post the proposal on @email{gdb@@sourceware.org, the GDB mailing
6820 list} Creating a bug report to track the task's state, is also highly
6821 recommended.
6822 @item
6823 Wait a week or so.
6824 @item
6825 Post the proposal on @email{gdb-announce@@sourceware.org, the GDB
6826 Announcement mailing list}.
6827 @item
6828 Wait a week or so.
6829 @item
6830 Go through and edit all relevant files and lines so that they are
6831 prefixed with the word @code{OBSOLETE}.
6832 @item
6833 Wait until the next GDB version, containing this obsolete code, has been
6834 released.
6835 @item
6836 Remove the obsolete code.
6837 @end enumerate
6838
6839 @noindent
6840 @emph{Maintainer note: While removing old code is regrettable it is
6841 hopefully better for @value{GDBN}'s long term development.  Firstly it
6842 helps the developers by removing code that is either no longer relevant
6843 or simply wrong.  Secondly since it removes any history associated with
6844 the file (effectively clearing the slate) the developer has a much freer
6845 hand when it comes to fixing broken files.}
6846
6847
6848
6849 @section Before the Branch
6850
6851 The most important objective at this stage is to find and fix simple
6852 changes that become a pain to track once the branch is created.  For
6853 instance, configuration problems that stop @value{GDBN} from even
6854 building.  If you can't get the problem fixed, document it in the
6855 @file{gdb/PROBLEMS} file.
6856
6857 @subheading Prompt for @file{gdb/NEWS}
6858
6859 People always forget.  Send a post reminding them but also if you know
6860 something interesting happened add it yourself.  The @code{schedule}
6861 script will mention this in its e-mail.
6862
6863 @subheading Review @file{gdb/README}
6864
6865 Grab one of the nightly snapshots and then walk through the
6866 @file{gdb/README} looking for anything that can be improved.  The
6867 @code{schedule} script will mention this in its e-mail.
6868
6869 @subheading Refresh any imported files.
6870
6871 A number of files are taken from external repositories.  They include:
6872
6873 @itemize @bullet
6874 @item
6875 @file{texinfo/texinfo.tex}
6876 @item
6877 @file{config.guess} et.@: al.@: (see the top-level @file{MAINTAINERS}
6878 file)
6879 @item
6880 @file{etc/standards.texi}, @file{etc/make-stds.texi}
6881 @end itemize
6882
6883 @subheading Check the ARI
6884
6885 @uref{http://sourceware.org/gdb/ari,,A.R.I.} is an @code{awk} script
6886 (Awk Regression Index ;-) that checks for a number of errors and coding
6887 conventions.  The checks include things like using @code{malloc} instead
6888 of @code{xmalloc} and file naming problems.  There shouldn't be any
6889 regressions.
6890
6891 @subsection Review the bug data base
6892
6893 Close anything obviously fixed.
6894
6895 @subsection Check all cross targets build
6896
6897 The targets are listed in @file{gdb/MAINTAINERS}.
6898
6899
6900 @section Cut the Branch
6901
6902 @subheading Create the branch
6903
6904 @smallexample
6905 $  u=5.1
6906 $  v=5.2
6907 $  V=`echo $v | sed 's/\./_/g'`
6908 $  D=`date -u +%Y-%m-%d`
6909 $  echo $u $V $D
6910 5.1 5_2 2002-03-03
6911 $  echo cvs -f -d :ext:sourceware.org:/cvs/src rtag \
6912 -D $D-gmt gdb_$V-$D-branchpoint insight
6913 cvs -f -d :ext:sourceware.org:/cvs/src rtag
6914 -D 2002-03-03-gmt gdb_5_2-2002-03-03-branchpoint insight
6915 $  ^echo ^^
6916 ...
6917 $  echo cvs -f -d :ext:sourceware.org:/cvs/src rtag \
6918 -b -r gdb_$V-$D-branchpoint gdb_$V-branch insight
6919 cvs -f -d :ext:sourceware.org:/cvs/src rtag \
6920 -b -r gdb_5_2-2002-03-03-branchpoint gdb_5_2-branch insight
6921 $  ^echo ^^
6922 ...
6923 $
6924 @end smallexample
6925
6926 @itemize @bullet
6927 @item
6928 By using @kbd{-D YYYY-MM-DD-gmt}, the branch is forced to an exact
6929 date/time.
6930 @item
6931 The trunk is first tagged so that the branch point can easily be found.
6932 @item
6933 Insight, which includes @value{GDBN}, is tagged at the same time.
6934 @item
6935 @file{version.in} gets bumped to avoid version number conflicts.
6936 @item
6937 The reading of @file{.cvsrc} is disabled using @file{-f}.
6938 @end itemize
6939
6940 @subheading Update @file{version.in}
6941
6942 @smallexample
6943 $  u=5.1
6944 $  v=5.2
6945 $  V=`echo $v | sed 's/\./_/g'`
6946 $  echo $u $v$V
6947 5.1 5_2
6948 $  cd /tmp
6949 $  echo cvs -f -d :ext:sourceware.org:/cvs/src co \
6950 -r gdb_$V-branch src/gdb/version.in
6951 cvs -f -d :ext:sourceware.org:/cvs/src co
6952  -r gdb_5_2-branch src/gdb/version.in
6953 $  ^echo ^^
6954 U src/gdb/version.in
6955 $  cd src/gdb
6956 $  echo $u.90-0000-00-00-cvs > version.in
6957 $  cat version.in
6958 5.1.90-0000-00-00-cvs
6959 $  cvs -f commit version.in
6960 @end smallexample
6961
6962 @itemize @bullet
6963 @item
6964 @file{0000-00-00} is used as a date to pump prime the version.in update
6965 mechanism.
6966 @item
6967 @file{.90} and the previous branch version are used as fairly arbitrary
6968 initial branch version number.
6969 @end itemize
6970
6971
6972 @subheading Update the web and news pages
6973
6974 Something?
6975
6976 @subheading Tweak cron to track the new branch
6977
6978 The file @file{gdbadmin/cron/crontab} contains gdbadmin's cron table.
6979 This file needs to be updated so that:
6980
6981 @itemize @bullet
6982 @item
6983 A daily timestamp is added to the file @file{version.in}.
6984 @item
6985 The new branch is included in the snapshot process.
6986 @end itemize
6987
6988 @noindent
6989 See the file @file{gdbadmin/cron/README} for how to install the updated
6990 cron table.
6991
6992 The file @file{gdbadmin/ss/README} should also be reviewed to reflect
6993 any changes.  That file is copied to both the branch/ and current/
6994 snapshot directories.
6995
6996
6997 @subheading Update the NEWS and README files
6998
6999 The @file{NEWS} file needs to be updated so that on the branch it refers
7000 to @emph{changes in the current release} while on the trunk it also
7001 refers to @emph{changes since the current release}.
7002
7003 The @file{README} file needs to be updated so that it refers to the
7004 current release.
7005
7006 @subheading Post the branch info
7007
7008 Send an announcement to the mailing lists:
7009
7010 @itemize @bullet
7011 @item
7012 @email{gdb-announce@@sourceware.org, GDB Announcement mailing list}
7013 @item
7014 @email{gdb@@sourceware.org, GDB Discussion mailing list} and
7015 @email{gdb-testers@@sourceware.org, GDB Testers mailing list}
7016 @end itemize
7017
7018 @emph{Pragmatics: The branch creation is sent to the announce list to
7019 ensure that people people not subscribed to the higher volume discussion
7020 list are alerted.}
7021
7022 The announcement should include:
7023
7024 @itemize @bullet
7025 @item
7026 The branch tag.
7027 @item
7028 How to check out the branch using CVS.
7029 @item
7030 The date/number of weeks until the release.
7031 @item
7032 The branch commit policy still holds.
7033 @end itemize
7034
7035 @section Stabilize the branch
7036
7037 Something goes here.
7038
7039 @section Create a Release
7040
7041 The process of creating and then making available a release is broken
7042 down into a number of stages.  The first part addresses the technical
7043 process of creating a releasable tar ball.  The later stages address the
7044 process of releasing that tar ball.
7045
7046 When making a release candidate just the first section is needed.
7047
7048 @subsection Create a release candidate
7049
7050 The objective at this stage is to create a set of tar balls that can be
7051 made available as a formal release (or as a less formal release
7052 candidate).
7053
7054 @subsubheading Freeze the branch
7055
7056 Send out an e-mail notifying everyone that the branch is frozen to
7057 @email{gdb-patches@@sourceware.org}.
7058
7059 @subsubheading Establish a few defaults.
7060
7061 @smallexample
7062 $  b=gdb_5_2-branch
7063 $  v=5.2
7064 $  t=/sourceware/snapshot-tmp/gdbadmin-tmp
7065 $  echo $t/$b/$v
7066 /sourceware/snapshot-tmp/gdbadmin-tmp/gdb_5_2-branch/5.2
7067 $  mkdir -p $t/$b/$v
7068 $  cd $t/$b/$v
7069 $  pwd
7070 /sourceware/snapshot-tmp/gdbadmin-tmp/gdb_5_2-branch/5.2
7071 $  which autoconf
7072 /home/gdbadmin/bin/autoconf
7073 $
7074 @end smallexample
7075
7076 @noindent
7077 Notes:
7078
7079 @itemize @bullet
7080 @item
7081 Check the @code{autoconf} version carefully.  You want to be using the
7082 version documented in the toplevel @file{README-maintainer-mode} file.
7083 It is very unlikely that the version of @code{autoconf} installed in
7084 system directories (e.g., @file{/usr/bin/autoconf}) is correct.
7085 @end itemize
7086
7087 @subsubheading Check out the relevant modules:
7088
7089 @smallexample
7090 $  for m in gdb insight
7091 do
7092 ( mkdir -p $m && cd $m && cvs -q -f -d /cvs/src co -P -r $b $m )
7093 done
7094 $
7095 @end smallexample
7096
7097 @noindent
7098 Note:
7099
7100 @itemize @bullet
7101 @item
7102 The reading of @file{.cvsrc} is disabled (@file{-f}) so that there isn't
7103 any confusion between what is written here and what your local
7104 @code{cvs} really does.
7105 @end itemize
7106
7107 @subsubheading Update relevant files.
7108
7109 @table @file
7110
7111 @item gdb/NEWS
7112
7113 Major releases get their comments added as part of the mainline.  Minor
7114 releases should probably mention any significant bugs that were fixed.
7115
7116 Don't forget to include the @file{ChangeLog} entry.
7117
7118 @smallexample
7119 $  emacs gdb/src/gdb/NEWS
7120 ...
7121 c-x 4 a
7122 ...
7123 c-x c-s c-x c-c
7124 $  cp gdb/src/gdb/NEWS insight/src/gdb/NEWS 
7125 $  cp gdb/src/gdb/ChangeLog insight/src/gdb/ChangeLog 
7126 @end smallexample
7127
7128 @item gdb/README
7129
7130 You'll need to update:
7131
7132 @itemize @bullet
7133 @item
7134 The version.
7135 @item
7136 The update date.
7137 @item
7138 Who did it.
7139 @end itemize
7140
7141 @smallexample
7142 $  emacs gdb/src/gdb/README
7143 ...
7144 c-x 4 a
7145 ...
7146 c-x c-s c-x c-c
7147 $  cp gdb/src/gdb/README insight/src/gdb/README 
7148 $  cp gdb/src/gdb/ChangeLog insight/src/gdb/ChangeLog 
7149 @end smallexample
7150
7151 @emph{Maintainer note: Hopefully the @file{README} file was reviewed
7152 before the initial branch was cut so just a simple substitute is needed
7153 to get it updated.}
7154
7155 @emph{Maintainer note: Other projects generate @file{README} and
7156 @file{INSTALL} from the core documentation.  This might be worth
7157 pursuing.}
7158
7159 @item gdb/version.in
7160
7161 @smallexample
7162 $  echo $v > gdb/src/gdb/version.in
7163 $  cat gdb/src/gdb/version.in
7164 5.2
7165 $  emacs gdb/src/gdb/version.in
7166 ...
7167 c-x 4 a
7168 ... Bump to version ...
7169 c-x c-s c-x c-c
7170 $  cp gdb/src/gdb/version.in insight/src/gdb/version.in 
7171 $  cp gdb/src/gdb/ChangeLog insight/src/gdb/ChangeLog 
7172 @end smallexample
7173
7174 @end table
7175
7176 @subsubheading Do the dirty work
7177
7178 This is identical to the process used to create the daily snapshot.
7179
7180 @smallexample
7181 $  for m in gdb insight
7182 do
7183 ( cd $m/src && gmake -f src-release $m.tar )
7184 done
7185 @end smallexample
7186
7187 If the top level source directory does not have @file{src-release}
7188 (@value{GDBN} version 5.3.1 or earlier), try these commands instead:
7189
7190 @smallexample
7191 $  for m in gdb insight
7192 do
7193 ( cd $m/src && gmake -f Makefile.in $m.tar )
7194 done
7195 @end smallexample
7196
7197 @subsubheading Check the source files
7198
7199 You're looking for files that have mysteriously disappeared.
7200 @kbd{distclean} has the habit of deleting files it shouldn't.  Watch out
7201 for the @file{version.in} update @kbd{cronjob}.
7202
7203 @smallexample
7204 $  ( cd gdb/src && cvs -f -q -n update )
7205 M djunpack.bat
7206 ? gdb-5.1.91.tar
7207 ? proto-toplev
7208 @dots{} lots of generated files @dots{}
7209 M gdb/ChangeLog
7210 M gdb/NEWS
7211 M gdb/README
7212 M gdb/version.in
7213 @dots{} lots of generated files @dots{}
7214 $
7215 @end smallexample
7216
7217 @noindent
7218 @emph{Don't worry about the @file{gdb.info-??} or
7219 @file{gdb/p-exp.tab.c}.  They were generated (and yes @file{gdb.info-1}
7220 was also generated only something strange with CVS means that they
7221 didn't get suppressed).  Fixing it would be nice though.}
7222
7223 @subsubheading Create compressed versions of the release
7224
7225 @smallexample
7226 $  cp */src/*.tar .
7227 $  cp */src/*.bz2 .
7228 $  ls -F
7229 gdb/ gdb-5.2.tar insight/ insight-5.2.tar
7230 $  for m in gdb insight
7231 do
7232 bzip2 -v -9 -c $m-$v.tar > $m-$v.tar.bz2
7233 gzip -v -9 -c $m-$v.tar > $m-$v.tar.gz
7234 done
7235 $
7236 @end smallexample
7237
7238 @noindent
7239 Note:
7240
7241 @itemize @bullet
7242 @item
7243 A pipe such as @kbd{bunzip2 < xxx.bz2 | gzip -9 > xxx.gz} is not since,
7244 in that mode, @code{gzip} does not know the name of the file and, hence,
7245 can not include it in the compressed file.  This is also why the release
7246 process runs @code{tar} and @code{bzip2} as separate passes.
7247 @end itemize
7248
7249 @subsection Sanity check the tar ball
7250
7251 Pick a popular machine (Solaris/PPC?) and try the build on that.
7252
7253 @smallexample
7254 $  bunzip2 < gdb-5.2.tar.bz2 | tar xpf -
7255 $  cd gdb-5.2
7256 $  ./configure 
7257 $  make
7258 @dots{}
7259 $  ./gdb/gdb ./gdb/gdb
7260 GNU gdb 5.2
7261 @dots{}
7262 (gdb)  b main
7263 Breakpoint 1 at 0x80732bc: file main.c, line 734.
7264 (gdb)  run
7265 Starting program: /tmp/gdb-5.2/gdb/gdb 
7266
7267 Breakpoint 1, main (argc=1, argv=0xbffff8b4) at main.c:734
7268 734       catch_errors (captured_main, &args, "", RETURN_MASK_ALL);
7269 (gdb)  print args
7270 $1 = @{argc = 136426532, argv = 0x821b7f0@}
7271 (gdb)
7272 @end smallexample
7273
7274 @subsection Make a release candidate available
7275
7276 If this is a release candidate then the only remaining steps are:
7277
7278 @enumerate
7279 @item
7280 Commit @file{version.in} and @file{ChangeLog}
7281 @item
7282 Tweak @file{version.in} (and @file{ChangeLog} to read
7283 @var{L}.@var{M}.@var{N}-0000-00-00-cvs so that the version update
7284 process can restart.
7285 @item
7286 Make the release candidate available in
7287 @uref{ftp://sourceware.org/pub/gdb/snapshots/branch}
7288 @item
7289 Notify the relevant mailing lists ( @email{gdb@@sourceware.org} and
7290 @email{gdb-testers@@sourceware.org} that the candidate is available.
7291 @end enumerate
7292
7293 @subsection Make a formal release available
7294
7295 (And you thought all that was required was to post an e-mail.)
7296
7297 @subsubheading Install on sware
7298
7299 Copy the new files to both the release and the old release directory:
7300
7301 @smallexample
7302 $  cp *.bz2 *.gz ~ftp/pub/gdb/old-releases/
7303 $  cp *.bz2 *.gz ~ftp/pub/gdb/releases
7304 @end smallexample
7305
7306 @noindent
7307 Clean up the releases directory so that only the most recent releases
7308 are available (e.g.@: keep 5.2 and 5.2.1 but remove 5.1):
7309
7310 @smallexample
7311 $  cd ~ftp/pub/gdb/releases
7312 $  rm @dots{}
7313 @end smallexample
7314
7315 @noindent
7316 Update the file @file{README} and @file{.message} in the releases
7317 directory:
7318
7319 @smallexample
7320 $  vi README
7321 @dots{}
7322 $  rm -f .message
7323 $  ln README .message
7324 @end smallexample
7325
7326 @subsubheading Update the web pages.
7327
7328 @table @file
7329
7330 @item htdocs/download/ANNOUNCEMENT
7331 This file, which is posted as the official announcement, includes:
7332 @itemize @bullet
7333 @item
7334 General announcement.
7335 @item
7336 News.  If making an @var{M}.@var{N}.1 release, retain the news from
7337 earlier @var{M}.@var{N} release.
7338 @item
7339 Errata.
7340 @end itemize
7341
7342 @item htdocs/index.html
7343 @itemx htdocs/news/index.html
7344 @itemx htdocs/download/index.html
7345 These files include:
7346 @itemize @bullet
7347 @item
7348 Announcement of the most recent release.
7349 @item
7350 News entry (remember to update both the top level and the news directory).
7351 @end itemize
7352 These pages also need to be regenerate using @code{index.sh}.
7353
7354 @item download/onlinedocs/
7355 You need to find the magic command that is used to generate the online
7356 docs from the @file{.tar.bz2}.  The best way is to look in the output
7357 from one of the nightly @code{cron} jobs and then just edit accordingly.
7358 Something like:
7359
7360 @smallexample
7361 $  ~/ss/update-web-docs \
7362  ~ftp/pub/gdb/releases/gdb-5.2.tar.bz2 \
7363  $PWD/www \
7364  /www/sourceware/htdocs/gdb/download/onlinedocs \
7365  gdb
7366 @end smallexample
7367
7368 @item download/ari/
7369 Just like the online documentation.  Something like:
7370
7371 @smallexample
7372 $  /bin/sh ~/ss/update-web-ari \
7373  ~ftp/pub/gdb/releases/gdb-5.2.tar.bz2 \
7374  $PWD/www \
7375  /www/sourceware/htdocs/gdb/download/ari \
7376  gdb
7377 @end smallexample
7378
7379 @end table
7380
7381 @subsubheading Shadow the pages onto gnu
7382
7383 Something goes here.
7384
7385
7386 @subsubheading Install the @value{GDBN} tar ball on GNU
7387
7388 At the time of writing, the GNU machine was @kbd{gnudist.gnu.org} in
7389 @file{~ftp/gnu/gdb}.
7390
7391 @subsubheading Make the @file{ANNOUNCEMENT}
7392
7393 Post the @file{ANNOUNCEMENT} file you created above to:
7394
7395 @itemize @bullet
7396 @item
7397 @email{gdb-announce@@sourceware.org, GDB Announcement mailing list}
7398 @item
7399 @email{info-gnu@@gnu.org, General GNU Announcement list} (but delay it a
7400 day or so to let things get out)
7401 @item
7402 @email{bug-gdb@@gnu.org, GDB Bug Report mailing list}
7403 @end itemize
7404
7405 @subsection Cleanup
7406
7407 The release is out but you're still not finished.
7408
7409 @subsubheading Commit outstanding changes
7410
7411 In particular you'll need to commit any changes to:
7412
7413 @itemize @bullet
7414 @item
7415 @file{gdb/ChangeLog}
7416 @item
7417 @file{gdb/version.in}
7418 @item
7419 @file{gdb/NEWS}
7420 @item
7421 @file{gdb/README}
7422 @end itemize
7423
7424 @subsubheading Tag the release
7425
7426 Something like:
7427
7428 @smallexample
7429 $  d=`date -u +%Y-%m-%d`
7430 $  echo $d
7431 2002-01-24
7432 $  ( cd insight/src/gdb && cvs -f -q update )
7433 $  ( cd insight/src && cvs -f -q tag gdb_5_2-$d-release )
7434 @end smallexample
7435
7436 Insight is used since that contains more of the release than
7437 @value{GDBN}.
7438
7439 @subsubheading Mention the release on the trunk
7440
7441 Just put something in the @file{ChangeLog} so that the trunk also
7442 indicates when the release was made.
7443
7444 @subsubheading Restart @file{gdb/version.in}
7445
7446 If @file{gdb/version.in} does not contain an ISO date such as
7447 @kbd{2002-01-24} then the daily @code{cronjob} won't update it.  Having
7448 committed all the release changes it can be set to
7449 @file{5.2.0_0000-00-00-cvs} which will restart things (yes the @kbd{_}
7450 is important - it affects the snapshot process).
7451
7452 Don't forget the @file{ChangeLog}.
7453
7454 @subsubheading Merge into trunk
7455
7456 The files committed to the branch may also need changes merged into the
7457 trunk.
7458
7459 @subsubheading Revise the release schedule
7460
7461 Post a revised release schedule to @email{gdb@@sourceware.org, GDB
7462 Discussion List} with an updated announcement.  The schedule can be
7463 generated by running:
7464
7465 @smallexample
7466 $  ~/ss/schedule `date +%s` schedule
7467 @end smallexample
7468
7469 @noindent
7470 The first parameter is approximate date/time in seconds (from the epoch)
7471 of the most recent release.
7472
7473 Also update the schedule @code{cronjob}.
7474
7475 @section Post release
7476
7477 Remove any @code{OBSOLETE} code.
7478
7479 @node Testsuite
7480
7481 @chapter Testsuite
7482 @cindex test suite
7483
7484 The testsuite is an important component of the @value{GDBN} package.
7485 While it is always worthwhile to encourage user testing, in practice
7486 this is rarely sufficient; users typically use only a small subset of
7487 the available commands, and it has proven all too common for a change
7488 to cause a significant regression that went unnoticed for some time.
7489
7490 The @value{GDBN} testsuite uses the DejaGNU testing framework.  The
7491 tests themselves are calls to various @code{Tcl} procs; the framework
7492 runs all the procs and summarizes the passes and fails.
7493
7494 @section Using the Testsuite
7495
7496 @cindex running the test suite
7497 To run the testsuite, simply go to the @value{GDBN} object directory (or to the
7498 testsuite's objdir) and type @code{make check}.  This just sets up some
7499 environment variables and invokes DejaGNU's @code{runtest} script.  While
7500 the testsuite is running, you'll get mentions of which test file is in use,
7501 and a mention of any unexpected passes or fails.  When the testsuite is
7502 finished, you'll get a summary that looks like this:
7503
7504 @smallexample
7505                 === gdb Summary ===
7506
7507 # of expected passes            6016
7508 # of unexpected failures        58
7509 # of unexpected successes       5
7510 # of expected failures          183
7511 # of unresolved testcases       3
7512 # of untested testcases         5
7513 @end smallexample
7514
7515 To run a specific test script, type:
7516 @example
7517 make check RUNTESTFLAGS='@var{tests}'
7518 @end example
7519 where @var{tests} is a list of test script file names, separated by
7520 spaces.
7521
7522 If you use GNU make, you can use its @option{-j} option to run the
7523 testsuite in parallel.  This can greatly reduce the amount of time it
7524 takes for the testsuite to run.  In this case, if you set
7525 @code{RUNTESTFLAGS} then, by default, the tests will be run serially
7526 even under @option{-j}.  You can override this and force a parallel run
7527 by setting the @code{make} variable @code{FORCE_PARALLEL} to any
7528 non-empty value.  Note that the parallel @kbd{make check} assumes
7529 that you want to run the entire testsuite, so it is not compatible
7530 with some dejagnu options, like @option{--directory}.
7531
7532 The ideal test run consists of expected passes only; however, reality
7533 conspires to keep us from this ideal.  Unexpected failures indicate
7534 real problems, whether in @value{GDBN} or in the testsuite.  Expected
7535 failures are still failures, but ones which have been decided are too
7536 hard to deal with at the time; for instance, a test case might work
7537 everywhere except on AIX, and there is no prospect of the AIX case
7538 being fixed in the near future.  Expected failures should not be added
7539 lightly, since you may be masking serious bugs in @value{GDBN}.
7540 Unexpected successes are expected fails that are passing for some
7541 reason, while unresolved and untested cases often indicate some minor
7542 catastrophe, such as the compiler being unable to deal with a test
7543 program.
7544
7545 When making any significant change to @value{GDBN}, you should run the
7546 testsuite before and after the change, to confirm that there are no
7547 regressions.  Note that truly complete testing would require that you
7548 run the testsuite with all supported configurations and a variety of
7549 compilers; however this is more than really necessary.  In many cases
7550 testing with a single configuration is sufficient.  Other useful
7551 options are to test one big-endian (Sparc) and one little-endian (x86)
7552 host, a cross config with a builtin simulator (powerpc-eabi,
7553 mips-elf), or a 64-bit host (Alpha).
7554
7555 If you add new functionality to @value{GDBN}, please consider adding
7556 tests for it as well; this way future @value{GDBN} hackers can detect
7557 and fix their changes that break the functionality you added.
7558 Similarly, if you fix a bug that was not previously reported as a test
7559 failure, please add a test case for it.  Some cases are extremely
7560 difficult to test, such as code that handles host OS failures or bugs
7561 in particular versions of compilers, and it's OK not to try to write
7562 tests for all of those.
7563
7564 DejaGNU supports separate build, host, and target machines.  However,
7565 some @value{GDBN} test scripts do not work if the build machine and
7566 the host machine are not the same.  In such an environment, these scripts
7567 will give a result of ``UNRESOLVED'', like this:
7568
7569 @smallexample
7570 UNRESOLVED: gdb.base/example.exp: This test script does not work on a remote host.
7571 @end smallexample
7572
7573 Sometimes it is convenient to get a transcript of the commands which
7574 the testsuite sends to @value{GDBN}.  For example, if @value{GDBN}
7575 crashes during testing, a transcript can be used to more easily
7576 reconstruct the failure when running @value{GDBN} under @value{GDBN}.
7577
7578 You can instruct the @value{GDBN} testsuite to write transcripts by
7579 setting the DejaGNU variable @code{TRANSCRIPT} (to any value)
7580 before invoking @code{runtest} or @kbd{make check}.  The transcripts
7581 will be written into DejaGNU's output directory.  One transcript will
7582 be made for each invocation of @value{GDBN}; they will be named
7583 @file{transcript.@var{n}}, where @var{n} is an integer.  The first
7584 line of the transcript file will show how @value{GDBN} was invoked;
7585 each subsequent line is a command sent as input to @value{GDBN}.
7586
7587 @smallexample
7588 make check RUNTESTFLAGS=TRANSCRIPT=y
7589 @end smallexample
7590
7591 Note that the transcript is not always complete.  In particular, tests
7592 of completion can yield partial command lines.
7593
7594 @section Testsuite Organization
7595
7596 @cindex test suite organization
7597 The testsuite is entirely contained in @file{gdb/testsuite}.  While the
7598 testsuite includes some makefiles and configury, these are very minimal,
7599 and used for little besides cleaning up, since the tests themselves
7600 handle the compilation of the programs that @value{GDBN} will run.  The file
7601 @file{testsuite/lib/gdb.exp} contains common utility procs useful for
7602 all @value{GDBN} tests, while the directory @file{testsuite/config} contains
7603 configuration-specific files, typically used for special-purpose
7604 definitions of procs like @code{gdb_load} and @code{gdb_start}.
7605
7606 The tests themselves are to be found in @file{testsuite/gdb.*} and
7607 subdirectories of those.  The names of the test files must always end
7608 with @file{.exp}.  DejaGNU collects the test files by wildcarding
7609 in the test directories, so both subdirectories and individual files
7610 get chosen and run in alphabetical order.
7611
7612 The following table lists the main types of subdirectories and what they
7613 are for.  Since DejaGNU finds test files no matter where they are
7614 located, and since each test file sets up its own compilation and
7615 execution environment, this organization is simply for convenience and
7616 intelligibility.
7617
7618 @table @file
7619 @item gdb.base
7620 This is the base testsuite.  The tests in it should apply to all
7621 configurations of @value{GDBN} (but generic native-only tests may live here).
7622 The test programs should be in the subset of C that is valid K&R,
7623 ANSI/ISO, and C@t{++} (@code{#ifdef}s are allowed if necessary, for instance
7624 for prototypes).
7625
7626 @item gdb.@var{lang}
7627 Language-specific tests for any language @var{lang} besides C.  Examples are
7628 @file{gdb.cp} and @file{gdb.java}.
7629
7630 @item gdb.@var{platform}
7631 Non-portable tests.  The tests are specific to a specific configuration
7632 (host or target), such as HP-UX or eCos.  Example is @file{gdb.hp}, for
7633 HP-UX.
7634
7635 @item gdb.@var{compiler}
7636 Tests specific to a particular compiler.  As of this writing (June
7637 1999), there aren't currently any groups of tests in this category that
7638 couldn't just as sensibly be made platform-specific, but one could
7639 imagine a @file{gdb.gcc}, for tests of @value{GDBN}'s handling of GCC
7640 extensions.
7641
7642 @item gdb.@var{subsystem}
7643 Tests that exercise a specific @value{GDBN} subsystem in more depth.  For
7644 instance, @file{gdb.disasm} exercises various disassemblers, while
7645 @file{gdb.stabs} tests pathways through the stabs symbol reader.
7646 @end table
7647
7648 @section Writing Tests
7649 @cindex writing tests
7650
7651 In many areas, the @value{GDBN} tests are already quite comprehensive; you
7652 should be able to copy existing tests to handle new cases.
7653
7654 You should try to use @code{gdb_test} whenever possible, since it
7655 includes cases to handle all the unexpected errors that might happen.
7656 However, it doesn't cost anything to add new test procedures; for
7657 instance, @file{gdb.base/exprs.exp} defines a @code{test_expr} that
7658 calls @code{gdb_test} multiple times.
7659
7660 Only use @code{send_gdb} and @code{gdb_expect} when absolutely
7661 necessary.  Even if @value{GDBN} has several valid responses to
7662 a command, you can use @code{gdb_test_multiple}.  Like @code{gdb_test},
7663 @code{gdb_test_multiple} recognizes internal errors and unexpected
7664 prompts.
7665
7666 Do not write tests which expect a literal tab character from @value{GDBN}.
7667 On some operating systems (e.g.@: OpenBSD) the TTY layer expands tabs to
7668 spaces, so by the time @value{GDBN}'s output reaches expect the tab is gone.
7669
7670 The source language programs do @emph{not} need to be in a consistent
7671 style.  Since @value{GDBN} is used to debug programs written in many different
7672 styles, it's worth having a mix of styles in the testsuite; for
7673 instance, some @value{GDBN} bugs involving the display of source lines would
7674 never manifest themselves if the programs used GNU coding style
7675 uniformly.
7676
7677 @node Hints
7678
7679 @chapter Hints
7680
7681 Check the @file{README} file, it often has useful information that does not
7682 appear anywhere else in the directory.
7683
7684 @menu
7685 * Getting Started::             Getting started working on @value{GDBN}
7686 * Debugging GDB::               Debugging @value{GDBN} with itself
7687 @end menu
7688
7689 @node Getting Started,,, Hints
7690
7691 @section Getting Started
7692
7693 @value{GDBN} is a large and complicated program, and if you first starting to
7694 work on it, it can be hard to know where to start.  Fortunately, if you
7695 know how to go about it, there are ways to figure out what is going on.
7696
7697 This manual, the @value{GDBN} Internals manual, has information which applies
7698 generally to many parts of @value{GDBN}.
7699
7700 Information about particular functions or data structures are located in
7701 comments with those functions or data structures.  If you run across a
7702 function or a global variable which does not have a comment correctly
7703 explaining what is does, this can be thought of as a bug in @value{GDBN}; feel
7704 free to submit a bug report, with a suggested comment if you can figure
7705 out what the comment should say.  If you find a comment which is
7706 actually wrong, be especially sure to report that.
7707
7708 Comments explaining the function of macros defined in host, target, or
7709 native dependent files can be in several places.  Sometimes they are
7710 repeated every place the macro is defined.  Sometimes they are where the
7711 macro is used.  Sometimes there is a header file which supplies a
7712 default definition of the macro, and the comment is there.  This manual
7713 also documents all the available macros.
7714 @c (@pxref{Host Conditionals}, @pxref{Target
7715 @c Conditionals}, @pxref{Native Conditionals}, and @pxref{Obsolete
7716 @c Conditionals})
7717
7718 Start with the header files.  Once you have some idea of how
7719 @value{GDBN}'s internal symbol tables are stored (see @file{symtab.h},
7720 @file{gdbtypes.h}), you will find it much easier to understand the
7721 code which uses and creates those symbol tables.
7722
7723 You may wish to process the information you are getting somehow, to
7724 enhance your understanding of it.  Summarize it, translate it to another
7725 language, add some (perhaps trivial or non-useful) feature to @value{GDBN}, use
7726 the code to predict what a test case would do and write the test case
7727 and verify your prediction, etc.  If you are reading code and your eyes
7728 are starting to glaze over, this is a sign you need to use a more active
7729 approach.
7730
7731 Once you have a part of @value{GDBN} to start with, you can find more
7732 specifically the part you are looking for by stepping through each
7733 function with the @code{next} command.  Do not use @code{step} or you
7734 will quickly get distracted; when the function you are stepping through
7735 calls another function try only to get a big-picture understanding
7736 (perhaps using the comment at the beginning of the function being
7737 called) of what it does.  This way you can identify which of the
7738 functions being called by the function you are stepping through is the
7739 one which you are interested in.  You may need to examine the data
7740 structures generated at each stage, with reference to the comments in
7741 the header files explaining what the data structures are supposed to
7742 look like.
7743
7744 Of course, this same technique can be used if you are just reading the
7745 code, rather than actually stepping through it.  The same general
7746 principle applies---when the code you are looking at calls something
7747 else, just try to understand generally what the code being called does,
7748 rather than worrying about all its details.
7749
7750 @cindex command implementation
7751 A good place to start when tracking down some particular area is with
7752 a command which invokes that feature.  Suppose you want to know how
7753 single-stepping works.  As a @value{GDBN} user, you know that the
7754 @code{step} command invokes single-stepping.  The command is invoked
7755 via command tables (see @file{command.h}); by convention the function
7756 which actually performs the command is formed by taking the name of
7757 the command and adding @samp{_command}, or in the case of an
7758 @code{info} subcommand, @samp{_info}.  For example, the @code{step}
7759 command invokes the @code{step_command} function and the @code{info
7760 display} command invokes @code{display_info}.  When this convention is
7761 not followed, you might have to use @code{grep} or @kbd{M-x
7762 tags-search} in emacs, or run @value{GDBN} on itself and set a
7763 breakpoint in @code{execute_command}.
7764
7765 @cindex @code{bug-gdb} mailing list
7766 If all of the above fail, it may be appropriate to ask for information
7767 on @code{bug-gdb}.  But @emph{never} post a generic question like ``I was
7768 wondering if anyone could give me some tips about understanding
7769 @value{GDBN}''---if we had some magic secret we would put it in this manual.
7770 Suggestions for improving the manual are always welcome, of course.
7771
7772 @node Debugging GDB,,,Hints
7773
7774 @section Debugging @value{GDBN} with itself
7775 @cindex debugging @value{GDBN}
7776
7777 If @value{GDBN} is limping on your machine, this is the preferred way to get it
7778 fully functional.  Be warned that in some ancient Unix systems, like
7779 Ultrix 4.2, a program can't be running in one process while it is being
7780 debugged in another.  Rather than typing the command @kbd{@w{./gdb
7781 ./gdb}}, which works on Suns and such, you can copy @file{gdb} to
7782 @file{gdb2} and then type @kbd{@w{./gdb ./gdb2}}.
7783
7784 When you run @value{GDBN} in the @value{GDBN} source directory, it will read a
7785 @file{.gdbinit} file that sets up some simple things to make debugging
7786 gdb easier.  The @code{info} command, when executed without a subcommand
7787 in a @value{GDBN} being debugged by gdb, will pop you back up to the top level
7788 gdb.  See @file{.gdbinit} for details.
7789
7790 If you use emacs, you will probably want to do a @code{make TAGS} after
7791 you configure your distribution; this will put the machine dependent
7792 routines for your local machine where they will be accessed first by
7793 @kbd{M-.}
7794
7795 Also, make sure that you've either compiled @value{GDBN} with your local cc, or
7796 have run @code{fixincludes} if you are compiling with gcc.
7797
7798 @section Submitting Patches
7799
7800 @cindex submitting patches
7801 Thanks for thinking of offering your changes back to the community of
7802 @value{GDBN} users.  In general we like to get well designed enhancements.
7803 Thanks also for checking in advance about the best way to transfer the
7804 changes.
7805
7806 The @value{GDBN} maintainers will only install ``cleanly designed'' patches.
7807 This manual summarizes what we believe to be clean design for @value{GDBN}.
7808
7809 If the maintainers don't have time to put the patch in when it arrives,
7810 or if there is any question about a patch, it goes into a large queue
7811 with everyone else's patches and bug reports.
7812
7813 @cindex legal papers for code contributions
7814 The legal issue is that to incorporate substantial changes requires a
7815 copyright assignment from you and/or your employer, granting ownership
7816 of the changes to the Free Software Foundation.  You can get the
7817 standard documents for doing this by sending mail to @code{gnu@@gnu.org}
7818 and asking for it.  We recommend that people write in "All programs
7819 owned by the Free Software Foundation" as "NAME OF PROGRAM", so that
7820 changes in many programs (not just @value{GDBN}, but GAS, Emacs, GCC,
7821 etc) can be
7822 contributed with only one piece of legalese pushed through the
7823 bureaucracy and filed with the FSF.  We can't start merging changes until
7824 this paperwork is received by the FSF (their rules, which we follow
7825 since we maintain it for them).
7826
7827 Technically, the easiest way to receive changes is to receive each
7828 feature as a small context diff or unidiff, suitable for @code{patch}.
7829 Each message sent to me should include the changes to C code and
7830 header files for a single feature, plus @file{ChangeLog} entries for
7831 each directory where files were modified, and diffs for any changes
7832 needed to the manuals (@file{gdb/doc/gdb.texinfo} or
7833 @file{gdb/doc/gdbint.texinfo}).  If there are a lot of changes for a
7834 single feature, they can be split down into multiple messages.
7835
7836 In this way, if we read and like the feature, we can add it to the
7837 sources with a single patch command, do some testing, and check it in.
7838 If you leave out the @file{ChangeLog}, we have to write one.  If you leave
7839 out the doc, we have to puzzle out what needs documenting.  Etc., etc.
7840
7841 The reason to send each change in a separate message is that we will not
7842 install some of the changes.  They'll be returned to you with questions
7843 or comments.  If we're doing our job correctly, the message back to you
7844 will say what you have to fix in order to make the change acceptable.
7845 The reason to have separate messages for separate features is so that
7846 the acceptable changes can be installed while one or more changes are
7847 being reworked.  If multiple features are sent in a single message, we
7848 tend to not put in the effort to sort out the acceptable changes from
7849 the unacceptable, so none of the features get installed until all are
7850 acceptable.
7851
7852 If this sounds painful or authoritarian, well, it is.  But we get a lot
7853 of bug reports and a lot of patches, and many of them don't get
7854 installed because we don't have the time to finish the job that the bug
7855 reporter or the contributor could have done.  Patches that arrive
7856 complete, working, and well designed, tend to get installed on the day
7857 they arrive.  The others go into a queue and get installed as time
7858 permits, which, since the maintainers have many demands to meet, may not
7859 be for quite some time.
7860
7861 Please send patches directly to
7862 @email{gdb-patches@@sourceware.org, the @value{GDBN} maintainers}.
7863
7864 @section Build Script
7865
7866 @cindex build script
7867
7868 The script @file{gdb_buildall.sh} builds @value{GDBN} with flag
7869 @option{--enable-targets=all} set.  This builds @value{GDBN} with all supported
7870 targets activated.  This helps testing @value{GDBN} when doing changes that
7871 affect more than one architecture and is much faster than using
7872 @file{gdb_mbuild.sh}.
7873
7874 After building @value{GDBN} the script checks which architectures are
7875 supported and then switches the current architecture to each of those to get
7876 information about the architecture.  The test results are stored in log files
7877 in the directory the script was called from.
7878
7879 @include observer.texi
7880 @raisesections
7881 @include fdl.texi
7882 @lowersections
7883
7884 @node Index
7885 @unnumbered Index
7886
7887 @printindex cp
7888
7889 @bye