{"id":3711,"date":"2024-02-04T04:35:03","date_gmt":"2024-02-03T23:05:03","guid":{"rendered":"https:\/\/cthecosmos.com\/?p=3711"},"modified":"2024-02-04T04:47:02","modified_gmt":"2024-02-03T23:17:02","slug":"few-gdb-debugging-commands","status":"publish","type":"post","link":"https:\/\/cthecosmos.com\/?p=3711","title":{"rendered":"Few GDB Debugging Commands"},"content":{"rendered":"\n<p class=\"has-black-color has-text-color has-background has-link-color wp-elements-23dd99eda5b7f2968e11b3961fb7ab45\" style=\"background-color:#7ed9c6\">If you&#8217;re embarking on your journey with the GDB debugger, this guide will introduce you to essential commands to get you started.<br><br><strong><em>break<\/em>:<\/strong> Sets a breakpoint at a specified line number or function. You can use this command to pause execution of the program at a certain point and examine its state. Example: <strong>break main<\/strong> sets a breakpoint at the beginning of the <strong>main<\/strong> function.<br><br><strong><em>run<\/em><\/strong>: Starts execution of the program from the beginning. You can use this command to run your program and stop it at a breakpoint or when it crashes.<br><br><strong><em>next<\/em><\/strong>: Executes the next line of code in the program. If the line is a function call, it will execute the function without stopping at the individual lines of code inside the function.<br><br><strong><em>step<\/em><\/strong>: Similar to <strong>next<\/strong>, but will stop at the individual lines of code inside a function.<br><br><strong><em>continue<\/em><\/strong>: Continues execution of the program until it reaches the next breakpoint or it finishes executing.<br><br><strong><em>print<\/em><\/strong>: Prints the value of a variable or expression. Example: <strong>print x<\/strong> will print the value of the variable <strong>x<\/strong>.<br><br><strong><em>backtrace<\/em><\/strong>: Displays the current call stack, showing the sequence of functions that were called to reach the current point in the program.<br><br><strong><em>watch<\/em><\/strong>: Sets a watchpoint on a variable. A watchpoint will stop execution of the program when the value of the variable changes. Example: <strong>watch x<\/strong> sets a watchpoint on the variable <strong>x<\/strong>.<br><br><strong><em>info<\/em><\/strong>: Displays various pieces of information about the program, such as the values of registers or the loaded libraries. Few of the sub commands are:<br><br><strong><em>info registers<\/em><\/strong>: This command will display the current values of all registers.<br><br><strong><em>info source<\/em><\/strong>: This command will display information about the current source file.<br><br><strong><em>info frame<\/em><\/strong>: This command displays information about the current stack frame, including the function name, arguments, and local variables. For example, use info frame to see information about the current stack frame.<br><br><strong><em>info locals<\/em><\/strong>: This command displays information about the local variables in the current stack frame. For example, use info locals to see the values of all the local variables in the current function.<br><br><strong><em>info breakpoints<\/em><\/strong>: This command lists all the breakpoints that have been set. For example, use info breakpoints to see a list of all the breakpoints that are currently active.<br><br><strong><em>quit<\/em><\/strong>: Exits gdb and terminates the debugging session<br><br><strong><em>unwatch<\/em><\/strong>: This command removes a watchpoint that was previously set. For example, to remove the watchpoint on <strong>x<\/strong>, use <strong>unwatch x<\/strong>.<br><br><strong><em>tbreak<\/em><\/strong>: This command is similar to break, but it only stops the program once. After the program stops, the breakpoint is automatically removed. For example, to stop the program once it reaches line <strong>10<\/strong>, use <strong>tbreak 10<\/strong>.<br><br><strong><em>return<\/em><\/strong>: This command is used to finish the current function and return to the calling function. You can use it when stepping through code to quickly move out of a function. For example, if you are inside the function <strong>foo<\/strong> and want to <strong>return<\/strong> to the calling function, use the return command.<br><br><strong><em>finish<\/em><\/strong>: This command is similar to return, but it finishes the current function and returns all the way to the main function. For example, if you are several levels deep in function calls and want to quickly return to the main function, use the <strong>finish<\/strong> command.<br><br><strong><em>disable<\/em><\/strong>: This command disables a breakpoint. For example, to disable breakpoint number <strong>1<\/strong>, use <strong>disable 1<\/strong>.<br><br><strong><em>enable<\/em><\/strong>: This command re-enables a disabled breakpoint. For example, to enable breakpoint number <strong>1<\/strong>, use <strong>enable 1<\/strong>.<br><br><strong><em>set<\/em><\/strong>: This command is used to set the value of a variable or register. For example, <strong>set $eax = 10<\/strong> will set the value of the <strong>eax<\/strong> register to <strong>10<\/strong>.<br><br><strong><em>display<\/em><\/strong>: This command is used to print the value of a variable or register every time gdb stops at a breakpoint. For example, <strong>display $eax<\/strong> will print the value of the <strong>eax<\/strong> register every time gdb stops at a breakpoint.<br><br><strong><em>condition<\/em><\/strong>: This command is used to set a condition for a breakpoint. For example, <strong>break main if argc &gt; 2<\/strong> will set a breakpoint at the beginning of the main function only if <strong>argc<\/strong> is greater than <strong>2<\/strong>.<br><br><strong><em>x<\/em><\/strong>: This command is used to examine memory. For example, <strong>x\/10x $esp<\/strong> will display the <strong>10 memory locations<\/strong> starting at the address pointed to by the <strong>esp<\/strong> register.<br><br><strong><em>stepi<\/em><\/strong> and <strong><em>nexti<\/em><\/strong>: These commands are used to step through the program one instruction at a time. stepi will <strong>step into<\/strong> function calls, while <strong>nexti <\/strong>will <strong>step over<\/strong> them.<br><br><strong><em>finish<\/em><\/strong>: This command is used to continue the execution of the program until the current function is finished.<br><br><strong><em>tui<\/em><\/strong>: This command is used to enable the Text User Interface mode in gdb. This mode provides a split screen display with the source code on one side and the gdb console on the other.<br><br><strong><em>attach<\/em><\/strong>: This command is used to attach gdb to a running process. For example, attach 1234 will attach gdb to the process with the process ID of 1234.<br><br><strong><em>continue<\/em><\/strong>: This command allows you to continue running the program after it has been stopped. This can be useful during loops when you want to continue running the program until a certain condition is met.<br><br><strong><em>until<\/em><\/strong>: This command continues execution of the program until a specific line of code is reached, which can be helpful for stepping out of loops or functions.<br><br><strong><em>where<\/em><\/strong>: Prints a stack trace of the current execution.<br><br><strong><em>unarg<\/em><\/strong>: This command is used to run a program with command line arguments.<br><br>(gdb) runarg argument1 argument2 argument<br><br>Starting program: \/path\/to\/program argument1 argument2 argument3<br><br><strong><em>record<\/em><\/strong>: This command is used to record the execution of a program for later replay.<br><br>(gdb) record full<br><br>Record all instructions in full.<br><br><strong><em>replay<\/em><\/strong>: This command is used to replay the execution of a program that was previously recorded.<br><br>For certain versions of gdb:<br><br><strong><em>reverse-continue<\/em><\/strong>: This command is used to continue execution of the program in reverse direction.<br><br><strong><em>reverse-stop<\/em><\/strong>: This command is used to stop execution of the program in reverse direction.<br><br><strong><em>reverse-next<\/em><\/strong>: This command is used to step backwards by one line in the program<br><br><strong><em>reverse-finish<\/em><\/strong>: This command is used to run the program in reverse direction until the current function returns.<br><\/p>\n\n\n\n<p class=\"has-white-color has-text-color has-background has-link-color has-medium-font-size wp-elements-486e7799b35920eece0029316d5e98bd\" style=\"background:linear-gradient(135deg,rgb(35,23,11) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%)\">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br><strong>An Article by: <\/strong>Yashwanth Naidu Tikkisetty<br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide introduces essential GDB debugger commands: break (set breakpoint), run (start execution), next (execute next line), step (execute and stop in function), continue (resume execution), print (print variable value), backtrace (display call stack), watch (set watchpoint), info (display program info), quit (exit debugger), and more.<\/p>\n<a href=\"https:\/\/cthecosmos.com\/?p=3711\" class=\"more-link\">Read More <span class=\"screen-reader-text\">Few GDB Debugging Commands<\/span><\/a>","protected":false},"author":120055267,"featured_media":3715,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false},"categories":[769112296,36985,35264203,769114260],"tags":[765740309,769114263,319,70886,772321191,320749,34922710,34920936],"class_list":{"0":"post-3711","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-c-concepts-hub","8":"category-c-programming","9":"category-linux-3","10":"category-linuxlearnings","11":"tag-linuxlearning","12":"tag-basic-gdb","13":"tag-commands","14":"tag-debugging","15":"tag-embedded","16":"tag-gdb","17":"tag-linux-2","18":"tag-ubuntu-2","20":"fallback-thumbnail"},"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/gdbcommands.jpg?fit=1792%2C1024&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8CiEf-XR","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/users\/120055267"}],"replies":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3711"}],"version-history":[{"count":6,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3711\/revisions"}],"predecessor-version":[{"id":3727,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3711\/revisions\/3727"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/media\/3715"}],"wp:attachment":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}