# vim: ft=gdb:ts=2:sw=2:sts=2
source ~/.config/gdb/dbinit_stl_views
source ~/.config/airy/python/reelf_gdb/gdbinit.py

# *.gdbinit* # origin from buildroot
## Resolve relocations immediately on startup
# set environment LD_BIND_NOW=1

# turn off pagination
set pagination off
set height 0
# DISABLED:(too verbose): show complaints per each type of unusual symbol
# set complaints 10
set output-radix 0x10

set prompt \033[0;31m──────\033[0m\n(gdb)\040
# py from gdb import prompt; prompt.substitute_prompt("\[\e[0;34m\](gdb)\[\e[0m\] ")



# Print C++ names in their source form rather than their mangled form
set print asm-demangle
set print demangle on
set demangle-style gnu-v3

## Features for C++
# SEE: http://ftp.gnu.org/old-gnu/Manuals/gdb/html_chapter/gdb_9.html#SEC58
# pretty = print inner object struct as tree instead of single line
set print pretty on
set print object on
set print static-members on
set print vtbl on


# Keep command history between sessions
set history filename ~/.cache/gdb_history
set history save on
# BAD: unavailable in old gdb <=v7.5
# set history remove-duplicates 2

# Use findsym $sp $sp+1000 to get a dump of all symbols on the stack
define findsym
  set $address = ($arg0)
  set $count = ($arg1)
  set $end = $count
  if( $address > $end )
    set $end = $address + ( $count * 4 )
  end
  while( $address < $end )
    if( *$address != 0 )
      printf "*0x%08X 0x%08X: ",$address,*$address
      info symbol *$address
    end
    set $address = $address + 4
  end
end

# Use findval addr count value to find symbol with a specific value
define findval
  set $address = ($arg0)
  set $count = ($arg1)
  set $value = ($arg2)
  set $end = $count
  if( $address > $end )
    set $end = $address + ( $count * 4 )
  end
  while( $address < $end )
    if( *$address == $value )
      printf "*%08X %08X: ",$address,*$address
      info symbol *$address
    end
  set $address = $address + 4
  end
end

# Use bta to print backtraces for all threads
define bta
  # Make things like C++ or STL classes (as function parameters)
  # not truncated (and shown as "...") in bt.
  # BAD: don't set implicitly here
  #   set print frame-arguments all
  thread apply all bt full
end

define dis
  set $sz = 0x60
  if $argc == 0
    set $addr = $pc
  else
    if $argc == 1
      set $addr = $arg0
    else
      if $argc == 2
        set $sz = $arg1
        set $addr = $arg0
      end
    end
  end
  disassemble /r $addr - $sz*2/3, +$sz
end

# Print disassembled frames
define frdis
  if $argc == 0
    info frame
    disassemble $pc-0x30, $pc+0x30
  end
  if $argc == 1
    set $i = 0
    while( $i < $arg0 )
      info frame
      disassemble
      up
      set $i = $i + 1
    end
  end
  if $argc == 2
    set $i = $arg0
    while( $i <= $arg1 )
      frame $i
      info frame
      info registers
      disassemble
      set $i = $i + 1
    end
  end
end

# Dump stack in 1-column: (like x/256x $sp-0x200)
# USAGE: xc $sp-0x100 $sp+0x100
define xc
  set $start = $arg0
  while ($start <= $arg1)
    x/1xw $start
    set $start = $start + 4
  end
end

# https://sourceware.org/gdb/onlinedocs/gdb/Output.html
# https://sourceware.org/gdb/onlinedocs/gdb/Output-Formats.html#Output-Formats
define px
  set $addr = $arg0
  set $i = 5
  while ($i > 0)
    output/xa $addr
    printf " -> "
    set $addr = *$addr
    set $i = $i - 1
  end
  printf "\n"
end


# SEE: http://stackoverflow.com/questions/12286311/gdb-backtrace-find-total-number-of-frame
define btl
  # BAD: don't set implicitly here
  #   set print frame-arguments all
  # relative absolute basename
  # BAD:(comment-out): No symbol "filename" in current context.
  # set filename-display relative
  if $argc == 0
    echo USE: btl <count> TODO: auto-parse this string OR python\n
    bt -1
  end
  if $argc == 1
    set $i = 0
    while( $i <= $arg0 )
      select-frame $i
      frame
      set $i = $i + 1
    end
    select-frame 0
  end
end


define btreg
  if $argc == 0
    echo USE: btreg <count> TODO: auto-parse this string OR python\n
    bt -1
  end
  if $argc == 1
    set $i = 0
    while( $i <= $arg0 )
      select-frame $i
      eval "echo #%02d: %08x %08x %08x\n", $i, $sp, $pc, $ra
      # info registers sp pc ra
      set $i = $i + 1
    end
    select-frame 0
  end
end


# ATT:NEED: pwd == /tmp/gdb/<bug>
define lognew
  set logging off
  shell mkdir -p log
  # ALT: init-if-undefined $variable = expression
  if $argc == 0
    eval "shell ln -sfT \"log/$(date +%%Y%%m%%d_%%H%%M%%S).cfg\" log.cfg"
  end
  if $argc == 1
    # BAD: don't work
    eval "shell ln -sfT \"log/%s.cfg\" log.cfg", $arg0
  end
  set logging on log.cfg
end

# SEE: redirect into fifo
#   https://stackoverflow.com/questions/209534/how-to-highlight-and-color-gdb-output-during-interactive-debugging
# define pipe
#   set logging off
#   set logging on /tmp/gdb.pipe
#   set $i = 0
#   while( $i <= $argc )
#     # eval "eval %s","$arg%d",$i
#     eval "$arg%d",$i
#     set $i = $i + 1
#   end
#   set logging off
#   shell grep '0x' /tmp/gdb.pipe
# end

# source /usr/share/peda/peda.py
# source ~/.cache/airy/pkg/pwndbg/gdbinit.py
# source /usr/share/pwndbg/gdbinit.py

## Pretty Printers
# python
# try:
#     import sys,glob,os
#     from libstdcxx.v6.printers import register_libstdcxx_printers
#     register_libstdcxx_printers (None)
# except ImportError:
#     print 'cant import c++ support - old gcc or not using target-gdb?'
# end
