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