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