]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-ui.el
(gdb-locals-watch-map, gdb-locals-watch-map-1): Suppress keymap first.
[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 inferior. 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 4)
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 (cond ((string-match "+" text) ;expand this node
906 (if (and
907 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
908 (string-equal gdb-version "pre-6.4"))
909 (gdb-var-list-children token)
910 (gdb-var-list-children-1 token)))
911 ((string-match "-" text) ;contract this node
912 (dolist (var gdb-var-list)
913 (if (string-match (concat token "\\.") (car var))
914 (setq gdb-var-list (delq var gdb-var-list))))
915 (speedbar-change-expand-button-char ?+)
916 (speedbar-delete-subblock indent))
917 (t (error "Ooops... not sure what to do")))
918 (speedbar-center-buffer-smartly))
919
920 (defun gdb-get-target-string ()
921 (with-current-buffer gud-comint-buffer
922 gud-target-name))
923 \f
924
925 ;;
926 ;; gdb buffers.
927 ;;
928 ;; Each buffer has a TYPE -- a symbol that identifies the function
929 ;; of that particular buffer.
930 ;;
931 ;; The usual gdb interaction buffer is given the type `gdba' and
932 ;; is constructed specially.
933 ;;
934 ;; Others are constructed by gdb-get-buffer-create and
935 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
936
937 (defvar gdb-buffer-rules-assoc '())
938
939 (defun gdb-get-buffer (key)
940 "Return the gdb buffer tagged with type KEY.
941 The key should be one of the cars in `gdb-buffer-rules-assoc'."
942 (save-excursion
943 (gdb-look-for-tagged-buffer key (buffer-list))))
944
945 (defun gdb-get-buffer-create (key)
946 "Create a new gdb buffer of the type specified by KEY.
947 The key should be one of the cars in `gdb-buffer-rules-assoc'."
948 (or (gdb-get-buffer key)
949 (let* ((rules (assoc key gdb-buffer-rules-assoc))
950 (name (funcall (gdb-rules-name-maker rules)))
951 (new (get-buffer-create name)))
952 (with-current-buffer new
953 (let ((trigger))
954 (if (cdr (cdr rules))
955 (setq trigger (funcall (car (cdr (cdr rules))))))
956 (setq gdb-buffer-type key)
957 (set (make-local-variable 'gud-minor-mode)
958 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
959 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
960 (if trigger (funcall trigger)))
961 new))))
962
963 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
964
965 (defun gdb-look-for-tagged-buffer (key bufs)
966 (let ((retval nil))
967 (while (and (not retval) bufs)
968 (set-buffer (car bufs))
969 (if (eq gdb-buffer-type key)
970 (setq retval (car bufs)))
971 (setq bufs (cdr bufs)))
972 retval))
973
974 ;;
975 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
976 ;; at least one and possible more functions. The functions have these
977 ;; roles in defining a buffer type:
978 ;;
979 ;; NAME - Return a name for this buffer type.
980 ;;
981 ;; The remaining function(s) are optional:
982 ;;
983 ;; MODE - called in a new buffer with no arguments, should establish
984 ;; the proper mode for the buffer.
985 ;;
986
987 (defun gdb-set-buffer-rules (buffer-type &rest rules)
988 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
989 (if binding
990 (setcdr binding rules)
991 (push (cons buffer-type rules)
992 gdb-buffer-rules-assoc))))
993
994 ;; GUD buffers are an exception to the rules
995 (gdb-set-buffer-rules 'gdba 'error)
996
997 ;; Partial-output buffer : This accumulates output from a command executed on
998 ;; behalf of emacs (rather than the user).
999 ;;
1000 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1001 'gdb-partial-output-name)
1002
1003 (defun gdb-partial-output-name ()
1004 (concat "*partial-output-"
1005 (gdb-get-target-string)
1006 "*"))
1007
1008 \f
1009 (gdb-set-buffer-rules 'gdb-inferior-io
1010 'gdb-inferior-io-name
1011 'gdb-inferior-io-mode)
1012
1013 (defun gdb-inferior-io-name ()
1014 (concat "*input/output of "
1015 (gdb-get-target-string)
1016 "*"))
1017
1018 (defun gdb-display-separate-io-buffer ()
1019 "Display IO of debugged program in a separate window."
1020 (interactive)
1021 (if gdb-use-separate-io-buffer
1022 (gdb-display-buffer
1023 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1024
1025 (defconst gdb-frame-parameters
1026 '((height . 14) (width . 80)
1027 (unsplittable . t)
1028 (tool-bar-lines . nil)
1029 (menu-bar-lines . nil)
1030 (minibuffer . nil)))
1031
1032 (defun gdb-frame-separate-io-buffer ()
1033 "Display IO of debugged program in a new frame."
1034 (interactive)
1035 (if gdb-use-separate-io-buffer
1036 (let ((special-display-regexps (append special-display-regexps '(".*")))
1037 (special-display-frame-alist gdb-frame-parameters))
1038 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1039
1040 (defvar gdb-inferior-io-mode-map
1041 (let ((map (make-sparse-keymap)))
1042 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1043 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1044 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1045 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1046 (define-key map "\C-d" 'gdb-separate-io-eof)
1047 map))
1048
1049 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1050 "Major mode for gdb inferior-io."
1051 :syntax-table nil :abbrev-table nil
1052 ;; We want to use comint because it has various nifty and familiar
1053 ;; features. We don't need a process, but comint wants one, so create
1054 ;; a dummy one.
1055 (make-comint-in-buffer
1056 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1057 (current-buffer) "hexl")
1058 (setq comint-input-sender 'gdb-inferior-io-sender))
1059
1060 (defun gdb-inferior-io-sender (proc string)
1061 ;; PROC is the pseudo-process created to satisfy comint.
1062 (with-current-buffer (process-buffer proc)
1063 (setq proc (get-buffer-process gud-comint-buffer))
1064 (process-send-string proc string)
1065 (process-send-string proc "\n")))
1066
1067 (defun gdb-separate-io-interrupt ()
1068 "Interrupt the program being debugged."
1069 (interactive)
1070 (interrupt-process
1071 (get-buffer-process gud-comint-buffer) comint-ptyp))
1072
1073 (defun gdb-separate-io-quit ()
1074 "Send quit signal to the program being debugged."
1075 (interactive)
1076 (quit-process
1077 (get-buffer-process gud-comint-buffer) comint-ptyp))
1078
1079 (defun gdb-separate-io-stop ()
1080 "Stop the program being debugged."
1081 (interactive)
1082 (stop-process
1083 (get-buffer-process gud-comint-buffer) comint-ptyp))
1084
1085 (defun gdb-separate-io-eof ()
1086 "Send end-of-file to the program being debugged."
1087 (interactive)
1088 (process-send-eof
1089 (get-buffer-process gud-comint-buffer)))
1090 \f
1091
1092 ;; gdb communications
1093 ;;
1094
1095 ;; INPUT: things sent to gdb
1096 ;;
1097 ;; The queues are lists. Each element is either a string (indicating user or
1098 ;; user-like input) or a list of the form:
1099 ;;
1100 ;; (INPUT-STRING HANDLER-FN)
1101 ;;
1102 ;; The handler function will be called from the partial-output buffer when the
1103 ;; command completes. This is the way to write commands which invoke gdb
1104 ;; commands autonomously.
1105 ;;
1106 ;; These lists are consumed tail first.
1107 ;;
1108
1109 (defun gdb-send (proc string)
1110 "A comint send filter for gdb.
1111 This filter may simply queue input for a later time."
1112 (with-current-buffer gud-comint-buffer
1113 (let ((inhibit-read-only t))
1114 (remove-text-properties (point-min) (point-max) '(face))))
1115 (if gud-running
1116 (progn
1117 (let ((item (concat string "\n")))
1118 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1119 (process-send-string proc item)))
1120 (if (and (string-match "\\\\$" string)
1121 (not comint-input-sender-no-newline)) ;;Try to catch C-d.
1122 (setq gdb-continuation (concat gdb-continuation string "\n"))
1123 (let ((item (concat gdb-continuation string "\n")))
1124 (gdb-enqueue-input item)
1125 (setq gdb-continuation nil)))))
1126
1127 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1128 ;; is a query, or other non-top-level prompt.
1129
1130 (defun gdb-enqueue-input (item)
1131 (if (not gud-running)
1132 (if gdb-prompting
1133 (progn
1134 (gdb-send-item item)
1135 (setq gdb-prompting nil))
1136 (push item gdb-input-queue))))
1137
1138 (defun gdb-dequeue-input ()
1139 (let ((queue gdb-input-queue))
1140 (and queue
1141 (let ((last (car (last queue))))
1142 (unless (nbutlast queue) (setq gdb-input-queue '()))
1143 last))))
1144
1145 (defun gdb-send-item (item)
1146 (setq gdb-flush-pending-output nil)
1147 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1148 (setq gdb-current-item item)
1149 (let ((process (get-buffer-process gud-comint-buffer)))
1150 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1151 (if (stringp item)
1152 (progn
1153 (setq gdb-output-sink 'user)
1154 (process-send-string process item))
1155 (progn
1156 (gdb-clear-partial-output)
1157 (setq gdb-output-sink 'pre-emacs)
1158 (process-send-string process
1159 (car item))))
1160 ;; case: eq gud-minor-mode 'gdbmi
1161 (gdb-clear-partial-output)
1162 (setq gdb-output-sink 'emacs)
1163 (process-send-string process (car item)))))
1164 \f
1165 ;;
1166 ;; output -- things gdb prints to emacs
1167 ;;
1168 ;; GDB output is a stream interrupted by annotations.
1169 ;; Annotations can be recognized by their beginning
1170 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1171 ;;
1172 ;; The tag is a string obeying symbol syntax.
1173 ;;
1174 ;; The optional part `<opt>' can be either the empty string
1175 ;; or a space followed by more data relating to the annotation.
1176 ;; For example, the SOURCE annotation is followed by a filename,
1177 ;; line number and various useless goo. This data must not include
1178 ;; any newlines.
1179 ;;
1180
1181 (defcustom gud-gdba-command-name "gdb -annotate=3"
1182 "Default command to execute an executable under the GDB-UI debugger."
1183 :type 'string
1184 :group 'gud
1185 :version "22.1")
1186
1187 (defvar gdb-annotation-rules
1188 '(("pre-prompt" gdb-pre-prompt)
1189 ("prompt" gdb-prompt)
1190 ("commands" gdb-subprompt)
1191 ("overload-choice" gdb-subprompt)
1192 ("query" gdb-subprompt)
1193 ;; Need this prompt for GDB 6.1
1194 ("nquery" gdb-subprompt)
1195 ("prompt-for-continue" gdb-subprompt)
1196 ("post-prompt" gdb-post-prompt)
1197 ("source" gdb-source)
1198 ("starting" gdb-starting)
1199 ("exited" gdb-exited)
1200 ("signalled" gdb-signalled)
1201 ("signal" gdb-signal)
1202 ("breakpoint" gdb-stopping)
1203 ("watchpoint" gdb-stopping)
1204 ("frame-begin" gdb-frame-begin)
1205 ("stopped" gdb-stopped)
1206 ("error-begin" gdb-error)
1207 ("error" gdb-error)
1208 ) "An assoc mapping annotation tags to functions which process them.")
1209
1210 (defun gdb-resync()
1211 (setq gdb-flush-pending-output t)
1212 (setq gud-running nil)
1213 (gdb-force-mode-line-update
1214 (propertize "stopped"'face font-lock-warning-face))
1215 (setq gdb-output-sink 'user)
1216 (setq gdb-input-queue nil)
1217 (setq gdb-pending-triggers nil)
1218 (setq gdb-prompting t))
1219
1220 (defconst gdb-source-spec-regexp
1221 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1222
1223 ;; Do not use this except as an annotation handler.
1224 (defun gdb-source (args)
1225 (string-match gdb-source-spec-regexp args)
1226 ;; Extract the frame position from the marker.
1227 (setq gud-last-frame
1228 (cons
1229 (match-string 1 args)
1230 (string-to-number (match-string 2 args))))
1231 (setq gdb-pc-address (match-string 3 args))
1232 ;; cover for auto-display output which comes *before*
1233 ;; stopped annotation
1234 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1235
1236 (defun gdb-pre-prompt (ignored)
1237 "An annotation handler for `pre-prompt'.
1238 This terminates the collection of output from a previous command if that
1239 happens to be in effect."
1240 (setq gdb-error nil)
1241 (let ((sink gdb-output-sink))
1242 (cond
1243 ((eq sink 'user) t)
1244 ((eq sink 'emacs)
1245 (setq gdb-output-sink 'post-emacs))
1246 (t
1247 (gdb-resync)
1248 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1249
1250 (defun gdb-prompt (ignored)
1251 "An annotation handler for `prompt'.
1252 This sends the next command (if any) to gdb."
1253 (when gdb-first-prompt
1254 (gdb-force-mode-line-update
1255 (propertize "initializing..." 'face font-lock-variable-name-face))
1256 (gdb-init-1)
1257 (setq gdb-first-prompt nil))
1258 (let ((sink gdb-output-sink))
1259 (cond
1260 ((eq sink 'user) t)
1261 ((eq sink 'post-emacs)
1262 (setq gdb-output-sink 'user)
1263 (let ((handler
1264 (car (cdr gdb-current-item))))
1265 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1266 (funcall handler))))
1267 (t
1268 (gdb-resync)
1269 (error "Phase error in gdb-prompt (got %s)" sink))))
1270 (let ((input (gdb-dequeue-input)))
1271 (if input
1272 (gdb-send-item input)
1273 (progn
1274 (setq gdb-prompting t)
1275 (gud-display-frame)))))
1276
1277 (defun gdb-subprompt (ignored)
1278 "An annotation handler for non-top-level prompts."
1279 (setq gdb-prompting t))
1280
1281 (defun gdb-starting (ignored)
1282 "An annotation handler for `starting'.
1283 This says that I/O for the subprocess is now the program being debugged,
1284 not GDB."
1285 (setq gdb-active-process t)
1286 (setq gdb-printing t)
1287 (let ((sink gdb-output-sink))
1288 (cond
1289 ((eq sink 'user)
1290 (progn
1291 (setq gud-running t)
1292 (setq gdb-inferior-status "running")
1293 (gdb-force-mode-line-update
1294 (propertize gdb-inferior-status 'face font-lock-type-face))
1295 (gdb-remove-text-properties)
1296 (setq gud-old-arrow gud-overlay-arrow-position)
1297 (setq gud-overlay-arrow-position nil)
1298 (setq gdb-overlay-arrow-position nil)
1299 (setq gdb-stack-position nil)
1300 (if gdb-use-separate-io-buffer
1301 (setq gdb-output-sink 'inferior))))
1302 (t
1303 (gdb-resync)
1304 (error "Unexpected `starting' annotation")))))
1305
1306 (defun gdb-signal (ignored)
1307 (setq gdb-inferior-status "signal")
1308 (gdb-force-mode-line-update
1309 (propertize gdb-inferior-status 'face font-lock-warning-face))
1310 (gdb-stopping ignored))
1311
1312 (defun gdb-stopping (ignored)
1313 "An annotation handler for `breakpoint' and other annotations.
1314 They say that I/O for the subprocess is now GDB, not the program
1315 being debugged."
1316 (if gdb-use-separate-io-buffer
1317 (let ((sink gdb-output-sink))
1318 (cond
1319 ((eq sink 'inferior)
1320 (setq gdb-output-sink 'user))
1321 (t
1322 (gdb-resync)
1323 (error "Unexpected stopping annotation"))))))
1324
1325 (defun gdb-exited (ignored)
1326 "An annotation handler for `exited' and `signalled'.
1327 They say that I/O for the subprocess is now GDB, not the program
1328 being debugged and that the program is no longer running. This
1329 function is used to change the focus of GUD tooltips to #define
1330 directives."
1331 (setq gdb-active-process nil)
1332 (setq gud-overlay-arrow-position nil)
1333 (setq gdb-overlay-arrow-position nil)
1334 (setq gdb-stack-position nil)
1335 (setq gud-old-arrow nil)
1336 (setq gdb-inferior-status "exited")
1337 (gdb-force-mode-line-update
1338 (propertize gdb-inferior-status 'face font-lock-warning-face))
1339 (gdb-stopping ignored))
1340
1341 (defun gdb-signalled (ignored)
1342 (setq gdb-signalled t))
1343
1344 (defun gdb-frame-begin (ignored)
1345 (setq gdb-frame-begin t)
1346 (setq gdb-printing nil)
1347 (let ((sink gdb-output-sink))
1348 (cond
1349 ((eq sink 'inferior)
1350 (setq gdb-output-sink 'user))
1351 ((eq sink 'user) t)
1352 ((eq sink 'emacs) t)
1353 (t
1354 (gdb-resync)
1355 (error "Unexpected frame-begin annotation (%S)" sink)))))
1356
1357 (defcustom gdb-same-frame focus-follows-mouse
1358 "Non-nil means pop up GUD buffer in same frame."
1359 :group 'gud
1360 :type 'boolean
1361 :version "22.1")
1362
1363 (defcustom gdb-find-source-frame nil
1364 "Non-nil means try to find a source frame further up stack e.g after signal."
1365 :group 'gud
1366 :type 'boolean
1367 :version "22.1")
1368
1369 (defun gdb-find-source-frame (arg)
1370 "Toggle trying to find a source frame further up stack.
1371 With arg, look for a source frame further up stack iff arg is positive."
1372 (interactive "P")
1373 (setq gdb-find-source-frame
1374 (if (null arg)
1375 (not gdb-find-source-frame)
1376 (> (prefix-numeric-value arg) 0)))
1377 (message (format "Looking for source frame %sabled"
1378 (if gdb-find-source-frame "en" "dis"))))
1379
1380 (defun gdb-stopped (ignored)
1381 "An annotation handler for `stopped'.
1382 It is just like `gdb-stopping', except that if we already set the output
1383 sink to `user' in `gdb-stopping', that is fine."
1384 (setq gud-running nil)
1385 (unless (or gud-overlay-arrow-position gud-last-frame)
1386 (if (and gdb-frame-begin gdb-printing)
1387 (setq gud-overlay-arrow-position gud-old-arrow)
1388 ;;Pop up GUD buffer to display current frame when it doesn't have source
1389 ;;information i.e id not compiled with -g as with libc routines generally.
1390 (if gdb-same-frame
1391 (gdb-display-gdb-buffer)
1392 (gdb-frame-gdb-buffer))
1393 (if gdb-find-source-frame
1394 ;;Try to find source further up stack e.g after signal.
1395 (setq gdb-look-up-stack
1396 (if (gdb-get-buffer 'gdb-stack-buffer)
1397 'keep
1398 (progn
1399 (gdb-get-buffer-create 'gdb-stack-buffer)
1400 (gdb-invalidate-frames)
1401 'delete))))))
1402 (unless (member gdb-inferior-status '("exited" "signal"))
1403 (setq gdb-inferior-status "stopped")
1404 (gdb-force-mode-line-update
1405 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1406 (let ((sink gdb-output-sink))
1407 (cond
1408 ((eq sink 'inferior)
1409 (setq gdb-output-sink 'user))
1410 ((eq sink 'user) t)
1411 (t
1412 (gdb-resync)
1413 (error "Unexpected stopped annotation"))))
1414 (if gdb-signalled (gdb-exited ignored)))
1415
1416 (defun gdb-error (ignored)
1417 (setq gdb-error (not gdb-error)))
1418
1419 (defun gdb-post-prompt (ignored)
1420 "An annotation handler for `post-prompt'.
1421 This begins the collection of output from the current command if that
1422 happens to be appropriate."
1423 ;; Don't add to queue if there outstanding items or gdb-version is not known
1424 ;; yet.
1425 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1426 (gdb-get-selected-frame)
1427 (gdb-invalidate-frames)
1428 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1429 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1430 (gdb-invalidate-breakpoints)
1431 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1432 ;; so gdb-pc-address is updated.
1433 ;; (gdb-invalidate-assembler)
1434
1435 (if (string-equal gdb-version "pre-6.4")
1436 (gdb-invalidate-registers)
1437 (gdb-get-changed-registers)
1438 (gdb-invalidate-registers-1))
1439
1440 (gdb-invalidate-memory)
1441 (if (string-equal gdb-version "pre-6.4")
1442 (gdb-invalidate-locals)
1443 (gdb-invalidate-locals-1))
1444
1445 (gdb-invalidate-threads)
1446 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1447 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1448 ;; Only needed/used with speedbar/watch expressions.
1449 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1450 (if (string-equal gdb-version "pre-6.4")
1451 (gdb-var-update)
1452 (gdb-var-update-1)))))
1453 (setq gdb-first-post-prompt nil)
1454 (let ((sink gdb-output-sink))
1455 (cond
1456 ((eq sink 'user) t)
1457 ((eq sink 'pre-emacs)
1458 (setq gdb-output-sink 'emacs))
1459 (t
1460 (gdb-resync)
1461 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1462
1463 (defconst gdb-buffer-list
1464 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1465
1466 (defun gdb-remove-text-properties ()
1467 (dolist (buffertype gdb-buffer-list)
1468 (let ((buffer (gdb-get-buffer buffertype)))
1469 (if buffer
1470 (with-current-buffer buffer
1471 (let ((inhibit-read-only t))
1472 (remove-text-properties
1473 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1474
1475 ;; GUD displays the selected GDB frame. This might might not be the current
1476 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1477 ;; visited breakpoint is, use that window.
1478 (defun gdb-display-source-buffer (buffer)
1479 (let* ((last-window (if gud-last-last-frame
1480 (get-buffer-window
1481 (gud-find-file (car gud-last-last-frame)))))
1482 (source-window (or last-window
1483 (if (and gdb-source-window
1484 (window-live-p gdb-source-window))
1485 gdb-source-window))))
1486 (when source-window
1487 (setq gdb-source-window source-window)
1488 (set-window-buffer source-window buffer))
1489 source-window))
1490
1491 (defun gud-gdba-marker-filter (string)
1492 "A gud marker filter for gdb. Handle a burst of output from GDB."
1493 (if gdb-flush-pending-output
1494 nil
1495 (when gdb-enable-debug
1496 (push (cons 'recv string) gdb-debug-ring)
1497 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1498 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1499 ;; Recall the left over gud-marker-acc from last time.
1500 (setq gud-marker-acc (concat gud-marker-acc string))
1501 ;; Start accumulating output for the GUD buffer.
1502 (let ((output ""))
1503 ;;
1504 ;; Process all the complete markers in this chunk.
1505 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1506 (let ((annotation (match-string 1 gud-marker-acc)))
1507 ;;
1508 ;; Stuff prior to the match is just ordinary output.
1509 ;; It is either concatenated to OUTPUT or directed
1510 ;; elsewhere.
1511 (setq output
1512 (gdb-concat-output
1513 output
1514 (substring gud-marker-acc 0 (match-beginning 0))))
1515 ;;
1516 ;; Take that stuff off the gud-marker-acc.
1517 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1518 ;;
1519 ;; Parse the tag from the annotation, and maybe its arguments.
1520 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1521 (let* ((annotation-type (match-string 1 annotation))
1522 (annotation-arguments (match-string 2 annotation))
1523 (annotation-rule (assoc annotation-type
1524 gdb-annotation-rules)))
1525 ;; Call the handler for this annotation.
1526 (if annotation-rule
1527 (funcall (car (cdr annotation-rule))
1528 annotation-arguments)
1529 ;; Else the annotation is not recognized. Ignore it silently,
1530 ;; so that GDB can add new annotations without causing
1531 ;; us to blow up.
1532 ))))
1533 ;;
1534 ;; Does the remaining text end in a partial line?
1535 ;; If it does, then keep part of the gud-marker-acc until we get more.
1536 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1537 gud-marker-acc)
1538 (progn
1539 ;; Everything before the potential marker start can be output.
1540 (setq output
1541 (gdb-concat-output output
1542 (substring gud-marker-acc 0
1543 (match-beginning 0))))
1544 ;;
1545 ;; Everything after, we save, to combine with later input.
1546 (setq gud-marker-acc (substring gud-marker-acc
1547 (match-beginning 0))))
1548 ;;
1549 ;; In case we know the gud-marker-acc contains no partial annotations:
1550 (progn
1551 (setq output (gdb-concat-output output gud-marker-acc))
1552 (setq gud-marker-acc "")))
1553 output)))
1554
1555 (defun gdb-concat-output (so-far new)
1556 (if gdb-error
1557 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1558 (let ((sink gdb-output-sink))
1559 (cond
1560 ((eq sink 'user) (concat so-far new))
1561 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1562 ((eq sink 'emacs)
1563 (gdb-append-to-partial-output new)
1564 so-far)
1565 ((eq sink 'inferior)
1566 (gdb-append-to-inferior-io new)
1567 so-far)
1568 (t
1569 (gdb-resync)
1570 (error "Bogon output sink %S" sink)))))
1571
1572 (defun gdb-append-to-partial-output (string)
1573 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1574 (goto-char (point-max))
1575 (insert string)))
1576
1577 (defun gdb-clear-partial-output ()
1578 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1579 (erase-buffer)))
1580
1581 (defun gdb-append-to-inferior-io (string)
1582 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1583 (goto-char (point-max))
1584 (insert-before-markers string))
1585 (if (not (string-equal string ""))
1586 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1587
1588 (defun gdb-clear-inferior-io ()
1589 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1590 (erase-buffer)))
1591 \f
1592
1593 ;; One trick is to have a command who's output is always available in a buffer
1594 ;; of it's own, and is always up to date. We build several buffers of this
1595 ;; type.
1596 ;;
1597 ;; There are two aspects to this: gdb has to tell us when the output for that
1598 ;; command might have changed, and we have to be able to run the command
1599 ;; behind the user's back.
1600 ;;
1601 ;; The output phasing associated with the variable gdb-output-sink
1602 ;; help us to run commands behind the user's back.
1603 ;;
1604 ;; Below is the code for specificly managing buffers of output from one
1605 ;; command.
1606 ;;
1607
1608 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1609 ;; It adds an input for the command we are tracking. It should be the
1610 ;; annotation rule binding of whatever gdb sends to tell us this command
1611 ;; might have changed it's output.
1612 ;;
1613 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1614 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1615 ;; input in the input queue (see comment about ``gdb communications'' above).
1616
1617 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1618 output-handler)
1619 `(defun ,name (&optional ignored)
1620 (if (and ,demand-predicate
1621 (not (member ',name
1622 gdb-pending-triggers)))
1623 (progn
1624 (gdb-enqueue-input
1625 (list ,gdb-command ',output-handler))
1626 (push ',name gdb-pending-triggers)))))
1627
1628 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1629 `(defun ,name ()
1630 (setq gdb-pending-triggers
1631 (delq ',trigger
1632 gdb-pending-triggers))
1633 (let ((buf (gdb-get-buffer ',buf-key)))
1634 (and buf
1635 (with-current-buffer buf
1636 (let* ((window (get-buffer-window buf 0))
1637 (start (window-start window))
1638 (p (window-point window))
1639 (buffer-read-only nil))
1640 (erase-buffer)
1641 (insert-buffer-substring (gdb-get-buffer-create
1642 'gdb-partial-output-buffer))
1643 (set-window-start window start)
1644 (set-window-point window p)))))
1645 ;; put customisation here
1646 (,custom-defun)))
1647
1648 (defmacro def-gdb-auto-updated-buffer (buffer-key
1649 trigger-name gdb-command
1650 output-handler-name custom-defun)
1651 `(progn
1652 (def-gdb-auto-update-trigger ,trigger-name
1653 ;; The demand predicate:
1654 (gdb-get-buffer ',buffer-key)
1655 ,gdb-command
1656 ,output-handler-name)
1657 (def-gdb-auto-update-handler ,output-handler-name
1658 ,trigger-name ,buffer-key ,custom-defun)))
1659
1660 \f
1661 ;;
1662 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1663 ;;
1664 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1665 'gdb-breakpoints-buffer-name
1666 'gdb-breakpoints-mode)
1667
1668 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1669 ;; This defines the auto update rule for buffers of type
1670 ;; `gdb-breakpoints-buffer'.
1671 ;;
1672 ;; It defines a function to serve as the annotation handler that
1673 ;; handles the `foo-invalidated' message. That function is called:
1674 gdb-invalidate-breakpoints
1675 ;;
1676 ;; To update the buffer, this command is sent to gdb.
1677 "server info breakpoints\n"
1678 ;;
1679 ;; This also defines a function to be the handler for the output
1680 ;; from the command above. That function will copy the output into
1681 ;; the appropriately typed buffer. That function will be called:
1682 gdb-info-breakpoints-handler
1683 ;; buffer specific functions
1684 gdb-info-breakpoints-custom)
1685
1686 (defconst breakpoint-xpm-data
1687 "/* XPM */
1688 static char *magick[] = {
1689 /* columns rows colors chars-per-pixel */
1690 \"10 10 2 1\",
1691 \" c red\",
1692 \"+ c None\",
1693 /* pixels */
1694 \"+++ +++\",
1695 \"++ ++\",
1696 \"+ +\",
1697 \" \",
1698 \" \",
1699 \" \",
1700 \" \",
1701 \"+ +\",
1702 \"++ ++\",
1703 \"+++ +++\",
1704 };"
1705 "XPM data used for breakpoint icon.")
1706
1707 (defconst breakpoint-enabled-pbm-data
1708 "P1
1709 10 10\",
1710 0 0 0 0 1 1 1 1 0 0 0 0
1711 0 0 0 1 1 1 1 1 1 0 0 0
1712 0 0 1 1 1 1 1 1 1 1 0 0
1713 0 1 1 1 1 1 1 1 1 1 1 0
1714 0 1 1 1 1 1 1 1 1 1 1 0
1715 0 1 1 1 1 1 1 1 1 1 1 0
1716 0 1 1 1 1 1 1 1 1 1 1 0
1717 0 0 1 1 1 1 1 1 1 1 0 0
1718 0 0 0 1 1 1 1 1 1 0 0 0
1719 0 0 0 0 1 1 1 1 0 0 0 0"
1720 "PBM data used for enabled breakpoint icon.")
1721
1722 (defconst breakpoint-disabled-pbm-data
1723 "P1
1724 10 10\",
1725 0 0 1 0 1 0 1 0 0 0
1726 0 1 0 1 0 1 0 1 0 0
1727 1 0 1 0 1 0 1 0 1 0
1728 0 1 0 1 0 1 0 1 0 1
1729 1 0 1 0 1 0 1 0 1 0
1730 0 1 0 1 0 1 0 1 0 1
1731 1 0 1 0 1 0 1 0 1 0
1732 0 1 0 1 0 1 0 1 0 1
1733 0 0 1 0 1 0 1 0 1 0
1734 0 0 0 1 0 1 0 1 0 0"
1735 "PBM data used for disabled breakpoint icon.")
1736
1737 (defvar breakpoint-enabled-icon nil
1738 "Icon for enabled breakpoint in display margin.")
1739
1740 (defvar breakpoint-disabled-icon nil
1741 "Icon for disabled breakpoint in display margin.")
1742
1743 (and (display-images-p)
1744 ;; Bitmap for breakpoint in fringe
1745 (define-fringe-bitmap 'breakpoint
1746 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1747 ;; Bitmap for gud-overlay-arrow in fringe
1748 (define-fringe-bitmap 'hollow-right-triangle
1749 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1750
1751 (defface breakpoint-enabled
1752 '((t
1753 :foreground "red"
1754 :weight bold))
1755 "Face for enabled breakpoint icon in fringe."
1756 :group 'gud)
1757
1758 (defface breakpoint-disabled
1759 ;; We use different values of grey for different background types,
1760 ;; so that on low-color displays it will end up as something visible
1761 ;; if it has to be approximated.
1762 '((t :foreground "grey70"))
1763 "Face for disabled breakpoint icon in fringe."
1764 :group 'gud)
1765
1766 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1767 (defun gdb-info-breakpoints-custom ()
1768 (let ((flag) (bptno))
1769 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1770 (dolist (buffer (buffer-list))
1771 (with-current-buffer buffer
1772 (if (and (memq gud-minor-mode '(gdba gdbmi))
1773 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1774 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1775 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1776 (save-excursion
1777 (let ((buffer-read-only nil))
1778 (goto-char (point-min))
1779 (while (< (point) (- (point-max) 1))
1780 (forward-line 1)
1781 (if (looking-at gdb-breakpoint-regexp)
1782 (progn
1783 (setq bptno (match-string 1))
1784 (setq flag (char-after (match-beginning 2)))
1785 (add-text-properties
1786 (match-beginning 2) (match-end 2)
1787 (if (eq flag ?y)
1788 '(face font-lock-warning-face)
1789 '(face font-lock-type-face)))
1790 (let ((bl (point))
1791 (el (line-end-position)))
1792 (if (re-search-forward " in \\(.*\\) at\\s-+" el t)
1793 (progn
1794 (add-text-properties
1795 (match-beginning 1) (match-end 1)
1796 '(face font-lock-function-name-face))
1797 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1798 (let ((line (match-string 2))
1799 (file (match-string 1)))
1800 (add-text-properties bl el
1801 '(mouse-face highlight
1802 help-echo "mouse-2, RET: visit breakpoint"))
1803 (unless (file-exists-p file)
1804 (setq file (cdr (assoc bptno gdb-location-alist))))
1805 (if (and file
1806 (not (string-equal file "File not found")))
1807 (with-current-buffer
1808 (find-file-noselect file 'nowarn)
1809 (set (make-local-variable 'gud-minor-mode)
1810 'gdba)
1811 (set (make-local-variable 'tool-bar-map)
1812 gud-tool-bar-map)
1813 ;; Only want one breakpoint icon at each
1814 ;; location.
1815 (save-excursion
1816 (goto-line (string-to-number line))
1817 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1818 (gdb-enqueue-input
1819 (list
1820 (concat gdb-server-prefix "list "
1821 (match-string-no-properties 1) ":1\n")
1822 'ignore))
1823 (gdb-enqueue-input
1824 (list (concat gdb-server-prefix "info source\n")
1825 `(lambda () (gdb-get-location
1826 ,bptno ,line ,flag)))))))
1827 (if (re-search-forward
1828 "<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1829 el t)
1830 (add-text-properties
1831 (match-beginning 1) (match-end 1)
1832 '(face font-lock-function-name-face))
1833 (end-of-line)
1834 (re-search-backward "\\s-\\(\\S-*\\)"
1835 bl t)
1836 (add-text-properties
1837 (match-beginning 1) (match-end 1)
1838 '(face font-lock-variable-name-face)))))))
1839 (end-of-line))))))
1840 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1841
1842 (defun gdb-mouse-set-clear-breakpoint (event)
1843 "Set/clear breakpoint in left fringe/margin with mouse click."
1844 (interactive "e")
1845 (mouse-minibuffer-check event)
1846 (let ((posn (event-end event)))
1847 (if (numberp (posn-point posn))
1848 (with-selected-window (posn-window posn)
1849 (save-excursion
1850 (goto-char (posn-point posn))
1851 (if (or (posn-object posn)
1852 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1853 'breakpoint))
1854 (gud-remove nil)
1855 (gud-break nil)))))))
1856
1857 (defun gdb-mouse-toggle-breakpoint-margin (event)
1858 "Enable/disable breakpoint in left margin with mouse click."
1859 (interactive "e")
1860 (mouse-minibuffer-check event)
1861 (let ((posn (event-end event)))
1862 (if (numberp (posn-point posn))
1863 (with-selected-window (posn-window posn)
1864 (save-excursion
1865 (goto-char (posn-point posn))
1866 (if (posn-object posn)
1867 (gdb-enqueue-input
1868 (list
1869 (let ((bptno (get-text-property
1870 0 'gdb-bptno (car (posn-string posn)))))
1871 (concat gdb-server-prefix
1872 (if (get-text-property
1873 0 'gdb-enabled (car (posn-string posn)))
1874 "disable "
1875 "enable ")
1876 bptno "\n"))
1877 'ignore))))))))
1878
1879 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1880 "Enable/disable breakpoint in left fringe with mouse click."
1881 (interactive "e")
1882 (mouse-minibuffer-check event)
1883 (let* ((posn (event-end event))
1884 (pos (posn-point posn))
1885 obj)
1886 (when (numberp pos)
1887 (with-selected-window (posn-window posn)
1888 (save-excursion
1889 (set-buffer (window-buffer (selected-window)))
1890 (goto-char pos)
1891 (dolist (overlay (overlays-in pos pos))
1892 (when (overlay-get overlay 'put-break)
1893 (setq obj (overlay-get overlay 'before-string))))
1894 (when (stringp obj)
1895 (gdb-enqueue-input
1896 (list
1897 (concat gdb-server-prefix
1898 (if (get-text-property 0 'gdb-enabled obj)
1899 "disable "
1900 "enable ")
1901 (get-text-property 0 'gdb-bptno obj) "\n")
1902 'ignore))))))))
1903
1904 (defun gdb-breakpoints-buffer-name ()
1905 (with-current-buffer gud-comint-buffer
1906 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1907
1908 (defun gdb-display-breakpoints-buffer ()
1909 "Display status of user-settable breakpoints."
1910 (interactive)
1911 (gdb-display-buffer
1912 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
1913
1914 (defun gdb-frame-breakpoints-buffer ()
1915 "Display status of user-settable breakpoints in a new frame."
1916 (interactive)
1917 (let ((special-display-regexps (append special-display-regexps '(".*")))
1918 (special-display-frame-alist gdb-frame-parameters))
1919 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1920
1921 (defvar gdb-breakpoints-mode-map
1922 (let ((map (make-sparse-keymap))
1923 (menu (make-sparse-keymap "Breakpoints")))
1924 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1925 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1926 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1927 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1928 (suppress-keymap map)
1929 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1930 (define-key map " " 'gdb-toggle-breakpoint)
1931 (define-key map "D" 'gdb-delete-breakpoint)
1932 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1933 (define-key map "q" 'gdb-delete-frame-or-window)
1934 (define-key map "\r" 'gdb-goto-breakpoint)
1935 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1936 (define-key map [follow-link] 'mouse-face)
1937 map))
1938
1939 (defun gdb-delete-frame-or-window ()
1940 "Delete frame if there is only one window. Otherwise delete the window."
1941 (interactive)
1942 (if (one-window-p) (delete-frame)
1943 (delete-window)))
1944
1945 (defun gdb-breakpoints-mode ()
1946 "Major mode for gdb breakpoints.
1947
1948 \\{gdb-breakpoints-mode-map}"
1949 (kill-all-local-variables)
1950 (setq major-mode 'gdb-breakpoints-mode)
1951 (setq mode-name "Breakpoints")
1952 (use-local-map gdb-breakpoints-mode-map)
1953 (setq buffer-read-only t)
1954 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1955 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1956 'gdb-invalidate-breakpoints
1957 'gdbmi-invalidate-breakpoints))
1958
1959 (defconst gdb-breakpoint-regexp
1960 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1961
1962 (defun gdb-toggle-breakpoint ()
1963 "Enable/disable breakpoint at current line."
1964 (interactive)
1965 (save-excursion
1966 (beginning-of-line 1)
1967 (if (looking-at gdb-breakpoint-regexp)
1968 (gdb-enqueue-input
1969 (list
1970 (concat gdb-server-prefix
1971 (if (eq ?y (char-after (match-beginning 2)))
1972 "disable "
1973 "enable ")
1974 (match-string 1) "\n") 'ignore))
1975 (error "Not recognized as break/watchpoint line"))))
1976
1977 (defun gdb-delete-breakpoint ()
1978 "Delete the breakpoint at current line."
1979 (interactive)
1980 (beginning-of-line 1)
1981 (if (looking-at gdb-breakpoint-regexp)
1982 (gdb-enqueue-input
1983 (list
1984 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1985 (error "Not recognized as break/watchpoint line")))
1986
1987 (defun gdb-goto-breakpoint (&optional event)
1988 "Display the breakpoint location specified at current line."
1989 (interactive (list last-input-event))
1990 (if event (posn-set-point (event-end event)))
1991 (save-excursion
1992 (beginning-of-line 1)
1993 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1994 (let ((bptno (match-string 1))
1995 (file (match-string 2))
1996 (line (match-string 3)))
1997 (save-selected-window
1998 (let* ((buffer (find-file-noselect
1999 (if (file-exists-p file) file
2000 (cdr (assoc bptno gdb-location-alist)))))
2001 (window (or (gdb-display-source-buffer buffer)
2002 (display-buffer buffer))))
2003 (setq gdb-source-window window)
2004 (with-current-buffer buffer
2005 (goto-line (string-to-number line))
2006 (set-window-point window (point))))))
2007 (error "No location specified."))))
2008 \f
2009
2010 ;; Frames buffer. This displays a perpetually correct bactracktrace
2011 ;; (from the command `where').
2012 ;;
2013 ;; Alas, if your stack is deep, it is costly.
2014 ;;
2015 (gdb-set-buffer-rules 'gdb-stack-buffer
2016 'gdb-stack-buffer-name
2017 'gdb-frames-mode)
2018
2019 (def-gdb-auto-updated-buffer gdb-stack-buffer
2020 gdb-invalidate-frames
2021 "server info stack\n"
2022 gdb-info-stack-handler
2023 gdb-info-stack-custom)
2024
2025 (defun gdb-info-stack-custom ()
2026 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2027 (save-excursion
2028 (unless (eq gdb-look-up-stack 'delete)
2029 (let ((buffer-read-only nil)
2030 bl el)
2031 (goto-char (point-min))
2032 (while (< (point) (point-max))
2033 (setq bl (line-beginning-position)
2034 el (line-end-position))
2035 (when (looking-at "#")
2036 (add-text-properties bl el
2037 '(mouse-face highlight
2038 help-echo "mouse-2, RET: Select frame")))
2039 (goto-char bl)
2040 (when (looking-at "^#\\([0-9]+\\)")
2041 (when (string-equal (match-string 1) gdb-frame-number)
2042 (if (> (car (window-fringes)) 0)
2043 (progn
2044 (or gdb-stack-position
2045 (setq gdb-stack-position (make-marker)))
2046 (set-marker gdb-stack-position (point)))
2047 (set-marker gdb-stack-position nil)
2048 (put-text-property bl (+ bl 4)
2049 'face '(:inverse-video t))))
2050 (when (re-search-forward
2051 (concat
2052 (if (string-equal (match-string 1) "0") "" " in ")
2053 "\\([^ ]+\\) (") el t)
2054 (put-text-property (match-beginning 1) (match-end 1)
2055 'face font-lock-function-name-face)
2056 (setq bl (match-end 0))
2057 (while (re-search-forward "<\\([^>]+\\)>" el t)
2058 (put-text-property (match-beginning 1) (match-end 1)
2059 'face font-lock-function-name-face))
2060 (goto-char bl)
2061 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2062 (put-text-property (match-beginning 1) (match-end 1)
2063 'face font-lock-variable-name-face))))
2064 (forward-line 1))))
2065 (when gdb-look-up-stack
2066 (goto-char (point-min))
2067 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2068 (let ((start (line-beginning-position))
2069 (file (match-string 1))
2070 (line (match-string 2)))
2071 (re-search-backward "^#*\\([0-9]+\\)" start t)
2072 (gdb-enqueue-input
2073 (list (concat gdb-server-prefix "frame "
2074 (match-string 1) "\n") 'gdb-set-hollow))
2075 (gdb-enqueue-input
2076 (list (concat gdb-server-prefix "frame 0\n") 'ignore)))))))
2077 (if (eq gdb-look-up-stack 'delete)
2078 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2079 (setq gdb-look-up-stack nil))
2080
2081 (defun gdb-set-hollow ()
2082 (if gud-last-last-frame
2083 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2084 (setq fringe-indicator-alist
2085 '((overlay-arrow . hollow-right-triangle))))))
2086
2087 (defun gdb-stack-buffer-name ()
2088 (with-current-buffer gud-comint-buffer
2089 (concat "*stack frames of " (gdb-get-target-string) "*")))
2090
2091 (defun gdb-display-stack-buffer ()
2092 "Display backtrace of current stack."
2093 (interactive)
2094 (gdb-display-buffer
2095 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2096
2097 (defun gdb-frame-stack-buffer ()
2098 "Display backtrace of current stack in a new frame."
2099 (interactive)
2100 (let ((special-display-regexps (append special-display-regexps '(".*")))
2101 (special-display-frame-alist gdb-frame-parameters))
2102 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2103
2104 (defvar gdb-frames-mode-map
2105 (let ((map (make-sparse-keymap)))
2106 (suppress-keymap map)
2107 (define-key map "q" 'kill-this-buffer)
2108 (define-key map "\r" 'gdb-frames-select)
2109 (define-key map [mouse-2] 'gdb-frames-select)
2110 (define-key map [follow-link] 'mouse-face)
2111 map))
2112
2113 (defun gdb-frames-mode ()
2114 "Major mode for gdb call stack.
2115
2116 \\{gdb-frames-mode-map}"
2117 (kill-all-local-variables)
2118 (setq major-mode 'gdb-frames-mode)
2119 (setq mode-name "Frames")
2120 (setq gdb-stack-position nil)
2121 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2122 (setq buffer-read-only t)
2123 (use-local-map gdb-frames-mode-map)
2124 (run-mode-hooks 'gdb-frames-mode-hook)
2125 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2126 'gdb-invalidate-frames
2127 'gdbmi-invalidate-frames))
2128
2129 (defun gdb-get-frame-number ()
2130 (save-excursion
2131 (end-of-line)
2132 (let* ((start (line-beginning-position))
2133 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2134 (n (or (and pos (match-string-no-properties 1)) "0")))
2135 n)))
2136
2137 (defun gdb-frames-select (&optional event)
2138 "Select the frame and display the relevant source."
2139 (interactive (list last-input-event))
2140 (if event (posn-set-point (event-end event)))
2141 (gdb-enqueue-input
2142 (list (concat gdb-server-prefix "frame "
2143 (gdb-get-frame-number) "\n") 'ignore)))
2144 \f
2145
2146 ;; Threads buffer. This displays a selectable thread list.
2147 ;;
2148 (gdb-set-buffer-rules 'gdb-threads-buffer
2149 'gdb-threads-buffer-name
2150 'gdb-threads-mode)
2151
2152 (def-gdb-auto-updated-buffer gdb-threads-buffer
2153 gdb-invalidate-threads
2154 (concat gdb-server-prefix "info threads\n")
2155 gdb-info-threads-handler
2156 gdb-info-threads-custom)
2157
2158 (defun gdb-info-threads-custom ()
2159 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2160 (let ((buffer-read-only nil))
2161 (save-excursion
2162 (goto-char (point-min))
2163 (while (< (point) (point-max))
2164 (unless (looking-at "No ")
2165 (add-text-properties (line-beginning-position) (line-end-position)
2166 '(mouse-face highlight
2167 help-echo "mouse-2, RET: select thread")))
2168 (forward-line 1))))))
2169
2170 (defun gdb-threads-buffer-name ()
2171 (with-current-buffer gud-comint-buffer
2172 (concat "*threads of " (gdb-get-target-string) "*")))
2173
2174 (defun gdb-display-threads-buffer ()
2175 "Display IDs of currently known threads."
2176 (interactive)
2177 (gdb-display-buffer
2178 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2179
2180 (defun gdb-frame-threads-buffer ()
2181 "Display IDs of currently known threads in a new frame."
2182 (interactive)
2183 (let ((special-display-regexps (append special-display-regexps '(".*")))
2184 (special-display-frame-alist gdb-frame-parameters))
2185 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2186
2187 (defvar gdb-threads-mode-map
2188 (let ((map (make-sparse-keymap)))
2189 (suppress-keymap map)
2190 (define-key map "q" 'kill-this-buffer)
2191 (define-key map "\r" 'gdb-threads-select)
2192 (define-key map [mouse-2] 'gdb-threads-select)
2193 (define-key map [follow-link] 'mouse-face)
2194 map))
2195
2196 (defvar gdb-threads-font-lock-keywords
2197 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2198 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2199 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2200 "Font lock keywords used in `gdb-threads-mode'.")
2201
2202 (defun gdb-threads-mode ()
2203 "Major mode for gdb threads.
2204
2205 \\{gdb-threads-mode-map}"
2206 (kill-all-local-variables)
2207 (setq major-mode 'gdb-threads-mode)
2208 (setq mode-name "Threads")
2209 (setq buffer-read-only t)
2210 (use-local-map gdb-threads-mode-map)
2211 (set (make-local-variable 'font-lock-defaults)
2212 '(gdb-threads-font-lock-keywords))
2213 (run-mode-hooks 'gdb-threads-mode-hook)
2214 'gdb-invalidate-threads)
2215
2216 (defun gdb-get-thread-number ()
2217 (save-excursion
2218 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2219 (match-string-no-properties 1)))
2220
2221 (defun gdb-threads-select (&optional event)
2222 "Select the thread and display the relevant source."
2223 (interactive (list last-input-event))
2224 (if event (posn-set-point (event-end event)))
2225 (gdb-enqueue-input
2226 (list (concat gdb-server-prefix "thread "
2227 (gdb-get-thread-number) "\n") 'ignore))
2228 (gud-display-frame))
2229 \f
2230
2231 ;; Registers buffer.
2232 ;;
2233 (defcustom gdb-all-registers nil
2234 "Non-nil means include floating-point registers."
2235 :type 'boolean
2236 :group 'gud
2237 :version "22.1")
2238
2239 (gdb-set-buffer-rules 'gdb-registers-buffer
2240 'gdb-registers-buffer-name
2241 'gdb-registers-mode)
2242
2243 (def-gdb-auto-updated-buffer gdb-registers-buffer
2244 gdb-invalidate-registers
2245 (concat
2246 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2247 gdb-info-registers-handler
2248 gdb-info-registers-custom)
2249
2250 (defun gdb-info-registers-custom ()
2251 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2252 (save-excursion
2253 (let ((buffer-read-only nil)
2254 start end)
2255 (goto-char (point-min))
2256 (while (< (point) (point-max))
2257 (setq start (line-beginning-position))
2258 (setq end (line-end-position))
2259 (when (looking-at "^[^ ]+")
2260 (unless (string-equal (match-string 0) "The")
2261 (put-text-property start (match-end 0)
2262 'face font-lock-variable-name-face)
2263 (add-text-properties start end
2264 '(help-echo "mouse-2: edit value"
2265 mouse-face highlight))))
2266 (forward-line 1))))))
2267
2268 (defun gdb-edit-register-value (&optional event)
2269 (interactive (list last-input-event))
2270 (save-excursion
2271 (if event (posn-set-point (event-end event)))
2272 (beginning-of-line)
2273 (let* ((register (current-word))
2274 (value (read-string (format "New value (%s): " register))))
2275 (gdb-enqueue-input
2276 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2277 'ignore)))))
2278
2279 (defvar gdb-registers-mode-map
2280 (let ((map (make-sparse-keymap)))
2281 (suppress-keymap map)
2282 (define-key map "\r" 'gdb-edit-register-value)
2283 (define-key map [mouse-2] 'gdb-edit-register-value)
2284 (define-key map " " 'gdb-all-registers)
2285 (define-key map "q" 'kill-this-buffer)
2286 map))
2287
2288 (defun gdb-registers-mode ()
2289 "Major mode for gdb registers.
2290
2291 \\{gdb-registers-mode-map}"
2292 (kill-all-local-variables)
2293 (setq major-mode 'gdb-registers-mode)
2294 (setq mode-name "Registers")
2295 (setq buffer-read-only t)
2296 (use-local-map gdb-registers-mode-map)
2297 (run-mode-hooks 'gdb-registers-mode-hook)
2298 (if (string-equal gdb-version "pre-6.4")
2299 (progn
2300 (if gdb-all-registers (setq mode-name "Registers:All"))
2301 'gdb-invalidate-registers)
2302 'gdb-invalidate-registers-1))
2303
2304 (defun gdb-registers-buffer-name ()
2305 (with-current-buffer gud-comint-buffer
2306 (concat "*registers of " (gdb-get-target-string) "*")))
2307
2308 (defun gdb-display-registers-buffer ()
2309 "Display integer register contents."
2310 (interactive)
2311 (gdb-display-buffer
2312 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2313
2314 (defun gdb-frame-registers-buffer ()
2315 "Display integer register contents in a new frame."
2316 (interactive)
2317 (let ((special-display-regexps (append special-display-regexps '(".*")))
2318 (special-display-frame-alist gdb-frame-parameters))
2319 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2320
2321 (defun gdb-all-registers ()
2322 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2323 (interactive)
2324 (when (string-equal gdb-version "pre-6.4")
2325 (if gdb-all-registers
2326 (progn
2327 (setq gdb-all-registers nil)
2328 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2329 (setq mode-name "Registers")))
2330 (setq gdb-all-registers t)
2331 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2332 (setq mode-name "Registers:All")))
2333 (message (format "Display of floating-point registers %sabled"
2334 (if gdb-all-registers "en" "dis")))
2335 (gdb-invalidate-registers)))
2336 \f
2337
2338 ;; Memory buffer.
2339 ;;
2340 (defcustom gdb-memory-repeat-count 32
2341 "Number of data items in memory window."
2342 :type 'integer
2343 :group 'gud
2344 :version "22.1")
2345
2346 (defcustom gdb-memory-format "x"
2347 "Display format of data items in memory window."
2348 :type '(choice (const :tag "Hexadecimal" "x")
2349 (const :tag "Signed decimal" "d")
2350 (const :tag "Unsigned decimal" "u")
2351 (const :tag "Octal" "o")
2352 (const :tag "Binary" "t"))
2353 :group 'gud
2354 :version "22.1")
2355
2356 (defcustom gdb-memory-unit "w"
2357 "Unit size of data items in memory window."
2358 :type '(choice (const :tag "Byte" "b")
2359 (const :tag "Halfword" "h")
2360 (const :tag "Word" "w")
2361 (const :tag "Giant word" "g"))
2362 :group 'gud
2363 :version "22.1")
2364
2365 (gdb-set-buffer-rules 'gdb-memory-buffer
2366 'gdb-memory-buffer-name
2367 'gdb-memory-mode)
2368
2369 (def-gdb-auto-updated-buffer gdb-memory-buffer
2370 gdb-invalidate-memory
2371 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2372 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2373 gdb-read-memory-handler
2374 gdb-read-memory-custom)
2375
2376 (defun gdb-read-memory-custom ()
2377 (save-excursion
2378 (goto-char (point-min))
2379 (if (looking-at "0x[[:xdigit:]]+")
2380 (setq gdb-memory-address (match-string 0)))))
2381
2382 (defvar gdb-memory-mode-map
2383 (let ((map (make-sparse-keymap)))
2384 (suppress-keymap map)
2385 (define-key map "q" 'kill-this-buffer)
2386 map))
2387
2388 (defun gdb-memory-set-address (event)
2389 "Set the start memory address."
2390 (interactive "e")
2391 (save-selected-window
2392 (select-window (posn-window (event-start event)))
2393 (let ((arg (read-from-minibuffer "Memory address: ")))
2394 (setq gdb-memory-address arg))
2395 (gdb-invalidate-memory)))
2396
2397 (defun gdb-memory-set-repeat-count (event)
2398 "Set the number of data items in memory window."
2399 (interactive "e")
2400 (save-selected-window
2401 (select-window (posn-window (event-start event)))
2402 (let* ((arg (read-from-minibuffer "Repeat count: "))
2403 (count (string-to-number arg)))
2404 (if (<= count 0)
2405 (error "Positive numbers only")
2406 (customize-set-variable 'gdb-memory-repeat-count count)
2407 (gdb-invalidate-memory)))))
2408
2409 (defun gdb-memory-format-binary ()
2410 "Set the display format to binary."
2411 (interactive)
2412 (customize-set-variable 'gdb-memory-format "t")
2413 (gdb-invalidate-memory))
2414
2415 (defun gdb-memory-format-octal ()
2416 "Set the display format to octal."
2417 (interactive)
2418 (customize-set-variable 'gdb-memory-format "o")
2419 (gdb-invalidate-memory))
2420
2421 (defun gdb-memory-format-unsigned ()
2422 "Set the display format to unsigned decimal."
2423 (interactive)
2424 (customize-set-variable 'gdb-memory-format "u")
2425 (gdb-invalidate-memory))
2426
2427 (defun gdb-memory-format-signed ()
2428 "Set the display format to decimal."
2429 (interactive)
2430 (customize-set-variable 'gdb-memory-format "d")
2431 (gdb-invalidate-memory))
2432
2433 (defun gdb-memory-format-hexadecimal ()
2434 "Set the display format to hexadecimal."
2435 (interactive)
2436 (customize-set-variable 'gdb-memory-format "x")
2437 (gdb-invalidate-memory))
2438
2439 (defvar gdb-memory-format-map
2440 (let ((map (make-sparse-keymap)))
2441 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2442 map)
2443 "Keymap to select format in the header line.")
2444
2445 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2446 "Menu of display formats in the header line.")
2447
2448 (define-key gdb-memory-format-menu [binary]
2449 '(menu-item "Binary" gdb-memory-format-binary
2450 :button (:radio . (equal gdb-memory-format "t"))))
2451 (define-key gdb-memory-format-menu [octal]
2452 '(menu-item "Octal" gdb-memory-format-octal
2453 :button (:radio . (equal gdb-memory-format "o"))))
2454 (define-key gdb-memory-format-menu [unsigned]
2455 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2456 :button (:radio . (equal gdb-memory-format "u"))))
2457 (define-key gdb-memory-format-menu [signed]
2458 '(menu-item "Signed Decimal" gdb-memory-format-signed
2459 :button (:radio . (equal gdb-memory-format "d"))))
2460 (define-key gdb-memory-format-menu [hexadecimal]
2461 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2462 :button (:radio . (equal gdb-memory-format "x"))))
2463
2464 (defun gdb-memory-format-menu (event)
2465 (interactive "@e")
2466 (x-popup-menu event gdb-memory-format-menu))
2467
2468 (defun gdb-memory-format-menu-1 (event)
2469 (interactive "e")
2470 (save-selected-window
2471 (select-window (posn-window (event-start event)))
2472 (let* ((selection (gdb-memory-format-menu event))
2473 (binding (and selection (lookup-key gdb-memory-format-menu
2474 (vector (car selection))))))
2475 (if binding (call-interactively binding)))))
2476
2477 (defun gdb-memory-unit-giant ()
2478 "Set the unit size to giant words (eight bytes)."
2479 (interactive)
2480 (customize-set-variable 'gdb-memory-unit "g")
2481 (gdb-invalidate-memory))
2482
2483 (defun gdb-memory-unit-word ()
2484 "Set the unit size to words (four bytes)."
2485 (interactive)
2486 (customize-set-variable 'gdb-memory-unit "w")
2487 (gdb-invalidate-memory))
2488
2489 (defun gdb-memory-unit-halfword ()
2490 "Set the unit size to halfwords (two bytes)."
2491 (interactive)
2492 (customize-set-variable 'gdb-memory-unit "h")
2493 (gdb-invalidate-memory))
2494
2495 (defun gdb-memory-unit-byte ()
2496 "Set the unit size to bytes."
2497 (interactive)
2498 (customize-set-variable 'gdb-memory-unit "b")
2499 (gdb-invalidate-memory))
2500
2501 (defvar gdb-memory-unit-map
2502 (let ((map (make-sparse-keymap)))
2503 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2504 map)
2505 "Keymap to select units in the header line.")
2506
2507 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2508 "Menu of units in the header line.")
2509
2510 (define-key gdb-memory-unit-menu [giantwords]
2511 '(menu-item "Giant words" gdb-memory-unit-giant
2512 :button (:radio . (equal gdb-memory-unit "g"))))
2513 (define-key gdb-memory-unit-menu [words]
2514 '(menu-item "Words" gdb-memory-unit-word
2515 :button (:radio . (equal gdb-memory-unit "w"))))
2516 (define-key gdb-memory-unit-menu [halfwords]
2517 '(menu-item "Halfwords" gdb-memory-unit-halfword
2518 :button (:radio . (equal gdb-memory-unit "h"))))
2519 (define-key gdb-memory-unit-menu [bytes]
2520 '(menu-item "Bytes" gdb-memory-unit-byte
2521 :button (:radio . (equal gdb-memory-unit "b"))))
2522
2523 (defun gdb-memory-unit-menu (event)
2524 (interactive "@e")
2525 (x-popup-menu event gdb-memory-unit-menu))
2526
2527 (defun gdb-memory-unit-menu-1 (event)
2528 (interactive "e")
2529 (save-selected-window
2530 (select-window (posn-window (event-start event)))
2531 (let* ((selection (gdb-memory-unit-menu event))
2532 (binding (and selection (lookup-key gdb-memory-unit-menu
2533 (vector (car selection))))))
2534 (if binding (call-interactively binding)))))
2535
2536 ;;from make-mode-line-mouse-map
2537 (defun gdb-make-header-line-mouse-map (mouse function) "\
2538 Return a keymap with single entry for mouse key MOUSE on the header line.
2539 MOUSE is defined to run function FUNCTION with no args in the buffer
2540 corresponding to the mode line clicked."
2541 (let ((map (make-sparse-keymap)))
2542 (define-key map (vector 'header-line mouse) function)
2543 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2544 map))
2545
2546 (defvar gdb-memory-font-lock-keywords
2547 '(;; <__function.name+n>
2548 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2549 )
2550 "Font lock keywords used in `gdb-memory-mode'.")
2551
2552 (defun gdb-memory-mode ()
2553 "Major mode for examining memory.
2554
2555 \\{gdb-memory-mode-map}"
2556 (kill-all-local-variables)
2557 (setq major-mode 'gdb-memory-mode)
2558 (setq mode-name "Memory")
2559 (setq buffer-read-only t)
2560 (use-local-map gdb-memory-mode-map)
2561 (setq header-line-format
2562 '(:eval
2563 (concat
2564 "Read address["
2565 (propertize
2566 "-"
2567 'face font-lock-warning-face
2568 'help-echo "mouse-1: Decrement address"
2569 'mouse-face 'mode-line-highlight
2570 'local-map
2571 (gdb-make-header-line-mouse-map
2572 'mouse-1
2573 (lambda () (interactive)
2574 (let ((gdb-memory-address
2575 ;; Let GDB do the arithmetic.
2576 (concat
2577 gdb-memory-address " - "
2578 (number-to-string
2579 (* gdb-memory-repeat-count
2580 (cond ((string= gdb-memory-unit "b") 1)
2581 ((string= gdb-memory-unit "h") 2)
2582 ((string= gdb-memory-unit "w") 4)
2583 ((string= gdb-memory-unit "g") 8)))))))
2584 (gdb-invalidate-memory)))))
2585 "|"
2586 (propertize "+"
2587 'face font-lock-warning-face
2588 'help-echo "mouse-1: Increment address"
2589 'mouse-face 'mode-line-highlight
2590 'local-map (gdb-make-header-line-mouse-map
2591 'mouse-1
2592 (lambda () (interactive)
2593 (let ((gdb-memory-address nil))
2594 (gdb-invalidate-memory)))))
2595 "]: "
2596 (propertize gdb-memory-address
2597 'face font-lock-warning-face
2598 'help-echo "mouse-1: Set memory address"
2599 'mouse-face 'mode-line-highlight
2600 'local-map (gdb-make-header-line-mouse-map
2601 'mouse-1
2602 #'gdb-memory-set-address))
2603 " Repeat Count: "
2604 (propertize (number-to-string gdb-memory-repeat-count)
2605 'face font-lock-warning-face
2606 'help-echo "mouse-1: Set repeat count"
2607 'mouse-face 'mode-line-highlight
2608 'local-map (gdb-make-header-line-mouse-map
2609 'mouse-1
2610 #'gdb-memory-set-repeat-count))
2611 " Display Format: "
2612 (propertize gdb-memory-format
2613 'face font-lock-warning-face
2614 'help-echo "mouse-3: Select display format"
2615 'mouse-face 'mode-line-highlight
2616 'local-map gdb-memory-format-map)
2617 " Unit Size: "
2618 (propertize gdb-memory-unit
2619 'face font-lock-warning-face
2620 'help-echo "mouse-3: Select unit size"
2621 'mouse-face 'mode-line-highlight
2622 'local-map gdb-memory-unit-map))))
2623 (set (make-local-variable 'font-lock-defaults)
2624 '(gdb-memory-font-lock-keywords))
2625 (run-mode-hooks 'gdb-memory-mode-hook)
2626 'gdb-invalidate-memory)
2627
2628 (defun gdb-memory-buffer-name ()
2629 (with-current-buffer gud-comint-buffer
2630 (concat "*memory of " (gdb-get-target-string) "*")))
2631
2632 (defun gdb-display-memory-buffer ()
2633 "Display memory contents."
2634 (interactive)
2635 (gdb-display-buffer
2636 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2637
2638 (defun gdb-frame-memory-buffer ()
2639 "Display memory contents in a new frame."
2640 (interactive)
2641 (let ((special-display-regexps (append special-display-regexps '(".*")))
2642 (special-display-frame-alist gdb-frame-parameters))
2643 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2644 \f
2645
2646 ;; Locals buffer.
2647 ;;
2648 (gdb-set-buffer-rules 'gdb-locals-buffer
2649 'gdb-locals-buffer-name
2650 'gdb-locals-mode)
2651
2652 (def-gdb-auto-update-trigger gdb-invalidate-locals
2653 (gdb-get-buffer 'gdb-locals-buffer)
2654 "server info locals\n"
2655 gdb-info-locals-handler)
2656
2657 (defvar gdb-locals-watch-map
2658 (let ((map (make-sparse-keymap)))
2659 (suppress-keymap map)
2660 (define-key map "\r" (lambda () (interactive)
2661 (beginning-of-line)
2662 (gud-watch)))
2663 (define-key map [mouse-2] (lambda (event) (interactive "e")
2664 (mouse-set-point event)
2665 (beginning-of-line)
2666 (gud-watch)))
2667 map)
2668 "Keymap to create watch expression of a complex data type local variable.")
2669
2670 (defconst gdb-struct-string
2671 (concat (propertize "[struct/union]"
2672 'mouse-face 'highlight
2673 'help-echo "mouse-2: create watch expression"
2674 'local-map gdb-locals-watch-map) "\n"))
2675
2676 (defconst gdb-array-string
2677 (concat " " (propertize "[array]"
2678 'mouse-face 'highlight
2679 'help-echo "mouse-2: create watch expression"
2680 'local-map gdb-locals-watch-map) "\n"))
2681
2682 ;; Abbreviate for arrays and structures.
2683 ;; These can be expanded using gud-display.
2684 (defun gdb-info-locals-handler ()
2685 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2686 gdb-pending-triggers))
2687 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2688 (with-current-buffer buf
2689 (goto-char (point-min))
2690 (while (re-search-forward "^[ }].*\n" nil t)
2691 (replace-match "" nil nil))
2692 (goto-char (point-min))
2693 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2694 (replace-match gdb-struct-string nil nil))
2695 (goto-char (point-min))
2696 (while (re-search-forward "\\s-*{.*\n" nil t)
2697 (replace-match gdb-array-string nil nil))))
2698 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2699 (and buf
2700 (with-current-buffer buf
2701 (let* ((window (get-buffer-window buf 0))
2702 (start (window-start window))
2703 (p (window-point window))
2704 (buffer-read-only nil))
2705 (erase-buffer)
2706 (insert-buffer-substring (gdb-get-buffer-create
2707 'gdb-partial-output-buffer))
2708 (set-window-start window start)
2709 (set-window-point window p))
2710 )))
2711 (run-hooks 'gdb-info-locals-hook))
2712
2713 (defvar gdb-locals-mode-map
2714 (let ((map (make-sparse-keymap)))
2715 (suppress-keymap map)
2716 (define-key map "q" 'kill-this-buffer)
2717 map))
2718
2719 (defun gdb-locals-mode ()
2720 "Major mode for gdb locals.
2721
2722 \\{gdb-locals-mode-map}"
2723 (kill-all-local-variables)
2724 (setq major-mode 'gdb-locals-mode)
2725 (setq mode-name (concat "Locals:" gdb-selected-frame))
2726 (setq buffer-read-only t)
2727 (use-local-map gdb-locals-mode-map)
2728 (set (make-local-variable 'font-lock-defaults)
2729 '(gdb-locals-font-lock-keywords))
2730 (run-mode-hooks 'gdb-locals-mode-hook)
2731 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2732 (string-equal gdb-version "pre-6.4"))
2733 'gdb-invalidate-locals
2734 'gdb-invalidate-locals-1))
2735
2736 (defun gdb-locals-buffer-name ()
2737 (with-current-buffer gud-comint-buffer
2738 (concat "*locals of " (gdb-get-target-string) "*")))
2739
2740 (defun gdb-display-locals-buffer ()
2741 "Display local variables of current stack and their values."
2742 (interactive)
2743 (gdb-display-buffer
2744 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2745
2746 (defun gdb-frame-locals-buffer ()
2747 "Display local variables of current stack and their values in a new frame."
2748 (interactive)
2749 (let ((special-display-regexps (append special-display-regexps '(".*")))
2750 (special-display-frame-alist gdb-frame-parameters))
2751 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2752 \f
2753
2754 ;;;; Window management
2755 (defun gdb-display-buffer (buf dedicated &optional size)
2756 (let ((answer (get-buffer-window buf 0))
2757 (must-split nil))
2758 (if answer
2759 (display-buffer buf nil 0) ;Raise the frame if necessary.
2760 ;; The buffer is not yet displayed.
2761 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2762 (let ((window (get-lru-window)))
2763 (if (and window
2764 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2765 ,gdb-source-window))))
2766 (progn
2767 (set-window-buffer window buf)
2768 (setq answer window))
2769 (setq must-split t)))
2770 (if must-split
2771 (let* ((largest (get-largest-window))
2772 (cur-size (window-height largest))
2773 (new-size (and size (< size cur-size) (- cur-size size))))
2774 (setq answer (split-window largest new-size))
2775 (set-window-buffer answer buf)
2776 (set-window-dedicated-p answer dedicated)))
2777 answer)))
2778
2779 \f
2780 ;;; Shared keymap initialization:
2781
2782 (let ((menu (make-sparse-keymap "GDB-Windows")))
2783 (define-key gud-menu-map [displays]
2784 `(menu-item "GDB-Windows" ,menu
2785 :visible (memq gud-minor-mode '(gdbmi gdba))))
2786 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2787 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2788 (define-key menu [inferior]
2789 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2790 :enable gdb-use-separate-io-buffer))
2791 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2792 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2793 (define-key menu [disassembly]
2794 '("Disassembly" . gdb-display-assembler-buffer))
2795 (define-key menu [breakpoints]
2796 '("Breakpoints" . gdb-display-breakpoints-buffer))
2797 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2798 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2799
2800 (let ((menu (make-sparse-keymap "GDB-Frames")))
2801 (define-key gud-menu-map [frames]
2802 `(menu-item "GDB-Frames" ,menu
2803 :visible (memq gud-minor-mode '(gdbmi gdba))))
2804 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2805 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2806 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2807 (define-key menu [inferior]
2808 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2809 :enable gdb-use-separate-io-buffer))
2810 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2811 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2812 (define-key menu [breakpoints]
2813 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2814 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2815 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2816
2817 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2818 (define-key gud-menu-map [ui]
2819 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2820 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2821 (define-key menu [gdb-find-source-frame]
2822 '(menu-item "Look For Source Frame" gdb-find-source-frame
2823 :visible (eq gud-minor-mode 'gdba)
2824 :help "Toggle look for source frame."
2825 :button (:toggle . gdb-find-source-frame)))
2826 (define-key menu [gdb-use-separate-io]
2827 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2828 :visible (eq gud-minor-mode 'gdba)
2829 :help "Toggle separate IO for debugged program."
2830 :button (:toggle . gdb-use-separate-io-buffer)))
2831 (define-key menu [gdb-many-windows]
2832 '(menu-item "Display Other Windows" gdb-many-windows
2833 :help "Toggle display of locals, stack and breakpoint information"
2834 :button (:toggle . gdb-many-windows)))
2835 (define-key menu [gdb-restore-windows]
2836 '(menu-item "Restore Window Layout" gdb-restore-windows
2837 :help "Restore standard layout for debug session.")))
2838
2839 (defun gdb-frame-gdb-buffer ()
2840 "Display GUD buffer in a new frame."
2841 (interactive)
2842 (let ((special-display-regexps (append special-display-regexps '(".*")))
2843 (special-display-frame-alist
2844 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2845 gdb-frame-parameters)))
2846 (same-window-regexps nil))
2847 (display-buffer gud-comint-buffer)))
2848
2849 (defun gdb-display-gdb-buffer ()
2850 "Display GUD buffer."
2851 (interactive)
2852 (let ((same-window-regexps nil))
2853 (pop-to-buffer gud-comint-buffer)))
2854
2855 (defun gdb-set-window-buffer (name)
2856 (set-window-buffer (selected-window) (get-buffer name))
2857 (set-window-dedicated-p (selected-window) t))
2858
2859 (defun gdb-setup-windows ()
2860 "Layout the window pattern for `gdb-many-windows'."
2861 (gdb-display-locals-buffer)
2862 (gdb-display-stack-buffer)
2863 (delete-other-windows)
2864 (gdb-display-breakpoints-buffer)
2865 (delete-other-windows)
2866 ; Don't dedicate.
2867 (pop-to-buffer gud-comint-buffer)
2868 (split-window nil ( / ( * (window-height) 3) 4))
2869 (split-window nil ( / (window-height) 3))
2870 (split-window-horizontally)
2871 (other-window 1)
2872 (gdb-set-window-buffer (gdb-locals-buffer-name))
2873 (other-window 1)
2874 (switch-to-buffer
2875 (if gud-last-last-frame
2876 (gud-find-file (car gud-last-last-frame))
2877 (if gdb-main-file
2878 (gud-find-file gdb-main-file)
2879 ;; Put buffer list in window if we
2880 ;; can't find a source file.
2881 (list-buffers-noselect))))
2882 (setq gdb-source-window (selected-window))
2883 (when gdb-use-separate-io-buffer
2884 (split-window-horizontally)
2885 (other-window 1)
2886 (gdb-set-window-buffer
2887 (gdb-get-buffer-create 'gdb-inferior-io)))
2888 (other-window 1)
2889 (gdb-set-window-buffer (gdb-stack-buffer-name))
2890 (split-window-horizontally)
2891 (other-window 1)
2892 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2893 (other-window 1))
2894
2895 (defun gdb-restore-windows ()
2896 "Restore the basic arrangement of windows used by gdba.
2897 This arrangement depends on the value of `gdb-many-windows'."
2898 (interactive)
2899 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2900 (delete-other-windows)
2901 (if gdb-many-windows
2902 (gdb-setup-windows)
2903 (when (or gud-last-last-frame gdb-show-main)
2904 (split-window)
2905 (other-window 1)
2906 (switch-to-buffer
2907 (if gud-last-last-frame
2908 (gud-find-file (car gud-last-last-frame))
2909 (gud-find-file gdb-main-file)))
2910 (setq gdb-source-window (selected-window))
2911 (other-window 1))))
2912
2913 (defun gdb-reset ()
2914 "Exit a debugging session cleanly.
2915 Kills the gdb buffers, and resets variables and the source buffers."
2916 (dolist (buffer (buffer-list))
2917 (unless (eq buffer gud-comint-buffer)
2918 (with-current-buffer buffer
2919 (if (memq gud-minor-mode '(gdbmi gdba))
2920 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2921 (kill-buffer nil)
2922 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2923 (setq gud-minor-mode nil)
2924 (kill-local-variable 'tool-bar-map)
2925 (kill-local-variable 'gdb-define-alist))))))
2926 (setq gdb-overlay-arrow-position nil)
2927 (setq overlay-arrow-variable-list
2928 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2929 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2930 (setq gdb-stack-position nil)
2931 (setq overlay-arrow-variable-list
2932 (delq 'gdb-stack-position overlay-arrow-variable-list))
2933 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
2934 (setq gud-running nil)
2935 (setq gdb-active-process nil)
2936 (setq gdb-var-list nil)
2937 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2938
2939 (defun gdb-source-info ()
2940 "Find the source file where the program starts and displays it with related
2941 buffers."
2942 (goto-char (point-min))
2943 (if (and (search-forward "Located in " nil t)
2944 (looking-at "\\S-+"))
2945 (setq gdb-main-file (match-string 0)))
2946 (goto-char (point-min))
2947 (if (search-forward "Includes preprocessor macro info." nil t)
2948 (setq gdb-macro-info t))
2949 (if gdb-many-windows
2950 (gdb-setup-windows)
2951 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2952 (if gdb-show-main
2953 (let ((pop-up-windows t))
2954 (display-buffer (gud-find-file gdb-main-file))))))
2955
2956 (defun gdb-get-location (bptno line flag)
2957 "Find the directory containing the relevant source file.
2958 Put in buffer and place breakpoint icon."
2959 (goto-char (point-min))
2960 (catch 'file-not-found
2961 (if (search-forward "Located in " nil t)
2962 (when (looking-at "\\S-+")
2963 (delete (cons bptno "File not found") gdb-location-alist)
2964 (push (cons bptno (match-string 0)) gdb-location-alist))
2965 (gdb-resync)
2966 (unless (assoc bptno gdb-location-alist)
2967 (push (cons bptno "File not found") gdb-location-alist)
2968 (message-box "Cannot find source file for breakpoint location.\n\
2969 Add directory to search path for source files using the GDB command, dir."))
2970 (throw 'file-not-found nil))
2971 (with-current-buffer
2972 (find-file-noselect (match-string 0))
2973 (save-current-buffer
2974 (set (make-local-variable 'gud-minor-mode) 'gdba)
2975 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2976 ;; only want one breakpoint icon at each location
2977 (save-excursion
2978 (goto-line (string-to-number line))
2979 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2980
2981 (add-hook 'find-file-hook 'gdb-find-file-hook)
2982
2983 (defun gdb-find-file-hook ()
2984 "Set up buffer for debugging if file is part of the source code
2985 of the current session."
2986 (if (and (buffer-name gud-comint-buffer)
2987 ;; in case gud or gdb-ui is just loaded
2988 gud-comint-buffer
2989 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2990 '(gdba gdbmi)))
2991 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
2992 (if (member (if (string-equal gdb-version "pre-6.4")
2993 (file-name-nondirectory buffer-file-name)
2994 buffer-file-name)
2995 gdb-source-file-list)
2996 (with-current-buffer (find-buffer-visiting buffer-file-name)
2997 (set (make-local-variable 'gud-minor-mode)
2998 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2999 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
3000
3001 ;;from put-image
3002 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3003 "Put string PUTSTRING in front of POS in the current buffer.
3004 PUTSTRING is displayed by putting an overlay into the current buffer with a
3005 `before-string' string that has a `display' property whose value is
3006 PUTSTRING."
3007 (let ((string (make-string 1 ?x))
3008 (buffer (current-buffer)))
3009 (setq putstring (copy-sequence putstring))
3010 (let ((overlay (make-overlay pos pos buffer))
3011 (prop (or dprop
3012 (list (list 'margin 'left-margin) putstring))))
3013 (put-text-property 0 1 'display prop string)
3014 (if sprops
3015 (add-text-properties 0 1 sprops string))
3016 (overlay-put overlay 'put-break t)
3017 (overlay-put overlay 'before-string string))))
3018
3019 ;;from remove-images
3020 (defun gdb-remove-strings (start end &optional buffer)
3021 "Remove strings between START and END in BUFFER.
3022 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3023 BUFFER nil or omitted means use the current buffer."
3024 (unless buffer
3025 (setq buffer (current-buffer)))
3026 (dolist (overlay (overlays-in start end))
3027 (when (overlay-get overlay 'put-break)
3028 (delete-overlay overlay))))
3029
3030 (defun gdb-put-breakpoint-icon (enabled bptno)
3031 (let ((start (- (line-beginning-position) 1))
3032 (end (+ (line-end-position) 1))
3033 (putstring (if enabled "B" "b"))
3034 (source-window (get-buffer-window (current-buffer) 0)))
3035 (add-text-properties
3036 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3037 putstring)
3038 (if enabled
3039 (add-text-properties
3040 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3041 (add-text-properties
3042 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3043 (gdb-remove-breakpoint-icons start end)
3044 (if (display-images-p)
3045 (if (>= (or left-fringe-width
3046 (if source-window (car (window-fringes source-window)))
3047 gdb-buffer-fringe-width) 8)
3048 (gdb-put-string
3049 nil (1+ start)
3050 `(left-fringe breakpoint
3051 ,(if enabled
3052 'breakpoint-enabled
3053 'breakpoint-disabled))
3054 'gdb-bptno bptno
3055 'gdb-enabled enabled)
3056 (when (< left-margin-width 2)
3057 (save-current-buffer
3058 (setq left-margin-width 2)
3059 (if source-window
3060 (set-window-margins
3061 source-window
3062 left-margin-width right-margin-width))))
3063 (put-image
3064 (if enabled
3065 (or breakpoint-enabled-icon
3066 (setq breakpoint-enabled-icon
3067 (find-image `((:type xpm :data
3068 ,breakpoint-xpm-data
3069 :ascent 100 :pointer hand)
3070 (:type pbm :data
3071 ,breakpoint-enabled-pbm-data
3072 :ascent 100 :pointer hand)))))
3073 (or breakpoint-disabled-icon
3074 (setq breakpoint-disabled-icon
3075 (find-image `((:type xpm :data
3076 ,breakpoint-xpm-data
3077 :conversion disabled
3078 :ascent 100 :pointer hand)
3079 (:type pbm :data
3080 ,breakpoint-disabled-pbm-data
3081 :ascent 100 :pointer hand))))))
3082 (+ start 1)
3083 putstring
3084 'left-margin))
3085 (when (< left-margin-width 2)
3086 (save-current-buffer
3087 (setq left-margin-width 2)
3088 (let ((window (get-buffer-window (current-buffer) 0)))
3089 (if window
3090 (set-window-margins
3091 window left-margin-width right-margin-width)))))
3092 (gdb-put-string
3093 (propertize putstring
3094 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3095 (1+ start)))))
3096
3097 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3098 (gdb-remove-strings start end)
3099 (if (display-images-p)
3100 (remove-images start end))
3101 (when remove-margin
3102 (setq left-margin-width 0)
3103 (let ((window (get-buffer-window (current-buffer) 0)))
3104 (if window
3105 (set-window-margins
3106 window left-margin-width right-margin-width)))))
3107
3108 \f
3109 ;;
3110 ;; Assembler buffer.
3111 ;;
3112 (gdb-set-buffer-rules 'gdb-assembler-buffer
3113 'gdb-assembler-buffer-name
3114 'gdb-assembler-mode)
3115
3116 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3117 ;; window-start but keep the overlay arrow/current line visible.
3118 (defun gdb-assembler-handler ()
3119 (setq gdb-pending-triggers
3120 (delq 'gdb-invalidate-assembler
3121 gdb-pending-triggers))
3122 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3123 (and buf
3124 (with-current-buffer buf
3125 (let* ((window (get-buffer-window buf 0))
3126 (p (window-point window))
3127 (buffer-read-only nil))
3128 (erase-buffer)
3129 (insert-buffer-substring (gdb-get-buffer-create
3130 'gdb-partial-output-buffer))
3131 (set-window-point window p)))))
3132 ;; put customisation here
3133 (gdb-assembler-custom))
3134
3135 (defun gdb-assembler-custom ()
3136 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3137 (pos 1) (address) (flag) (bptno))
3138 (with-current-buffer buffer
3139 (save-excursion
3140 (if (not (equal gdb-pc-address "main"))
3141 (progn
3142 (goto-char (point-min))
3143 (if (and gdb-pc-address
3144 (search-forward gdb-pc-address nil t))
3145 (progn
3146 (setq pos (point))
3147 (beginning-of-line)
3148 (setq fringe-indicator-alist
3149 (if (string-equal gdb-frame-number "0")
3150 nil
3151 '((overlay-arrow . hollow-right-triangle))))
3152 (or gdb-overlay-arrow-position
3153 (setq gdb-overlay-arrow-position (make-marker)))
3154 (set-marker gdb-overlay-arrow-position (point))))))
3155 ;; remove all breakpoint-icons in assembler buffer before updating.
3156 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3157 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3158 (goto-char (point-min))
3159 (while (< (point) (- (point-max) 1))
3160 (forward-line 1)
3161 (if (looking-at "[^\t].*?breakpoint")
3162 (progn
3163 (looking-at
3164 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3165 (setq bptno (match-string 1))
3166 (setq flag (char-after (match-beginning 2)))
3167 (setq address (match-string 3))
3168 (with-current-buffer buffer
3169 (save-excursion
3170 (goto-char (point-min))
3171 (if (search-forward address nil t)
3172 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3173 (if (not (equal gdb-pc-address "main"))
3174 (with-current-buffer buffer
3175 (set-window-point (get-buffer-window buffer 0) pos)))))
3176
3177 (defvar gdb-assembler-mode-map
3178 (let ((map (make-sparse-keymap)))
3179 (suppress-keymap map)
3180 (define-key map "q" 'kill-this-buffer)
3181 map))
3182
3183 (defvar gdb-assembler-font-lock-keywords
3184 '(;; <__function.name+n>
3185 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3186 (1 font-lock-function-name-face))
3187 ;; 0xNNNNNNNN <__function.name+n>: opcode
3188 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3189 (4 font-lock-keyword-face))
3190 ;; %register(at least i386)
3191 ("%\\sw+" . font-lock-variable-name-face)
3192 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3193 (1 font-lock-comment-face)
3194 (2 font-lock-function-name-face))
3195 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3196 "Font lock keywords used in `gdb-assembler-mode'.")
3197
3198 (defun gdb-assembler-mode ()
3199 "Major mode for viewing code assembler.
3200
3201 \\{gdb-assembler-mode-map}"
3202 (kill-all-local-variables)
3203 (setq major-mode 'gdb-assembler-mode)
3204 (setq mode-name (concat "Machine:" gdb-selected-frame))
3205 (setq gdb-overlay-arrow-position nil)
3206 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3207 (setq fringes-outside-margins t)
3208 (setq buffer-read-only t)
3209 (use-local-map gdb-assembler-mode-map)
3210 (gdb-invalidate-assembler)
3211 (set (make-local-variable 'font-lock-defaults)
3212 '(gdb-assembler-font-lock-keywords))
3213 (run-mode-hooks 'gdb-assembler-mode-hook)
3214 'gdb-invalidate-assembler)
3215
3216 (defun gdb-assembler-buffer-name ()
3217 (with-current-buffer gud-comint-buffer
3218 (concat "*disassembly of " (gdb-get-target-string) "*")))
3219
3220 (defun gdb-display-assembler-buffer ()
3221 "Display disassembly view."
3222 (interactive)
3223 (setq gdb-previous-frame nil)
3224 (gdb-display-buffer
3225 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3226
3227 (defun gdb-frame-assembler-buffer ()
3228 "Display disassembly view in a new frame."
3229 (interactive)
3230 (setq gdb-previous-frame nil)
3231 (let ((special-display-regexps (append special-display-regexps '(".*")))
3232 (special-display-frame-alist gdb-frame-parameters))
3233 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3234
3235 ;; modified because if gdb-pc-address has changed value a new command
3236 ;; must be enqueued to update the buffer with the new output
3237 (defun gdb-invalidate-assembler (&optional ignored)
3238 (if (gdb-get-buffer 'gdb-assembler-buffer)
3239 (progn
3240 (unless (and gdb-selected-frame
3241 (string-equal gdb-selected-frame gdb-previous-frame))
3242 (if (or (not (member 'gdb-invalidate-assembler
3243 gdb-pending-triggers))
3244 (not (string-equal gdb-pc-address
3245 gdb-previous-frame-address)))
3246 (progn
3247 ;; take previous disassemble command, if any, off the queue
3248 (with-current-buffer gud-comint-buffer
3249 (let ((queue gdb-input-queue))
3250 (dolist (item queue)
3251 (if (equal (cdr item) '(gdb-assembler-handler))
3252 (setq gdb-input-queue
3253 (delete item gdb-input-queue))))))
3254 (gdb-enqueue-input
3255 (list
3256 (concat gdb-server-prefix "disassemble "
3257 (if (member gdb-pc-address '(nil "main")) nil "0x")
3258 gdb-pc-address "\n")
3259 'gdb-assembler-handler))
3260 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3261 (setq gdb-previous-frame-address gdb-pc-address)
3262 (setq gdb-previous-frame gdb-selected-frame)))))))
3263
3264 (defun gdb-get-selected-frame ()
3265 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3266 (progn
3267 (gdb-enqueue-input
3268 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3269 (push 'gdb-get-selected-frame
3270 gdb-pending-triggers))))
3271
3272 (defun gdb-frame-handler ()
3273 (setq gdb-pending-triggers
3274 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3275 (goto-char (point-min))
3276 (when (re-search-forward
3277 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3278 (setq gdb-frame-number (match-string 1))
3279 (setq gdb-frame-address (match-string 2)))
3280 (goto-char (point-min))
3281 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3282 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3283 nil t)
3284 (setq gdb-selected-frame (match-string 2))
3285 (if (gdb-get-buffer 'gdb-locals-buffer)
3286 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3287 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3288 (if (gdb-get-buffer 'gdb-assembler-buffer)
3289 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3290 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3291 (setq gdb-pc-address (match-string 1))
3292 (if (and (match-string 3) gud-overlay-arrow-position)
3293 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3294 (position (marker-position gud-overlay-arrow-position)))
3295 (when (and buffer
3296 (string-equal (buffer-name buffer)
3297 (file-name-nondirectory (match-string 3))))
3298 (with-current-buffer buffer
3299 (setq fringe-indicator-alist
3300 (if (string-equal gdb-frame-number "0")
3301 nil
3302 '((overlay-arrow . hollow-right-triangle))))
3303 (set-marker gud-overlay-arrow-position position))))))
3304 (goto-char (point-min))
3305 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3306 (setq gdb-current-language (match-string 1)))
3307 (gdb-invalidate-assembler))
3308
3309 \f
3310 ;; Code specific to GDB 6.4
3311 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3312
3313 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3314 "Create list of source files for current GDB session.
3315 If buffers already exist for any of these files, gud-minor-mode
3316 is set in them."
3317 (goto-char (point-min))
3318 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3319 (push (match-string 1) gdb-source-file-list))
3320 (dolist (buffer (buffer-list))
3321 (with-current-buffer buffer
3322 (when (member buffer-file-name gdb-source-file-list)
3323 (set (make-local-variable 'gud-minor-mode)
3324 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3325 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3326 (when gud-tooltip-mode
3327 (make-local-variable 'gdb-define-alist)
3328 (gdb-create-define-alist)
3329 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3330 (gdb-force-mode-line-update
3331 (propertize "ready" 'face font-lock-variable-name-face)))
3332
3333 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3334 (defun gdb-var-list-children-1 (varnum)
3335 (gdb-enqueue-input
3336 (list
3337 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3338 (concat "server interpreter mi \"-var-list-children --all-values "
3339 varnum "\"\n")
3340 (concat "-var-list-children --all-values " varnum "\n"))
3341 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3342
3343 (defconst gdb-var-list-children-regexp-1
3344 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3345 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\),.*?type=\"\\(.+?\\)\".*?}")
3346
3347 (defun gdb-var-list-children-handler-1 (varnum)
3348 (goto-char (point-min))
3349 (let ((var-list nil))
3350 (catch 'child-already-watched
3351 (dolist (var gdb-var-list)
3352 (if (string-equal varnum (car var))
3353 (progn
3354 (push var var-list)
3355 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3356 (let ((varchild (list (match-string 1)
3357 (match-string 2)
3358 (match-string 3)
3359 (match-string 5)
3360 (read (match-string 4))
3361 nil)))
3362 (if (assoc (car varchild) gdb-var-list)
3363 (throw 'child-already-watched nil))
3364 (push varchild var-list))))
3365 (push var var-list)))
3366 (setq gdb-var-list (nreverse var-list))))
3367 (gdb-speedbar-update))
3368
3369 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3370 (defun gdb-var-update-1 ()
3371 (if (not (member 'gdb-var-update gdb-pending-triggers))
3372 (progn
3373 (gdb-enqueue-input
3374 (list
3375 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3376 "server interpreter mi \"-var-update --all-values *\"\n"
3377 "-var-update --all-values *\n")
3378 'gdb-var-update-handler-1))
3379 (push 'gdb-var-update gdb-pending-triggers))))
3380
3381 (defconst gdb-var-update-regexp-1
3382 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3383 in_scope=\"\\(.*?\\)\".*?}")
3384
3385 (defun gdb-var-update-handler-1 ()
3386 (dolist (var gdb-var-list)
3387 (setcar (nthcdr 5 var) nil))
3388 (goto-char (point-min))
3389 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3390 (let* ((varnum (match-string 1))
3391 (var (assoc varnum gdb-var-list)))
3392 (when var
3393 (if (string-equal (match-string 3) "false")
3394 (setcar (nthcdr 5 var) 'out-of-scope)
3395 (setcar (nthcdr 5 var) 'changed)
3396 (setcar (nthcdr 4 var)
3397 (read (match-string 2)))))))
3398 (setq gdb-pending-triggers
3399 (delq 'gdb-var-update gdb-pending-triggers))
3400 (gdb-speedbar-update))
3401
3402 ;; Registers buffer.
3403 ;;
3404 (gdb-set-buffer-rules 'gdb-registers-buffer
3405 'gdb-registers-buffer-name
3406 'gdb-registers-mode)
3407
3408 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3409 (gdb-get-buffer 'gdb-registers-buffer)
3410 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3411 "server interpreter mi \"-data-list-register-values x\"\n"
3412 "-data-list-register-values x\n")
3413 gdb-data-list-register-values-handler)
3414
3415 (defconst gdb-data-list-register-values-regexp
3416 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3417
3418 (defun gdb-data-list-register-values-handler ()
3419 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3420 gdb-pending-triggers))
3421 (goto-char (point-min))
3422 (if (re-search-forward gdb-error-regexp nil t)
3423 (let ((err (match-string 1)))
3424 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3425 (let ((buffer-read-only nil))
3426 (erase-buffer)
3427 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3428 (insert err)
3429 (goto-char (point-min)))))
3430 (let ((register-list (reverse gdb-register-names))
3431 (register nil) (register-string nil) (register-values nil))
3432 (goto-char (point-min))
3433 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3434 (setq register (pop register-list))
3435 (setq register-string (concat register "\t" (match-string 2) "\n"))
3436 (if (member (match-string 1) gdb-changed-registers)
3437 (put-text-property 0 (length register-string)
3438 'face 'font-lock-warning-face
3439 register-string))
3440 (setq register-values
3441 (concat register-values register-string)))
3442 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3443 (with-current-buffer buf
3444 (let* ((window (get-buffer-window buf 0))
3445 (start (window-start window))
3446 (p (window-point window))
3447 (buffer-read-only nil))
3448 (erase-buffer)
3449 (insert register-values)
3450 (set-window-start window start)
3451 (set-window-point window p))))))
3452 (gdb-data-list-register-values-custom))
3453
3454 (defun gdb-data-list-register-values-custom ()
3455 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3456 (save-excursion
3457 (let ((buffer-read-only nil)
3458 start end)
3459 (goto-char (point-min))
3460 (while (< (point) (point-max))
3461 (setq start (line-beginning-position))
3462 (setq end (line-end-position))
3463 (when (looking-at "^[^\t]+")
3464 (unless (string-equal (match-string 0) "No registers.")
3465 (put-text-property start (match-end 0)
3466 'face font-lock-variable-name-face)
3467 (add-text-properties start end
3468 '(help-echo "mouse-2: edit value"
3469 mouse-face highlight))))
3470 (forward-line 1))))))
3471
3472 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3473 (defun gdb-get-changed-registers ()
3474 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3475 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3476 (progn
3477 (gdb-enqueue-input
3478 (list
3479 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3480 "server interpreter mi -data-list-changed-registers\n"
3481 "-data-list-changed-registers\n")
3482 'gdb-get-changed-registers-handler))
3483 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3484
3485 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3486
3487 (defun gdb-get-changed-registers-handler ()
3488 (setq gdb-pending-triggers
3489 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3490 (setq gdb-changed-registers nil)
3491 (goto-char (point-min))
3492 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3493 (push (match-string 1) gdb-changed-registers)))
3494 \f
3495
3496 ;; Locals buffer.
3497 ;;
3498 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3499 (gdb-set-buffer-rules 'gdb-locals-buffer
3500 'gdb-locals-buffer-name
3501 'gdb-locals-mode)
3502
3503 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3504 (gdb-get-buffer 'gdb-locals-buffer)
3505 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3506 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3507 "-stack-list-locals --simple-values\n")
3508 gdb-stack-list-locals-handler)
3509
3510 (defconst gdb-stack-list-locals-regexp
3511 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3512
3513 (defvar gdb-locals-watch-map-1
3514 (let ((map (make-sparse-keymap)))
3515 (suppress-keymap map)
3516 (define-key map "\r" 'gud-watch)
3517 (define-key map [mouse-2] 'gud-watch)
3518 map)
3519 "Keymap to create watch expression of a complex data type local variable.")
3520
3521 (defvar gdb-edit-locals-map-1
3522 (let ((map (make-sparse-keymap)))
3523 (suppress-keymap map)
3524 (define-key map "\r" 'gdb-edit-locals-value)
3525 (define-key map [mouse-2] 'gdb-edit-locals-value)
3526 map)
3527 "Keymap to edit value of a simple data type local variable.")
3528
3529 (defun gdb-edit-locals-value (&optional event)
3530 "Assign a value to a variable displayed in the locals buffer."
3531 (interactive (list last-input-event))
3532 (save-excursion
3533 (if event (posn-set-point (event-end event)))
3534 (beginning-of-line)
3535 (let* ((var (current-word))
3536 (value (read-string (format "New value (%s): " var))))
3537 (gdb-enqueue-input
3538 (list (concat gdb-server-prefix"set variable " var " = " value "\n")
3539 'ignore))))))
3540
3541 ;; Dont display values of arrays or structures.
3542 ;; These can be expanded using gud-watch.
3543 (defun gdb-stack-list-locals-handler ()
3544 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3545 gdb-pending-triggers))
3546 (goto-char (point-min))
3547 (if (re-search-forward gdb-error-regexp nil t)
3548 (let ((err (match-string 1)))
3549 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3550 (let ((buffer-read-only nil))
3551 (erase-buffer)
3552 (insert err)
3553 (goto-char (point-min)))))
3554 (let (local locals-list)
3555 (goto-char (point-min))
3556 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3557 (let ((local (list (match-string 1)
3558 (match-string 2)
3559 nil)))
3560 (if (looking-at ",value=\\(\".*\"\\).*?}")
3561 (setcar (nthcdr 2 local) (read (match-string 1))))
3562 (push local locals-list)))
3563 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3564 (and buf (with-current-buffer buf
3565 (let* ((window (get-buffer-window buf 0))
3566 (start (window-start window))
3567 (p (window-point window))
3568 (buffer-read-only nil) (name) (value))
3569 (erase-buffer)
3570 (dolist (local locals-list)
3571 (setq name (car local))
3572 (setq value (nth 2 local))
3573 (if (or (not value)
3574 (string-match "^\\0x" value))
3575 (add-text-properties 0 (length name)
3576 `(mouse-face highlight
3577 help-echo "mouse-2: create watch expression"
3578 local-map ,gdb-locals-watch-map-1)
3579 name)
3580 (add-text-properties 0 (length value)
3581 `(mouse-face highlight
3582 help-echo "mouse-2: edit value"
3583 local-map ,gdb-edit-locals-map-1)
3584 value))
3585 (insert
3586 (concat name "\t" (nth 1 local)
3587 "\t" (nth 2 local) "\n")))
3588 (set-window-start window start)
3589 (set-window-point window p))))))))
3590
3591 (defun gdb-get-register-names ()
3592 "Create a list of register names."
3593 (goto-char (point-min))
3594 (setq gdb-register-names nil)
3595 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3596 (push (match-string 1) gdb-register-names)))
3597
3598 (provide 'gdb-ui)
3599
3600 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3601 ;;; gdb-ui.el ends here