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