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