Setting up FreeBSD 13.0 for debugging Redis using JetBrains CLion and gdb

Setting up FreeBSD 13.0 to debug Redis using JetBrains CLion and gdb.

Contrary to the common belief, FreeBSD is not just for the servers. I am noting down all the steps needed to set up FreeBSD for debugging Redis. Mate Desktop Environment will give life to the Desktop Metaphor. JetBrains CLion will provide the Integrated Development Environment (IDE) – with gcc, gdb, and make doing the heavy lifting behind the scenes – making FreeBSD a potent development environment.

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.

FreeBSD is an outstanding operating system and I just love it. Netflix, Apple, Sony, and many others would concur.

Setting up FreeBSD 13 as a Desktop Operating System

The installation process is pretty straightforward and very well documented. The only thing special about my install is the ZFS file system in stripe mode on a single disk. What this means is that:

  • the performance benefits of striping as available in a multi-disk setup, will not be available;
  • ZFS snapshots can be taken, but this is not a backup of any sort;
  • backups are my responsibility as stripe mode, even in a multi-disk setup, is bereft of any redundancy.

The section 2.6.4. Guided Partitioning Using Root-on-ZFS describes how to configure  ZFS in FreeBSD during install.

Once the installation is done, log in to the system as root, fetch, and install security updates:

freebsd-update fetch
freebsd-update install

A fresh FreeBSD installation has nothing other than command line user interface. The X Window System needs to be installed to get a Graphical User Interface running:

pkg install xorg

FreeBSD graphics driver on bare metal

With xorg installed, it is time to install DRM kmod graphics driver for Intel\AMD GPUs.

amdgpu, i915, and radeon DRM modules for the linuxkpi-based KMS components. Currently corresponding to Linux 5.4.144 DRM. This version is for FreeBSD 13. https://github.com/freebsd/drm-kmod/
pkg install drm-fbsd13-kmod
sysrc kld_list+="i915kms"

FreeBSD graphics driver in virtualized environment

Do not install the bare metal drivers in a virtualized environment. Instead do the following:

pkg install xf86-video-vmware

Mate Desktop Environment and the rest

The following will install a minimal Mate Desktop Environment:

pkg install mate-base mate-terminal

Get sudo:

pkg install sudo

Add user to wheel group and use visudo command to edit the sudoers file to allow users in wheel group to run system tools with authentication:

pw usermod msiyer -G wheel

Create a ~\.xinitrc file with the below content. This will let startx command know that it is supposed to start a Mate session:

exec mate-session

I also installed a few very important applications – chromium browser, peazip archive manager, eom image viewer:

pkg install chromium flameshot peazip eom

It is now time to reboot:

pkg reboot

ZFS snapshots

Once rebooted, login and execute startx command to start a Mate session. A snapshot of the file system is in order now. The power of zfs is simply mind blowing. The only other file system that comes close is btrfs on Linux:

zfs list                                           
NAME                 USED  AVAIL     REFER  MOUNTPOINT
zroot               8.37G   113G       96K  /zroot
zroot/ROOT          6.67G   113G       96K  none
zroot/ROOT/default  6.67G   113G     6.67G  /
zroot/tmp            136K   113G      136K  /tmp
zroot/usr           1.59G   113G       96K  /usr
zroot/usr/home       216M   113G      216M  /usr/home
zroot/usr/ports      711M   113G      711M  /usr/ports
zroot/usr/src        702M   113G      702M  /usr/src
zroot/var            111M   113G       96K  /var
zroot/var/audit       96K   113G       96K  /var/audit
zroot/var/crash      110M   113G      110M  /var/crash
zroot/var/log        240K   113G      240K  /var/log
zroot/var/mail       148K   113G      148K  /var/mail
zroot/var/tmp         96K   113G       96K  /var/tmp

Recursive snapshots of zroot/ROOT/default mounted at / and zroot/usr mounted at /usr should suffice:

sudo zfs snapshot -r zroot/ROOT/@2022-01-15-fullStack
sudo zfs snapshot -r zroot/usr/@2022-01-15-fullStack

List snapshots created above:

zfs list -t snapshot                                   
NAME                                      USED  AVAIL     REFER  MOUNTPOINT
zroot/ROOT/default@2022-01-15-fullStack    68K      -     6.67G  -
zroot/usr@2022-01-15-fullStack              0B      -       96K  -
zroot/usr/home@2022-01-15-fullStack       164K      -      216M  -
zroot/usr/ports@2022-01-15-fullStack        0B      -      711M  -
zroot/usr/src@2022-01-15-fullStack          0B      -      702M  -

To rollback to the pristine-state snapshots created above, all I have to do is:

zfs rollback -r zroot/ROOT/default@2022-01-15-fullStack
zfs rollback -r zroot/usr@2022-01-15-fullStack

Setting up FreeBSD as a Redis development environment

Redis is written in C language. The source code is hosted on GitHub. Need git to get it. Compiling it is simple. As per documentation, just make it in the source code root directory. However, FreeBSD has its own implementation of make, which is not fully compatible with GNU Make. On FreeBSD one needs to install gmake to get the GNU implementation. Also, TCL is required to run tests. Do the following to get everything needed to fetch sources, compile, run tests and debug:

pkg install git gmake cmake tcl87 gcc gdb

Now let us get the sources:

cd <DESIRED_LOCATION>
git clone https://github.com/redis/redis.git

Running the gmake and gmake test commands in the <DESIRED_LOCATION>\redis directory will compile the project and execute all tests. Thereafter, if one is adventurous enough, a debugging session with appropriate breakpoints can be launched in the terminal using gdb. Also, gdb has a curses-based tui (Text User Interface) mode that improves usability:

gdb tui (text user interface)

For others that need an IDE to feel at home, vim or emacs can be configured to behave as an IDE. There is also a modern alternative for the venerable vim known as neovim. I spent a few hours configuring neovim and gave up. I just do not have the time right now to fiddle with configurations.

JetBrains has a great C\C++ IDE called CLion. It is available in FreeBSD repos and I happen to have a license. This is an option for those who want to hit the ground running rather than fiddling with config\dotfiles:

pkg install jetbrains-clion

Setting up JetBrains CLion for Redis debugging

Use the File -> Settings menu option to launch the following dialog to set up Toolchains. Make sure to use gmake for Make option:

JetBrains CLion Toolchains

I had downloaded the source manually and asked CLion to load an existing project. CLion informed me that the import works best when clean is executed. I let it do its thing and ended up with a successfully imported project.

CLion detects all configurations from the makefile and lists them as shown below. We can use the highlighted ones for debugging redis-cli (client executable) and redis-server (server executable). The target executable needs to be filled in in the Executable field of the dialog.

JetBrains CLion Debug Configuration

That is all there is to set up CLion. So simple.

JetBrains CLion debugging in progress

Ricing FreeBSD (optional)

Material Design from Google is quite important in the UX Design world. Materia GTK theme tries to bring the same UX to Unix desktop. Get it:

sudo pkg install materia-gtk-theme

Papirus Icons are a must for me:

sudo pkg install wget
wget -qO- https://git.io/papirus-icon-theme-install | DESTDIR="$HOME/.icons" sh

Now comes the ricing of the terminal emulator. What better to spice up the terminal than Oh My ZSH with the agnoster theme. Installing it is as simple as (install curl is not already available):

$ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

To activate agnoster theme, open .zshrc and update the ZSH_THEME key's value to agnoster.

agnoster theme requires powerline-fonts:

pkg install powerline-fonts

References

The FreeBSD Project
FreeBSD is an operating system used to power modern servers, desktops, and embedded platforms.
FreeBSD Handbook
A constantly evolving, comprehensive resource for FreeBSD users
MATE Desktop Environment
The MATE Desktop Environment is the continuation of GNOME 2. It providesan intuitive and attractive desktop environment using traditionalmetaphors for Linux and other Unix-like operating systems.MA
Redis
CLion: A Cross-Platform IDE for C and C++ by JetBrains
A powerful IDE from JetBrains helps you develop in C and C++ on Linux, macOS and Windows.
Coreutils - GNU core utilities
GCC, the GNU Compiler Collection- GNU Project
GDB: The GNU Project Debugger
Oh My Zsh - a delightful & open source framework for Zsh
Oh-My-Zsh is a delightful, open source, community-driven framework for managing your ZSH configuration. It comes bundled with several helpful functions, helpers, plugins, themes, and a few things that make you shout... OH MY ZSH!
Sudoers - Community Help Wiki