Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / etc / emacs-buffer.gdb
blob94a060234f5a393ed2762571c8ba1f0b55606cdc
1 # emacs-buffer.gdb --- gdb macros for recovering buffers from emacs coredumps
3 # Copyright (C) 2005-2014 Free Software Foundation, Inc.
5 # Maintainer: Noah Friedman <friedman@splode.com>
6 # Created: 2005-04-28
8 # This file is part of GNU Emacs.
10 # GNU Emacs is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # GNU Emacs is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23 # Commentary:
25 # This is a set of gdb macros for recovering the contents of buffers from
26 # an Emacs coredump; they may not always be file-backed or have a recent
27 # autosave.
29 # The Emacs executable must have debugging symbols for this to work.
30 # But you never strip Emacs, right?
32 # The main commands of interest are `ybuffer-list', `yfile-buffers',
33 # `ysave-buffer', and `ybuffer-contents'.  The `y' prefix avoids any
34 # namespace collisions with emacs/src/.gdbinit.
36 # Since the internal data structures in Emacs occasionally from time to
37 # time, you should use the version of this file that came with your
38 # particular Emacs version; older versions might not work anymore.
40 # Example usage:
42 #     $ gdb /export/src/emacs/2005-05-02--03-17/src/emacs core.emacs.6.9845
43 #     Current directory is /u/noah/
44 #     GNU gdb (6.1post-1.20040607.43rh)
45 #     ...
46 #     #0  0x400007a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
47 #     (gdb) source emacs-buffer.gdb
48 #     (gdb) ybuffer-list
49 #     B# M       Size Name                 Mode       File
50 #     -- -       ---- ----                 ----       ----
51 #      0 *        556 mail to emacs-devel@gnu.org Mail
52 #      1 *          0  *Minibuf-1*         Fundamental
53 #      2       145769 ChangeLog            Change Log /u/noah/lib/elisp/noahf/ChangeLog
54 #      3         6619 ascii-table.el       Elisp      /u/noah/lib/elisp/noahf/ascii-table.el
55 #      4 *      48396 *Messages*           Fundamental
56 #      5         3191 *Apropos*            Apropos
57 #      6        17642 init-21.el           Elisp      /u/noah/etc/init/emacs/init-21.el
58 #      7          333 cpuid.c              C          /u/noah/cpuid.c
59 #      8          230 src                  Dired
60 #      9          218 noah                 Dired
61 #     10 *         21  *Echo Area 0*       Fundamental
62 #     11 *          0  *Echo Area 1*       Fundamental
63 #     12       319952  *bbdb data*         Text       /u/noah/.bbdb
64 #     (gdb) ysave-buffer 0 mail.save
65 #     [Wrote buffer "mail to emacs-devel@gnu.org" to file mail.save]
66 #     (gdb) quit
67 #     $ ls -l mail.save
68 #     -rw-rw-rw-  1 noah user 556 May  2 04:05 mail.save
69 #     $
71 # Code:
73 # Force loading of symbols, enough to give us VALMASK etc.
74 set main
76 # When nonzero, display some extra diagnostics in various commands
77 set $yverbose = 1
78 set $yfile_buffers_only = 0
80 define ygetptr
81   set $ptr = $arg0
82   set $ptr = ((CHECK_LISP_OBJECT_TYPE ? $ptr.i : $ptr) & VALMASK) | DATA_SEG_BITS
83 end
85 define ybuffer-list
86   set $files_only         = $yfile_buffers_only
87   set $yfile_buffers_only = 0
89   if $yverbose
90     printf "B# M       Size Name                 Mode       File\n"
91     printf "-- -       ---- ----                 ----       ----\n"
92   end
94   set $i = 0
95   set $alist = Vbuffer_alist
96   while $alist != Qnil
97     ygetptr $alist
98     set $this  = ((struct Lisp_Cons *) $ptr)->car
99     set $alist = ((struct Lisp_Cons *) $ptr)->u.cdr
101     # Vbuffer_alist elts are pairs of the form (name . buffer)
102     ygetptr $this
103     set $buf  = ((struct Lisp_Cons *) $ptr)->u.cdr
104     ygetptr $buf
105     set $buf = (struct buffer *) $ptr
107     if ! ($files_only && $buf->filename_ == Qnil)
108       ygetptr $buf->name_
109       set $name = ((struct Lisp_String *) $ptr)->data
110       set $modp = ($buf->text->modiff > $buf->text->save_modiff) ? '*' : ' '
112       ygetptr $buf->mode_name_
113       set $mode = ((struct Lisp_String *) $ptr)->data
115       if $buf->filename_ != Qnil
116         ygetptr $buf->filename_
117         printf "%2d %c  %9d %-20s %-10s %s\n", \
118                $i, $modp, ($buf->text->z_byte - 1), $name, $mode, \
119                ((struct Lisp_String *) $ptr)->data
120       else
121         printf "%2d %c  %9d %-20s %-10s\n", \
122                $i, $modp, ($buf->text->z_byte - 1), $name, $mode
123       end
124     end
126     set $i++
127   end
129 document ybuffer-list
130   Display a list of buffer names, sizes, and other attributes.
131   The buffer number in the first column is used as an argument
132   to some other emacs-buffer recovery commands, e.g. `ysave-buffer'.
135 define yfile-buffers
136   set $yfile_buffers_only = 1
137   ybuffer-list
139 document yfile-buffers
140   Display a list of buffers which are associated with files.
141   This is like `ybuffer-list', but only buffers that were visiting files
142   are displayed.
145 define yset-buffer
146   set $i = $arg0
148   set $alist = Vbuffer_alist
149   while ($alist != Qnil && $i > 0)
150     ygetptr $alist
151     set $alist = ((struct Lisp_Cons *) $ptr)->u.cdr
152     set $i--
153   end
155   # Get car of alist; this is a pair (name . buffer)
156   ygetptr $alist
157   set $this = ((struct Lisp_Cons *) $ptr)->car
159   # Get the buffer object
160   ygetptr $this
161   set $this = ((struct Lisp_Cons *) $ptr)->u.cdr
163   ygetptr $this
164   set $ycurrent_buffer = (struct buffer *) $ptr
166 document yset-buffer
167   Set current buffer (for other emacs-buffer recovery commands) to the ARG'th
168   buffer as displayed by `ybuffer-list'.
171 define yget-buffer-pointers
172   yset-buffer $arg0
173   set $buf = $ycurrent_buffer->text
175   set $beg     = $buf->beg
176   set $gap     = $beg + $buf->gpt_byte
177   set $gap_end = $gap + $buf->gap_size - 1
178   set $end     = $gap_end + ($buf->z_byte - $buf->gpt_byte)
180   set $modp    = $buf->modiff > $buf->save_modiff
182   #print *$beg@($gap - $beg)
183   #print *$gap_end@($end - $gap_end)
185 document yget-buffer-pointers
186   Update convenience variables with address pointers for the ARG'th buffer
187   as displayed by `ybuffer-list'.
189   This also sets the current buffer using `yset-buffer' (which see).
192 define yget-current-buffer-name
193   set $this = $ycurrent_buffer->name_
194   ygetptr $this
195   set $ycurrent_buffer_name = ((struct Lisp_String *) $ptr)->data
197 document yget-current-buffer-name
198   Set $ycurrent_buffer_name to the name of the currently selected buffer.
201 define ycurrent-buffer
202   yget-current-buffer-name
203   printf "%s\n", $ycurrent_buffer_name
205 document ycurrent-buffer
206   Display the currently selected buffer.
209 define ydump-buffer
210   yget-buffer-pointers $arg0
211   if $buf->z_byte > 1
212     if $buf->z_byte <= $buf->gpt_byte
213       set $endptr = $beg + $buf->gpt_byte - 1
214       dump binary memory $arg1 $beg $endptr
215     else
216       dump   binary memory $arg1 $beg $gap-1
217       append binary memory $arg1 $gap_end $end
218       set $endptr = $end
219     end
220   end
222 document ydump-buffer
223   Write contents of buffer N (as numbered according to `ybuffer-list') to
224   file FILE.
226   This is mainly used as an internal subroutine for `ysave-buffer' and
227   `ybuffer-contents', which see.
230 define ysave-buffer
231   ydump-buffer $arg0 $arg1
232   if $yverbose
233     yget-current-buffer-name
234     if $buf->z_byte <= 1
235       printf "[Buffer \"%s\" is empty.]\n", $ycurrent_buffer_name
236     else
237       # Output string broken into separate calls as necessary to avoid
238       # requiring a running process for evaluation.
239       printf "[Wrote buffer \"%s\" to file ", $ycurrent_buffer_name
240       echo $arg1]\n
241     end
242   end
244 document ysave-buffer
245   Save contents of buffer N (as numbered according to `ybuffer-list') to
246   file FILE.
249 define ybuffer-contents
250   ydump-buffer $arg0 /dev/stdout
251   if $yverbose && $buf->z_byte <= 1
252     yget-current-buffer-name
253     printf "[Buffer \"%s\" is empty.]\n", $ycurrent_buffer_name
254   else
255     if *($endptr-1) != '\n'
256       echo \n
257     end
258   end
260 document ybuffer-contents
261   Write contents of buffer N (numbered according to `ybuffer-list') to stdout.
264 # local variables:
265 # mode: gdb-script
266 # end: