Subject: 24.3; gud and/or gdb: Add hooks to detect when a source file is visited during debugging

From: Yuri Khan <[email-masked]>
Date: Tue, 19 Aug 2014 11:59:41 +0700

Hello,

I am writing a minor mode to control the debugger from a source buffer
by pressing single-letter keystrokes, e.g. "n" for gud-next, etc. The
mode itself is very simple — it has a keymap, and it sets the buffer
read-only while it is active so as to free these keys from their usual
'self-insert-command' bindings.

I need to be able to enable this mode for the same buffers where gdb
allows clicking in the fringe to set a breakpoint, and at the same
time as gdb sets up reaction to these clicks. For that purpose, I
currently advise the 'gdb-init-buffer' function.

Additionally, I need to detect when debugging is finished, in order to
disable the mode in all buffers, so that the user would be able to
edit files again. Currently, I advise 'gdb-reset' and loop over all
buffers, disabling the mode.

It would be nice if gdb-mi.el provided hooks that I could add to
instead of advising gdb functions.

Implementation proposal:

* Add a normal hook 'gdb-init-buffer-hook' that runs at the end of
'gdb-init-buffer' with the buffer being initialized still current.
* Add a normal hook 'gdb-reset-hook' that runs at the end of 'gdb-reset'.

Additional considerations:

The mode I am writing might be useful for Gud backends other than
gdbmi. To generalize to those, I would need the following hooks:

* A hook that can be used to enable the mode. It needs to run at the
start of a debugging session, once for each existing buffer visiting a
source file of the program being debugged, with that buffer being
current. Additionally, it needs to run when a new buffer is opened
while debugging, iff that buffer is visiting a source file of the
program being debugged.

* A hook that can be used to disable the mode. It needs to run once
when a debugging session ends. Alternatively, a hook that runs once
for each buffer in which the above hook has previously ran.

In GNU Emacs 24.3.1 (x86_64-pc-linux-gnu, GTK+ Version 3.10.7)
 of 2014-03-08 on lamiak, modified by Debian
Windowing system distributor `The X.Org Foundation', version 11.0.11501000
System Description:    Ubuntu 14.04.1 LTS

Configured using:
 `configure '--build' 'x86_64-linux-gnu' '--build' 'x86_64-linux-gnu'
 '--prefix=/usr' '--sharedstatedir=/var/lib' '--libexecdir=/usr/lib'
 '--localstatedir=/var/lib' '--infodir=/usr/share/info'
 '--mandir=/usr/share/man' '--with-pop=yes'
 '--enable-locallisppath=/etc/emacs24:/etc/emacs:/usr/local/share/emacs/24.3/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/24.3/site-lisp:/usr/share/emacs/site-lisp'
 '--with-crt-dir=/usr/lib/x86_64-linux-gnu' '--with-x=yes'
 '--with-x-toolkit=gtk3' '--with-toolkit-scroll-bars'
 'build_alias=x86_64-linux-gnu' 'CFLAGS=-g -O2 -fstack-protector
 --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wall'
 'LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro'
 'CPPFLAGS=-D_FORTIFY_SOURCE=2''

Important settings:
  value of $LC_MONETARY: en_US.UTF-8
  value of $LC_NUMERIC: en_US.UTF-8
  value of $LC_TIME: en_DK.utf8
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix
  default enable-multibyte-characters: t

Major mode: Emacs-Lisp

Minor modes in effect:
  global-whitespace-mode: t
  global-git-gutter-mode: t
  git-gutter-mode: t
  shell-dirtrack-mode: t
  show-paren-mode: t
  global-hl-line-mode: t
  cua-mode: t
  diff-auto-refine-mode: t
  auto-revert-mode: t
  yas-global-mode: t
  yas-minor-mode: t
  global-srecode-minor-mode: t
  global-ede-mode: t
  ede-minor-mode: t
  tabbar-mode: t
  desktop-save-mode: t
  server-mode: t
  tooltip-mode: t
  mouse-wheel-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  size-indication-mode: t
  column-number-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent input:
<down> <down> <down> <down> <down> <down> <down> <down>
<down> <down> <C-down> <C-down> <C-down> <C-down> <C-down>
<C-down> <C-down> <C-down> <C-down> <down> <down> <down>
<up> <up> <up> <up> <up> <up> <up> <up> <up> <up> <up>
<up> <up> <up> <up> <up> <up> <up> <up> <up> <up> <up>
<up> <up> <down> <down> <down> <down> <down> <up> <up>
<up> <down> <down> <down> <down> <down> <down> <down>
<down> <down> <down> <down> <down> <down> <down> <down>
<down> <down> <down> <down> <down> <down> <down> <down>
<M-left> <up> <up> <up> <up> <up> <up> <up> <up> <up>
<up> <up> <up> <up> <M-down> <M-right> <M-left> <f1>
f g u d - m i n o r - m o d e <return> C-g <up> <up>
<up> <up> <up> <up> <up> <up> <up> <up> <up> <up> <up>
<up> <up> <up> <up> <up> <up> <up> <up> <up> <up> <up>
<up> <down> <down> <down> <down> <down> <down> <down>
<down> <down> <down> <down> <down> <down> <down> <down>
<down> <down> <down> <down> <down> <down> <down> <down>
<down> <down> <down> <down> <down> <down> <down> <down>
<down> <down> <down> <down> <down> <down> <down> <down>
<down> <down> <down> <down> <down> <down> <down> <down>
<up> <up> <up> <up> <right> <right> <right> <right>
<right> <right> <right> <right> <right> <right> <right>
<right> <right> <right> <right> <right> <right> <right>
<right> <right> <right> <right> <right> <right> <right>
<right> <right> <right> <right> <right> <right> <right>
<right> <right> <right> <right> <right> <right> <right>
<right> <left> <left> <left> <left> <left> <left> <left>
<left> <left> <down> <left> <left> <left> <left> <left>
<left> <left> <left> <left> <left> <left> <left> <C-down>
<C-down> <C-down> <C-down> <C-down> <C-down> <C-down>
<C-down> <C-down> <C-down> <C-down> <C-down> <C-down>
<C-down> <C-down> <C-down> <C-down> <f1> v g u d -
m i n o r - m o d e - h <tab> o o k <return> C-g <M-left>
M-x r e p o r <tab> <return>

Recent messages:
Mark saved where search started
Mark set
Mark saved where search started [2 times]
Quit
Type "q" to restore previous buffer, <next> to scroll help.
byte-code: Beginning of buffer [3 times]
mouse-2, RET: find variable's definition
uncompressing gud.el.gz...done
Note: file is write protected
Here is not Git work tree [2 times]
Quit [2 times]

Load-path shadows:
~/.emacs.d/browse-kill-ring/browse-kill-ring hides
/usr/share/emacs24/site-lisp/emacs-goodies-el/browse-kill-ring
/usr/share/emacs/24.3/site-lisp/debian-startup hides
/usr/share/emacs/site-lisp/debian-startup
/usr/share/emacs24/site-lisp/dictionaries-common/ispell hides
/usr/share/emacs/24.3/lisp/textmodes/ispell
/usr/share/emacs24/site-lisp/dictionaries-common/flyspell hides
/usr/share/emacs/24.3/lisp/textmodes/flyspell

Features:
(pp misearch multi-isearch jka-compr find-func shadow sort mail-extr
emacsbug message rfc822 mml mml-sec mm-decode mm-bodies mm-encode
mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail rfc2047
rfc2045 ietf-drums mail-utils cc-langs cc-mode cc-fonts cc-guess
cc-menus cc-cmds cc-styles cc-align cc-engine cc-vars cc-defs gdb-mi
bindat json gud ibuf-ext ibuffer windmove ede/dired persistent-soft
pcache font-utils highlight-beyond-fill-column disp-table whitespace
hi-lock sql-indent sql browse-kill-ring redo git-gutter tramp
tramp-compat auth-source gnus-util mm-util mail-prsvr password-cache
tramp-loaddefs shell pcomplete paren hl-line cua-base unicode-fonts
warnings magit-key-mode magit package iswitchb ido esh-var esh-io
esh-cmd esh-opt esh-ext esh-proc esh-arg eldoc esh-groups eshell
esh-module esh-mode esh-util ediff-merg ediff-diff ediff-wind ediff-help
ediff-util ediff-mult ediff-init ediff view grep format-spec epa epg
epg-config diff-mode autorevert git-rebase-mode thingatpt
git-commit-mode log-edit pcvs-util add-log yasnippet dropdown-list
semantic/bovine/gcc semantic/dep semantic/ia semantic/analyze/refs
semantic/db-find semantic/db-ref semantic/analyze semantic/sort
semantic/scope semantic/analyze/fcn semantic/db srecode/mode
semantic/senator semantic/format semantic/ctxt semantic/wisent
semantic/wisent/wisent semantic/decorate pulse srecode/insert
srecode/filters srecode/args srecode/find srecode/map srecode/ctxt
semantic/tag-ls semantic/find srecode/compile srecode/dictionary
srecode/table srecode semantic/util-modes semantic/util semantic
semantic/tag semantic/lex semantic/fw mode-local vc-git ede/cpp-root
ede/emacs ede/speedbar ede/files ede ede/base ede/auto ede/source
eieio-base eieio-speedbar speedbar sb-image ezimage dframe eieio-custom
wid-edit eieio byte-opt bytecomp byte-compile cconv cedet tabbar
framemove advice help-fns advice-preload windcycle help-mode dired+
dired-x cl-macs gv dired-aux dired easymenu cl cl-lib desktop saveplace
server derived debian-el debian-el-loaddefs pylint compile comint
ansi-color ring haskell-mode-autoloads edmacro kmacro emacs-goodies-el
emacs-goodies-custom emacs-goodies-loaddefs easy-mmode dpkg-dev-el
dpkg-dev-el-loaddefs devhelp time-date tooltip ediff-hook vc-hooks
lisp-float-type mwheel x-win x-dnd tool-bar dnd fontset image regexp-opt
fringe tabulated-list newcomment lisp-mode register page menu-bar
rfn-eshadow timer select scroll-bar mouse jit-lock font-lock syntax
facemenu font-core frame cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese hebrew greek romanian slovak
czech european ethiopic indian cyrillic chinese case-table epa-hook
jka-cmpr-hook help simple abbrev minibuffer loaddefs button faces
cus-face macroexp files text-properties overlay sha1 md5 base64 format
env code-pages mule custom widget hashtable-print-readable backquote
make-network-process dbusbind dynamic-setting system-font-setting
font-render-setting move-toolbar gtk x-toolkit x multi-tty emacs)