Debugging with GDB (February 2008)

Table Of Contents
Chapter 4: Running Programs Under GDB 27
run > outfile
starts your program, diverting its output to the file outfile’.
An explicit redirection in run overrides the tty command’s effect on the input/output
device, but not its effect on the controlling terminal.
When you use the tty command or redirect input in the run command, only the input
for your program is affected. The input for GDB still comes from your terminal.
4.7 Debugging a Running Process
You can use GDB to debug a running process by specifying the process ID. Following
commands are used to debug a running process:
attach process-id
This command attaches to a running process—one that was started outside
GDB. (info files 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 ps utility, or with the jobs -l shell command.
attach does not repeat if you press
h
RET
i
a second time after executing the
command.
Note:
To use attach, your program must be running in an environment which supports
processes; for example, attach 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.
When you use attach, 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 7.3 [Specifying source directories], page 59). You
can also use the file command to load the program. See Section 12.1 [Commands to
Specify Files], page 103.
GDB stops the process being attached for debugging. You can examine and modify an
attached process with the GDB commands that are available when you start processes
with run. You can insert breakpoints; you can step and continue; you can modify
storage. See Section 5.1 [Breakpoints in shared libraries], page 33. If you want the
process to continue running, you can use the continue command after attaching GDB
to the process.
detach
When you have finished debugging the attached process, you can use the detach
command to release it from GDB control. The process continues its execution
after being detached. After the detach command, that process and GDB be-
come completely independent once more, and you are ready to attach another
process or start one with run. detach does not repeat if you press
h
RET
i
again
after executing the command.