gdb_5.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 - Running Programs Under GDB</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="gdb_1.html">first</A>, <A HREF="gdb_4.html">previous</A>, <A HREF="gdb_6.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="SEC15" HREF="gdb_toc.html#TOC15">Running Programs Under GDB</A></H1>

<P>
When you run a program under GDB, you must first generate
debugging information when you compile it.

</P>
<P>
You may start GDB with its arguments, if any, in an environment
of your choice.  If you are doing native debugging, you may redirect
your program's input and output, debug an already running process, or
kill a child process.

</P>



<H2><A NAME="SEC16" HREF="gdb_toc.html#TOC16">Compiling for debugging</A></H2>

<P>
In order to debug a program effectively, you need to generate
debugging information when you compile it.  This debugging information
is stored in the object file; it describes the data type of each
variable or function and the correspondence between source line numbers
and addresses in the executable code.

</P>
<P>
To request debugging information, specify the <SAMP>`-g'</SAMP> option when you run
the compiler.

</P>
<P>
Many C compilers are unable to handle the <SAMP>`-g'</SAMP> and <SAMP>`-O'</SAMP>
options together.  Using those compilers, you cannot generate optimized
executables containing debugging information.

</P>
<P>
GCC, the GNU C compiler, supports <SAMP>`-g'</SAMP> with or without
<SAMP>`-O'</SAMP>, making it possible to debug optimized code.  We recommend
that you <EM>always</EM> use <SAMP>`-g'</SAMP> whenever you compile a program.
You may think your program is correct, but there is no sense in pushing
your luck.

</P>
<P>
<A NAME="IDX37"></A>
<A NAME="IDX38"></A>
When you debug a program compiled with <SAMP>`-g -O'</SAMP>, remember that the
optimizer is rearranging your code; the debugger shows you what is
really there.  Do not be too surprised when the execution path does not
exactly match your source file!  An extreme example: if you define a
variable, but never use it, GDB never sees that
variable--because the compiler optimizes it out of existence.

</P>
<P>
Some things do not work as well with <SAMP>`-g -O'</SAMP> as with just
<SAMP>`-g'</SAMP>, particularly on machines with instruction scheduling.  If in
doubt, recompile with <SAMP>`-g'</SAMP> alone, and if this fixes the problem,
please report it to us as a bug (including a test case!).

</P>
<P>
Older versions of the GNU C compiler permitted a variant option
<SAMP>`-gg'</SAMP> for debugging information.  GDB no longer supports this
format; if your GNU C compiler has this option, do not use it.

</P>


<H2><A NAME="SEC17" HREF="gdb_toc.html#TOC17">Starting your program</A></H2>
<P>
<A NAME="IDX39"></A>
<A NAME="IDX40"></A>

</P>
<DL COMPACT>

<DT><CODE>run</CODE>
<DD>
<A NAME="IDX41"></A>
 
<DT><CODE>r</CODE>
<DD>
Use the <CODE>run</CODE> command to start your program under GDB.
You must first specify the program name (except on VxWorks) with an
argument to GDB (see section <A HREF="gdb_3.html#SEC5">Getting In and Out of GDB</A>), or by using the <CODE>file</CODE> or <CODE>exec-file</CODE> command
(see section <A HREF="gdb_13.html#SEC110">Commands to specify files</A>).

</DL>

<P>
If you are running your program in an execution environment that
supports processes, <CODE>run</CODE> creates an inferior process and makes
that process run your program.  (In environments without processes,
<CODE>run</CODE> jumps to the start of your program.)

</P>
<P>
The execution of a program is affected by certain information it
receives from its superior.  GDB provides ways to specify this
information, which you must do <EM>before</EM> starting your program.  (You
can change it after starting your program, but such changes only affect
your program the next time you start it.)  This information may be
divided into four categories:

</P>
<DL COMPACT>

<DT>The <EM>arguments.</EM>
<DD>
Specify the arguments to give your program as the arguments of the
<CODE>run</CODE> command.  If a shell is available on your target, the shell
is used to pass the arguments, so that you may use normal conventions
(such as wildcard expansion or variable substitution) in describing
the arguments.
In Unix systems, you can control which shell is used with the
<CODE>SHELL</CODE> environment variable.
See section <A HREF="gdb_5.html#SEC18">Your program's arguments</A>.

<DT>The <EM>environment.</EM>
<DD>
Your program normally inherits its environment from GDB, but you can
use the GDB commands <CODE>set environment</CODE> and <CODE>unset
environment</CODE> to change parts of the environment that affect
your program.  See section <A HREF="gdb_5.html#SEC19">Your program's environment</A>.

<DT>The <EM>working directory.</EM>
<DD>
Your program inherits its working directory from GDB.  You can set
the GDB working directory with the <CODE>cd</CODE> command in GDB.
See section <A HREF="gdb_5.html#SEC20">Your program's working directory</A>.

<DT>The <EM>standard input and output.</EM>
<DD>
Your program normally uses the same device for standard input and
standard output as GDB is using.  You can redirect input and output
in the <CODE>run</CODE> command line, or you can use the <CODE>tty</CODE> command to
set a different device for your program.
See section <A HREF="gdb_5.html#SEC21">Your program's input and output</A>.

<A NAME="IDX42"></A>
<EM>Warning:</EM> While input and output redirection work, you cannot use
pipes to pass the output of the program you are debugging to another
program; if you attempt this, GDB is likely to wind up debugging the
wrong program.
</DL>

<P>
When you issue the <CODE>run</CODE> command, your program begins to execute
immediately.  See section <A HREF="gdb_6.html#SEC27">Stopping and Continuing</A>, for discussion
of how to arrange for your program to stop.  Once your program has
stopped, you may call functions in your program, using the <CODE>print</CODE>
or <CODE>call</CODE> commands.  See section <A HREF="gdb_9.html#SEC51">Examining Data</A>.

</P>
<P>
If the modification time of your symbol file has changed since the last
time GDB read its symbols, GDB discards its symbol
table, and reads it again.  When it does this, GDB tries to retain
your current breakpoints.

</P>


<H2><A NAME="SEC18" HREF="gdb_toc.html#TOC18">Your program's arguments</A></H2>

<P>
<A NAME="IDX43"></A>
The arguments to your program can be specified by the arguments of the
<CODE>run</CODE> command.  
They are passed to a shell, which expands wildcard characters and
performs redirection of I/O, and thence to your program.  Your
<CODE>SHELL</CODE> environment variable (if it exists) specifies what shell
GDB uses.  If you do not define <CODE>SHELL</CODE>, GDB uses
<CODE>/bin/sh</CODE>.

</P>
<P>
<CODE>run</CODE> with no arguments uses the same arguments used by the previous
<CODE>run</CODE>, or those set by the <CODE>set args</CODE> command.

</P>
<P>
<A NAME="IDX44"></A>
<DL COMPACT>

<DT><CODE>set args</CODE>
<DD>
Specify the arguments to be used the next time your program is run.  If
<CODE>set args</CODE> has no arguments, <CODE>run</CODE> executes your program
with no arguments.  Once you have run your program with arguments,
using <CODE>set args</CODE> before the next <CODE>run</CODE> is the only way to run
it again without arguments.

<A NAME="IDX45"></A>
<DT><CODE>show args</CODE>
<DD>
Show the arguments to give your program when it is started.
</DL>



<H2><A NAME="SEC19" HREF="gdb_toc.html#TOC19">Your program's environment</A></H2>

<P>
<A NAME="IDX46"></A>
The <STRONG>environment</STRONG> consists of a set of environment variables and
their values.  Environment variables conventionally record such things as
your user name, your home directory, your terminal type, and your search
path for programs to run.  Usually you set up environment variables with
the shell and they are inherited by all the other programs you run.  When
debugging, it can be useful to try running your program with a modified
environment without having to start GDB over again.

</P>
<DL COMPACT>

<DT><CODE>path <VAR>directory</VAR></CODE>
<DD>
<A NAME="IDX47"></A>
 
Add <VAR>directory</VAR> to the front of the <CODE>PATH</CODE> environment variable
(the search path for executables), for both GDB and your program.
You may specify several directory names, separated by <SAMP>`:'</SAMP> or
whitespace.  If <VAR>directory</VAR> is already in the path, it is moved to
the front, so it is searched sooner.

You can use the string <SAMP>`$cwd'</SAMP> to refer to whatever is the current
working directory at the time GDB searches the path.  If you
use <SAMP>`.'</SAMP> instead, it refers to the directory where you executed the
<CODE>path</CODE> command.  GDB replaces <SAMP>`.'</SAMP> in the
<VAR>directory</VAR> argument (with the current path) before adding
<VAR>directory</VAR> to the search path.

<A NAME="IDX48"></A>
<DT><CODE>show paths</CODE>
<DD>
Display the list of search paths for executables (the <CODE>PATH</CODE>
environment variable).

<A NAME="IDX49"></A>
<DT><CODE>show environment [<VAR>varname</VAR>]</CODE>
<DD>
Print the value of environment variable <VAR>varname</VAR> to be given to
your program when it starts.  If you do not supply <VAR>varname</VAR>,
print the names and values of all environment variables to be given to
your program.  You can abbreviate <CODE>environment</CODE> as <CODE>env</CODE>.

<A NAME="IDX50"></A>
<DT><CODE>set environment <VAR>varname</VAR> [=] <VAR>value</VAR></CODE>
<DD>
Set environment variable <VAR>varname</VAR> to <VAR>value</VAR>.  The value
changes for your program only, not for GDB itself.  <VAR>value</VAR> may
be any string; the values of environment variables are just strings, and
any interpretation is supplied by your program itself.  The <VAR>value</VAR>
parameter is optional; if it is eliminated, the variable is set to a
null value.

For example, this command:


<PRE>
set env USER = foo
</PRE>

tells a Unix program, when subsequently run, that its user is named
<SAMP>`foo'</SAMP>.  (The spaces around <SAMP>`='</SAMP> are used for clarity here; they
are not actually required.)

<A NAME="IDX51"></A>
<DT><CODE>unset environment <VAR>varname</VAR></CODE>
<DD>
Remove variable <VAR>varname</VAR> from the environment to be passed to your
program.  This is different from <SAMP>`set env <VAR>varname</VAR> ='</SAMP>;
<CODE>unset environment</CODE> removes the variable from the environment,
rather than assigning it an empty value.
</DL>

<P>
<EM>Warning:</EM> GDB runs your program using the shell indicated
by your <CODE>SHELL</CODE> environment variable if it exists (or
<CODE>/bin/sh</CODE> if not).  If your <CODE>SHELL</CODE> variable names a shell
that runs an initialization file--such as <TT>`.cshrc'</TT> for C-shell, or
<TT>`.bashrc'</TT> for BASH--any variables you set in that file affect
your program.  You may wish to move setting of environment variables to
files that are only run when you sign on, such as <TT>`.login'</TT> or
<TT>`.profile'</TT>.

</P>


<H2><A NAME="SEC20" HREF="gdb_toc.html#TOC20">Your program's working directory</A></H2>

<P>
<A NAME="IDX52"></A>
Each time you start your program with <CODE>run</CODE>, it inherits its
working directory from the current working directory of GDB.
The GDB working directory is initially whatever it inherited
from its parent process (typically the shell), but you can specify a new
working directory in GDB with the <CODE>cd</CODE> command.

</P>
<P>
The GDB working directory also serves as a default for the commands
that specify files for GDB to operate on.  See section <A HREF="gdb_13.html#SEC110">Commands to specify files</A>.

</P>
<DL COMPACT>

<DT><CODE>cd <VAR>directory</VAR></CODE>
<DD>
<A NAME="IDX53"></A>
 
Set the GDB working directory to <VAR>directory</VAR>.

<A NAME="IDX54"></A>
<DT><CODE>pwd</CODE>
<DD>
Print the GDB working directory.
</DL>



<H2><A NAME="SEC21" HREF="gdb_toc.html#TOC21">Your program's input and output</A></H2>

<P>
<A NAME="IDX55"></A>
<A NAME="IDX56"></A>
<A NAME="IDX57"></A>
By default, the program you run under GDB does input and output to
the same terminal that GDB uses.  GDB switches the terminal 
to its own terminal modes to interact with you, but it records the terminal
modes your program was using and switches back to them when you continue
running your program.

</P>
<DL COMPACT>

<DT><CODE>info terminal</CODE>
<DD>
<A NAME="IDX58"></A>
 
Displays information recorded by GDB about the terminal modes your
program is using.
</DL>

<P>
You can redirect your program's input and/or output using shell
redirection with the <CODE>run</CODE> command.  For example,

</P>

<PRE>
run &#62; outfile
</PRE>

<P>
starts your program, diverting its output to the file <TT>`outfile'</TT>.

</P>
<P>
<A NAME="IDX59"></A>
<A NAME="IDX60"></A>
Another way to specify where your program should do input and output is
with the <CODE>tty</CODE> command.  This command accepts a file name as
argument, and causes this file to be the default for future <CODE>run</CODE>
commands.  It also resets the controlling terminal for the child
process, for future <CODE>run</CODE> commands.  For example,

</P>

<PRE>
tty /dev/ttyb
</PRE>

<P>
directs that processes started with subsequent <CODE>run</CODE> commands
default to do input and output on the terminal <TT>`/dev/ttyb'</TT> and have
that as their controlling terminal.

</P>
<P>
An explicit redirection in <CODE>run</CODE> overrides the <CODE>tty</CODE> command's
effect on the input/output device, but not its effect on the controlling
terminal.

</P>
<P>
When you use the <CODE>tty</CODE> command or redirect input in the <CODE>run</CODE>
command, only the input <EM>for your program</EM> is affected.  The input
for GDB still comes from your terminal.

</P>


<H2><A NAME="SEC22" HREF="gdb_toc.html#TOC22">Debugging an already-running process</A></H2>
<P>
<A NAME="IDX61"></A>
<A NAME="IDX62"></A>

</P>
<DL COMPACT>

<DT><CODE>attach <VAR>process-id</VAR></CODE>
<DD>
This command attaches to a running process--one that was started
outside GDB.  (<CODE>info files</CODE> shows your active
targets.)  The command takes as argument a process ID.  The usual way to
find out the process-id of a Unix process is with the <CODE>ps</CODE> utility,
or with the <SAMP>`jobs -l'</SAMP> shell command.

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

<P>
To use <CODE>attach</CODE>, your program must be running in an environment
which supports processes; for example, <CODE>attach</CODE> does not work for
programs on bare-board targets that lack an operating system.  You must
also have permission to send the process a signal.

</P>
<P>
When you use <CODE>attach</CODE>, the debugger finds the program running in
the process first by looking in the current working directory, then (if
the program is not found) by using the source file search path
(see section <A HREF="gdb_8.html#SEC49">Specifying source directories</A>).  You can also use
the <CODE>file</CODE> command to load the program.  See section <A HREF="gdb_13.html#SEC110">Commands to specify files</A>.

</P>
<P>
The first thing GDB does after arranging to debug the specified
process is to stop it.  You can examine and modify an attached process
with all the GDB commands that are ordinarily available when you start
processes with <CODE>run</CODE>.  You can insert breakpoints; you can step and
continue; you can modify storage.  If you would rather the process
continue running, you may use the <CODE>continue</CODE> command after
attaching GDB to the process.

</P>
<DL COMPACT>

<DT><CODE>detach</CODE>
<DD>
<A NAME="IDX63"></A>
 
When you have finished debugging the attached process, you can use the
<CODE>detach</CODE> command to release it from GDB control.  Detaching
the process continues its execution.  After the <CODE>detach</CODE> command,
that process and GDB become completely independent once more, and you
are ready to <CODE>attach</CODE> another process or start one with <CODE>run</CODE>.
<CODE>detach</CODE> does not repeat if you press <KBD>RET</KBD> again after
executing the command.
</DL>

<P>
If you exit GDB or use the <CODE>run</CODE> command while you have an
attached process, you kill that process.  By default, GDB asks
for confirmation if you try to do either of these things; you can
control whether or not you need to confirm by using the <CODE>set
confirm</CODE> command (see section <A HREF="gdb_15.html#SEC155">Optional warnings and messages</A>).

</P>


<H2><A NAME="SEC23" HREF="gdb_toc.html#TOC23">Killing the child process</A></H2>

<DL COMPACT>

<DT><CODE>kill</CODE>
<DD>
<A NAME="IDX64"></A>
 
Kill the child process in which your program is running under GDB.
</DL>

<P>
This command is useful if you wish to debug a core dump instead of a
running process.  GDB ignores any core dump file while your program
is running.

</P>
<P>
On some operating systems, a program cannot be executed outside GDB
while you have breakpoints set on it inside GDB.  You can use the
<CODE>kill</CODE> command in this situation to permit running your program
outside the debugger.

</P>
<P>
The <CODE>kill</CODE> command is also useful if you wish to recompile and
relink your program, since on many systems it is impossible to modify an
executable file while it is running in a process.  In this case, when you
next type <CODE>run</CODE>, GDB notices that the file has changed, and
reads the symbol table again (while trying to preserve your current
breakpoint settings).

</P>


<H2><A NAME="SEC24" HREF="gdb_toc.html#TOC24">Additional process information</A></H2>

<P>
<A NAME="IDX65"></A>
<A NAME="IDX66"></A>

</P>
<P>
Some operating systems provide a facility called <SAMP>`/proc'</SAMP> that can
be used to examine the image of a running process using file-system
subroutines.  If GDB is configured for an operating system with this
facility, the command <CODE>info proc</CODE> is available to report on several
kinds of information about the process running your program.  
<CODE>info proc</CODE> works only on SVR4 systems that support <CODE>procfs</CODE>.
This includes OSF/1 (Digital Unix), Solaris, Irix, and Unixware,
but not HP-UX or Linux, for example.

</P>
<DL COMPACT>

<DT><CODE>info proc</CODE>
<DD>
<A NAME="IDX67"></A>
 
Summarize available information about the process.

<A NAME="IDX68"></A>
<DT><CODE>info proc mappings</CODE>
<DD>
Report on the address ranges accessible in the program, with information
on whether your program may read, write, or execute each range.

<A NAME="IDX69"></A>
<DT><CODE>info proc times</CODE>
<DD>
Starting time, user CPU time, and system CPU time for your program and
its children.

<A NAME="IDX70"></A>
<DT><CODE>info proc id</CODE>
<DD>
Report on the process IDs related to your program: its own process ID,
the ID of its parent, the process group ID, and the session ID.

<A NAME="IDX71"></A>
<DT><CODE>info proc status</CODE>
<DD>
General information on the state of the process.  If the process is
stopped, this report includes the reason for stopping, and any signal
received.

<DT><CODE>info proc all</CODE>
<DD>
Show all the above information about the process.
</DL>



<H2><A NAME="SEC25" HREF="gdb_toc.html#TOC25">Debugging programs with multiple threads</A></H2>

<P>
<A NAME="IDX72"></A>
<A NAME="IDX73"></A>
<A NAME="IDX74"></A>
In some operating systems, such as HP-UX and Solaris, a single program
may have more than one <STRONG>thread</STRONG> of execution.  The precise semantics
of threads differ from one operating system to another, but in general
the threads of a single program are akin to multiple processes--except
that they share one address space (that is, they can all examine and
modify the same variables).  On the other hand, each thread has its own
registers and execution stack, and perhaps private memory.

</P>
<P>
GDB provides these facilities for debugging multi-thread
programs:

</P>

<UL>
<LI>automatic notification of new threads

<LI><SAMP>`thread <VAR>threadno</VAR>'</SAMP>, a command to switch among threads

<LI><SAMP>`info threads'</SAMP>, a command to inquire about existing threads

<LI><SAMP>`thread apply [<VAR>threadno</VAR>] [<VAR>all</VAR>] <VAR>args</VAR>'</SAMP>,

a command to apply a command to a list of threads
<LI>thread-specific breakpoints

</UL>


<BLOCKQUOTE>
<P>
<EM>Warning:</EM> These facilities are not yet available on every
GDB configuration where the operating system supports threads.
If your GDB does not support threads, these commands have no
effect.  For example, a system without thread support shows no output
from <SAMP>`info threads'</SAMP>, and always rejects the <CODE>thread</CODE> command,
like this:

</P>

<PRE>
(gdb) info threads
(gdb) thread 1
Thread ID 1 not known.  Use the "info threads" command to
see the IDs of currently known threads.
</PRE>

</BLOCKQUOTE>

<P>
<A NAME="IDX75"></A>
<A NAME="IDX76"></A>
The GDB thread debugging facility allows you to observe all
threads while your program runs--but whenever GDB takes
control, one thread in particular is always the focus of debugging.
This thread is called the <STRONG>current thread</STRONG>.  Debugging commands show
program information from the perspective of the current thread.

</P>
<P>
<A NAME="IDX77"></A>
<A NAME="IDX78"></A>
Whenever GDB detects a new thread in your program, it displays
the target system's identification for the thread with a message in the
form <SAMP>`[New <VAR>systag</VAR>]'</SAMP>.  <VAR>systag</VAR> is a thread identifier
whose form varies depending on the particular system.  For example, on
LynxOS, you might see

</P>

<PRE>
[New process 35 thread 27]
</PRE>

<P>
when GDB notices a new thread.  In contrast, on an SGI system,
the <VAR>systag</VAR> is simply something like <SAMP>`process 368'</SAMP>, with no
further qualifier.

</P>

<P>
<A NAME="IDX79"></A>
<A NAME="IDX80"></A>
For debugging purposes, GDB associates its own thread
number--always a single integer--with each thread in your program.

</P>
<DL COMPACT>

<DT><CODE>info threads</CODE>
<DD>
<A NAME="IDX81"></A>
 
Display a summary of all threads currently in your
program.  GDB displays for each thread (in this order):


<OL>
<LI>the thread number assigned by GDB

<LI>the target system's thread identifier (<VAR>systag</VAR>)

<LI>the current stack frame summary for that thread

</OL>

An asterisk <SAMP>`*'</SAMP> to the left of the GDB thread number
indicates the current thread.

For example, 
</DL>


<PRE>
(gdb) info threads
  3 process 35 thread 27  0x34e5 in sigpause ()
  2 process 35 thread 23  0x34e5 in sigpause ()
* 1 process 35 thread 13  main (argc=1, argv=0x7ffffff8)
    at threadtest.c:68
</PRE>

<DL COMPACT>

<DT><CODE>thread <VAR>threadno</VAR></CODE>
<DD>
<A NAME="IDX82"></A>
 
Make thread number <VAR>threadno</VAR> the current thread.  The command
argument <VAR>threadno</VAR> is the internal GDB thread number, as
shown in the first field of the <SAMP>`info threads'</SAMP> display.
GDB responds by displaying the system identifier of the thread
you selected, and its current stack frame summary:


<PRE>
(gdb) thread 2
[Switching to process 35 thread 23]
0x34e5 in sigpause ()
</PRE>

As with the <SAMP>`[New ...]'</SAMP> message, the form of the text after
<SAMP>`Switching to'</SAMP> depends on your system's conventions for identifying
threads.  

<A NAME="IDX83"></A>
<DT><CODE>thread apply [<VAR>threadno</VAR>] [<VAR>all</VAR>]  <VAR>args</VAR></CODE>
<DD>
The <CODE>thread apply</CODE> command allows you to apply a command to one or
more threads.  Specify the numbers of the threads that you want affected
with the command argument <VAR>threadno</VAR>.  <VAR>threadno</VAR> is the internal
GDB thread number, as shown in the first field of the <SAMP>`info
threads'</SAMP> display.  To apply a command to all threads, use 
<CODE>thread apply all</CODE> <VAR>args</VAR>. 
</DL>

<P>
<A NAME="IDX84"></A>
<A NAME="IDX85"></A>
<A NAME="IDX86"></A>
Whenever GDB stops your program, due to a breakpoint or a
signal, it automatically selects the thread where that breakpoint or
signal happened.  GDB alerts you to the context switch with a
message of the form <SAMP>`[Switching to <VAR>systag</VAR>]'</SAMP> to identify the
thread.

</P>
<P>
See section <A HREF="gdb_6.html#SEC39">Stopping and starting multi-thread programs</A>, for
more information about how GDB behaves when you stop and start
programs with multiple threads.

</P>
<P>
See section <A HREF="gdb_6.html#SEC30">Setting watchpoints</A>, for information about
watchpoints in programs with multiple threads.

</P>


<H2><A NAME="SEC26" HREF="gdb_toc.html#TOC26">Debugging programs with multiple processes</A></H2>

<P>
<A NAME="IDX87"></A>
<A NAME="IDX88"></A>
<A NAME="IDX89"></A>
GDB has no special support for debugging programs which create
additional processes using the <CODE>fork</CODE> function.  When a program
forks, GDB will continue to debug the parent process and the
child process will run unimpeded.  If you have set a breakpoint in any
code which the child then executes, the child will get a <CODE>SIGTRAP</CODE>
signal which (unless it catches the signal) will cause it to terminate.

</P>
<P>
However, if you want to debug the child process there is a workaround
which isn't too painful.  Put a call to <CODE>sleep</CODE> in the code which
the child process executes after the fork.  It may be useful to sleep
only if a certain environment variable is set, or a certain file exists,
so that the delay need not occur when you don't want to run GDB
on the child.  While the child is sleeping, use the <CODE>ps</CODE> program to
get its process ID.  Then tell GDB (a new invocation of
GDB if you are also debugging the parent process) to attach to
the child process (see section <A HREF="gdb_5.html#SEC22">Debugging an already-running process</A>).  From that point on you can debug
the child process just like any other process which you attached to.

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