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