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