Why do backtraces of some threads start from main() throughout the course?

If you are accustomed to the WinDbg debugger, then this is a completely justified question. The simple answer to the question is that of course, they do not start there, but by default, a stack trace is shown starting from the main function in GDB. We can change this behavior by using the set backtrace past-main command.

Is it possible to use scripts in GDB?

Yes, for example, in the past we wrote the following script to emulate the WinDbg dpp command in a file UserCommands.txt:

define dpp
  set $i = 0
  set $p = $arg0
  while $i < $arg1
    printf "%p: ", $p
    x/ga *(long *)$p
    set $i = $i + 1
    set $p = $p + 8
  end
end

We load the file in GDB and execute the dpp command supplying the initial address and the number of addresses to iterate (we also double check its correctness):

Get hands-on with 1400+ tech skills courses.