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