{"id":12840,"date":"2019-03-28T23:32:41","date_gmt":"2019-03-28T23:32:41","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12840"},"modified":"2019-03-28T23:32:41","modified_gmt":"2019-03-28T23:32:41","slug":"gnu-debugger-or-gdb-a-powerful-source-code-debugging-tool-for-linux-programs","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/gnu-debugger-or-gdb-a-powerful-source-code-debugging-tool-for-linux-programs\/","title":{"rendered":"GNU Debugger or GDB: A Powerful Source Code Debugging tool for Linux Programs"},"content":{"rendered":"<p>A debugger plays a vital role in any software development system. Nobody can write a bug-free code all at once. During the course of development, bugs are being raised and needs to be solved for further enhancement. A development system is incomplete without a debugger. Considering the open source developers community, GNU Debugger is their best choice. It is also used for commercial software development on UNIX type platforms.<\/p>\n<div id=\"attachment_6276\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/04\/GNU-Debugger.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6276\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/04\/GNU-Debugger.jpg\" alt=\"GNU Debugger Tool\" width=\"400\" height=\"260\" aria-describedby=\"caption-attachment-6276\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-6276\" class=\"wp-caption-text\">Debugging source code with GNU Debugger<\/p>\n<\/div>\n<p><strong>GNU Debugger<\/strong>, also known as\u00a0<strong>gdb<\/strong>, allows us to sneak through the code while it executes or what a program was trying to do at the moment before it crashed. GDB basically helps us to do four main things to catch flaws in the source code.<\/p>\n<ol>\n<li>Start the program, specifying arguments that may affect the general behavior.<\/li>\n<li>Stop the program on specified conditions.<\/li>\n<li>Examine the crash or when program was stopped.<\/li>\n<li>Change the code and to experiment with the modified code instantaneously.<\/li>\n<\/ol>\n<p>We can use gdb to debug programs written in\u00a0<strong>C<\/strong>\u00a0and C++ without much effort. As of now support for other programming languages like\u00a0<strong>D<\/strong>,\u00a0<strong>Modula-2<\/strong>,\u00a0<strong>Fortran<\/strong>\u00a0are partial.<\/p>\n<h3>Getting started with GNU Debugger or GDB<\/h3>\n<p><strong>GDB<\/strong>\u00a0is invoked using the\u00a0<strong><em>gdb<\/em><\/strong>\u00a0command. On issuing\u00a0<strong><em>gdb<\/em><\/strong>, it displays some information about platform and drops you into the (<strong><em>gdb<\/em><\/strong>) prompt as shown below.<\/p>\n<pre>[root@fedora20 ~]# gdb<\/pre>\n<h5>Sample Output<\/h5>\n<pre>GNU gdb (GDB) Fedora 7.6.50.20130731-19.fc20 \r\nCopyright (C) 2013 Free Software Foundation, Inc. \r\nLicense GPLv3+: GNU GPL version 3 or later &lt;http:\/\/gnu.org\/licenses\/gpl.html&gt; \r\nThis is free software: you are free to change and redistribute it. \r\nThere is NO WARRANTY, to the extent permitted by law.  Type \"show copying\" \r\nand \"show warranty\" for details. \r\nThis GDB was configured as \"x86_64-redhat-linux-gnu\". \r\nType \"show configuration\" for configuration details. \r\nFor bug reporting instructions, please see: \r\n&lt;http:\/\/www.gnu.org\/software\/gdb\/bugs\/&gt;. \r\nFind the GDB manual and other documentation resources online at: \r\n&lt;http:\/\/www.gnu.org\/software\/gdb\/documentation\/&gt;. \r\nFor help, type \"help\". \r\nType \"apropos word\" to search for commands related to \"word\". \r\n(<strong>gdb<\/strong>)<\/pre>\n<p>Type\u00a0<strong><em>help<\/em><\/strong>\u00a0list to out the different classes of commands available inside gdb. Type\u00a0<strong><em>help<\/em>\u00a0<\/strong>followed by a class name for a list of commands in that class. Type\u00a0<strong><em>help all<\/em><\/strong>\u00a0for the list of all commands. Command name abbreviations are allowed if they are unambiguous. For example, you can type\u00a0<strong><em>n<\/em><\/strong>\u00a0instead of typing\u00a0<strong><em>next<\/em><\/strong>\u00a0or\u00a0<strong><em>c<\/em><\/strong>\u00a0for\u00a0<strong><em>continue<\/em><\/strong>\u00a0and so on.<\/p>\n<h4>Most Commonly used GDB Commands<\/h4>\n<p>Commonly used\u00a0<strong>gdb commands<\/strong>\u00a0are listed in the following table. These commands are to be used from the gdb command prompt (<strong><em>gdb<\/em><\/strong>).<\/p>\n<table cellspacing=\"0\" cellpadding=\"4\">\n<tbody>\n<tr valign=\"TOP\">\n<td width=\"159\">\n<div align=\"JUSTIFY\"><b>Command<\/b><\/div>\n<\/td>\n<td width=\"488\">\n<div align=\"JUSTIFY\"><b>Description<\/b><\/div>\n<\/td>\n<\/tr>\n<tr class=\"alt\" valign=\"TOP\">\n<td width=\"159\">\n<div align=\"JUSTIFY\">run<\/div>\n<\/td>\n<td width=\"488\">\n<div align=\"JUSTIFY\">Start a program execution<\/div>\n<\/td>\n<\/tr>\n<tr valign=\"TOP\">\n<td width=\"159\">\n<div align=\"JUSTIFY\">quit<\/div>\n<\/td>\n<td width=\"488\">\n<div align=\"JUSTIFY\">Quit gdb<\/div>\n<\/td>\n<\/tr>\n<tr class=\"alt\" valign=\"TOP\">\n<td width=\"159\">\n<div align=\"JUSTIFY\">print expr<\/div>\n<\/td>\n<td width=\"488\">\n<div align=\"JUSTIFY\">Print expression where\u00a0<i>expr\u00a0<\/i>may be a variable name too<\/div>\n<\/td>\n<\/tr>\n<tr valign=\"TOP\">\n<td width=\"159\">\n<div align=\"JUSTIFY\">next<\/div>\n<\/td>\n<td width=\"488\">\n<div align=\"JUSTIFY\">Go to next line<\/div>\n<\/td>\n<\/tr>\n<tr class=\"alt\" valign=\"TOP\">\n<td width=\"159\">\n<div align=\"JUSTIFY\">step<\/div>\n<\/td>\n<td width=\"488\">\n<div align=\"JUSTIFY\">Step into next line<\/div>\n<\/td>\n<\/tr>\n<tr valign=\"TOP\">\n<td width=\"159\">\n<div align=\"JUSTIFY\">continue<\/div>\n<\/td>\n<td width=\"488\">\n<div align=\"JUSTIFY\">Continue from the current line till the end of program or next break point<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Note the difference between the two commands\u00a0<strong><em>step<\/em><\/strong>\u00a0and\u00a0<strong><em>next<\/em><\/strong>. The\u00a0<strong>next<\/strong>\u00a0command does not go inside function if next line is a function call. Whereas\u00a0<strong><em>step<\/em><\/strong>\u00a0command can go in inside function and see what happens there.<\/p>\n<h4>A sample session with GDB<\/h4>\n<p>Consider the following source code.<\/p>\n<pre>\/\/ sum.c\r\n#include &lt;stdio.h&gt; \r\n\r\nint sum (int a, int b) { \r\n\tint c; \r\n\tc = a + b; \r\n\treturn c; \r\n} \r\n\r\nint main() { \r\n\tint x, y, z; \r\n\tprintf(\"\\nEnter the first number: \"); \r\n\tscanf(\"%d\", &amp;x); \r\n\tprintf(\"Enter the second number: \"); \r\n\tscanf(\"%d\", &amp;y); \r\n\tz = sum (x, y); \r\n\tprintf(\"The sum is %d\\n\\n\", z); \r\n\treturn 0; \r\n}<\/pre>\n<p>In order to debug the output file we need to compile the same with\u00a0<em>-g<\/em>\u00a0option to\u00a0<strong>gcc<\/strong>\u00a0as follows.<\/p>\n<pre>$ gcc -g sum.c -o sum<\/pre>\n<p>The output file\u00a0<strong><em>sum<\/em><\/strong>\u00a0can be attached to gdb via either of the following 2 ways:<\/p>\n<p>1. By specifying the output file as an argument to gdb.<\/p>\n<pre>$ gdb sum<\/pre>\n<p>2. Running output file inside gdb using\u00a0<strong><em>file<\/em><\/strong>\u00a0command.<\/p>\n<pre>$ gdb\r\n(gdb) file sum<\/pre>\n<p>The\u00a0<strong><em>list<\/em><\/strong>\u00a0command lists lines in the source code file and moves the pointer. So first\u00a0<strong><em>list<\/em>\u00a0<\/strong>will display the first 10 lines and next\u00a0<strong><em>list<\/em><\/strong>\u00a0displays the next 10 and so on.<\/p>\n<pre>(gdb) list\r\n1\t#include &lt;stdio.h&gt;   \r\n2\t \r\n3\tint sum (int a, int b) { \r\n4\t\tint c; \r\n5\t\tc = a + b; \r\n6\t\treturn c; \r\n7\t} \r\n8\t \r\n9\tint main() { \r\n10\t\tint x, y, z;<\/pre>\n<p>To start execution, issue the\u00a0<strong><em>run<\/em>\u00a0<\/strong>command. Now the program gets executed normally. But we forgot to put some breakpoints in the source code for debugging, right? These breakpoints can be specified for functions or at specified lines.<\/p>\n<pre>(gdb) b main<\/pre>\n<p><strong>Note<\/strong>: I have used an abbreviation\u00a0<strong>b<\/strong>\u00a0for\u00a0<strong>break<\/strong>.<\/p>\n<p>After setting break point at main function, rerunning the program will stop at the line 11. The same thing can be made into effect if the line number is known before.<\/p>\n<pre>(gdb) b sum.c:11<\/pre>\n<p>Now step through the lines of code using the\u00a0<strong><em>next<\/em>\u00a0<\/strong>or\u00a0<strong><em>n<\/em><\/strong>\u00a0command. It is important to note that\u00a0<strong><em>next<\/em><\/strong>\u00a0command does not go inside function code unless a break point is set on the function. Let\u2019s try out the\u00a0<strong><em>print<\/em><\/strong>\u00a0command now. Set break point on function sum as below.<\/p>\n<pre>(gdb) b sum \r\nBreakpoint 1 at 0x4005aa: file sum.c, line 5. \r\n(gdb) r \r\nStarting program: \/root\/sum \r\n\r\nEnter the first number: 2 \r\nEnter the second number: 3 \r\n\r\nBreakpoint 1, sum (a=2, b=3) at sum.c:5 \r\n5\t\tc = a + b; \r\n(gdb) p a \r\n$1 = 2 \r\n(gdb) p b \r\n$2 = 3\r\n(gdb) c \r\nContinuing. \r\nThe sum is 5 \r\n\r\n[Inferior 1 (process 3444) exited normally]<\/pre>\n<div class=\"google-auto-placed ap_container\">\n<p>If the program being run requires command line parameters then provide the same along with the\u00a0<strong><em>run<\/em><\/strong>\u00a0command as.<\/p>\n<pre>(gdb) run   . . .<\/pre>\n<p>Shared library files associated with the current running program can be listed as.<\/p>\n<pre>(gdb) info share \r\nFrom                To                  Syms Read   Shared Object Library \r\n0x00000035a6000b10  0x00000035a6019c70  Yes         \/lib64\/ld-linux-x86-64.so.2 \r\n0x00000035a641f560  0x00000035a6560bb4  Yes         \/lib64\/libc.so.6<\/pre>\n<h4>Modifying Variables<\/h4>\n<p><strong>GDB<\/strong>\u00a0is also capable of modifying variables throughout the execution of program. Let\u2019s try this out. As mentioned above set break point at line 16 and run the program.<\/p>\n<pre>(gdb) r \r\nStarting program: \/root\/sum \r\n\r\nEnter the first number: 1 \r\nEnter the second number: 2 \r\n\r\nBreakpoint 1, main ( ) at sum.c:16 \r\n16\t\tprintf(\"The sum is %d\\n\\n\", z); \r\n(gdb) set z=4 \r\n(gdb) c \r\nContinuing. \r\nThe sum is 4<\/pre>\n<p>Now\u00a0<strong>a = 1<\/strong>,\u00a0<strong>b = 2<\/strong>\u00a0and result should be\u00a0<strong>z = 3<\/strong>. But here we changed the final result to<strong>\u00a0z = 4<\/strong>\u00a0in the main function. In this way debugging can be made easier using gdb.<\/p>\n<h4>Enable\/Disable Breakpoints<\/h4>\n<p>To get the list of all breakpoints type\u00a0<strong><em>info breakpoints<\/em><\/strong>.<\/p>\n<pre>(gdb) info breakpoints \r\nNum     Type           Disp Enb Address            What \r\n1       breakpoint     keep y   0x00000000004005c2 in main at sum.c:11<\/pre>\n<div class=\"google-auto-placed ap_container\">\n<p>Here there is only one break point and it is To. enabled disable the breakpoints specify the breakpoint number along with the\u00a0<strong><em>disable<\/em><\/strong>\u00a0command. To enable afterwards use the\u00a0<strong><em>enable<\/em><\/strong>\u00a0command.<\/p>\n<pre>(gdb) disable 1 \r\n(gdb) info breakpoints \r\nNum     Type           Disp Enb Address            What \r\n1       breakpoint     keep n   0x00000000004005c2 in main at sum.c:11<\/pre>\n<p>You can also delete the breakpoints with\u00a0<strong><em>delete<\/em><\/strong>\u00a0command.<\/p>\n<h4>Debugging running Processes<\/h4>\n<p>Numerous processes are running in background in a GNU\/Linux system. To debug a running process first of all we need to find the process id of that particular process.\u00a0<strong><em>pidof<\/em>\u00a0<\/strong>command gives you the pid of a process.<\/p>\n<pre>$ pidof &lt;process_name&gt;<\/pre>\n<p>Now we need to attach this pid to gdb. There are 2 ways.<\/p>\n<p>1. By specifying pid along with gdb.<\/p>\n<pre>$ gdb -p &lt;pid&gt;<\/pre>\n<p>2. Using\u00a0<em>attach<\/em>\u00a0command from gdb.<\/p>\n<pre>(gdb) attach &lt;pid&gt;<\/pre>\n<p>That\u2019s all for now. These are only basics of gdb to get a good start in debugging source code and it is much more than the things explained above. For example, we can debug using the stack information, environment variables and lot more. Try to play around with all these stuffs\u2026<\/p>\n<p><a style=\"font-size: 1rem;\" href=\"https:\/\/www.tecmint.com\/debug-source-code-in-linux-using-gdb\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A debugger plays a vital role in any software development system. Nobody can write a bug-free code all at once. During the course of development, bugs are being raised and needs to be solved for further enhancement. A development system is incomplete without a debugger. Considering the open source developers community, GNU Debugger is their &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/gnu-debugger-or-gdb-a-powerful-source-code-debugging-tool-for-linux-programs\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;GNU Debugger or GDB: A Powerful Source Code Debugging tool for Linux Programs&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12840","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12840"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12840\/revisions"}],"predecessor-version":[{"id":12841,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12840\/revisions\/12841"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}