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