Visual debugging using gdbgui
Tags: gdb, debugging
Published: 2020-07-23 13:38:03

---
title: Visual debugging using gdbgui
published: true
date: 2020-07-15 13:00:32 UTC
tags: gdb,debugging
canonical_url: https://blog.logrocket.com/visual-debugging-using-gdbgui/
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/j1pwrxoy1ccb3m7ivy9m.png
---
**Written by [Kasra Khosravi](https://blog.logrocket.com/author/kasrakhosravi/)**✏️

## Introduction

Developers spend a lot of time debugging and maintaining current codebases. Understanding different methods of debugging is critical. Some developers still feel comfortable with more manual ways of debugging. There are also a lot of snapshot tools that give you a detailed report of the issues and errors after a certain part of the code runs. Both of these approaches can be beneficial but they are often focused on when the error happens. With the help of debugger tools, we can see how an error has happened.

In this post, we will be examining [gdbgui](https://www.gdbgui.com/), an extension of [gdb](https://www.gnu.org/software/gdb/), with visual debugging abilities, which will help us debug compiled languages.

## What is gdb?

[gdb](https://www.gnu.org/software/gdb/) is a very handy tool for debugging compiled languages such as [C](https://en.wikipedia.org/wiki/C_(programming_language), [C++](https://en.wikipedia.org/wiki/C%2B%2B), [Go](https://en.wikipedia.org/wiki/Go_(programming_language), [Rust](https://en.wikipedia.org/wiki/Rust_(programming_language)), etc. It is available for common operating systems such as Mac, Windows, and Linux. This debugger can help us in [several ways](https://www.tutorialspoint.com/gnu_debugger/what_is_gdb.htm) such as:

- Getting an overview of program variables and context when the error occurs
- If a core dump, which is the state of memory at a specific time, happens because of a crash or abnormal termination, we can understand what statement of expression caused it
- Stopping on parts of the program that are causing issues while the program is running
- Testing and experimenting with the program while debugging to narrow down or fix the problems

While these are very helpful in our debugging effort, you still need to run the debugging commands in the terminal and might not get a good overview of the context of the program.

[![LogRocket Free Trial Banner](https://i0.wp.com/blog.logrocket.com/wp-content/uploads/2017/03/f760c-1gpjapknnuyhu8esa3z0jga.png?resize=1200%2C280&ssl=1)](https://logrocket.com/signup/)

## What is gdbgui?

[gdbgui](https://www.gdbgui.com/) is another debugging tool that is built on top of gdb. The main difference is that gdbgui exposes a browser-based frontend to developers, so they can add breakpoints, view stack traces, and change the context and parameter values while the debugger is running.

![gdb debugger browser based frontend](https://i0.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/gdbgui2.png?resize=1378%2C991&ssl=1)<figcaption id="caption-attachment-21812">Source: <a href="https://www.gdbgui.com/screenshots/" target="_blank" rel="noopener noreferrer">https://www.gdbgui.com/screenshots/</a></figcaption>

## gdbgui architecture

The library takes advantage of [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket). Whenever the frontend debugging tool starts, a WebSocket connection is established which is the communication channel between the browser and the backend. After each established WebSocket, the backend starts a new managed gdb subprocess for parsing the output while spawning a separate subprocess for constantly checking for the output from the other subprocess. Finally, it transports the output to the client using the same WebSocket.

## Installation

Let us install gdbgui on our machine. As mentioned before, gdbgui can be installed on various operating systems, but it can also run remotely.

There are [several ways](https://www.gdbgui.com/installation/) to install gdbgui on your machine depending on your operating system, but I am going to follow the simplest and most straightforward one. Since this library is reliant on [Python 3](https://www.python.org/download/releases/3.0/), you need to be careful in following any of the installation guides as it might interfere with your machine’s global settings.

The best way to install gdbgui is via `pipx`. This tool lets you install libraries and applications written in Python in an isolated environment while allowing you to run them via shell as globals via shell. This removes a lot of headaches that might happen because of version incompatibility.

Start by [running these commands](https://www.gdbgui.com/installation/):

```jsx
// Step 1: Install pipx
python3 -m pip install --user pipx

// Step 2: Add new path in user path
python3 -m userpath append ~/.local/bin

// Step 3: Install gdbgui
pipx install gdbgui

// Or you can run gdbgui without installing
pipx run gdbgui
```

## Debugging using gdbgui

Let us debug a simple C++ program using gdbgui to get familiar with its features.

First, download [this repository](https://github.com/cs01/gdbgui) to your machine, using the dropdown on the right side of the page.

![gdbuigithub](https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/gdbuigithub.png?resize=730%2C374&ssl=1)<figcaption id="caption-attachment-21817">Source: <a href="https://github.com/cs01/gdbgui" target="_blank" rel="noopener noreferrer">https://github.com/cs01/gdbgui</a></figcaption>

Now, run these commands in the terminal:

```jsx
// First, enter the directory for C example
cd gdbgui-master/examples/c

// Second, run the command to run the debugger
make
```

After running these commands, the debugger should pop open in your browser.

![debugger in browser](https://i1.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/debuggerinbrowser.png?resize=730%2C416&ssl=1)

At first glance, we see a lot of options on this page. But one thing we need to consider is that the developers of this tool have tried to limit the number of available GUI options, as to lessen the confusion when debugging.

On top of the page, we see a section called _Load Binary_, which we can use to load program binaries (with a relative path from where the gdbgui is running) and also pass arguments to it like we would in a command line. Please note that this section only accepts executables, so you cannot just pass current executables in your directory without expecting it to be compiled.

![Lord binary hello c.a arg](https://i0.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/lordbinary.png?resize=678%2C86&ssl=1)

On the right side of the page, we have a controller section which will allow developers to control the general flow of the debugging like restarting a program from the beginning, continuing until the next breakpoint, sending an “interrupt” signal to gdb process for pausing and step over, in, or out of the next functional call. There are also [keyboard shortcuts](https://www.gdbgui.com/gettingstarted/) for all of these controls.

![keyboard shortcut buttons](https://i0.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/keyboardshortcuts.png?resize=626%2C82&ssl=1)

At the bottom of the page, you see an output section that is used to display the output of the program or gdb. It also allows you to run gdb commands in the terminal section.

![display of output or program](https://i1.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/outputsection.png?resize=730%2C130&ssl=1)

When you load the executable file, you see the source code with applied breakpoints in the middle of the page. Also, we see a dropdown section that shows a list of all the source files that were used to compile the executable. Although it was not a necessary thing to specify the source file in the dropdown menu, for some debugging scenarios like Rust, you might need to guide gdbgui by entering the name of the main source file in the dropdown section and select it.

![entering the name of the main source file on the left side of the page](https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/enternameofmainsourcefile.png?resize=730%2C390&ssl=1)

On the right side, you see a few panels as well. The call stack section shows where in the stack we are on a certain breakpoint (file and instruction address, which is the pointer in code pointing to the breakpoint). We can also explore the local variables related to attacked breakpoints. Additionally, by clicking on any of the locales here we can see a detailed tree view that can show complex relationships between variables in a visual format.

![detailed tree view](https://i0.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/detailedtreeview.png?resize=730%2C330&ssl=1)

We also have an expression section where we can watch a certain value persistently through the lifetime of a program. This will be interesting to use for a variable that is expected to change in different sequences of the program.

!["greet" typed in expression section](https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/expressionssection.png?resize=730%2C172&ssl=1)

One cool feature about the debugger is that it turns memory addresses into hyperlinks so you can click on them. When that is done, you can see all the memory addresses in a data table and recognize which character they refer to.

![Hello world memory address in data table](https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/memoryaddressesindatatable.png?resize=730%2C355&ssl=1)

An additional visual feature that sets this debugger apart is the ability to monitor the value of certain variables over time on a chart.

![chart icon in expression section](https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/charticoninexpressionesection.png?resize=730%2C296&ssl=1)

If you click on the chart icon in the expression section, you will be shown a graph with both X and Y lines. If the value of the expression changes across the runtime of a debugger, you will be able to see this change visually on the graph. ![graph showing no expression changes](https://i0.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/debuggergraoh.png?resize=730%2C759&ssl=1)

Finally, we can explore the settings section of the debugger which is accessible by clicking on the gear icon on the top right of the page. Many of the settings are self-explanatory like adding a breakpoint to main after loading the executable. Also if you leave the option for refreshing components when a command is sent from the terminal, then you will be able to refresh all the visual views after each command (like entering `n` for the next line of execution). You will also be able to set the theme of debugger here (I personally prefer the darker mode and that is why I am leaving it to be the `monokai`).

Lastly, you can check the option to print all the commands in the console. Checking this option will show not only all the commands that you run, but it will also display all the commands that gdbgui is running behind the scenes for debugging purposes. In most cases, it is better to have this option unchecked as it will make the terminal console noisy and hard to read.

![leave "print all command in console" unchecked](https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2020/07/settings.png?resize=730%2C481&ssl=1)

## Conclusion

You can follow the instructions in this guide as well as following [the official wiki for gdbgui](https://www.gdbgui.com/) to test a debugging flow. Such a tool can be particularly helpful for you if you do not have access to a proper IDE debugger tool and would prefer to spin up a visual debugger interface in a matter of seconds and start investigating an issue.

Also, remember that C is not the only language you can debug using this tool. There is support for several languages such as Rust and Go as well. However, there might be small tweaks that you need to make in the debugger environment to make it work for those languages as well. For those scenarios, make sure to check [gdbgui’s official documentation](https://www.gdbgui.com/) to get familiar with the proper [setup guide](https://www.gdbgui.com/guides/).

## Resources

[https://www.gdbgui.com/](https://www.gdbgui.com/)  
[http://www.cs.cornell.edu/courses/cs312/2006fa/lectures/lec26.html](http://www.cs.cornell.edu/courses/cs312/2006fa/lectures/lec26.html)  
[https://resources.infosecinstitute.com/category/certifications-training/malware-analysis-reverse-engineering/debug-malware-analysis/debugging-basics/#gref](https://resources.infosecinstitute.com/category/certifications-training/malware-analysis-reverse-engineering/debug-malware-analysis/debugging-basics/#gref)  
[https://www.tutorialspoint.com/gnu\_debugger/what\_is\_gdb.htm](https://www.tutorialspoint.com/gnu_debugger/what_is_gdb.htm)  

* * * 
## [LogRocket:](logrocket.com/devto) Full visibility into production web apps 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/eq752g8qhbffxt3hp9t4.png)

LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.

[Demo it yourself for free – no account required.](https://logrocket-sample-site.web.app/) 
* * *

The post [Visual debugging using gdbgui](https://blog.logrocket.com/visual-debugging-using-gdbgui/) appeared first on [LogRocket Blog](https://blog.logrocket.com).