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