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