Chapter 6.
Running Programs Under gdb
When you run a program under gdb, you must first generate debugging information when you compile
it.
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.
6.1. Compiling for debugging
In order to debug a program effectively, you need to generate debugging information when you com-
pile 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.
To request debugging information, specify the
-g
option when you run the compiler.
Most compilers do not include information about preprocessor macros in the debugging information
if you specify the
-g
flag alone, because this information is rather large. Version 3.1 of gcc, the
gnu C compiler, provides macro information if you specify the options
-gdwarf-2
and
-g3
; the
former option requests debugging information in the Dwarf 2 format, and the latter requests "extra
information". In the future, we hope to find more compact ways to represent macro information, so
that it can be included with
-g
alone.
Many C compilers are unable to handle the
-g
and
-O
options together. Using those compilers, you
cannot generate optimized executables containing debugging information.
gcc, the gnu C compiler, supports
-g
with or without
-O
, making it possible to debug optimized
code. We recommend that you
always
use
-g
whenever you compile a program. You may think your
program is correct, but there is no sense in pushing your luck.
When you debug a program compiled with
-g -O
, 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.
Some things do not work as well with
-g -O
as with just
-g
, particularly on machines with instruction
scheduling. If in doubt, recompile with
-g
alone, and if this fixes the problem, please report it to us as
a bug (including a test case!).
Older versions of the gnu C compiler permitted a variant option
-gg
for debugging information. gdb
no longer supports this format; if your gnu C compiler has this option, do not use it.
6.2. Starting your program
run
r
Use the
run
command to start your program under gdb. You must first specify the program name
(except on VxWorks) with an argument to gdb (refer to Chapter 4
Getting In and Out of gdb
), or
by using the
file
or
exec-file
command (refer to Section 17.1
Commands to specify files
).
Содержание ENTERPRISE LINUX 4 - DEVELOPER TOOLS GUIDE
Страница 1: ...Red Hat Enterprise Linux 4 Debugging with gdb ...
Страница 12: ...2 Chapter 1 Debugging with gdb ...
Страница 28: ...18 Chapter 4 Getting In and Out of gdb ...
Страница 34: ...24 Chapter 5 gdb Commands ...
Страница 44: ...34 Chapter 6 Running Programs Under gdb ...
Страница 68: ...58 Chapter 8 Examining the Stack ...
Страница 98: ...88 Chapter 10 Examining Data ...
Страница 112: ...102 Chapter 12 Tracepoints ...
Страница 118: ...108 Chapter 13 Debugging Programs That Use Overlays ...
Страница 138: ...128 Chapter 14 Using gdb with Different Languages ...
Страница 144: ...134 Chapter 15 Examining the Symbol Table ...
Страница 170: ...160 Chapter 19 Debugging remote programs ...
Страница 198: ...188 Chapter 21 Controlling gdb ...
Страница 204: ...194 Chapter 22 Canned Sequences of Commands ...
Страница 206: ...196 Chapter 23 Command Interpreters ...
Страница 216: ...206 Chapter 25 Using gdb under gnu Emacs ...
Страница 296: ...286 Chapter 27 gdb Annotations ...
Страница 300: ...290 Chapter 28 Reporting Bugs in gdb ...
Страница 322: ...312 Chapter 30 Using History Interactively ...
Страница 362: ...352 Appendix D gdb Remote Serial Protocol ...
Страница 380: ...370 Appendix F GNU GENERAL PUBLIC LICENSE ...
Страница 386: ...376 Appendix G GNU Free Documentation License ...
Страница 410: ......