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