# requires: https://github.com/cyrus-and/gdb-dashboard
#
python
import time

# class AmosCommand(gdb.Command):

#     def __init__(self):
#         gdb.Command.__init__(self, 'amos',
#                              gdb.COMMAND_USER, gdb.COMPLETE_FILENAME)

#     def invoke(self, arg, from_tty):
#         if gdb.selected_thread().is_stopped():
#             return
#         for thread in gdb.Inferior.threads(gdb.selected_inferior()):
#             if thread.is_stopped():
#                 gdb.execute("thread {}".format(thread.num), from_tty=True)
#                 return

# AmosCommand()

# class BirdCommand(gdb.Command):

#     def __init__(self):
#         gdb.Command.__init__(self, 'bird',
#                              gdb.COMMAND_USER, gdb.COMPLETE_FILENAME)

#     def invoke(self, arg, from_tty):
#         for thread in gdb.Inferior.threads(gdb.selected_inferior()):
#             if thread.is_stopped():
#                 self.bird_interrupted = None
#                 return
#         self.bird_interrupted = gdb.selected_thread()
#         gdb.execute("interrupt", from_tty=True)
#         time.sleep(0.1)

# bc = BirdCommand()

# class AfterBirdCommand(gdb.Command):

#     def __init__(self, bc):
#         self.bc = bc
#         self.bc.bird_interrupted = None
#         gdb.Command.__init__(self, 'afterbird',
#                              gdb.COMMAND_USER, gdb.COMPLETE_FILENAME)

#     def invoke(self, arg, from_tty):
#         if self.bc.bird_interrupted is not None:
#             gdb.execute("continue {}&".format(self.bc.bird_interrupted.num), from_tty=True)
#             self.bc.bird_interrupted = None

# AfterBirdCommand(bc)

# class ThreadSwitchCommand(gdb.Command):

#     def __init__(self):
#         gdb.Command.__init__(self, 'threadswitch',
#                              gdb.COMMAND_USER, gdb.COMPLETE_FILENAME)

#     def invoke(self, arg, from_tty):
#         print(arg)
#         for threadid, filename in gdb.stopped_threads:
#             if filename == arg:
#                 gdb.execute("thread {}".format(threadid), from_tty=True)
#                 return

#         for threadid, filename in gdb.stopped_threads:
#             for bp in gdb.breakpoints():
#                 if filename == bp.location:
#                     gdb.execute("thread {}".format(threadid), from_tty=True)
#                     return

# ThreadSwitchCommand()

# def signal_stop_handler (event):
#     if (isinstance (event, gdb.StopEvent)):
#         print ("event type: stop")
#     if (isinstance (event, gdb.SignalEvent)):
#         print ("stop reason: signal")
#         print ("stop signal: %s" % (event.stop_signal))
#         if ( event.inferior_thread is not None) :
#             print ("thread num: %s" % (event.inferior_thread.num))

# def breakpoint_stop_handler (event):
#     print ("why")
#     if (isinstance (event, gdb.StopEvent)):
#         print ("event type: stop")
#     if (isinstance (event, gdb.BreakpointEvent)):
#         print ("stop reason: breakpoint")
#         print ("first breakpoint number: %s" % (event.breakpoint.number))
#         for bp in event.breakpoints:
#             print ("breakpoint number: %s" % (bp.number))
#         if ( event.inferior_thread is not None) :
#             print ("thread num: %s" % (event.inferior_thread.num))
#         else:
#             print ("all threads stopped")

# gdb.events.stop.connect(breakpoint_stop_handler)

# useful auto-redraw hooks

# class AmosCommand(gdb.Command):

#     def __init__(self):
#         gdb.Command.__init__(self, 'amos',
#                              gdb.COMMAND_USER, gdb.COMPLETE_FILENAME)

#     def invoke(self, arg, from_tty):
#         print("current thread has exited")
#         for thread in gdb.Inferior.threads(gdb.selected_inferior()):
#             if thread.is_valid() and thread.is_stopped():
#                 gdb.execute("thread {}".format(thread.num), from_tty=True)
#                 gdb.execute("dashboard", from_tty=True)
#                 return
#         for thread in gdb.Inferior.threads(gdb.selected_inferior()):
#             if thread.is_valid():
#                 gdb.execute("thread {}".format(thread.num), from_tty=True)
#                 gdb.execute("dashboard", from_tty=True)
#                 return
#         print("wtf????")
# AmosCommand()

# class BirdCommand(gdb.Command):
#     def __init__(self):
#         gdb.Command.__init__(self, 'bird', gdb.COMMAND_USER, gdb.COMPLETE_FILENAME)

#     def invoke(self, arg, from_tty):
#         try:
#             gdb.selected_thread()
#         except SystemError:
#             for thread in gdb.Inferior.threads(gdb.selected_inferior()):
#                 if thread.is_valid() and thread.is_stopped():
#                     gdb.execute("thread {}".format(thread.num), from_tty=True)
#                     gdb.execute("dashboard", from_tty=True)
#                     return
#             for thread in gdb.Inferior.threads(gdb.selected_inferior()):
#                 if thread.is_valid():
#                     gdb.execute("thread {}".format(thread.num), from_tty=True)
#                     gdb.execute("dashboard", from_tty=True)
#                     return
#         if gdb.selected_thread() is not None and gdb.selected_thread().is_stopped():
#             return
#         for thread in gdb.Inferior.threads(gdb.selected_inferior()):
#             if thread.is_valid() and thread.is_stopped():
#                 gdb.execute("thread {}".format(thread.num), from_tty=True)
#                 gdb.execute("dashboard", from_tty=True)
#                 return
#         gdb.execute("dashboard", from_tty=True)

# BirdCommand()

# class RedashCommand(gdb.Command):
#     def __init__(self):
#         gdb.Command.__init__(self, 'redash', gdb.COMMAND_USER, gdb.COMPLETE_FILENAME)

#     def invoke(self, arg, from_tty):
#         for thread in gdb.Inferior.threads(gdb.selected_inferior()):
#             if thread.is_valid() and thread.is_stopped():
#                 return
#         gdb.execute("dashboard", from_tty=True)

# RedashCommand()

# class FunctionFinishBreakpoint(gdb.FinishBreakpoint):
#     def __init__ (self):
#         gdb.FinishBreakpoint.__init__(self, gdb.newest_frame(),
#                                       internal=True)
#         self.silent = True

#     def stop(self):
#         #print("after: {}".format(int(self.return_value)))
#         return not int(self.return_value)

# class FunctionBreakpoint(gdb.Breakpoint):
#     def __init__ (self, spec):
#         gdb.Breakpoint.__init__(self, spec)
#         self.silent = True

#     def stop (self):
#         #print("before")
#         FunctionFinishBreakpoint() # set breakpoint on function return

#         return False # do not stop at function entry

# import sys
# libcxx
# sys.path.insert(0, '/tmp/gentoo/home/amos/git/ClickHouse-pretty-printer/libcxx/v2')
# from libcxx_printers_new import register_libcxx_printer_loader
# register_libcxx_printer_loader()

# clickhouse pretty printer
# sys.path.insert(0, '/tmp/gentoo/home/amos/git/ClickHouse-pretty-printer/clickhouse')
# from printers import register_ch_printers
# register_ch_printers()

end

# define hookpost-finish
#   redash
# end
# define hookpost-next
#   redash
# end
# define hookpost-step
#   redash
# end
# define hookpost-stepi
#   redash
# end
# define hookpost-continue
#   redash
# end

define hookpost-up
  dashboard
end

define hookpost-down
  dashboard
end

define hookpost-frame
  dashboard
end

# dashboard -layout stack expressions threads registers
dashboard -layout stack threads
dashboard -style syntax_highlighting 'monokai'
# dashboard stack -style compact True
# dashboard stack -style limit 3
dashboard stack -style arguments True
dashboard stack -style locals True
dashboard threads -style skip-running True

# enable auto-load
set auto-load safe-path /
set auto-load scripts-directory ~/.cgdb
set disassembly-flavor intel
set opaque-type-resolution off
set history filename ~/.gdb_history
set history save on
set history remove-duplicates 5000

handle SIGUSR1 noprint nostop ignore
handle SIGUSR2 noprint nostop ignore
# handle SIGTSTP noprint nostop pass
# handle SIGQUIT noprint nostop pass
# handle SIGINT noprint nostop pass

# set $dlopen = (void*(*)(char*, int)) dlopen
# call $dlopen("/home/amos/git/luakit/lualib.so", 1)

# set scheduler-locking off

# set auto-solib-add off
set basenames-may-differ 1
set mi-async 1
set non-stop on
set confirm off
set pagination off
# Finally, turn it on!

# skip -gfi usr/include/c++/*/*/*
# skip -gfi usr/local/include/c++/*/*/*
# skip -gfi libc++/include/*
# skip -gfi libcxx/include/*

# vim: filetype=python
# Local Variables:
# mode: python
# End:
