January 31, 2024

Using breakpoints in C

An easy shortcut to get a breakpoint in gdb going is by using global assembly labels.

int fun(int a, int b) {
    // do something

    asm("b:");
    // or
    asm("b: nop");

    // return something
}

The label does nothing, but it means that once gdb is open, you can simply do:

(gdb) b b

Original idea from:

In case the above does not work, please try:

Using POSIX raise

You can also try:

// main.c
#include "signal.h"

int main() {
  raise(SIGTRAP);
  return 0;
}
// Compiled with gcc -g /tmp/main.c -o /tmp/main && gdb /tmp/main

Then when running the program with gdb, this will effectively pause execution so that you can have a look around. I.e:

Neovim shortcut

vim.api.nvim_create_autocmd({"BufNewFile", "BufRead"}, {
  group = vim.api.nvim_create_augroup("UserCAutoCommands", {clear=true}),
  pattern = '*.c',
  command = [[
    noremap <F1> :normal oasm("b: nop");<ESC> |
    noremap <F2> :g/^.*asm("b: nop").*/d<CR>
  ]]
})