14.3. Type and range checking
Warning: In this release, the gdb commands for type and range
checking are included, but they do not yet have any effect. This
section documents the intended facilities.
Some languages are designed to guard you against making seemingly common
errors through a series of compile- and run-time checks. These include
checking the type of arguments to functions and operators, and making
sure mathematical overflows are caught at run time. Checks such as
these help to ensure a program's correctness once it has been compiled
by eliminating type mismatches, and providing active checks for range
errors when your program is running.
gdb can check for conditions like the above if you wish.
Although gdb does not check the statements in your program, it
can check expressions entered directly into gdb for evaluation via
the print command, for example. As with the working language,
gdb can also decide whether or not to check automatically based on
your program's source language.
Refer to Section 14.4 Supported languages,
for the default settings of supported languages.
14.3.1. An overview of type checking
Some languages, such as Modula-2, are strongly typed, meaning that the
arguments to operators and functions have to be of the correct type,
otherwise an error occurs. These checks prevent type mismatch
errors from ever causing any run-time problems. For example,
1 + 2 => 3
but error--> 1 + 2.3 |
The second example fails because the CARDINAL 1 is not
type-compatible with the REAL 2.3.
For the expressions you use in gdb commands, you can tell the
gdb type checker to skip checking;
to treat any mismatches as errors and abandon the expression;
or to only issue warnings when type mismatches occur,
but evaluate the expression anyway. When you choose the last of
these, gdb evaluates expressions like the second example above, but
also issues a warning.
Even if you turn type checking off, there may be other reasons
related to type that prevent gdb from evaluating an expression.
For instance, gdb does not know how to add an int and
a struct foo. These particular type errors have nothing to do
with the language in use, and usually arise from expressions, such as
the one described above, which make little sense to evaluate anyway.
Each language defines to what degree it is strict about type. For
instance, both Modula-2 and C require the arguments to arithmetical
operators to be numbers. In C, enumerated types and pointers can be
represented as numbers, so that they are valid arguments to mathematical
operators.
Refer to Section 14.4 Supported languages, for further
details on specific languages.
gdb provides some additional commands for controlling the type checker:
- set check type auto
Set type checking on or off based on the current working language. Refer to Section 14.4 Supported languages, for the default settings for
each language.
- set check type on, set check type off
Set type checking on or off, overriding the default setting for the
current working language. Issue a warning if the setting does not
match the language default. If any type mismatches occur in
evaluating an expression while type checking is on, gdb prints a
message and aborts evaluation of the expression.
- set check type warn
Cause the type checker to issue warnings, but to always attempt to
evaluate the expression. Evaluating the expression may still
be impossible for other reasons. For example, gdb cannot add
numbers and structures.
- show type
Show the current setting of the type checker, and whether or not gdb
is setting it automatically.
14.3.2. An overview of range checking
In some languages (such as Modula-2), it is an error to exceed the
bounds of a type; this is enforced with run-time checks. Such range
checking is meant to ensure program correctness by making sure
computations do not overflow, or indices on an array element access do
not exceed the bounds of the array.
For expressions you use in gdb commands, you can tell
gdb to treat range errors in one of three ways: ignore them,
always treat them as errors and abandon the expression, or issue
warnings but evaluate the expression anyway.
A range error can result from numerical overflow, from exceeding an
array index bound, or when you type a constant that is not a member
of any type. Some languages, however, do not treat overflows as an
error. In many implementations of C, mathematical overflow causes the
result to "wrap around" to lower values--for example, if m is
the largest integer value, and s is the smallest, then
This, too, is specific to individual languages, and in some cases
specific to individual compilers or machines.
Refer to Section 14.4 Supported languages, for further details on specific languages.
gdb provides some additional commands for controlling the range checker:
- set check range auto
Set range checking on or off based on the current working language. Refer to Section 14.4 Supported languages, for the default settings for
each language.
- set check range on, set check range off
Set range checking on or off, overriding the default setting for the
current working language. A warning is issued if the setting does not
match the language default. If a range error occurs and range checking is on,
then a message is printed and evaluation of the expression is aborted.
- set check range warn
Output messages when the gdb range checker detects a range error,
but attempt to evaluate the expression anyway. Evaluating the
expression may still be impossible for other reasons, such as accessing
memory that the process does not own (a typical example from many Unix
systems).
- show range
Show the current setting of the range checker, and whether or not it is
being set automatically by gdb.