Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
GNU Debugger
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
GNU Debugger
(section)
Add topic