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