Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
GNU Debugger
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==An example session== Consider the following source-code written in [[C (programming language)|C]]: <syntaxhighlight lang="c"> #include <stdio.h> #include <stdlib.h> #include <string.h> size_t foo_len( const char *s ) { return strlen(s); } int main(int argc, char *argv[]) { const char *a = NULL; printf("size of a = %lu\n", foo_len(a)); exit(0); } </syntaxhighlight> Using the [[GNU Compiler Collection|GCC]] compiler on [[Linux]], the code above must be compiled using the <code>-g</code> flag in order to include appropriate debug information on the binary generated, thus making it possible to inspect it using GDB. Assuming that the file containing the code above is named <code>example.c</code>, the command for the [[Compilation (programming)|compilation]] could be: <syntaxhighlight lang="console"> $ gcc example.c -Og -g -o example </syntaxhighlight> And the binary can now be run: <syntaxhighlight lang="console"> $ ./example Segmentation fault </syntaxhighlight> Since the example code, when executed, generates a [[segmentation fault]], GDB can be used to inspect the problem. <syntaxhighlight lang="console"> $ gdb ./example GNU gdb (GDB) Fedora (7.3.50.20110722-13.fc16) Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <https://www.gnu.org/software/gdb/bugs/>... Reading symbols from /path/example...done. (gdb) run Starting program: /path/example Program received signal SIGSEGV, Segmentation fault. 0x0000000000400527 in foo_len (s=0x0) at example.c:7 7 return strlen (s); (gdb) print s $1 = 0x0 </syntaxhighlight> The problem is present in line 7, and occurs when calling the function <code>[[Strlen#strlen|strlen]]</code> (because its argument, <code>s</code>, is <code>[[Null pointer#Null pointer|NULL]]</code>). Depending on the implementation of strlen ([[Inline function|inline]] or not), the output can be different, e.g.: <syntaxhighlight lang="console"> GNU gdb (GDB) 7.3.1 Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i686-pc-linux-gnu". For bug reporting instructions, please see: <https://www.gnu.org/software/gdb/bugs/>... Reading symbols from /tmp/gdb/example...done. (gdb) run Starting program: /tmp/gdb/example Program received signal SIGSEGV, Segmentation fault. 0xb7ee94f3 in strlen () from /lib/i686/cmov/libc.so.6 (gdb) bt #0 0xb7ee94f3 in strlen () from /lib/i686/cmov/libc.so.6 #1 0x08048435 in foo_len (s=0x0) at example.c:7 #2 0x0804845a in main (argc=<optimized out>, argv=<optimized out>) at example.c:14 </syntaxhighlight> To fix the problem, the variable <code>a</code> (in the function <code>main</code>) must contain a valid string. Here is a fixed version of the code: <syntaxhighlight lang="c"> #include <stdio.h> #include <stdlib.h> #include <string.h> size_t foo_len(const char *s) { return strlen(s); } int main(int argc, char *argv[]) { const char *a = "This is a test string"; printf("size of a = %lu\n", foo_len(a)); exit(0); } </syntaxhighlight> Recompiling and running the executable again inside GDB now gives a correct result: <syntaxhighlight lang="console"> $ gdb ./example GNU gdb (GDB) Fedora (7.3.50.20110722-13.fc16) Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <https://www.gnu.org/software/gdb/bugs/>... Reading symbols from /path/example...done. (gdb) run Starting program: /path/example size of a = 21 [Inferior 1 (process 14290) exited normally] </syntaxhighlight> GDB prints the output of <code>printf</code> in the screen, and then informs the user that the program exited normally.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)