]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-ui.el
(gdb-stopped): Even when there is no source
[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, 2006
8 ;; Free Software Foundation, Inc.
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This mode acts as a graphical user interface to GDB. You can interact with
30 ;; GDB through the GUD buffer in the usual way, but there are also further
31 ;; buffers which control the execution and describe the state of your program.
32 ;; It separates the input/output of your program from that of GDB, if
33 ;; required, and watches expressions in the speedbar. It also uses features of
34 ;; Emacs 21 such as the fringe/display margin for breakpoints, and the toolbar
35 ;; (see the GDB Graphical Interface section in the Emacs info manual).
36
37 ;; By default, M-x gdb will start the debugger. However, if you have customised
38 ;; gud-gdb-command-name, then start it with M-x gdba.
39
40 ;; This file has evolved from gdba.el that was included with GDB 5.0 and
41 ;; written by Tom Lord and Jim Kingdon. It uses GDB's annotation interface.
42 ;; You don't need to know about annotations to use this mode as a debugger,
43 ;; but if you are interested developing the mode itself, then see the
44 ;; Annotations section in the GDB info manual.
45
46 ;; GDB developers plan to make the annotation interface obsolete. A new
47 ;; interface called GDB/MI (machine interface) has been designed to replace
48 ;; it. Some GDB/MI commands are used in this file through the CLI command
49 ;; 'interpreter mi <mi-command>'. A file called gdb-mi.el is included with
50 ;; GDB (6.2 onwards) that uses GDB/MI as the primary interface to GDB. It is
51 ;; still under development and is part of a process to migrate Emacs from
52 ;; annotations to GDB/MI.
53
54 ;; This mode SHOULD WORK WITH GDB 5.0 onwards but you will NEED GDB 6.0
55 ;; onwards to use watch expressions. It works best with GDB 6.4 where
56 ;; watch expressions will update more quickly.
57
58 ;;; Windows Platforms:
59
60 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
61 ;; explicitly in your program if you want timely display of I/O in Emacs.
62 ;; Alternatively you can make the output stream unbuffered, for example, by
63 ;; using a macro:
64
65 ;; #ifdef UNBUFFERED
66 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
67 ;; #endif
68
69 ;; and compiling with -DUNBUFFERED while debugging.
70
71 ;;; Known Bugs:
72
73 ;; 1) Strings that are watched don't update in the speedbar when their
74 ;; contents change unless the first character changes.
75 ;; 2) Cannot handle multiple debug sessions.
76 ;; 3) Initially, the assembler buffer does not display the cursor at the
77 ;; current line if the line is not visible in the window (but when testing
78 ;; gdb-assembler-custom with a lisp debugger it does!).
79
80 ;;; Problems with watch expressions, GDB/MI:
81
82 ;; 1) They go out of scope when the inferior is re-run.
83 ;; 2) -stack-list-locals has a type field but also prints type in values field.
84 ;; 3) VARNUM increments even when variable object is not created (maybe trivial).
85
86 ;;; TODO:
87
88 ;; 1) Use MI command -data-read-memory for memory window.
89 ;; 2) Use tree-widget.el instead of the speedbar for watch-expressions?
90 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
91 ;; 4) With gud-print and gud-pstar, print the variable name in the GUD
92 ;; buffer instead of the value's history number.
93
94 ;;; Code:
95
96 (require 'gud)
97
98 (defvar tool-bar-map)
99 (defvar speedbar-initial-expansion-list-name)
100
101 (defvar gdb-frame-address "main" "Initialization for Assembler buffer.")
102 (defvar gdb-previous-frame-address nil)
103 (defvar gdb-memory-address "main")
104 (defvar gdb-previous-frame nil)
105 (defvar gdb-selected-frame nil)
106 (defvar gdb-frame-number nil)
107 (defvar gdb-current-language nil)
108 (defvar gdb-var-list nil
109 "List of variables in watch window.
110 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS) where
111 STATUS is nil (unchanged), `changed' or `out-of-scope'.")
112 (defvar gdb-force-update t
113 "Non-nil means that view of watch expressions will be updated in the speedbar.")
114 (defvar gdb-main-file nil "Source file from which program execution begins.")
115 (defvar gdb-overlay-arrow-position nil)
116 (defvar gdb-server-prefix nil)
117 (defvar gdb-flush-pending-output nil)
118 (defvar gdb-location-alist nil
119 "Alist of breakpoint numbers and full filenames.")
120 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
121 and #define directives otherwise.")
122 (defvar gdb-error "Non-nil when GDB is reporting an error.")
123 (defvar gdb-macro-info nil
124 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
125 (defvar gdb-buffer-fringe-width nil)
126 (defvar gdb-signalled nil)
127 (defvar gdb-source-window nil)
128 (defvar gdb-inferior-status nil)
129
130 (defvar gdb-buffer-type nil
131 "One of the symbols bound in `gdb-buffer-rules'.")
132 (make-variable-buffer-local 'gdb-buffer-type)
133
134 (defvar gdb-input-queue ()
135 "A list of gdb command objects.")
136
137 (defvar gdb-prompting nil
138 "True when gdb is idle with no pending input.")
139
140 (defvar gdb-output-sink 'user
141 "The disposition of the output of the current gdb command.
142 Possible values are these symbols:
143
144 `user' -- gdb output should be copied to the GUD buffer
145 for the user to see.
146
147 `inferior' -- gdb output should be copied to the inferior-io buffer.
148
149 `pre-emacs' -- output should be ignored util the post-prompt
150 annotation is received. Then the output-sink
151 becomes:...
152 `emacs' -- output should be collected in the partial-output-buffer
153 for subsequent processing by a command. This is the
154 disposition of output generated by commands that
155 gdb mode sends to gdb on its own behalf.
156 `post-emacs' -- ignore output until the prompt annotation is
157 received, then go to USER disposition.
158
159 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
160 \(`user' and `emacs').")
161
162 (defvar gdb-current-item nil
163 "The most recent command item sent to gdb.")
164
165 (defvar gdb-pending-triggers '()
166 "A list of trigger functions that have run later than their output
167 handlers.")
168
169 (defvar gdb-first-post-prompt nil)
170 (defvar gdb-version nil)
171 (defvar gdb-locals-font-lock-keywords nil)
172 (defvar gdb-source-file-list nil
173 "List of source files for the current executable")
174 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
175
176 (defvar gdb-locals-font-lock-keywords-1
177 '(
178 ;; var = (struct struct_tag) value
179 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
180 (1 font-lock-variable-name-face)
181 (3 font-lock-keyword-face)
182 (4 font-lock-type-face))
183 ;; var = (type) value
184 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
185 (1 font-lock-variable-name-face)
186 (3 font-lock-type-face))
187 ;; var = val
188 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
189 (1 font-lock-variable-name-face))
190 )
191 "Font lock keywords used in `gdb-local-mode'.")
192
193 (defvar gdb-locals-font-lock-keywords-2
194 '(
195 ;; var = type value
196 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
197 (1 font-lock-variable-name-face)
198 (3 font-lock-type-face))
199 )
200 "Font lock keywords used in `gdb-local-mode'.")
201
202 ;; Variables for GDB 6.4+
203 (defvar gdb-register-names nil "List of register names.")
204 (defvar gdb-changed-registers nil
205 "List of changed register numbers (strings).")
206
207 ;;;###autoload
208 (defun gdba (command-line)
209 "Run gdb on program FILE in buffer *gud-FILE*.
210 The directory containing FILE becomes the initial working directory
211 and source-file directory for your debugger.
212
213 If `gdb-many-windows' is nil (the default value) then gdb just
214 pops up the GUD buffer unless `gdb-show-main' is t. In this case
215 it starts with two windows: one displaying the GUD buffer and the
216 other with the source file with the main routine of the inferior.
217
218 If `gdb-many-windows' is t, regardless of the value of
219 `gdb-show-main', the layout below will appear unless
220 `gdb-use-separate-io-buffer' is nil when the source buffer
221 occupies the full width of the frame. Keybindings are shown in
222 some of the buffers.
223
224 Watch expressions appear in the speedbar/slowbar.
225
226 The following commands help control operation :
227
228 `gdb-many-windows' - Toggle the number of windows gdb uses.
229 `gdb-restore-windows' - To restore the window layout.
230
231 See Info node `(emacs)GDB Graphical Interface' for a more
232 detailed description of this mode.
233
234
235 +----------------------------------------------------------------------+
236 | GDB Toolbar |
237 +-----------------------------------+----------------------------------+
238 | GUD buffer (I/O of GDB) | Locals buffer |
239 | | |
240 | | |
241 | | |
242 +-----------------------------------+----------------------------------+
243 | Source buffer | I/O buffer (of debugged program) |
244 | | (comint-mode) |
245 | | |
246 | | |
247 | | |
248 | | |
249 | | |
250 | | |
251 +-----------------------------------+----------------------------------+
252 | Stack buffer | Breakpoints buffer |
253 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
254 | | RET gdb-goto-breakpoint |
255 | | D gdb-delete-breakpoint |
256 +-----------------------------------+----------------------------------+"
257 ;;
258 (interactive (list (gud-query-cmdline 'gdba)))
259 ;;
260 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
261 (gdb command-line))
262
263 (defcustom gdb-debug-ring-max 128
264 "Maximum size of `gdb-debug-ring'."
265 :group 'gud
266 :type 'integer
267 :version "22.1")
268
269 (defvar gdb-debug-ring nil
270 "List of commands, most recent first, sent to and replies received from GDB.
271 This variable is used to debug GDB-UI.")
272
273 ;;;###autoload
274 (defcustom gdb-enable-debug nil
275 "Non-nil means record the process input and output in `gdb-debug-ring'."
276 :type 'boolean
277 :group 'gud
278 :version "22.1")
279
280 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
281 "Shell command for generating a list of defined macros in a source file.
282 This list is used to display the #define directive associated
283 with an identifier as a tooltip. It works in a debug session with
284 GDB, when gud-tooltip-mode is t.
285
286 Set `gdb-cpp-define-alist-flags' for any include paths or
287 predefined macros."
288 :type 'string
289 :group 'gud
290 :version "22.1")
291
292 (defcustom gdb-cpp-define-alist-flags ""
293 "Preprocessor flags for `gdb-cpp-define-alist-program'."
294 :type 'string
295 :group 'gud
296 :version "22.1")
297
298 (defcustom gdb-show-main nil
299 "Non-nil means display source file containing the main routine at startup.
300 Also display the main routine in the disassembly buffer if present."
301 :type 'boolean
302 :group 'gud
303 :version "22.1")
304
305 (defcustom gdb-many-windows nil
306 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
307 In this case it starts with two windows: one displaying the GUD
308 buffer and the other with the source file with the main routine
309 of the inferior. Non-nil means display the layout shown for
310 `gdba'."
311 :type 'boolean
312 :group 'gud
313 :version "22.1")
314
315 (defcustom gdb-use-separate-io-buffer nil
316 "Non-nil means display output from the inferior in a separate buffer."
317 :type 'boolean
318 :group 'gud
319 :version "22.1")
320
321 (defun gdb-force-mode-line-update (status)
322 (let ((buffer gud-comint-buffer))
323 (if (and buffer (buffer-name buffer))
324 (with-current-buffer buffer
325 (setq mode-line-process
326 (format ":%s [%s]"
327 (process-status (get-buffer-process buffer)) status))
328 ;; Force mode line redisplay soon.
329 (force-mode-line-update)))))
330
331 (defun gdb-many-windows (arg)
332 "Toggle the number of windows in the basic arrangement.
333 With arg, display additional buffers iff arg is positive."
334 (interactive "P")
335 (setq gdb-many-windows
336 (if (null arg)
337 (not gdb-many-windows)
338 (> (prefix-numeric-value arg) 0)))
339 (message (format "Display of other windows %sabled"
340 (if gdb-many-windows "en" "dis")))
341 (if (and gud-comint-buffer
342 (buffer-name gud-comint-buffer))
343 (condition-case nil
344 (gdb-restore-windows)
345 (error nil))))
346
347 (defun gdb-use-separate-io-buffer (arg)
348 "Toggle separate IO for inferior.
349 With arg, use separate IO iff arg is positive."
350 (interactive "P")
351 (setq gdb-use-separate-io-buffer
352 (if (null arg)
353 (not gdb-use-separate-io-buffer)
354 (> (prefix-numeric-value arg) 0)))
355 (message (format "Separate inferior IO %sabled"
356 (if gdb-use-separate-io-buffer "en" "dis")))
357 (if (and gud-comint-buffer
358 (buffer-name gud-comint-buffer))
359 (condition-case nil
360 (if gdb-use-separate-io-buffer
361 (if gdb-many-windows (gdb-restore-windows))
362 (kill-buffer (gdb-inferior-io-name)))
363 (error nil))))
364
365 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
366
367 (defun gdb-create-define-alist ()
368 "Create an alist of #define directives for GUD tooltips."
369 (let* ((file (buffer-file-name))
370 (output
371 (with-output-to-string
372 (with-current-buffer standard-output
373 (call-process shell-file-name
374 (if (file-exists-p file) file nil)
375 (list t nil) nil "-c"
376 (concat gdb-cpp-define-alist-program " "
377 gdb-cpp-define-alist-flags)))))
378 (define-list (split-string output "\n" t))
379 (name))
380 (setq gdb-define-alist nil)
381 (dolist (define define-list)
382 (setq name (nth 1 (split-string define "[( ]")))
383 (push (cons name define) gdb-define-alist))))
384
385 (defun gdb-tooltip-print (expr)
386 (tooltip-show
387 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
388 (goto-char (point-min))
389 (let ((string
390 (if (search-forward "=" nil t)
391 (concat expr (buffer-substring (- (point) 2) (point-max)))
392 (buffer-string))))
393 ;; remove newline for gud-tooltip-echo-area
394 (substring string 0 (- (length string) 1))))
395 (or gud-tooltip-echo-area tooltip-use-echo-area)))
396
397 ;; If expr is a macro for a function don't print because of possible dangerous
398 ;; side-effects. Also printing a function within a tooltip generates an
399 ;; unexpected starting annotation (phase error).
400 (defun gdb-tooltip-print-1 (expr)
401 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
402 (goto-char (point-min))
403 (if (search-forward "expands to: " nil t)
404 (unless (looking-at "\\S-+.*(.*).*")
405 (gdb-enqueue-input
406 (list (concat gdb-server-prefix "print " expr "\n")
407 `(lambda () (gdb-tooltip-print ,expr))))))))
408
409 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
410
411 (defun gdb-set-gud-minor-mode-existing-buffers ()
412 "Create list of source files for current GDB session."
413 (goto-char (point-min))
414 (when (search-forward "read in on demand:" nil t)
415 (while (re-search-forward gdb-source-file-regexp nil t)
416 (push (or (match-string 1) (match-string 2)) gdb-source-file-list))
417 (dolist (buffer (buffer-list))
418 (with-current-buffer buffer
419 (when (and buffer-file-name
420 (member (file-name-nondirectory buffer-file-name)
421 gdb-source-file-list))
422 (set (make-local-variable 'gud-minor-mode) 'gdba)
423 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
424 (when gud-tooltip-mode
425 (make-local-variable 'gdb-define-alist)
426 (gdb-create-define-alist)
427 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))))
428
429 (defun gdb-find-watch-expression ()
430 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
431 (varnum (car var)) expr array)
432 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
433 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
434 (component-list (split-string (match-string 2 varnum) "\\." t)))
435 (setq expr (nth 1 var1))
436 (setq varnumlet (car var1))
437 (dolist (component component-list)
438 (setq var2 (assoc varnumlet gdb-var-list))
439 (setq expr (concat expr
440 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
441 (concat "[" component "]")
442 (concat "." component))))
443 (setq varnumlet (concat varnumlet "." component)))
444 expr)))
445
446 (defun gdb-init-1 ()
447 (set (make-local-variable 'gud-minor-mode) 'gdba)
448 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
449 ;;
450 (gud-def gud-break (if (not (string-match "Machine" mode-name))
451 (gud-call "break %f:%l" arg)
452 (save-excursion
453 (beginning-of-line)
454 (forward-char 2)
455 (gud-call "break *%a" arg)))
456 "\C-b" "Set breakpoint at current line or address.")
457 ;;
458 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
459 (gud-call "clear %f:%l" arg)
460 (save-excursion
461 (beginning-of-line)
462 (forward-char 2)
463 (gud-call "clear *%a" arg)))
464 "\C-d" "Remove breakpoint at current line or address.")
465 ;;
466 (gud-def gud-until (if (not (string-match "Machine" mode-name))
467 (gud-call "until %f:%l" arg)
468 (save-excursion
469 (beginning-of-line)
470 (forward-char 2)
471 (gud-call "until *%a" arg)))
472 "\C-u" "Continue to current line or address.")
473 ;;
474 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
475 nil "Start or continue execution.")
476
477 ;; For debugging Emacs only.
478 (gud-def gud-pp
479 (gud-call
480 (concat
481 "pp1 " (if (eq (buffer-local-value
482 'major-mode (window-buffer)) 'speedbar-mode)
483 (gdb-find-watch-expression) "%e")) arg)
484 nil "Print the emacs s-expression.")
485
486 (define-key gud-minor-mode-map [left-margin mouse-1]
487 'gdb-mouse-set-clear-breakpoint)
488 (define-key gud-minor-mode-map [left-fringe mouse-1]
489 'gdb-mouse-set-clear-breakpoint)
490 (define-key gud-minor-mode-map [left-fringe mouse-2]
491 'gdb-mouse-until)
492 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
493 'gdb-mouse-until)
494 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
495 'gdb-mouse-until)
496 (define-key gud-minor-mode-map [left-margin mouse-2]
497 'gdb-mouse-until)
498 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
499 'gdb-mouse-jump)
500 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
501 'gdb-mouse-jump)
502 (define-key gud-minor-mode-map [left-fringe C-mouse-2]
503 'gdb-mouse-jump)
504 (define-key gud-minor-mode-map [left-margin C-mouse-2]
505 'gdb-mouse-jump)
506 (define-key gud-minor-mode-map [left-margin mouse-3]
507 'gdb-mouse-toggle-breakpoint-margin)
508 (define-key gud-minor-mode-map [left-fringe mouse-3]
509 'gdb-mouse-toggle-breakpoint-fringe)
510
511 (setq comint-input-sender 'gdb-send)
512
513 ;; (re-)initialize
514 (setq gdb-frame-address (if gdb-show-main "main" nil))
515 (setq gdb-previous-frame-address nil
516 gdb-memory-address "main"
517 gdb-previous-frame nil
518 gdb-selected-frame nil
519 gdb-current-language nil
520 gdb-frame-number nil
521 gdb-var-list nil
522 gdb-force-update t
523 gdb-first-post-prompt t
524 gdb-prompting nil
525 gdb-input-queue nil
526 gdb-current-item nil
527 gdb-pending-triggers nil
528 gdb-output-sink 'user
529 gdb-server-prefix "server "
530 gdb-flush-pending-output nil
531 gdb-location-alist nil
532 gdb-source-file-list nil
533 gdb-error nil
534 gdb-macro-info nil
535 gdb-buffer-fringe-width (car (window-fringes))
536 gdb-debug-ring nil
537 gdb-signalled nil
538 gdb-source-window nil
539 gdb-inferior-status nil
540 gdb-continuation nil)
541
542 (setq gdb-buffer-type 'gdba)
543
544 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
545
546 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
547 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
548 'gdb-get-version)))
549
550 (defun gdb-init-2 ()
551 (if (eq window-system 'w32)
552 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
553 (gdb-enqueue-input (list "set height 0\n" 'ignore))
554 (gdb-enqueue-input (list "set width 0\n" 'ignore))
555
556 (if (string-equal gdb-version "pre-6.4")
557 (progn
558 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
559 'gdb-set-gud-minor-mode-existing-buffers))
560 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
561 (gdb-enqueue-input
562 (list "server interpreter mi -data-list-register-names\n"
563 'gdb-get-register-names))
564 ; Needs GDB 6.2 onwards.
565 (gdb-enqueue-input
566 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
567 'gdb-set-gud-minor-mode-existing-buffers-1))
568 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
569
570 ;; find source file and compilation directory here
571 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
572 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
573 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
574
575 (run-hooks 'gdba-mode-hook))
576
577 (defun gdb-get-version ()
578 (goto-char (point-min))
579 (if (and (re-search-forward gdb-error-regexp nil t)
580 (string-match ".*(missing implementation)" (match-string 1)))
581 (setq gdb-version "pre-6.4")
582 (setq gdb-version "6.4+"))
583 (gdb-init-2))
584
585 (defun gdb-mouse-until (event)
586 "Continue running until a source line past the current line.
587 The destination source line can be selected either by clicking with mouse-2
588 on the fringe/margin or dragging the arrow with mouse-1 (default bindings)."
589 (interactive "e")
590 (if gud-overlay-arrow-position
591 (let ((start (event-start event))
592 (end (event-end event))
593 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
594 (if (not (string-match "Machine" mode-name))
595 (if (equal buffer (window-buffer (posn-window end)))
596 (with-current-buffer buffer
597 (when (or (equal start end)
598 (equal (posn-point start)
599 (marker-position
600 gud-overlay-arrow-position)))
601 (setq line (line-number-at-pos (posn-point end)))
602 (gud-call (concat "until " (number-to-string line))))))
603 (if (equal (marker-buffer gdb-overlay-arrow-position)
604 (window-buffer (posn-window end)))
605 (when (or (equal start end)
606 (equal (posn-point start)
607 (marker-position
608 gdb-overlay-arrow-position)))
609 (save-excursion
610 (goto-line (line-number-at-pos (posn-point end)))
611 (forward-char 2)
612 (gud-call (concat "until *%a")))))))))
613
614 (defun gdb-mouse-jump (event)
615 "Set execution address/line.
616 The destination source line can be selected either by clicking with mouse-2
617 on the fringe/margin or dragging the arrow with mouse-1 (default bindings).
618 Unlike gdb-mouse-until the destination address can be before the current
619 line, and no execution takes place."
620 (interactive "e")
621 (if gud-overlay-arrow-position
622 (let ((start (event-start event))
623 (end (event-end event))
624 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
625 (if (not (string-match "Machine" mode-name))
626 (if (equal buffer (window-buffer (posn-window end)))
627 (with-current-buffer buffer
628 (when (or (equal start end)
629 (equal (posn-point start)
630 (marker-position
631 gud-overlay-arrow-position)))
632 (setq line (line-number-at-pos (posn-point end)))
633 (progn (gud-call (concat "tbreak " (number-to-string line)))
634 (gud-call (concat "jump " (number-to-string line)))))))
635 (if (equal (marker-buffer gdb-overlay-arrow-position)
636 (window-buffer (posn-window end)))
637 (when (or (equal start end)
638 (equal (posn-point start)
639 (marker-position
640 gdb-overlay-arrow-position)))
641 (save-excursion
642 (goto-line (line-number-at-pos (posn-point end)))
643 (forward-char 2)
644 (progn
645 (gud-call (concat "tbreak *%a"))
646 (gud-call (concat "jump *%a"))))))))))
647
648 (defcustom gdb-speedbar-auto-raise nil
649 "If non-nil raise speedbar every time display of watch expressions is\
650 updated."
651 :type 'boolean
652 :group 'gud
653 :version "22.1")
654
655 (defun gdb-speedbar-auto-raise (arg)
656 "Toggle automatic raising of the speedbar for watch expressions.
657 With arg, automatically raise speedbar iff arg is positive."
658 (interactive "P")
659 (setq gdb-speedbar-auto-raise
660 (if (null arg)
661 (not gdb-speedbar-auto-raise)
662 (> (prefix-numeric-value arg) 0)))
663 (message (format "Auto raising %sabled"
664 (if gdb-speedbar-auto-raise "en" "dis"))))
665
666 (defcustom gdb-use-colon-colon-notation nil
667 "If non-nil use FUN::VAR format to display variables in the speedbar."
668 :type 'boolean
669 :group 'gud
670 :version "22.1")
671
672 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
673 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
674
675 (defun gud-watch (&optional arg event)
676 "Watch expression at point.
677 With arg, enter name of variable to be watched in the minibuffer."
678 (interactive (list current-prefix-arg last-input-event))
679 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
680 (if (memq minor-mode '(gdbmi gdba))
681 (progn
682 (if event (posn-set-point (event-end event)))
683 (require 'tooltip)
684 (save-selected-window
685 (let ((expr (if arg
686 (completing-read "Name of variable: "
687 'gud-gdb-complete-command)
688 (tooltip-identifier-from-point (point)))))
689 (catch 'already-watched
690 (dolist (var gdb-var-list)
691 (unless (string-match "\\." (car var))
692 (if (string-equal expr (nth 1 var))
693 (throw 'already-watched nil))))
694 (set-text-properties 0 (length expr) nil expr)
695 (gdb-enqueue-input
696 (list
697 (if (eq minor-mode 'gdba)
698 (concat
699 "server interpreter mi \"-var-create - * " expr "\"\n")
700 (concat"-var-create - * " expr "\n"))
701 `(lambda () (gdb-var-create-handler ,expr))))))))
702 (message "gud-watch is a no-op in this mode."))))
703
704 (defconst gdb-var-create-regexp
705 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
706
707 (defun gdb-var-create-handler (expr)
708 (goto-char (point-min))
709 (if (re-search-forward gdb-var-create-regexp nil t)
710 (let ((var (list
711 (match-string 1)
712 (if (and (string-equal gdb-current-language "c")
713 gdb-use-colon-colon-notation gdb-selected-frame)
714 (setq expr (concat gdb-selected-frame "::" expr))
715 expr)
716 (match-string 2)
717 (match-string 3)
718 nil nil)))
719 (push var gdb-var-list)
720 (speedbar 1)
721 (unless (string-equal
722 speedbar-initial-expansion-list-name "GUD")
723 (speedbar-change-initial-expansion-list "GUD"))
724 (gdb-enqueue-input
725 (list
726 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
727 (concat "server interpreter mi \"-var-evaluate-expression "
728 (car var) "\"\n")
729 (concat "-var-evaluate-expression " (car var) "\n"))
730 `(lambda () (gdb-var-evaluate-expression-handler
731 ,(car var) nil)))))
732 (if (search-forward "Undefined command" nil t)
733 (message-box "Watching expressions requires gdb 6.0 onwards")
734 (message-box "No symbol \"%s\" in current context." expr))))
735
736 (defun gdb-var-evaluate-expression-handler (varnum changed)
737 (goto-char (point-min))
738 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
739 (let ((var (assoc varnum gdb-var-list)))
740 (when var
741 (if changed (setcar (nthcdr 5 var) 'changed))
742 (setcar (nthcdr 4 var) (read (match-string 1))))))
743
744 (defun gdb-var-list-children (varnum)
745 (gdb-enqueue-input
746 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
747 `(lambda () (gdb-var-list-children-handler ,varnum)))))
748
749 (defconst gdb-var-list-children-regexp
750 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",\
751 type=\"\\(.*?\\)\"")
752
753 (defun gdb-var-list-children-handler (varnum)
754 (goto-char (point-min))
755 (let ((var-list nil))
756 (catch 'child-already-watched
757 (dolist (var gdb-var-list)
758 (if (string-equal varnum (car var))
759 (progn
760 (push var var-list)
761 (while (re-search-forward gdb-var-list-children-regexp nil t)
762 (let ((varchild (list (match-string 1)
763 (match-string 2)
764 (match-string 3)
765 (match-string 4)
766 nil nil)))
767 (if (assoc (car varchild) gdb-var-list)
768 (throw 'child-already-watched nil))
769 (push varchild var-list)
770 (gdb-enqueue-input
771 (list
772 (concat
773 "server interpreter mi \"-var-evaluate-expression "
774 (car varchild) "\"\n")
775 `(lambda () (gdb-var-evaluate-expression-handler
776 ,(car varchild) nil)))))))
777 (push var var-list)))
778 (setq gdb-var-list (nreverse var-list)))))
779
780 (defun gdb-var-update ()
781 (when (not (member 'gdb-var-update gdb-pending-triggers))
782 (gdb-enqueue-input
783 (list "server interpreter mi \"-var-update *\"\n"
784 'gdb-var-update-handler))
785 (push 'gdb-var-update gdb-pending-triggers)))
786
787 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\",in_scope=\"\\(.*?\\)\"")
788
789 (defun gdb-var-update-handler ()
790 (dolist (var gdb-var-list)
791 (setcar (nthcdr 5 var) nil))
792 (goto-char (point-min))
793 (while (re-search-forward gdb-var-update-regexp nil t)
794 (let ((varnum (match-string 1)))
795 (if (string-equal (match-string 2) "false")
796 (let ((var (assoc varnum gdb-var-list)))
797 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
798 (gdb-enqueue-input
799 (list
800 (concat "server interpreter mi \"-var-evaluate-expression "
801 varnum "\"\n")
802 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t)))))))
803 (setq gdb-pending-triggers
804 (delq 'gdb-var-update gdb-pending-triggers))
805 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
806 ;; Dummy command to update speedbar at right time.
807 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
808 ;; Keep gdb-pending-triggers non-nil till end.
809 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
810
811 (defun gdb-speedbar-refresh ()
812 (setq gdb-pending-triggers
813 (delq 'gdb-speedbar-refresh gdb-pending-triggers))
814 (with-current-buffer gud-comint-buffer
815 (let ((speedbar-verbosity-level 0)
816 (speedbar-shown-directories nil))
817 (save-excursion
818 (speedbar-refresh)))))
819
820 (defun gdb-var-delete ()
821 "Delete watch expression at point from the speedbar."
822 (interactive)
823 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
824 '(gdbmi gdba))
825 (let ((text (speedbar-line-text)))
826 (string-match "\\(\\S-+\\)" text)
827 (let ((expr (match-string 1 text)) var varnum)
828 (catch 'expr-found
829 (dolist (var1 gdb-var-list)
830 (when (string-equal expr (nth 1 var1))
831 (setq var var1)
832 (setq varnum (car var1))
833 (throw 'expr-found nil))))
834 (unless (string-match "\\." (car var))
835 (gdb-enqueue-input
836 (list
837 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
838 'gdba)
839 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
840 (concat "-var-delete " varnum "\n"))
841 'ignore))
842 (setq gdb-var-list (delq var gdb-var-list))
843 (dolist (varchild gdb-var-list)
844 (if (string-match (concat (car var) "\\.") (car varchild))
845 (setq gdb-var-list (delq varchild gdb-var-list)))))))))
846
847 (defun gdb-edit-value (text token indent)
848 "Assign a value to a variable displayed in the speedbar."
849 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
850 (varnum (car var)) (value))
851 (setq value (read-string "New value: "))
852 (gdb-enqueue-input
853 (list
854 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
855 (concat "server interpreter mi \"-var-assign "
856 varnum " " value "\"\n")
857 (concat "-var-assign " varnum " " value "\n"))
858 `(lambda () (gdb-edit-value-handler ,value))))))
859
860 (defun gdb-edit-value-handler (value)
861 (goto-char (point-min))
862 (if (re-search-forward gdb-error-regexp nil t)
863 (message-box "Invalid number or expression (%s)" value)))
864
865 (defcustom gdb-show-changed-values t
866 "If non-nil change the face of out of scope variables and changed values.
867 Out of scope variables are suppressed with `shadow' face.
868 Changed values are highlighted with the face `font-lock-warning-face'."
869 :type 'boolean
870 :group 'gud
871 :version "22.1")
872
873 (defun gdb-speedbar-expand-node (text token indent)
874 "Expand the node the user clicked on.
875 TEXT is the text of the button we clicked on, a + or - item.
876 TOKEN is data related to this node.
877 INDENT is the current indentation depth."
878 (cond ((string-match "+" text) ;expand this node
879 (if (and
880 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
881 (string-equal gdb-version "pre-6.4"))
882 (gdb-var-list-children token)
883 (gdb-var-list-children-1 token)))
884 ((string-match "-" text) ;contract this node
885 (dolist (var gdb-var-list)
886 (if (string-match (concat token "\\.") (car var))
887 (setq gdb-var-list (delq var gdb-var-list))))
888 (speedbar-change-expand-button-char ?+)
889 (speedbar-delete-subblock indent))
890 (t (error "Ooops... not sure what to do")))
891 (speedbar-center-buffer-smartly))
892
893 (defun gdb-get-target-string ()
894 (with-current-buffer gud-comint-buffer
895 gud-target-name))
896 \f
897
898 ;;
899 ;; gdb buffers.
900 ;;
901 ;; Each buffer has a TYPE -- a symbol that identifies the function
902 ;; of that particular buffer.
903 ;;
904 ;; The usual gdb interaction buffer is given the type `gdba' and
905 ;; is constructed specially.
906 ;;
907 ;; Others are constructed by gdb-get-buffer-create and
908 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
909
910 (defvar gdb-buffer-rules-assoc '())
911
912 (defun gdb-get-buffer (key)
913 "Return the gdb buffer tagged with type KEY.
914 The key should be one of the cars in `gdb-buffer-rules-assoc'."
915 (save-excursion
916 (gdb-look-for-tagged-buffer key (buffer-list))))
917
918 (defun gdb-get-buffer-create (key)
919 "Create a new gdb buffer of the type specified by KEY.
920 The key should be one of the cars in `gdb-buffer-rules-assoc'."
921 (or (gdb-get-buffer key)
922 (let* ((rules (assoc key gdb-buffer-rules-assoc))
923 (name (funcall (gdb-rules-name-maker rules)))
924 (new (get-buffer-create name)))
925 (with-current-buffer new
926 (let ((trigger))
927 (if (cdr (cdr rules))
928 (setq trigger (funcall (car (cdr (cdr rules))))))
929 (setq gdb-buffer-type key)
930 (set (make-local-variable 'gud-minor-mode)
931 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
932 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
933 (if trigger (funcall trigger)))
934 new))))
935
936 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
937
938 (defun gdb-look-for-tagged-buffer (key bufs)
939 (let ((retval nil))
940 (while (and (not retval) bufs)
941 (set-buffer (car bufs))
942 (if (eq gdb-buffer-type key)
943 (setq retval (car bufs)))
944 (setq bufs (cdr bufs)))
945 retval))
946
947 ;;
948 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
949 ;; at least one and possible more functions. The functions have these
950 ;; roles in defining a buffer type:
951 ;;
952 ;; NAME - Return a name for this buffer type.
953 ;;
954 ;; The remaining function(s) are optional:
955 ;;
956 ;; MODE - called in a new buffer with no arguments, should establish
957 ;; the proper mode for the buffer.
958 ;;
959
960 (defun gdb-set-buffer-rules (buffer-type &rest rules)
961 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
962 (if binding
963 (setcdr binding rules)
964 (push (cons buffer-type rules)
965 gdb-buffer-rules-assoc))))
966
967 ;; GUD buffers are an exception to the rules
968 (gdb-set-buffer-rules 'gdba 'error)
969
970 ;; Partial-output buffer : This accumulates output from a command executed on
971 ;; behalf of emacs (rather than the user).
972 ;;
973 (gdb-set-buffer-rules 'gdb-partial-output-buffer
974 'gdb-partial-output-name)
975
976 (defun gdb-partial-output-name ()
977 (concat "*partial-output-"
978 (gdb-get-target-string)
979 "*"))
980
981 \f
982 (gdb-set-buffer-rules 'gdb-inferior-io
983 'gdb-inferior-io-name
984 'gdb-inferior-io-mode)
985
986 (defun gdb-inferior-io-name ()
987 (concat "*input/output of "
988 (gdb-get-target-string)
989 "*"))
990
991 (defun gdb-display-separate-io-buffer ()
992 "Display IO of debugged program in a separate window."
993 (interactive)
994 (if gdb-use-separate-io-buffer
995 (gdb-display-buffer
996 (gdb-get-buffer-create 'gdb-inferior-io))))
997
998 (defconst gdb-frame-parameters
999 '((height . 14) (width . 80)
1000 (unsplittable . t)
1001 (tool-bar-lines . nil)
1002 (menu-bar-lines . nil)
1003 (minibuffer . nil)))
1004
1005 (defun gdb-frame-separate-io-buffer ()
1006 "Display IO of inferior in a new frame."
1007 (interactive)
1008 (if gdb-use-separate-io-buffer
1009 (let ((special-display-regexps (append special-display-regexps '(".*")))
1010 (special-display-frame-alist gdb-frame-parameters))
1011 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1012
1013 (defvar gdb-inferior-io-mode-map
1014 (let ((map (make-sparse-keymap)))
1015 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1016 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1017 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1018 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1019 (define-key map "\C-d" 'gdb-separate-io-eof)
1020 map))
1021
1022 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1023 "Major mode for gdb inferior-io."
1024 :syntax-table nil :abbrev-table nil
1025 ;; We want to use comint because it has various nifty and familiar
1026 ;; features. We don't need a process, but comint wants one, so create
1027 ;; a dummy one.
1028 (make-comint-in-buffer
1029 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1030 (current-buffer) "hexl")
1031 (setq comint-input-sender 'gdb-inferior-io-sender))
1032
1033 (defun gdb-inferior-io-sender (proc string)
1034 ;; PROC is the pseudo-process created to satisfy comint.
1035 (with-current-buffer (process-buffer proc)
1036 (setq proc (get-buffer-process gud-comint-buffer))
1037 (process-send-string proc string)
1038 (process-send-string proc "\n")))
1039
1040 (defun gdb-separate-io-interrupt ()
1041 "Interrupt the program being debugged."
1042 (interactive)
1043 (interrupt-process
1044 (get-buffer-process gud-comint-buffer) comint-ptyp))
1045
1046 (defun gdb-separate-io-quit ()
1047 "Send quit signal to the program being debugged."
1048 (interactive)
1049 (quit-process
1050 (get-buffer-process gud-comint-buffer) comint-ptyp))
1051
1052 (defun gdb-separate-io-stop ()
1053 "Stop the program being debugged."
1054 (interactive)
1055 (stop-process
1056 (get-buffer-process gud-comint-buffer) comint-ptyp))
1057
1058 (defun gdb-separate-io-eof ()
1059 "Send end-of-file to the program being debugged."
1060 (interactive)
1061 (process-send-eof
1062 (get-buffer-process gud-comint-buffer)))
1063 \f
1064
1065 ;; gdb communications
1066 ;;
1067
1068 ;; INPUT: things sent to gdb
1069 ;;
1070 ;; The queues are lists. Each element is either a string (indicating user or
1071 ;; user-like input) or a list of the form:
1072 ;;
1073 ;; (INPUT-STRING HANDLER-FN)
1074 ;;
1075 ;; The handler function will be called from the partial-output buffer when the
1076 ;; command completes. This is the way to write commands which invoke gdb
1077 ;; commands autonomously.
1078 ;;
1079 ;; These lists are consumed tail first.
1080 ;;
1081 (defvar gdb-continuation nil)
1082
1083 (defun gdb-send (proc string)
1084 "A comint send filter for gdb.
1085 This filter may simply queue input for a later time."
1086 (with-current-buffer gud-comint-buffer
1087 (let ((inhibit-read-only t))
1088 (remove-text-properties (point-min) (point-max) '(face))))
1089 (if (string-match "\\\\$" string)
1090 (setq gdb-continuation (concat gdb-continuation string "\n"))
1091 (let ((item (concat gdb-continuation string "\n")))
1092 (if gud-running
1093 (progn
1094 (if gdb-enable-debug (push (cons 'send item) gdb-debug-ring))
1095 (process-send-string proc item))
1096 (gdb-enqueue-input item)))
1097 (setq gdb-continuation nil)))
1098
1099 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1100 ;; is a query, or other non-top-level prompt.
1101
1102 (defun gdb-enqueue-input (item)
1103 (if (not gud-running)
1104 (if gdb-prompting
1105 (progn
1106 (gdb-send-item item)
1107 (setq gdb-prompting nil))
1108 (push item gdb-input-queue))))
1109
1110 (defun gdb-dequeue-input ()
1111 (let ((queue gdb-input-queue))
1112 (and queue
1113 (let ((last (car (last queue))))
1114 (unless (nbutlast queue) (setq gdb-input-queue '()))
1115 last))))
1116
1117 (defun gdb-send-item (item)
1118 (setq gdb-flush-pending-output nil)
1119 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1120 (setq gdb-current-item item)
1121 (let ((process (get-buffer-process gud-comint-buffer)))
1122 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1123 (if (stringp item)
1124 (progn
1125 (setq gdb-output-sink 'user)
1126 (process-send-string process item))
1127 (progn
1128 (gdb-clear-partial-output)
1129 (setq gdb-output-sink 'pre-emacs)
1130 (process-send-string process
1131 (car item))))
1132 ;; case: eq gud-minor-mode 'gdbmi
1133 (gdb-clear-partial-output)
1134 (setq gdb-output-sink 'emacs)
1135 (process-send-string process (car item)))))
1136 \f
1137 ;;
1138 ;; output -- things gdb prints to emacs
1139 ;;
1140 ;; GDB output is a stream interrupted by annotations.
1141 ;; Annotations can be recognized by their beginning
1142 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1143 ;;
1144 ;; The tag is a string obeying symbol syntax.
1145 ;;
1146 ;; The optional part `<opt>' can be either the empty string
1147 ;; or a space followed by more data relating to the annotation.
1148 ;; For example, the SOURCE annotation is followed by a filename,
1149 ;; line number and various useless goo. This data must not include
1150 ;; any newlines.
1151 ;;
1152
1153 (defcustom gud-gdba-command-name "gdb -annotate=3"
1154 "Default command to execute an executable under the GDB-UI debugger."
1155 :type 'string
1156 :group 'gud
1157 :version "22.1")
1158
1159 (defvar gdb-annotation-rules
1160 '(("pre-prompt" gdb-pre-prompt)
1161 ("prompt" gdb-prompt)
1162 ("commands" gdb-subprompt)
1163 ("overload-choice" gdb-subprompt)
1164 ("query" gdb-subprompt)
1165 ;; Need this prompt for GDB 6.1
1166 ("nquery" gdb-subprompt)
1167 ("prompt-for-continue" gdb-subprompt)
1168 ("post-prompt" gdb-post-prompt)
1169 ("source" gdb-source)
1170 ("starting" gdb-starting)
1171 ("exited" gdb-exited)
1172 ("signalled" gdb-signalled)
1173 ("signal" gdb-signal)
1174 ("breakpoint" gdb-stopping)
1175 ("watchpoint" gdb-stopping)
1176 ("frame-begin" gdb-frame-begin)
1177 ("stopped" gdb-stopped)
1178 ("error-begin" gdb-error)
1179 ("error" gdb-error)
1180 ) "An assoc mapping annotation tags to functions which process them.")
1181
1182 (defun gdb-resync()
1183 (setq gdb-flush-pending-output t)
1184 (setq gud-running nil)
1185 (gdb-force-mode-line-update "stopped")
1186 (setq gdb-output-sink 'user)
1187 (setq gdb-input-queue nil)
1188 (setq gdb-pending-triggers nil)
1189 (setq gdb-prompting t))
1190
1191 (defconst gdb-source-spec-regexp
1192 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1193
1194 ;; Do not use this except as an annotation handler.
1195 (defun gdb-source (args)
1196 (string-match gdb-source-spec-regexp args)
1197 ;; Extract the frame position from the marker.
1198 (setq gud-last-frame
1199 (cons
1200 (match-string 1 args)
1201 (string-to-number (match-string 2 args))))
1202 (setq gdb-frame-address (match-string 3 args))
1203 ;; cover for auto-display output which comes *before*
1204 ;; stopped annotation
1205 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1206
1207 (defun gdb-pre-prompt (ignored)
1208 "An annotation handler for `pre-prompt'.
1209 This terminates the collection of output from a previous command if that
1210 happens to be in effect."
1211 (setq gdb-error nil)
1212 (let ((sink gdb-output-sink))
1213 (cond
1214 ((eq sink 'user) t)
1215 ((eq sink 'emacs)
1216 (setq gdb-output-sink 'post-emacs))
1217 (t
1218 (gdb-resync)
1219 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1220
1221 (defun gdb-prompt (ignored)
1222 "An annotation handler for `prompt'.
1223 This sends the next command (if any) to gdb."
1224 (when gdb-first-prompt
1225 (gdb-init-1)
1226 (setq gdb-first-prompt nil))
1227 (let ((sink gdb-output-sink))
1228 (cond
1229 ((eq sink 'user) t)
1230 ((eq sink 'post-emacs)
1231 (setq gdb-output-sink 'user)
1232 (let ((handler
1233 (car (cdr gdb-current-item))))
1234 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1235 (funcall handler))))
1236 (t
1237 (gdb-resync)
1238 (error "Phase error in gdb-prompt (got %s)" sink))))
1239 (let ((input (gdb-dequeue-input)))
1240 (if input
1241 (gdb-send-item input)
1242 (progn
1243 (setq gdb-prompting t)
1244 (gud-display-frame)))))
1245
1246 (defun gdb-subprompt (ignored)
1247 "An annotation handler for non-top-level prompts."
1248 (setq gdb-prompting t))
1249
1250 (defun gdb-starting (ignored)
1251 "An annotation handler for `starting'.
1252 This says that I/O for the subprocess is now the program being debugged,
1253 not GDB."
1254 (setq gdb-active-process t)
1255 (let ((sink gdb-output-sink))
1256 (cond
1257 ((eq sink 'user)
1258 (progn
1259 (setq gud-running t)
1260 (setq gdb-inferior-status "running")
1261 (gdb-force-mode-line-update gdb-inferior-status)
1262 (gdb-remove-text-properties)
1263 (setq gud-overlay-arrow-position nil)
1264 (setq gdb-overlay-arrow-position nil)
1265 (if gdb-use-separate-io-buffer
1266 (setq gdb-output-sink 'inferior))))
1267 (t
1268 (gdb-resync)
1269 (error "Unexpected `starting' annotation")))))
1270
1271 (defun gdb-signal (ignored)
1272 (setq gdb-inferior-status "signal")
1273 (gdb-force-mode-line-update gdb-inferior-status)
1274 (gdb-stopping ignored))
1275
1276 (defun gdb-stopping (ignored)
1277 "An annotation handler for `breakpoint' and other annotations.
1278 They say that I/O for the subprocess is now GDB, not the program
1279 being debugged."
1280 (if gdb-use-separate-io-buffer
1281 (let ((sink gdb-output-sink))
1282 (cond
1283 ((eq sink 'inferior)
1284 (setq gdb-output-sink 'user))
1285 (t
1286 (gdb-resync)
1287 (error "Unexpected stopping annotation"))))))
1288
1289 (defun gdb-exited (ignored)
1290 "An annotation handler for `exited' and `signalled'.
1291 They say that I/O for the subprocess is now GDB, not the program
1292 being debugged and that the program is no longer running. This
1293 function is used to change the focus of GUD tooltips to #define
1294 directives."
1295 (setq gdb-active-process nil)
1296 (setq gud-overlay-arrow-position nil)
1297 (setq gdb-overlay-arrow-position nil)
1298 (setq gdb-inferior-status "exited")
1299 (gdb-force-mode-line-update gdb-inferior-status)
1300 (gdb-stopping ignored))
1301
1302 (defun gdb-signalled (ignored)
1303 (setq gdb-signalled t))
1304
1305 (defun gdb-frame-begin (ignored)
1306 (let ((sink gdb-output-sink))
1307 (cond
1308 ((eq sink 'inferior)
1309 (setq gdb-output-sink 'user))
1310 ((eq sink 'user) t)
1311 ((eq sink 'emacs) t)
1312 (t
1313 (gdb-resync)
1314 (error "Unexpected frame-begin annotation (%S)" sink)))))
1315
1316 (defun gdb-stopped (ignored)
1317 "An annotation handler for `stopped'.
1318 It is just like `gdb-stopping', except that if we already set the output
1319 sink to `user' in `gdb-stopping', that is fine."
1320 (setq gud-running nil)
1321 (unless (or gud-overlay-arrow-position gud-last-frame)
1322 (gud-display-line (car gud-last-last-frame) (cdr gud-last-last-frame)))
1323 (unless (member gdb-inferior-status '("exited" "signal"))
1324 (setq gdb-inferior-status "stopped")
1325 (gdb-force-mode-line-update gdb-inferior-status))
1326 (let ((sink gdb-output-sink))
1327 (cond
1328 ((eq sink 'inferior)
1329 (setq gdb-output-sink 'user))
1330 ((eq sink 'user) t)
1331 (t
1332 (gdb-resync)
1333 (error "Unexpected stopped annotation"))))
1334 (if gdb-signalled (gdb-exited ignored)))
1335
1336 (defun gdb-error (ignored)
1337 (setq gdb-error (not gdb-error)))
1338
1339 (defun gdb-post-prompt (ignored)
1340 "An annotation handler for `post-prompt'.
1341 This begins the collection of output from the current command if that
1342 happens to be appropriate."
1343 ;; Don't add to queue if there outstanding items or gdb-version is not known
1344 ;; yet.
1345 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1346 (gdb-get-selected-frame)
1347 (gdb-invalidate-frames)
1348 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1349 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1350 (gdb-invalidate-breakpoints)
1351 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1352 ;; so gdb-frame-address is updated.
1353 ;; (gdb-invalidate-assembler)
1354
1355 (if (string-equal gdb-version "pre-6.4")
1356 (gdb-invalidate-registers)
1357 (gdb-get-changed-registers)
1358 (gdb-invalidate-registers-1))
1359
1360 (gdb-invalidate-memory)
1361 (if (string-equal gdb-version "pre-6.4")
1362 (gdb-invalidate-locals)
1363 (gdb-invalidate-locals-1))
1364
1365 (gdb-invalidate-threads)
1366 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1367 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1368 ;; Only needed/used with speedbar/watch expressions.
1369 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1370 (setq gdb-force-update t)
1371 (if (string-equal gdb-version "pre-6.4")
1372 (gdb-var-update)
1373 (gdb-var-update-1)))))
1374 (setq gdb-first-post-prompt nil)
1375 (let ((sink gdb-output-sink))
1376 (cond
1377 ((eq sink 'user) t)
1378 ((eq sink 'pre-emacs)
1379 (setq gdb-output-sink 'emacs))
1380 (t
1381 (gdb-resync)
1382 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1383
1384 (defconst gdb-buffer-list
1385 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1386
1387 (defun gdb-remove-text-properties ()
1388 (dolist (buffertype gdb-buffer-list)
1389 (let ((buffer (gdb-get-buffer buffertype)))
1390 (if buffer
1391 (with-current-buffer buffer
1392 (let ((inhibit-read-only t))
1393 (remove-text-properties
1394 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1395
1396 ;; GUD displays the selected GDB frame. This might might not be the current
1397 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1398 ;; visited breakpoint is, use that window.
1399 (defun gdb-display-source-buffer (buffer)
1400 (let* ((last-window (if gud-last-last-frame
1401 (get-buffer-window
1402 (gud-find-file (car gud-last-last-frame)))))
1403 (source-window (or last-window
1404 (if (and gdb-source-window
1405 (window-live-p gdb-source-window))
1406 gdb-source-window))))
1407 (when source-window
1408 (setq gdb-source-window source-window)
1409 (set-window-buffer source-window buffer))
1410 source-window))
1411
1412 (defun gud-gdba-marker-filter (string)
1413 "A gud marker filter for gdb. Handle a burst of output from GDB."
1414 (if gdb-flush-pending-output
1415 nil
1416 (when gdb-enable-debug
1417 (push (cons 'recv string) gdb-debug-ring)
1418 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1419 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1420 ;; Recall the left over gud-marker-acc from last time.
1421 (setq gud-marker-acc (concat gud-marker-acc string))
1422 ;; Start accumulating output for the GUD buffer.
1423 (let ((output ""))
1424 ;;
1425 ;; Process all the complete markers in this chunk.
1426 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1427 (let ((annotation (match-string 1 gud-marker-acc)))
1428 ;;
1429 ;; Stuff prior to the match is just ordinary output.
1430 ;; It is either concatenated to OUTPUT or directed
1431 ;; elsewhere.
1432 (setq output
1433 (gdb-concat-output
1434 output
1435 (substring gud-marker-acc 0 (match-beginning 0))))
1436 ;;
1437 ;; Take that stuff off the gud-marker-acc.
1438 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1439 ;;
1440 ;; Parse the tag from the annotation, and maybe its arguments.
1441 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1442 (let* ((annotation-type (match-string 1 annotation))
1443 (annotation-arguments (match-string 2 annotation))
1444 (annotation-rule (assoc annotation-type
1445 gdb-annotation-rules)))
1446 ;; Call the handler for this annotation.
1447 (if annotation-rule
1448 (funcall (car (cdr annotation-rule))
1449 annotation-arguments)
1450 ;; Else the annotation is not recognized. Ignore it silently,
1451 ;; so that GDB can add new annotations without causing
1452 ;; us to blow up.
1453 ))))
1454 ;;
1455 ;; Does the remaining text end in a partial line?
1456 ;; If it does, then keep part of the gud-marker-acc until we get more.
1457 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1458 gud-marker-acc)
1459 (progn
1460 ;; Everything before the potential marker start can be output.
1461 (setq output
1462 (gdb-concat-output output
1463 (substring gud-marker-acc 0
1464 (match-beginning 0))))
1465 ;;
1466 ;; Everything after, we save, to combine with later input.
1467 (setq gud-marker-acc (substring gud-marker-acc
1468 (match-beginning 0))))
1469 ;;
1470 ;; In case we know the gud-marker-acc contains no partial annotations:
1471 (progn
1472 (setq output (gdb-concat-output output gud-marker-acc))
1473 (setq gud-marker-acc "")))
1474 output)))
1475
1476 (defun gdb-concat-output (so-far new)
1477 (if gdb-error
1478 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1479 (let ((sink gdb-output-sink))
1480 (cond
1481 ((eq sink 'user) (concat so-far new))
1482 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1483 ((eq sink 'emacs)
1484 (gdb-append-to-partial-output new)
1485 so-far)
1486 ((eq sink 'inferior)
1487 (gdb-append-to-inferior-io new)
1488 so-far)
1489 (t
1490 (gdb-resync)
1491 (error "Bogon output sink %S" sink)))))
1492
1493 (defun gdb-append-to-partial-output (string)
1494 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1495 (goto-char (point-max))
1496 (insert string)))
1497
1498 (defun gdb-clear-partial-output ()
1499 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1500 (erase-buffer)))
1501
1502 (defun gdb-append-to-inferior-io (string)
1503 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1504 (goto-char (point-max))
1505 (insert-before-markers string))
1506 (if (not (string-equal string ""))
1507 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1508
1509 (defun gdb-clear-inferior-io ()
1510 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1511 (erase-buffer)))
1512 \f
1513
1514 ;; One trick is to have a command who's output is always available in a buffer
1515 ;; of it's own, and is always up to date. We build several buffers of this
1516 ;; type.
1517 ;;
1518 ;; There are two aspects to this: gdb has to tell us when the output for that
1519 ;; command might have changed, and we have to be able to run the command
1520 ;; behind the user's back.
1521 ;;
1522 ;; The output phasing associated with the variable gdb-output-sink
1523 ;; help us to run commands behind the user's back.
1524 ;;
1525 ;; Below is the code for specificly managing buffers of output from one
1526 ;; command.
1527 ;;
1528
1529 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1530 ;; It adds an input for the command we are tracking. It should be the
1531 ;; annotation rule binding of whatever gdb sends to tell us this command
1532 ;; might have changed it's output.
1533 ;;
1534 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1535 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1536 ;; input in the input queue (see comment about ``gdb communications'' above).
1537
1538 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1539 output-handler)
1540 `(defun ,name (&optional ignored)
1541 (if (and ,demand-predicate
1542 (not (member ',name
1543 gdb-pending-triggers)))
1544 (progn
1545 (gdb-enqueue-input
1546 (list ,gdb-command ',output-handler))
1547 (push ',name gdb-pending-triggers)))))
1548
1549 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1550 `(defun ,name ()
1551 (setq gdb-pending-triggers
1552 (delq ',trigger
1553 gdb-pending-triggers))
1554 (let ((buf (gdb-get-buffer ',buf-key)))
1555 (and buf
1556 (with-current-buffer buf
1557 (let* ((window (get-buffer-window buf 0))
1558 (start (window-start window))
1559 (p (window-point window))
1560 (buffer-read-only nil))
1561 (erase-buffer)
1562 (insert-buffer-substring (gdb-get-buffer-create
1563 'gdb-partial-output-buffer))
1564 (set-window-start window start)
1565 (set-window-point window p)))))
1566 ;; put customisation here
1567 (,custom-defun)))
1568
1569 (defmacro def-gdb-auto-updated-buffer (buffer-key
1570 trigger-name gdb-command
1571 output-handler-name custom-defun)
1572 `(progn
1573 (def-gdb-auto-update-trigger ,trigger-name
1574 ;; The demand predicate:
1575 (gdb-get-buffer ',buffer-key)
1576 ,gdb-command
1577 ,output-handler-name)
1578 (def-gdb-auto-update-handler ,output-handler-name
1579 ,trigger-name ,buffer-key ,custom-defun)))
1580
1581 \f
1582 ;;
1583 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1584 ;;
1585 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1586 'gdb-breakpoints-buffer-name
1587 'gdb-breakpoints-mode)
1588
1589 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1590 ;; This defines the auto update rule for buffers of type
1591 ;; `gdb-breakpoints-buffer'.
1592 ;;
1593 ;; It defines a function to serve as the annotation handler that
1594 ;; handles the `foo-invalidated' message. That function is called:
1595 gdb-invalidate-breakpoints
1596 ;;
1597 ;; To update the buffer, this command is sent to gdb.
1598 "server info breakpoints\n"
1599 ;;
1600 ;; This also defines a function to be the handler for the output
1601 ;; from the command above. That function will copy the output into
1602 ;; the appropriately typed buffer. That function will be called:
1603 gdb-info-breakpoints-handler
1604 ;; buffer specific functions
1605 gdb-info-breakpoints-custom)
1606
1607 (defconst breakpoint-xpm-data
1608 "/* XPM */
1609 static char *magick[] = {
1610 /* columns rows colors chars-per-pixel */
1611 \"10 10 2 1\",
1612 \" c red\",
1613 \"+ c None\",
1614 /* pixels */
1615 \"+++ +++\",
1616 \"++ ++\",
1617 \"+ +\",
1618 \" \",
1619 \" \",
1620 \" \",
1621 \" \",
1622 \"+ +\",
1623 \"++ ++\",
1624 \"+++ +++\",
1625 };"
1626 "XPM data used for breakpoint icon.")
1627
1628 (defconst breakpoint-enabled-pbm-data
1629 "P1
1630 10 10\",
1631 0 0 0 0 1 1 1 1 0 0 0 0
1632 0 0 0 1 1 1 1 1 1 0 0 0
1633 0 0 1 1 1 1 1 1 1 1 0 0
1634 0 1 1 1 1 1 1 1 1 1 1 0
1635 0 1 1 1 1 1 1 1 1 1 1 0
1636 0 1 1 1 1 1 1 1 1 1 1 0
1637 0 1 1 1 1 1 1 1 1 1 1 0
1638 0 0 1 1 1 1 1 1 1 1 0 0
1639 0 0 0 1 1 1 1 1 1 0 0 0
1640 0 0 0 0 1 1 1 1 0 0 0 0"
1641 "PBM data used for enabled breakpoint icon.")
1642
1643 (defconst breakpoint-disabled-pbm-data
1644 "P1
1645 10 10\",
1646 0 0 1 0 1 0 1 0 0 0
1647 0 1 0 1 0 1 0 1 0 0
1648 1 0 1 0 1 0 1 0 1 0
1649 0 1 0 1 0 1 0 1 0 1
1650 1 0 1 0 1 0 1 0 1 0
1651 0 1 0 1 0 1 0 1 0 1
1652 1 0 1 0 1 0 1 0 1 0
1653 0 1 0 1 0 1 0 1 0 1
1654 0 0 1 0 1 0 1 0 1 0
1655 0 0 0 1 0 1 0 1 0 0"
1656 "PBM data used for disabled breakpoint icon.")
1657
1658 (defvar breakpoint-enabled-icon nil
1659 "Icon for enabled breakpoint in display margin.")
1660
1661 (defvar breakpoint-disabled-icon nil
1662 "Icon for disabled breakpoint in display margin.")
1663
1664 ;; Bitmap for breakpoint in fringe
1665 (and (display-images-p)
1666 (define-fringe-bitmap 'breakpoint
1667 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1668
1669 (defface breakpoint-enabled
1670 '((t
1671 :foreground "red"
1672 :weight bold))
1673 "Face for enabled breakpoint icon in fringe."
1674 :group 'gud)
1675
1676 (defface breakpoint-disabled
1677 ;; We use different values of grey for different background types,
1678 ;; so that on low-color displays it will end up as something visible
1679 ;; if it has to be approximated.
1680 '((((background dark)) :foreground "grey60")
1681 (((background light)) :foreground "grey40"))
1682 "Face for disabled breakpoint icon in fringe."
1683 :group 'gud)
1684
1685 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1686 (defun gdb-info-breakpoints-custom ()
1687 (let ((flag) (bptno))
1688 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1689 (dolist (buffer (buffer-list))
1690 (with-current-buffer buffer
1691 (if (and (memq gud-minor-mode '(gdba gdbmi))
1692 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1693 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1694 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1695 (save-excursion
1696 (goto-char (point-min))
1697 (while (< (point) (- (point-max) 1))
1698 (forward-line 1)
1699 (if (looking-at "[^\t].*?breakpoint")
1700 (progn
1701 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1702 (setq bptno (match-string 1))
1703 (setq flag (char-after (match-beginning 2)))
1704 (beginning-of-line)
1705 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1706 (progn
1707 (let ((buffer-read-only nil))
1708 (add-text-properties (match-beginning 1) (match-end 1)
1709 '(face font-lock-function-name-face)))
1710 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1711 (let ((line (match-string 2)) (buffer-read-only nil)
1712 (file (match-string 1)))
1713 (add-text-properties (line-beginning-position)
1714 (line-end-position)
1715 '(mouse-face highlight
1716 help-echo "mouse-2, RET: visit breakpoint"))
1717 (unless (file-exists-p file)
1718 (setq file (cdr (assoc bptno gdb-location-alist))))
1719 (if (and file
1720 (not (string-equal file "File not found")))
1721 (with-current-buffer
1722 (find-file-noselect file 'nowarn)
1723 (set (make-local-variable 'gud-minor-mode)
1724 'gdba)
1725 (set (make-local-variable 'tool-bar-map)
1726 gud-tool-bar-map)
1727 ;; Only want one breakpoint icon at each
1728 ;; location.
1729 (save-excursion
1730 (goto-line (string-to-number line))
1731 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1732 (gdb-enqueue-input
1733 (list
1734 (concat gdb-server-prefix "list "
1735 (match-string-no-properties 1) ":1\n")
1736 'ignore))
1737 (gdb-enqueue-input
1738 (list (concat gdb-server-prefix "info source\n")
1739 `(lambda () (gdb-get-location
1740 ,bptno ,line ,flag))))))))))
1741 (end-of-line)))))
1742 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1743
1744 (defun gdb-mouse-set-clear-breakpoint (event)
1745 "Set/clear breakpoint in left fringe/margin with mouse click."
1746 (interactive "e")
1747 (mouse-minibuffer-check event)
1748 (let ((posn (event-end event)))
1749 (if (numberp (posn-point posn))
1750 (with-selected-window (posn-window posn)
1751 (save-excursion
1752 (goto-char (posn-point posn))
1753 (if (or (posn-object posn)
1754 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1755 'breakpoint))
1756 (gud-remove nil)
1757 (gud-break nil)))))))
1758
1759 (defun gdb-mouse-toggle-breakpoint-margin (event)
1760 "Enable/disable breakpoint in left margin with mouse click."
1761 (interactive "e")
1762 (mouse-minibuffer-check event)
1763 (let ((posn (event-end event)))
1764 (if (numberp (posn-point posn))
1765 (with-selected-window (posn-window posn)
1766 (save-excursion
1767 (goto-char (posn-point posn))
1768 (if (posn-object posn)
1769 (gdb-enqueue-input
1770 (list
1771 (let ((bptno (get-text-property
1772 0 'gdb-bptno (car (posn-string posn)))))
1773 (concat gdb-server-prefix
1774 (if (get-text-property
1775 0 'gdb-enabled (car (posn-string posn)))
1776 "disable "
1777 "enable ")
1778 bptno "\n"))
1779 'ignore))))))))
1780
1781 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1782 "Enable/disable breakpoint in left fringe with mouse click."
1783 (interactive "e")
1784 (mouse-minibuffer-check event)
1785 (let* ((posn (event-end event))
1786 (pos (posn-point posn))
1787 obj)
1788 (when (numberp pos)
1789 (with-selected-window (posn-window posn)
1790 (save-excursion
1791 (set-buffer (window-buffer (selected-window)))
1792 (goto-char pos)
1793 (dolist (overlay (overlays-in pos pos))
1794 (when (overlay-get overlay 'put-break)
1795 (setq obj (overlay-get overlay 'before-string))))
1796 (when (stringp obj)
1797 (gdb-enqueue-input
1798 (list
1799 (concat gdb-server-prefix
1800 (if (get-text-property 0 'gdb-enabled obj)
1801 "disable "
1802 "enable ")
1803 (get-text-property 0 'gdb-bptno obj) "\n")
1804 'ignore))))))))
1805
1806 (defun gdb-breakpoints-buffer-name ()
1807 (with-current-buffer gud-comint-buffer
1808 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1809
1810 (defun gdb-display-breakpoints-buffer ()
1811 "Display status of user-settable breakpoints."
1812 (interactive)
1813 (gdb-display-buffer
1814 (gdb-get-buffer-create 'gdb-breakpoints-buffer)))
1815
1816 (defun gdb-frame-breakpoints-buffer ()
1817 "Display status of user-settable breakpoints in a new frame."
1818 (interactive)
1819 (let ((special-display-regexps (append special-display-regexps '(".*")))
1820 (special-display-frame-alist gdb-frame-parameters))
1821 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1822
1823 (defvar gdb-breakpoints-mode-map
1824 (let ((map (make-sparse-keymap))
1825 (menu (make-sparse-keymap "Breakpoints")))
1826 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1827 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1828 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1829 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1830 (suppress-keymap map)
1831 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1832 (define-key map " " 'gdb-toggle-breakpoint)
1833 (define-key map "D" 'gdb-delete-breakpoint)
1834 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1835 (define-key map "q" 'gdb-delete-frame-or-window)
1836 (define-key map "\r" 'gdb-goto-breakpoint)
1837 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1838 (define-key map [follow-link] 'mouse-face)
1839 map))
1840
1841 (defun gdb-delete-frame-or-window ()
1842 "Delete frame if there is only one window. Otherwise delete the window."
1843 (interactive)
1844 (if (one-window-p) (delete-frame)
1845 (delete-window)))
1846
1847 (defun gdb-breakpoints-mode ()
1848 "Major mode for gdb breakpoints.
1849
1850 \\{gdb-breakpoints-mode-map}"
1851 (kill-all-local-variables)
1852 (setq major-mode 'gdb-breakpoints-mode)
1853 (setq mode-name "Breakpoints")
1854 (use-local-map gdb-breakpoints-mode-map)
1855 (setq buffer-read-only t)
1856 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1857 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1858 'gdb-invalidate-breakpoints
1859 'gdbmi-invalidate-breakpoints))
1860
1861 (defconst gdb-breakpoint-regexp
1862 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1863
1864 (defun gdb-toggle-breakpoint ()
1865 "Enable/disable breakpoint at current line."
1866 (interactive)
1867 (save-excursion
1868 (beginning-of-line 1)
1869 (if (looking-at gdb-breakpoint-regexp)
1870 (gdb-enqueue-input
1871 (list
1872 (concat gdb-server-prefix
1873 (if (eq ?y (char-after (match-beginning 2)))
1874 "disable "
1875 "enable ")
1876 (match-string 1) "\n") 'ignore))
1877 (error "Not recognized as break/watchpoint line"))))
1878
1879 (defun gdb-delete-breakpoint ()
1880 "Delete the breakpoint at current line."
1881 (interactive)
1882 (beginning-of-line 1)
1883 (if (looking-at gdb-breakpoint-regexp)
1884 (gdb-enqueue-input
1885 (list
1886 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1887 (error "Not recognized as break/watchpoint line")))
1888
1889 (defun gdb-goto-breakpoint (&optional event)
1890 "Display the breakpoint location specified at current line."
1891 (interactive (list last-input-event))
1892 (if event (posn-set-point (event-end event)))
1893 (save-excursion
1894 (beginning-of-line 1)
1895 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1896 (let ((bptno (match-string 1))
1897 (file (match-string 2))
1898 (line (match-string 3)))
1899 (save-selected-window
1900 (let* ((buffer (find-file-noselect
1901 (if (file-exists-p file) file
1902 (cdr (assoc bptno gdb-location-alist)))))
1903 (window (unless (gdb-display-source-buffer buffer)
1904 (display-buffer buffer))))
1905 (setq gdb-source-window window)
1906 (with-current-buffer buffer
1907 (goto-line (string-to-number line))
1908 (set-window-point window (point))))))
1909 (error "No location specified."))))
1910 \f
1911
1912 ;; Frames buffer. This displays a perpetually correct bactracktrace
1913 ;; (from the command `where').
1914 ;;
1915 ;; Alas, if your stack is deep, it is costly.
1916 ;;
1917 (gdb-set-buffer-rules 'gdb-stack-buffer
1918 'gdb-stack-buffer-name
1919 'gdb-frames-mode)
1920
1921 (def-gdb-auto-updated-buffer gdb-stack-buffer
1922 gdb-invalidate-frames
1923 "server where\n"
1924 gdb-info-frames-handler
1925 gdb-info-frames-custom)
1926
1927 (defun gdb-info-frames-custom ()
1928 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1929 (save-excursion
1930 (let ((buffer-read-only nil)
1931 bl el)
1932 (goto-char (point-min))
1933 (while (< (point) (point-max))
1934 (setq bl (line-beginning-position)
1935 el (line-end-position))
1936 (when (looking-at "#")
1937 (add-text-properties bl el
1938 '(mouse-face highlight
1939 help-echo "mouse-2, RET: Select frame")))
1940 (goto-char bl)
1941 (when (looking-at "^#\\([0-9]+\\)")
1942 (when (string-equal (match-string 1) gdb-frame-number)
1943 (put-text-property bl (+ bl 4)
1944 'face '(:inverse-video t)))
1945 (when (re-search-forward
1946 (concat
1947 (if (string-equal (match-string 1) "0") "" " in ")
1948 "\\([^ ]+\\) (") el t)
1949 (put-text-property (match-beginning 1) (match-end 1)
1950 'face font-lock-function-name-face)
1951 (setq bl (match-end 0))
1952 (while (re-search-forward "<\\([^>]+\\)>" el t)
1953 (put-text-property (match-beginning 1) (match-end 1)
1954 'face font-lock-function-name-face))
1955 (goto-char bl)
1956 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1957 (put-text-property (match-beginning 1) (match-end 1)
1958 'face font-lock-variable-name-face))))
1959 (forward-line 1))))))
1960
1961 (defun gdb-stack-buffer-name ()
1962 (with-current-buffer gud-comint-buffer
1963 (concat "*stack frames of " (gdb-get-target-string) "*")))
1964
1965 (defun gdb-display-stack-buffer ()
1966 "Display backtrace of current stack."
1967 (interactive)
1968 (gdb-display-buffer
1969 (gdb-get-buffer-create 'gdb-stack-buffer)))
1970
1971 (defun gdb-frame-stack-buffer ()
1972 "Display backtrace of current stack in a new frame."
1973 (interactive)
1974 (let ((special-display-regexps (append special-display-regexps '(".*")))
1975 (special-display-frame-alist gdb-frame-parameters))
1976 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
1977
1978 (defvar gdb-frames-mode-map
1979 (let ((map (make-sparse-keymap)))
1980 (suppress-keymap map)
1981 (define-key map "q" 'kill-this-buffer)
1982 (define-key map "\r" 'gdb-frames-select)
1983 (define-key map [mouse-2] 'gdb-frames-select)
1984 (define-key map [follow-link] 'mouse-face)
1985 map))
1986
1987 (defun gdb-frames-mode ()
1988 "Major mode for gdb frames.
1989
1990 \\{gdb-frames-mode-map}"
1991 (kill-all-local-variables)
1992 (setq major-mode 'gdb-frames-mode)
1993 (setq mode-name "Frames")
1994 (setq buffer-read-only t)
1995 (use-local-map gdb-frames-mode-map)
1996 (run-mode-hooks 'gdb-frames-mode-hook)
1997 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1998 'gdb-invalidate-frames
1999 'gdbmi-invalidate-frames))
2000
2001 (defun gdb-get-frame-number ()
2002 (save-excursion
2003 (end-of-line)
2004 (let* ((start (line-beginning-position))
2005 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2006 (n (or (and pos (match-string-no-properties 1)) "0")))
2007 n)))
2008
2009 (defun gdb-frames-select (&optional event)
2010 "Select the frame and display the relevant source."
2011 (interactive (list last-input-event))
2012 (if event (posn-set-point (event-end event)))
2013 (gdb-enqueue-input
2014 (list (concat gdb-server-prefix "frame "
2015 (gdb-get-frame-number) "\n") 'ignore))
2016 (gud-display-frame))
2017 \f
2018
2019 ;; Threads buffer. This displays a selectable thread list.
2020 ;;
2021 (gdb-set-buffer-rules 'gdb-threads-buffer
2022 'gdb-threads-buffer-name
2023 'gdb-threads-mode)
2024
2025 (def-gdb-auto-updated-buffer gdb-threads-buffer
2026 gdb-invalidate-threads
2027 (concat gdb-server-prefix "info threads\n")
2028 gdb-info-threads-handler
2029 gdb-info-threads-custom)
2030
2031 (defun gdb-info-threads-custom ()
2032 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2033 (let ((buffer-read-only nil))
2034 (goto-char (point-min))
2035 (while (< (point) (point-max))
2036 (unless (looking-at "No ")
2037 (add-text-properties (line-beginning-position) (line-end-position)
2038 '(mouse-face highlight
2039 help-echo "mouse-2, RET: select thread")))
2040 (forward-line 1)))))
2041
2042 (defun gdb-threads-buffer-name ()
2043 (with-current-buffer gud-comint-buffer
2044 (concat "*threads of " (gdb-get-target-string) "*")))
2045
2046 (defun gdb-display-threads-buffer ()
2047 "Display IDs of currently known threads."
2048 (interactive)
2049 (gdb-display-buffer
2050 (gdb-get-buffer-create 'gdb-threads-buffer)))
2051
2052 (defun gdb-frame-threads-buffer ()
2053 "Display IDs of currently known threads in a new frame."
2054 (interactive)
2055 (let ((special-display-regexps (append special-display-regexps '(".*")))
2056 (special-display-frame-alist gdb-frame-parameters))
2057 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2058
2059 (defvar gdb-threads-mode-map
2060 (let ((map (make-sparse-keymap)))
2061 (suppress-keymap map)
2062 (define-key map "q" 'kill-this-buffer)
2063 (define-key map "\r" 'gdb-threads-select)
2064 (define-key map [mouse-2] 'gdb-threads-select)
2065 (define-key map [follow-link] 'mouse-face)
2066 map))
2067
2068 (defvar gdb-threads-font-lock-keywords
2069 '(
2070 (") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2071 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2072 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face))
2073 )
2074 "Font lock keywords used in `gdb-threads-mode'.")
2075
2076 (defun gdb-threads-mode ()
2077 "Major mode for gdb frames.
2078
2079 \\{gdb-threads-mode-map}"
2080 (kill-all-local-variables)
2081 (setq major-mode 'gdb-threads-mode)
2082 (setq mode-name "Threads")
2083 (setq buffer-read-only t)
2084 (use-local-map gdb-threads-mode-map)
2085 (set (make-local-variable 'font-lock-defaults)
2086 '(gdb-threads-font-lock-keywords))
2087 (run-mode-hooks 'gdb-threads-mode-hook)
2088 'gdb-invalidate-threads)
2089
2090 (defun gdb-get-thread-number ()
2091 (save-excursion
2092 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2093 (match-string-no-properties 1)))
2094
2095 (defun gdb-threads-select (&optional event)
2096 "Select the thread and display the relevant source."
2097 (interactive (list last-input-event))
2098 (if event (posn-set-point (event-end event)))
2099 (gdb-enqueue-input
2100 (list (concat gdb-server-prefix "thread "
2101 (gdb-get-thread-number) "\n") 'ignore))
2102 (gud-display-frame))
2103 \f
2104
2105 ;; Registers buffer.
2106 ;;
2107 (defcustom gdb-all-registers nil
2108 "Non-nil means include floating-point registers."
2109 :type 'boolean
2110 :group 'gud
2111 :version "22.1")
2112
2113 (gdb-set-buffer-rules 'gdb-registers-buffer
2114 'gdb-registers-buffer-name
2115 'gdb-registers-mode)
2116
2117 (def-gdb-auto-updated-buffer gdb-registers-buffer
2118 gdb-invalidate-registers
2119 (concat
2120 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2121 gdb-info-registers-handler
2122 gdb-info-registers-custom)
2123
2124 (defun gdb-info-registers-custom ()
2125 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2126 (save-excursion
2127 (let ((buffer-read-only nil)
2128 start end)
2129 (goto-char (point-min))
2130 (while (< (point) (point-max))
2131 (setq start (line-beginning-position))
2132 (setq end (line-end-position))
2133 (when (looking-at "^[^ ]+")
2134 (unless (string-equal (match-string 0) "The")
2135 (put-text-property start (match-end 0)
2136 'face font-lock-variable-name-face)
2137 (add-text-properties start end
2138 '(help-echo "mouse-2: edit value"
2139 mouse-face highlight))))
2140 (forward-line 1))))))
2141
2142 (defun gdb-edit-register-value (&optional event)
2143 (interactive (list last-input-event))
2144 (save-excursion
2145 (if event (posn-set-point (event-end event)))
2146 (beginning-of-line)
2147 (let* ((register (current-word))
2148 (value (read-string (format "New value (%s): " register))))
2149 (gdb-enqueue-input
2150 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2151 'ignore)))))
2152
2153 (defvar gdb-registers-mode-map
2154 (let ((map (make-sparse-keymap)))
2155 (suppress-keymap map)
2156 (define-key map "\r" 'gdb-edit-register-value)
2157 (define-key map [mouse-2] 'gdb-edit-register-value)
2158 (define-key map " " 'gdb-all-registers)
2159 (define-key map "q" 'kill-this-buffer)
2160 map))
2161
2162 (defun gdb-registers-mode ()
2163 "Major mode for gdb registers.
2164
2165 \\{gdb-registers-mode-map}"
2166 (kill-all-local-variables)
2167 (setq major-mode 'gdb-registers-mode)
2168 (setq mode-name "Registers")
2169 (setq buffer-read-only t)
2170 (use-local-map gdb-registers-mode-map)
2171 (run-mode-hooks 'gdb-registers-mode-hook)
2172 (if (string-equal gdb-version "pre-6.4")
2173 (progn
2174 (if gdb-all-registers (setq mode-name "Registers:All"))
2175 'gdb-invalidate-registers)
2176 'gdb-invalidate-registers-1))
2177
2178 (defun gdb-registers-buffer-name ()
2179 (with-current-buffer gud-comint-buffer
2180 (concat "*registers of " (gdb-get-target-string) "*")))
2181
2182 (defun gdb-display-registers-buffer ()
2183 "Display integer register contents."
2184 (interactive)
2185 (gdb-display-buffer
2186 (gdb-get-buffer-create 'gdb-registers-buffer)))
2187
2188 (defun gdb-frame-registers-buffer ()
2189 "Display integer register contents in a new frame."
2190 (interactive)
2191 (let ((special-display-regexps (append special-display-regexps '(".*")))
2192 (special-display-frame-alist gdb-frame-parameters))
2193 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2194
2195 (defun gdb-all-registers ()
2196 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2197 (interactive)
2198 (when (string-equal gdb-version "pre-6.4")
2199 (if gdb-all-registers
2200 (progn
2201 (setq gdb-all-registers nil)
2202 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2203 (setq mode-name "Registers")))
2204 (setq gdb-all-registers t)
2205 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2206 (setq mode-name "Registers:All")))
2207 (message (format "Display of floating-point registers %sabled"
2208 (if gdb-all-registers "en" "dis")))
2209 (gdb-invalidate-registers)))
2210 \f
2211
2212 ;; Memory buffer.
2213 ;;
2214 (defcustom gdb-memory-repeat-count 32
2215 "Number of data items in memory window."
2216 :type 'integer
2217 :group 'gud
2218 :version "22.1")
2219
2220 (defcustom gdb-memory-format "x"
2221 "Display format of data items in memory window."
2222 :type '(choice (const :tag "Hexadecimal" "x")
2223 (const :tag "Signed decimal" "d")
2224 (const :tag "Unsigned decimal" "u")
2225 (const :tag "Octal" "o")
2226 (const :tag "Binary" "t"))
2227 :group 'gud
2228 :version "22.1")
2229
2230 (defcustom gdb-memory-unit "w"
2231 "Unit size of data items in memory window."
2232 :type '(choice (const :tag "Byte" "b")
2233 (const :tag "Halfword" "h")
2234 (const :tag "Word" "w")
2235 (const :tag "Giant word" "g"))
2236 :group 'gud
2237 :version "22.1")
2238
2239 (gdb-set-buffer-rules 'gdb-memory-buffer
2240 'gdb-memory-buffer-name
2241 'gdb-memory-mode)
2242
2243 (def-gdb-auto-updated-buffer gdb-memory-buffer
2244 gdb-invalidate-memory
2245 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2246 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2247 gdb-read-memory-handler
2248 gdb-read-memory-custom)
2249
2250 (defun gdb-read-memory-custom ()
2251 (save-excursion
2252 (goto-char (point-min))
2253 (if (looking-at "0x[[:xdigit:]]+")
2254 (setq gdb-memory-address (match-string 0)))))
2255
2256 (defvar gdb-memory-mode-map
2257 (let ((map (make-sparse-keymap)))
2258 (suppress-keymap map)
2259 (define-key map "q" 'kill-this-buffer)
2260 map))
2261
2262 (defun gdb-memory-set-address (event)
2263 "Set the start memory address."
2264 (interactive "e")
2265 (save-selected-window
2266 (select-window (posn-window (event-start event)))
2267 (let ((arg (read-from-minibuffer "Memory address: ")))
2268 (setq gdb-memory-address arg))
2269 (gdb-invalidate-memory)))
2270
2271 (defun gdb-memory-set-repeat-count (event)
2272 "Set the number of data items in memory window."
2273 (interactive "e")
2274 (save-selected-window
2275 (select-window (posn-window (event-start event)))
2276 (let* ((arg (read-from-minibuffer "Repeat count: "))
2277 (count (string-to-number arg)))
2278 (if (<= count 0)
2279 (error "Positive numbers only")
2280 (customize-set-variable 'gdb-memory-repeat-count count)
2281 (gdb-invalidate-memory)))))
2282
2283 (defun gdb-memory-format-binary ()
2284 "Set the display format to binary."
2285 (interactive)
2286 (customize-set-variable 'gdb-memory-format "t")
2287 (gdb-invalidate-memory))
2288
2289 (defun gdb-memory-format-octal ()
2290 "Set the display format to octal."
2291 (interactive)
2292 (customize-set-variable 'gdb-memory-format "o")
2293 (gdb-invalidate-memory))
2294
2295 (defun gdb-memory-format-unsigned ()
2296 "Set the display format to unsigned decimal."
2297 (interactive)
2298 (customize-set-variable 'gdb-memory-format "u")
2299 (gdb-invalidate-memory))
2300
2301 (defun gdb-memory-format-signed ()
2302 "Set the display format to decimal."
2303 (interactive)
2304 (customize-set-variable 'gdb-memory-format "d")
2305 (gdb-invalidate-memory))
2306
2307 (defun gdb-memory-format-hexadecimal ()
2308 "Set the display format to hexadecimal."
2309 (interactive)
2310 (customize-set-variable 'gdb-memory-format "x")
2311 (gdb-invalidate-memory))
2312
2313 (defvar gdb-memory-format-map
2314 (let ((map (make-sparse-keymap)))
2315 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2316 map)
2317 "Keymap to select format in the header line.")
2318
2319 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2320 "Menu of display formats in the header line.")
2321
2322 (define-key gdb-memory-format-menu [binary]
2323 '(menu-item "Binary" gdb-memory-format-binary
2324 :button (:radio . (equal gdb-memory-format "t"))))
2325 (define-key gdb-memory-format-menu [octal]
2326 '(menu-item "Octal" gdb-memory-format-octal
2327 :button (:radio . (equal gdb-memory-format "o"))))
2328 (define-key gdb-memory-format-menu [unsigned]
2329 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2330 :button (:radio . (equal gdb-memory-format "u"))))
2331 (define-key gdb-memory-format-menu [signed]
2332 '(menu-item "Signed Decimal" gdb-memory-format-signed
2333 :button (:radio . (equal gdb-memory-format "d"))))
2334 (define-key gdb-memory-format-menu [hexadecimal]
2335 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2336 :button (:radio . (equal gdb-memory-format "x"))))
2337
2338 (defun gdb-memory-format-menu (event)
2339 (interactive "@e")
2340 (x-popup-menu event gdb-memory-format-menu))
2341
2342 (defun gdb-memory-format-menu-1 (event)
2343 (interactive "e")
2344 (save-selected-window
2345 (select-window (posn-window (event-start event)))
2346 (let* ((selection (gdb-memory-format-menu event))
2347 (binding (and selection (lookup-key gdb-memory-format-menu
2348 (vector (car selection))))))
2349 (if binding (call-interactively binding)))))
2350
2351 (defun gdb-memory-unit-giant ()
2352 "Set the unit size to giant words (eight bytes)."
2353 (interactive)
2354 (customize-set-variable 'gdb-memory-unit "g")
2355 (gdb-invalidate-memory))
2356
2357 (defun gdb-memory-unit-word ()
2358 "Set the unit size to words (four bytes)."
2359 (interactive)
2360 (customize-set-variable 'gdb-memory-unit "w")
2361 (gdb-invalidate-memory))
2362
2363 (defun gdb-memory-unit-halfword ()
2364 "Set the unit size to halfwords (two bytes)."
2365 (interactive)
2366 (customize-set-variable 'gdb-memory-unit "h")
2367 (gdb-invalidate-memory))
2368
2369 (defun gdb-memory-unit-byte ()
2370 "Set the unit size to bytes."
2371 (interactive)
2372 (customize-set-variable 'gdb-memory-unit "b")
2373 (gdb-invalidate-memory))
2374
2375 (defvar gdb-memory-unit-map
2376 (let ((map (make-sparse-keymap)))
2377 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2378 map)
2379 "Keymap to select units in the header line.")
2380
2381 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2382 "Menu of units in the header line.")
2383
2384 (define-key gdb-memory-unit-menu [giantwords]
2385 '(menu-item "Giant words" gdb-memory-unit-giant
2386 :button (:radio . (equal gdb-memory-unit "g"))))
2387 (define-key gdb-memory-unit-menu [words]
2388 '(menu-item "Words" gdb-memory-unit-word
2389 :button (:radio . (equal gdb-memory-unit "w"))))
2390 (define-key gdb-memory-unit-menu [halfwords]
2391 '(menu-item "Halfwords" gdb-memory-unit-halfword
2392 :button (:radio . (equal gdb-memory-unit "h"))))
2393 (define-key gdb-memory-unit-menu [bytes]
2394 '(menu-item "Bytes" gdb-memory-unit-byte
2395 :button (:radio . (equal gdb-memory-unit "b"))))
2396
2397 (defun gdb-memory-unit-menu (event)
2398 (interactive "@e")
2399 (x-popup-menu event gdb-memory-unit-menu))
2400
2401 (defun gdb-memory-unit-menu-1 (event)
2402 (interactive "e")
2403 (save-selected-window
2404 (select-window (posn-window (event-start event)))
2405 (let* ((selection (gdb-memory-unit-menu event))
2406 (binding (and selection (lookup-key gdb-memory-unit-menu
2407 (vector (car selection))))))
2408 (if binding (call-interactively binding)))))
2409
2410 ;;from make-mode-line-mouse-map
2411 (defun gdb-make-header-line-mouse-map (mouse function) "\
2412 Return a keymap with single entry for mouse key MOUSE on the header line.
2413 MOUSE is defined to run function FUNCTION with no args in the buffer
2414 corresponding to the mode line clicked."
2415 (let ((map (make-sparse-keymap)))
2416 (define-key map (vector 'header-line mouse) function)
2417 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2418 map))
2419
2420 (defvar gdb-memory-font-lock-keywords
2421 '(;; <__function.name+n>
2422 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2423 )
2424 "Font lock keywords used in `gdb-memory-mode'.")
2425
2426 (defun gdb-memory-mode ()
2427 "Major mode for examining memory.
2428
2429 \\{gdb-memory-mode-map}"
2430 (kill-all-local-variables)
2431 (setq major-mode 'gdb-memory-mode)
2432 (setq mode-name "Memory")
2433 (setq buffer-read-only t)
2434 (use-local-map gdb-memory-mode-map)
2435 (setq header-line-format
2436 '(:eval
2437 (concat
2438 "Read address["
2439 (propertize
2440 "-"
2441 'face font-lock-warning-face
2442 'help-echo "mouse-1: Decrement address"
2443 'mouse-face 'mode-line-highlight
2444 'local-map
2445 (gdb-make-header-line-mouse-map
2446 'mouse-1
2447 #'(lambda () (interactive)
2448 (let ((gdb-memory-address
2449 ;; Let GDB do the arithmetic.
2450 (concat
2451 gdb-memory-address " - "
2452 (number-to-string
2453 (* gdb-memory-repeat-count
2454 (cond ((string= gdb-memory-unit "b") 1)
2455 ((string= gdb-memory-unit "h") 2)
2456 ((string= gdb-memory-unit "w") 4)
2457 ((string= gdb-memory-unit "g") 8)))))))
2458 (gdb-invalidate-memory)))))
2459 "|"
2460 (propertize "+"
2461 'face font-lock-warning-face
2462 'help-echo "mouse-1: Increment address"
2463 'mouse-face 'mode-line-highlight
2464 'local-map (gdb-make-header-line-mouse-map
2465 'mouse-1
2466 #'(lambda () (interactive)
2467 (let ((gdb-memory-address nil))
2468 (gdb-invalidate-memory)))))
2469 "]: "
2470 (propertize gdb-memory-address
2471 'face font-lock-warning-face
2472 'help-echo "mouse-1: Set memory address"
2473 'mouse-face 'mode-line-highlight
2474 'local-map (gdb-make-header-line-mouse-map
2475 'mouse-1
2476 #'gdb-memory-set-address))
2477 " Repeat Count: "
2478 (propertize (number-to-string gdb-memory-repeat-count)
2479 'face font-lock-warning-face
2480 'help-echo "mouse-1: Set repeat count"
2481 'mouse-face 'mode-line-highlight
2482 'local-map (gdb-make-header-line-mouse-map
2483 'mouse-1
2484 #'gdb-memory-set-repeat-count))
2485 " Display Format: "
2486 (propertize gdb-memory-format
2487 'face font-lock-warning-face
2488 'help-echo "mouse-3: Select display format"
2489 'mouse-face 'mode-line-highlight
2490 'local-map gdb-memory-format-map)
2491 " Unit Size: "
2492 (propertize gdb-memory-unit
2493 'face font-lock-warning-face
2494 'help-echo "mouse-3: Select unit size"
2495 'mouse-face 'mode-line-highlight
2496 'local-map gdb-memory-unit-map))))
2497 (set (make-local-variable 'font-lock-defaults)
2498 '(gdb-memory-font-lock-keywords))
2499 (run-mode-hooks 'gdb-memory-mode-hook)
2500 'gdb-invalidate-memory)
2501
2502 (defun gdb-memory-buffer-name ()
2503 (with-current-buffer gud-comint-buffer
2504 (concat "*memory of " (gdb-get-target-string) "*")))
2505
2506 (defun gdb-display-memory-buffer ()
2507 "Display memory contents."
2508 (interactive)
2509 (gdb-display-buffer
2510 (gdb-get-buffer-create 'gdb-memory-buffer)))
2511
2512 (defun gdb-frame-memory-buffer ()
2513 "Display memory contents in a new frame."
2514 (interactive)
2515 (let ((special-display-regexps (append special-display-regexps '(".*")))
2516 (special-display-frame-alist gdb-frame-parameters))
2517 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2518 \f
2519
2520 ;; Locals buffer.
2521 ;;
2522 (gdb-set-buffer-rules 'gdb-locals-buffer
2523 'gdb-locals-buffer-name
2524 'gdb-locals-mode)
2525
2526 (def-gdb-auto-update-trigger gdb-invalidate-locals
2527 (gdb-get-buffer 'gdb-locals-buffer)
2528 "server info locals\n"
2529 gdb-info-locals-handler)
2530
2531 (defvar gdb-locals-watch-map
2532 (let ((map (make-sparse-keymap)))
2533 (define-key map "\r" '(lambda () (interactive)
2534 (beginning-of-line)
2535 (gud-watch)))
2536 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2537 (mouse-set-point event)
2538 (beginning-of-line)
2539 (gud-watch)))
2540 map)
2541 "Keymap to create watch expression of a complex data type local variable.")
2542
2543 (defconst gdb-struct-string
2544 (concat (propertize "[struct/union]"
2545 'mouse-face 'highlight
2546 'help-echo "mouse-2: create watch expression"
2547 'local-map gdb-locals-watch-map) "\n"))
2548
2549 (defconst gdb-array-string
2550 (concat " " (propertize "[array]"
2551 'mouse-face 'highlight
2552 'help-echo "mouse-2: create watch expression"
2553 'local-map gdb-locals-watch-map) "\n"))
2554
2555 ;; Abbreviate for arrays and structures.
2556 ;; These can be expanded using gud-display.
2557 (defun gdb-info-locals-handler ()
2558 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2559 gdb-pending-triggers))
2560 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2561 (with-current-buffer buf
2562 (goto-char (point-min))
2563 (while (re-search-forward "^[ }].*\n" nil t)
2564 (replace-match "" nil nil))
2565 (goto-char (point-min))
2566 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2567 (replace-match gdb-struct-string nil nil))
2568 (goto-char (point-min))
2569 (while (re-search-forward "\\s-*{.*\n" nil t)
2570 (replace-match gdb-array-string nil nil))))
2571 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2572 (and buf
2573 (with-current-buffer buf
2574 (let* ((window (get-buffer-window buf 0))
2575 (start (window-start window))
2576 (p (window-point window))
2577 (buffer-read-only nil))
2578 (erase-buffer)
2579 (insert-buffer-substring (gdb-get-buffer-create
2580 'gdb-partial-output-buffer))
2581 (set-window-start window start)
2582 (set-window-point window p))
2583 )))
2584 (run-hooks 'gdb-info-locals-hook))
2585
2586 (defvar gdb-locals-mode-map
2587 (let ((map (make-sparse-keymap)))
2588 (suppress-keymap map)
2589 (define-key map "q" 'kill-this-buffer)
2590 map))
2591
2592 (defun gdb-locals-mode ()
2593 "Major mode for gdb locals.
2594
2595 \\{gdb-locals-mode-map}"
2596 (kill-all-local-variables)
2597 (setq major-mode 'gdb-locals-mode)
2598 (setq mode-name (concat "Locals:" gdb-selected-frame))
2599 (setq buffer-read-only t)
2600 (use-local-map gdb-locals-mode-map)
2601 (set (make-local-variable 'font-lock-defaults)
2602 '(gdb-locals-font-lock-keywords))
2603 (run-mode-hooks 'gdb-locals-mode-hook)
2604 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2605 (string-equal gdb-version "pre-6.4"))
2606 'gdb-invalidate-locals
2607 'gdb-invalidate-locals-1))
2608
2609 (defun gdb-locals-buffer-name ()
2610 (with-current-buffer gud-comint-buffer
2611 (concat "*locals of " (gdb-get-target-string) "*")))
2612
2613 (defun gdb-display-locals-buffer ()
2614 "Display local variables of current stack and their values."
2615 (interactive)
2616 (gdb-display-buffer
2617 (gdb-get-buffer-create 'gdb-locals-buffer)))
2618
2619 (defun gdb-frame-locals-buffer ()
2620 "Display local variables of current stack and their values in a new frame."
2621 (interactive)
2622 (let ((special-display-regexps (append special-display-regexps '(".*")))
2623 (special-display-frame-alist gdb-frame-parameters))
2624 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2625 \f
2626
2627 ;;;; Window management
2628 (defun gdb-display-buffer (buf &optional size)
2629 (let ((answer (get-buffer-window buf 0))
2630 (must-split nil))
2631 (if answer
2632 (display-buffer buf nil 0) ;Raise the frame if necessary.
2633 ;; The buffer is not yet displayed.
2634 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2635 (let ((window (get-lru-window)))
2636 (if (and window
2637 (not (eq window (get-buffer-window gud-comint-buffer))))
2638 (progn
2639 (set-window-buffer window buf)
2640 (setq answer window))
2641 (setq must-split t)))
2642 (if must-split
2643 (let* ((largest (get-largest-window))
2644 (cur-size (window-height largest))
2645 (new-size (and size (< size cur-size) (- cur-size size))))
2646 (setq answer (split-window largest new-size))
2647 (set-window-buffer answer buf)
2648 (set-window-dedicated-p answer t)))
2649 answer)))
2650
2651 \f
2652 ;;; Shared keymap initialization:
2653
2654 (let ((menu (make-sparse-keymap "GDB-Windows")))
2655 (define-key gud-menu-map [displays]
2656 `(menu-item "GDB-Windows" ,menu
2657 :visible (memq gud-minor-mode '(gdbmi gdba))))
2658 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2659 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2660 (define-key menu [inferior]
2661 '(menu-item "Inferior IO" gdb-display-separate-io-buffer
2662 :enable gdb-use-separate-io-buffer))
2663 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2664 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2665 (define-key menu [disassembly]
2666 '("Disassembly" . gdb-display-assembler-buffer))
2667 (define-key menu [breakpoints]
2668 '("Breakpoints" . gdb-display-breakpoints-buffer))
2669 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2670 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2671
2672 (let ((menu (make-sparse-keymap "GDB-Frames")))
2673 (define-key gud-menu-map [frames]
2674 `(menu-item "GDB-Frames" ,menu
2675 :visible (memq gud-minor-mode '(gdbmi gdba))))
2676 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2677 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2678 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2679 (define-key menu [inferior]
2680 '(menu-item "Inferior IO" gdb-frame-separate-io-buffer
2681 :enable gdb-use-separate-io-buffer))
2682 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2683 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2684 (define-key menu [breakpoints]
2685 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2686 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2687 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2688
2689 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2690 (define-key gud-menu-map [ui]
2691 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2692 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2693 (define-key menu [gdb-use-separate-io]
2694 '(menu-item "Separate inferior IO" gdb-use-separate-io-buffer
2695 :visible (eq gud-minor-mode 'gdba)
2696 :help "Toggle separate IO for inferior."
2697 :button (:toggle . gdb-use-separate-io-buffer)))
2698 (define-key menu [gdb-many-windows]
2699 '(menu-item "Display Other Windows" gdb-many-windows
2700 :help "Toggle display of locals, stack and breakpoint information"
2701 :button (:toggle . gdb-many-windows)))
2702 (define-key menu [gdb-restore-windows]
2703 '(menu-item "Restore Window Layout" gdb-restore-windows
2704 :help "Restore standard layout for debug session.")))
2705
2706 (defun gdb-frame-gdb-buffer ()
2707 "Display GUD buffer in a new frame."
2708 (interactive)
2709 (let ((special-display-regexps (append special-display-regexps '(".*")))
2710 (special-display-frame-alist gdb-frame-parameters)
2711 (same-window-regexps nil))
2712 (display-buffer gud-comint-buffer)))
2713
2714 (defun gdb-display-gdb-buffer ()
2715 "Display GUD buffer."
2716 (interactive)
2717 (let ((same-window-regexps nil))
2718 (pop-to-buffer gud-comint-buffer)))
2719
2720 (defun gdb-set-window-buffer (name)
2721 (set-window-buffer (selected-window) (get-buffer name))
2722 (set-window-dedicated-p (selected-window) t))
2723
2724 (defun gdb-setup-windows ()
2725 "Layout the window pattern for `gdb-many-windows'."
2726 (gdb-display-locals-buffer)
2727 (gdb-display-stack-buffer)
2728 (delete-other-windows)
2729 (gdb-display-breakpoints-buffer)
2730 (delete-other-windows)
2731 ; Don't dedicate.
2732 (pop-to-buffer gud-comint-buffer)
2733 (split-window nil ( / ( * (window-height) 3) 4))
2734 (split-window nil ( / (window-height) 3))
2735 (split-window-horizontally)
2736 (other-window 1)
2737 (gdb-set-window-buffer (gdb-locals-buffer-name))
2738 (other-window 1)
2739 (switch-to-buffer
2740 (if gud-last-last-frame
2741 (gud-find-file (car gud-last-last-frame))
2742 (gud-find-file gdb-main-file)))
2743 (when gdb-use-separate-io-buffer
2744 (split-window-horizontally)
2745 (other-window 1)
2746 (gdb-set-window-buffer
2747 (gdb-get-buffer-create 'gdb-inferior-io)))
2748 (other-window 1)
2749 (gdb-set-window-buffer (gdb-stack-buffer-name))
2750 (split-window-horizontally)
2751 (other-window 1)
2752 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2753 (other-window 1))
2754
2755 (defun gdb-restore-windows ()
2756 "Restore the basic arrangement of windows used by gdba.
2757 This arrangement depends on the value of `gdb-many-windows'."
2758 (interactive)
2759 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2760 (delete-other-windows)
2761 (if gdb-many-windows
2762 (gdb-setup-windows)
2763 (when (or gud-last-last-frame gdb-show-main)
2764 (split-window)
2765 (other-window 1)
2766 (switch-to-buffer
2767 (if gud-last-last-frame
2768 (gud-find-file (car gud-last-last-frame))
2769 (gud-find-file gdb-main-file)))
2770 (other-window 1))))
2771
2772 (defun gdb-reset ()
2773 "Exit a debugging session cleanly.
2774 Kills the gdb buffers, and resets variables and the source buffers."
2775 (dolist (buffer (buffer-list))
2776 (unless (eq buffer gud-comint-buffer)
2777 (with-current-buffer buffer
2778 (if (memq gud-minor-mode '(gdbmi gdba))
2779 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2780 (kill-buffer nil)
2781 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2782 (setq gud-minor-mode nil)
2783 (kill-local-variable 'tool-bar-map)
2784 (kill-local-variable 'gdb-define-alist))))))
2785 (when (markerp gdb-overlay-arrow-position)
2786 (move-marker gdb-overlay-arrow-position nil)
2787 (setq gdb-overlay-arrow-position nil))
2788 (setq overlay-arrow-variable-list
2789 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2790 (if (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
2791 (speedbar-refresh))
2792 (setq gud-running nil)
2793 (setq gdb-active-process nil)
2794 (setq gdb-var-list nil)
2795 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2796
2797 (defun gdb-source-info ()
2798 "Find the source file where the program starts and displays it with related
2799 buffers."
2800 (goto-char (point-min))
2801 (if (and (search-forward "Located in " nil t)
2802 (looking-at "\\S-+"))
2803 (setq gdb-main-file (match-string 0)))
2804 (goto-char (point-min))
2805 (if (search-forward "Includes preprocessor macro info." nil t)
2806 (setq gdb-macro-info t))
2807 (if gdb-many-windows
2808 (gdb-setup-windows)
2809 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2810 (if gdb-show-main
2811 (let ((pop-up-windows t))
2812 (display-buffer (gud-find-file gdb-main-file))))))
2813
2814 (defun gdb-get-location (bptno line flag)
2815 "Find the directory containing the relevant source file.
2816 Put in buffer and place breakpoint icon."
2817 (goto-char (point-min))
2818 (catch 'file-not-found
2819 (if (search-forward "Located in " nil t)
2820 (when (looking-at "\\S-+")
2821 (delete (cons bptno "File not found") gdb-location-alist)
2822 (push (cons bptno (match-string 0)) gdb-location-alist))
2823 (gdb-resync)
2824 (unless (assoc bptno gdb-location-alist)
2825 (push (cons bptno "File not found") gdb-location-alist)
2826 (message-box "Cannot find source file for breakpoint location.\n\
2827 Add directory to search path for source files using the GDB command, dir."))
2828 (throw 'file-not-found nil))
2829 (with-current-buffer
2830 (find-file-noselect (match-string 0))
2831 (save-current-buffer
2832 (set (make-local-variable 'gud-minor-mode) 'gdba)
2833 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2834 ;; only want one breakpoint icon at each location
2835 (save-excursion
2836 (goto-line (string-to-number line))
2837 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2838
2839 (add-hook 'find-file-hook 'gdb-find-file-hook)
2840
2841 (defun gdb-find-file-hook ()
2842 "Set up buffer for debugging if file is part of the source code
2843 of the current session."
2844 (if (and (buffer-name gud-comint-buffer)
2845 ;; in case gud or gdb-ui is just loaded
2846 gud-comint-buffer
2847 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2848 '(gdba gdbmi)))
2849 (if (member buffer-file-name gdb-source-file-list)
2850 (with-current-buffer (find-buffer-visiting buffer-file-name)
2851 (set (make-local-variable 'gud-minor-mode)
2852 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2853 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2854
2855 ;;from put-image
2856 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2857 "Put string PUTSTRING in front of POS in the current buffer.
2858 PUTSTRING is displayed by putting an overlay into the current buffer with a
2859 `before-string' string that has a `display' property whose value is
2860 PUTSTRING."
2861 (let ((string (make-string 1 ?x))
2862 (buffer (current-buffer)))
2863 (setq putstring (copy-sequence putstring))
2864 (let ((overlay (make-overlay pos pos buffer))
2865 (prop (or dprop
2866 (list (list 'margin 'left-margin) putstring))))
2867 (put-text-property 0 1 'display prop string)
2868 (if sprops
2869 (add-text-properties 0 1 sprops string))
2870 (overlay-put overlay 'put-break t)
2871 (overlay-put overlay 'before-string string))))
2872
2873 ;;from remove-images
2874 (defun gdb-remove-strings (start end &optional buffer)
2875 "Remove strings between START and END in BUFFER.
2876 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2877 BUFFER nil or omitted means use the current buffer."
2878 (unless buffer
2879 (setq buffer (current-buffer)))
2880 (dolist (overlay (overlays-in start end))
2881 (when (overlay-get overlay 'put-break)
2882 (delete-overlay overlay))))
2883
2884 (defun gdb-put-breakpoint-icon (enabled bptno)
2885 (let ((start (- (line-beginning-position) 1))
2886 (end (+ (line-end-position) 1))
2887 (putstring (if enabled "B" "b"))
2888 (source-window (get-buffer-window (current-buffer) 0)))
2889 (add-text-properties
2890 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2891 putstring)
2892 (if enabled
2893 (add-text-properties
2894 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2895 (add-text-properties
2896 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2897 (gdb-remove-breakpoint-icons start end)
2898 (if (display-images-p)
2899 (if (>= (or left-fringe-width
2900 (if source-window (car (window-fringes source-window)))
2901 gdb-buffer-fringe-width) 8)
2902 (gdb-put-string
2903 nil (1+ start)
2904 `(left-fringe breakpoint
2905 ,(if enabled
2906 'breakpoint-enabled
2907 'breakpoint-disabled))
2908 'gdb-bptno bptno
2909 'gdb-enabled enabled)
2910 (when (< left-margin-width 2)
2911 (save-current-buffer
2912 (setq left-margin-width 2)
2913 (if source-window
2914 (set-window-margins
2915 source-window
2916 left-margin-width right-margin-width))))
2917 (put-image
2918 (if enabled
2919 (or breakpoint-enabled-icon
2920 (setq breakpoint-enabled-icon
2921 (find-image `((:type xpm :data
2922 ,breakpoint-xpm-data
2923 :ascent 100 :pointer hand)
2924 (:type pbm :data
2925 ,breakpoint-enabled-pbm-data
2926 :ascent 100 :pointer hand)))))
2927 (or breakpoint-disabled-icon
2928 (setq breakpoint-disabled-icon
2929 (find-image `((:type xpm :data
2930 ,breakpoint-xpm-data
2931 :conversion disabled
2932 :ascent 100 :pointer hand)
2933 (:type pbm :data
2934 ,breakpoint-disabled-pbm-data
2935 :ascent 100 :pointer hand))))))
2936 (+ start 1)
2937 putstring
2938 'left-margin))
2939 (when (< left-margin-width 2)
2940 (save-current-buffer
2941 (setq left-margin-width 2)
2942 (let ((window (get-buffer-window (current-buffer) 0)))
2943 (if window
2944 (set-window-margins
2945 window left-margin-width right-margin-width)))))
2946 (gdb-put-string
2947 (propertize putstring
2948 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2949 (1+ start)))))
2950
2951 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2952 (gdb-remove-strings start end)
2953 (if (display-images-p)
2954 (remove-images start end))
2955 (when remove-margin
2956 (setq left-margin-width 0)
2957 (let ((window (get-buffer-window (current-buffer) 0)))
2958 (if window
2959 (set-window-margins
2960 window left-margin-width right-margin-width)))))
2961
2962 \f
2963 ;;
2964 ;; Assembler buffer.
2965 ;;
2966 (gdb-set-buffer-rules 'gdb-assembler-buffer
2967 'gdb-assembler-buffer-name
2968 'gdb-assembler-mode)
2969
2970 (def-gdb-auto-update-handler gdb-assembler-handler
2971 gdb-invalidate-assembler
2972 gdb-assembler-buffer
2973 gdb-assembler-custom)
2974
2975 (defun gdb-assembler-custom ()
2976 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2977 (pos 1) (address) (flag) (bptno))
2978 (with-current-buffer buffer
2979 (save-excursion
2980 (if (not (equal gdb-frame-address "main"))
2981 (progn
2982 (goto-char (point-min))
2983 (if (and gdb-frame-address
2984 (search-forward gdb-frame-address nil t))
2985 (progn
2986 (setq pos (point))
2987 (beginning-of-line)
2988 (or gdb-overlay-arrow-position
2989 (setq gdb-overlay-arrow-position (make-marker)))
2990 (set-marker gdb-overlay-arrow-position
2991 (point) (current-buffer))))))
2992 ;; remove all breakpoint-icons in assembler buffer before updating.
2993 (gdb-remove-breakpoint-icons (point-min) (point-max))))
2994 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2995 (goto-char (point-min))
2996 (while (< (point) (- (point-max) 1))
2997 (forward-line 1)
2998 (if (looking-at "[^\t].*?breakpoint")
2999 (progn
3000 (looking-at
3001 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3002 (setq bptno (match-string 1))
3003 (setq flag (char-after (match-beginning 2)))
3004 (setq address (match-string 3))
3005 (with-current-buffer buffer
3006 (save-excursion
3007 (goto-char (point-min))
3008 (if (search-forward address nil t)
3009 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3010 (if (not (equal gdb-frame-address "main"))
3011 (with-current-buffer buffer
3012 (set-window-point (get-buffer-window buffer 0) pos)))))
3013
3014 (defvar gdb-assembler-mode-map
3015 (let ((map (make-sparse-keymap)))
3016 (suppress-keymap map)
3017 (define-key map "q" 'kill-this-buffer)
3018 map))
3019
3020 (defvar gdb-assembler-font-lock-keywords
3021 '(;; <__function.name+n>
3022 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3023 (1 font-lock-function-name-face))
3024 ;; 0xNNNNNNNN <__function.name+n>: opcode
3025 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3026 (4 font-lock-keyword-face))
3027 ;; %register(at least i386)
3028 ("%\\sw+" . font-lock-variable-name-face)
3029 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3030 (1 font-lock-comment-face)
3031 (2 font-lock-function-name-face))
3032 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3033 "Font lock keywords used in `gdb-assembler-mode'.")
3034
3035 (defun gdb-assembler-mode ()
3036 "Major mode for viewing code assembler.
3037
3038 \\{gdb-assembler-mode-map}"
3039 (kill-all-local-variables)
3040 (setq major-mode 'gdb-assembler-mode)
3041 (setq mode-name (concat "Machine:" gdb-selected-frame))
3042 (setq gdb-overlay-arrow-position nil)
3043 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3044 (setq fringes-outside-margins t)
3045 (setq buffer-read-only t)
3046 (use-local-map gdb-assembler-mode-map)
3047 (gdb-invalidate-assembler)
3048 (set (make-local-variable 'font-lock-defaults)
3049 '(gdb-assembler-font-lock-keywords))
3050 (run-mode-hooks 'gdb-assembler-mode-hook)
3051 'gdb-invalidate-assembler)
3052
3053 (defun gdb-assembler-buffer-name ()
3054 (with-current-buffer gud-comint-buffer
3055 (concat "*disassembly of " (gdb-get-target-string) "*")))
3056
3057 (defun gdb-display-assembler-buffer ()
3058 "Display disassembly view."
3059 (interactive)
3060 (setq gdb-previous-frame nil)
3061 (gdb-display-buffer
3062 (gdb-get-buffer-create 'gdb-assembler-buffer)))
3063
3064 (defun gdb-frame-assembler-buffer ()
3065 "Display disassembly view in a new frame."
3066 (interactive)
3067 (setq gdb-previous-frame nil)
3068 (let ((special-display-regexps (append special-display-regexps '(".*")))
3069 (special-display-frame-alist gdb-frame-parameters))
3070 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3071
3072 ;; modified because if gdb-frame-address has changed value a new command
3073 ;; must be enqueued to update the buffer with the new output
3074 (defun gdb-invalidate-assembler (&optional ignored)
3075 (if (gdb-get-buffer 'gdb-assembler-buffer)
3076 (progn
3077 (unless (and gdb-selected-frame
3078 (string-equal gdb-selected-frame gdb-previous-frame))
3079 (if (or (not (member 'gdb-invalidate-assembler
3080 gdb-pending-triggers))
3081 (not (string-equal gdb-frame-address
3082 gdb-previous-frame-address)))
3083 (progn
3084 ;; take previous disassemble command, if any, off the queue
3085 (with-current-buffer gud-comint-buffer
3086 (let ((queue gdb-input-queue))
3087 (dolist (item queue)
3088 (if (equal (cdr item) '(gdb-assembler-handler))
3089 (setq gdb-input-queue
3090 (delete item gdb-input-queue))))))
3091 (gdb-enqueue-input
3092 (list
3093 (concat gdb-server-prefix "disassemble "
3094 (if (member gdb-frame-address '(nil "main")) nil "0x")
3095 gdb-frame-address "\n")
3096 'gdb-assembler-handler))
3097 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3098 (setq gdb-previous-frame-address gdb-frame-address)
3099 (setq gdb-previous-frame gdb-selected-frame)))))))
3100
3101 (defun gdb-get-selected-frame ()
3102 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3103 (progn
3104 (gdb-enqueue-input
3105 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3106 (push 'gdb-get-selected-frame
3107 gdb-pending-triggers))))
3108
3109 (defun gdb-frame-handler ()
3110 (setq gdb-pending-triggers
3111 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3112 (goto-char (point-min))
3113 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
3114 (setq gdb-frame-number (match-string 1)))
3115 (goto-char (point-min))
3116 (if (re-search-forward
3117 ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
3118 (progn
3119 (setq gdb-selected-frame (match-string 2))
3120 (if (gdb-get-buffer 'gdb-locals-buffer)
3121 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3122 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3123 (if (gdb-get-buffer 'gdb-assembler-buffer)
3124 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3125 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3126 (setq gdb-frame-address (match-string 1))))
3127 (goto-char (point-min))
3128 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
3129 (setq gdb-current-language (match-string 1)))
3130 (gdb-invalidate-assembler))
3131
3132 \f
3133 ;; Code specific to GDB 6.4
3134 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3135
3136 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3137 "Create list of source files for current GDB session.
3138 If buffers already exist for any of these files, gud-minor-mode
3139 is set in them."
3140 (goto-char (point-min))
3141 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3142 (push (match-string 1) gdb-source-file-list))
3143 (dolist (buffer (buffer-list))
3144 (with-current-buffer buffer
3145 (when (member buffer-file-name gdb-source-file-list)
3146 (set (make-local-variable 'gud-minor-mode)
3147 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3148 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3149 (when gud-tooltip-mode
3150 (make-local-variable 'gdb-define-alist)
3151 (gdb-create-define-alist)
3152 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
3153
3154 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3155 (defun gdb-var-list-children-1 (varnum)
3156 (gdb-enqueue-input
3157 (list
3158 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3159 (concat "server interpreter mi \"-var-list-children --all-values "
3160 varnum "\"\n")
3161 (concat "-var-list-children --all-values " varnum "\n"))
3162 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3163
3164 (defconst gdb-var-list-children-regexp-1
3165 "name=\"\\(.+?\\)\",exp=\"\\(.+?\\)\",numchild=\"\\(.+?\\)\",\
3166 value=\\(\".*?\"\\),type=\"\\(.+?\\)\"}")
3167
3168 (defun gdb-var-list-children-handler-1 (varnum)
3169 (goto-char (point-min))
3170 (let ((var-list nil))
3171 (catch 'child-already-watched
3172 (dolist (var gdb-var-list)
3173 (if (string-equal varnum (car var))
3174 (progn
3175 (push var var-list)
3176 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3177 (let ((varchild (list (match-string 1)
3178 (match-string 2)
3179 (match-string 3)
3180 (match-string 5)
3181 (read (match-string 4))
3182 nil)))
3183 (if (assoc (car varchild) gdb-var-list)
3184 (throw 'child-already-watched nil))
3185 (push varchild var-list))))
3186 (push var var-list)))
3187 (setq gdb-var-list (nreverse var-list)))))
3188
3189 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3190 (defun gdb-var-update-1 ()
3191 (if (not (member 'gdb-var-update gdb-pending-triggers))
3192 (progn
3193 (gdb-enqueue-input
3194 (list
3195 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3196 "server interpreter mi \"-var-update --all-values *\"\n"
3197 "-var-update --all-values *\n")
3198 'gdb-var-update-handler-1))
3199 (push 'gdb-var-update gdb-pending-triggers))))
3200
3201 (defconst gdb-var-update-regexp-1
3202 "name=\"\\(.*?\\)\",\\(?:value=\\(\".*?\"\\),\\)?in_scope=\"\\(.*?\\)\"")
3203
3204 (defun gdb-var-update-handler-1 ()
3205 (dolist (var gdb-var-list)
3206 (setcar (nthcdr 5 var) nil))
3207 (goto-char (point-min))
3208 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3209 (let* ((varnum (match-string 1))
3210 (var (assoc varnum gdb-var-list)))
3211 (when var
3212 (if (string-equal (match-string 3) "false")
3213 (setcar (nthcdr 5 var) 'out-of-scope)
3214 (setcar (nthcdr 5 var) 'changed)
3215 (setcar (nthcdr 4 var)
3216 (read (match-string 2)))))))
3217 (setq gdb-pending-triggers
3218 (delq 'gdb-var-update gdb-pending-triggers))
3219 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
3220 ;; dummy command to update speedbar at right time
3221 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
3222 ;; keep gdb-pending-triggers non-nil till end
3223 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
3224
3225 ;; Registers buffer.
3226 ;;
3227 (gdb-set-buffer-rules 'gdb-registers-buffer
3228 'gdb-registers-buffer-name
3229 'gdb-registers-mode)
3230
3231 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3232 (gdb-get-buffer 'gdb-registers-buffer)
3233 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3234 "server interpreter mi \"-data-list-register-values x\"\n"
3235 "-data-list-register-values x\n")
3236 gdb-data-list-register-values-handler)
3237
3238 (defconst gdb-data-list-register-values-regexp
3239 "number=\"\\(.*?\\)\",value=\"\\(.*?\\)\"")
3240
3241 (defun gdb-data-list-register-values-handler ()
3242 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3243 gdb-pending-triggers))
3244 (goto-char (point-min))
3245 (if (re-search-forward gdb-error-regexp nil t)
3246 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3247 (let ((buffer-read-only nil))
3248 (erase-buffer)
3249 (insert (match-string 1))
3250 (goto-char (point-min))))
3251 (let ((register-list (reverse gdb-register-names))
3252 (register nil) (register-string nil) (register-values nil))
3253 (goto-char (point-min))
3254 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3255 (setq register (pop register-list))
3256 (setq register-string (concat register "\t" (match-string 2) "\n"))
3257 (if (member (match-string 1) gdb-changed-registers)
3258 (put-text-property 0 (length register-string)
3259 'face 'font-lock-warning-face
3260 register-string))
3261 (setq register-values
3262 (concat register-values register-string)))
3263 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3264 (with-current-buffer buf
3265 (let* ((window (get-buffer-window buf 0))
3266 (start (window-start window))
3267 (p (window-point window))
3268 (buffer-read-only nil))
3269 (erase-buffer)
3270 (insert register-values)
3271 (set-window-start window start)
3272 (set-window-point window p))))))
3273 (gdb-data-list-register-values-custom))
3274
3275 (defun gdb-data-list-register-values-custom ()
3276 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3277 (save-excursion
3278 (let ((buffer-read-only nil)
3279 start end)
3280 (goto-char (point-min))
3281 (while (< (point) (point-max))
3282 (setq start (line-beginning-position))
3283 (setq end (line-end-position))
3284 (when (looking-at "^[^\t]+")
3285 (unless (string-equal (match-string 0) "No registers.")
3286 (put-text-property start (match-end 0)
3287 'face font-lock-variable-name-face)
3288 (add-text-properties start end
3289 '(help-echo "mouse-2: edit value"
3290 mouse-face highlight))))
3291 (forward-line 1))))))
3292
3293 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3294 (defun gdb-get-changed-registers ()
3295 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3296 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3297 (progn
3298 (gdb-enqueue-input
3299 (list
3300 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3301 "server interpreter mi -data-list-changed-registers\n"
3302 "-data-list-changed-registers\n")
3303 'gdb-get-changed-registers-handler))
3304 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3305
3306 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3307
3308 (defun gdb-get-changed-registers-handler ()
3309 (setq gdb-pending-triggers
3310 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3311 (setq gdb-changed-registers nil)
3312 (goto-char (point-min))
3313 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3314 (push (match-string 1) gdb-changed-registers)))
3315 \f
3316
3317 ;; Locals buffer.
3318 ;;
3319 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3320 (gdb-set-buffer-rules 'gdb-locals-buffer
3321 'gdb-locals-buffer-name
3322 'gdb-locals-mode)
3323
3324 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3325 (gdb-get-buffer 'gdb-locals-buffer)
3326 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3327 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3328 "-stack-list-locals --simple-values\n")
3329 gdb-stack-list-locals-handler)
3330
3331 (defconst gdb-stack-list-locals-regexp
3332 "name=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
3333
3334 (defvar gdb-locals-watch-map-1
3335 (let ((map (make-sparse-keymap)))
3336 (define-key map [mouse-2] 'gud-watch)
3337 map)
3338 "Keymap to create watch expression of a complex data type local variable.")
3339
3340 ;; Dont display values of arrays or structures.
3341 ;; These can be expanded using gud-watch.
3342 (defun gdb-stack-list-locals-handler ()
3343 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3344 gdb-pending-triggers))
3345 (let (local locals-list)
3346 (goto-char (point-min))
3347 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3348 (let ((local (list (match-string 1)
3349 (match-string 2)
3350 nil)))
3351 (if (looking-at ",value=\\(\".*\"\\)}")
3352 (setcar (nthcdr 2 local) (read (match-string 1))))
3353 (push local locals-list)))
3354 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3355 (and buf (with-current-buffer buf
3356 (let* ((window (get-buffer-window buf 0))
3357 (start (window-start window))
3358 (p (window-point window))
3359 (buffer-read-only nil))
3360 (erase-buffer)
3361 (dolist (local locals-list)
3362 (setq name (car local))
3363 (if (or (not (nth 2 local))
3364 (string-match "\\0x" (nth 2 local)))
3365 (add-text-properties 0 (length name)
3366 `(mouse-face highlight
3367 help-echo "mouse-2: create watch expression"
3368 local-map ,gdb-locals-watch-map-1)
3369 name))
3370 (insert
3371 (concat name "\t" (nth 1 local)
3372 "\t" (nth 2 local) "\n")))
3373 (set-window-start window start)
3374 (set-window-point window p)))))))
3375
3376 (defun gdb-get-register-names ()
3377 "Create a list of register names."
3378 (goto-char (point-min))
3379 (setq gdb-register-names nil)
3380 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3381 (push (match-string 1) gdb-register-names)))
3382
3383 (provide 'gdb-ui)
3384
3385 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3386 ;;; gdb-ui.el ends here