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