Sunday, September 29, 2024

Enhancing LLDB with "search history" and "completion" Support: A Guide to Better Debugging

As developers, we often find ourselves deep in debugging sessions, relying heavily on tools like LLDB (Low Level Debugger). However, one common frustration with LLDB is its lack of built-in command history and advanced line editing features by default. In this post, we'll explore two methods to enhance LLDB's functionality: using rlwrap and compiling LLDB with native readline support.

Method 1: Using rlwrap

What is rlwrap?

rlwrap (readline wrapper) is a utility that adds readline-style editing and history capabilities to command line programs that don't have these features built-in. It's a quick and easy way to improve LLDB's usability without modifying the debugger itself.

Features of rlwrap:

  • Maintains a history of commands
  • Provides searchable history (use Ctrl-R to search backwards)
  • Allows line editing with arrow keys, home/end, etc.
  • Can save history between sessions
  • Offers tab completion (if supported by the wrapped program)

Setting up rlwrap with LLDB:

  1. Install rlwrap:
    • On macOS with Homebrew: brew install rlwrap
    • On Ubuntu/Debian: sudo apt-get install rlwrap
  2. Create an alias in your shell configuration file (e.g., ~/.bashrc, ~/.zshrc):
    alias lldb='rlwrap lldb'
  3. Reload your shell configuration or start a new terminal session.

Now, when you run lldb, you'll have access to command history, line editing, and other readline features.

Method 2: Compiling LLDB with Native Readline Support

For a more integrated solution, we can compile LLDB from source with readline support enabled. This method provides native readline functionality without relying on external wrappers.

Steps to Compile LLDB with Readline:

  1. Install prerequisites:
    sudo apt-get update sudo apt-get install -y cmake ninja-build libreadline-dev
  2. Clone the LLVM project (which includes LLDB):
    git clone https://github.com/llvm/llvm-project.git cd llvm-project
  3. Create a build directory and configure CMake:
    mkdir build && cd build cmake -G Ninja ../llvm \ -DLLVM_ENABLE_PROJECTS="clang;lldb" \ -DCMAKE_BUILD_TYPE=Release \ -DLLDB_ENABLE_LIBEDIT=OFF \ -DLLDB_ENABLE_CURSES=OFF \ -DLLDB_ENABLE_READLINE=ON \ -DCMAKE_INSTALL_PREFIX=/usr/local
  4. Build and install LLDB:
    ninja lldb sudo ninja install-lldb

This process will give you an LLDB binary with native readline support, providing features like searchable history and improved line editing.

Considerations

  • The rlwrap method is quick and doesn't require recompiling LLDB, but it's an external wrapper and may not integrate as seamlessly.
  • Compiling LLDB from source provides a more integrated solution but requires more time, disk space, and technical knowledge.
  • Building LLDB replaces your system's LLDB with a custom version. Make sure to back up your current installation first.

Final words

Both methods significantly improve LLDB's usability by adding crucial features like command history and advanced line editing. The choice between using rlwrap or compiling with readline support depends on your specific needs and comfort level with building software from source.

By enhancing LLDB with these readline capabilities, you can make your debugging sessions more efficient and enjoyable. Happy debugging!

Updating linked library name in an ELF binary(without recompiling)

 To update the linked library in your ELF executable without recompiling, you can use the patchelf tool. This tool allows you to modify the dynamic linker and RPATH of ELF executables. Here's how you can do it:

  1. First, make sure you have patchelf installed. On most Linux distributions, you can install it using your package manager. For example, on Ubuntu or Debian:

    sudo apt-get install patchelf
  2. Once installed, you can use patchelf to change the linked library. The command will look like this:

    patchelf --replace-needed libffi.so.6 libffi-mine.so.6 your_executable
    Replace your_executable with the actual name of your ELF binary.
  3. After running this command, you can verify the change using the ldd command:

    ldd your_executable
    This should show that your executable is now linked against libffi-mine.so.6 instead of libffi.so.6.
  4. If libffi-mine.so.6 is not in the standard library search path, you may need to add its location to the RPATH of your executable:

    patchelf --set-rpath /path/to/library/directory your_executable
    Replace /path/to/library/directory with the actual path where libffi-snps.so.6 is located.

Remember to make a backup of your original executable before making these changes. Also, ensure that libffi-snps.so.6 is compatible with your executable, as changing the linked library can lead to runtime issues if the new library is not compatible.

This method allows you to change the linked library without recompiling, but it's generally safer to recompile with the correct library if possible, as it ensures complete compatibility.

Tuesday, September 17, 2024

A python script to print ARM cpuinfo



Here's a Python program that fetches ARM CPU features from /proc/cpuinfo and prints the corresponding ARMv extension:

https://github.com/rednaveen/aarch64_cpu_info

A comprahensive list of ARM CPUs which shows arm v8 and v9 extensions supported by ARM CPUs. This link provides list according to /proc/cpuinfo on linux.

https://gpages.juszkiewicz.com.pl/arm-socs-table/arm-socs.html 


The below link does not provide the extension codes used by ARMv8 but it gives details straight from ARM site:

https://developer.arm.com/documentation/102378/0201/Armv8-x-A-and-the-SBSA

Monday, September 16, 2024

Deploying prebuilt Clang/LLVM compiler tool chain


At below location there are few set of lines that needs to be added.

https://apt.llvm.org/

Like for example:

deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main
deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main
This needs to be added to /etc/apt/source.list
After adding update.
# LLVM
apt-get install libllvm-18-ocaml-dev libllvm18 llvm-18 llvm-18-dev llvm-18-doc llvm-18-examples llvm-18-runtime
# Clang and co
apt-get install clang-18 clang-tools-18 clang-18-doc libclang-common-18-dev libclang-18-dev libclang1-18 clang-format-18 python3-clang-18 clangd-18 clang-tidy-18
# compiler-rt
apt-get install libclang-rt-18-dev
# polly
apt-get install libpolly-18-dev
# lldb
apt-get install lldb-18
# lld (linker)
apt-get install lld-18
# libc++
apt-get install libc++-18-dev libc++abi-18-dev
# OpenMP
apt-get install libomp-18-dev
# libclc
apt-get install libclc-18-dev
# libunwind
apt-get install libunwind-18-dev
# mlir
apt-get install libmlir-18-dev mlir-18-tools
# bolt
apt-get install libbolt-18-dev bolt-18
# flang
apt-get install flang-18
# wasm support
apt-get install libclang-rt-18-dev-wasm32 libclang-rt-18-dev-wasm64 libc++-18-dev-wasm32 libc++abi-18-dev-wasm32 libclang-rt-18-dev-wasm32 libclang-rt-18-dev-wasm64

References: blog