OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.3/] [gdb/] [doc/] [gdbint.info-4] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1181 sfurman
This is gdbint.info, produced by makeinfo version 4.1 from
2
./gdbint.texinfo.
3
 
4
INFO-DIR-SECTION Programming & development tools.
5
START-INFO-DIR-ENTRY
6
* Gdb-Internals: (gdbint).      The GNU debugger's internals.
7
END-INFO-DIR-ENTRY
8
 
9
   This file documents the internals of the GNU debugger GDB.
10
Copyright 1990,1991,1992,1993,1994,1996,1998,1999,2000,2001,2002
11
Free Software Foundation, Inc.  Contributed by Cygnus Solutions.
12
Written by John Gilmore.  Second Edition by Stan Shebs.
13
 
14
   Permission is granted to copy, distribute and/or modify this document
15
under the terms of the GNU Free Documentation License, Version 1.1 or
16
any later version published by the Free Software Foundation; with no
17
Invariant Sections, with the Front-Cover Texts being "A GNU Manual,"
18
and with the Back-Cover Texts as in (a) below.
19
 
20
   (a) The FSF's Back-Cover Text is: "You have freedom to copy and
21
modify this GNU Manual, like GNU software.  Copies published by the Free
22
Software Foundation raise funds for GNU development."
23
 
24
   (1) Some D10V instructions are actually pairs of 16-bit
25
sub-instructions.  However, since you can't jump into the middle of
26
such a pair, code addresses can only refer to full 32 bit instructions,
27
which is what matters in this explanation.
28
 
29
   (2) The above is from the original example and uses K&R C.  GDB has
30
since converted to ISO C but lets ignore that.
31
 
32

33
File: gdbint.info,  Node: Target Vector Definition,  Next: Native Debugging,  Prev: Target Architecture Definition,  Up: Top
34
 
35
Target Vector Definition
36
************************
37
 
38
   The target vector defines the interface between GDB's abstract
39
handling of target systems, and the nitty-gritty code that actually
40
exercises control over a process or a serial port.  GDB includes some
41
30-40 different target vectors; however, each configuration of GDB
42
includes only a few of them.
43
 
44
File Targets
45
============
46
 
47
   Both executables and core files have target vectors.
48
 
49
Standard Protocol and Remote Stubs
50
==================================
51
 
52
   GDB's file `remote.c' talks a serial protocol to code that runs in
53
the target system.  GDB provides several sample "stubs" that can be
54
integrated into target programs or operating systems for this purpose;
55
they are named `*-stub.c'.
56
 
57
   The GDB user's manual describes how to put such a stub into your
58
target code.  What follows is a discussion of integrating the SPARC
59
stub into a complicated operating system (rather than a simple
60
program), by Stu Grossman, the author of this stub.
61
 
62
   The trap handling code in the stub assumes the following upon entry
63
to `trap_low':
64
 
65
  1. %l1 and %l2 contain pc and npc respectively at the time of the
66
     trap;
67
 
68
  2. traps are disabled;
69
 
70
  3. you are in the correct trap window.
71
 
72
   As long as your trap handler can guarantee those conditions, then
73
there is no reason why you shouldn't be able to "share" traps with the
74
stub.  The stub has no requirement that it be jumped to directly from
75
the hardware trap vector.  That is why it calls `exceptionHandler()',
76
which is provided by the external environment.  For instance, this could
77
set up the hardware traps to actually execute code which calls the stub
78
first, and then transfers to its own trap handler.
79
 
80
   For the most point, there probably won't be much of an issue with
81
"sharing" traps, as the traps we use are usually not used by the kernel,
82
and often indicate unrecoverable error conditions.  Anyway, this is all
83
controlled by a table, and is trivial to modify.  The most important
84
trap for us is for `ta 1'.  Without that, we can't single step or do
85
breakpoints.  Everything else is unnecessary for the proper operation
86
of the debugger/stub.
87
 
88
   From reading the stub, it's probably not obvious how breakpoints
89
work.  They are simply done by deposit/examine operations from GDB.
90
 
91
ROM Monitor Interface
92
=====================
93
 
94
Custom Protocols
95
================
96
 
97
Transport Layer
98
===============
99
 
100
Builtin Simulator
101
=================
102
 
103

104
File: gdbint.info,  Node: Native Debugging,  Next: Support Libraries,  Prev: Target Vector Definition,  Up: Top
105
 
106
Native Debugging
107
****************
108
 
109
   Several files control GDB's configuration for native support:
110
 
111
`gdb/config/ARCH/XYZ.mh'
112
     Specifies Makefile fragments needed by a _native_ configuration on
113
     machine XYZ.  In particular, this lists the required
114
     native-dependent object files, by defining `NATDEPFILES=...'.
115
     Also specifies the header file which describes native support on
116
     XYZ, by defining `NAT_FILE= nm-XYZ.h'.  You can also define
117
     `NAT_CFLAGS', `NAT_ADD_FILES', `NAT_CLIBS', `NAT_CDEPS', etc.; see
118
     `Makefile.in'.
119
 
120
     _Maintainer's note: The `.mh' suffix is because this file
121
     originally contained `Makefile' fragments for hosting GDB on
122
     machine XYZ.  While the file is no longer used for this purpose,
123
     the `.mh' suffix remains.  Perhaps someone will eventually rename
124
     these fragments so that they have a `.mn' suffix._
125
 
126
`gdb/config/ARCH/nm-XYZ.h'
127
     (`nm.h' is a link to this file, created by `configure').  Contains
128
     C macro definitions describing the native system environment, such
129
     as child process control and core file support.
130
 
131
`gdb/XYZ-nat.c'
132
     Contains any miscellaneous C code required for this native support
133
     of this machine.  On some machines it doesn't exist at all.
134
 
135
   There are some "generic" versions of routines that can be used by
136
various systems.  These can be customized in various ways by macros
137
defined in your `nm-XYZ.h' file.  If these routines work for the XYZ
138
host, you can just include the generic file's name (with `.o', not
139
`.c') in `NATDEPFILES'.
140
 
141
   Otherwise, if your machine needs custom support routines, you will
142
need to write routines that perform the same functions as the generic
143
file.  Put them into `XYZ-nat.c', and put `XYZ-nat.o' into
144
`NATDEPFILES'.
145
 
146
`inftarg.c'
147
     This contains the _target_ops vector_ that supports Unix child
148
     processes on systems which use ptrace and wait to control the
149
     child.
150
 
151
`procfs.c'
152
     This contains the _target_ops vector_ that supports Unix child
153
     processes on systems which use /proc to control the child.
154
 
155
`fork-child.c'
156
     This does the low-level grunge that uses Unix system calls to do a
157
     "fork and exec" to start up a child process.
158
 
159
`infptrace.c'
160
     This is the low level interface to inferior processes for systems
161
     using the Unix `ptrace' call in a vanilla way.
162
 
163
Native core file Support
164
========================
165
 
166
`core-aout.c::fetch_core_registers()'
167
     Support for reading registers out of a core file.  This routine
168
     calls `register_addr()', see below.  Now that BFD is used to read
169
     core files, virtually all machines should use `core-aout.c', and
170
     should just provide `fetch_core_registers' in `XYZ-nat.c' (or
171
     `REGISTER_U_ADDR' in `nm-XYZ.h').
172
 
173
`core-aout.c::register_addr()'
174
     If your `nm-XYZ.h' file defines the macro `REGISTER_U_ADDR(addr,
175
     blockend, regno)', it should be defined to set `addr' to the
176
     offset within the `user' struct of GDB register number `regno'.
177
     `blockend' is the offset within the "upage" of `u.u_ar0'.  If
178
     `REGISTER_U_ADDR' is defined, `core-aout.c' will define the
179
     `register_addr()' function and use the macro in it.  If you do not
180
     define `REGISTER_U_ADDR', but you are using the standard
181
     `fetch_core_registers()', you will need to define your own version
182
     of `register_addr()', put it into your `XYZ-nat.c' file, and be
183
     sure `XYZ-nat.o' is in the `NATDEPFILES' list.  If you have your
184
     own `fetch_core_registers()', you may not need a separate
185
     `register_addr()'.  Many custom `fetch_core_registers()'
186
     implementations simply locate the registers themselves.
187
 
188
   When making GDB run native on a new operating system, to make it
189
possible to debug core files, you will need to either write specific
190
code for parsing your OS's core files, or customize `bfd/trad-core.c'.
191
First, use whatever `#include' files your machine uses to define the
192
struct of registers that is accessible (possibly in the u-area) in a
193
core file (rather than `machine/reg.h'), and an include file that
194
defines whatever header exists on a core file (e.g. the u-area or a
195
`struct core').  Then modify `trad_unix_core_file_p' to use these
196
values to set up the section information for the data segment, stack
197
segment, any other segments in the core file (perhaps shared library
198
contents or control information), "registers" segment, and if there are
199
two discontiguous sets of registers (e.g.  integer and float), the
200
"reg2" segment.  This section information basically delimits areas in
201
the core file in a standard way, which the section-reading routines in
202
BFD know how to seek around in.
203
 
204
   Then back in GDB, you need a matching routine called
205
`fetch_core_registers'.  If you can use the generic one, it's in
206
`core-aout.c'; if not, it's in your `XYZ-nat.c' file.  It will be
207
passed a char pointer to the entire "registers" segment, its length,
208
and a zero; or a char pointer to the entire "regs2" segment, its
209
length, and a 2.  The routine should suck out the supplied register
210
values and install them into GDB's "registers" array.
211
 
212
   If your system uses `/proc' to control processes, and uses ELF
213
format core files, then you may be able to use the same routines for
214
reading the registers out of processes and out of core files.
215
 
216
ptrace
217
======
218
 
219
/proc
220
=====
221
 
222
win32
223
=====
224
 
225
shared libraries
226
================
227
 
228
Native Conditionals
229
===================
230
 
231
   When GDB is configured and compiled, various macros are defined or
232
left undefined, to control compilation when the host and target systems
233
are the same.  These macros should be defined (or left undefined) in
234
`nm-SYSTEM.h'.
235
 
236
`ATTACH_DETACH'
237
     If defined, then GDB will include support for the `attach' and
238
     `detach' commands.
239
 
240
`CHILD_PREPARE_TO_STORE'
241
     If the machine stores all registers at once in the child process,
242
     then define this to ensure that all values are correct.  This
243
     usually entails a read from the child.
244
 
245
     [Note that this is incorrectly defined in `xm-SYSTEM.h' files
246
     currently.]
247
 
248
`FETCH_INFERIOR_REGISTERS'
249
     Define this if the native-dependent code will provide its own
250
     routines `fetch_inferior_registers' and `store_inferior_registers'
251
     in `HOST-nat.c'.  If this symbol is _not_ defined, and
252
     `infptrace.c' is included in this configuration, the default
253
     routines in `infptrace.c' are used for these functions.
254
 
255
`FILES_INFO_HOOK'
256
     (Only defined for Convex.)
257
 
258
`FP0_REGNUM'
259
     This macro is normally defined to be the number of the first
260
     floating point register, if the machine has such registers.  As
261
     such, it would appear only in target-specific code.  However,
262
     `/proc' support uses this to decide whether floats are in use on
263
     this target.
264
 
265
`GET_LONGJMP_TARGET'
266
     For most machines, this is a target-dependent parameter.  On the
267
     DECstation and the Iris, this is a native-dependent parameter,
268
     since `setjmp.h' is needed to define it.
269
 
270
     This macro determines the target PC address that `longjmp' will
271
     jump to, assuming that we have just stopped at a longjmp
272
     breakpoint.  It takes a `CORE_ADDR *' as argument, and stores the
273
     target PC value through this pointer.  It examines the current
274
     state of the machine as needed.
275
 
276
`I386_USE_GENERIC_WATCHPOINTS'
277
     An x86-based machine can define this to use the generic x86
278
     watchpoint support; see *Note I386_USE_GENERIC_WATCHPOINTS:
279
     Algorithms.
280
 
281
`KERNEL_U_ADDR'
282
     Define this to the address of the `u' structure (the "user
283
     struct", also known as the "u-page") in kernel virtual memory.
284
     GDB needs to know this so that it can subtract this address from
285
     absolute addresses in the upage, that are obtained via ptrace or
286
     from core files.  On systems that don't need this value, set it to
287
     zero.
288
 
289
`KERNEL_U_ADDR_BSD'
290
     Define this to cause GDB to determine the address of `u' at
291
     runtime, by using Berkeley-style `nlist' on the kernel's image in
292
     the root directory.
293
 
294
`KERNEL_U_ADDR_HPUX'
295
     Define this to cause GDB to determine the address of `u' at
296
     runtime, by using HP-style `nlist' on the kernel's image in the
297
     root directory.
298
 
299
`ONE_PROCESS_WRITETEXT'
300
     Define this to be able to, when a breakpoint insertion fails, warn
301
     the user that another process may be running with the same
302
     executable.
303
 
304
`PREPARE_TO_PROCEED (SELECT_IT)'
305
     This (ugly) macro allows a native configuration to customize the
306
     way the `proceed' function in `infrun.c' deals with switching
307
     between threads.
308
 
309
     In a multi-threaded task we may select another thread and then
310
     continue or step.  But if the old thread was stopped at a
311
     breakpoint, it will immediately cause another breakpoint stop
312
     without any execution (i.e. it will report a breakpoint hit
313
     incorrectly).  So GDB must step over it first.
314
 
315
     If defined, `PREPARE_TO_PROCEED' should check the current thread
316
     against the thread that reported the most recent event.  If a
317
     step-over is required, it returns TRUE.  If SELECT_IT is non-zero,
318
     it should reselect the old thread.
319
 
320
`PROC_NAME_FMT'
321
     Defines the format for the name of a `/proc' device.  Should be
322
     defined in `nm.h' _only_ in order to override the default
323
     definition in `procfs.c'.
324
 
325
`PTRACE_FP_BUG'
326
     See `mach386-xdep.c'.
327
 
328
`PTRACE_ARG3_TYPE'
329
     The type of the third argument to the `ptrace' system call, if it
330
     exists and is different from `int'.
331
 
332
`REGISTER_U_ADDR'
333
     Defines the offset of the registers in the "u area".
334
 
335
`SHELL_COMMAND_CONCAT'
336
     If defined, is a string to prefix on the shell command used to
337
     start the inferior.
338
 
339
`SHELL_FILE'
340
     If defined, this is the name of the shell to use to run the
341
     inferior.  Defaults to `"/bin/sh"'.
342
 
343
`SOLIB_ADD (FILENAME, FROM_TTY, TARG, READSYMS)'
344
     Define this to expand into an expression that will cause the
345
     symbols in FILENAME to be added to GDB's symbol table. If READSYMS
346
     is zero symbols are not read but any necessary low level
347
     processing for FILENAME is still done.
348
 
349
`SOLIB_CREATE_INFERIOR_HOOK'
350
     Define this to expand into any shared-library-relocation code that
351
     you want to be run just after the child process has been forked.
352
 
353
`START_INFERIOR_TRAPS_EXPECTED'
354
     When starting an inferior, GDB normally expects to trap twice;
355
     once when the shell execs, and once when the program itself execs.
356
     If the actual number of traps is something other than 2, then
357
     define this macro to expand into the number expected.
358
 
359
`SVR4_SHARED_LIBS'
360
     Define this to indicate that SVR4-style shared libraries are in
361
     use.
362
 
363
`USE_PROC_FS'
364
     This determines whether small routines in `*-tdep.c', which
365
     translate register values between GDB's internal representation
366
     and the `/proc' representation, are compiled.
367
 
368
`U_REGS_OFFSET'
369
     This is the offset of the registers in the upage.  It need only be
370
     defined if the generic ptrace register access routines in
371
     `infptrace.c' are being used (that is, `infptrace.c' is configured
372
     in, and `FETCH_INFERIOR_REGISTERS' is not defined).  If the
373
     default value from `infptrace.c' is good enough, leave it
374
     undefined.
375
 
376
     The default value means that u.u_ar0 _points to_ the location of
377
     the registers.  I'm guessing that `#define U_REGS_OFFSET 0' means
378
     that `u.u_ar0' _is_ the location of the registers.
379
 
380
`CLEAR_SOLIB'
381
     See `objfiles.c'.
382
 
383
`DEBUG_PTRACE'
384
     Define this to debug `ptrace' calls.
385
 
386

387
File: gdbint.info,  Node: Support Libraries,  Next: Coding,  Prev: Native Debugging,  Up: Top
388
 
389
Support Libraries
390
*****************
391
 
392
BFD
393
===
394
 
395
   BFD provides support for GDB in several ways:
396
 
397
_identifying executable and core files_
398
     BFD will identify a variety of file types, including a.out, coff,
399
     and several variants thereof, as well as several kinds of core
400
     files.
401
 
402
_access to sections of files_
403
     BFD parses the file headers to determine the names, virtual
404
     addresses, sizes, and file locations of all the various named
405
     sections in files (such as the text section or the data section).
406
     GDB simply calls BFD to read or write section X at byte offset Y
407
     for length Z.
408
 
409
_specialized core file support_
410
     BFD provides routines to determine the failing command name stored
411
     in a core file, the signal with which the program failed, and
412
     whether a core file matches (i.e. could be a core dump of) a
413
     particular executable file.
414
 
415
_locating the symbol information_
416
     GDB uses an internal interface of BFD to determine where to find
417
     the symbol information in an executable file or symbol-file.  GDB
418
     itself handles the reading of symbols, since BFD does not
419
     "understand" debug symbols, but GDB uses BFD's cached information
420
     to find the symbols, string table, etc.
421
 
422
opcodes
423
=======
424
 
425
   The opcodes library provides GDB's disassembler.  (It's a separate
426
library because it's also used in binutils, for `objdump').
427
 
428
readline
429
========
430
 
431
mmalloc
432
=======
433
 
434
libiberty
435
=========
436
 
437
gnu-regex
438
=========
439
 
440
   Regex conditionals.
441
 
442
`C_ALLOCA'
443
 
444
`NFAILURES'
445
 
446
`RE_NREGS'
447
 
448
`SIGN_EXTEND_CHAR'
449
 
450
`SWITCH_ENUM_BUG'
451
 
452
`SYNTAX_TABLE'
453
 
454
`Sword'
455
 
456
`sparc'
457
include
458
=======
459
 
460

461
File: gdbint.info,  Node: Coding,  Next: Porting GDB,  Prev: Support Libraries,  Up: Top
462
 
463
Coding
464
******
465
 
466
   This chapter covers topics that are lower-level than the major
467
algorithms of GDB.
468
 
469
Cleanups
470
========
471
 
472
   Cleanups are a structured way to deal with things that need to be
473
done later.
474
 
475
   When your code does something (e.g., `xmalloc' some memory, or
476
`open' a file) that needs to be undone later (e.g., `xfree' the memory
477
or `close' the file), it can make a cleanup.  The cleanup will be done
478
at some future point: when the command is finished and control returns
479
to the top level; when an error occurs and the stack is unwound; or
480
when your code decides it's time to explicitly perform cleanups.
481
Alternatively you can elect to discard the cleanups you created.
482
 
483
   Syntax:
484
 
485
`struct cleanup *OLD_CHAIN;'
486
     Declare a variable which will hold a cleanup chain handle.
487
 
488
`OLD_CHAIN = make_cleanup (FUNCTION, ARG);'
489
     Make a cleanup which will cause FUNCTION to be called with ARG (a
490
     `char *') later.  The result, OLD_CHAIN, is a handle that can
491
     later be passed to `do_cleanups' or `discard_cleanups'.  Unless
492
     you are going to call `do_cleanups' or `discard_cleanups', you can
493
     ignore the result from `make_cleanup'.
494
 
495
`do_cleanups (OLD_CHAIN);'
496
     Do all cleanups added to the chain since the corresponding
497
     `make_cleanup' call was made.
498
 
499
`discard_cleanups (OLD_CHAIN);'
500
     Same as `do_cleanups' except that it just removes the cleanups from
501
     the chain and does not call the specified functions.
502
 
503
   Cleanups are implemented as a chain.  The handle returned by
504
`make_cleanups' includes the cleanup passed to the call and any later
505
cleanups appended to the chain (but not yet discarded or performed).
506
E.g.:
507
 
508
     make_cleanup (a, 0);
509
     {
510
       struct cleanup *old = make_cleanup (b, 0);
511
       make_cleanup (c, 0)
512
       ...
513
       do_cleanups (old);
514
     }
515
 
516
will call `c()' and `b()' but will not call `a()'.  The cleanup that
517
calls `a()' will remain in the cleanup chain, and will be done later
518
unless otherwise discarded.
519
 
520
   Your function should explicitly do or discard the cleanups it
521
creates.  Failing to do this leads to non-deterministic behavior since
522
the caller will arbitrarily do or discard your functions cleanups.
523
This need leads to two common cleanup styles.
524
 
525
   The first style is try/finally.  Before it exits, your code-block
526
calls `do_cleanups' with the old cleanup chain and thus ensures that
527
your code-block's cleanups are always performed.  For instance, the
528
following code-segment avoids a memory leak problem (even when `error'
529
is called and a forced stack unwind occurs) by ensuring that the
530
`xfree' will always be called:
531
 
532
     struct cleanup *old = make_cleanup (null_cleanup, 0);
533
     data = xmalloc (sizeof blah);
534
     make_cleanup (xfree, data);
535
     ... blah blah ...
536
     do_cleanups (old);
537
 
538
   The second style is try/except.  Before it exits, your code-block
539
calls `discard_cleanups' with the old cleanup chain and thus ensures
540
that any created cleanups are not performed.  For instance, the
541
following code segment, ensures that the file will be closed but only
542
if there is an error:
543
 
544
     FILE *file = fopen ("afile", "r");
545
     struct cleanup *old = make_cleanup (close_file, file);
546
     ... blah blah ...
547
     discard_cleanups (old);
548
     return file;
549
 
550
   Some functions, e.g. `fputs_filtered()' or `error()', specify that
551
they "should not be called when cleanups are not in place".  This means
552
that any actions you need to reverse in the case of an error or
553
interruption must be on the cleanup chain before you call these
554
functions, since they might never return to your code (they `longjmp'
555
instead).
556
 
557
Per-architecture module data
558
============================
559
 
560
   The multi-arch framework includes a mechanism for adding module
561
specific per-architecture data-pointers to the `struct gdbarch'
562
architecture object.
563
 
564
   A module registers one or more per-architecture data-pointers using
565
the function `register_gdbarch_data':
566
 
567
 - Function: struct gdbarch_data *register_gdbarch_data
568
          (gdbarch_data_init_ftype *INIT, gdbarch_data_free_ftype *FREE)
569
     The INIT function is used to obtain an initial value for a
570
     per-architecture data-pointer.  The function is called, after the
571
     architecture has been created, when the data-pointer is still
572
     uninitialized (`NULL') and its value has been requested via a call
573
     to `gdbarch_data'.  A data-pointer can also be initialize
574
     explicitly using `set_gdbarch_data'.
575
 
576
     The FREE function is called when a data-pointer needs to be
577
     destroyed.  This occurs when either the corresponding `struct
578
     gdbarch' object is being destroyed or when `set_gdbarch_data' is
579
     overriding a non-`NULL' data-pointer value.
580
 
581
     The function `register_gdbarch_data' returns a `struct
582
     gdbarch_data' that is used to identify the data-pointer that was
583
     added to the module.
584
 
585
 
586
   A typical module has `init' and `free' functions of the form:
587
 
588
     static struct gdbarch_data *nozel_handle;
589
     static void *
590
     nozel_init (struct gdbarch *gdbarch)
591
     {
592
       struct nozel *data = XMALLOC (struct nozel);
593
       ...
594
       return data;
595
     }
596
     ...
597
     static void
598
     nozel_free (struct gdbarch *gdbarch, void *data)
599
     {
600
       xfree (data);
601
     }
602
 
603
   Since uninitialized (`NULL') data-pointers are initialized
604
on-demand, an `init' function is free to call other modules that use
605
data-pointers.  Those modules data-pointers will be initialized as
606
needed.  Care should be taken to ensure that the `init' call graph does
607
not contain cycles.
608
 
609
   The data-pointer is registered with the call:
610
 
611
     void
612
     _initialize_nozel (void)
613
     {
614
       nozel_handle = register_gdbarch_data (nozel_init, nozel_free);
615
     ...
616
 
617
   The per-architecture data-pointer is accessed using the function:
618
 
619
 - Function: void *gdbarch_data (struct gdbarch *GDBARCH, struct
620
          gdbarch_data *DATA_HANDLE)
621
     Given the architecture ARCH and module data handle DATA_HANDLE
622
     (returned by `register_gdbarch_data', this function returns the
623
     current value of the per-architecture data-pointer.
624
 
625
   The non-`NULL' data-pointer returned by `gdbarch_data' should be
626
saved in a local variable and then used directly:
627
 
628
     int
629
     nozel_total (struct gdbarch *gdbarch)
630
     {
631
       int total;
632
       struct nozel *data = gdbarch_data (gdbarch, nozel_handle);
633
       ...
634
       return total;
635
     }
636
 
637
   It is also possible to directly initialize the data-pointer using:
638
 
639
 - Function: void set_gdbarch_data (struct gdbarch *GDBARCH, struct
640
          gdbarch_data *handle, void *POINTER)
641
     Update the data-pointer corresponding to HANDLE with the value of
642
     POINTER.  If the previous data-pointer value is non-NULL, then it
643
     is freed using data-pointers FREE function.
644
 
645
   This function is used by modules that require a mechanism for
646
explicitly setting the per-architecture data-pointer during
647
architecture creation:
648
 
649
     /* Called during architecture creation.  */
650
     extern void
651
     set_gdbarch_nozel (struct gdbarch *gdbarch,
652
                        int total)
653
     {
654
       struct nozel *data = XMALLOC (struct nozel);
655
       ...
656
       set_gdbarch_data (gdbarch, nozel_handle, nozel);
657
     }
658
 
659
     /* Default, called when nozel not set by set_gdbarch_nozel().  */
660
     static void *
661
     nozel_init (struct gdbarch *gdbarch)
662
     {
663
       struct nozel *default_nozel = XMALLOC (struc nozel);
664
       ...
665
       return default_nozel;
666
     }
667
 
668
     void
669
     _initialize_nozel (void)
670
     {
671
       nozel_handle = register_gdbarch_data (nozel_init, NULL);
672
       ...
673
 
674
Note that an `init' function still needs to be registered.  It is used
675
to initialize the data-pointer when the architecture creation phase
676
fail to set an initial value.
677
 
678
Wrapping Output Lines
679
=====================
680
 
681
   Output that goes through `printf_filtered' or `fputs_filtered' or
682
`fputs_demangled' needs only to have calls to `wrap_here' added in
683
places that would be good breaking points.  The utility routines will
684
take care of actually wrapping if the line width is exceeded.
685
 
686
   The argument to `wrap_here' is an indentation string which is
687
printed _only_ if the line breaks there.  This argument is saved away
688
and used later.  It must remain valid until the next call to
689
`wrap_here' or until a newline has been printed through the
690
`*_filtered' functions.  Don't pass in a local variable and then return!
691
 
692
   It is usually best to call `wrap_here' after printing a comma or
693
space.  If you call it before printing a space, make sure that your
694
indentation properly accounts for the leading space that will print if
695
the line wraps there.
696
 
697
   Any function or set of functions that produce filtered output must
698
finish by printing a newline, to flush the wrap buffer, before switching
699
to unfiltered (`printf') output.  Symbol reading routines that print
700
warnings are a good example.
701
 
702
GDB Coding Standards
703
====================
704
 
705
   GDB follows the GNU coding standards, as described in
706
`etc/standards.texi'.  This file is also available for anonymous FTP
707
from GNU archive sites.  GDB takes a strict interpretation of the
708
standard; in general, when the GNU standard recommends a practice but
709
does not require it, GDB requires it.
710
 
711
   GDB follows an additional set of coding standards specific to GDB,
712
as described in the following sections.
713
 
714
ISO C
715
-----
716
 
717
   GDB assumes an ISO/IEC 9899:1990 (a.k.a. ISO C90) compliant compiler.
718
 
719
   GDB does not assume an ISO C or POSIX compliant C library.
720
 
721
Memory Management
722
-----------------
723
 
724
   GDB does not use the functions `malloc', `realloc', `calloc', `free'
725
and `asprintf'.
726
 
727
   GDB uses the functions `xmalloc', `xrealloc' and `xcalloc' when
728
allocating memory.  Unlike `malloc' et.al.  these functions do not
729
return when the memory pool is empty.  Instead, they unwind the stack
730
using cleanups.  These functions return `NULL' when requested to
731
allocate a chunk of memory of size zero.
732
 
733
   _Pragmatics: By using these functions, the need to check every
734
memory allocation is removed.  These functions provide portable
735
behavior._
736
 
737
   GDB does not use the function `free'.
738
 
739
   GDB uses the function `xfree' to return memory to the memory pool.
740
Consistent with ISO-C, this function ignores a request to free a `NULL'
741
pointer.
742
 
743
   _Pragmatics: On some systems `free' fails when passed a `NULL'
744
pointer._
745
 
746
   GDB can use the non-portable function `alloca' for the allocation of
747
small temporary values (such as strings).
748
 
749
   _Pragmatics: This function is very non-portable.  Some systems
750
restrict the memory being allocated to no more than a few kilobytes._
751
 
752
   GDB uses the string function `xstrdup' and the print function
753
`xasprintf'.
754
 
755
   _Pragmatics: `asprintf' and `strdup' can fail.  Print functions such
756
as `sprintf' are very prone to buffer overflow errors._
757
 
758
Compiler Warnings
759
-----------------
760
 
761
   With few exceptions, developers should include the configuration
762
option `--enable-gdb-build-warnings=,-Werror' when building GDB.  The
763
exceptions are listed in the file `gdb/MAINTAINERS'.
764
 
765
   This option causes GDB (when built using GCC) to be compiled with a
766
carefully selected list of compiler warning flags.  Any warnings from
767
those flags being treated as errors.
768
 
769
   The current list of warning flags includes:
770
 
771
`-Wimplicit'
772
     Since GDB coding standard requires all functions to be declared
773
     using a prototype, the flag has the side effect of ensuring that
774
     prototyped functions are always visible with out resorting to
775
     `-Wstrict-prototypes'.
776
 
777
`-Wreturn-type'
778
     Such code often appears to work except on instruction set
779
     architectures that use register windows.
780
 
781
`-Wcomment'
782
 
783
`-Wtrigraphs'
784
 
785
`-Wformat'
786
     Since GDB uses the `format printf' attribute on all `printf' like
787
     functions this checks not just `printf' calls but also calls to
788
     functions such as `fprintf_unfiltered'.
789
 
790
`-Wparentheses'
791
     This warning includes uses of the assignment operator within an
792
     `if' statement.
793
 
794
`-Wpointer-arith'
795
 
796
`-Wuninitialized'
797
   _Pragmatics: Due to the way that GDB is implemented most functions
798
have unused parameters.  Consequently the warning `-Wunused-parameter'
799
is precluded from the list.  The macro `ATTRIBUTE_UNUSED' is not used
800
as it leads to false negatives -- it is not an error to have
801
`ATTRIBUTE_UNUSED' on a parameter that is being used.  The options
802
`-Wall' and `-Wunused' are also precluded because they both include
803
`-Wunused-parameter'._
804
 
805
   _Pragmatics: GDB has not simply accepted the warnings enabled by
806
`-Wall -Werror -W...'.  Instead it is selecting warnings when and where
807
their benefits can be demonstrated._
808
 
809
Formatting
810
----------
811
 
812
   The standard GNU recommendations for formatting must be followed
813
strictly.
814
 
815
   A function declaration should not have its name in column zero.  A
816
function definition should have its name in column zero.
817
 
818
     /* Declaration */
819
     static void foo (void);
820
     /* Definition */
821
     void
822
     foo (void)
823
     {
824
     }
825
 
826
   _Pragmatics: This simplifies scripting.  Function definitions can be
827
found using `^function-name'._
828
 
829
   There must be a space between a function or macro name and the
830
opening parenthesis of its argument list (except for macro definitions,
831
as required by C).  There must not be a space after an open
832
paren/bracket or before a close paren/bracket.
833
 
834
   While additional whitespace is generally helpful for reading, do not
835
use more than one blank line to separate blocks, and avoid adding
836
whitespace after the end of a program line (as of 1/99, some 600 lines
837
had whitespace after the semicolon).  Excess whitespace causes
838
difficulties for `diff' and `patch' utilities.
839
 
840
   Pointers are declared using the traditional K&R C style:
841
 
842
     void *foo;
843
 
844
and not:
845
 
846
     void * foo;
847
     void* foo;
848
 
849
Comments
850
--------
851
 
852
   The standard GNU requirements on comments must be followed strictly.
853
 
854
   Block comments must appear in the following form, with no `/*'- or
855
`*/'-only lines, and no leading `*':
856
 
857
     /* Wait for control to return from inferior to debugger.  If inferior
858
        gets a signal, we may decide to start it up again instead of
859
        returning.  That is why there is a loop in this function.  When
860
        this function actually returns it means the inferior should be left
861
        stopped and GDB should read more commands.  */
862
 
863
   (Note that this format is encouraged by Emacs; tabbing for a
864
multi-line comment works correctly, and `M-q' fills the block
865
consistently.)
866
 
867
   Put a blank line between the block comments preceding function or
868
variable definitions, and the definition itself.
869
 
870
   In general, put function-body comments on lines by themselves, rather
871
than trying to fit them into the 20 characters left at the end of a
872
line, since either the comment or the code will inevitably get longer
873
than will fit, and then somebody will have to move it anyhow.
874
 
875
C Usage
876
-------
877
 
878
   Code must not depend on the sizes of C data types, the format of the
879
host's floating point numbers, the alignment of anything, or the order
880
of evaluation of expressions.
881
 
882
   Use functions freely.  There are only a handful of compute-bound
883
areas in GDB that might be affected by the overhead of a function call,
884
mainly in symbol reading.  Most of GDB's performance is limited by the
885
target interface (whether serial line or system call).
886
 
887
   However, use functions with moderation.  A thousand one-line
888
functions are just as hard to understand as a single thousand-line
889
function.
890
 
891
   _Macros are bad, M'kay._ (But if you have to use a macro, make sure
892
that the macro arguments are protected with parentheses.)
893
 
894
   Declarations like `struct foo *' should be used in preference to
895
declarations like `typedef struct foo { ... } *foo_ptr'.
896
 
897
Function Prototypes
898
-------------------
899
 
900
   Prototypes must be used when both _declaring_ and _defining_ a
901
function.  Prototypes for GDB functions must include both the argument
902
type and name, with the name matching that used in the actual function
903
definition.
904
 
905
   All external functions should have a declaration in a header file
906
that callers include, except for `_initialize_*' functions, which must
907
be external so that `init.c' construction works, but shouldn't be
908
visible to random source files.
909
 
910
   Where a source file needs a forward declaration of a static function,
911
that declaration must appear in a block near the top of the source file.
912
 
913
Internal Error Recovery
914
-----------------------
915
 
916
   During its execution, GDB can encounter two types of errors.  User
917
errors and internal errors.  User errors include not only a user
918
entering an incorrect command but also problems arising from corrupt
919
object files and system errors when interacting with the target.
920
Internal errors include situations where GDB has detected, at run time,
921
a corrupt or erroneous situation.
922
 
923
   When reporting an internal error, GDB uses `internal_error' and
924
`gdb_assert'.
925
 
926
   GDB must not call `abort' or `assert'.
927
 
928
   _Pragmatics: There is no `internal_warning' function.  Either the
929
code detected a user error, recovered from it and issued a `warning' or
930
the code failed to correctly recover from the user error and issued an
931
`internal_error'._
932
 
933
File Names
934
----------
935
 
936
   Any file used when building the core of GDB must be in lower case.
937
Any file used when building the core of GDB must be 8.3 unique.  These
938
requirements apply to both source and generated files.
939
 
940
   _Pragmatics: The core of GDB must be buildable on many platforms
941
including DJGPP and MacOS/HFS.  Every time an unfriendly file is
942
introduced to the build process both `Makefile.in' and `configure.in'
943
need to be modified accordingly.  Compare the convoluted conversion
944
process needed to transform `COPYING' into `copying.c' with the
945
conversion needed to transform `version.in' into `version.c'._
946
 
947
   Any file non 8.3 compliant file (that is not used when building the
948
core of GDB) must be added to `gdb/config/djgpp/fnchange.lst'.
949
 
950
   _Pragmatics: This is clearly a compromise._
951
 
952
   When GDB has a local version of a system header file (ex `string.h')
953
the file name based on the POSIX header prefixed with `gdb_'
954
(`gdb_string.h').
955
 
956
   For other files `-' is used as the separator.
957
 
958
Include Files
959
-------------
960
 
961
   A `.c' file should include `defs.h' first.
962
 
963
   A `.c' file should directly include the `.h' file of every
964
declaration and/or definition it directly refers to.  It cannot rely on
965
indirect inclusion.
966
 
967
   A `.h' file should directly include the `.h' file of every
968
declaration and/or definition it directly refers to.  It cannot rely on
969
indirect inclusion.  Exception: The file `defs.h' does not need to be
970
directly included.
971
 
972
   An external declaration should only appear in one include file.
973
 
974
   An external declaration should never appear in a `.c' file.
975
Exception: a declaration for the `_initialize' function that pacifies
976
`-Wmissing-declaration'.
977
 
978
   A `typedef' definition should only appear in one include file.
979
 
980
   An opaque `struct' declaration can appear in multiple `.h' files.
981
Where possible, a `.h' file should use an opaque `struct' declaration
982
instead of an include.
983
 
984
   All `.h' files should be wrapped in:
985
 
986
     #ifndef INCLUDE_FILE_NAME_H
987
     #define INCLUDE_FILE_NAME_H
988
     header body
989
     #endif
990
 
991
Clean Design and Portable Implementation
992
----------------------------------------
993
 
994
   In addition to getting the syntax right, there's the little question
995
of semantics.  Some things are done in certain ways in GDB because long
996
experience has shown that the more obvious ways caused various kinds of
997
trouble.
998
 
999
   You can't assume the byte order of anything that comes from a target
1000
(including VALUEs, object files, and instructions).  Such things must
1001
be byte-swapped using `SWAP_TARGET_AND_HOST' in GDB, or one of the swap
1002
routines defined in `bfd.h', such as `bfd_get_32'.
1003
 
1004
   You can't assume that you know what interface is being used to talk
1005
to the target system.  All references to the target must go through the
1006
current `target_ops' vector.
1007
 
1008
   You can't assume that the host and target machines are the same
1009
machine (except in the "native" support modules).  In particular, you
1010
can't assume that the target machine's header files will be available
1011
on the host machine.  Target code must bring along its own header files
1012
- written from scratch or explicitly donated by their owner, to avoid
1013
copyright problems.
1014
 
1015
   Insertion of new `#ifdef''s will be frowned upon.  It's much better
1016
to write the code portably than to conditionalize it for various
1017
systems.
1018
 
1019
   New `#ifdef''s which test for specific compilers or manufacturers or
1020
operating systems are unacceptable.  All `#ifdef''s should test for
1021
features.  The information about which configurations contain which
1022
features should be segregated into the configuration files.  Experience
1023
has proven far too often that a feature unique to one particular system
1024
often creeps into other systems; and that a conditional based on some
1025
predefined macro for your current system will become worthless over
1026
time, as new versions of your system come out that behave differently
1027
with regard to this feature.
1028
 
1029
   Adding code that handles specific architectures, operating systems,
1030
target interfaces, or hosts, is not acceptable in generic code.
1031
 
1032
   One particularly notorious area where system dependencies tend to
1033
creep in is handling of file names.  The mainline GDB code assumes
1034
Posix semantics of file names: absolute file names begin with a forward
1035
slash `/', slashes are used to separate leading directories,
1036
case-sensitive file names.  These assumptions are not necessarily true
1037
on non-Posix systems such as MS-Windows.  To avoid system-dependent
1038
code where you need to take apart or construct a file name, use the
1039
following portable macros:
1040
 
1041
`HAVE_DOS_BASED_FILE_SYSTEM'
1042
     This preprocessing symbol is defined to a non-zero value on hosts
1043
     whose filesystems belong to the MS-DOS/MS-Windows family.  Use this
1044
     symbol to write conditional code which should only be compiled for
1045
     such hosts.
1046
 
1047
`IS_DIR_SEPARATOR (C)'
1048
     Evaluates to a non-zero value if C is a directory separator
1049
     character.  On Unix and GNU/Linux systems, only a slash `/' is
1050
     such a character, but on Windows, both `/' and `\' will pass.
1051
 
1052
`IS_ABSOLUTE_PATH (FILE)'
1053
     Evaluates to a non-zero value if FILE is an absolute file name.
1054
     For Unix and GNU/Linux hosts, a name which begins with a slash `/'
1055
     is absolute.  On DOS and Windows, `d:/foo' and `x:\bar' are also
1056
     absolute file names.
1057
 
1058
`FILENAME_CMP (F1, F2)'
1059
     Calls a function which compares file names F1 and F2 as
1060
     appropriate for the underlying host filesystem.  For Posix systems,
1061
     this simply calls `strcmp'; on case-insensitive filesystems it
1062
     will call `strcasecmp' instead.
1063
 
1064
`DIRNAME_SEPARATOR'
1065
     Evaluates to a character which separates directories in
1066
     `PATH'-style lists, typically held in environment variables.  This
1067
     character is `:' on Unix, `;' on DOS and Windows.
1068
 
1069
`SLASH_STRING'
1070
     This evaluates to a constant string you should use to produce an
1071
     absolute filename from leading directories and the file's basename.
1072
     `SLASH_STRING' is `"/"' on most systems, but might be `"\\"' for
1073
     some Windows-based ports.
1074
 
1075
   In addition to using these macros, be sure to use portable library
1076
functions whenever possible.  For example, to extract a directory or a
1077
basename part from a file name, use the `dirname' and `basename'
1078
library functions (available in `libiberty' for platforms which don't
1079
provide them), instead of searching for a slash with `strrchr'.
1080
 
1081
   Another way to generalize GDB along a particular interface is with an
1082
attribute struct.  For example, GDB has been generalized to handle
1083
multiple kinds of remote interfaces--not by `#ifdef's everywhere, but
1084
by defining the `target_ops' structure and having a current target (as
1085
well as a stack of targets below it, for memory references).  Whenever
1086
something needs to be done that depends on which remote interface we are
1087
using, a flag in the current target_ops structure is tested (e.g.,
1088
`target_has_stack'), or a function is called through a pointer in the
1089
current target_ops structure.  In this way, when a new remote interface
1090
is added, only one module needs to be touched--the one that actually
1091
implements the new remote interface.  Other examples of
1092
attribute-structs are BFD access to multiple kinds of object file
1093
formats, or GDB's access to multiple source languages.
1094
 
1095
   Please avoid duplicating code.  For example, in GDB 3.x all the code
1096
interfacing between `ptrace' and the rest of GDB was duplicated in
1097
`*-dep.c', and so changing something was very painful.  In GDB 4.x,
1098
these have all been consolidated into `infptrace.c'.  `infptrace.c' can
1099
deal with variations between systems the same way any system-independent
1100
file would (hooks, `#if defined', etc.), and machines which are
1101
radically different don't need to use `infptrace.c' at all.
1102
 
1103
   All debugging code must be controllable using the `set debug MODULE'
1104
command.  Do not use `printf' to print trace messages.  Use
1105
`fprintf_unfiltered(gdb_stdlog, ...'.  Do not use `#ifdef DEBUG'.
1106
 
1107

1108
File: gdbint.info,  Node: Porting GDB,  Next: Releasing GDB,  Prev: Coding,  Up: Top
1109
 
1110
Porting GDB
1111
***********
1112
 
1113
   Most of the work in making GDB compile on a new machine is in
1114
specifying the configuration of the machine.  This is done in a
1115
dizzying variety of header files and configuration scripts, which we
1116
hope to make more sensible soon.  Let's say your new host is called an
1117
XYZ (e.g.,  `sun4'), and its full three-part configuration name is
1118
`ARCH-XVEND-XOS' (e.g., `sparc-sun-sunos4').  In particular:
1119
 
1120
   * In the top level directory, edit `config.sub' and add ARCH, XVEND,
1121
     and XOS to the lists of supported architectures, vendors, and
1122
     operating systems near the bottom of the file.  Also, add XYZ as
1123
     an alias that maps to `ARCH-XVEND-XOS'.  You can test your changes
1124
     by running
1125
 
1126
          ./config.sub XYZ
1127
 
1128
     and
1129
 
1130
          ./config.sub `ARCH-XVEND-XOS'
1131
 
1132
     which should both respond with `ARCH-XVEND-XOS' and no error
1133
     messages.
1134
 
1135
     You need to port BFD, if that hasn't been done already.  Porting
1136
     BFD is beyond the scope of this manual.
1137
 
1138
   * To configure GDB itself, edit `gdb/configure.host' to recognize
1139
     your system and set `gdb_host' to XYZ, and (unless your desired
1140
     target is already available) also edit `gdb/configure.tgt',
1141
     setting `gdb_target' to something appropriate (for instance, XYZ).
1142
 
1143
     _Maintainer's note: Work in progress.  The file
1144
     `gdb/configure.host' originally needed to be modified when either a
1145
     new native target or a new host machine was being added to GDB.
1146
     Recent changes have removed this requirement.  The file now only
1147
     needs to be modified when adding a new native configuration.  This
1148
     will likely changed again in the future._
1149
 
1150
   * Finally, you'll need to specify and define GDB's host-, native-,
1151
     and target-dependent `.h' and `.c' files used for your
1152
     configuration.
1153
 
1154
Configuring GDB for Release
1155
===========================
1156
 
1157
   From the top level directory (containing `gdb', `bfd', `libiberty',
1158
and so on):
1159
 
1160
     make -f Makefile.in gdb.tar.gz
1161
 
1162
This will properly configure, clean, rebuild any files that are
1163
distributed pre-built (e.g. `c-exp.tab.c' or `refcard.ps'), and will
1164
then make a tarfile.  (If the top level directory has already been
1165
configured, you can just do `make gdb.tar.gz' instead.)
1166
 
1167
   This procedure requires:
1168
 
1169
   * symbolic links;
1170
 
1171
   * `makeinfo' (texinfo2 level);
1172
 
1173
   * TeX;
1174
 
1175
   * `dvips';
1176
 
1177
   * `yacc' or `bison'.
1178
 
1179
... and the usual slew of utilities (`sed', `tar', etc.).
1180
 
1181
TEMPORARY RELEASE PROCEDURE FOR DOCUMENTATION
1182
---------------------------------------------
1183
 
1184
   `gdb.texinfo' is currently marked up using the texinfo-2 macros,
1185
which are not yet a default for anything (but we have to start using
1186
them sometime).
1187
 
1188
   For making paper, the only thing this implies is the right
1189
generation of `texinfo.tex' needs to be included in the distribution.
1190
 
1191
   For making info files, however, rather than duplicating the texinfo2
1192
distribution, generate `gdb-all.texinfo' locally, and include the files
1193
`gdb.info*' in the distribution.  Note the plural; `makeinfo' will
1194
split the document into one overall file and five or so included files.
1195
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.