]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-ui.el
(compilation-start): Move let-binding of
[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 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 ;; Start the debugger with M-x gdba.
37
38 ;; This file has evolved from gdba.el from GDB 5.0 written by Tom Lord and Jim
39 ;; Kingdon and uses GDB's annotation interface. You don't need to know about
40 ;; annotations to use this mode as a debugger, but if you are interested
41 ;; developing the mode itself, then see the Annotations section in the GDB
42 ;; info manual.
43 ;;
44 ;; GDB developers plan to make the annotation interface obsolete. A new
45 ;; interface called GDB/MI (machine interface) has been designed to replace
46 ;; it. Some GDB/MI commands are used in this file through the CLI command
47 ;; 'interpreter mi <mi-command>'. A file called gdb-mi.el is included in the
48 ;; GDB repository for future releases (6.2 onwards) that uses GDB/MI as the
49 ;; primary interface to GDB. It is still under development and is part of a
50 ;; process to migrate Emacs from annotations to GDB/MI.
51 ;;
52 ;; Known Bugs:
53 ;;
54
55 ;;; Code:
56
57 (require 'gud)
58
59 (defvar gdb-current-address "main" "Initialisation for Assembler buffer.")
60 (defvar gdb-previous-address nil)
61 (defvar gdb-previous-frame nil)
62 (defvar gdb-current-frame nil)
63 (defvar gdb-current-language nil)
64 (defvar gdb-view-source t "Non-nil means that source code can be viewed.")
65 (defvar gdb-selected-view 'source "Code type that user wishes to view.")
66 (defvar gdb-var-list nil "List of variables in watch window")
67 (defvar gdb-var-changed nil "Non-nil means that gdb-var-list has changed.")
68 (defvar gdb-buffer-type nil)
69 (defvar gdb-overlay-arrow-position nil)
70 (defvar gdb-variables '()
71 "A list of variables that are local to the GUD buffer.")
72 (defvar gdb-server-prefix nil)
73
74 ;;;###autoload
75 (defun gdba (command-line)
76 "Run gdb on program FILE in buffer *gud-FILE*.
77 The directory containing FILE becomes the initial working directory
78 and source-file directory for your debugger.
79
80 If `gdb-many-windows' is nil (the default value) then gdb just
81 pops up the GUD buffer unless `gdb-show-main' is t. In this case
82 it starts with two windows: one displaying the GUD buffer and the
83 other with the source file with the main routine of the inferior.
84
85 If `gdb-many-windows' is t, regardless of the value of
86 `gdb-show-main', the layout below will appear unless
87 `gdb-use-inferior-io-buffer' is nil when the source buffer
88 occupies the full width of the frame. Keybindings are given in
89 relevant buffer.
90
91 Watch expressions appear in the speedbar/slowbar.
92
93 The following interactive lisp functions help control operation :
94
95 `gdb-many-windows' - Toggle the number of windows gdb uses.
96 `gdb-restore-windows' - To restore the window layout.
97
98 See Info node `(emacs)GDB Graphical Interface' for a more
99 detailed description of this mode.
100
101
102 ---------------------------------------------------------------------
103 GDB Toolbar
104 ---------------------------------------------------------------------
105 GUD buffer (I/O of GDB) | Locals buffer
106 |
107 |
108 |
109 ---------------------------------------------------------------------
110 Source buffer | Input/Output (of inferior) buffer
111 | (comint-mode)
112 |
113 |
114 |
115 |
116 |
117 |
118 ---------------------------------------------------------------------
119 Stack buffer | Breakpoints buffer
120 RET gdb-frames-select | SPC gdb-toggle-breakpoint
121 | RET gdb-goto-breakpoint
122 | d gdb-delete-breakpoint
123 ---------------------------------------------------------------------
124 "
125 ;;
126 (interactive (list (gud-query-cmdline 'gdba)))
127 ;;
128 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
129 (gdb command-line)
130 (gdb-ann3))
131
132 (defvar gdb-debug-log nil)
133
134 (defcustom gdb-enable-debug-log nil
135 "Non-nil means record the process input and output in `gdb-debug-log'."
136 :type 'boolean
137 :group 'gud)
138
139 (defcustom gdb-use-inferior-io-buffer nil
140 "Non-nil means display output from the inferior in a separate buffer."
141 :type 'boolean
142 :group 'gud)
143
144 (defun gdb-ann3 ()
145 (setq gdb-debug-log nil)
146 (set (make-local-variable 'gud-minor-mode) 'gdba)
147 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
148 ;;
149 (gud-def gud-break (if (not (string-equal mode-name "Machine"))
150 (gud-call "break %f:%l" arg)
151 (save-excursion
152 (beginning-of-line)
153 (forward-char 2)
154 (gud-call "break *%a" arg)))
155 "\C-b" "Set breakpoint at current line or address.")
156 ;;
157 (gud-def gud-remove (if (not (string-equal mode-name "Machine"))
158 (gud-call "clear %f:%l" arg)
159 (save-excursion
160 (beginning-of-line)
161 (forward-char 2)
162 (gud-call "clear *%a" arg)))
163 "\C-d" "Remove breakpoint at current line or address.")
164 ;;
165 (gud-def gud-until (if (not (string-equal mode-name "Machine"))
166 (gud-call "until %f:%l" arg)
167 (save-excursion
168 (beginning-of-line)
169 (forward-char 2)
170 (gud-call "until *%a" arg)))
171 "\C-u" "Continue to current line or address.")
172
173 (define-key gud-minor-mode-map [left-margin mouse-1]
174 'gdb-mouse-toggle-breakpoint)
175 (define-key gud-minor-mode-map [left-fringe mouse-1]
176 'gdb-mouse-toggle-breakpoint)
177
178 (setq comint-input-sender 'gdb-send)
179 ;;
180 ;; (re-)initialise
181 (setq gdb-current-address "main")
182 (setq gdb-previous-address nil)
183 (setq gdb-previous-frame nil)
184 (setq gdb-current-frame nil)
185 (setq gdb-view-source t)
186 (setq gdb-selected-view 'source)
187 (setq gdb-var-list nil)
188 (setq gdb-var-changed nil)
189 (setq gdb-first-prompt nil)
190 (setq gdb-prompting nil)
191 (setq gdb-input-queue nil)
192 (setq gdb-current-item nil)
193 (setq gdb-pending-triggers nil)
194 (setq gdb-output-sink 'user)
195 (setq gdb-server-prefix "server ")
196 ;;
197 (mapc 'make-local-variable gdb-variables)
198 (setq gdb-buffer-type 'gdba)
199 ;;
200 (if gdb-use-inferior-io-buffer (gdb-clear-inferior-io))
201 ;;
202 (if (eq window-system 'w32)
203 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
204 (gdb-enqueue-input (list "set height 0\n" 'ignore))
205 ;; find source file and compilation directory here
206 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
207 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
208 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
209 ;;
210 (run-hooks 'gdba-mode-hook))
211
212 (defcustom gdb-use-colon-colon-notation nil
213 "Non-nil means use FUNCTION::VARIABLE format to display variables in the
214 speedbar."
215 :type 'boolean
216 :group 'gud)
217
218 (defun gud-watch ()
219 "Watch expression at point."
220 (interactive)
221 (require 'tooltip)
222 (let ((expr (tooltip-identifier-from-point (point))))
223 (if (and (string-equal gdb-current-language "c")
224 gdb-use-colon-colon-notation gdb-current-frame)
225 (setq expr (concat gdb-current-frame "::" expr)))
226 (catch 'already-watched
227 (dolist (var gdb-var-list)
228 (if (string-equal expr (car var)) (throw 'already-watched nil)))
229 (set-text-properties 0 (length expr) nil expr)
230 (gdb-enqueue-input
231 (list
232 (if (eq gud-minor-mode 'gdba)
233 (concat "server interpreter mi \"-var-create - * " expr "\"\n")
234 (concat"-var-create - * " expr "\n"))
235 `(lambda () (gdb-var-create-handler ,expr))))))
236 (select-window (get-buffer-window gud-comint-buffer 'visible)))
237
238 (defun gdb-goto-info ()
239 "Go to Emacs info node: GDB Graphical Interface."
240 (interactive)
241 (select-frame (make-frame))
242 (require 'info)
243 (Info-goto-node "(emacs)GDB Graphical Interface"))
244
245 (defconst gdb-var-create-regexp
246 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
247
248 (defun gdb-var-create-handler (expr)
249 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
250 (goto-char (point-min))
251 (if (re-search-forward gdb-var-create-regexp nil t)
252 (let ((var (list expr
253 (match-string 1)
254 (match-string 2)
255 (match-string 3)
256 nil nil)))
257 (push var gdb-var-list)
258 (setq speedbar-update-flag t)
259 (speedbar 1)
260 (if (equal (nth 2 var) "0")
261 (gdb-enqueue-input
262 (list (concat "server interpreter mi \"-var-evaluate-expression "
263 (nth 1 var) "\"\n")
264 `(lambda () (gdb-var-evaluate-expression-handler
265 ,(nth 1 var) nil))))
266 (setq gdb-var-changed t)))
267 (if (re-search-forward "Undefined command" nil t)
268 (message "Watching expressions requires gdb 6.0 onwards")
269 (message "No symbol %s in current context." expr)))))
270
271 (defun gdb-var-evaluate-expression-handler (varnum changed)
272 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
273 (goto-char (point-min))
274 (re-search-forward ".*value=\"\\(.*?\\)\"" nil t)
275 (catch 'var-found
276 (let ((var-list nil) (num 0))
277 (dolist (var gdb-var-list)
278 (if (string-equal varnum (cadr var))
279 (progn
280 (if changed (setcar (nthcdr 5 var) t))
281 (setcar (nthcdr 4 var) (match-string 1))
282 (setcar (nthcdr num gdb-var-list) var)
283 (throw 'var-found nil)))
284 (setq num (+ num 1))))))
285 (setq gdb-var-changed t))
286
287 (defun gdb-var-list-children (varnum)
288 (gdb-enqueue-input
289 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
290 `(lambda () (gdb-var-list-children-handler ,varnum)))))
291
292 (defconst gdb-var-list-children-regexp
293 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\"")
294
295 (defun gdb-var-list-children-handler (varnum)
296 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
297 (goto-char (point-min))
298 (let ((var-list nil))
299 (catch 'child-already-watched
300 (dolist (var gdb-var-list)
301 (if (string-equal varnum (cadr var))
302 (progn
303 (push var var-list)
304 (while (re-search-forward gdb-var-list-children-regexp nil t)
305 (let ((varchild (list (match-string 2)
306 (match-string 1)
307 (match-string 3)
308 nil nil nil)))
309 (if (looking-at ",type=\"\\(.*?\\)\"")
310 (setcar (nthcdr 3 varchild) (match-string 1)))
311 (dolist (var1 gdb-var-list)
312 (if (string-equal (cadr var1) (cadr varchild))
313 (throw 'child-already-watched nil)))
314 (push varchild var-list)
315 (if (equal (nth 2 varchild) "0")
316 (gdb-enqueue-input
317 (list
318 (concat
319 "server interpreter mi \"-var-evaluate-expression "
320 (nth 1 varchild) "\"\n")
321 `(lambda () (gdb-var-evaluate-expression-handler
322 ,(nth 1 varchild) nil))))))))
323 (push var var-list)))
324 (setq gdb-var-list (nreverse var-list))))))
325
326 (defun gdb-var-update ()
327 (if (not (member 'gdb-var-update gdb-pending-triggers))
328 (progn
329 (gdb-enqueue-input
330 (list
331 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
332 "server interpreter mi \"-var-update *\"\n"
333 "-var-update *\n")
334 'gdb-var-update-handler))
335 (push 'gdb-var-update gdb-pending-triggers))))
336
337 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\"")
338
339 (defun gdb-var-update-handler ()
340 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
341 (goto-char (point-min))
342 (while (re-search-forward gdb-var-update-regexp nil t)
343 (let ((varnum (match-string 1)))
344 (gdb-enqueue-input
345 (list
346 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
347 (concat "server interpreter mi \"-var-evaluate-expression "
348 varnum "\"\n")
349 (concat "-var-evaluate-expression " varnum "\n"))
350 `(lambda () (gdb-var-evaluate-expression-handler
351 ,varnum t)))))))
352 (setq gdb-pending-triggers
353 (delq 'gdb-var-update gdb-pending-triggers)))
354
355 (defun gdb-var-delete ()
356 "Delete watched expression from the speedbar."
357 (interactive)
358 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
359 (let ((text (speedbar-line-text)))
360 (string-match "\\(\\S-+\\)" text)
361 (let* ((expr (match-string 1 text))
362 (var (assoc expr gdb-var-list))
363 (varnum (cadr var)))
364 (unless (string-match "\\." varnum)
365 (gdb-enqueue-input
366 (list
367 (if (with-current-buffer gud-comint-buffer
368 (eq gud-minor-mode 'gdba))
369 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
370 (concat "-var-delete " varnum "\n"))
371 'ignore))
372 (setq gdb-var-list (delq var gdb-var-list))
373 (dolist (varchild gdb-var-list)
374 (if (string-match (concat (nth 1 var) "\\.") (nth 1 varchild))
375 (setq gdb-var-list (delq varchild gdb-var-list))))
376 (setq gdb-var-changed t))))))
377
378 (defun gdb-edit-value (text token indent)
379 "Assign a value to a variable displayed in the speedbar"
380 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
381 (varnum (cadr var)) (value))
382 (setq value (read-string "New value: "))
383 (gdb-enqueue-input
384 (list
385 (if (with-current-buffer gud-comint-buffer
386 (eq gud-minor-mode 'gdba))
387 (concat "server interpreter mi \"-var-assign " varnum " " value "\"\n")
388 (concat "-var-assign " varnum " " value "\n"))
389 'ignore))))
390
391 (defcustom gdb-show-changed-values t
392 "Non-nil means use font-lock-warning-face to display values that have
393 recently changed in the speedbar."
394 :type 'boolean
395 :group 'gud)
396
397 (defun gdb-speedbar-expand-node (text token indent)
398 "Expand the node the user clicked on.
399 TEXT is the text of the button we clicked on, a + or - item.
400 TOKEN is data related to this node.
401 INDENT is the current indentation depth."
402 (cond ((string-match "+" text) ;expand this node
403 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
404 (gdb-var-list-children token)
405 (gdbmi-var-list-children token)))
406 ((string-match "-" text) ;contract this node
407 (dolist (var gdb-var-list)
408 (if (string-match (concat token "\\.") (nth 1 var))
409 (setq gdb-var-list (delq var gdb-var-list))))
410 (setq gdb-var-changed t))))
411 \f
412 (defvar gdb-buffer-type nil
413 "One of the symbols bound in `gdb-buffer-rules'.")
414
415 (defvar gdb-input-queue ()
416 "A list of gdb command objects.")
417
418 (defvar gdb-prompting nil
419 "True when gdb is idle with no pending input.")
420
421 (defvar gdb-output-sink 'user
422 "The disposition of the output of the current gdb command.
423 Possible values are these symbols:
424
425 user -- gdb output should be copied to the GUD buffer
426 for the user to see.
427
428 inferior -- gdb output should be copied to the inferior-io buffer
429
430 pre-emacs -- output should be ignored util the post-prompt
431 annotation is received. Then the output-sink
432 becomes:...
433 emacs -- output should be collected in the partial-output-buffer
434 for subsequent processing by a command. This is the
435 disposition of output generated by commands that
436 gdb mode sends to gdb on its own behalf.
437 post-emacs -- ignore output until the prompt annotation is
438 received, then go to USER disposition.
439
440 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
441 (user and emacs).")
442
443 (defvar gdb-current-item nil
444 "The most recent command item sent to gdb.")
445
446 (defvar gdb-pending-triggers '()
447 "A list of trigger functions that have run later than their output
448 handlers.")
449
450 ;; end of gdb variables
451
452 (defun gdb-get-target-string ()
453 (with-current-buffer gud-comint-buffer
454 gud-target-name))
455 \f
456
457 ;;
458 ;; gdb buffers.
459 ;;
460 ;; Each buffer has a TYPE -- a symbol that identifies the function
461 ;; of that particular buffer.
462 ;;
463 ;; The usual gdb interaction buffer is given the type `gdba' and
464 ;; is constructed specially.
465 ;;
466 ;; Others are constructed by gdb-get-create-buffer and
467 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
468
469 (defvar gdb-buffer-rules-assoc '())
470
471 (defun gdb-get-buffer (key)
472 "Return the gdb buffer tagged with type KEY.
473 The key should be one of the cars in `gdb-buffer-rules-assoc'."
474 (save-excursion
475 (gdb-look-for-tagged-buffer key (buffer-list))))
476
477 (defun gdb-get-create-buffer (key)
478 "Create a new gdb buffer of the type specified by KEY.
479 The key should be one of the cars in `gdb-buffer-rules-assoc'."
480 (or (gdb-get-buffer key)
481 (let* ((rules (assoc key gdb-buffer-rules-assoc))
482 (name (funcall (gdb-rules-name-maker rules)))
483 (new (get-buffer-create name)))
484 (with-current-buffer new
485 ;; FIXME: This should be set after calling the function, since the
486 ;; function should run kill-all-local-variables.
487 (set (make-local-variable 'gdb-buffer-type) key)
488 (if (cdr (cdr rules))
489 (funcall (car (cdr (cdr rules)))))
490 (set (make-local-variable 'gud-minor-mode)
491 (with-current-buffer gud-comint-buffer gud-minor-mode))
492 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
493 new))))
494
495 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
496
497 (defun gdb-look-for-tagged-buffer (key bufs)
498 (let ((retval nil))
499 (while (and (not retval) bufs)
500 (set-buffer (car bufs))
501 (if (eq gdb-buffer-type key)
502 (setq retval (car bufs)))
503 (setq bufs (cdr bufs)))
504 retval))
505
506 ;;
507 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
508 ;; at least one and possible more functions. The functions have these
509 ;; roles in defining a buffer type:
510 ;;
511 ;; NAME - Return a name for this buffer type.
512 ;;
513 ;; The remaining function(s) are optional:
514 ;;
515 ;; MODE - called in a new buffer with no arguments, should establish
516 ;; the proper mode for the buffer.
517 ;;
518
519 (defun gdb-set-buffer-rules (buffer-type &rest rules)
520 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
521 (if binding
522 (setcdr binding rules)
523 (push (cons buffer-type rules)
524 gdb-buffer-rules-assoc))))
525
526 ;; GUD buffers are an exception to the rules
527 (gdb-set-buffer-rules 'gdba 'error)
528
529 ;;
530 ;; Partial-output buffer : This accumulates output from a command executed on
531 ;; behalf of emacs (rather than the user).
532 ;;
533 (gdb-set-buffer-rules 'gdb-partial-output-buffer
534 'gdb-partial-output-name)
535
536 (defun gdb-partial-output-name ()
537 (concat "*partial-output-"
538 (gdb-get-target-string)
539 "*"))
540
541 \f
542 (gdb-set-buffer-rules 'gdb-inferior-io
543 'gdb-inferior-io-name
544 'gdb-inferior-io-mode)
545
546 (defun gdb-inferior-io-name ()
547 (concat "*input/output of "
548 (gdb-get-target-string)
549 "*"))
550
551 (defvar gdb-inferior-io-mode-map
552 (let ((map (make-sparse-keymap)))
553 (define-key map "\C-c\C-c" 'gdb-inferior-io-interrupt)
554 (define-key map "\C-c\C-z" 'gdb-inferior-io-stop)
555 (define-key map "\C-c\C-\\" 'gdb-inferior-io-quit)
556 (define-key map "\C-c\C-d" 'gdb-inferior-io-eof)
557 map))
558
559 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
560 "Major mode for gdb inferior-io."
561 :syntax-table nil :abbrev-table nil
562 ;; We want to use comint because it has various nifty and familiar
563 ;; features. We don't need a process, but comint wants one, so create
564 ;; a dummy one.
565 (make-comint-in-buffer
566 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
567 (current-buffer) "hexl")
568 (setq comint-input-sender 'gdb-inferior-io-sender))
569
570 (defun gdb-inferior-io-sender (proc string)
571 ;; PROC is the pseudo-process created to satisfy comint.
572 (with-current-buffer (process-buffer proc)
573 (setq proc (get-buffer-process gud-comint-buffer))
574 (process-send-string proc string)
575 (process-send-string proc "\n")))
576
577 (defun gdb-inferior-io-interrupt ()
578 "Interrupt the program being debugged."
579 (interactive)
580 (interrupt-process
581 (get-buffer-process gud-comint-buffer) comint-ptyp))
582
583 (defun gdb-inferior-io-quit ()
584 "Send quit signal to the program being debugged."
585 (interactive)
586 (quit-process
587 (get-buffer-process gud-comint-buffer) comint-ptyp))
588
589 (defun gdb-inferior-io-stop ()
590 "Stop the program being debugged."
591 (interactive)
592 (stop-process
593 (get-buffer-process gud-comint-buffer) comint-ptyp))
594
595 (defun gdb-inferior-io-eof ()
596 "Send end-of-file to the program being debugged."
597 (interactive)
598 (process-send-eof
599 (get-buffer-process gud-comint-buffer)))
600 \f
601
602 ;;
603 ;; gdb communications
604 ;;
605
606 ;; INPUT: things sent to gdb
607 ;;
608 ;; The queues are lists. Each element is either a string (indicating user or
609 ;; user-like input) or a list of the form:
610 ;;
611 ;; (INPUT-STRING HANDLER-FN)
612 ;;
613 ;; The handler function will be called from the partial-output buffer when the
614 ;; command completes. This is the way to write commands which invoke gdb
615 ;; commands autonomously.
616 ;;
617 ;; These lists are consumed tail first.
618 ;;
619
620 (defun gdb-send (proc string)
621 "A comint send filter for gdb.
622 This filter may simply queue output for a later time."
623 (if gud-running
624 (process-send-string proc (concat string "\n"))
625 (gdb-enqueue-input (concat string "\n"))))
626
627 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
628 ;; is a query, or other non-top-level prompt.
629
630 (defun gdb-enqueue-input (item)
631 (if gdb-prompting
632 (progn
633 (gdb-send-item item)
634 (setq gdb-prompting nil))
635 (push item gdb-input-queue)))
636
637 (defun gdb-dequeue-input ()
638 (let ((queue gdb-input-queue))
639 (and queue
640 (let ((last (car (last queue))))
641 (unless (nbutlast queue) (setq gdb-input-queue '()))
642 last))))
643 \f
644 ;;
645 ;; output -- things gdb prints to emacs
646 ;;
647 ;; GDB output is a stream interrupted by annotations.
648 ;; Annotations can be recognized by their beginning
649 ;; with \C-j\C-z\C-z<tag><opt>\C-j
650 ;;
651 ;; The tag is a string obeying symbol syntax.
652 ;;
653 ;; The optional part `<opt>' can be either the empty string
654 ;; or a space followed by more data relating to the annotation.
655 ;; For example, the SOURCE annotation is followed by a filename,
656 ;; line number and various useless goo. This data must not include
657 ;; any newlines.
658 ;;
659
660 (defcustom gud-gdba-command-name "gdb -annotate=3"
661 "Default command to execute an executable under the GDB-UI debugger."
662 :type 'string
663 :group 'gud)
664
665 (defvar gdb-annotation-rules
666 '(("pre-prompt" gdb-pre-prompt)
667 ("prompt" gdb-prompt)
668 ("commands" gdb-subprompt)
669 ("overload-choice" gdb-subprompt)
670 ("query" gdb-subprompt)
671 ;; Need this prompt for GDB 6.1
672 ("nquery" gdb-subprompt)
673 ("prompt-for-continue" gdb-subprompt)
674 ("post-prompt" gdb-post-prompt)
675 ("source" gdb-source)
676 ("starting" gdb-starting)
677 ("exited" gdb-stopping)
678 ("signalled" gdb-stopping)
679 ("signal" gdb-stopping)
680 ("breakpoint" gdb-stopping)
681 ("watchpoint" gdb-stopping)
682 ("frame-begin" gdb-frame-begin)
683 ("stopped" gdb-stopped)
684 ) "An assoc mapping annotation tags to functions which process them.")
685
686 (defconst gdb-source-spec-regexp
687 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:\\(0x[a-f0-9]*\\)")
688
689 ;; Do not use this except as an annotation handler.
690 (defun gdb-source (args)
691 (string-match gdb-source-spec-regexp args)
692 ;; Extract the frame position from the marker.
693 (setq gud-last-frame
694 (cons
695 (match-string 1 args)
696 (string-to-int (match-string 2 args))))
697 (setq gdb-current-address (match-string 3 args))
698 (setq gdb-view-source t)
699 ;; cover for auto-display output which comes *before*
700 ;; stopped annotation
701 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
702
703 (defun gdb-send-item (item)
704 (if gdb-enable-debug-log (push (cons 'send item) gdb-debug-log))
705 (setq gdb-current-item item)
706 (with-current-buffer gud-comint-buffer
707 (if (eq gud-minor-mode 'gdba)
708 (progn
709 (if (stringp item)
710 (progn
711 (setq gdb-output-sink 'user)
712 (process-send-string (get-buffer-process gud-comint-buffer) item))
713 (progn
714 (gdb-clear-partial-output)
715 (setq gdb-output-sink 'pre-emacs)
716 (process-send-string (get-buffer-process gud-comint-buffer)
717 (car item)))))
718 ; case: eq gud-minor-mode 'gdbmi
719 (gdb-clear-partial-output)
720 (setq gdb-output-sink 'emacs)
721 (process-send-string (get-buffer-process gud-comint-buffer)
722 (car item)))))
723
724 (defun gdb-pre-prompt (ignored)
725 "An annotation handler for `pre-prompt'. This terminates the collection of
726 output from a previous command if that happens to be in effect."
727 (let ((sink gdb-output-sink))
728 (cond
729 ((eq sink 'user) t)
730 ((eq sink 'emacs)
731 (setq gdb-output-sink 'post-emacs))
732 (t
733 (setq gdb-output-sink 'user)
734 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
735
736 (defun gdb-prompt (ignored)
737 "An annotation handler for `prompt'.
738 This sends the next command (if any) to gdb."
739 (when gdb-first-prompt (gdb-ann3))
740 (let ((sink gdb-output-sink))
741 (cond
742 ((eq sink 'user) t)
743 ((eq sink 'post-emacs)
744 (setq gdb-output-sink 'user)
745 (let ((handler
746 (car (cdr gdb-current-item))))
747 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
748 (funcall handler))))
749 (t
750 (setq gdb-output-sink 'user)
751 (error "Phase error in gdb-prompt (got %s)" sink))))
752 (let ((input (gdb-dequeue-input)))
753 (if input
754 (gdb-send-item input)
755 (progn
756 (setq gdb-prompting t)
757 (gud-display-frame)))))
758
759 (defun gdb-subprompt (ignored)
760 "An annotation handler for non-top-level prompts."
761 (setq gdb-prompting t))
762
763 (defun gdb-starting (ignored)
764 "An annotation handler for `starting'. This says that I/O for the
765 subprocess is now the program being debugged, not GDB."
766 (let ((sink gdb-output-sink))
767 (cond
768 ((eq sink 'user)
769 (progn
770 (setq gud-running t)
771 (if gdb-use-inferior-io-buffer
772 (setq gdb-output-sink 'inferior))))
773 (t (error "Unexpected `starting' annotation")))))
774
775 (defun gdb-stopping (ignored)
776 "An annotation handler for `exited' and other annotations which say that I/O
777 for the subprocess is now GDB, not the program being debugged."
778 (if gdb-use-inferior-io-buffer
779 (let ((sink gdb-output-sink))
780 (cond
781 ((eq sink 'inferior)
782 (setq gdb-output-sink 'user))
783 (t (error "Unexpected stopping annotation"))))))
784
785 (defun gdb-frame-begin (ignored)
786 (let ((sink gdb-output-sink))
787 (cond
788 ((eq sink 'inferior)
789 (setq gdb-output-sink 'user))
790 ((eq sink 'user) t)
791 ((eq sink 'emacs) t)
792 (t (error "Unexpected frame-begin annotation (%S)" sink)))))
793
794 (defun gdb-stopped (ignored)
795 "An annotation handler for `stopped'. It is just like gdb-stopping, except
796 that if we already set the output sink to 'user in gdb-stopping, that is fine."
797 (setq gud-running nil)
798 (let ((sink gdb-output-sink))
799 (cond
800 ((eq sink 'inferior)
801 (setq gdb-output-sink 'user))
802 ((eq sink 'user) t)
803 (t (error "Unexpected stopped annotation")))))
804
805 (defun gdb-post-prompt (ignored)
806 "An annotation handler for `post-prompt'. This begins the collection of
807 output from the current command if that happens to be appropriate."
808 (if (not gdb-pending-triggers)
809 (progn
810 (gdb-get-current-frame)
811 (gdb-invalidate-frames)
812 (gdb-invalidate-breakpoints)
813 (gdb-invalidate-assembler)
814 (gdb-invalidate-registers)
815 (gdb-invalidate-locals)
816 (gdb-invalidate-threads)
817 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
818 ;; FIXME: with GDB-6 on Darwin, this might very well work.
819 (dolist (frame (frame-list))
820 (when (string-equal (frame-parameter frame 'name) "Speedbar")
821 (setq gdb-var-changed t) ; force update
822 (dolist (var gdb-var-list)
823 (setcar (nthcdr 5 var) nil))))
824 (gdb-var-update))))
825 (let ((sink gdb-output-sink))
826 (cond
827 ((eq sink 'user) t)
828 ((eq sink 'pre-emacs)
829 (setq gdb-output-sink 'emacs))
830 (t
831 (setq gdb-output-sink 'user)
832 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
833
834 (defun gud-gdba-marker-filter (string)
835 "A gud marker filter for gdb. Handle a burst of output from GDB."
836 (if gdb-enable-debug-log (push (cons 'recv string) gdb-debug-log))
837 ;; Recall the left over gud-marker-acc from last time
838 (setq gud-marker-acc (concat gud-marker-acc string))
839 ;; Start accumulating output for the GUD buffer
840 (let ((output ""))
841 ;;
842 ;; Process all the complete markers in this chunk.
843 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
844 (let ((annotation (match-string 1 gud-marker-acc)))
845 ;;
846 ;; Stuff prior to the match is just ordinary output.
847 ;; It is either concatenated to OUTPUT or directed
848 ;; elsewhere.
849 (setq output
850 (gdb-concat-output
851 output
852 (substring gud-marker-acc 0 (match-beginning 0))))
853 ;;
854 ;; Take that stuff off the gud-marker-acc.
855 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
856 ;;
857 ;; Parse the tag from the annotation, and maybe its arguments.
858 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
859 (let* ((annotation-type (match-string 1 annotation))
860 (annotation-arguments (match-string 2 annotation))
861 (annotation-rule (assoc annotation-type
862 gdb-annotation-rules)))
863 ;; Call the handler for this annotation.
864 (if annotation-rule
865 (funcall (car (cdr annotation-rule))
866 annotation-arguments)
867 ;; Else the annotation is not recognized. Ignore it silently,
868 ;; so that GDB can add new annotations without causing
869 ;; us to blow up.
870 ))))
871 ;;
872 ;; Does the remaining text end in a partial line?
873 ;; If it does, then keep part of the gud-marker-acc until we get more.
874 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
875 gud-marker-acc)
876 (progn
877 ;; Everything before the potential marker start can be output.
878 (setq output
879 (gdb-concat-output output
880 (substring gud-marker-acc 0
881 (match-beginning 0))))
882 ;;
883 ;; Everything after, we save, to combine with later input.
884 (setq gud-marker-acc (substring gud-marker-acc (match-beginning 0))))
885 ;;
886 ;; In case we know the gud-marker-acc contains no partial annotations:
887 (progn
888 (setq output (gdb-concat-output output gud-marker-acc))
889 (setq gud-marker-acc "")))
890 output))
891
892 (defun gdb-concat-output (so-far new)
893 (let ((sink gdb-output-sink))
894 (cond
895 ((eq sink 'user) (concat so-far new))
896 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
897 ((eq sink 'emacs)
898 (gdb-append-to-partial-output new)
899 so-far)
900 ((eq sink 'inferior)
901 (gdb-append-to-inferior-io new)
902 so-far)
903 (t (error "Bogon output sink %S" sink)))))
904
905 (defun gdb-append-to-partial-output (string)
906 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
907 (goto-char (point-max))
908 (insert string)))
909
910 (defun gdb-clear-partial-output ()
911 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
912 (erase-buffer)))
913
914 (defun gdb-append-to-inferior-io (string)
915 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
916 (goto-char (point-max))
917 (insert-before-markers string))
918 (if (not (string-equal string ""))
919 (gdb-display-buffer (gdb-get-create-buffer 'gdb-inferior-io))))
920
921 (defun gdb-clear-inferior-io ()
922 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
923 (erase-buffer)))
924 \f
925
926 ;; One trick is to have a command who's output is always available in a buffer
927 ;; of it's own, and is always up to date. We build several buffers of this
928 ;; type.
929 ;;
930 ;; There are two aspects to this: gdb has to tell us when the output for that
931 ;; command might have changed, and we have to be able to run the command
932 ;; behind the user's back.
933 ;;
934 ;; The output phasing associated with the variable gdb-output-sink
935 ;; help us to run commands behind the user's back.
936 ;;
937 ;; Below is the code for specificly managing buffers of output from one
938 ;; command.
939 ;;
940
941 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
942 ;; It adds an input for the command we are tracking. It should be the
943 ;; annotation rule binding of whatever gdb sends to tell us this command
944 ;; might have changed it's output.
945 ;;
946 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
947 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
948 ;; input in the input queue (see comment about ``gdb communications'' above).
949
950 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
951 output-handler)
952 `(defun ,name (&optional ignored)
953 (if (and (,demand-predicate)
954 (not (member ',name
955 gdb-pending-triggers)))
956 (progn
957 (gdb-enqueue-input
958 (list ,gdb-command ',output-handler))
959 (push ',name gdb-pending-triggers)))))
960
961 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
962 `(defun ,name ()
963 (setq gdb-pending-triggers
964 (delq ',trigger
965 gdb-pending-triggers))
966 (let ((buf (gdb-get-buffer ',buf-key)))
967 (and buf
968 (with-current-buffer buf
969 (let ((p (point))
970 (buffer-read-only nil))
971 (erase-buffer)
972 (insert-buffer-substring (gdb-get-create-buffer
973 'gdb-partial-output-buffer))
974 (goto-char p)))))
975 ;; put customisation here
976 (,custom-defun)))
977
978 (defmacro def-gdb-auto-updated-buffer (buffer-key trigger-name gdb-command
979 output-handler-name custom-defun)
980 `(progn
981 (def-gdb-auto-update-trigger ,trigger-name
982 ;; The demand predicate:
983 (lambda () (gdb-get-buffer ',buffer-key))
984 ,gdb-command
985 ,output-handler-name)
986 (def-gdb-auto-update-handler ,output-handler-name
987 ,trigger-name ,buffer-key ,custom-defun)))
988
989 \f
990 ;;
991 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
992 ;;
993 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
994 'gdb-breakpoints-buffer-name
995 'gdb-breakpoints-mode)
996
997 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
998 ;; This defines the auto update rule for buffers of type
999 ;; `gdb-breakpoints-buffer'.
1000 ;;
1001 ;; It defines a function to serve as the annotation handler that
1002 ;; handles the `foo-invalidated' message. That function is called:
1003 gdb-invalidate-breakpoints
1004 ;;
1005 ;; To update the buffer, this command is sent to gdb.
1006 "server info breakpoints\n"
1007 ;;
1008 ;; This also defines a function to be the handler for the output
1009 ;; from the command above. That function will copy the output into
1010 ;; the appropriately typed buffer. That function will be called:
1011 gdb-info-breakpoints-handler
1012 ;; buffer specific functions
1013 gdb-info-breakpoints-custom)
1014
1015 (defvar gdb-cdir nil "Compilation directory.")
1016
1017 (defconst breakpoint-xpm-data "/* XPM */
1018 static char *magick[] = {
1019 /* columns rows colors chars-per-pixel */
1020 \"10 10 2 1\",
1021 \" c red\",
1022 \"+ c None\",
1023 /* pixels */
1024 \"+++ +++\",
1025 \"++ ++\",
1026 \"+ +\",
1027 \" \",
1028 \" \",
1029 \" \",
1030 \" \",
1031 \"+ +\",
1032 \"++ ++\",
1033 \"+++ +++\",
1034 };"
1035 "XPM data used for breakpoint icon.")
1036
1037 (defconst breakpoint-enabled-pbm-data
1038 "P1
1039 10 10\",
1040 0 0 0 0 1 1 1 1 0 0 0 0
1041 0 0 0 1 1 1 1 1 1 0 0 0
1042 0 0 1 1 1 1 1 1 1 1 0 0
1043 0 1 1 1 1 1 1 1 1 1 1 0
1044 0 1 1 1 1 1 1 1 1 1 1 0
1045 0 1 1 1 1 1 1 1 1 1 1 0
1046 0 1 1 1 1 1 1 1 1 1 1 0
1047 0 0 1 1 1 1 1 1 1 1 0 0
1048 0 0 0 1 1 1 1 1 1 0 0 0
1049 0 0 0 0 1 1 1 1 0 0 0 0"
1050 "PBM data used for enabled breakpoint icon.")
1051
1052 (defconst breakpoint-disabled-pbm-data
1053 "P1
1054 10 10\",
1055 0 0 1 0 1 0 1 0 0 0
1056 0 1 0 1 0 1 0 1 0 0
1057 1 0 1 0 1 0 1 0 1 0
1058 0 1 0 1 0 1 0 1 0 1
1059 1 0 1 0 1 0 1 0 1 0
1060 0 1 0 1 0 1 0 1 0 1
1061 1 0 1 0 1 0 1 0 1 0
1062 0 1 0 1 0 1 0 1 0 1
1063 0 0 1 0 1 0 1 0 1 0
1064 0 0 0 1 0 1 0 1 0 0"
1065 "PBM data used for disabled breakpoint icon.")
1066
1067 (defvar breakpoint-enabled-icon nil
1068 "Icon for enabled breakpoint in display margin")
1069
1070 (defvar breakpoint-disabled-icon nil
1071 "Icon for disabled breakpoint in display margin")
1072
1073 ;; Bitmap for breakpoint in fringe
1074 (define-fringe-bitmap 'breakpoint
1075 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1076
1077 (defface breakpoint-enabled-bitmap-face
1078 '((t
1079 :inherit fringe
1080 :foreground "red"))
1081 "Face for enabled breakpoint icon in fringe."
1082 :group 'gud)
1083
1084 (defface breakpoint-disabled-bitmap-face
1085 '((t
1086 :inherit fringe
1087 :foreground "grey60"))
1088 "Face for disabled breakpoint icon in fringe."
1089 :group 'gud)
1090
1091
1092 ;;-put breakpoint icons in relevant margins (even those set in the GUD buffer)
1093 (defun gdb-info-breakpoints-custom ()
1094 (let ((flag)(address))
1095 ;;
1096 ;; remove all breakpoint-icons in source buffers but not assembler buffer
1097 (dolist (buffer (buffer-list))
1098 (with-current-buffer buffer
1099 (if (and (eq gud-minor-mode 'gdba)
1100 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1101 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1102 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1103 (save-excursion
1104 (goto-char (point-min))
1105 (while (< (point) (- (point-max) 1))
1106 (forward-line 1)
1107 (if (looking-at "[^\t].*breakpoint")
1108 (progn
1109 (looking-at "[0-9]*\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)")
1110 (setq flag (char-after (match-beginning 1)))
1111 (beginning-of-line)
1112 (if (re-search-forward "in\\s-+\\S-+\\s-+at\\s-+" nil t)
1113 (progn
1114 (looking-at "\\(\\S-*\\):\\([0-9]+\\)")
1115 (let ((line (match-string 2)) (buffer-read-only nil)
1116 (file (match-string 1)))
1117 (add-text-properties (point-at-bol) (point-at-eol)
1118 '(mouse-face highlight
1119 help-echo "mouse-2, RET: visit breakpoint"))
1120 (with-current-buffer
1121 (find-file-noselect
1122 (if (file-exists-p file) file
1123 (expand-file-name file gdb-cdir)))
1124 (save-current-buffer
1125 (set (make-local-variable 'gud-minor-mode) 'gdba)
1126 (set (make-local-variable 'tool-bar-map)
1127 gud-tool-bar-map))
1128 ;; only want one breakpoint icon at each location
1129 (save-excursion
1130 (goto-line (string-to-number line))
1131 (gdb-put-breakpoint-icon (eq flag ?y)))))))))
1132 (end-of-line)))))
1133 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1134
1135 (defun gdb-mouse-toggle-breakpoint (event)
1136 "Toggle breakpoint in left fringe/margin with mouse click"
1137 (interactive "e")
1138 (mouse-minibuffer-check event)
1139 (let ((posn (event-end event)))
1140 (if (numberp (posn-point posn))
1141 (with-selected-window (posn-window posn)
1142 (save-excursion
1143 (goto-char (posn-point posn))
1144 (if (or (posn-object posn)
1145 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1146 'breakpoint))
1147 (gud-remove nil)
1148 (gud-break nil)))))))
1149
1150 (defun gdb-breakpoints-buffer-name ()
1151 (with-current-buffer gud-comint-buffer
1152 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1153
1154 (defun gdb-display-breakpoints-buffer ()
1155 "Display status of user-settable breakpoints."
1156 (interactive)
1157 (gdb-display-buffer
1158 (gdb-get-create-buffer 'gdb-breakpoints-buffer)))
1159
1160 (defconst gdb-frame-parameters
1161 '((height . 12) (width . 60)
1162 (unsplittable . t)
1163 (tool-bar-lines . nil)
1164 (menu-bar-lines . nil)
1165 (minibuffer . nil)))
1166
1167 (defun gdb-frame-breakpoints-buffer ()
1168 "Display status of user-settable breakpoints in a new frame."
1169 (interactive)
1170 (select-frame (make-frame gdb-frame-parameters))
1171 (switch-to-buffer (gdb-get-create-buffer 'gdb-breakpoints-buffer))
1172 (set-window-dedicated-p (selected-window) t))
1173
1174 (defvar gdb-breakpoints-mode-map
1175 (let ((map (make-sparse-keymap))
1176 (menu (make-sparse-keymap "Breakpoints")))
1177 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1178 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1179 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1180
1181 (suppress-keymap map)
1182 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1183 (define-key map " " 'gdb-toggle-breakpoint)
1184 (define-key map "d" 'gdb-delete-breakpoint)
1185 (define-key map "\r" 'gdb-goto-breakpoint)
1186 (define-key map [mouse-2] 'gdb-mouse-goto-breakpoint)
1187 map))
1188
1189 (defun gdb-breakpoints-mode ()
1190 "Major mode for gdb breakpoints.
1191
1192 \\{gdb-breakpoints-mode-map}"
1193 (setq major-mode 'gdb-breakpoints-mode)
1194 (setq mode-name "Breakpoints")
1195 (use-local-map gdb-breakpoints-mode-map)
1196 (setq buffer-read-only t)
1197 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1198 (gdb-invalidate-breakpoints)
1199 (gdbmi-invalidate-breakpoints)))
1200
1201 (defun gdb-toggle-breakpoint ()
1202 "Enable/disable the breakpoint at current line."
1203 (interactive)
1204 (save-excursion
1205 (beginning-of-line 1)
1206 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1207 (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)")
1208 (looking-at
1209 "\\([0-9]+\\)\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)\\s-*\\S-*\\s-*\\S-*:[0-9]+"))
1210 (gdb-enqueue-input
1211 (list
1212 (concat gdb-server-prefix
1213 (if (eq ?y (char-after (match-beginning 2)))
1214 "disable "
1215 "enable ")
1216 (match-string 1) "\n") 'ignore))
1217 (error "Not recognized as break/watchpoint line"))))
1218
1219 (defun gdb-delete-breakpoint ()
1220 "Delete the breakpoint at current line."
1221 (interactive)
1222 (beginning-of-line 1)
1223 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1224 (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)")
1225 (looking-at
1226 "\\([0-9]+\\)\\s-*\\S-*\\s-*\\S-*\\s-*.\\s-*\\S-*\\s-*\\S-*:[0-9]+"))
1227 (gdb-enqueue-input
1228 (list
1229 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1230 (error "Not recognized as break/watchpoint line")))
1231
1232 (defun gdb-goto-breakpoint ()
1233 "Display the breakpoint location specified at current line."
1234 (interactive)
1235 (save-excursion
1236 (beginning-of-line 1)
1237 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1238 (looking-at ".*in\\s-+\\S-+\\s-+at\\s-+\\(\\S-*\\):\\([0-9]+\\)")
1239 (looking-at
1240 "[0-9]+\\s-*\\S-*\\s-*\\S-*\\s-*.\\s-*\\S-*\\s-*\\(\\S-*\\):\\([0-9]+\\)"))
1241 (let ((line (match-string 2))
1242 (file (match-string 1)))
1243 (save-selected-window
1244 (let* ((buf (find-file-noselect (if (file-exists-p file)
1245 file
1246 (expand-file-name file gdb-cdir))))
1247 (window (gdb-display-buffer buf)))
1248 (with-current-buffer buf
1249 (goto-line (string-to-number line))
1250 (set-window-point window (point))))))
1251 (error "Not recognized as break/watchpoint line"))))
1252
1253 (defun gdb-mouse-goto-breakpoint (event)
1254 "Display the breakpoint location that you click on."
1255 (interactive "e")
1256 (mouse-set-point event)
1257 (gdb-goto-breakpoint))
1258 \f
1259 ;;
1260 ;; Frames buffer. This displays a perpetually correct bactracktrace
1261 ;; (from the command `where').
1262 ;;
1263 ;; Alas, if your stack is deep, it is costly.
1264 ;;
1265 (gdb-set-buffer-rules 'gdb-stack-buffer
1266 'gdb-stack-buffer-name
1267 'gdb-frames-mode)
1268
1269 (def-gdb-auto-updated-buffer gdb-stack-buffer
1270 gdb-invalidate-frames
1271 "server where\n"
1272 gdb-info-frames-handler
1273 gdb-info-frames-custom)
1274
1275 (defun gdb-info-frames-custom ()
1276 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1277 (save-excursion
1278 (let ((buffer-read-only nil))
1279 (goto-char (point-min))
1280 (while (< (point) (point-max))
1281 (add-text-properties (point-at-bol) (point-at-eol)
1282 '(mouse-face highlight
1283 help-echo "mouse-2, RET: Select frame"))
1284 (beginning-of-line)
1285 (when (and (or (looking-at "^#[0-9]*\\s-*\\S-* in \\(\\S-*\\)")
1286 (looking-at "^#[0-9]*\\s-*\\(\\S-*\\)"))
1287 (equal (match-string 1) gdb-current-frame))
1288 (put-text-property (point-at-bol) (point-at-eol)
1289 'face '(:inverse-video t)))
1290 (forward-line 1))))))
1291
1292 (defun gdb-stack-buffer-name ()
1293 (with-current-buffer gud-comint-buffer
1294 (concat "*stack frames of " (gdb-get-target-string) "*")))
1295
1296 (defun gdb-display-stack-buffer ()
1297 "Display backtrace of current stack."
1298 (interactive)
1299 (gdb-display-buffer
1300 (gdb-get-create-buffer 'gdb-stack-buffer)))
1301
1302 (defun gdb-frame-stack-buffer ()
1303 "Display backtrace of current stack in a new frame."
1304 (interactive)
1305 (select-frame (make-frame gdb-frame-parameters))
1306 (switch-to-buffer (gdb-get-create-buffer 'gdb-stack-buffer))
1307 (set-window-dedicated-p (selected-window) t))
1308
1309 (defvar gdb-frames-mode-map
1310 (let ((map (make-sparse-keymap)))
1311 (suppress-keymap map)
1312 (define-key map "\r" 'gdb-frames-select)
1313 (define-key map [mouse-2] 'gdb-frames-mouse-select)
1314 map))
1315
1316 (defun gdb-frames-mode ()
1317 "Major mode for gdb frames.
1318
1319 \\{gdb-frames-mode-map}"
1320 (setq major-mode 'gdb-frames-mode)
1321 (setq mode-name "Frames")
1322 (setq buffer-read-only t)
1323 (use-local-map gdb-frames-mode-map)
1324 (font-lock-mode -1)
1325 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1326 (gdb-invalidate-frames)
1327 (gdbmi-invalidate-frames)))
1328
1329 (defun gdb-get-frame-number ()
1330 (save-excursion
1331 (let* ((pos (re-search-backward "^#*\\([0-9]*\\)" nil t))
1332 (n (or (and pos (match-string-no-properties 1)) "0")))
1333 n)))
1334
1335 (defun gdb-frames-select ()
1336 "Select the frame and display the relevant source."
1337 (interactive)
1338 (gdb-enqueue-input
1339 (list (concat gdb-server-prefix "frame " (gdb-get-frame-number) "\n") 'ignore))
1340 (gud-display-frame))
1341
1342 (defun gdb-frames-mouse-select (event)
1343 "Select the frame you click on and display the relevant source."
1344 (interactive "e")
1345 (mouse-set-point event)
1346 (gdb-frames-select))
1347 \f
1348 ;;
1349 ;; Threads buffer. This displays a selectable thread list.
1350 ;;
1351 (gdb-set-buffer-rules 'gdb-threads-buffer
1352 'gdb-threads-buffer-name
1353 'gdb-threads-mode)
1354
1355 (def-gdb-auto-updated-buffer gdb-threads-buffer
1356 gdb-invalidate-threads
1357 (concat gdb-server-prefix "info threads\n")
1358 gdb-info-threads-handler
1359 gdb-info-threads-custom)
1360
1361 (defun gdb-info-threads-custom ()
1362 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1363 (let ((buffer-read-only nil))
1364 (goto-char (point-min))
1365 (while (< (point) (point-max))
1366 (add-text-properties (point-at-bol) (point-at-eol)
1367 '(mouse-face highlight
1368 help-echo "mouse-2, RET: select thread"))
1369 (forward-line 1)))))
1370
1371 (defun gdb-threads-buffer-name ()
1372 (with-current-buffer gud-comint-buffer
1373 (concat "*threads of " (gdb-get-target-string) "*")))
1374
1375 (defun gdb-display-threads-buffer ()
1376 "Display IDs of currently known threads."
1377 (interactive)
1378 (gdb-display-buffer
1379 (gdb-get-create-buffer 'gdb-threads-buffer)))
1380
1381 (defun gdb-frame-threads-buffer ()
1382 "Display IDs of currently known threads in a new frame."
1383 (interactive)
1384 (select-frame (make-frame gdb-frame-parameters))
1385 (switch-to-buffer (gdb-get-create-buffer 'gdb-threads-buffer))
1386 (set-window-dedicated-p (selected-window) t))
1387
1388 (defvar gdb-threads-mode-map
1389 (let ((map (make-sparse-keymap)))
1390 (suppress-keymap map)
1391 (define-key map "\r" 'gdb-threads-select)
1392 (define-key map [mouse-2] 'gdb-threads-mouse-select)
1393 map))
1394
1395 (defun gdb-threads-mode ()
1396 "Major mode for gdb frames.
1397
1398 \\{gdb-threads-mode-map}"
1399 (setq major-mode 'gdb-threads-mode)
1400 (setq mode-name "Threads")
1401 (setq buffer-read-only t)
1402 (use-local-map gdb-threads-mode-map)
1403 (gdb-invalidate-threads))
1404
1405 (defun gdb-get-thread-number ()
1406 (save-excursion
1407 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1408 (match-string-no-properties 1)))
1409
1410 (defun gdb-threads-select ()
1411 "Select the thread and display the relevant source."
1412 (interactive)
1413 (gdb-enqueue-input
1414 (list (concat "thread " (gdb-get-thread-number) "\n") 'ignore))
1415 (gud-display-frame))
1416
1417 (defun gdb-threads-mouse-select (event)
1418 "Select the thread you click on and display the relevant source."
1419 (interactive "e")
1420 (mouse-set-point event)
1421 (gdb-threads-select))
1422 \f
1423 ;;
1424 ;; Registers buffer.
1425 ;;
1426 (gdb-set-buffer-rules 'gdb-registers-buffer
1427 'gdb-registers-buffer-name
1428 'gdb-registers-mode)
1429
1430 (def-gdb-auto-updated-buffer gdb-registers-buffer
1431 gdb-invalidate-registers
1432 (concat gdb-server-prefix "info registers\n")
1433 gdb-info-registers-handler
1434 gdb-info-registers-custom)
1435
1436 (defun gdb-info-registers-custom ())
1437
1438 (defvar gdb-registers-mode-map
1439 (let ((map (make-sparse-keymap)))
1440 (suppress-keymap map)
1441 map))
1442
1443 (defun gdb-registers-mode ()
1444 "Major mode for gdb registers.
1445
1446 \\{gdb-registers-mode-map}"
1447 (setq major-mode 'gdb-registers-mode)
1448 (setq mode-name "Registers")
1449 (setq buffer-read-only t)
1450 (use-local-map gdb-registers-mode-map)
1451 (gdb-invalidate-registers))
1452
1453 (defun gdb-registers-buffer-name ()
1454 (with-current-buffer gud-comint-buffer
1455 (concat "*registers of " (gdb-get-target-string) "*")))
1456
1457 (defun gdb-display-registers-buffer ()
1458 "Display integer register contents."
1459 (interactive)
1460 (gdb-display-buffer
1461 (gdb-get-create-buffer 'gdb-registers-buffer)))
1462
1463 (defun gdb-frame-registers-buffer ()
1464 "Display integer register contents in a new frame."
1465 (interactive)
1466 (select-frame (make-frame gdb-frame-parameters))
1467 (switch-to-buffer (gdb-get-create-buffer 'gdb-registers-buffer))
1468 (set-window-dedicated-p (selected-window) t))
1469 \f
1470 ;;
1471 ;; Locals buffer.
1472 ;;
1473 (gdb-set-buffer-rules 'gdb-locals-buffer
1474 'gdb-locals-buffer-name
1475 'gdb-locals-mode)
1476
1477 (def-gdb-auto-updated-buffer gdb-locals-buffer
1478 gdb-invalidate-locals
1479 "server info locals\n"
1480 gdb-info-locals-handler
1481 gdb-info-locals-custom)
1482
1483 ;; Abbreviate for arrays and structures.
1484 ;; These can be expanded using gud-display.
1485 (defun gdb-info-locals-handler nil
1486 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
1487 gdb-pending-triggers))
1488 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
1489 (with-current-buffer buf
1490 (goto-char (point-min))
1491 (while (re-search-forward "^ .*\n" nil t)
1492 (replace-match "" nil nil))
1493 (goto-char (point-min))
1494 (while (re-search-forward "{[-0-9, {}\]*\n" nil t)
1495 (replace-match "(array);\n" nil nil))
1496 (goto-char (point-min))
1497 (while (re-search-forward "{.*=.*\n" nil t)
1498 (replace-match "(structure);\n" nil nil))))
1499 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
1500 (and buf (with-current-buffer buf
1501 (let ((p (point))
1502 (buffer-read-only nil))
1503 (delete-region (point-min) (point-max))
1504 (insert-buffer-substring (gdb-get-create-buffer
1505 'gdb-partial-output-buffer))
1506 (goto-char p)))))
1507 (run-hooks 'gdb-info-locals-hook))
1508
1509 (defun gdb-info-locals-custom ()
1510 nil)
1511
1512 (defvar gdb-locals-mode-map
1513 (let ((map (make-sparse-keymap)))
1514 (suppress-keymap map)
1515 map))
1516
1517 (defun gdb-locals-mode ()
1518 "Major mode for gdb locals.
1519
1520 \\{gdb-locals-mode-map}"
1521 (setq major-mode 'gdb-locals-mode)
1522 (setq mode-name (concat "Locals:" gdb-current-frame))
1523 (setq buffer-read-only t)
1524 (use-local-map gdb-locals-mode-map)
1525 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1526 (gdb-invalidate-locals)
1527 (gdbmi-invalidate-locals)))
1528
1529 (defun gdb-locals-buffer-name ()
1530 (with-current-buffer gud-comint-buffer
1531 (concat "*locals of " (gdb-get-target-string) "*")))
1532
1533 (defun gdb-display-locals-buffer ()
1534 "Display local variables of current stack and their values."
1535 (interactive)
1536 (gdb-display-buffer
1537 (gdb-get-create-buffer 'gdb-locals-buffer)))
1538
1539 (defun gdb-frame-locals-buffer ()
1540 "Display local variables of current stack and their values in a new frame."
1541 (interactive)
1542 (select-frame (make-frame gdb-frame-parameters))
1543 (switch-to-buffer (gdb-get-create-buffer 'gdb-locals-buffer))
1544 (set-window-dedicated-p (selected-window) t))
1545 \f
1546
1547 ;;;; Window management
1548
1549 ;;; The way we abuse the dedicated-p flag is pretty gross, but seems
1550 ;;; to do the right thing. Seeing as there is no way for Lisp code to
1551 ;;; get at the use_time field of a window, I'm not sure there exists a
1552 ;;; more elegant solution without writing C code.
1553
1554 (defun gdb-display-buffer (buf &optional size)
1555 (let ((must-split nil)
1556 (answer nil))
1557 (unwind-protect
1558 (progn
1559 (walk-windows
1560 #'(lambda (win)
1561 (if (eq gud-comint-buffer (window-buffer win))
1562 (set-window-dedicated-p win t))))
1563 (setq answer (get-buffer-window buf 'visible))
1564 (if (not answer)
1565 (let ((window (get-lru-window 'visible)))
1566 (if window
1567 (progn
1568 (set-window-buffer window buf)
1569 (setq answer window))
1570 (setq must-split t)))))
1571 (walk-windows
1572 #'(lambda (win)
1573 (if (eq gud-comint-buffer (window-buffer win))
1574 (set-window-dedicated-p win nil)))))
1575 (if must-split
1576 (let* ((largest (get-largest-window 'visible))
1577 (cur-size (window-height largest))
1578 (new-size (and size (< size cur-size) (- cur-size size))))
1579 (setq answer (split-window largest new-size))
1580 (set-window-buffer answer buf)))
1581 answer))
1582
1583 (defun gdb-display-source-buffer (buffer)
1584 (if (eq gdb-selected-view 'source)
1585 (gdb-display-buffer buffer)
1586 (gdb-display-buffer (gdb-get-buffer 'gdb-assembler-buffer)))
1587 (get-buffer-window buffer 'visible))
1588
1589 \f
1590 ;;; Shared keymap initialization:
1591
1592 (let ((menu (make-sparse-keymap "GDB-Frames")))
1593 (define-key gud-menu-map [frames]
1594 `(menu-item "GDB-Frames" ,menu :visible (eq gud-minor-mode 'gdba)))
1595 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
1596 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
1597 (define-key menu [assembler] '("Machine" . gdb-frame-assembler-buffer))
1598 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
1599 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
1600 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
1601 (define-key menu [breakpoints] '("Breakpoints" . gdb-frame-breakpoints-buffer)))
1602
1603 (let ((menu (make-sparse-keymap "GDB-Windows")))
1604 (define-key gud-menu-map [displays]
1605 `(menu-item "GDB-Windows" ,menu :visible (eq gud-minor-mode 'gdba)))
1606 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
1607 (define-key menu [assembler] '("Machine" . gdb-display-assembler-buffer))
1608 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
1609 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
1610 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
1611 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
1612 (define-key menu [breakpoints] '("Breakpoints" . gdb-display-breakpoints-buffer)))
1613
1614 (let ((menu (make-sparse-keymap "View")))
1615 (define-key gud-menu-map [view]
1616 `(menu-item "View" ,menu :visible (eq gud-minor-mode 'gdba)))
1617 ; (define-key menu [both] '(menu-item "Both" gdb-view-both
1618 ; :help "Display both source and assembler"
1619 ; :button (:radio . (eq gdb-selected-view 'both))))
1620 (define-key menu [assembler] '(menu-item "Machine" gdb-view-assembler
1621 :help "Display assembler only"
1622 :button (:radio . (eq gdb-selected-view 'assembler))))
1623 (define-key menu [source] '(menu-item "Source" gdb-view-source-function
1624 :help "Display source only"
1625 :button (:radio . (eq gdb-selected-view 'source)))))
1626
1627 (let ((menu (make-sparse-keymap "GDB-UI")))
1628 (define-key gud-menu-map [ui]
1629 `(menu-item "GDB-UI" ,menu :visible (eq gud-minor-mode 'gdba)))
1630 (define-key menu [gdb-restore-windows]
1631 '("Restore window layout" . gdb-restore-windows))
1632 (define-key menu [gdb-many-windows]
1633 (menu-bar-make-toggle gdb-many-windows gdb-many-windows
1634 "Display other windows" "Many Windows %s"
1635 "Display locals, stack and breakpoint information")))
1636
1637 (defun gdb-frame-gdb-buffer ()
1638 "Display GUD buffer in a new frame."
1639 (interactive)
1640 (select-frame (make-frame gdb-frame-parameters))
1641 (switch-to-buffer (gdb-get-create-buffer 'gdba))
1642 (set-window-dedicated-p (selected-window) t))
1643
1644 (defun gdb-display-gdb-buffer ()
1645 "Display GUD buffer."
1646 (interactive)
1647 (gdb-display-buffer
1648 (gdb-get-create-buffer 'gdba)))
1649
1650 (defvar gdb-main-file nil "Source file from which program execution begins.")
1651
1652 (defun gdb-view-source-function ()
1653 "Select source view."
1654 (interactive)
1655 (if gdb-view-source
1656 (gdb-display-buffer
1657 (if gud-last-last-frame
1658 (gud-find-file (car gud-last-last-frame))
1659 (gud-find-file gdb-main-file))))
1660 (setq gdb-selected-view 'source))
1661
1662 (defun gdb-view-assembler()
1663 "Select disassembly view."
1664 (interactive)
1665 (gdb-display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))
1666 (gdb-invalidate-assembler)
1667 (setq gdb-selected-view 'assembler))
1668
1669 ;(defun gdb-view-both()
1670 ;(interactive)
1671 ;(setq gdb-selected-view 'both))
1672
1673 (defcustom gdb-show-main nil
1674 "Nil means don't display source file containing the main routine."
1675 :type 'boolean
1676 :group 'gud)
1677
1678 (defun gdb-setup-windows ()
1679 "Layout the window pattern for gdb-many-windows."
1680 (gdb-display-locals-buffer)
1681 (gdb-display-stack-buffer)
1682 (delete-other-windows)
1683 (gdb-display-breakpoints-buffer)
1684 (delete-other-windows)
1685 (switch-to-buffer gud-comint-buffer)
1686 (split-window nil ( / ( * (window-height) 3) 4))
1687 (split-window nil ( / (window-height) 3))
1688 (split-window-horizontally)
1689 (other-window 1)
1690 (switch-to-buffer (gdb-locals-buffer-name))
1691 (other-window 1)
1692 (switch-to-buffer
1693 (if (and gdb-view-source
1694 (eq gdb-selected-view 'source))
1695 (if gud-last-last-frame
1696 (gud-find-file (car gud-last-last-frame))
1697 (gud-find-file gdb-main-file))
1698 (gdb-get-create-buffer 'gdb-assembler-buffer)))
1699 (when gdb-use-inferior-io-buffer
1700 (split-window-horizontally)
1701 (other-window 1)
1702 (switch-to-buffer (gdb-inferior-io-name)))
1703 (other-window 1)
1704 (switch-to-buffer (gdb-stack-buffer-name))
1705 (split-window-horizontally)
1706 (other-window 1)
1707 (switch-to-buffer (gdb-breakpoints-buffer-name))
1708 (other-window 1))
1709
1710 (defcustom gdb-many-windows nil
1711 "Nil (the default value) means just pop up the GUD buffer
1712 unless `gdb-show-main' is t. In this case it starts with two
1713 windows: one displaying the GUD buffer and the other with the
1714 source file with the main routine of the inferior. Non-nil means
1715 display the layout shown for `gdba'."
1716 :type 'boolean
1717 :group 'gud)
1718
1719 (defun gdb-many-windows (arg)
1720 "Toggle the number of windows in the basic arrangement."
1721 (interactive "P")
1722 (setq gdb-many-windows
1723 (if (null arg)
1724 (not gdb-many-windows)
1725 (> (prefix-numeric-value arg) 0)))
1726 (condition-case nil
1727 (gdb-restore-windows)
1728 (error nil)))
1729
1730 (defun gdb-restore-windows ()
1731 "Restore the basic arrangement of windows used by gdba.
1732 This arrangement depends on the value of `gdb-many-windows'."
1733 (interactive)
1734 (if gdb-many-windows
1735 (progn
1736 (switch-to-buffer gud-comint-buffer)
1737 (delete-other-windows)
1738 (gdb-setup-windows))
1739 (switch-to-buffer gud-comint-buffer)
1740 (delete-other-windows)
1741 (split-window)
1742 (other-window 1)
1743 (switch-to-buffer
1744 (if (and gdb-view-source
1745 (eq gdb-selected-view 'source))
1746 (if gud-last-last-frame
1747 (gud-find-file (car gud-last-last-frame))
1748 (gud-find-file gdb-main-file))
1749 (gdb-get-create-buffer 'gdb-assembler-buffer)))
1750 (other-window 1)))
1751
1752 (defun gdb-reset ()
1753 "Exit a debugging session cleanly by killing the gdb buffers and resetting
1754 the source buffers."
1755 (dolist (buffer (buffer-list))
1756 (unless (eq buffer gud-comint-buffer)
1757 (with-current-buffer buffer
1758 (if (memq gud-minor-mode '(gdbmi gdba))
1759 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
1760 (kill-buffer nil)
1761 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
1762 (setq gud-minor-mode nil)
1763 (kill-local-variable 'tool-bar-map)
1764 (setq gud-running nil))))))
1765 (when (markerp gdb-overlay-arrow-position)
1766 (move-marker gdb-overlay-arrow-position nil)
1767 (setq gdb-overlay-arrow-position nil))
1768 (setq overlay-arrow-variable-list
1769 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list)))
1770
1771 (defun gdb-source-info ()
1772 "Find the source file where the program starts and displays it with related
1773 buffers."
1774 (goto-char (point-min))
1775 (if (search-forward "directory is " nil t)
1776 (if (looking-at "\\S-*:\\(\\S-*\\)")
1777 (setq gdb-cdir (match-string 1))
1778 (looking-at "\\S-*")
1779 (setq gdb-cdir (match-string 0))))
1780 (if (search-forward "Located in " nil t)
1781 (if (looking-at "\\S-*")
1782 (setq gdb-main-file (match-string 0)))
1783 (setq gdb-view-source nil))
1784 (if gdb-many-windows
1785 (gdb-setup-windows)
1786 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
1787 (when gdb-show-main
1788 (switch-to-buffer gud-comint-buffer)
1789 (delete-other-windows)
1790 (split-window)
1791 (other-window 1)
1792 (switch-to-buffer
1793 (if gdb-view-source
1794 (gud-find-file gdb-main-file)
1795 (gdb-get-create-buffer 'gdb-assembler-buffer)))
1796 (other-window 1))))
1797
1798 ;;from put-image
1799 (defun gdb-put-string (putstring pos &optional dprop)
1800 "Put string PUTSTRING in front of POS in the current buffer.
1801 PUTSTRING is displayed by putting an overlay into the current buffer with a
1802 `before-string' STRING that has a `display' property whose value is
1803 PUTSTRING."
1804 (let ((gdb-string "x")
1805 (buffer (current-buffer)))
1806 (let ((overlay (make-overlay pos pos buffer))
1807 (prop (or dprop
1808 (list (list 'margin 'left-margin) putstring))))
1809 (put-text-property 0 (length gdb-string) 'display prop gdb-string)
1810 (overlay-put overlay 'put-break t)
1811 (overlay-put overlay 'before-string gdb-string))))
1812
1813 ;;from remove-images
1814 (defun gdb-remove-strings (start end &optional buffer)
1815 "Remove strings between START and END in BUFFER.
1816 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
1817 BUFFER nil or omitted means use the current buffer."
1818 (unless buffer
1819 (setq buffer (current-buffer)))
1820 (let ((overlays (overlays-in start end)))
1821 (while overlays
1822 (let ((overlay (car overlays)))
1823 (when (overlay-get overlay 'put-break)
1824 (delete-overlay overlay)))
1825 (setq overlays (cdr overlays)))))
1826
1827 (defun gdb-put-breakpoint-icon (enabled)
1828 (let ((start (progn (beginning-of-line) (- (point) 1)))
1829 (end (progn (end-of-line) (+ (point) 1))))
1830 (gdb-remove-breakpoint-icons start end)
1831 (if (display-images-p)
1832 (if (>= (car (window-fringes)) 8)
1833 (gdb-put-string
1834 nil (1+ start)
1835 `(left-fringe breakpoint
1836 ,(if enabled
1837 'breakpoint-enabled-bitmap-face
1838 'breakpoint-disabled-bitmap-face)))
1839 (when (< left-margin-width 2)
1840 (save-current-buffer
1841 (setq left-margin-width 2)
1842 (if (get-buffer-window (current-buffer) 'visible)
1843 (set-window-margins
1844 (get-buffer-window (current-buffer) 'visible)
1845 left-margin-width right-margin-width))))
1846 (put-image
1847 (if enabled
1848 (or breakpoint-enabled-icon
1849 (setq breakpoint-enabled-icon
1850 (find-image `((:type xpm :data
1851 ,breakpoint-xpm-data
1852 :ascent 100 :pointer hand)
1853 (:type pbm :data
1854 ,breakpoint-enabled-pbm-data
1855 :ascent 100 :pointer hand)))))
1856 (or breakpoint-disabled-icon
1857 (setq breakpoint-disabled-icon
1858 (find-image `((:type xpm :data
1859 ,breakpoint-xpm-data
1860 :conversion disabled
1861 :ascent 100)
1862 (:type pbm :data
1863 ,breakpoint-disabled-pbm-data
1864 :ascent 100))))))
1865 (+ start 1) nil 'left-margin))
1866 (when (< left-margin-width 2)
1867 (save-current-buffer
1868 (setq left-margin-width 2)
1869 (if (get-buffer-window (current-buffer) 'visible)
1870 (set-window-margins
1871 (get-buffer-window (current-buffer) 'visible)
1872 left-margin-width right-margin-width))))
1873 (gdb-put-string (if enabled "B" "b") (1+ start)))))
1874
1875 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
1876 (gdb-remove-strings start end)
1877 (if (display-images-p)
1878 (remove-images start end))
1879 (when remove-margin
1880 (setq left-margin-width 0)
1881 (if (get-buffer-window (current-buffer) 'visible)
1882 (set-window-margins
1883 (get-buffer-window (current-buffer) 'visible)
1884 left-margin-width right-margin-width))))
1885
1886 \f
1887 ;;
1888 ;; Assembler buffer.
1889 ;;
1890 (gdb-set-buffer-rules 'gdb-assembler-buffer
1891 'gdb-assembler-buffer-name
1892 'gdb-assembler-mode)
1893
1894 (def-gdb-auto-updated-buffer gdb-assembler-buffer
1895 gdb-invalidate-assembler
1896 (concat gdb-server-prefix "disassemble " gdb-current-address "\n")
1897 gdb-assembler-handler
1898 gdb-assembler-custom)
1899
1900 (defun gdb-assembler-custom ()
1901 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
1902 (pos 1) (address) (flag))
1903 (with-current-buffer buffer
1904 (if (not (equal gdb-current-address "main"))
1905 (progn
1906 (goto-char (point-min))
1907 (if (re-search-forward gdb-current-address nil t)
1908 (progn
1909 (setq pos (point))
1910 (beginning-of-line)
1911 (or gdb-overlay-arrow-position
1912 (setq gdb-overlay-arrow-position (make-marker)))
1913 (set-marker gdb-overlay-arrow-position
1914 (point) (current-buffer))))))
1915 ;; remove all breakpoint-icons in assembler buffer before updating.
1916 (gdb-remove-breakpoint-icons (point-min) (point-max)))
1917 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1918 (goto-char (point-min))
1919 (while (< (point) (- (point-max) 1))
1920 (forward-line 1)
1921 (if (looking-at "[^\t].*breakpoint")
1922 (progn
1923 (looking-at
1924 "[0-9]*\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)\\s-*0x\\(\\S-*\\)")
1925 (setq flag (char-after (match-beginning 1)))
1926 (setq address (match-string 2))
1927 ;; remove leading 0s from output of info break.
1928 (if (string-match "^0+\\(.*\\)" address)
1929 (setq address (match-string 1 address)))
1930 (with-current-buffer buffer
1931 (goto-char (point-min))
1932 (if (re-search-forward address nil t)
1933 (gdb-put-breakpoint-icon (eq flag ?y))))))))
1934 (if (not (equal gdb-current-address "main"))
1935 (set-window-point (get-buffer-window buffer 'visible) pos))))
1936
1937 (defvar gdb-assembler-mode-map
1938 (let ((map (make-sparse-keymap)))
1939 (suppress-keymap map)
1940 map))
1941
1942 (defun gdb-assembler-mode ()
1943 "Major mode for viewing code assembler.
1944
1945 \\{gdb-assembler-mode-map}"
1946 (setq major-mode 'gdb-assembler-mode)
1947 (setq mode-name "Machine")
1948 (setq gdb-overlay-arrow-position nil)
1949 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
1950 (put 'gdb-overlay-arrow-position 'overlay-arrow-string "=>")
1951 (setq fringes-outside-margins t)
1952 (setq buffer-read-only t)
1953 (use-local-map gdb-assembler-mode-map)
1954 (gdb-invalidate-assembler))
1955
1956 (defun gdb-assembler-buffer-name ()
1957 (with-current-buffer gud-comint-buffer
1958 (concat "*Machine Code " (gdb-get-target-string) "*")))
1959
1960 (defun gdb-display-assembler-buffer ()
1961 "Display disassembly view."
1962 (interactive)
1963 (gdb-display-buffer
1964 (gdb-get-create-buffer 'gdb-assembler-buffer)))
1965
1966 (defun gdb-frame-assembler-buffer ()
1967 "Display disassembly view in a new frame."
1968 (interactive)
1969 (select-frame (make-frame gdb-frame-parameters))
1970 (switch-to-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))
1971 (set-window-dedicated-p (selected-window) t))
1972
1973 ;; modified because if gdb-current-address has changed value a new command
1974 ;; must be enqueued to update the buffer with the new output
1975 (defun gdb-invalidate-assembler (&optional ignored)
1976 (if (gdb-get-buffer 'gdb-assembler-buffer)
1977 (progn
1978 (unless (string-equal gdb-current-frame gdb-previous-frame)
1979 (if (or (not (member 'gdb-invalidate-assembler
1980 gdb-pending-triggers))
1981 (not (string-equal gdb-current-address
1982 gdb-previous-address)))
1983 (progn
1984 ;; take previous disassemble command off the queue
1985 (with-current-buffer gud-comint-buffer
1986 (let ((queue gdb-input-queue) (item))
1987 (dolist (item queue)
1988 (if (equal (cdr item) '(gdb-assembler-handler))
1989 (setq gdb-input-queue
1990 (delete item gdb-input-queue))))))
1991 (gdb-enqueue-input
1992 (list (concat gdb-server-prefix "disassemble " gdb-current-address "\n")
1993 'gdb-assembler-handler))
1994 (push 'gdb-invalidate-assembler gdb-pending-triggers)
1995 (setq gdb-previous-address gdb-current-address)
1996 (setq gdb-previous-frame gdb-current-frame)))))))
1997
1998 (defun gdb-get-current-frame ()
1999 (if (not (member 'gdb-get-current-frame gdb-pending-triggers))
2000 (progn
2001 (gdb-enqueue-input
2002 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
2003 (push 'gdb-get-current-frame
2004 gdb-pending-triggers))))
2005
2006 (defun gdb-frame-handler ()
2007 (setq gdb-pending-triggers
2008 (delq 'gdb-get-current-frame gdb-pending-triggers))
2009 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
2010 (goto-char (point-min))
2011 (forward-line)
2012 (if (looking-at ".*=\\s-+0x\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? ")
2013 (progn
2014 (setq gdb-current-frame (match-string 2))
2015 (if (gdb-get-buffer 'gdb-locals-buffer)
2016 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2017 (setq mode-name (concat "Locals:" gdb-current-frame))))
2018 (let ((address (match-string 1)))
2019 ;; remove leading 0s from output of info frame command.
2020 (if (string-match "^0+\\(.*\\)" address)
2021 (setq gdb-current-address
2022 (concat "0x" (match-string 1 address)))
2023 (setq gdb-current-address (concat "0x" address))))
2024 (if (or (if (not (re-search-forward "(\\S-*:[0-9]*);" nil t))
2025 (progn (setq gdb-view-source nil) t))
2026 (eq gdb-selected-view 'assembler))
2027 (progn
2028 (gdb-display-buffer
2029 (gdb-get-create-buffer 'gdb-assembler-buffer))
2030 ;;update with new frame for machine code if necessary
2031 (gdb-invalidate-assembler))))))
2032 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
2033 (setq gdb-current-language (match-string 1))))
2034
2035 (provide 'gdb-ui)
2036
2037 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
2038 ;;; gdb-ui.el ends here