]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-ui.el
be2b99c897553004c34338f38b0c6040e046c654
[gnu-emacs] / lisp / progmodes / gdb-ui.el
1 ;;; gdb-ui.el --- User Interface for running GDB
2
3 ;; Author: Nick Roberts <nickrob@gnu.org>
4 ;; Maintainer: FSF
5 ;; Keywords: unix, tools
6
7 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006
8 ;; Free Software Foundation, Inc.
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This mode acts as a graphical user interface to GDB. You can interact with
30 ;; GDB through the GUD buffer in the usual way, but there are also further
31 ;; buffers which control the execution and describe the state of your program.
32 ;; It separates the input/output of your program from that of GDB, if
33 ;; required, and watches expressions in the speedbar. It also uses features of
34 ;; Emacs 21 such as the fringe/display margin for breakpoints, and the toolbar
35 ;; (see the GDB Graphical Interface section in the Emacs info manual).
36
37 ;; By default, M-x gdb will start the debugger. However, if you have customised
38 ;; gud-gdb-command-name, then start it with M-x gdba.
39
40 ;; This file has evolved from gdba.el that was included with GDB 5.0 and
41 ;; written by Tom Lord and Jim Kingdon. It uses GDB's annotation interface.
42 ;; You don't need to know about annotations to use this mode as a debugger,
43 ;; but if you are interested developing the mode itself, then see the
44 ;; Annotations section in the GDB info manual.
45
46 ;; GDB developers plan to make the annotation interface obsolete. A new
47 ;; interface called GDB/MI (machine interface) has been designed to replace
48 ;; it. Some GDB/MI commands are used in this file through the CLI command
49 ;; 'interpreter mi <mi-command>'. A file called gdb-mi.el is included with
50 ;; GDB (6.2 onwards) that uses GDB/MI as the primary interface to GDB. It is
51 ;; still under development and is part of a process to migrate Emacs from
52 ;; annotations to GDB/MI.
53
54 ;; This mode SHOULD WORK WITH GDB 5.0 onwards but you will NEED GDB 6.0
55 ;; onwards to use watch expressions. It works best with GDB 6.4 where
56 ;; watch expressions will update more quickly.
57
58 ;;; Windows Platforms:
59
60 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
61 ;; explicitly in your program if you want timely display of I/O in Emacs.
62 ;; Alternatively you can make the output stream unbuffered, for example, by
63 ;; using a macro:
64
65 ;; #ifdef UNBUFFERED
66 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
67 ;; #endif
68
69 ;; and compiling with -DUNBUFFERED while debugging.
70
71 ;;; Known Bugs:
72
73 ;; 1) Strings that are watched don't update in the speedbar when their
74 ;; contents change unless the first character changes.
75 ;; 2) Cannot handle multiple debug sessions.
76 ;; 3) M-x gdb doesn't work with "run" command in .gdbinit, use M-x gdba instead.
77 ;; 4) M-x gdb doesn't work if the corefile is specified in the command in the
78 ;; minibuffer, use M-x gdba instead (or specify the core in the GUD buffer).
79 ;; 5) If you wish to call procedures from your program in GDB
80 ;; e.g "call myproc ()", "p mysquare (5)" then use level 2 annotations
81 ;; "gdb --annotate=2 myprog" to keep source buffer/selected frame fixed.
82
83 ;;; Problems with watch expressions, GDB/MI:
84 ;; 1) They go out of scope when the inferior is re-run.
85 ;; 2) -stack-list-locals has a type field but also prints type in values field.
86 ;; 3) VARNUM increments even when variable object is not created
87 ;; (maybe trivial).
88
89 ;;; TODO:
90 ;; 1) Use MI command -data-read-memory for memory window.
91 ;; 2) Use tree-widget.el instead of the speedbar for watch-expressions?
92 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
93
94 ;;; Code:
95
96 (require 'gud)
97
98 (defvar tool-bar-map)
99 (defvar speedbar-initial-expansion-list-name)
100
101 (defvar gdb-pc-address nil "Initialization for Assembler buffer.
102 Set to \"main\" at start if gdb-show-main is t.")
103 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
104 (defvar gdb-previous-frame-address nil)
105 (defvar gdb-memory-address "main")
106 (defvar gdb-previous-frame nil)
107 (defvar gdb-selected-frame nil)
108 (defvar gdb-frame-number nil)
109 (defvar gdb-current-language nil)
110 (defvar gdb-var-list nil
111 "List of variables in watch window.
112 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS FP)
113 where STATUS is nil (unchanged), `changed' or `out-of-scope', FP the frame
114 address for root variables.")
115 (defvar gdb-main-file nil "Source file from which program execution begins.")
116 (defvar gud-old-arrow nil)
117 (defvar gdb-overlay-arrow-position nil)
118 (defvar gdb-stack-position nil)
119 (defvar gdb-server-prefix nil)
120 (defvar gdb-flush-pending-output nil)
121 (defvar gdb-location-alist nil
122 "Alist of breakpoint numbers and full filenames.")
123 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
124 and #define directives otherwise.")
125 (defvar gdb-error "Non-nil when GDB is reporting an error.")
126 (defvar gdb-macro-info nil
127 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
128 (defvar gdb-buffer-fringe-width nil)
129 (defvar gdb-signalled nil)
130 (defvar gdb-source-window nil)
131 (defvar gdb-inferior-status nil)
132 (defvar gdb-continuation nil)
133 (defvar gdb-look-up-stack nil)
134 (defvar gdb-frame-begin nil
135 "Non-nil when GDB generates frame-begin annotation.")
136 (defvar gdb-printing t)
137
138 (defvar gdb-buffer-type nil
139 "One of the symbols bound in `gdb-buffer-rules'.")
140 (make-variable-buffer-local 'gdb-buffer-type)
141
142 (defvar gdb-input-queue ()
143 "A list of gdb command objects.")
144
145 (defvar gdb-prompting nil
146 "True when gdb is idle with no pending input.")
147
148 (defvar gdb-output-sink 'user
149 "The disposition of the output of the current gdb command.
150 Possible values are these symbols:
151
152 `user' -- gdb output should be copied to the GUD buffer
153 for the user to see.
154
155 `inferior' -- gdb output should be copied to the inferior-io buffer.
156
157 `pre-emacs' -- output should be ignored util the post-prompt
158 annotation is received. Then the output-sink
159 becomes:...
160 `emacs' -- output should be collected in the partial-output-buffer
161 for subsequent processing by a command. This is the
162 disposition of output generated by commands that
163 gdb mode sends to gdb on its own behalf.
164 `post-emacs' -- ignore output until the prompt annotation is
165 received, then go to USER disposition.
166
167 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
168 \(`user' and `emacs').")
169
170 (defvar gdb-current-item nil
171 "The most recent command item sent to gdb.")
172
173 (defvar gdb-pending-triggers '()
174 "A list of trigger functions that have run later than their output
175 handlers.")
176
177 (defvar gdb-first-post-prompt nil)
178 (defvar gdb-version nil)
179 (defvar gdb-locals-font-lock-keywords nil)
180 (defvar gdb-source-file-list nil
181 "List of source files for the current executable")
182 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
183
184 (defvar gdb-locals-font-lock-keywords-1
185 '(
186 ;; var = (struct struct_tag) value
187 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
188 (1 font-lock-variable-name-face)
189 (3 font-lock-keyword-face)
190 (4 font-lock-type-face))
191 ;; var = (type) value
192 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
193 (1 font-lock-variable-name-face)
194 (3 font-lock-type-face))
195 ;; var = val
196 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
197 (1 font-lock-variable-name-face))
198 )
199 "Font lock keywords used in `gdb-local-mode'.")
200
201 (defvar gdb-locals-font-lock-keywords-2
202 '(
203 ;; var = type value
204 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
205 (1 font-lock-variable-name-face)
206 (3 font-lock-type-face))
207 )
208 "Font lock keywords used in `gdb-local-mode'.")
209
210 ;; Variables for GDB 6.4+
211 (defvar gdb-register-names nil "List of register names.")
212 (defvar gdb-changed-registers nil
213 "List of changed register numbers (strings).")
214
215 ;;;###autoload
216 (defun gdba (command-line)
217 "Run gdb on program FILE in buffer *gud-FILE*.
218 The directory containing FILE becomes the initial working directory
219 and source-file directory for your debugger.
220
221 If `gdb-many-windows' is nil (the default value) then gdb just
222 pops up the GUD buffer unless `gdb-show-main' is t. In this case
223 it starts with two windows: one displaying the GUD buffer and the
224 other with the source file with the main routine of the inferior.
225
226 If `gdb-many-windows' is t, regardless of the value of
227 `gdb-show-main', the layout below will appear unless
228 `gdb-use-separate-io-buffer' is nil when the source buffer
229 occupies the full width of the frame. Keybindings are shown in
230 some of the buffers.
231
232 Watch expressions appear in the speedbar/slowbar.
233
234 The following commands help control operation :
235
236 `gdb-many-windows' - Toggle the number of windows gdb uses.
237 `gdb-restore-windows' - To restore the window layout.
238
239 See Info node `(emacs)GDB Graphical Interface' for a more
240 detailed description of this mode.
241
242
243 +----------------------------------------------------------------------+
244 | GDB Toolbar |
245 +-----------------------------------+----------------------------------+
246 | GUD buffer (I/O of GDB) | Locals buffer |
247 | | |
248 | | |
249 | | |
250 +-----------------------------------+----------------------------------+
251 | Source buffer | I/O buffer (of debugged program) |
252 | | (comint-mode) |
253 | | |
254 | | |
255 | | |
256 | | |
257 | | |
258 | | |
259 +-----------------------------------+----------------------------------+
260 | Stack buffer | Breakpoints buffer |
261 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
262 | | RET gdb-goto-breakpoint |
263 | | D gdb-delete-breakpoint |
264 +-----------------------------------+----------------------------------+"
265 ;;
266 (interactive (list (gud-query-cmdline 'gdba)))
267 ;;
268 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
269 (gdb command-line)
270 (gdb-init-1))
271
272 (defcustom gdb-debug-ring-max 128
273 "Maximum size of `gdb-debug-ring'."
274 :group 'gud
275 :type 'integer
276 :version "22.1")
277
278 (defvar gdb-debug-ring nil
279 "List of commands, most recent first, sent to and replies received from GDB.
280 This variable is used to debug GDB-UI.")
281
282 ;;;###autoload
283 (defcustom gdb-enable-debug nil
284 "Non-nil means record the process input and output in `gdb-debug-ring'."
285 :type 'boolean
286 :group 'gud
287 :version "22.1")
288
289 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
290 "Shell command for generating a list of defined macros in a source file.
291 This list is used to display the #define directive associated
292 with an identifier as a tooltip. It works in a debug session with
293 GDB, when gud-tooltip-mode is t.
294
295 Set `gdb-cpp-define-alist-flags' for any include paths or
296 predefined macros."
297 :type 'string
298 :group 'gud
299 :version "22.1")
300
301 (defcustom gdb-cpp-define-alist-flags ""
302 "Preprocessor flags for `gdb-cpp-define-alist-program'."
303 :type 'string
304 :group 'gud
305 :version "22.1")
306
307 (defcustom gdb-show-main nil
308 "Non-nil means display source file containing the main routine at startup.
309 Also display the main routine in the disassembly buffer if present."
310 :type 'boolean
311 :group 'gud
312 :version "22.1")
313
314 (defcustom gdb-many-windows nil
315 "nil means just pop up the GUD buffer unless `gdb-show-main' is t.
316 In this case it starts with two windows: one displaying the GUD
317 buffer and the other with the source file with the main routine
318 of the debugged program. Non-nil means display the layout shown for
319 `gdba'."
320 :type 'boolean
321 :group 'gud
322 :version "22.1")
323
324 (defcustom gdb-use-separate-io-buffer nil
325 "Non-nil means display output from the debugged program in a separate buffer."
326 :type 'boolean
327 :group 'gud
328 :version "22.1")
329
330 (defun gdb-force-mode-line-update (status)
331 (let ((buffer gud-comint-buffer))
332 (if (and buffer (buffer-name buffer))
333 (with-current-buffer buffer
334 (setq mode-line-process
335 (format ":%s [%s]"
336 (process-status (get-buffer-process buffer)) status))
337 ;; Force mode line redisplay soon.
338 (force-mode-line-update)))))
339
340 (defun gdb-many-windows (arg)
341 "Toggle the number of windows in the basic arrangement.
342 With arg, display additional buffers iff arg is positive."
343 (interactive "P")
344 (setq gdb-many-windows
345 (if (null arg)
346 (not gdb-many-windows)
347 (> (prefix-numeric-value arg) 0)))
348 (message (format "Display of other windows %sabled"
349 (if gdb-many-windows "en" "dis")))
350 (if (and gud-comint-buffer
351 (buffer-name gud-comint-buffer))
352 (condition-case nil
353 (gdb-restore-windows)
354 (error nil))))
355
356 (defun gdb-use-separate-io-buffer (arg)
357 "Toggle separate IO for debugged program.
358 With arg, use separate IO iff arg is positive."
359 (interactive "P")
360 (setq gdb-use-separate-io-buffer
361 (if (null arg)
362 (not gdb-use-separate-io-buffer)
363 (> (prefix-numeric-value arg) 0)))
364 (message (format "Separate IO %sabled"
365 (if gdb-use-separate-io-buffer "en" "dis")))
366 (if (and gud-comint-buffer
367 (buffer-name gud-comint-buffer))
368 (condition-case nil
369 (if gdb-use-separate-io-buffer
370 (if gdb-many-windows (gdb-restore-windows))
371 (kill-buffer (gdb-inferior-io-name)))
372 (error nil))))
373
374 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
375
376 (defun gdb-create-define-alist ()
377 "Create an alist of #define directives for GUD tooltips."
378 (let* ((file (buffer-file-name))
379 (output
380 (with-output-to-string
381 (with-current-buffer standard-output
382 (call-process shell-file-name
383 (if (file-exists-p file) file nil)
384 (list t nil) nil "-c"
385 (concat gdb-cpp-define-alist-program " "
386 gdb-cpp-define-alist-flags)))))
387 (define-list (split-string output "\n" t)) (name))
388 (setq gdb-define-alist nil)
389 (dolist (define define-list)
390 (setq name (nth 1 (split-string define "[( ]")))
391 (push (cons name define) gdb-define-alist))))
392
393 (defun gdb-tooltip-print (expr)
394 (tooltip-show
395 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
396 (goto-char (point-min))
397 (let ((string
398 (if (search-forward "=" nil t)
399 (concat expr (buffer-substring (- (point) 2) (point-max)))
400 (buffer-string))))
401 ;; remove newline for gud-tooltip-echo-area
402 (substring string 0 (- (length string) 1))))
403 (or gud-tooltip-echo-area tooltip-use-echo-area)))
404
405 ;; If expr is a macro for a function don't print because of possible dangerous
406 ;; side-effects. Also printing a function within a tooltip generates an
407 ;; unexpected starting annotation (phase error).
408 (defun gdb-tooltip-print-1 (expr)
409 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
410 (goto-char (point-min))
411 (if (search-forward "expands to: " nil t)
412 (unless (looking-at "\\S-+.*(.*).*")
413 (gdb-enqueue-input
414 (list (concat gdb-server-prefix "print " expr "\n")
415 `(lambda () (gdb-tooltip-print ,expr))))))))
416
417 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
418
419 (defun gdb-set-gud-minor-mode-existing-buffers ()
420 "Create list of source files for current GDB session."
421 (goto-char (point-min))
422 (when (search-forward "read in on demand:" nil t)
423 (while (re-search-forward gdb-source-file-regexp nil t)
424 (push (file-name-nondirectory (or (match-string 1) (match-string 2)))
425 gdb-source-file-list))
426 (dolist (buffer (buffer-list))
427 (with-current-buffer buffer
428 (when (and buffer-file-name
429 (member (file-name-nondirectory buffer-file-name)
430 gdb-source-file-list))
431 (set (make-local-variable 'gud-minor-mode) 'gdba)
432 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
433 (when gud-tooltip-mode
434 (make-local-variable 'gdb-define-alist)
435 (gdb-create-define-alist)
436 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
437 (gdb-force-mode-line-update
438 (propertize "ready" 'face font-lock-variable-name-face)))
439
440 (defun gdb-find-watch-expression ()
441 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
442 (varnum (car var)) expr array)
443 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
444 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
445 (component-list (split-string (match-string 2 varnum) "\\." t)))
446 (setq expr (nth 1 var1))
447 (setq varnumlet (car var1))
448 (dolist (component component-list)
449 (setq var2 (assoc varnumlet gdb-var-list))
450 (setq expr (concat expr
451 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
452 (concat "[" component "]")
453 (concat "." component))))
454 (setq varnumlet (concat varnumlet "." component)))
455 expr)))
456
457 (defun gdb-init-1 ()
458 (set (make-local-variable 'gud-minor-mode) 'gdba)
459 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
460 ;;
461 (gud-def gud-break (if (not (string-match "Machine" mode-name))
462 (gud-call "break %f:%l" arg)
463 (save-excursion
464 (beginning-of-line)
465 (forward-char 2)
466 (gud-call "break *%a" arg)))
467 "\C-b" "Set breakpoint at current line or address.")
468 ;;
469 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
470 (gud-call "clear %f:%l" arg)
471 (save-excursion
472 (beginning-of-line)
473 (forward-char 2)
474 (gud-call "clear *%a" arg)))
475 "\C-d" "Remove breakpoint at current line or address.")
476 ;;
477 (gud-def gud-until (if (not (string-match "Machine" mode-name))
478 (gud-call "until %f:%l" arg)
479 (save-excursion
480 (beginning-of-line)
481 (forward-char 2)
482 (gud-call "until *%a" arg)))
483 "\C-u" "Continue to current line or address.")
484 ;;
485 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
486 nil "Start or continue execution.")
487
488 ;; For debugging Emacs only.
489 (gud-def gud-pp
490 (gud-call
491 (concat
492 "pp1 " (if (eq (buffer-local-value
493 'major-mode (window-buffer)) 'speedbar-mode)
494 (gdb-find-watch-expression) "%e")) arg)
495 nil "Print the emacs s-expression.")
496
497 (define-key gud-minor-mode-map [left-margin mouse-1]
498 'gdb-mouse-set-clear-breakpoint)
499 (define-key gud-minor-mode-map [left-fringe mouse-1]
500 'gdb-mouse-set-clear-breakpoint)
501 (define-key gud-minor-mode-map [left-margin C-mouse-1]
502 'gdb-mouse-toggle-breakpoint-margin)
503 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
504 'gdb-mouse-toggle-breakpoint-fringe)
505
506 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
507 'gdb-mouse-until)
508 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
509 'gdb-mouse-until)
510 (define-key gud-minor-mode-map [left-margin mouse-3]
511 'gdb-mouse-until)
512 (define-key gud-minor-mode-map [left-fringe mouse-3]
513 'gdb-mouse-until)
514
515 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
516 'gdb-mouse-jump)
517 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
518 'gdb-mouse-jump)
519 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
520 'gdb-mouse-jump)
521 (define-key gud-minor-mode-map [left-margin C-mouse-3]
522 'gdb-mouse-jump)
523
524 (setq comint-input-sender 'gdb-send)
525
526 ;; (re-)initialize
527 (setq gdb-pc-address (if gdb-show-main "main" nil))
528 (setq gdb-previous-frame-address nil
529 gdb-memory-address "main"
530 gdb-previous-frame nil
531 gdb-selected-frame nil
532 gdb-current-language nil
533 gdb-frame-number nil
534 gdb-var-list nil
535 gdb-main-file nil
536 gdb-first-post-prompt t
537 gdb-prompting nil
538 gdb-input-queue nil
539 gdb-current-item nil
540 gdb-pending-triggers nil
541 gdb-output-sink 'user
542 gdb-server-prefix "server "
543 gdb-flush-pending-output nil
544 gdb-location-alist nil
545 gdb-source-file-list nil
546 gdb-error nil
547 gdb-macro-info nil
548 gdb-buffer-fringe-width (car (window-fringes))
549 gdb-debug-ring nil
550 gdb-signalled nil
551 gdb-source-window nil
552 gdb-inferior-status nil
553 gdb-continuation nil
554 gdb-look-up-stack nil
555 gdb-frame-begin nil
556 gdb-printing t
557 gud-old-arrow nil)
558
559 (setq gdb-buffer-type 'gdba)
560
561 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
562
563 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
564 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
565 'gdb-get-version)))
566
567 (defun gdb-init-2 ()
568 (if (eq window-system 'w32)
569 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
570 (gdb-enqueue-input (list "set height 0\n" 'ignore))
571 (gdb-enqueue-input (list "set width 0\n" 'ignore))
572
573 (if (string-equal gdb-version "pre-6.4")
574 (progn
575 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
576 'gdb-set-gud-minor-mode-existing-buffers))
577 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
578 (gdb-enqueue-input
579 (list "server interpreter mi -data-list-register-names\n"
580 'gdb-get-register-names))
581 ; Needs GDB 6.2 onwards.
582 (gdb-enqueue-input
583 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
584 'gdb-set-gud-minor-mode-existing-buffers-1))
585 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
586
587 ;; Find source file and compilation directory here.
588 ;; Works for C, C++, Fortran and Ada but not Java (GDB 6.4)
589 (gdb-enqueue-input (list "server list\n" 'ignore))
590 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
591
592 (run-hooks 'gdba-mode-hook))
593
594 (defun gdb-get-version ()
595 (goto-char (point-min))
596 (if (re-search-forward "Undefined\\( mi\\)* command:" nil t)
597 (setq gdb-version "pre-6.4")
598 (setq gdb-version "6.4+"))
599 (gdb-init-2))
600
601 (defun gdb-mouse-until (event)
602 "Continue running until a source line past the current line.
603 The destination source line can be selected either by clicking with mouse-2
604 on the fringe/margin or dragging the arrow with mouse-1 (default bindings)."
605 (interactive "e")
606 (if gud-overlay-arrow-position
607 (let ((start (event-start event))
608 (end (event-end event))
609 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
610 (if (not (string-match "Machine" mode-name))
611 (if (equal buffer (window-buffer (posn-window end)))
612 (with-current-buffer buffer
613 (when (or (equal start end)
614 (equal (posn-point start)
615 (marker-position
616 gud-overlay-arrow-position)))
617 (setq line (line-number-at-pos (posn-point end)))
618 (gud-call (concat "until " (number-to-string line))))))
619 (if (equal (marker-buffer gdb-overlay-arrow-position)
620 (window-buffer (posn-window end)))
621 (when (or (equal start end)
622 (equal (posn-point start)
623 (marker-position
624 gdb-overlay-arrow-position)))
625 (save-excursion
626 (goto-line (line-number-at-pos (posn-point end)))
627 (forward-char 2)
628 (gud-call (concat "until *%a")))))))))
629
630 (defun gdb-mouse-jump (event)
631 "Set execution address/line.
632 The destination source line can be selected either by clicking with mouse-2
633 on the fringe/margin or dragging the arrow with mouse-1 (default bindings).
634 Unlike gdb-mouse-until the destination address can be before the current
635 line, and no execution takes place."
636 (interactive "e")
637 (if gud-overlay-arrow-position
638 (let ((start (event-start event))
639 (end (event-end event))
640 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
641 (if (not (string-match "Machine" mode-name))
642 (if (equal buffer (window-buffer (posn-window end)))
643 (with-current-buffer buffer
644 (when (or (equal start end)
645 (equal (posn-point start)
646 (marker-position
647 gud-overlay-arrow-position)))
648 (setq line (line-number-at-pos (posn-point end)))
649 (progn (gud-call (concat "tbreak " (number-to-string line)))
650 (gud-call (concat "jump " (number-to-string line)))))))
651 (if (equal (marker-buffer gdb-overlay-arrow-position)
652 (window-buffer (posn-window end)))
653 (when (or (equal start end)
654 (equal (posn-point start)
655 (marker-position
656 gdb-overlay-arrow-position)))
657 (save-excursion
658 (goto-line (line-number-at-pos (posn-point end)))
659 (forward-char 2)
660 (progn
661 (gud-call (concat "tbreak *%a"))
662 (gud-call (concat "jump *%a"))))))))))
663
664 (defcustom gdb-speedbar-auto-raise nil
665 "If non-nil raise speedbar every time display of watch expressions is\
666 updated."
667 :type 'boolean
668 :group 'gud
669 :version "22.1")
670
671 (defun gdb-speedbar-auto-raise (arg)
672 "Toggle automatic raising of the speedbar for watch expressions.
673 With arg, automatically raise speedbar iff arg is positive."
674 (interactive "P")
675 (setq gdb-speedbar-auto-raise
676 (if (null arg)
677 (not gdb-speedbar-auto-raise)
678 (> (prefix-numeric-value arg) 0)))
679 (message (format "Auto raising %sabled"
680 (if gdb-speedbar-auto-raise "en" "dis"))))
681
682 (defcustom gdb-use-colon-colon-notation nil
683 "If non-nil use FUN::VAR format to display variables in the speedbar."
684 :type 'boolean
685 :group 'gud
686 :version "22.1")
687
688 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
689 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
690
691 (defun gud-watch (&optional arg event)
692 "Watch expression at point.
693 With arg, enter name of variable to be watched in the minibuffer."
694 (interactive (list current-prefix-arg last-input-event))
695 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
696 (if (memq minor-mode '(gdbmi gdba))
697 (progn
698 (if event (posn-set-point (event-end event)))
699 (require 'tooltip)
700 (save-selected-window
701 (let ((expr
702 (if arg
703 (completing-read "Name of variable: "
704 'gud-gdb-complete-command)
705 (if (and transient-mark-mode mark-active)
706 (buffer-substring (region-beginning) (region-end))
707 (tooltip-identifier-from-point (point))))))
708 (speedbar 1)
709 (catch 'already-watched
710 (dolist (var gdb-var-list)
711 (unless (string-match "\\." (car var))
712 (if (string-equal expr (nth 1 var))
713 (throw 'already-watched nil))))
714 (set-text-properties 0 (length expr) nil expr)
715 (gdb-enqueue-input
716 (list
717 (if (eq minor-mode 'gdba)
718 (concat
719 "server interpreter mi \"-var-create - * " expr "\"\n")
720 (concat"-var-create - * " expr "\n"))
721 `(lambda () (gdb-var-create-handler ,expr))))))))
722 (message "gud-watch is a no-op in this mode."))))
723
724 (defconst gdb-var-create-regexp
725 "name=\"\\(.*?\\)\",.*numchild=\"\\(.*?\\)\",.*type=\"\\(.*?\\)\"")
726
727 (defun gdb-var-create-handler (expr)
728 (goto-char (point-min))
729 (if (re-search-forward gdb-var-create-regexp nil t)
730 (let ((var (list
731 (match-string 1)
732 (if (and (string-equal gdb-current-language "c")
733 gdb-use-colon-colon-notation gdb-selected-frame)
734 (setq expr (concat gdb-selected-frame "::" expr))
735 expr)
736 (match-string 2)
737 (match-string 3)
738 nil nil gdb-frame-address)))
739 (push var gdb-var-list)
740 (unless (string-equal
741 speedbar-initial-expansion-list-name "GUD")
742 (speedbar-change-initial-expansion-list "GUD"))
743 (gdb-enqueue-input
744 (list
745 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
746 (concat "server interpreter mi \"0-var-evaluate-expression "
747 (car var) "\"\n")
748 (concat "0-var-evaluate-expression " (car var) "\n"))
749 `(lambda () (gdb-var-evaluate-expression-handler
750 ,(car var) nil)))))
751 (if (search-forward "Undefined command" nil t)
752 (message-box "Watching expressions requires GDB 6.0 onwards")
753 (message-box "No symbol \"%s\" in current context." expr))))
754
755 (defun gdb-speedbar-update ()
756 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
757 ;; Dummy command to update speedbar even when idle.
758 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
759 ;; Keep gdb-pending-triggers non-nil till end.
760 (push 'gdb-speedbar-timer gdb-pending-triggers)))
761
762 (defun gdb-speedbar-timer-fn ()
763 (setq gdb-pending-triggers
764 (delq 'gdb-speedbar-timer gdb-pending-triggers))
765 (speedbar-timer-fn))
766
767 (defun gdb-var-evaluate-expression-handler (varnum changed)
768 (goto-char (point-min))
769 (re-search-forward "\\(.+\\)\\^done,value=\\(\".*\"\\)" nil t)
770 (setq gdb-pending-triggers
771 (delq (string-to-number (match-string 1)) gdb-pending-triggers))
772 (let ((var (assoc varnum gdb-var-list)))
773 (when var
774 (if changed (setcar (nthcdr 5 var) 'changed))
775 (setcar (nthcdr 4 var) (read (match-string 2)))))
776 (gdb-speedbar-update))
777
778 (defun gdb-var-list-children (varnum)
779 (gdb-enqueue-input
780 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
781 `(lambda () (gdb-var-list-children-handler ,varnum)))))
782
783 (defconst gdb-var-list-children-regexp
784 "child={.*?name=\"\\(.*?\\)\",.*?exp=\"\\(.*?\\)\",.*?\
785 numchild=\"\\(.*?\\)\"\\(}\\|,.*?\\(type=\"\\(.*?\\)\"\\)?.*?}\\)")
786
787 (defun gdb-var-list-children-handler (varnum)
788 (goto-char (point-min))
789 (let ((var-list nil))
790 (catch 'child-already-watched
791 (dolist (var gdb-var-list)
792 (if (string-equal varnum (car var))
793 (progn
794 (push var var-list)
795 (while (re-search-forward gdb-var-list-children-regexp nil t)
796 (let ((varchild (list (match-string 1)
797 (match-string 2)
798 (match-string 3)
799 (match-string 6)
800 nil nil)))
801 (if (assoc (car varchild) gdb-var-list)
802 (throw 'child-already-watched nil))
803 (push varchild var-list)
804 (gdb-enqueue-input
805 (list
806 (concat
807 "server interpreter mi \"0-var-evaluate-expression "
808 (car varchild) "\"\n")
809 `(lambda () (gdb-var-evaluate-expression-handler
810 ,(car varchild) nil)))))))
811 (push var var-list)))
812 (setq gdb-var-list (nreverse var-list)))))
813
814 (defun gdb-var-update ()
815 (when (not (member 'gdb-var-update gdb-pending-triggers))
816 (gdb-enqueue-input
817 (list "server interpreter mi \"-var-update *\"\n"
818 'gdb-var-update-handler))
819 (push 'gdb-var-update gdb-pending-triggers)))
820
821 (defconst gdb-var-update-regexp
822 "{.*?name=\"\\(.*?\\)\",.*?in_scope=\"\\(.*?\\)\",.*?\
823 type_changed=\".*?\".*?}")
824
825 (defun gdb-var-update-handler ()
826 (dolist (var gdb-var-list)
827 (setcar (nthcdr 5 var) nil))
828 (goto-char (point-min))
829 (let ((n 0))
830 (while (re-search-forward gdb-var-update-regexp nil t)
831 (let ((varnum (match-string 1)))
832 (if (string-equal (match-string 2) "false")
833 (let ((var (assoc varnum gdb-var-list)))
834 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
835 (setq n (1+ n))
836 (push n gdb-pending-triggers)
837 (gdb-enqueue-input
838 (list
839 (concat "server interpreter mi \"" (number-to-string n)
840 "-var-evaluate-expression " varnum "\"\n")
841 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t))))))))
842 (setq gdb-pending-triggers
843 (delq 'gdb-var-update gdb-pending-triggers)))
844
845 (defun gdb-var-delete ()
846 "Delete watch expression at point from the speedbar."
847 (interactive)
848 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
849 '(gdbmi gdba))
850 (let ((text (speedbar-line-text)))
851 ;; Can't use \\S-+ for whitespace because
852 ;; speedbar has a whacky syntax table.
853 (string-match "\\([^ \t]+\\)" text)
854 (let ((expr (match-string 1 text)) var varnum)
855 (catch 'expr-found
856 (dolist (var1 gdb-var-list)
857 (when (string-equal expr (nth 1 var1))
858 (setq var var1)
859 (setq varnum (car var1))
860 (throw 'expr-found nil))))
861 (unless (string-match "\\." (car var))
862 (gdb-enqueue-input
863 (list
864 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
865 'gdba)
866 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
867 (concat "-var-delete " varnum "\n"))
868 'ignore))
869 (setq gdb-var-list (delq var gdb-var-list))
870 (dolist (varchild gdb-var-list)
871 (if (string-match (concat (car var) "\\.") (car varchild))
872 (setq gdb-var-list (delq varchild gdb-var-list)))))))))
873
874 (defun gdb-edit-value (text token indent)
875 "Assign a value to a variable displayed in the speedbar."
876 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
877 (varnum (car var)) (value))
878 (setq value (read-string "New value: "))
879 (gdb-enqueue-input
880 (list
881 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
882 (concat "server interpreter mi \"-var-assign "
883 varnum " " value "\"\n")
884 (concat "-var-assign " varnum " " value "\n"))
885 `(lambda () (gdb-edit-value-handler ,value))))))
886
887 (defun gdb-edit-value-handler (value)
888 (goto-char (point-min))
889 (if (re-search-forward gdb-error-regexp nil t)
890 (message-box "Invalid number or expression (%s)" value)))
891
892 (defcustom gdb-show-changed-values t
893 "If non-nil change the face of out of scope variables and changed values.
894 Out of scope variables are suppressed with `shadow' face.
895 Changed values are highlighted with the face `font-lock-warning-face'."
896 :type 'boolean
897 :group 'gud
898 :version "22.1")
899
900 (defun gdb-speedbar-expand-node (text token indent)
901 "Expand the node the user clicked on.
902 TEXT is the text of the button we clicked on, a + or - item.
903 TOKEN is data related to this node.
904 INDENT is the current indentation depth."
905 (if (and gud-comint-buffer (buffer-name gud-comint-buffer))
906 (progn
907 (cond ((string-match "+" text) ;expand this node
908 (if (and (eq (buffer-local-value
909 'gud-minor-mode gud-comint-buffer) 'gdba)
910 (string-equal gdb-version "pre-6.4"))
911 (gdb-var-list-children token)
912 (gdb-var-list-children-1 token)))
913 ((string-match "-" text) ;contract this node
914 (dolist (var gdb-var-list)
915 (if (string-match (concat token "\\.") (car var))
916 (setq gdb-var-list (delq var gdb-var-list))))
917 (speedbar-change-expand-button-char ?+)
918 (speedbar-delete-subblock indent))
919 (t (error "Ooops... not sure what to do")))
920 (speedbar-center-buffer-smartly))
921 (message-box "GUD session has been killed")))
922
923 (defun gdb-get-target-string ()
924 (with-current-buffer gud-comint-buffer
925 gud-target-name))
926 \f
927
928 ;;
929 ;; gdb buffers.
930 ;;
931 ;; Each buffer has a TYPE -- a symbol that identifies the function
932 ;; of that particular buffer.
933 ;;
934 ;; The usual gdb interaction buffer is given the type `gdba' and
935 ;; is constructed specially.
936 ;;
937 ;; Others are constructed by gdb-get-buffer-create and
938 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
939
940 (defvar gdb-buffer-rules-assoc '())
941
942 (defun gdb-get-buffer (key)
943 "Return the gdb buffer tagged with type KEY.
944 The key should be one of the cars in `gdb-buffer-rules-assoc'."
945 (save-excursion
946 (gdb-look-for-tagged-buffer key (buffer-list))))
947
948 (defun gdb-get-buffer-create (key)
949 "Create a new gdb buffer of the type specified by KEY.
950 The key should be one of the cars in `gdb-buffer-rules-assoc'."
951 (or (gdb-get-buffer key)
952 (let* ((rules (assoc key gdb-buffer-rules-assoc))
953 (name (funcall (gdb-rules-name-maker rules)))
954 (new (get-buffer-create name)))
955 (with-current-buffer new
956 (let ((trigger))
957 (if (cdr (cdr rules))
958 (setq trigger (funcall (car (cdr (cdr rules))))))
959 (setq gdb-buffer-type key)
960 (set (make-local-variable 'gud-minor-mode)
961 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
962 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
963 (if trigger (funcall trigger)))
964 new))))
965
966 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
967
968 (defun gdb-look-for-tagged-buffer (key bufs)
969 (let ((retval nil))
970 (while (and (not retval) bufs)
971 (set-buffer (car bufs))
972 (if (eq gdb-buffer-type key)
973 (setq retval (car bufs)))
974 (setq bufs (cdr bufs)))
975 retval))
976
977 ;;
978 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
979 ;; at least one and possible more functions. The functions have these
980 ;; roles in defining a buffer type:
981 ;;
982 ;; NAME - Return a name for this buffer type.
983 ;;
984 ;; The remaining function(s) are optional:
985 ;;
986 ;; MODE - called in a new buffer with no arguments, should establish
987 ;; the proper mode for the buffer.
988 ;;
989
990 (defun gdb-set-buffer-rules (buffer-type &rest rules)
991 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
992 (if binding
993 (setcdr binding rules)
994 (push (cons buffer-type rules)
995 gdb-buffer-rules-assoc))))
996
997 ;; GUD buffers are an exception to the rules
998 (gdb-set-buffer-rules 'gdba 'error)
999
1000 ;; Partial-output buffer : This accumulates output from a command executed on
1001 ;; behalf of emacs (rather than the user).
1002 ;;
1003 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1004 'gdb-partial-output-name)
1005
1006 (defun gdb-partial-output-name ()
1007 (concat "*partial-output-"
1008 (gdb-get-target-string)
1009 "*"))
1010
1011 \f
1012 (gdb-set-buffer-rules 'gdb-inferior-io
1013 'gdb-inferior-io-name
1014 'gdb-inferior-io-mode)
1015
1016 (defun gdb-inferior-io-name ()
1017 (concat "*input/output of "
1018 (gdb-get-target-string)
1019 "*"))
1020
1021 (defun gdb-display-separate-io-buffer ()
1022 "Display IO of debugged program in a separate window."
1023 (interactive)
1024 (if gdb-use-separate-io-buffer
1025 (gdb-display-buffer
1026 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1027
1028 (defconst gdb-frame-parameters
1029 '((height . 14) (width . 80)
1030 (unsplittable . t)
1031 (tool-bar-lines . nil)
1032 (menu-bar-lines . nil)
1033 (minibuffer . nil)))
1034
1035 (defun gdb-frame-separate-io-buffer ()
1036 "Display IO of debugged program in a new frame."
1037 (interactive)
1038 (if gdb-use-separate-io-buffer
1039 (let ((special-display-regexps (append special-display-regexps '(".*")))
1040 (special-display-frame-alist gdb-frame-parameters))
1041 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1042
1043 (defvar gdb-inferior-io-mode-map
1044 (let ((map (make-sparse-keymap)))
1045 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1046 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1047 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1048 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1049 (define-key map "\C-d" 'gdb-separate-io-eof)
1050 map))
1051
1052 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1053 "Major mode for gdb inferior-io."
1054 :syntax-table nil :abbrev-table nil
1055 ;; We want to use comint because it has various nifty and familiar
1056 ;; features. We don't need a process, but comint wants one, so create
1057 ;; a dummy one.
1058 (make-comint-in-buffer
1059 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1060 (current-buffer) "hexl")
1061 (setq comint-input-sender 'gdb-inferior-io-sender))
1062
1063 (defun gdb-inferior-io-sender (proc string)
1064 ;; PROC is the pseudo-process created to satisfy comint.
1065 (with-current-buffer (process-buffer proc)
1066 (setq proc (get-buffer-process gud-comint-buffer))
1067 (process-send-string proc string)
1068 (process-send-string proc "\n")))
1069
1070 (defun gdb-separate-io-interrupt ()
1071 "Interrupt the program being debugged."
1072 (interactive)
1073 (interrupt-process
1074 (get-buffer-process gud-comint-buffer) comint-ptyp))
1075
1076 (defun gdb-separate-io-quit ()
1077 "Send quit signal to the program being debugged."
1078 (interactive)
1079 (quit-process
1080 (get-buffer-process gud-comint-buffer) comint-ptyp))
1081
1082 (defun gdb-separate-io-stop ()
1083 "Stop the program being debugged."
1084 (interactive)
1085 (stop-process
1086 (get-buffer-process gud-comint-buffer) comint-ptyp))
1087
1088 (defun gdb-separate-io-eof ()
1089 "Send end-of-file to the program being debugged."
1090 (interactive)
1091 (process-send-eof
1092 (get-buffer-process gud-comint-buffer)))
1093 \f
1094
1095 ;; gdb communications
1096 ;;
1097
1098 ;; INPUT: things sent to gdb
1099 ;;
1100 ;; The queues are lists. Each element is either a string (indicating user or
1101 ;; user-like input) or a list of the form:
1102 ;;
1103 ;; (INPUT-STRING HANDLER-FN)
1104 ;;
1105 ;; The handler function will be called from the partial-output buffer when the
1106 ;; command completes. This is the way to write commands which invoke gdb
1107 ;; commands autonomously.
1108 ;;
1109 ;; These lists are consumed tail first.
1110 ;;
1111
1112 (defun gdb-send (proc string)
1113 "A comint send filter for gdb.
1114 This filter may simply queue input for a later time."
1115 (with-current-buffer gud-comint-buffer
1116 (let ((inhibit-read-only t))
1117 (remove-text-properties (point-min) (point-max) '(face))))
1118 (if gud-running
1119 (progn
1120 (let ((item (concat string "\n")))
1121 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1122 (process-send-string proc item)))
1123 (if (and (string-match "\\\\$" string)
1124 (not comint-input-sender-no-newline)) ;;Try to catch C-d.
1125 (setq gdb-continuation (concat gdb-continuation string "\n"))
1126 (let ((item (concat gdb-continuation string "\n")))
1127 (gdb-enqueue-input item)
1128 (setq gdb-continuation nil)))))
1129
1130 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1131 ;; is a query, or other non-top-level prompt.
1132
1133 (defun gdb-enqueue-input (item)
1134 (if (not gud-running)
1135 (if gdb-prompting
1136 (progn
1137 (gdb-send-item item)
1138 (setq gdb-prompting nil))
1139 (push item gdb-input-queue))))
1140
1141 (defun gdb-dequeue-input ()
1142 (let ((queue gdb-input-queue))
1143 (and queue
1144 (let ((last (car (last queue))))
1145 (unless (nbutlast queue) (setq gdb-input-queue '()))
1146 last))))
1147
1148 (defun gdb-send-item (item)
1149 (setq gdb-flush-pending-output nil)
1150 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1151 (setq gdb-current-item item)
1152 (let ((process (get-buffer-process gud-comint-buffer)))
1153 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1154 (if (stringp item)
1155 (progn
1156 (setq gdb-output-sink 'user)
1157 (process-send-string process item))
1158 (progn
1159 (gdb-clear-partial-output)
1160 (setq gdb-output-sink 'pre-emacs)
1161 (process-send-string process
1162 (car item))))
1163 ;; case: eq gud-minor-mode 'gdbmi
1164 (gdb-clear-partial-output)
1165 (setq gdb-output-sink 'emacs)
1166 (process-send-string process (car item)))))
1167 \f
1168 ;;
1169 ;; output -- things gdb prints to emacs
1170 ;;
1171 ;; GDB output is a stream interrupted by annotations.
1172 ;; Annotations can be recognized by their beginning
1173 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1174 ;;
1175 ;; The tag is a string obeying symbol syntax.
1176 ;;
1177 ;; The optional part `<opt>' can be either the empty string
1178 ;; or a space followed by more data relating to the annotation.
1179 ;; For example, the SOURCE annotation is followed by a filename,
1180 ;; line number and various useless goo. This data must not include
1181 ;; any newlines.
1182 ;;
1183
1184 (defcustom gud-gdba-command-name "gdb -annotate=3"
1185 "Default command to execute an executable under the GDB-UI debugger."
1186 :type 'string
1187 :group 'gud
1188 :version "22.1")
1189
1190 (defvar gdb-annotation-rules
1191 '(("pre-prompt" gdb-pre-prompt)
1192 ("prompt" gdb-prompt)
1193 ("commands" gdb-subprompt)
1194 ("overload-choice" gdb-subprompt)
1195 ("query" gdb-subprompt)
1196 ;; Need this prompt for GDB 6.1
1197 ("nquery" gdb-subprompt)
1198 ("prompt-for-continue" gdb-subprompt)
1199 ("post-prompt" gdb-post-prompt)
1200 ("source" gdb-source)
1201 ("starting" gdb-starting)
1202 ("exited" gdb-exited)
1203 ("signalled" gdb-signalled)
1204 ("signal" gdb-signal)
1205 ("breakpoint" gdb-stopping)
1206 ("watchpoint" gdb-stopping)
1207 ("frame-begin" gdb-frame-begin)
1208 ("stopped" gdb-stopped)
1209 ("error-begin" gdb-error)
1210 ("error" gdb-error)
1211 ) "An assoc mapping annotation tags to functions which process them.")
1212
1213 (defun gdb-resync()
1214 (setq gdb-flush-pending-output t)
1215 (setq gud-running nil)
1216 (gdb-force-mode-line-update
1217 (propertize "stopped"'face font-lock-warning-face))
1218 (setq gdb-output-sink 'user)
1219 (setq gdb-input-queue nil)
1220 (setq gdb-pending-triggers nil)
1221 (setq gdb-prompting t))
1222
1223 (defconst gdb-source-spec-regexp
1224 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1225
1226 ;; Do not use this except as an annotation handler.
1227 (defun gdb-source (args)
1228 (string-match gdb-source-spec-regexp args)
1229 ;; Extract the frame position from the marker.
1230 (setq gud-last-frame
1231 (cons
1232 (match-string 1 args)
1233 (string-to-number (match-string 2 args))))
1234 (setq gdb-pc-address (match-string 3 args))
1235 ;; cover for auto-display output which comes *before*
1236 ;; stopped annotation
1237 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1238
1239 (defun gdb-pre-prompt (ignored)
1240 "An annotation handler for `pre-prompt'.
1241 This terminates the collection of output from a previous command if that
1242 happens to be in effect."
1243 (setq gdb-error nil)
1244 (let ((sink gdb-output-sink))
1245 (cond
1246 ((eq sink 'user) t)
1247 ((eq sink 'emacs)
1248 (setq gdb-output-sink 'post-emacs))
1249 (t
1250 (gdb-resync)
1251 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1252
1253 (defun gdb-prompt (ignored)
1254 "An annotation handler for `prompt'.
1255 This sends the next command (if any) to gdb."
1256 (when gdb-first-prompt
1257 (gdb-force-mode-line-update
1258 (propertize "initializing..." 'face font-lock-variable-name-face))
1259 (gdb-init-1)
1260 (setq gdb-first-prompt nil))
1261 (let ((sink gdb-output-sink))
1262 (cond
1263 ((eq sink 'user) t)
1264 ((eq sink 'post-emacs)
1265 (setq gdb-output-sink 'user)
1266 (let ((handler
1267 (car (cdr gdb-current-item))))
1268 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1269 (funcall handler))))
1270 (t
1271 (gdb-resync)
1272 (error "Phase error in gdb-prompt (got %s)" sink))))
1273 (let ((input (gdb-dequeue-input)))
1274 (if input
1275 (gdb-send-item input)
1276 (progn
1277 (setq gdb-prompting t)
1278 (gud-display-frame)))))
1279
1280 (defun gdb-subprompt (ignored)
1281 "An annotation handler for non-top-level prompts."
1282 (setq gdb-prompting t))
1283
1284 (defun gdb-starting (ignored)
1285 "An annotation handler for `starting'.
1286 This says that I/O for the subprocess is now the program being debugged,
1287 not GDB."
1288 (setq gdb-active-process t)
1289 (setq gdb-printing t)
1290 (let ((sink gdb-output-sink))
1291 (cond
1292 ((eq sink 'user)
1293 (progn
1294 (setq gud-running t)
1295 (setq gdb-inferior-status "running")
1296 (setq gdb-signalled nil)
1297 (gdb-force-mode-line-update
1298 (propertize gdb-inferior-status 'face font-lock-type-face))
1299 (gdb-remove-text-properties)
1300 (setq gud-old-arrow gud-overlay-arrow-position)
1301 (setq gud-overlay-arrow-position nil)
1302 (setq gdb-overlay-arrow-position nil)
1303 (setq gdb-stack-position nil)
1304 (if gdb-use-separate-io-buffer
1305 (setq gdb-output-sink 'inferior))))
1306 (t
1307 (gdb-resync)
1308 (error "Unexpected `starting' annotation")))))
1309
1310 (defun gdb-signal (ignored)
1311 (setq gdb-inferior-status "signal")
1312 (gdb-force-mode-line-update
1313 (propertize gdb-inferior-status 'face font-lock-warning-face))
1314 (gdb-stopping ignored))
1315
1316 (defun gdb-stopping (ignored)
1317 "An annotation handler for `breakpoint' and other annotations.
1318 They say that I/O for the subprocess is now GDB, not the program
1319 being debugged."
1320 (if gdb-use-separate-io-buffer
1321 (let ((sink gdb-output-sink))
1322 (cond
1323 ((eq sink 'inferior)
1324 (setq gdb-output-sink 'user))
1325 (t
1326 (gdb-resync)
1327 (error "Unexpected stopping annotation"))))))
1328
1329 (defun gdb-exited (ignored)
1330 "An annotation handler for `exited' and `signalled'.
1331 They say that I/O for the subprocess is now GDB, not the program
1332 being debugged and that the program is no longer running. This
1333 function is used to change the focus of GUD tooltips to #define
1334 directives."
1335 (setq gdb-active-process nil)
1336 (setq gud-overlay-arrow-position nil)
1337 (setq gdb-overlay-arrow-position nil)
1338 (setq gdb-stack-position nil)
1339 (setq gud-old-arrow nil)
1340 (setq gdb-inferior-status "exited")
1341 (gdb-force-mode-line-update
1342 (propertize gdb-inferior-status 'face font-lock-warning-face))
1343 (gdb-stopping ignored))
1344
1345 (defun gdb-signalled (ignored)
1346 (setq gdb-signalled t))
1347
1348 (defun gdb-frame-begin (ignored)
1349 (setq gdb-frame-begin t)
1350 (setq gdb-printing nil)
1351 (let ((sink gdb-output-sink))
1352 (cond
1353 ((eq sink 'inferior)
1354 (setq gdb-output-sink 'user))
1355 ((eq sink 'user) t)
1356 ((eq sink 'emacs) t)
1357 (t
1358 (gdb-resync)
1359 (error "Unexpected frame-begin annotation (%S)" sink)))))
1360
1361 (defcustom gdb-same-frame focus-follows-mouse
1362 "Non-nil means pop up GUD buffer in same frame."
1363 :group 'gud
1364 :type 'boolean
1365 :version "22.1")
1366
1367 (defcustom gdb-find-source-frame nil
1368 "Non-nil means try to find a source frame further up stack e.g after signal."
1369 :group 'gud
1370 :type 'boolean
1371 :version "22.1")
1372
1373 (defun gdb-find-source-frame (arg)
1374 "Toggle trying to find a source frame further up stack.
1375 With arg, look for a source frame further up stack iff arg is positive."
1376 (interactive "P")
1377 (setq gdb-find-source-frame
1378 (if (null arg)
1379 (not gdb-find-source-frame)
1380 (> (prefix-numeric-value arg) 0)))
1381 (message (format "Looking for source frame %sabled"
1382 (if gdb-find-source-frame "en" "dis"))))
1383
1384 (defun gdb-stopped (ignored)
1385 "An annotation handler for `stopped'.
1386 It is just like `gdb-stopping', except that if we already set the output
1387 sink to `user' in `gdb-stopping', that is fine."
1388 (setq gud-running nil)
1389 (unless (or gud-overlay-arrow-position gud-last-frame)
1390 (if (and gdb-frame-begin gdb-printing)
1391 (setq gud-overlay-arrow-position gud-old-arrow)
1392 ;;Pop up GUD buffer to display current frame when it doesn't have source
1393 ;;information i.e id not compiled with -g as with libc routines generally.
1394 (if gdb-same-frame
1395 (gdb-display-gdb-buffer)
1396 (gdb-frame-gdb-buffer))
1397 (if gdb-find-source-frame
1398 ;;Try to find source further up stack e.g after signal.
1399 (setq gdb-look-up-stack
1400 (if (gdb-get-buffer 'gdb-stack-buffer)
1401 'keep
1402 (progn
1403 (gdb-get-buffer-create 'gdb-stack-buffer)
1404 (gdb-invalidate-frames)
1405 'delete))))))
1406 (unless (member gdb-inferior-status '("exited" "signal"))
1407 (setq gdb-inferior-status "stopped")
1408 (gdb-force-mode-line-update
1409 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1410 (let ((sink gdb-output-sink))
1411 (cond
1412 ((eq sink 'inferior)
1413 (setq gdb-output-sink 'user))
1414 ((eq sink 'user) t)
1415 (t
1416 (gdb-resync)
1417 (error "Unexpected stopped annotation"))))
1418 (if gdb-signalled (gdb-exited ignored)))
1419
1420 (defun gdb-error (ignored)
1421 (setq gdb-error (not gdb-error)))
1422
1423 (defun gdb-post-prompt (ignored)
1424 "An annotation handler for `post-prompt'.
1425 This begins the collection of output from the current command if that
1426 happens to be appropriate."
1427 ;; Don't add to queue if there outstanding items or gdb-version is not known
1428 ;; yet.
1429 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1430 (gdb-get-selected-frame)
1431 (gdb-invalidate-frames)
1432 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1433 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1434 (gdb-invalidate-breakpoints)
1435 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1436 ;; so gdb-pc-address is updated.
1437 ;; (gdb-invalidate-assembler)
1438
1439 (if (string-equal gdb-version "pre-6.4")
1440 (gdb-invalidate-registers)
1441 (gdb-get-changed-registers)
1442 (gdb-invalidate-registers-1))
1443
1444 (gdb-invalidate-memory)
1445 (if (string-equal gdb-version "pre-6.4")
1446 (gdb-invalidate-locals)
1447 (gdb-invalidate-locals-1))
1448
1449 (gdb-invalidate-threads)
1450 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1451 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1452 ;; Only needed/used with speedbar/watch expressions.
1453 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1454 (if (string-equal gdb-version "pre-6.4")
1455 (gdb-var-update)
1456 (gdb-var-update-1)))))
1457 (setq gdb-first-post-prompt nil)
1458 (let ((sink gdb-output-sink))
1459 (cond
1460 ((eq sink 'user) t)
1461 ((eq sink 'pre-emacs)
1462 (setq gdb-output-sink 'emacs))
1463 (t
1464 (gdb-resync)
1465 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1466
1467 (defconst gdb-buffer-list
1468 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1469
1470 (defun gdb-remove-text-properties ()
1471 (dolist (buffertype gdb-buffer-list)
1472 (let ((buffer (gdb-get-buffer buffertype)))
1473 (if buffer
1474 (with-current-buffer buffer
1475 (let ((inhibit-read-only t))
1476 (remove-text-properties
1477 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1478
1479 ;; GUD displays the selected GDB frame. This might might not be the current
1480 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1481 ;; visited breakpoint is, use that window.
1482 (defun gdb-display-source-buffer (buffer)
1483 (let* ((last-window (if gud-last-last-frame
1484 (get-buffer-window
1485 (gud-find-file (car gud-last-last-frame)))))
1486 (source-window (or last-window
1487 (if (and gdb-source-window
1488 (window-live-p gdb-source-window))
1489 gdb-source-window))))
1490 (when source-window
1491 (setq gdb-source-window source-window)
1492 (set-window-buffer source-window buffer))
1493 source-window))
1494
1495 (defun gud-gdba-marker-filter (string)
1496 "A gud marker filter for gdb. Handle a burst of output from GDB."
1497 (if gdb-flush-pending-output
1498 nil
1499 (when gdb-enable-debug
1500 (push (cons 'recv string) gdb-debug-ring)
1501 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1502 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1503 ;; Recall the left over gud-marker-acc from last time.
1504 (setq gud-marker-acc (concat gud-marker-acc string))
1505 ;; Start accumulating output for the GUD buffer.
1506 (let ((output ""))
1507 ;;
1508 ;; Process all the complete markers in this chunk.
1509 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1510 (let ((annotation (match-string 1 gud-marker-acc)))
1511 ;;
1512 ;; Stuff prior to the match is just ordinary output.
1513 ;; It is either concatenated to OUTPUT or directed
1514 ;; elsewhere.
1515 (setq output
1516 (gdb-concat-output
1517 output
1518 (substring gud-marker-acc 0 (match-beginning 0))))
1519 ;;
1520 ;; Take that stuff off the gud-marker-acc.
1521 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1522 ;;
1523 ;; Parse the tag from the annotation, and maybe its arguments.
1524 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1525 (let* ((annotation-type (match-string 1 annotation))
1526 (annotation-arguments (match-string 2 annotation))
1527 (annotation-rule (assoc annotation-type
1528 gdb-annotation-rules)))
1529 ;; Call the handler for this annotation.
1530 (if annotation-rule
1531 (funcall (car (cdr annotation-rule))
1532 annotation-arguments)
1533 ;; Else the annotation is not recognized. Ignore it silently,
1534 ;; so that GDB can add new annotations without causing
1535 ;; us to blow up.
1536 ))))
1537 ;;
1538 ;; Does the remaining text end in a partial line?
1539 ;; If it does, then keep part of the gud-marker-acc until we get more.
1540 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1541 gud-marker-acc)
1542 (progn
1543 ;; Everything before the potential marker start can be output.
1544 (setq output
1545 (gdb-concat-output output
1546 (substring gud-marker-acc 0
1547 (match-beginning 0))))
1548 ;;
1549 ;; Everything after, we save, to combine with later input.
1550 (setq gud-marker-acc (substring gud-marker-acc
1551 (match-beginning 0))))
1552 ;;
1553 ;; In case we know the gud-marker-acc contains no partial annotations:
1554 (progn
1555 (setq output (gdb-concat-output output gud-marker-acc))
1556 (setq gud-marker-acc "")))
1557 output)))
1558
1559 (defun gdb-concat-output (so-far new)
1560 (if gdb-error
1561 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1562 (let ((sink gdb-output-sink))
1563 (cond
1564 ((eq sink 'user) (concat so-far new))
1565 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1566 ((eq sink 'emacs)
1567 (gdb-append-to-partial-output new)
1568 so-far)
1569 ((eq sink 'inferior)
1570 (gdb-append-to-inferior-io new)
1571 so-far)
1572 (t
1573 (gdb-resync)
1574 (error "Bogon output sink %S" sink)))))
1575
1576 (defun gdb-append-to-partial-output (string)
1577 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1578 (goto-char (point-max))
1579 (insert string)))
1580
1581 (defun gdb-clear-partial-output ()
1582 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1583 (erase-buffer)))
1584
1585 (defun gdb-append-to-inferior-io (string)
1586 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1587 (goto-char (point-max))
1588 (insert-before-markers string))
1589 (if (not (string-equal string ""))
1590 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1591
1592 (defun gdb-clear-inferior-io ()
1593 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1594 (erase-buffer)))
1595 \f
1596
1597 ;; One trick is to have a command who's output is always available in a buffer
1598 ;; of it's own, and is always up to date. We build several buffers of this
1599 ;; type.
1600 ;;
1601 ;; There are two aspects to this: gdb has to tell us when the output for that
1602 ;; command might have changed, and we have to be able to run the command
1603 ;; behind the user's back.
1604 ;;
1605 ;; The output phasing associated with the variable gdb-output-sink
1606 ;; help us to run commands behind the user's back.
1607 ;;
1608 ;; Below is the code for specificly managing buffers of output from one
1609 ;; command.
1610 ;;
1611
1612 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1613 ;; It adds an input for the command we are tracking. It should be the
1614 ;; annotation rule binding of whatever gdb sends to tell us this command
1615 ;; might have changed it's output.
1616 ;;
1617 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1618 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1619 ;; input in the input queue (see comment about ``gdb communications'' above).
1620
1621 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1622 output-handler)
1623 `(defun ,name (&optional ignored)
1624 (if (and ,demand-predicate
1625 (not (member ',name
1626 gdb-pending-triggers)))
1627 (progn
1628 (gdb-enqueue-input
1629 (list ,gdb-command ',output-handler))
1630 (push ',name gdb-pending-triggers)))))
1631
1632 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1633 `(defun ,name ()
1634 (setq gdb-pending-triggers
1635 (delq ',trigger
1636 gdb-pending-triggers))
1637 (let ((buf (gdb-get-buffer ',buf-key)))
1638 (and buf
1639 (with-current-buffer buf
1640 (let* ((window (get-buffer-window buf 0))
1641 (start (window-start window))
1642 (p (window-point window))
1643 (buffer-read-only nil))
1644 (erase-buffer)
1645 (insert-buffer-substring (gdb-get-buffer-create
1646 'gdb-partial-output-buffer))
1647 (set-window-start window start)
1648 (set-window-point window p)))))
1649 ;; put customisation here
1650 (,custom-defun)))
1651
1652 (defmacro def-gdb-auto-updated-buffer (buffer-key
1653 trigger-name gdb-command
1654 output-handler-name custom-defun)
1655 `(progn
1656 (def-gdb-auto-update-trigger ,trigger-name
1657 ;; The demand predicate:
1658 (gdb-get-buffer ',buffer-key)
1659 ,gdb-command
1660 ,output-handler-name)
1661 (def-gdb-auto-update-handler ,output-handler-name
1662 ,trigger-name ,buffer-key ,custom-defun)))
1663
1664 \f
1665 ;;
1666 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1667 ;;
1668 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1669 'gdb-breakpoints-buffer-name
1670 'gdb-breakpoints-mode)
1671
1672 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1673 ;; This defines the auto update rule for buffers of type
1674 ;; `gdb-breakpoints-buffer'.
1675 ;;
1676 ;; It defines a function to serve as the annotation handler that
1677 ;; handles the `foo-invalidated' message. That function is called:
1678 gdb-invalidate-breakpoints
1679 ;;
1680 ;; To update the buffer, this command is sent to gdb.
1681 "server info breakpoints\n"
1682 ;;
1683 ;; This also defines a function to be the handler for the output
1684 ;; from the command above. That function will copy the output into
1685 ;; the appropriately typed buffer. That function will be called:
1686 gdb-info-breakpoints-handler
1687 ;; buffer specific functions
1688 gdb-info-breakpoints-custom)
1689
1690 (defconst breakpoint-xpm-data
1691 "/* XPM */
1692 static char *magick[] = {
1693 /* columns rows colors chars-per-pixel */
1694 \"10 10 2 1\",
1695 \" c red\",
1696 \"+ c None\",
1697 /* pixels */
1698 \"+++ +++\",
1699 \"++ ++\",
1700 \"+ +\",
1701 \" \",
1702 \" \",
1703 \" \",
1704 \" \",
1705 \"+ +\",
1706 \"++ ++\",
1707 \"+++ +++\",
1708 };"
1709 "XPM data used for breakpoint icon.")
1710
1711 (defconst breakpoint-enabled-pbm-data
1712 "P1
1713 10 10\",
1714 0 0 0 0 1 1 1 1 0 0 0 0
1715 0 0 0 1 1 1 1 1 1 0 0 0
1716 0 0 1 1 1 1 1 1 1 1 0 0
1717 0 1 1 1 1 1 1 1 1 1 1 0
1718 0 1 1 1 1 1 1 1 1 1 1 0
1719 0 1 1 1 1 1 1 1 1 1 1 0
1720 0 1 1 1 1 1 1 1 1 1 1 0
1721 0 0 1 1 1 1 1 1 1 1 0 0
1722 0 0 0 1 1 1 1 1 1 0 0 0
1723 0 0 0 0 1 1 1 1 0 0 0 0"
1724 "PBM data used for enabled breakpoint icon.")
1725
1726 (defconst breakpoint-disabled-pbm-data
1727 "P1
1728 10 10\",
1729 0 0 1 0 1 0 1 0 0 0
1730 0 1 0 1 0 1 0 1 0 0
1731 1 0 1 0 1 0 1 0 1 0
1732 0 1 0 1 0 1 0 1 0 1
1733 1 0 1 0 1 0 1 0 1 0
1734 0 1 0 1 0 1 0 1 0 1
1735 1 0 1 0 1 0 1 0 1 0
1736 0 1 0 1 0 1 0 1 0 1
1737 0 0 1 0 1 0 1 0 1 0
1738 0 0 0 1 0 1 0 1 0 0"
1739 "PBM data used for disabled breakpoint icon.")
1740
1741 (defvar breakpoint-enabled-icon nil
1742 "Icon for enabled breakpoint in display margin.")
1743
1744 (defvar breakpoint-disabled-icon nil
1745 "Icon for disabled breakpoint in display margin.")
1746
1747 (and (display-images-p)
1748 ;; Bitmap for breakpoint in fringe
1749 (define-fringe-bitmap 'breakpoint
1750 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1751 ;; Bitmap for gud-overlay-arrow in fringe
1752 (define-fringe-bitmap 'hollow-right-triangle
1753 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1754
1755 (defface breakpoint-enabled
1756 '((t
1757 :foreground "red"
1758 :weight bold))
1759 "Face for enabled breakpoint icon in fringe."
1760 :group 'gud)
1761
1762 (defface breakpoint-disabled
1763 ;; We use different values of grey for different background types,
1764 ;; so that on low-color displays it will end up as something visible
1765 ;; if it has to be approximated.
1766 '((t :foreground "grey70"))
1767 "Face for disabled breakpoint icon in fringe."
1768 :group 'gud)
1769
1770 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1771 (defun gdb-info-breakpoints-custom ()
1772 (let ((flag) (bptno))
1773 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1774 (dolist (buffer (buffer-list))
1775 (with-current-buffer buffer
1776 (if (and (memq gud-minor-mode '(gdba gdbmi))
1777 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1778 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1779 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1780 (save-excursion
1781 (let ((buffer-read-only nil))
1782 (goto-char (point-min))
1783 (while (< (point) (- (point-max) 1))
1784 (forward-line 1)
1785 (if (looking-at gdb-breakpoint-regexp)
1786 (progn
1787 (setq bptno (match-string 1))
1788 (setq flag (char-after (match-beginning 2)))
1789 (add-text-properties
1790 (match-beginning 2) (match-end 2)
1791 (if (eq flag ?y)
1792 '(face font-lock-warning-face)
1793 '(face font-lock-type-face)))
1794 (let ((bl (point))
1795 (el (line-end-position)))
1796 (if (re-search-forward " in \\(.*\\) at\\s-+" el t)
1797 (progn
1798 (add-text-properties
1799 (match-beginning 1) (match-end 1)
1800 '(face font-lock-function-name-face))
1801 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1802 (let ((line (match-string 2))
1803 (file (match-string 1)))
1804 (add-text-properties bl el
1805 '(mouse-face highlight
1806 help-echo "mouse-2, RET: visit breakpoint"))
1807 (unless (file-exists-p file)
1808 (setq file (cdr (assoc bptno gdb-location-alist))))
1809 (if (and file
1810 (not (string-equal file "File not found")))
1811 (with-current-buffer
1812 (find-file-noselect file 'nowarn)
1813 (set (make-local-variable 'gud-minor-mode)
1814 'gdba)
1815 (set (make-local-variable 'tool-bar-map)
1816 gud-tool-bar-map)
1817 ;; Only want one breakpoint icon at each
1818 ;; location.
1819 (save-excursion
1820 (goto-line (string-to-number line))
1821 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1822 (gdb-enqueue-input
1823 (list
1824 (concat gdb-server-prefix "list "
1825 (match-string-no-properties 1) ":1\n")
1826 'ignore))
1827 (gdb-enqueue-input
1828 (list (concat gdb-server-prefix "info source\n")
1829 `(lambda () (gdb-get-location
1830 ,bptno ,line ,flag)))))))
1831 (if (re-search-forward
1832 "<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1833 el t)
1834 (add-text-properties
1835 (match-beginning 1) (match-end 1)
1836 '(face font-lock-function-name-face))
1837 (end-of-line)
1838 (re-search-backward "\\s-\\(\\S-*\\)"
1839 bl t)
1840 (add-text-properties
1841 (match-beginning 1) (match-end 1)
1842 '(face font-lock-variable-name-face)))))))
1843 (end-of-line))))))
1844 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1845
1846 (defun gdb-mouse-set-clear-breakpoint (event)
1847 "Set/clear breakpoint in left fringe/margin with mouse click."
1848 (interactive "e")
1849 (mouse-minibuffer-check event)
1850 (let ((posn (event-end event)))
1851 (if (numberp (posn-point posn))
1852 (with-selected-window (posn-window posn)
1853 (save-excursion
1854 (goto-char (posn-point posn))
1855 (if (or (posn-object posn)
1856 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1857 'breakpoint))
1858 (gud-remove nil)
1859 (gud-break nil)))))))
1860
1861 (defun gdb-mouse-toggle-breakpoint-margin (event)
1862 "Enable/disable breakpoint in left margin with mouse click."
1863 (interactive "e")
1864 (mouse-minibuffer-check event)
1865 (let ((posn (event-end event)))
1866 (if (numberp (posn-point posn))
1867 (with-selected-window (posn-window posn)
1868 (save-excursion
1869 (goto-char (posn-point posn))
1870 (if (posn-object posn)
1871 (gdb-enqueue-input
1872 (list
1873 (let ((bptno (get-text-property
1874 0 'gdb-bptno (car (posn-string posn)))))
1875 (concat gdb-server-prefix
1876 (if (get-text-property
1877 0 'gdb-enabled (car (posn-string posn)))
1878 "disable "
1879 "enable ")
1880 bptno "\n"))
1881 'ignore))))))))
1882
1883 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1884 "Enable/disable breakpoint in left fringe with mouse click."
1885 (interactive "e")
1886 (mouse-minibuffer-check event)
1887 (let* ((posn (event-end event))
1888 (pos (posn-point posn))
1889 obj)
1890 (when (numberp pos)
1891 (with-selected-window (posn-window posn)
1892 (save-excursion
1893 (set-buffer (window-buffer (selected-window)))
1894 (goto-char pos)
1895 (dolist (overlay (overlays-in pos pos))
1896 (when (overlay-get overlay 'put-break)
1897 (setq obj (overlay-get overlay 'before-string))))
1898 (when (stringp obj)
1899 (gdb-enqueue-input
1900 (list
1901 (concat gdb-server-prefix
1902 (if (get-text-property 0 'gdb-enabled obj)
1903 "disable "
1904 "enable ")
1905 (get-text-property 0 'gdb-bptno obj) "\n")
1906 'ignore))))))))
1907
1908 (defun gdb-breakpoints-buffer-name ()
1909 (with-current-buffer gud-comint-buffer
1910 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1911
1912 (defun gdb-display-breakpoints-buffer ()
1913 "Display status of user-settable breakpoints."
1914 (interactive)
1915 (gdb-display-buffer
1916 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
1917
1918 (defun gdb-frame-breakpoints-buffer ()
1919 "Display status of user-settable breakpoints in a new frame."
1920 (interactive)
1921 (let ((special-display-regexps (append special-display-regexps '(".*")))
1922 (special-display-frame-alist gdb-frame-parameters))
1923 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1924
1925 (defvar gdb-breakpoints-mode-map
1926 (let ((map (make-sparse-keymap))
1927 (menu (make-sparse-keymap "Breakpoints")))
1928 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1929 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1930 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1931 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1932 (suppress-keymap map)
1933 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1934 (define-key map " " 'gdb-toggle-breakpoint)
1935 (define-key map "D" 'gdb-delete-breakpoint)
1936 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1937 (define-key map "q" 'gdb-delete-frame-or-window)
1938 (define-key map "\r" 'gdb-goto-breakpoint)
1939 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1940 (define-key map [follow-link] 'mouse-face)
1941 map))
1942
1943 (defun gdb-delete-frame-or-window ()
1944 "Delete frame if there is only one window. Otherwise delete the window."
1945 (interactive)
1946 (if (one-window-p) (delete-frame)
1947 (delete-window)))
1948
1949 (defun gdb-breakpoints-mode ()
1950 "Major mode for gdb breakpoints.
1951
1952 \\{gdb-breakpoints-mode-map}"
1953 (kill-all-local-variables)
1954 (setq major-mode 'gdb-breakpoints-mode)
1955 (setq mode-name "Breakpoints")
1956 (use-local-map gdb-breakpoints-mode-map)
1957 (setq buffer-read-only t)
1958 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1959 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1960 'gdb-invalidate-breakpoints
1961 'gdbmi-invalidate-breakpoints))
1962
1963 (defconst gdb-breakpoint-regexp
1964 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1965
1966 (defun gdb-toggle-breakpoint ()
1967 "Enable/disable breakpoint at current line."
1968 (interactive)
1969 (save-excursion
1970 (beginning-of-line 1)
1971 (if (looking-at gdb-breakpoint-regexp)
1972 (gdb-enqueue-input
1973 (list
1974 (concat gdb-server-prefix
1975 (if (eq ?y (char-after (match-beginning 2)))
1976 "disable "
1977 "enable ")
1978 (match-string 1) "\n") 'ignore))
1979 (error "Not recognized as break/watchpoint line"))))
1980
1981 (defun gdb-delete-breakpoint ()
1982 "Delete the breakpoint at current line."
1983 (interactive)
1984 (beginning-of-line 1)
1985 (if (looking-at gdb-breakpoint-regexp)
1986 (gdb-enqueue-input
1987 (list
1988 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1989 (error "Not recognized as break/watchpoint line")))
1990
1991 (defun gdb-goto-breakpoint (&optional event)
1992 "Display the breakpoint location specified at current line."
1993 (interactive (list last-input-event))
1994 (if event (posn-set-point (event-end event)))
1995 (save-excursion
1996 (beginning-of-line 1)
1997 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1998 (let ((bptno (match-string 1))
1999 (file (match-string 2))
2000 (line (match-string 3)))
2001 (save-selected-window
2002 (let* ((buffer (find-file-noselect
2003 (if (file-exists-p file) file
2004 (cdr (assoc bptno gdb-location-alist)))))
2005 (window (or (gdb-display-source-buffer buffer)
2006 (display-buffer buffer))))
2007 (setq gdb-source-window window)
2008 (with-current-buffer buffer
2009 (goto-line (string-to-number line))
2010 (set-window-point window (point))))))
2011 (error "No location specified."))))
2012 \f
2013
2014 ;; Frames buffer. This displays a perpetually correct bactracktrace
2015 ;; (from the command `where').
2016 ;;
2017 ;; Alas, if your stack is deep, it is costly.
2018 ;;
2019 (gdb-set-buffer-rules 'gdb-stack-buffer
2020 'gdb-stack-buffer-name
2021 'gdb-frames-mode)
2022
2023 (def-gdb-auto-updated-buffer gdb-stack-buffer
2024 gdb-invalidate-frames
2025 "server info stack\n"
2026 gdb-info-stack-handler
2027 gdb-info-stack-custom)
2028
2029 (defun gdb-info-stack-custom ()
2030 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2031 (save-excursion
2032 (unless (eq gdb-look-up-stack 'delete)
2033 (let ((buffer-read-only nil)
2034 bl el)
2035 (goto-char (point-min))
2036 (while (< (point) (point-max))
2037 (setq bl (line-beginning-position)
2038 el (line-end-position))
2039 (when (looking-at "#")
2040 (add-text-properties bl el
2041 '(mouse-face highlight
2042 help-echo "mouse-2, RET: Select frame")))
2043 (goto-char bl)
2044 (when (looking-at "^#\\([0-9]+\\)")
2045 (when (string-equal (match-string 1) gdb-frame-number)
2046 (if (> (car (window-fringes)) 0)
2047 (progn
2048 (or gdb-stack-position
2049 (setq gdb-stack-position (make-marker)))
2050 (set-marker gdb-stack-position (point)))
2051 (put-text-property bl (+ bl 4)
2052 'face '(:inverse-video t))))
2053 (when (re-search-forward
2054 (concat
2055 (if (string-equal (match-string 1) "0") "" " in ")
2056 "\\([^ ]+\\) (") el t)
2057 (put-text-property (match-beginning 1) (match-end 1)
2058 'face font-lock-function-name-face)
2059 (setq bl (match-end 0))
2060 (while (re-search-forward "<\\([^>]+\\)>" el t)
2061 (put-text-property (match-beginning 1) (match-end 1)
2062 'face font-lock-function-name-face))
2063 (goto-char bl)
2064 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2065 (put-text-property (match-beginning 1) (match-end 1)
2066 'face font-lock-variable-name-face))))
2067 (forward-line 1))))
2068 (when gdb-look-up-stack
2069 (goto-char (point-min))
2070 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2071 (let ((start (line-beginning-position))
2072 (file (match-string 1))
2073 (line (match-string 2)))
2074 (re-search-backward "^#*\\([0-9]+\\)" start t)
2075 (gdb-enqueue-input
2076 (list (concat gdb-server-prefix "frame "
2077 (match-string 1) "\n") 'gdb-set-hollow))
2078 (gdb-enqueue-input
2079 (list (concat gdb-server-prefix "frame 0\n") 'ignore)))))))
2080 (if (eq gdb-look-up-stack 'delete)
2081 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2082 (setq gdb-look-up-stack nil))
2083
2084 (defun gdb-set-hollow ()
2085 (if gud-last-last-frame
2086 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2087 (setq fringe-indicator-alist
2088 '((overlay-arrow . hollow-right-triangle))))))
2089
2090 (defun gdb-stack-buffer-name ()
2091 (with-current-buffer gud-comint-buffer
2092 (concat "*stack frames of " (gdb-get-target-string) "*")))
2093
2094 (defun gdb-display-stack-buffer ()
2095 "Display backtrace of current stack."
2096 (interactive)
2097 (gdb-display-buffer
2098 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2099
2100 (defun gdb-frame-stack-buffer ()
2101 "Display backtrace of current stack in a new frame."
2102 (interactive)
2103 (let ((special-display-regexps (append special-display-regexps '(".*")))
2104 (special-display-frame-alist gdb-frame-parameters))
2105 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2106
2107 (defvar gdb-frames-mode-map
2108 (let ((map (make-sparse-keymap)))
2109 (suppress-keymap map)
2110 (define-key map "q" 'kill-this-buffer)
2111 (define-key map "\r" 'gdb-frames-select)
2112 (define-key map [mouse-2] 'gdb-frames-select)
2113 (define-key map [follow-link] 'mouse-face)
2114 map))
2115
2116 (defun gdb-frames-mode ()
2117 "Major mode for gdb call stack.
2118
2119 \\{gdb-frames-mode-map}"
2120 (kill-all-local-variables)
2121 (setq major-mode 'gdb-frames-mode)
2122 (setq mode-name "Frames")
2123 (setq gdb-stack-position nil)
2124 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2125 (setq buffer-read-only t)
2126 (use-local-map gdb-frames-mode-map)
2127 (run-mode-hooks 'gdb-frames-mode-hook)
2128 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2129 'gdb-invalidate-frames
2130 'gdbmi-invalidate-frames))
2131
2132 (defun gdb-get-frame-number ()
2133 (save-excursion
2134 (end-of-line)
2135 (let* ((start (line-beginning-position))
2136 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2137 (n (or (and pos (match-string-no-properties 1)) "0")))
2138 n)))
2139
2140 (defun gdb-frames-select (&optional event)
2141 "Select the frame and display the relevant source."
2142 (interactive (list last-input-event))
2143 (if event (posn-set-point (event-end event)))
2144 (gdb-enqueue-input
2145 (list (concat gdb-server-prefix "frame "
2146 (gdb-get-frame-number) "\n") 'ignore)))
2147 \f
2148
2149 ;; Threads buffer. This displays a selectable thread list.
2150 ;;
2151 (gdb-set-buffer-rules 'gdb-threads-buffer
2152 'gdb-threads-buffer-name
2153 'gdb-threads-mode)
2154
2155 (def-gdb-auto-updated-buffer gdb-threads-buffer
2156 gdb-invalidate-threads
2157 (concat gdb-server-prefix "info threads\n")
2158 gdb-info-threads-handler
2159 gdb-info-threads-custom)
2160
2161 (defun gdb-info-threads-custom ()
2162 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2163 (let ((buffer-read-only nil))
2164 (save-excursion
2165 (goto-char (point-min))
2166 (while (< (point) (point-max))
2167 (unless (looking-at "No ")
2168 (add-text-properties (line-beginning-position) (line-end-position)
2169 '(mouse-face highlight
2170 help-echo "mouse-2, RET: select thread")))
2171 (forward-line 1))))))
2172
2173 (defun gdb-threads-buffer-name ()
2174 (with-current-buffer gud-comint-buffer
2175 (concat "*threads of " (gdb-get-target-string) "*")))
2176
2177 (defun gdb-display-threads-buffer ()
2178 "Display IDs of currently known threads."
2179 (interactive)
2180 (gdb-display-buffer
2181 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2182
2183 (defun gdb-frame-threads-buffer ()
2184 "Display IDs of currently known threads in a new frame."
2185 (interactive)
2186 (let ((special-display-regexps (append special-display-regexps '(".*")))
2187 (special-display-frame-alist gdb-frame-parameters))
2188 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2189
2190 (defvar gdb-threads-mode-map
2191 (let ((map (make-sparse-keymap)))
2192 (suppress-keymap map)
2193 (define-key map "q" 'kill-this-buffer)
2194 (define-key map "\r" 'gdb-threads-select)
2195 (define-key map [mouse-2] 'gdb-threads-select)
2196 (define-key map [follow-link] 'mouse-face)
2197 map))
2198
2199 (defvar gdb-threads-font-lock-keywords
2200 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2201 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2202 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2203 "Font lock keywords used in `gdb-threads-mode'.")
2204
2205 (defun gdb-threads-mode ()
2206 "Major mode for gdb threads.
2207
2208 \\{gdb-threads-mode-map}"
2209 (kill-all-local-variables)
2210 (setq major-mode 'gdb-threads-mode)
2211 (setq mode-name "Threads")
2212 (setq buffer-read-only t)
2213 (use-local-map gdb-threads-mode-map)
2214 (set (make-local-variable 'font-lock-defaults)
2215 '(gdb-threads-font-lock-keywords))
2216 (run-mode-hooks 'gdb-threads-mode-hook)
2217 'gdb-invalidate-threads)
2218
2219 (defun gdb-get-thread-number ()
2220 (save-excursion
2221 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2222 (match-string-no-properties 1)))
2223
2224 (defun gdb-threads-select (&optional event)
2225 "Select the thread and display the relevant source."
2226 (interactive (list last-input-event))
2227 (if event (posn-set-point (event-end event)))
2228 (gdb-enqueue-input
2229 (list (concat gdb-server-prefix "thread "
2230 (gdb-get-thread-number) "\n") 'ignore))
2231 (gud-display-frame))
2232 \f
2233
2234 ;; Registers buffer.
2235 ;;
2236 (defcustom gdb-all-registers nil
2237 "Non-nil means include floating-point registers."
2238 :type 'boolean
2239 :group 'gud
2240 :version "22.1")
2241
2242 (gdb-set-buffer-rules 'gdb-registers-buffer
2243 'gdb-registers-buffer-name
2244 'gdb-registers-mode)
2245
2246 (def-gdb-auto-updated-buffer gdb-registers-buffer
2247 gdb-invalidate-registers
2248 (concat
2249 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2250 gdb-info-registers-handler
2251 gdb-info-registers-custom)
2252
2253 (defun gdb-info-registers-custom ()
2254 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2255 (save-excursion
2256 (let ((buffer-read-only nil)
2257 start end)
2258 (goto-char (point-min))
2259 (while (< (point) (point-max))
2260 (setq start (line-beginning-position))
2261 (setq end (line-end-position))
2262 (when (looking-at "^[^ ]+")
2263 (unless (string-equal (match-string 0) "The")
2264 (put-text-property start (match-end 0)
2265 'face font-lock-variable-name-face)
2266 (add-text-properties start end
2267 '(help-echo "mouse-2: edit value"
2268 mouse-face highlight))))
2269 (forward-line 1))))))
2270
2271 (defun gdb-edit-register-value (&optional event)
2272 (interactive (list last-input-event))
2273 (save-excursion
2274 (if event (posn-set-point (event-end event)))
2275 (beginning-of-line)
2276 (let* ((register (current-word))
2277 (value (read-string (format "New value (%s): " register))))
2278 (gdb-enqueue-input
2279 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2280 'ignore)))))
2281
2282 (defvar gdb-registers-mode-map
2283 (let ((map (make-sparse-keymap)))
2284 (suppress-keymap map)
2285 (define-key map "\r" 'gdb-edit-register-value)
2286 (define-key map [mouse-2] 'gdb-edit-register-value)
2287 (define-key map " " 'gdb-all-registers)
2288 (define-key map "q" 'kill-this-buffer)
2289 map))
2290
2291 (defun gdb-registers-mode ()
2292 "Major mode for gdb registers.
2293
2294 \\{gdb-registers-mode-map}"
2295 (kill-all-local-variables)
2296 (setq major-mode 'gdb-registers-mode)
2297 (setq mode-name "Registers")
2298 (setq buffer-read-only t)
2299 (use-local-map gdb-registers-mode-map)
2300 (run-mode-hooks 'gdb-registers-mode-hook)
2301 (if (string-equal gdb-version "pre-6.4")
2302 (progn
2303 (if gdb-all-registers (setq mode-name "Registers:All"))
2304 'gdb-invalidate-registers)
2305 'gdb-invalidate-registers-1))
2306
2307 (defun gdb-registers-buffer-name ()
2308 (with-current-buffer gud-comint-buffer
2309 (concat "*registers of " (gdb-get-target-string) "*")))
2310
2311 (defun gdb-display-registers-buffer ()
2312 "Display integer register contents."
2313 (interactive)
2314 (gdb-display-buffer
2315 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2316
2317 (defun gdb-frame-registers-buffer ()
2318 "Display integer register contents in a new frame."
2319 (interactive)
2320 (let ((special-display-regexps (append special-display-regexps '(".*")))
2321 (special-display-frame-alist gdb-frame-parameters))
2322 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2323
2324 (defun gdb-all-registers ()
2325 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2326 (interactive)
2327 (when (string-equal gdb-version "pre-6.4")
2328 (if gdb-all-registers
2329 (progn
2330 (setq gdb-all-registers nil)
2331 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2332 (setq mode-name "Registers")))
2333 (setq gdb-all-registers t)
2334 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2335 (setq mode-name "Registers:All")))
2336 (message (format "Display of floating-point registers %sabled"
2337 (if gdb-all-registers "en" "dis")))
2338 (gdb-invalidate-registers)))
2339 \f
2340
2341 ;; Memory buffer.
2342 ;;
2343 (defcustom gdb-memory-repeat-count 32
2344 "Number of data items in memory window."
2345 :type 'integer
2346 :group 'gud
2347 :version "22.1")
2348
2349 (defcustom gdb-memory-format "x"
2350 "Display format of data items in memory window."
2351 :type '(choice (const :tag "Hexadecimal" "x")
2352 (const :tag "Signed decimal" "d")
2353 (const :tag "Unsigned decimal" "u")
2354 (const :tag "Octal" "o")
2355 (const :tag "Binary" "t"))
2356 :group 'gud
2357 :version "22.1")
2358
2359 (defcustom gdb-memory-unit "w"
2360 "Unit size of data items in memory window."
2361 :type '(choice (const :tag "Byte" "b")
2362 (const :tag "Halfword" "h")
2363 (const :tag "Word" "w")
2364 (const :tag "Giant word" "g"))
2365 :group 'gud
2366 :version "22.1")
2367
2368 (gdb-set-buffer-rules 'gdb-memory-buffer
2369 'gdb-memory-buffer-name
2370 'gdb-memory-mode)
2371
2372 (def-gdb-auto-updated-buffer gdb-memory-buffer
2373 gdb-invalidate-memory
2374 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2375 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2376 gdb-read-memory-handler
2377 gdb-read-memory-custom)
2378
2379 (defun gdb-read-memory-custom ()
2380 (save-excursion
2381 (goto-char (point-min))
2382 (if (looking-at "0x[[:xdigit:]]+")
2383 (setq gdb-memory-address (match-string 0)))))
2384
2385 (defvar gdb-memory-mode-map
2386 (let ((map (make-sparse-keymap)))
2387 (suppress-keymap map)
2388 (define-key map "q" 'kill-this-buffer)
2389 map))
2390
2391 (defun gdb-memory-set-address (event)
2392 "Set the start memory address."
2393 (interactive "e")
2394 (save-selected-window
2395 (select-window (posn-window (event-start event)))
2396 (let ((arg (read-from-minibuffer "Memory address: ")))
2397 (setq gdb-memory-address arg))
2398 (gdb-invalidate-memory)))
2399
2400 (defun gdb-memory-set-repeat-count (event)
2401 "Set the number of data items in memory window."
2402 (interactive "e")
2403 (save-selected-window
2404 (select-window (posn-window (event-start event)))
2405 (let* ((arg (read-from-minibuffer "Repeat count: "))
2406 (count (string-to-number arg)))
2407 (if (<= count 0)
2408 (error "Positive numbers only")
2409 (customize-set-variable 'gdb-memory-repeat-count count)
2410 (gdb-invalidate-memory)))))
2411
2412 (defun gdb-memory-format-binary ()
2413 "Set the display format to binary."
2414 (interactive)
2415 (customize-set-variable 'gdb-memory-format "t")
2416 (gdb-invalidate-memory))
2417
2418 (defun gdb-memory-format-octal ()
2419 "Set the display format to octal."
2420 (interactive)
2421 (customize-set-variable 'gdb-memory-format "o")
2422 (gdb-invalidate-memory))
2423
2424 (defun gdb-memory-format-unsigned ()
2425 "Set the display format to unsigned decimal."
2426 (interactive)
2427 (customize-set-variable 'gdb-memory-format "u")
2428 (gdb-invalidate-memory))
2429
2430 (defun gdb-memory-format-signed ()
2431 "Set the display format to decimal."
2432 (interactive)
2433 (customize-set-variable 'gdb-memory-format "d")
2434 (gdb-invalidate-memory))
2435
2436 (defun gdb-memory-format-hexadecimal ()
2437 "Set the display format to hexadecimal."
2438 (interactive)
2439 (customize-set-variable 'gdb-memory-format "x")
2440 (gdb-invalidate-memory))
2441
2442 (defvar gdb-memory-format-map
2443 (let ((map (make-sparse-keymap)))
2444 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2445 map)
2446 "Keymap to select format in the header line.")
2447
2448 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2449 "Menu of display formats in the header line.")
2450
2451 (define-key gdb-memory-format-menu [binary]
2452 '(menu-item "Binary" gdb-memory-format-binary
2453 :button (:radio . (equal gdb-memory-format "t"))))
2454 (define-key gdb-memory-format-menu [octal]
2455 '(menu-item "Octal" gdb-memory-format-octal
2456 :button (:radio . (equal gdb-memory-format "o"))))
2457 (define-key gdb-memory-format-menu [unsigned]
2458 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2459 :button (:radio . (equal gdb-memory-format "u"))))
2460 (define-key gdb-memory-format-menu [signed]
2461 '(menu-item "Signed Decimal" gdb-memory-format-signed
2462 :button (:radio . (equal gdb-memory-format "d"))))
2463 (define-key gdb-memory-format-menu [hexadecimal]
2464 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2465 :button (:radio . (equal gdb-memory-format "x"))))
2466
2467 (defun gdb-memory-format-menu (event)
2468 (interactive "@e")
2469 (x-popup-menu event gdb-memory-format-menu))
2470
2471 (defun gdb-memory-format-menu-1 (event)
2472 (interactive "e")
2473 (save-selected-window
2474 (select-window (posn-window (event-start event)))
2475 (let* ((selection (gdb-memory-format-menu event))
2476 (binding (and selection (lookup-key gdb-memory-format-menu
2477 (vector (car selection))))))
2478 (if binding (call-interactively binding)))))
2479
2480 (defun gdb-memory-unit-giant ()
2481 "Set the unit size to giant words (eight bytes)."
2482 (interactive)
2483 (customize-set-variable 'gdb-memory-unit "g")
2484 (gdb-invalidate-memory))
2485
2486 (defun gdb-memory-unit-word ()
2487 "Set the unit size to words (four bytes)."
2488 (interactive)
2489 (customize-set-variable 'gdb-memory-unit "w")
2490 (gdb-invalidate-memory))
2491
2492 (defun gdb-memory-unit-halfword ()
2493 "Set the unit size to halfwords (two bytes)."
2494 (interactive)
2495 (customize-set-variable 'gdb-memory-unit "h")
2496 (gdb-invalidate-memory))
2497
2498 (defun gdb-memory-unit-byte ()
2499 "Set the unit size to bytes."
2500 (interactive)
2501 (customize-set-variable 'gdb-memory-unit "b")
2502 (gdb-invalidate-memory))
2503
2504 (defvar gdb-memory-unit-map
2505 (let ((map (make-sparse-keymap)))
2506 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2507 map)
2508 "Keymap to select units in the header line.")
2509
2510 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2511 "Menu of units in the header line.")
2512
2513 (define-key gdb-memory-unit-menu [giantwords]
2514 '(menu-item "Giant words" gdb-memory-unit-giant
2515 :button (:radio . (equal gdb-memory-unit "g"))))
2516 (define-key gdb-memory-unit-menu [words]
2517 '(menu-item "Words" gdb-memory-unit-word
2518 :button (:radio . (equal gdb-memory-unit "w"))))
2519 (define-key gdb-memory-unit-menu [halfwords]
2520 '(menu-item "Halfwords" gdb-memory-unit-halfword
2521 :button (:radio . (equal gdb-memory-unit "h"))))
2522 (define-key gdb-memory-unit-menu [bytes]
2523 '(menu-item "Bytes" gdb-memory-unit-byte
2524 :button (:radio . (equal gdb-memory-unit "b"))))
2525
2526 (defun gdb-memory-unit-menu (event)
2527 (interactive "@e")
2528 (x-popup-menu event gdb-memory-unit-menu))
2529
2530 (defun gdb-memory-unit-menu-1 (event)
2531 (interactive "e")
2532 (save-selected-window
2533 (select-window (posn-window (event-start event)))
2534 (let* ((selection (gdb-memory-unit-menu event))
2535 (binding (and selection (lookup-key gdb-memory-unit-menu
2536 (vector (car selection))))))
2537 (if binding (call-interactively binding)))))
2538
2539 ;;from make-mode-line-mouse-map
2540 (defun gdb-make-header-line-mouse-map (mouse function) "\
2541 Return a keymap with single entry for mouse key MOUSE on the header line.
2542 MOUSE is defined to run function FUNCTION with no args in the buffer
2543 corresponding to the mode line clicked."
2544 (let ((map (make-sparse-keymap)))
2545 (define-key map (vector 'header-line mouse) function)
2546 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2547 map))
2548
2549 (defvar gdb-memory-font-lock-keywords
2550 '(;; <__function.name+n>
2551 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2552 )
2553 "Font lock keywords used in `gdb-memory-mode'.")
2554
2555 (defun gdb-memory-mode ()
2556 "Major mode for examining memory.
2557
2558 \\{gdb-memory-mode-map}"
2559 (kill-all-local-variables)
2560 (setq major-mode 'gdb-memory-mode)
2561 (setq mode-name "Memory")
2562 (setq buffer-read-only t)
2563 (use-local-map gdb-memory-mode-map)
2564 (setq header-line-format
2565 '(:eval
2566 (concat
2567 "Read address["
2568 (propertize
2569 "-"
2570 'face font-lock-warning-face
2571 'help-echo "mouse-1: Decrement address"
2572 'mouse-face 'mode-line-highlight
2573 'local-map
2574 (gdb-make-header-line-mouse-map
2575 'mouse-1
2576 (lambda () (interactive)
2577 (let ((gdb-memory-address
2578 ;; Let GDB do the arithmetic.
2579 (concat
2580 gdb-memory-address " - "
2581 (number-to-string
2582 (* gdb-memory-repeat-count
2583 (cond ((string= gdb-memory-unit "b") 1)
2584 ((string= gdb-memory-unit "h") 2)
2585 ((string= gdb-memory-unit "w") 4)
2586 ((string= gdb-memory-unit "g") 8)))))))
2587 (gdb-invalidate-memory)))))
2588 "|"
2589 (propertize "+"
2590 'face font-lock-warning-face
2591 'help-echo "mouse-1: Increment address"
2592 'mouse-face 'mode-line-highlight
2593 'local-map (gdb-make-header-line-mouse-map
2594 'mouse-1
2595 (lambda () (interactive)
2596 (let ((gdb-memory-address nil))
2597 (gdb-invalidate-memory)))))
2598 "]: "
2599 (propertize gdb-memory-address
2600 'face font-lock-warning-face
2601 'help-echo "mouse-1: Set memory address"
2602 'mouse-face 'mode-line-highlight
2603 'local-map (gdb-make-header-line-mouse-map
2604 'mouse-1
2605 #'gdb-memory-set-address))
2606 " Repeat Count: "
2607 (propertize (number-to-string gdb-memory-repeat-count)
2608 'face font-lock-warning-face
2609 'help-echo "mouse-1: Set repeat count"
2610 'mouse-face 'mode-line-highlight
2611 'local-map (gdb-make-header-line-mouse-map
2612 'mouse-1
2613 #'gdb-memory-set-repeat-count))
2614 " Display Format: "
2615 (propertize gdb-memory-format
2616 'face font-lock-warning-face
2617 'help-echo "mouse-3: Select display format"
2618 'mouse-face 'mode-line-highlight
2619 'local-map gdb-memory-format-map)
2620 " Unit Size: "
2621 (propertize gdb-memory-unit
2622 'face font-lock-warning-face
2623 'help-echo "mouse-3: Select unit size"
2624 'mouse-face 'mode-line-highlight
2625 'local-map gdb-memory-unit-map))))
2626 (set (make-local-variable 'font-lock-defaults)
2627 '(gdb-memory-font-lock-keywords))
2628 (run-mode-hooks 'gdb-memory-mode-hook)
2629 'gdb-invalidate-memory)
2630
2631 (defun gdb-memory-buffer-name ()
2632 (with-current-buffer gud-comint-buffer
2633 (concat "*memory of " (gdb-get-target-string) "*")))
2634
2635 (defun gdb-display-memory-buffer ()
2636 "Display memory contents."
2637 (interactive)
2638 (gdb-display-buffer
2639 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2640
2641 (defun gdb-frame-memory-buffer ()
2642 "Display memory contents in a new frame."
2643 (interactive)
2644 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2645 (special-display-frame-alist
2646 (cons '(left-fringe . 0)
2647 (cons '(right-fringe . 0)
2648 (cons '(width . 83) gdb-frame-parameters)))))
2649 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2650 \f
2651
2652 ;; Locals buffer.
2653 ;;
2654 (gdb-set-buffer-rules 'gdb-locals-buffer
2655 'gdb-locals-buffer-name
2656 'gdb-locals-mode)
2657
2658 (def-gdb-auto-update-trigger gdb-invalidate-locals
2659 (gdb-get-buffer 'gdb-locals-buffer)
2660 "server info locals\n"
2661 gdb-info-locals-handler)
2662
2663 (defvar gdb-locals-watch-map
2664 (let ((map (make-sparse-keymap)))
2665 (suppress-keymap map)
2666 (define-key map "\r" (lambda () (interactive)
2667 (beginning-of-line)
2668 (gud-watch)))
2669 (define-key map [mouse-2] (lambda (event) (interactive "e")
2670 (mouse-set-point event)
2671 (beginning-of-line)
2672 (gud-watch)))
2673 map)
2674 "Keymap to create watch expression of a complex data type local variable.")
2675
2676 (defconst gdb-struct-string
2677 (concat (propertize "[struct/union]"
2678 'mouse-face 'highlight
2679 'help-echo "mouse-2: create watch expression"
2680 'local-map gdb-locals-watch-map) "\n"))
2681
2682 (defconst gdb-array-string
2683 (concat " " (propertize "[array]"
2684 'mouse-face 'highlight
2685 'help-echo "mouse-2: create watch expression"
2686 'local-map gdb-locals-watch-map) "\n"))
2687
2688 ;; Abbreviate for arrays and structures.
2689 ;; These can be expanded using gud-display.
2690 (defun gdb-info-locals-handler ()
2691 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2692 gdb-pending-triggers))
2693 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2694 (with-current-buffer buf
2695 (goto-char (point-min))
2696 (while (re-search-forward "^[ }].*\n" nil t)
2697 (replace-match "" nil nil))
2698 (goto-char (point-min))
2699 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2700 (replace-match gdb-struct-string nil nil))
2701 (goto-char (point-min))
2702 (while (re-search-forward "\\s-*{.*\n" nil t)
2703 (replace-match gdb-array-string nil nil))))
2704 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2705 (and buf
2706 (with-current-buffer buf
2707 (let* ((window (get-buffer-window buf 0))
2708 (start (window-start window))
2709 (p (window-point window))
2710 (buffer-read-only nil))
2711 (erase-buffer)
2712 (insert-buffer-substring (gdb-get-buffer-create
2713 'gdb-partial-output-buffer))
2714 (set-window-start window start)
2715 (set-window-point window p))
2716 )))
2717 (run-hooks 'gdb-info-locals-hook))
2718
2719 (defvar gdb-locals-mode-map
2720 (let ((map (make-sparse-keymap)))
2721 (suppress-keymap map)
2722 (define-key map "q" 'kill-this-buffer)
2723 map))
2724
2725 (defun gdb-locals-mode ()
2726 "Major mode for gdb locals.
2727
2728 \\{gdb-locals-mode-map}"
2729 (kill-all-local-variables)
2730 (setq major-mode 'gdb-locals-mode)
2731 (setq mode-name (concat "Locals:" gdb-selected-frame))
2732 (setq buffer-read-only t)
2733 (use-local-map gdb-locals-mode-map)
2734 (set (make-local-variable 'font-lock-defaults)
2735 '(gdb-locals-font-lock-keywords))
2736 (run-mode-hooks 'gdb-locals-mode-hook)
2737 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2738 (string-equal gdb-version "pre-6.4"))
2739 'gdb-invalidate-locals
2740 'gdb-invalidate-locals-1))
2741
2742 (defun gdb-locals-buffer-name ()
2743 (with-current-buffer gud-comint-buffer
2744 (concat "*locals of " (gdb-get-target-string) "*")))
2745
2746 (defun gdb-display-locals-buffer ()
2747 "Display local variables of current stack and their values."
2748 (interactive)
2749 (gdb-display-buffer
2750 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2751
2752 (defun gdb-frame-locals-buffer ()
2753 "Display local variables of current stack and their values in a new frame."
2754 (interactive)
2755 (let ((special-display-regexps (append special-display-regexps '(".*")))
2756 (special-display-frame-alist gdb-frame-parameters))
2757 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2758 \f
2759
2760 ;;;; Window management
2761 (defun gdb-display-buffer (buf dedicated &optional size)
2762 (let ((answer (get-buffer-window buf 0))
2763 (must-split nil))
2764 (if answer
2765 (display-buffer buf nil 0) ;Raise the frame if necessary.
2766 ;; The buffer is not yet displayed.
2767 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2768 (let ((window (get-lru-window)))
2769 (if (and window
2770 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2771 ,gdb-source-window))))
2772 (progn
2773 (set-window-buffer window buf)
2774 (setq answer window))
2775 (setq must-split t)))
2776 (if must-split
2777 (let* ((largest (get-largest-window))
2778 (cur-size (window-height largest))
2779 (new-size (and size (< size cur-size) (- cur-size size))))
2780 (setq answer (split-window largest new-size))
2781 (set-window-buffer answer buf)
2782 (set-window-dedicated-p answer dedicated)))
2783 answer)))
2784
2785 \f
2786 ;;; Shared keymap initialization:
2787
2788 (let ((menu (make-sparse-keymap "GDB-Windows")))
2789 (define-key gud-menu-map [displays]
2790 `(menu-item "GDB-Windows" ,menu
2791 :visible (memq gud-minor-mode '(gdbmi gdba))))
2792 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2793 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2794 (define-key menu [inferior]
2795 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2796 :enable gdb-use-separate-io-buffer))
2797 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2798 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2799 (define-key menu [disassembly]
2800 '("Disassembly" . gdb-display-assembler-buffer))
2801 (define-key menu [breakpoints]
2802 '("Breakpoints" . gdb-display-breakpoints-buffer))
2803 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2804 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2805
2806 (let ((menu (make-sparse-keymap "GDB-Frames")))
2807 (define-key gud-menu-map [frames]
2808 `(menu-item "GDB-Frames" ,menu
2809 :visible (memq gud-minor-mode '(gdbmi gdba))))
2810 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2811 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2812 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2813 (define-key menu [inferior]
2814 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2815 :enable gdb-use-separate-io-buffer))
2816 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2817 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2818 (define-key menu [breakpoints]
2819 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2820 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2821 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2822
2823 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2824 (define-key gud-menu-map [ui]
2825 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2826 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2827 (define-key menu [gdb-find-source-frame]
2828 '(menu-item "Look For Source Frame" gdb-find-source-frame
2829 :visible (eq gud-minor-mode 'gdba)
2830 :help "Toggle look for source frame."
2831 :button (:toggle . gdb-find-source-frame)))
2832 (define-key menu [gdb-use-separate-io]
2833 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2834 :visible (eq gud-minor-mode 'gdba)
2835 :help "Toggle separate IO for debugged program."
2836 :button (:toggle . gdb-use-separate-io-buffer)))
2837 (define-key menu [gdb-many-windows]
2838 '(menu-item "Display Other Windows" gdb-many-windows
2839 :help "Toggle display of locals, stack and breakpoint information"
2840 :button (:toggle . gdb-many-windows)))
2841 (define-key menu [gdb-restore-windows]
2842 '(menu-item "Restore Window Layout" gdb-restore-windows
2843 :help "Restore standard layout for debug session.")))
2844
2845 (defun gdb-frame-gdb-buffer ()
2846 "Display GUD buffer in a new frame."
2847 (interactive)
2848 (let ((special-display-regexps (append special-display-regexps '(".*")))
2849 (special-display-frame-alist
2850 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2851 gdb-frame-parameters)))
2852 (same-window-regexps nil))
2853 (display-buffer gud-comint-buffer)))
2854
2855 (defun gdb-display-gdb-buffer ()
2856 "Display GUD buffer."
2857 (interactive)
2858 (let ((same-window-regexps nil))
2859 (pop-to-buffer gud-comint-buffer)))
2860
2861 (defun gdb-set-window-buffer (name)
2862 (set-window-buffer (selected-window) (get-buffer name))
2863 (set-window-dedicated-p (selected-window) t))
2864
2865 (defun gdb-setup-windows ()
2866 "Layout the window pattern for `gdb-many-windows'."
2867 (gdb-display-locals-buffer)
2868 (gdb-display-stack-buffer)
2869 (delete-other-windows)
2870 (gdb-display-breakpoints-buffer)
2871 (delete-other-windows)
2872 ; Don't dedicate.
2873 (pop-to-buffer gud-comint-buffer)
2874 (split-window nil ( / ( * (window-height) 3) 4))
2875 (split-window nil ( / (window-height) 3))
2876 (split-window-horizontally)
2877 (other-window 1)
2878 (gdb-set-window-buffer (gdb-locals-buffer-name))
2879 (other-window 1)
2880 (switch-to-buffer
2881 (if gud-last-last-frame
2882 (gud-find-file (car gud-last-last-frame))
2883 (if gdb-main-file
2884 (gud-find-file gdb-main-file)
2885 ;; Put buffer list in window if we
2886 ;; can't find a source file.
2887 (list-buffers-noselect))))
2888 (setq gdb-source-window (selected-window))
2889 (when gdb-use-separate-io-buffer
2890 (split-window-horizontally)
2891 (other-window 1)
2892 (gdb-set-window-buffer
2893 (gdb-get-buffer-create 'gdb-inferior-io)))
2894 (other-window 1)
2895 (gdb-set-window-buffer (gdb-stack-buffer-name))
2896 (split-window-horizontally)
2897 (other-window 1)
2898 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2899 (other-window 1))
2900
2901 (defun gdb-restore-windows ()
2902 "Restore the basic arrangement of windows used by gdba.
2903 This arrangement depends on the value of `gdb-many-windows'."
2904 (interactive)
2905 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2906 (delete-other-windows)
2907 (if gdb-many-windows
2908 (gdb-setup-windows)
2909 (when (or gud-last-last-frame gdb-show-main)
2910 (split-window)
2911 (other-window 1)
2912 (switch-to-buffer
2913 (if gud-last-last-frame
2914 (gud-find-file (car gud-last-last-frame))
2915 (gud-find-file gdb-main-file)))
2916 (setq gdb-source-window (selected-window))
2917 (other-window 1))))
2918
2919 (defun gdb-reset ()
2920 "Exit a debugging session cleanly.
2921 Kills the gdb buffers, and resets variables and the source buffers."
2922 (dolist (buffer (buffer-list))
2923 (unless (eq buffer gud-comint-buffer)
2924 (with-current-buffer buffer
2925 (if (memq gud-minor-mode '(gdbmi gdba))
2926 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2927 (kill-buffer nil)
2928 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2929 (setq gud-minor-mode nil)
2930 (kill-local-variable 'tool-bar-map)
2931 (kill-local-variable 'gdb-define-alist))))))
2932 (setq gdb-overlay-arrow-position nil)
2933 (setq overlay-arrow-variable-list
2934 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2935 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2936 (setq gdb-stack-position nil)
2937 (setq overlay-arrow-variable-list
2938 (delq 'gdb-stack-position overlay-arrow-variable-list))
2939 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
2940 (setq gud-running nil)
2941 (setq gdb-active-process nil)
2942 (setq gdb-var-list nil)
2943 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2944
2945 (defun gdb-source-info ()
2946 "Find the source file where the program starts and displays it with related
2947 buffers."
2948 (goto-char (point-min))
2949 (if (and (search-forward "Located in " nil t)
2950 (looking-at "\\S-+"))
2951 (setq gdb-main-file (match-string 0)))
2952 (goto-char (point-min))
2953 (if (search-forward "Includes preprocessor macro info." nil t)
2954 (setq gdb-macro-info t))
2955 (if gdb-many-windows
2956 (gdb-setup-windows)
2957 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2958 (if gdb-show-main
2959 (let ((pop-up-windows t))
2960 (display-buffer (gud-find-file gdb-main-file))))))
2961
2962 (defun gdb-get-location (bptno line flag)
2963 "Find the directory containing the relevant source file.
2964 Put in buffer and place breakpoint icon."
2965 (goto-char (point-min))
2966 (catch 'file-not-found
2967 (if (search-forward "Located in " nil t)
2968 (when (looking-at "\\S-+")
2969 (delete (cons bptno "File not found") gdb-location-alist)
2970 (push (cons bptno (match-string 0)) gdb-location-alist))
2971 (gdb-resync)
2972 (unless (assoc bptno gdb-location-alist)
2973 (push (cons bptno "File not found") gdb-location-alist)
2974 (message-box "Cannot find source file for breakpoint location.\n\
2975 Add directory to search path for source files using the GDB command, dir."))
2976 (throw 'file-not-found nil))
2977 (with-current-buffer
2978 (find-file-noselect (match-string 0))
2979 (save-current-buffer
2980 (set (make-local-variable 'gud-minor-mode) 'gdba)
2981 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2982 ;; only want one breakpoint icon at each location
2983 (save-excursion
2984 (goto-line (string-to-number line))
2985 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2986
2987 (add-hook 'find-file-hook 'gdb-find-file-hook)
2988
2989 (defun gdb-find-file-hook ()
2990 "Set up buffer for debugging if file is part of the source code
2991 of the current session."
2992 (if (and (buffer-name gud-comint-buffer)
2993 ;; in case gud or gdb-ui is just loaded
2994 gud-comint-buffer
2995 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2996 '(gdba gdbmi)))
2997 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
2998 (if (member (if (string-equal gdb-version "pre-6.4")
2999 (file-name-nondirectory buffer-file-name)
3000 buffer-file-name)
3001 gdb-source-file-list)
3002 (with-current-buffer (find-buffer-visiting buffer-file-name)
3003 (set (make-local-variable 'gud-minor-mode)
3004 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3005 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
3006
3007 ;;from put-image
3008 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3009 "Put string PUTSTRING in front of POS in the current buffer.
3010 PUTSTRING is displayed by putting an overlay into the current buffer with a
3011 `before-string' string that has a `display' property whose value is
3012 PUTSTRING."
3013 (let ((string (make-string 1 ?x))
3014 (buffer (current-buffer)))
3015 (setq putstring (copy-sequence putstring))
3016 (let ((overlay (make-overlay pos pos buffer))
3017 (prop (or dprop
3018 (list (list 'margin 'left-margin) putstring))))
3019 (put-text-property 0 1 'display prop string)
3020 (if sprops
3021 (add-text-properties 0 1 sprops string))
3022 (overlay-put overlay 'put-break t)
3023 (overlay-put overlay 'before-string string))))
3024
3025 ;;from remove-images
3026 (defun gdb-remove-strings (start end &optional buffer)
3027 "Remove strings between START and END in BUFFER.
3028 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3029 BUFFER nil or omitted means use the current buffer."
3030 (unless buffer
3031 (setq buffer (current-buffer)))
3032 (dolist (overlay (overlays-in start end))
3033 (when (overlay-get overlay 'put-break)
3034 (delete-overlay overlay))))
3035
3036 (defun gdb-put-breakpoint-icon (enabled bptno)
3037 (let ((start (- (line-beginning-position) 1))
3038 (end (+ (line-end-position) 1))
3039 (putstring (if enabled "B" "b"))
3040 (source-window (get-buffer-window (current-buffer) 0)))
3041 (add-text-properties
3042 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3043 putstring)
3044 (if enabled
3045 (add-text-properties
3046 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3047 (add-text-properties
3048 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3049 (gdb-remove-breakpoint-icons start end)
3050 (if (display-images-p)
3051 (if (>= (or left-fringe-width
3052 (if source-window (car (window-fringes source-window)))
3053 gdb-buffer-fringe-width) 8)
3054 (gdb-put-string
3055 nil (1+ start)
3056 `(left-fringe breakpoint
3057 ,(if enabled
3058 'breakpoint-enabled
3059 'breakpoint-disabled))
3060 'gdb-bptno bptno
3061 'gdb-enabled enabled)
3062 (when (< left-margin-width 2)
3063 (save-current-buffer
3064 (setq left-margin-width 2)
3065 (if source-window
3066 (set-window-margins
3067 source-window
3068 left-margin-width right-margin-width))))
3069 (put-image
3070 (if enabled
3071 (or breakpoint-enabled-icon
3072 (setq breakpoint-enabled-icon
3073 (find-image `((:type xpm :data
3074 ,breakpoint-xpm-data
3075 :ascent 100 :pointer hand)
3076 (:type pbm :data
3077 ,breakpoint-enabled-pbm-data
3078 :ascent 100 :pointer hand)))))
3079 (or breakpoint-disabled-icon
3080 (setq breakpoint-disabled-icon
3081 (find-image `((:type xpm :data
3082 ,breakpoint-xpm-data
3083 :conversion disabled
3084 :ascent 100 :pointer hand)
3085 (:type pbm :data
3086 ,breakpoint-disabled-pbm-data
3087 :ascent 100 :pointer hand))))))
3088 (+ start 1)
3089 putstring
3090 'left-margin))
3091 (when (< left-margin-width 2)
3092 (save-current-buffer
3093 (setq left-margin-width 2)
3094 (let ((window (get-buffer-window (current-buffer) 0)))
3095 (if window
3096 (set-window-margins
3097 window left-margin-width right-margin-width)))))
3098 (gdb-put-string
3099 (propertize putstring
3100 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3101 (1+ start)))))
3102
3103 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3104 (gdb-remove-strings start end)
3105 (if (display-images-p)
3106 (remove-images start end))
3107 (when remove-margin
3108 (setq left-margin-width 0)
3109 (let ((window (get-buffer-window (current-buffer) 0)))
3110 (if window
3111 (set-window-margins
3112 window left-margin-width right-margin-width)))))
3113
3114 \f
3115 ;;
3116 ;; Assembler buffer.
3117 ;;
3118 (gdb-set-buffer-rules 'gdb-assembler-buffer
3119 'gdb-assembler-buffer-name
3120 'gdb-assembler-mode)
3121
3122 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3123 ;; window-start but keep the overlay arrow/current line visible.
3124 (defun gdb-assembler-handler ()
3125 (setq gdb-pending-triggers
3126 (delq 'gdb-invalidate-assembler
3127 gdb-pending-triggers))
3128 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3129 (and buf
3130 (with-current-buffer buf
3131 (let* ((window (get-buffer-window buf 0))
3132 (p (window-point window))
3133 (buffer-read-only nil))
3134 (erase-buffer)
3135 (insert-buffer-substring (gdb-get-buffer-create
3136 'gdb-partial-output-buffer))
3137 (set-window-point window p)))))
3138 ;; put customisation here
3139 (gdb-assembler-custom))
3140
3141 (defun gdb-assembler-custom ()
3142 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3143 (pos 1) (address) (flag) (bptno))
3144 (with-current-buffer buffer
3145 (save-excursion
3146 (if (not (equal gdb-pc-address "main"))
3147 (progn
3148 (goto-char (point-min))
3149 (if (and gdb-pc-address
3150 (search-forward gdb-pc-address nil t))
3151 (progn
3152 (setq pos (point))
3153 (beginning-of-line)
3154 (setq fringe-indicator-alist
3155 (if (string-equal gdb-frame-number "0")
3156 nil
3157 '((overlay-arrow . hollow-right-triangle))))
3158 (or gdb-overlay-arrow-position
3159 (setq gdb-overlay-arrow-position (make-marker)))
3160 (set-marker gdb-overlay-arrow-position (point))))))
3161 ;; remove all breakpoint-icons in assembler buffer before updating.
3162 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3163 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3164 (goto-char (point-min))
3165 (while (< (point) (- (point-max) 1))
3166 (forward-line 1)
3167 (if (looking-at "[^\t].*?breakpoint")
3168 (progn
3169 (looking-at
3170 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3171 (setq bptno (match-string 1))
3172 (setq flag (char-after (match-beginning 2)))
3173 (setq address (match-string 3))
3174 (with-current-buffer buffer
3175 (save-excursion
3176 (goto-char (point-min))
3177 (if (search-forward address nil t)
3178 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3179 (if (not (equal gdb-pc-address "main"))
3180 (with-current-buffer buffer
3181 (set-window-point (get-buffer-window buffer 0) pos)))))
3182
3183 (defvar gdb-assembler-mode-map
3184 (let ((map (make-sparse-keymap)))
3185 (suppress-keymap map)
3186 (define-key map "q" 'kill-this-buffer)
3187 map))
3188
3189 (defvar gdb-assembler-font-lock-keywords
3190 '(;; <__function.name+n>
3191 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3192 (1 font-lock-function-name-face))
3193 ;; 0xNNNNNNNN <__function.name+n>: opcode
3194 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3195 (4 font-lock-keyword-face))
3196 ;; %register(at least i386)
3197 ("%\\sw+" . font-lock-variable-name-face)
3198 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3199 (1 font-lock-comment-face)
3200 (2 font-lock-function-name-face))
3201 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3202 "Font lock keywords used in `gdb-assembler-mode'.")
3203
3204 (defun gdb-assembler-mode ()
3205 "Major mode for viewing code assembler.
3206
3207 \\{gdb-assembler-mode-map}"
3208 (kill-all-local-variables)
3209 (setq major-mode 'gdb-assembler-mode)
3210 (setq mode-name (concat "Machine:" gdb-selected-frame))
3211 (setq gdb-overlay-arrow-position nil)
3212 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3213 (setq fringes-outside-margins t)
3214 (setq buffer-read-only t)
3215 (use-local-map gdb-assembler-mode-map)
3216 (gdb-invalidate-assembler)
3217 (set (make-local-variable 'font-lock-defaults)
3218 '(gdb-assembler-font-lock-keywords))
3219 (run-mode-hooks 'gdb-assembler-mode-hook)
3220 'gdb-invalidate-assembler)
3221
3222 (defun gdb-assembler-buffer-name ()
3223 (with-current-buffer gud-comint-buffer
3224 (concat "*disassembly of " (gdb-get-target-string) "*")))
3225
3226 (defun gdb-display-assembler-buffer ()
3227 "Display disassembly view."
3228 (interactive)
3229 (setq gdb-previous-frame nil)
3230 (gdb-display-buffer
3231 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3232
3233 (defun gdb-frame-assembler-buffer ()
3234 "Display disassembly view in a new frame."
3235 (interactive)
3236 (setq gdb-previous-frame nil)
3237 (let ((special-display-regexps (append special-display-regexps '(".*")))
3238 (special-display-frame-alist gdb-frame-parameters))
3239 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3240
3241 ;; modified because if gdb-pc-address has changed value a new command
3242 ;; must be enqueued to update the buffer with the new output
3243 (defun gdb-invalidate-assembler (&optional ignored)
3244 (if (gdb-get-buffer 'gdb-assembler-buffer)
3245 (progn
3246 (unless (and gdb-selected-frame
3247 (string-equal gdb-selected-frame gdb-previous-frame))
3248 (if (or (not (member 'gdb-invalidate-assembler
3249 gdb-pending-triggers))
3250 (not (string-equal gdb-pc-address
3251 gdb-previous-frame-address)))
3252 (progn
3253 ;; take previous disassemble command, if any, off the queue
3254 (with-current-buffer gud-comint-buffer
3255 (let ((queue gdb-input-queue))
3256 (dolist (item queue)
3257 (if (equal (cdr item) '(gdb-assembler-handler))
3258 (setq gdb-input-queue
3259 (delete item gdb-input-queue))))))
3260 (gdb-enqueue-input
3261 (list
3262 (concat gdb-server-prefix "disassemble "
3263 (if (member gdb-pc-address '(nil "main")) nil "0x")
3264 gdb-pc-address "\n")
3265 'gdb-assembler-handler))
3266 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3267 (setq gdb-previous-frame-address gdb-pc-address)
3268 (setq gdb-previous-frame gdb-selected-frame)))))))
3269
3270 (defun gdb-get-selected-frame ()
3271 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3272 (progn
3273 (gdb-enqueue-input
3274 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3275 (push 'gdb-get-selected-frame
3276 gdb-pending-triggers))))
3277
3278 (defun gdb-frame-handler ()
3279 (setq gdb-pending-triggers
3280 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3281 (goto-char (point-min))
3282 (when (re-search-forward
3283 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3284 (setq gdb-frame-number (match-string 1))
3285 (setq gdb-frame-address (match-string 2)))
3286 (goto-char (point-min))
3287 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3288 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3289 nil t)
3290 (setq gdb-selected-frame (match-string 2))
3291 (if (gdb-get-buffer 'gdb-locals-buffer)
3292 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3293 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3294 (if (gdb-get-buffer 'gdb-assembler-buffer)
3295 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3296 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3297 (setq gdb-pc-address (match-string 1))
3298 (if (and (match-string 3) gud-overlay-arrow-position)
3299 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3300 (position (marker-position gud-overlay-arrow-position)))
3301 (when (and buffer
3302 (string-equal (buffer-name buffer)
3303 (file-name-nondirectory (match-string 3))))
3304 (with-current-buffer buffer
3305 (setq fringe-indicator-alist
3306 (if (string-equal gdb-frame-number "0")
3307 nil
3308 '((overlay-arrow . hollow-right-triangle))))
3309 (set-marker gud-overlay-arrow-position position))))))
3310 (goto-char (point-min))
3311 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3312 (setq gdb-current-language (match-string 1)))
3313 (gdb-invalidate-assembler))
3314
3315 \f
3316 ;; Code specific to GDB 6.4
3317 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3318
3319 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3320 "Create list of source files for current GDB session.
3321 If buffers already exist for any of these files, gud-minor-mode
3322 is set in them."
3323 (goto-char (point-min))
3324 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3325 (push (match-string 1) gdb-source-file-list))
3326 (dolist (buffer (buffer-list))
3327 (with-current-buffer buffer
3328 (when (member buffer-file-name gdb-source-file-list)
3329 (set (make-local-variable 'gud-minor-mode)
3330 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3331 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3332 (when gud-tooltip-mode
3333 (make-local-variable 'gdb-define-alist)
3334 (gdb-create-define-alist)
3335 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3336 (gdb-force-mode-line-update
3337 (propertize "ready" 'face font-lock-variable-name-face)))
3338
3339 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3340 (defun gdb-var-list-children-1 (varnum)
3341 (gdb-enqueue-input
3342 (list
3343 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3344 (concat "server interpreter mi \"-var-list-children --all-values "
3345 varnum "\"\n")
3346 (concat "-var-list-children --all-values " varnum "\n"))
3347 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3348
3349 (defconst gdb-var-list-children-regexp-1
3350 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3351 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\)\
3352 \\(}\\|,.*?\\(type=\"\\(.+?\\)\"\\)?.*?}\\)")
3353
3354 (defun gdb-var-list-children-handler-1 (varnum)
3355 (goto-char (point-min))
3356 (let ((var-list nil))
3357 (catch 'child-already-watched
3358 (dolist (var gdb-var-list)
3359 (if (string-equal varnum (car var))
3360 (progn
3361 (push var var-list)
3362 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3363 (let ((varchild (list (match-string 1)
3364 (match-string 2)
3365 (match-string 3)
3366 (match-string 7)
3367 (read (match-string 4))
3368 nil)))
3369 (if (assoc (car varchild) gdb-var-list)
3370 (throw 'child-already-watched nil))
3371 (push varchild var-list))))
3372 (push var var-list)))
3373 (setq gdb-var-list (nreverse var-list))))
3374 (gdb-speedbar-update))
3375
3376 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3377 (defun gdb-var-update-1 ()
3378 (if (not (member 'gdb-var-update gdb-pending-triggers))
3379 (progn
3380 (gdb-enqueue-input
3381 (list
3382 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3383 "server interpreter mi \"-var-update --all-values *\"\n"
3384 "-var-update --all-values *\n")
3385 'gdb-var-update-handler-1))
3386 (push 'gdb-var-update gdb-pending-triggers))))
3387
3388 (defconst gdb-var-update-regexp-1
3389 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3390 in_scope=\"\\(.*?\\)\".*?}")
3391
3392 (defun gdb-var-update-handler-1 ()
3393 (dolist (var gdb-var-list)
3394 (setcar (nthcdr 5 var) nil))
3395 (goto-char (point-min))
3396 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3397 (let* ((varnum (match-string 1))
3398 (var (assoc varnum gdb-var-list)))
3399 (when var
3400 (if (string-equal (match-string 3) "false")
3401 (setcar (nthcdr 5 var) 'out-of-scope)
3402 (setcar (nthcdr 5 var) 'changed)
3403 (setcar (nthcdr 4 var)
3404 (read (match-string 2)))))))
3405 (setq gdb-pending-triggers
3406 (delq 'gdb-var-update gdb-pending-triggers))
3407 (gdb-speedbar-update))
3408
3409 ;; Registers buffer.
3410 ;;
3411 (gdb-set-buffer-rules 'gdb-registers-buffer
3412 'gdb-registers-buffer-name
3413 'gdb-registers-mode)
3414
3415 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3416 (gdb-get-buffer 'gdb-registers-buffer)
3417 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3418 "server interpreter mi \"-data-list-register-values x\"\n"
3419 "-data-list-register-values x\n")
3420 gdb-data-list-register-values-handler)
3421
3422 (defconst gdb-data-list-register-values-regexp
3423 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3424
3425 (defun gdb-data-list-register-values-handler ()
3426 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3427 gdb-pending-triggers))
3428 (goto-char (point-min))
3429 (if (re-search-forward gdb-error-regexp nil t)
3430 (let ((err (match-string 1)))
3431 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3432 (let ((buffer-read-only nil))
3433 (erase-buffer)
3434 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3435 (insert err)
3436 (goto-char (point-min)))))
3437 (let ((register-list (reverse gdb-register-names))
3438 (register nil) (register-string nil) (register-values nil))
3439 (goto-char (point-min))
3440 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3441 (setq register (pop register-list))
3442 (setq register-string (concat register "\t" (match-string 2) "\n"))
3443 (if (member (match-string 1) gdb-changed-registers)
3444 (put-text-property 0 (length register-string)
3445 'face 'font-lock-warning-face
3446 register-string))
3447 (setq register-values
3448 (concat register-values register-string)))
3449 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3450 (with-current-buffer buf
3451 (let* ((window (get-buffer-window buf 0))
3452 (start (window-start window))
3453 (p (window-point window))
3454 (buffer-read-only nil))
3455 (erase-buffer)
3456 (insert register-values)
3457 (set-window-start window start)
3458 (set-window-point window p))))))
3459 (gdb-data-list-register-values-custom))
3460
3461 (defun gdb-data-list-register-values-custom ()
3462 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3463 (save-excursion
3464 (let ((buffer-read-only nil)
3465 start end)
3466 (goto-char (point-min))
3467 (while (< (point) (point-max))
3468 (setq start (line-beginning-position))
3469 (setq end (line-end-position))
3470 (when (looking-at "^[^\t]+")
3471 (unless (string-equal (match-string 0) "No registers.")
3472 (put-text-property start (match-end 0)
3473 'face font-lock-variable-name-face)
3474 (add-text-properties start end
3475 '(help-echo "mouse-2: edit value"
3476 mouse-face highlight))))
3477 (forward-line 1))))))
3478
3479 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3480 (defun gdb-get-changed-registers ()
3481 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3482 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3483 (progn
3484 (gdb-enqueue-input
3485 (list
3486 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3487 "server interpreter mi -data-list-changed-registers\n"
3488 "-data-list-changed-registers\n")
3489 'gdb-get-changed-registers-handler))
3490 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3491
3492 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3493
3494 (defun gdb-get-changed-registers-handler ()
3495 (setq gdb-pending-triggers
3496 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3497 (setq gdb-changed-registers nil)
3498 (goto-char (point-min))
3499 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3500 (push (match-string 1) gdb-changed-registers)))
3501 \f
3502
3503 ;; Locals buffer.
3504 ;;
3505 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3506 (gdb-set-buffer-rules 'gdb-locals-buffer
3507 'gdb-locals-buffer-name
3508 'gdb-locals-mode)
3509
3510 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3511 (gdb-get-buffer 'gdb-locals-buffer)
3512 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3513 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3514 "-stack-list-locals --simple-values\n")
3515 gdb-stack-list-locals-handler)
3516
3517 (defconst gdb-stack-list-locals-regexp
3518 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3519
3520 (defvar gdb-locals-watch-map-1
3521 (let ((map (make-sparse-keymap)))
3522 (suppress-keymap map)
3523 (define-key map "\r" 'gud-watch)
3524 (define-key map [mouse-2] 'gud-watch)
3525 map)
3526 "Keymap to create watch expression of a complex data type local variable.")
3527
3528 (defvar gdb-edit-locals-map-1
3529 (let ((map (make-sparse-keymap)))
3530 (suppress-keymap map)
3531 (define-key map "\r" 'gdb-edit-locals-value)
3532 (define-key map [mouse-2] 'gdb-edit-locals-value)
3533 map)
3534 "Keymap to edit value of a simple data type local variable.")
3535
3536 (defun gdb-edit-locals-value (&optional event)
3537 "Assign a value to a variable displayed in the locals buffer."
3538 (interactive (list last-input-event))
3539 (save-excursion
3540 (if event (posn-set-point (event-end event)))
3541 (beginning-of-line)
3542 (let* ((var (current-word))
3543 (value (read-string (format "New value (%s): " var))))
3544 (gdb-enqueue-input
3545 (list (concat gdb-server-prefix"set variable " var " = " value "\n")
3546 'ignore)))))
3547
3548 ;; Dont display values of arrays or structures.
3549 ;; These can be expanded using gud-watch.
3550 (defun gdb-stack-list-locals-handler ()
3551 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3552 gdb-pending-triggers))
3553 (goto-char (point-min))
3554 (if (re-search-forward gdb-error-regexp nil t)
3555 (let ((err (match-string 1)))
3556 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3557 (let ((buffer-read-only nil))
3558 (erase-buffer)
3559 (insert err)
3560 (goto-char (point-min)))))
3561 (let (local locals-list)
3562 (goto-char (point-min))
3563 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3564 (let ((local (list (match-string 1)
3565 (match-string 2)
3566 nil)))
3567 (if (looking-at ",value=\\(\".*\"\\).*?}")
3568 (setcar (nthcdr 2 local) (read (match-string 1))))
3569 (push local locals-list)))
3570 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3571 (and buf (with-current-buffer buf
3572 (let* ((window (get-buffer-window buf 0))
3573 (start (window-start window))
3574 (p (window-point window))
3575 (buffer-read-only nil) (name) (value))
3576 (erase-buffer)
3577 (dolist (local locals-list)
3578 (setq name (car local))
3579 (setq value (nth 2 local))
3580 (if (or (not value)
3581 (string-match "^\\0x" value))
3582 (add-text-properties 0 (length name)
3583 `(mouse-face highlight
3584 help-echo "mouse-2: create watch expression"
3585 local-map ,gdb-locals-watch-map-1)
3586 name)
3587 (add-text-properties 0 (length value)
3588 `(mouse-face highlight
3589 help-echo "mouse-2: edit value"
3590 local-map ,gdb-edit-locals-map-1)
3591 value))
3592 (insert
3593 (concat name "\t" (nth 1 local)
3594 "\t" value "\n")))
3595 (set-window-start window start)
3596 (set-window-point window p))))))))
3597
3598 (defun gdb-get-register-names ()
3599 "Create a list of register names."
3600 (goto-char (point-min))
3601 (setq gdb-register-names nil)
3602 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3603 (push (match-string 1) gdb-register-names)))
3604
3605 (provide 'gdb-ui)
3606
3607 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3608 ;;; gdb-ui.el ends here