gdb_12.html   [plain text]


<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.51
     from /mnt/apple/gdb/source/gdb.apple/source/gdb/gdb/doc/gdb.texinfo on 23 November 1999 -->

<TITLE>Debugging with GDB - Altering Execution</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="gdb_1.html">first</A>, <A HREF="gdb_11.html">previous</A>, <A HREF="gdb_13.html">next</A>, <A HREF="gdb_21.html">last</A> section, <A HREF="gdb_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC102" HREF="gdb_toc.html#TOC102">Altering Execution</A></H1>

<P>
Once you think you have found an error in your program, you might want to
find out for certain whether correcting the apparent error would lead to
correct results in the rest of the run.  You can find the answer by
experiment, using the GDB features for altering execution of the
program.

</P>
<P>
For example, you can store new values into variables or memory
locations, give your program a signal, restart it at a different
address, or even return prematurely from a function.

</P>



<H2><A NAME="SEC103" HREF="gdb_toc.html#TOC103">Assignment to variables</A></H2>

<P>
<A NAME="IDX438"></A>
<A NAME="IDX439"></A>
To alter the value of a variable, evaluate an assignment expression.
See section <A HREF="gdb_9.html#SEC52">Expressions</A>.  For example,

</P>

<PRE>
print x=4
</PRE>

<P>
stores the value 4 into the variable <CODE>x</CODE>, and then prints the
value of the assignment expression (which is 4).  
See section <A HREF="gdb_10.html#SEC63">Using GDB with Different Languages</A>, for more
information on operators in supported languages.

</P>
<P>
<A NAME="IDX440"></A>
<A NAME="IDX441"></A>
If you are not interested in seeing the value of the assignment, use the
<CODE>set</CODE> command instead of the <CODE>print</CODE> command.  <CODE>set</CODE> is
really the same as <CODE>print</CODE> except that the expression's value is
not printed and is not put in the value history (see section <A HREF="gdb_9.html#SEC59">Value history</A>).  The expression is evaluated only for its effects.

</P>
<P>
If the beginning of the argument string of the <CODE>set</CODE> command
appears identical to a <CODE>set</CODE> subcommand, use the <CODE>set
variable</CODE> command instead of just <CODE>set</CODE>.  This command is identical
to <CODE>set</CODE> except for its lack of subcommands.  For example, if your
program has a variable <CODE>width</CODE>, you get an error if you try to set
a new value with just <SAMP>`set width=13'</SAMP>, because GDB has the
command <CODE>set width</CODE>:

</P>

<PRE>
(gdb) whatis width
type = double
(gdb) p width
$4 = 13
(gdb) set width=47
Invalid syntax in expression.
</PRE>

<P>
The invalid expression, of course, is <SAMP>`=47'</SAMP>.  In
order to actually set the program's variable <CODE>width</CODE>, use

</P>

<PRE>
(gdb) set var width=47
</PRE>

<P>
GDB allows more implicit conversions in assignments than C; you can
freely store an integer value into a pointer variable or vice versa,
and you can convert any structure to any other structure that is the
same length or shorter.

</P>
<P>
To store values into arbitrary places in memory, use the <SAMP>`{...}'</SAMP>
construct to generate a value of specified type at a specified address
(see section <A HREF="gdb_9.html#SEC52">Expressions</A>).  For example, <CODE>{int}0x83040</CODE> refers
to memory location <CODE>0x83040</CODE> as an integer (which implies a certain size
and representation in memory), and

</P>

<PRE>
set {int}0x83040 = 4
</PRE>

<P>
stores the value 4 into that memory location.

</P>


<H2><A NAME="SEC104" HREF="gdb_toc.html#TOC104">Continuing at a different address</A></H2>

<P>
Ordinarily, when you continue your program, you do so at the place where
it stopped, with the <CODE>continue</CODE> command.  You can instead continue at
an address of your own choosing, with the following commands:

</P>
<DL COMPACT>

<DT><CODE>jump <VAR>linespec</VAR></CODE>
<DD>
<A NAME="IDX442"></A>
 
Resume execution at line <VAR>linespec</VAR>.  Execution stops again
immediately if there is a breakpoint there.  See section <A HREF="gdb_8.html#SEC47">Printing source lines</A>, for a description of the different forms of
<VAR>linespec</VAR>.  It is common practice to use the <CODE>tbreak</CODE> command
in conjunction with <CODE>jump</CODE>.  See section <A HREF="gdb_6.html#SEC29">Setting breakpoints</A>.

The <CODE>jump</CODE> command does not change the current stack frame, or
the stack pointer, or the contents of any memory location or any
register other than the program counter.  If line <VAR>linespec</VAR> is in
a different function from the one currently executing, the results may
be bizarre if the two functions expect different patterns of arguments or
of local variables.  For this reason, the <CODE>jump</CODE> command requests
confirmation if the specified line is not in the function currently
executing.  However, even bizarre results are predictable if you are
well acquainted with the machine-language code of your program.

<DT><CODE>jump *<VAR>address</VAR></CODE>
<DD>
Resume execution at the instruction at address <VAR>address</VAR>.
</DL>

<P>
You can get much the same effect as the <CODE>jump</CODE> command by storing a
new value into the register <CODE>$pc</CODE>.  The difference is that this
does not start your program running; it only changes the address of where it
<EM>will</EM> run when you continue.  For example,

</P>

<PRE>
set $pc = 0x485
</PRE>

<P>
makes the next <CODE>continue</CODE> command or stepping command execute at
address <CODE>0x485</CODE>, rather than at the address where your program stopped.
See section <A HREF="gdb_6.html#SEC37">Continuing and stepping</A>.

</P>
<P>
The most common occasion to use the <CODE>jump</CODE> command is to back
up--perhaps with more breakpoints set--over a portion of a program
that has already executed, in order to examine its execution in more
detail.

</P>


<H2><A NAME="SEC105" HREF="gdb_toc.html#TOC105">Giving your program a signal</A></H2>

<DL COMPACT>

<DT><CODE>signal <VAR>signal</VAR></CODE>
<DD>
<A NAME="IDX443"></A>
 
Resume execution where your program stopped, but immediately give it the
signal <VAR>signal</VAR>.  <VAR>signal</VAR> can be the name or the number of a
signal.  For example, on many systems <CODE>signal 2</CODE> and <CODE>signal
SIGINT</CODE> are both ways of sending an interrupt signal.

Alternatively, if <VAR>signal</VAR> is zero, continue execution without
giving a signal.  This is useful when your program stopped on account of
a signal and would ordinary see the signal when resumed with the
<CODE>continue</CODE> command; <SAMP>`signal 0'</SAMP> causes it to resume without a
signal.

<CODE>signal</CODE> does not repeat when you press <KBD>RET</KBD> a second time
after executing the command.
</DL>

<P>
Invoking the <CODE>signal</CODE> command is not the same as invoking the
<CODE>kill</CODE> utility from the shell.  Sending a signal with <CODE>kill</CODE>
causes GDB to decide what to do with the signal depending on
the signal handling tables (see section <A HREF="gdb_6.html#SEC38">Signals</A>).  The <CODE>signal</CODE> command
passes the signal directly to your program.

</P>



<H2><A NAME="SEC106" HREF="gdb_toc.html#TOC106">Returning from a function</A></H2>

<DL COMPACT>

<DT><CODE>return</CODE>
<DD>
<A NAME="IDX444"></A>
 <A NAME="IDX445"></A>
 
<DT><CODE>return <VAR>expression</VAR></CODE>
<DD>
You can cancel execution of a function call with the <CODE>return</CODE>
command.  If you give an
<VAR>expression</VAR> argument, its value is used as the function's return
value.
</DL>

<P>
When you use <CODE>return</CODE>, GDB discards the selected stack frame
(and all frames within it).  You can think of this as making the
discarded frame return prematurely.  If you wish to specify a value to
be returned, give that value as the argument to <CODE>return</CODE>.

</P>
<P>
This pops the selected stack frame (see section <A HREF="gdb_7.html#SEC43">Selecting a frame</A>), and any other frames inside of it, leaving its caller as the
innermost remaining frame.  That frame becomes selected.  The
specified value is stored in the registers used for returning values
of functions.

</P>
<P>
The <CODE>return</CODE> command does not resume execution; it leaves the
program stopped in the state that would exist if the function had just
returned.  In contrast, the <CODE>finish</CODE> command (see section <A HREF="gdb_6.html#SEC37">Continuing and stepping</A>) resumes execution until the
selected stack frame returns naturally.

</P>


<H2><A NAME="SEC107" HREF="gdb_toc.html#TOC107">Calling program functions</A></H2>

<P>
<A NAME="IDX446"></A>
<A NAME="IDX447"></A>
<DL COMPACT>

<DT><CODE>call <VAR>expr</VAR></CODE>
<DD>
Evaluate the expression <VAR>expr</VAR> without displaying <CODE>void</CODE>
returned values.
</DL>

<P>
You can use this variant of the <CODE>print</CODE> command if you want to
execute a function from your program, but without cluttering the output
with <CODE>void</CODE> returned values.  If the result is not void, it 
is printed and saved in the value history. 

</P>
<P>
For the A29K, a user-controlled variable <CODE>call_scratch_address</CODE>,
specifies the location of a scratch area to be used when GDB
calls a function in the target.  This is necessary because the usual
method of putting the scratch area on the stack does not work in systems
that have separate instruction and data spaces.

</P>


<H2><A NAME="SEC108" HREF="gdb_toc.html#TOC108">Patching programs</A></H2>

<P>
<A NAME="IDX448"></A>
<A NAME="IDX449"></A>
<A NAME="IDX450"></A>

</P>
<P>
By default, GDB opens the file containing your program's
executable code (or the corefile) read-only.  This prevents accidental
alterations to machine code; but it also prevents you from intentionally
patching your program's binary.

</P>
<P>
If you'd like to be able to patch the binary, you can specify that
explicitly with the <CODE>set write</CODE> command.  For example, you might
want to turn on internal debugging flags, or even to make emergency
repairs.

</P>
<DL COMPACT>

<DT><CODE>set write on</CODE>
<DD>
<A NAME="IDX451"></A>
 
<DT><CODE>set write off</CODE>
<DD>
If you specify <SAMP>`set write on'</SAMP>, GDB opens executable and
core files for both reading and writing; if you specify <SAMP>`set write
off'</SAMP> (the default), GDB opens them read-only.

If you have already loaded a file, you must load it again (using the
<CODE>exec-file</CODE> or <CODE>core-file</CODE> command) after changing <CODE>set
write</CODE>, for your new setting to take effect.

<DT><CODE>show write</CODE>
<DD>
<A NAME="IDX452"></A>
Display whether executable files and core files are opened for writing
as well as reading.
</DL>

<P><HR><P>
Go to the <A HREF="gdb_1.html">first</A>, <A HREF="gdb_11.html">previous</A>, <A HREF="gdb_13.html">next</A>, <A HREF="gdb_21.html">last</A> section, <A HREF="gdb_toc.html">table of contents</A>.
</BODY>
</HTML>