]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/gcc/gcc-command-options/options-for-debugging-your-program.rst
sphinx: copy files from texi2rst-generated repository
[thirdparty/gcc.git] / gcc / doc / gcc / gcc-command-options / options-for-debugging-your-program.rst
1 ..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6 .. index:: options, debugging, debugging information options
7
8 .. _debugging-options:
9
10 Options for Debugging Your Program
11 **********************************
12
13 To tell GCC to emit extra information for use by a debugger, in almost
14 all cases you need only to add :option:`-g` to your other options. Some debug
15 formats can co-exist (like DWARF with CTF) when each of them is enabled
16 explicitly by adding the respective command line option to your other options.
17
18 GCC allows you to use :option:`-g` with
19 :option:`-O`. The shortcuts taken by optimized code may occasionally
20 be surprising: some variables you declared may not exist
21 at all; flow of control may briefly move where you did not expect it;
22 some statements may not be executed because they compute constant
23 results or their values are already at hand; some statements may
24 execute in different places because they have been moved out of loops.
25 Nevertheless it is possible to debug optimized output. This makes
26 it reasonable to use the optimizer for programs that might have bugs.
27
28 If you are not using some other optimization option, consider
29 using :option:`-Og` (see :ref:`optimize-options`) with :option:`-g`.
30 With no :option:`-O` option at all, some compiler passes that collect
31 information useful for debugging do not run at all, so that
32 :option:`-Og` may result in a better debugging experience.
33
34 .. option:: -g
35
36 Produce debugging information in the operating system's native format
37 (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging
38 information.
39
40 On most systems that use stabs format, :option:`-g` enables use of extra
41 debugging information that only GDB can use; this extra information
42 makes debugging work better in GDB but probably makes other debuggers
43 crash or refuse to read the program. If you want to control for certain whether
44 to generate the extra information, use :option:`-gvms` (see below).
45
46 .. option:: -ggdb
47
48 Produce debugging information for use by GDB. This means to use the
49 most expressive format available (DWARF, stabs, or the native format
50 if neither of those are supported), including GDB extensions if at all
51 possible.
52
53 .. option:: -gdwarf, -gdwarf-version
54
55 Produce debugging information in DWARF format (if that is supported).
56 The value of :samp:`{version}` may be either 2, 3, 4 or 5; the default
57 version for most targets is 5 (with the exception of VxWorks, TPF and
58 Darwin/Mac OS X, which default to version 2, and AIX, which defaults
59 to version 4).
60
61 Note that with DWARF Version 2, some ports require and always
62 use some non-conflicting DWARF 3 extensions in the unwind tables.
63
64 Version 4 may require GDB 7.0 and :option:`-fvar-tracking-assignments`
65 for maximum benefit. Version 5 requires GDB 8.0 or higher.
66
67 GCC no longer supports DWARF Version 1, which is substantially
68 different than Version 2 and later. For historical reasons, some
69 other DWARF-related options such as
70 :option:`-fno-dwarf2-cfi-asm`) retain a reference to DWARF Version 2
71 in their names, but apply to all currently-supported versions of DWARF.
72
73 .. option:: -gbtf
74
75 Request BTF debug information. BTF is the default debugging format for the
76 eBPF target. On other targets, like x86, BTF debug information can be
77 generated along with DWARF debug information when both of the debug formats are
78 enabled explicitly via their respective command line options.
79
80 .. option:: -gctf, -gctflevel
81
82 Request CTF debug information and use level to specify how much CTF debug
83 information should be produced. If :option:`-gctf` is specified
84 without a value for level, the default level of CTF debug information is 2.
85
86 CTF debug information can be generated along with DWARF debug information when
87 both of the debug formats are enabled explicitly via their respective command
88 line options.
89
90 Level 0 produces no CTF debug information at all. Thus, :option:`-gctf0`
91 negates :option:`-gctf`.
92
93 Level 1 produces CTF information for tracebacks only. This includes callsite
94 information, but does not include type information.
95
96 Level 2 produces type information for entities (functions, data objects etc.)
97 at file-scope or global-scope only.
98
99 .. option:: -gvms
100
101 Produce debugging information in Alpha/VMS debug format (if that is
102 supported). This is the format used by DEBUG on Alpha/VMS systems.
103
104 :samp:`-g{level}` :samp:`-ggdb{level}` :samp:`-gvms{level}`
105 Request debugging information and also use :samp:`{level}` to specify how
106 much information. The default level is 2.
107
108 Level 0 produces no debug information at all. Thus, :option:`-g0` negates
109 :option:`-g`.
110
111 Level 1 produces minimal information, enough for making backtraces in
112 parts of the program that you don't plan to debug. This includes
113 descriptions of functions and external variables, and line number
114 tables, but no information about local variables.
115
116 Level 3 includes extra information, such as all the macro definitions
117 present in the program. Some debuggers support macro expansion when
118 you use :option:`-g3`.
119
120 If you use multiple :option:`-g` options, with or without level numbers,
121 the last such option is the one that is effective.
122
123 :option:`-gdwarf` does not accept a concatenated debug level, to avoid
124 confusion with :option:`-gdwarf-level`.
125 Instead use an additional :option:`-glevel` option to change the
126 debug level for DWARF.
127
128 .. option:: -fno-eliminate-unused-debug-symbols
129
130 By default, no debug information is produced for symbols that are not actually
131 used. Use this option if you want debug information for all symbols.
132
133 .. option:: -feliminate-unused-debug-symbols
134
135 Default setting; overrides :option:`-fno-eliminate-unused-debug-symbols`.
136
137 .. option:: -femit-class-debug-always
138
139 Instead of emitting debugging information for a C++ class in only one
140 object file, emit it in all object files using the class. This option
141 should be used only with debuggers that are unable to handle the way GCC
142 normally emits debugging information for classes because using this
143 option increases the size of debugging information by as much as a
144 factor of two.
145
146 .. option:: -fno-merge-debug-strings
147
148 Direct the linker to not merge together strings in the debugging
149 information that are identical in different object files. Merging is
150 not supported by all assemblers or linkers. Merging decreases the size
151 of the debug information in the output file at the cost of increasing
152 link processing time. Merging is enabled by default.
153
154 .. option:: -fmerge-debug-strings
155
156 Default setting; overrides :option:`-fno-merge-debug-strings`.
157
158 .. option:: -fdebug-prefix-map={old}={new}
159
160 When compiling files residing in directory :samp:`{old}`, record
161 debugging information describing them as if the files resided in
162 directory :samp:`{new}` instead. This can be used to replace a
163 build-time path with an install-time path in the debug info. It can
164 also be used to change an absolute path to a relative path by using
165 :samp:`.` for :samp:`{new}`. This can give more reproducible builds, which
166 are location independent, but may require an extra command to tell GDB
167 where to find the source files. See also :option:`-ffile-prefix-map`.
168
169 .. option:: -fvar-tracking
170
171 Run variable tracking pass. It computes where variables are stored at each
172 position in code. Better debugging information is then generated
173 (if the debugging information format supports this information).
174
175 It is enabled by default when compiling with optimization (:option:`-Os`,
176 :option:`-O`, :option:`-O2`, ...), debugging information (:option:`-g`) and
177 the debug info format supports it.
178
179 .. option:: -fvar-tracking-assignments
180
181 Annotate assignments to user variables early in the compilation and
182 attempt to carry the annotations over throughout the compilation all the
183 way to the end, in an attempt to improve debug information while
184 optimizing. Use of :option:`-gdwarf-4` is recommended along with it.
185
186 It can be enabled even if var-tracking is disabled, in which case
187 annotations are created and maintained, but discarded at the end.
188 By default, this flag is enabled together with :option:`-fvar-tracking`,
189 except when selective scheduling is enabled.
190
191 .. option:: -fno-var-tracking-assignments
192
193 Default setting; overrides :option:`-fvar-tracking-assignments`.
194
195 .. option:: -gsplit-dwarf
196
197 If DWARF debugging information is enabled, separate as much debugging
198 information as possible into a separate output file with the extension
199 :samp:`.dwo`. This option allows the build system to avoid linking files with
200 debug information. To be useful, this option requires a debugger capable of
201 reading :samp:`.dwo` files.
202
203 .. option:: -gdwarf32, -gdwarf64
204
205 If DWARF debugging information is enabled, the :option:`-gdwarf32` selects
206 the 32-bit DWARF format and the :option:`-gdwarf64` selects the 64-bit
207 DWARF format. The default is target specific, on most targets it is
208 :option:`-gdwarf32` though. The 32-bit DWARF format is smaller, but
209 can't support more than 2GiB of debug information in any of the DWARF
210 debug information sections. The 64-bit DWARF format allows larger debug
211 information and might not be well supported by all consumers yet.
212
213 .. option:: -gdescribe-dies
214
215 Add description attributes to some DWARF DIEs that have no name attribute,
216 such as artificial variables, external references and call site
217 parameter DIEs.
218
219 .. option:: -gpubnames
220
221 Generate DWARF ``.debug_pubnames`` and ``.debug_pubtypes`` sections.
222
223 .. option:: -ggnu-pubnames
224
225 Generate ``.debug_pubnames`` and ``.debug_pubtypes`` sections in a format
226 suitable for conversion into a GDBindex. This option is only useful
227 with a linker that can produce GDBindex version 7.
228
229 .. option:: -fdebug-types-section
230
231 When using DWARF Version 4 or higher, type DIEs can be put into
232 their own ``.debug_types`` section instead of making them part of the
233 ``.debug_info`` section. It is more efficient to put them in a separate
234 comdat section since the linker can then remove duplicates.
235 But not all DWARF consumers support ``.debug_types`` sections yet
236 and on some objects ``.debug_types`` produces larger instead of smaller
237 debugging information.
238
239 .. option:: -fno-debug-types-section
240
241 Default setting; overrides :option:`-fdebug-types-section`.
242
243 .. option:: -grecord-gcc-switches, -gno-record-gcc-switches
244
245 This switch causes the command-line options used to invoke the
246 compiler that may affect code generation to be appended to the
247 DW_AT_producer attribute in DWARF debugging information. The options
248 are concatenated with spaces separating them from each other and from
249 the compiler version.
250 It is enabled by default.
251 See also :option:`-frecord-gcc-switches` for another
252 way of storing compiler options into the object file.
253
254 .. option:: -gstrict-dwarf
255
256 Disallow using extensions of later DWARF standard version than selected
257 with :option:`-gdwarf-version`. On most targets using non-conflicting
258 DWARF extensions from later standard versions is allowed.
259
260 .. option:: -gno-strict-dwarf
261
262 Allow using extensions of later DWARF standard version than selected with
263 :option:`-gdwarf-version`.
264
265 .. option:: -gas-loc-support
266
267 Inform the compiler that the assembler supports ``.loc`` directives.
268 It may then use them for the assembler to generate DWARF2+ line number
269 tables.
270
271 This is generally desirable, because assembler-generated line-number
272 tables are a lot more compact than those the compiler can generate
273 itself.
274
275 This option will be enabled by default if, at GCC configure time, the
276 assembler was found to support such directives.
277
278 .. option:: -gno-as-loc-support
279
280 Force GCC to generate DWARF2+ line number tables internally, if DWARF2+
281 line number tables are to be generated.
282
283 .. option:: -gas-locview-support
284
285 Inform the compiler that the assembler supports ``view`` assignment
286 and reset assertion checking in ``.loc`` directives.
287
288 This option will be enabled by default if, at GCC configure time, the
289 assembler was found to support them.
290
291 .. option:: -gno-as-locview-support
292
293 Force GCC to assign view numbers internally, if
294 :option:`-gvariable-location-views` are explicitly requested.
295
296 .. option:: -gcolumn-info, -gno-column-info
297
298 Emit location column information into DWARF debugging information, rather
299 than just file and line.
300 This option is enabled by default.
301
302 .. option:: -gstatement-frontiers, -gno-statement-frontiers
303
304 This option causes GCC to create markers in the internal representation
305 at the beginning of statements, and to keep them roughly in place
306 throughout compilation, using them to guide the output of ``is_stmt``
307 markers in the line number table. This is enabled by default when
308 compiling with optimization (:option:`-Os`, :option:`-O1`, :option:`-O2`,
309 ...), and outputting DWARF 2 debug information at the normal level.
310
311 .. option:: -gvariable-location-views, -gvariable-location-views=incompat5, -gno-variable-location-views
312
313 Augment variable location lists with progressive view numbers implied
314 from the line number table. This enables debug information consumers to
315 inspect state at certain points of the program, even if no instructions
316 associated with the corresponding source locations are present at that
317 point. If the assembler lacks support for view numbers in line number
318 tables, this will cause the compiler to emit the line number table,
319 which generally makes them somewhat less compact. The augmented line
320 number tables and location lists are fully backward-compatible, so they
321 can be consumed by debug information consumers that are not aware of
322 these augmentations, but they won't derive any benefit from them either.
323
324 This is enabled by default when outputting DWARF 2 debug information at
325 the normal level, as long as there is assembler support,
326 :option:`-fvar-tracking-assignments` is enabled and
327 :option:`-gstrict-dwarf` is not. When assembler support is not
328 available, this may still be enabled, but it will force GCC to output
329 internal line number tables, and if
330 :option:`-ginternal-reset-location-views` is not enabled, that will most
331 certainly lead to silently mismatching location views.
332
333 There is a proposed representation for view numbers that is not backward
334 compatible with the location list format introduced in DWARF 5, that can
335 be enabled with :option:`-gvariable-location-views=incompat5`. This
336 option may be removed in the future, is only provided as a reference
337 implementation of the proposed representation. Debug information
338 consumers are not expected to support this extended format, and they
339 would be rendered unable to decode location lists using it.
340
341 .. option:: -ginternal-reset-location-views, -gno-internal-reset-location-views
342
343 Attempt to determine location views that can be omitted from location
344 view lists. This requires the compiler to have very accurate insn
345 length estimates, which isn't always the case, and it may cause
346 incorrect view lists to be generated silently when using an assembler
347 that does not support location view lists. The GNU assembler will flag
348 any such error as a ``view number mismatch``. This is only enabled
349 on ports that define a reliable estimation function.
350
351 .. option:: -ginline-points, -gno-inline-points
352
353 Generate extended debug information for inlined functions. Location
354 view tracking markers are inserted at inlined entry points, so that
355 address and view numbers can be computed and output in debug
356 information. This can be enabled independently of location views, in
357 which case the view numbers won't be output, but it can only be enabled
358 along with statement frontiers, and it is only enabled by default if
359 location views are enabled.
360
361 .. option:: -gz[={type}]
362
363 Produce compressed debug sections in DWARF format, if that is supported.
364 If :samp:`{type}` is not given, the default type depends on the capabilities
365 of the assembler and linker used. :samp:`{type}` may be one of
366 :samp:`none` (don't compress debug sections), or :samp:`zlib` (use zlib
367 compression in ELF gABI format). If the linker doesn't support writing
368 compressed debug sections, the option is rejected. Otherwise, if the
369 assembler does not support them, :option:`-gz` is silently ignored when
370 producing object files.
371
372 .. option:: -femit-struct-debug-baseonly
373
374 Emit debug information for struct-like types
375 only when the base name of the compilation source file
376 matches the base name of file in which the struct is defined.
377
378 This option substantially reduces the size of debugging information,
379 but at significant potential loss in type information to the debugger.
380 See :option:`-femit-struct-debug-reduced` for a less aggressive option.
381 See :option:`-femit-struct-debug-detailed` for more detailed control.
382
383 This option works only with DWARF debug output.
384
385 .. option:: -femit-struct-debug-reduced
386
387 Emit debug information for struct-like types
388 only when the base name of the compilation source file
389 matches the base name of file in which the type is defined,
390 unless the struct is a template or defined in a system header.
391
392 This option significantly reduces the size of debugging information,
393 with some potential loss in type information to the debugger.
394 See :option:`-femit-struct-debug-baseonly` for a more aggressive option.
395 See :option:`-femit-struct-debug-detailed` for more detailed control.
396
397 This option works only with DWARF debug output.
398
399 .. option:: -femit-struct-debug-detailed[={spec-list}]
400
401 Specify the struct-like types
402 for which the compiler generates debug information.
403 The intent is to reduce duplicate struct debug information
404 between different object files within the same program.
405
406 This option is a detailed version of
407 :option:`-femit-struct-debug-reduced` and :option:`-femit-struct-debug-baseonly`,
408 which serves for most needs.
409
410 A specification has the syntax
411
412 [:samp:`dir:` | :samp:`ind:`][:samp:`ord:` | :samp:`gen:`](:samp:`any` | :samp:`sys` | :samp:`base` | :samp:`none`)
413
414 The optional first word limits the specification to
415 structs that are used directly (:samp:`dir:`) or used indirectly (:samp:`ind:`).
416 A struct type is used directly when it is the type of a variable, member.
417 Indirect uses arise through pointers to structs.
418 That is, when use of an incomplete struct is valid, the use is indirect.
419 An example is
420 :samp:`struct one direct; struct two * indirect;`.
421
422 The optional second word limits the specification to
423 ordinary structs (:samp:`ord:`) or generic structs (:samp:`gen:`).
424 Generic structs are a bit complicated to explain.
425 For C++, these are non-explicit specializations of template classes,
426 or non-template classes within the above.
427 Other programming languages have generics,
428 but :option:`-femit-struct-debug-detailed` does not yet implement them.
429
430 The third word specifies the source files for those
431 structs for which the compiler should emit debug information.
432 The values :samp:`none` and :samp:`any` have the normal meaning.
433 The value :samp:`base` means that
434 the base of name of the file in which the type declaration appears
435 must match the base of the name of the main compilation file.
436 In practice, this means that when compiling :samp:`foo.c`, debug information
437 is generated for types declared in that file and :samp:`foo.h`,
438 but not other header files.
439 The value :samp:`sys` means those types satisfying :samp:`base`
440 or declared in system or compiler headers.
441
442 You may need to experiment to determine the best settings for your application.
443
444 The default is :option:`-femit-struct-debug-detailed=all`.
445
446 This option works only with DWARF debug output.
447
448 .. option:: -fno-dwarf2-cfi-asm
449
450 Emit DWARF unwind info as compiler generated ``.eh_frame`` section
451 instead of using GAS ``.cfi_*`` directives.
452
453 .. option:: -fdwarf2-cfi-asm
454
455 Default setting; overrides :option:`-fno-dwarf2-cfi-asm`.
456
457 .. option:: -fno-eliminate-unused-debug-types
458
459 Normally, when producing DWARF output, GCC avoids producing debug symbol
460 output for types that are nowhere used in the source file being compiled.
461 Sometimes it is useful to have GCC emit debugging
462 information for all types declared in a compilation
463 unit, regardless of whether or not they are actually used
464 in that compilation unit, for example
465 if, in the debugger, you want to cast a value to a type that is
466 not actually used in your program (but is declared). More often,
467 however, this results in a significant amount of wasted space.
468
469 .. option:: -feliminate-unused-debug-types
470
471 Default setting; overrides :option:`-fno-eliminate-unused-debug-types`.