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