Go to the first, previous, next, last section, table of contents.
Miscellaneous gdb commands. L command: Use gdb command l or list to print the source code in the debug mode. Use l line-number to view a specific line number (or) l function to view a specific function. Bt: backtrack – Print backtrace of all stack frames, or innermost COUNT frames. Help – View help for a particular gdb topic — help TOPICNAME. Apr 25, 2021 GDB: The GNU Project Debugger bugs GDB Maintainers contributing current git documentation download home irc links mailing lists news schedule song wiki.
The usual way to examine data in your program is with the print
command (abbreviated p
), or its synonym inspect
. Itevaluates and prints the value of an expression of the language yourprogram is written in (see section Using GDB with Different Languages).
- See full list on cs.cmu.edu.
- GNU or GDB debugger is an application for finding out how your C or C program runs or for analyzing the moment the program crashes. You can perform many useful tasks with GDB: run the program, stop the program under specific conditions, analyze the situation, make modifications, and test new changes.
- (gdb) f #0 main (argc=4, argv=0xf7fffae8) at m4.c:206 206 expandinput; (gdb) until 195 for (; argc 0; NEXTARG) This happened because, for execution efficiency, the compiler had generated code for the loop closure test at the end, rather than the start, of the loop-even though the test in a C for -loop is written before the body of the.
- , wheref is a letter specifying the format; see section Output formats.
print
print /f
- If you omit expr, GDB displays the last value again (from thevalue history; see section Value history). This allows you toconveniently inspect the same value in an alternative format.
A more low-level way of examining data is with the x
command.It examines data in memory at a specified address and prints it in aspecified format. See section Examining memory.
If you are interested in information about types, or about how thefields of a struct or a class are declared, use the ptype exp
command rather than print
. See section Examining the Symbol Table.
- Expressions: Expressions
- Variables: Program variables
- Arrays: Artificial arrays
- Output Formats: Output formats
- Memory: Examining memory
- Auto Display: Automatic display
- Print Settings: Print settings
- Value History: Value history
- Convenience Vars: Convenience variables
- Registers: Registers
- Floating Point Hardware: Floating point hardware
- Memory Region Attributes: Memory region attributes
Expressions
print
and many other GDB commands accept an expression andcompute its value. Any kind of constant, variable or operator definedby the programming language you are using is valid in an expression inGDB. This includes conditional expressions, function calls, castsand string constants. It unfortunately does not include symbols definedby preprocessor #define
commands.
GDB supports array constants in expressions input bythe user. The syntax is {element, element...}. For example,you can use the command print {1, 2, 3}
to build up an array inmemory that is malloc
ed in the target program.
Because C is so widespread, most of the expressions shown in examples inthis manual are in C. See section Using GDB with Different Languages, for information on how to use expressions in otherlanguages.
In this section, we discuss operators that you can use in GDBexpressions regardless of your programming language.
Casts are supported in all languages, not just in C, because it is souseful to cast a number into a pointer in order to examine a structureat that address in memory.
GDB supports these operators, in addition to those commonto programming languages:
- is a binary operator for treating parts of memory as arrays.See section Artificial arrays, for more information.
::
- `::' allows you to specify a variable in terms of the file orfunction where it is defined. See section Program variables.
{type} addr
- Refers to an object of type type stored at address addr inmemory. addr may be any expression whose value is an integer orpointer (but parentheses are required around binary operators, just as ina cast). This construct is allowed regardless of what kind of data isnormally supposed to reside at addr.
Program variables
The most common kind of expression to use is the name of a variablein your program.
Variables in expressions are understood in the selected stack frame(see section Selecting a frame); they must be either:
- global (or file-static)
or
- visible according to the scope rules of theprogramming language from the point of execution in that frame
This means that in the function
you can examine and use the variable a
whenever your program isexecuting within the function foo
, but you can only use orexamine the variable b
while your program is executing insidethe block where b
is declared.
There is an exception: you can refer to a variable or function whosescope is a single source file even if the current execution point is notin this file. But it is possible to have more than one such variable orfunction with the same name (in different source files). If thathappens, referring to that name has unpredictable effects. If you wish,you can specify a static variable in a particular function or file,using the colon-colon notation:
Here file or function is the name of the context for thestatic variable. In the case of file names, you can use quotes tomake sure GDB parses the file name as a single word--for example,to print a global value of x
defined in `f2.c':
This use of `::' is very rarely in conflict with the very similaruse of the same notation in C++. GDB also supports use of the C++scope resolution operator in GDB expressions.
Warning: Occasionally, a local variable may appear to have thewrong value at certain points in a function--just after entry to a newscope, and just before exit.
You may see this problem when you are stepping by machine instructions.This is because, on most machines, it takes more than one instruction toset up a stack frame (including local variable definitions); if you arestepping by machine instructions, variables may appear to have the wrongvalues until the stack frame is completely built. On exit, it usuallyalso takes more than one machine instruction to destroy a stack frame;after you begin stepping through that group of instructions, localvariable definitions may be gone.
This may also happen when the compiler does significant optimizations.To be sure of always seeing accurate values, turn off all optimizationwhen compiling.
Another possible effect of compiler optimizations is to optimizeunused variables out of existence, or assign variables to registers (asopposed to memory addresses). Depending on the support for such casesoffered by the debug info format used by the compiler, GDBmight not be able to display values for such local variables. If thathappens, GDB will print a message like this:
To solve such problems, either recompile without optimizations, or use adifferent debug info format, if the compiler supports several suchformats. For example, GCC, the GNU C/C++ compiler usuallysupports the `-gstabs' option. `-gstabs' produces debug infoin a format that is superior to formats such as COFF. You may be ableto use DWARF2 (`-gdwarf-2'), which is also an effective form fordebug info. See section `Options for Debugging Your Program or GNU CC' in Using GNU CC, for moreinformation.
Artificial arrays
It is often useful to print out several successive objects of thesame type in memory; a section of an array, or an array ofdynamically determined size for which only a pointer exists in theprogram.
You can do this by referring to a contiguous span of memory as anartificial array, using the binary operator `@'. The leftoperand of `@' should be the first element of the desired arrayand be an individual object. The right operand should be the desired lengthof the array. The result is an array value whose elements are all ofthe type of the left argument. The first element is actually the leftargument; the second element comes from bytes of memory immediatelyfollowing those that hold the first element, and so on. Here is anexample. If a program says
you can print the contents of array
with
The left operand of `@' must reside in memory. Array values madewith `@' in this way behave just like other arrays in terms ofsubscripting, and are coerced to pointers when used in expressions.Artificial arrays most often appear in expressions via the value history(see section Value history), after printing one out.
Another way to create an artificial array is to use a cast.This re-interprets a value as if it were an array.The value need not be in memory:
As a convenience, if you leave the array length out (as in`(type[])value') GDB calculates the size to fillthe value (as `sizeof(value)/sizeof(type)':
Sometimes the artificial array mechanism is not quite enough; inmoderately complex data structures, the elements of interest may notactually be adjacent--for example, if you are interested in the valuesof pointers in an array. One useful work-around in this situation isto use a convenience variable (see section Convenience variables) as a counter in an expression that prints the firstinteresting value, and then repeat that expression via RET. Forinstance, suppose you have an array dtab
of pointers tostructures, and you are interested in the values of a field fv
in each structure. Here is an example of what you might type:
Output formats
By default, GDB prints a value according to its data type. Sometimesthis is not what you want. For example, you might want to print a numberin hex, or a pointer in decimal. Or you might want to view data in memoryat a certain address as a character string or as an instruction. To dothese things, specify an output format when you print a value.
Gdb Compiler
The simplest use of output formats is to say how to print a valuealready computed. This is done by starting the arguments of theprint
command with a slash and a format letter. The formatletters supported are:
- stands for 'two'.(2)
a
- Print as an address, both absolute in hexadecimal and as an offset fromthe nearest preceding symbol. You can use this format used to discoverwhere (in what function) an unknown address is located:The command
info symbol 0x54320
yields similar results.See section Examining the Symbol Table. c
- Regard as an integer and print it as a character constant.
f
- Regard the bits of the value as a floating point number and printusing typical floating point syntax.
For example, to print the program counter in hex (see section Registers), type
Note that no space is required before the slash; this is because commandnames in GDB cannot contain a slash.
To reprint the last value in the value history with a different format,you can use the print
command with just a format and noexpression. For example, `p/x' reprints the last value in hex.
Examining memory
You can use the command x
(for 'examine') to examine memory inany of several formats, independently of your program's data types.
- .Several commands set convenient defaults for addr.
- u, the unit size
- The unit size is any of
- and`i' formats, the unit size is ignored and is normally not written.)
- addr, starting display address
- addr is the address where you want GDB to begin displayingmemory. The expression need not have a pointer value (though it may);it is always interpreted as an integer address of a byte of memory.See section Expressions, for more information on expressions. The default foraddr is usually just after the last address examined--but severalother commands also set the default address:
info breakpoints
(tothe address of the last breakpoint listed),info line
(to thestarting address of a line), andprint
(if you use it to displaya value from memory). disable display dnums...
- Disable the display of item numbers dnums. A disabled displayitem is not printed automatically, but is not forgotten. It may beenabled again later.
enable display dnums...
- Enable display of item numbers dnums. It becomes effective onceagain in auto display of its expression, until you specify otherwise.
display
- Display the current values of the expressions on the list, just as isdone when your program stops.
info display
- Print the list of expressions previously set up to displayautomatically, each one with its item number, but without showing thevalues. This includes disabled expressions, which are marked as such.It also includes expressions which would not be displayed right nowbecause they refer to automatic variables not currently available.
show print address
- Show whether or not addresses are to be printed.
set print max-symbolic-offset max-offset
- Tell GDB to only display the symbolic form of an address if theoffset between the closest earlier symbol and the address is less thanmax-offset. The default is 0, which tells GDBto always print the symbolic form of an address if any symbol precedes it.
show print max-symbolic-offset
- Ask how large the maximum offset is that GDB prints in asymbolic address.
set print sevenbit-strings off
- Print full eight-bit characters. This allows the use of moreinternational character sets, and is the default.
show print sevenbit-strings
- Show whether or not GDB is printing only seven-bit characters.
set print union on
- Tell GDB to print unions which are contained in structures. Thisis the default setting.
set print union off
- Tell GDB not to print unions which are contained in structures.
show print union
- Ask GDB whether or not it will print unions which are contained instructures.For example, given the declarationswith
set print union on
in effect `p foo' would printand withset print union off
in effect it would print show print demangle
- Show whether C++ names are printed in mangled or demangled form.
set print asm-demangle
set print asm-demangle on
- Print C++ names in their source form rather than their mangled form, evenin assembler code printouts such as instruction disassemblies.The default is off.
show print asm-demangle
- Show whether C++ names in assembly listings are printed in mangledor demangled form.
set demangle-style style
- Choose among several encoding schemes used by different compilers torepresent C++ names. The choices for style are currently:
- before the value; here num is thehistory number.
To refer to any previous value, use `$' followed by the value'shistory number. The way
print
labels its output is designed toremind you of this. Just$
refers to the most recent value inthe history, and$$
refers to the value before that.$$n
refers to the nth value from the end;$$2
is the value just prior to$$
,$$1
is equivalent to$$
, and$$0
is equivalent to$
.For example, suppose you have just printed a pointer to a structure andwant to see the contents of the structure. It suffices to type
If you have a chain of structures where the component
next
pointsto the next one, you can print the contents of the next one with this:You can print successive links in the chain by repeating thiscommand--which you can do by just typing RET.
Note that the history records values, not expressions. If the value of
x
is 4 and you type these commands:then the value recorded in the value history by the
print
commandremains 4 even though the value ofx
has changed.- repeated ten times, except that
showvalues
does not change the history. show values n
- Print ten history values centered on history item number n.
show values +
- Print ten history values just after the values last printed. If no morevalues are available,
show values +
produces no display. $_
- The variable
$_
is automatically set by thex
command tothe last address examined (see section Examining memory). Othercommands which provide a default address forx
to examine alsoset$_
to that address; these commands includeinfo line
andinfo breakpoint
. The type of$_
isvoid *
except when set by thex
command, in which case it is a pointerto the type of$__
. $__
- The variable
$__
is automatically set by thex
commandto the value found in the last address examined. Its type is chosento match the format in which the data was printed. $_exitcode
- The variable
$_exitcode
is automatically set to the exit code whenthe program being debugged terminates. - Memory Region Number
- Enabled or Disabled.
- Enabled memory regions are marked with `y'. Disabled memory regions are marked with `n'.
- Lo Address
- The address defining the inclusive lower bound of the memory region.
- Hi Address
- The address defining the exclusive upper bound of the memory region.
- Attributes
- The list of attributes set for this memory region.
- (null-terminated string), or `i' (machine instruction).The default is `x' (hexadecimal) initially.The default changes each time you use either
x
or print
.For example, `x/3uh 0x54320' is a request to display three halfwords(h
) of memory, formatted as unsigned decimal integers (`u'),starting at address 0x54320
. `x/4xw $sp' prints the fourwords (`w') of memory above the stack pointer (here, `$sp';see section Registers) in hexadecimal (`x').
Since the letters indicating unit sizes are all distinct from theletters specifying output formats, you do not have to remember whetherunit size or format comes first; either order works. The outputspecifications `4xw' and `4wx' mean exactly the same thing.(However, the count n must come first; `wx4' does not work.)
Even though the unit size u is ignored for the formats `s'and `i', you might still want to use a count n; for example,`3i' specifies that you want to see three machine instructions,including any operands. The command disassemble
gives analternative way of inspecting machine instructions; see section Source and machine code.
All the defaults for the arguments to x
are designed to make iteasy to continue scanning memory with minimal specifications each timeyou use x
. For example, after you have inspected three machineinstructions with `x/3i addr', you can inspect the next sevenwith just `x/7'. If you use RET to repeat the x
command,the repeat count n is used again; the other arguments default asfor successive uses of x
.
The addresses and contents printed by the x
command are not savedin the value history because there is often too much of them and theywould get in the way. Instead, GDB makes these values available forsubsequent use in expressions as values of the convenience variables$_
and $__
. After an x
command, the last addressexamined is available for use in expressions in the convenience variable$_
. The contents of that address, as examined, are available inthe convenience variable $__
.
If the x
command has a repeat count, the address and contents savedare from the last memory unit printed; this is not the same as the lastaddress printed if several units were printed on the last line of output.
Automatic display
If you find that you want to print the value of an expression frequently(to see how it changes), you might want to add it to the automaticdisplay list so that GDB prints its value each time your program stops.Each expression added to the list is given a number to identify it;to remove an expression from the list, you specify that number.The automatic display looks like this:
This display shows item numbers, expressions and their current values. As withdisplays you request manually using x
or print
, you canspecify the output format you prefer; in fact, display
decideswhether to use print
or x
depending on how elaborate yourformat specification is--it uses x
if you specify a unit size,or one of the two formats (`i' and `s') that are onlysupported by x
; otherwise it uses print
.
- or `s', or including a unit-size or anumber of units, add the expression addr as a memory address tobe examined each time your program stops. Examining means in effectdoing `x/fmtaddr'. See section Examining memory.
For example, `display/i $pc' can be helpful, to see the machineinstruction about to be executed each time execution stops (`$pc'is a common name for the program counter; see section Registers).
- .)
If a display expression refers to local variables, then it does not makesense outside the lexical context for which it was set up. Such anexpression is disabled when execution enters a context where one of itsvariables is not defined. For example, if you give the commanddisplay last_char
while inside a function with an argumentlast_char
, GDB displays this argument while your programcontinues to stop inside that function. When it stops elsewhere--wherethere is no variable last_char
---the display is disabledautomatically. The next time your program stops where last_char
is meaningful, you can enable the display expression once again.
Print settings
GDB provides the following ways to control how arrays, structures,and symbols are printed.
These settings are useful for debugging programs in any language:
- to eliminate all machinedependent displays from the GDB interface. For example, with
print address off
, you should get the same text for backtraces onall machines--whether or not they involve pointer arguments.When GDB prints a symbolic address, it normally prints theclosest earlier symbol plus an offset. If that symbol does not uniquelyidentify the address (for example, it is a name whose scope is a singlesource file), you may need to clarify. One way to do this is withinfo line
, for example `info line *0x4537'. Alternately,you can set GDB to print the source file and line number whenit prints a symbolic address:
If you have a pointer and you are not sure where it points, try`set print symbol-filename on'. Then you can determine the nameand source file location of the variable where it points, using`p/a pointer'. This interprets the address in symbolic form.For example, here GDB shows that a variable ptt
pointsat another variable t
, defined in `hi2.c':
Warning: For pointers that point to a local variable, `p/a'does not show the symbol name and filename of the referent, even withthe appropriate set print
options turned on.
Other settings control how different kinds of objects are printed:
These settings are of interest when debugging C++ programs:
Pressing RET to repeat show values n
has exactly thesame effect as `show values +'.
Convenience variables
GDB provides convenience variables that you can use withinGDB to hold on to a value and refer to it later. These variablesexist entirely within GDB; they are not part of your program, andsetting a convenience variable has no direct effect on further executionof your program. That is why you can use them freely.
Convenience variables are prefixed with `$'. Any name preceded by`$' can be used for a convenience variable, unless it is one ofthe predefined machine-specific register names (see section Registers).(Value history references, in contrast, are numbers precededby `$'. See section Value history.)
You can save a value in a convenience variable with an assignmentexpression, just as you would set a variable in your program.For example:
would save in $foo
the value contained in the object pointed to byobject_ptr
.
Using a convenience variable for the first time creates it, but itsvalue is void
until you assign a new value. You can alter thevalue with another assignment at any time.
Convenience variables have no fixed types. You can assign a conveniencevariable any type of value, including structures and arrays, even ifthat variable already has a value of a different type. The conveniencevariable, when used as an expression, has the type of its current value.
On HP-UX systems, if you refer to a function or variable name thatbegins with a dollar sign, GDB searches for a user or systemname first, before it searches for a convenience variable.
Registers
You can refer to machine register contents, in expressions, as variableswith names starting with `$'. The names of registers are differentfor each machine; use info registers
to see the names used onyour machine.
- .
GDB has four 'standard' register names that are available (inexpressions) on most machines--whenever they do not conflict with anarchitecture's canonical mnemonics for registers. The register names$pc
and $sp
are used for the program counter register andthe stack pointer. $fp
is used for a register that contains apointer to the current stack frame, and $ps
is used for aregister that contains the processor status. For example,you could print the program counter in hex with
or print the instruction to be executed next with
or add four to the stack pointer(3) with
Whenever possible, these four standard register names are available onyour machine even though the machine has different canonical mnemonics,so long as there is no conflict. The info registers
commandshows the canonical names. For example, on the SPARC, inforegisters
displays the processor status register as $psr
but youcan also refer to it as $ps
; and on x86-based machines $ps
is an alias for the EFLAGS register.
GDB always considers the contents of an ordinary register as aninteger when the register is examined in this way. Some machines havespecial registers which can hold nothing but floating point; theseregisters are considered to have floating point values. There is no wayto refer to the contents of an ordinary register as floating point value(although you can print it as a floating point value with`print/f $regname').
Some registers have distinct 'raw' and 'virtual' data formats. Thismeans that the data format in which the register contents are saved bythe operating system is not the same one that your program normallysees. For example, the registers of the 68881 floating pointcoprocessor are always saved in 'extended' (raw) format, but all Cprograms expect to work with 'double' (virtual) format. In suchcases, GDB normally works with the virtual format only (the formatthat makes sense for your program), but the info registers
commandprints the data in both formats.
Normally, register values are relative to the selected stack frame(see section Selecting a frame). This means that you get thevalue that the register would contain if all stack frames farther inwere exited and their saved registers restored. In order to see thetrue contents of hardware registers, you must select the innermostframe (with `frame 0').
However, GDB must deduce where registers are saved, from the machinecode generated by your compiler. If some registers are not saved, or ifGDB is unable to locate the saved registers, the selected stackframe makes no difference.
Floating point hardware
Depending on the configuration, GDB may be able to giveyou more information about the status of the floating point hardware.
- is supported onthe ARM and x86 machines.
Memory Region Attributes
Memory region attributes allow you to describe special handling required by regions of your target's memory. GDB uses attributes to determine whether to allow certain types of memory accesses; whether touse specific width accesses; and whether to cache target memory.
Defined memory regions can be individually enabled and disabled. When amemory region is disabled, GDB uses the default attributes whenaccessing memory in that region. Similarly, if no memory regions havebeen defined, GDB uses the default attributes when accessingall memory.
When a memory region is defined, it is given a number to identify it; to enable, disable, or remove a memory region, you specify that number.
Attributes
Memory Access Mode
The access mode attributes set whether GDB may make read orwrite accesses to a memory region.
While these attributes prevent GDB from performing invalidmemory accesses, they do nothing to prevent the target system, I/O DMA,etc. from accessing memory.
8
- Use 8 bit memory accesses.
16
- Use 16 bit memory accesses.
32
- Use 32 bit memory accesses.
64
- Use 64 bit memory accesses.
Data Cache
The data cache attributes set whether GDB will cache targetmemory. While this generally improves performance by reducing debugprotocol overhead, it can lead to incorrect results because GDBdoes not know about volatile variables or memory mapped deviceregisters.
- Gdba
- Supply Chain Analyst – New Brunswick, NJ – Analyze product delivery and supply chain process to identify and recommend changes. Test warehouse management systems. Research and analyze supply chain activities; develop and implement solutions to reduce supply chain costs and improve efficiencies; analyze and interpret business data. Associate’s degree in Business/Commerce or equivalent, 2 years experience. Send resume: 1 Home News Row, New Brunswick, NJ 08901.
- Sr. Scientist, Research & Development – Nashville, IL – Conduct chemical analysis and experiments to improve or develop new paint formulations. Develop, monitor and implement management operations systems. Manage paint production. Recommend changes to the chemistry of paints by identifying compounds and additives. Perform re-sampling and analysis. Develop new application through chemical variations in paint. Rework batches, color and quality monitoring.
Employer’s Name: GDB International, Inc.
Position & Job Description:
Eligibility: PhD in Chemistry, 1 year experience.
Gdb Cheat Sheet
3. Business Development Manager – New Brunswick, NJ – Research international paint market conditions and plan, develop and implement business strategy. Manage the marketing and company paint brand. Evaluate and design sales and marketing systems. Analyze company portfolio and identify new business opportunities internationally. Introduce new products to increase sales. Will travel internationally (mainly to China and India) to meet customers at least once a month.
Eligibility: Master’s degree in Business Administration, 1 year experience.
4. Sr. Scientist – Nashville, IL – Conduct chemical analysis and experiments to improve or develop new paint formulations. Introduce and implement paint formulation techniques. Recommend changes to the chemistry of paints by identifying compounds and additives. Review plant production trials and observe the quality improvement stages. Perform re-sampling and analysis. Develop new applications through chemical variations in paint. Rework batches, color and quality monitoring. Test paint using ASTM Standards.
Gdb Debugger
Eligibility: Master’s degree in Chemical Technology and 1 year experience or Bachelor’s degree plus 5 years experience.
Gdbus
APPLY TO:
GDB International, Inc.
Attn: HR/Recruitment
One Home News Row
New Brunswick, NJ 08901
Comments are closed.