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