]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-ui.el
(gud-watch): Let user select an expression.
[gnu-emacs] / lisp / progmodes / gdb-ui.el
1 ;;; gdb-ui.el --- User Interface for running GDB
2
3 ;; Author: Nick Roberts <nickrob@gnu.org>
4 ;; Maintainer: FSF
5 ;; Keywords: unix, tools
6
7 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006
8 ;; Free Software Foundation, Inc.
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This mode acts as a graphical user interface to GDB. You can interact with
30 ;; GDB through the GUD buffer in the usual way, but there are also further
31 ;; buffers which control the execution and describe the state of your program.
32 ;; It separates the input/output of your program from that of GDB, if
33 ;; required, and watches expressions in the speedbar. It also uses features of
34 ;; Emacs 21 such as the fringe/display margin for breakpoints, and the toolbar
35 ;; (see the GDB Graphical Interface section in the Emacs info manual).
36
37 ;; By default, M-x gdb will start the debugger. However, if you have customised
38 ;; gud-gdb-command-name, then start it with M-x gdba.
39
40 ;; This file has evolved from gdba.el that was included with GDB 5.0 and
41 ;; written by Tom Lord and Jim Kingdon. It uses GDB's annotation interface.
42 ;; You don't need to know about annotations to use this mode as a debugger,
43 ;; but if you are interested developing the mode itself, then see the
44 ;; Annotations section in the GDB info manual.
45
46 ;; GDB developers plan to make the annotation interface obsolete. A new
47 ;; interface called GDB/MI (machine interface) has been designed to replace
48 ;; it. Some GDB/MI commands are used in this file through the CLI command
49 ;; 'interpreter mi <mi-command>'. A file called gdb-mi.el is included with
50 ;; GDB (6.2 onwards) that uses GDB/MI as the primary interface to GDB. It is
51 ;; still under development and is part of a process to migrate Emacs from
52 ;; annotations to GDB/MI.
53
54 ;; This mode SHOULD WORK WITH GDB 5.0 onwards but you will NEED GDB 6.0
55 ;; onwards to use watch expressions. It works best with GDB 6.4 where
56 ;; watch expressions will update more quickly.
57
58 ;;; Windows Platforms:
59
60 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
61 ;; explicitly in your program if you want timely display of I/O in Emacs.
62 ;; Alternatively you can make the output stream unbuffered, for example, by
63 ;; using a macro:
64
65 ;; #ifdef UNBUFFERED
66 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
67 ;; #endif
68
69 ;; and compiling with -DUNBUFFERED while debugging.
70
71 ;;; Known Bugs:
72
73 ;; 1) Strings that are watched don't update in the speedbar when their
74 ;; contents change unless the first character changes.
75 ;; 2) Cannot handle multiple debug sessions.
76 ;; 3) Initially, the assembler buffer does not display the cursor at the
77 ;; current line if the line is not visible in the window (but when testing
78 ;; gdb-assembler-custom with a lisp debugger it does!).
79
80 ;;; Problems with watch expressions, GDB/MI:
81 ;; 1) They go out of scope when the inferior is re-run.
82 ;; 2) -stack-list-locals has a type field but also prints type in values field.
83 ;; 3) VARNUM increments even when variable object is not created
84 ;; (maybe trivial).
85
86 ;; Known Bugs:
87 ;; 1) M-x gdb doesn't work with "run" command in .gdbinit, use M-x gdba instead.
88
89 ;;; TODO:
90 ;; 1) Use MI command -data-read-memory for memory window.
91 ;; 2) Use tree-widget.el instead of the speedbar for watch-expressions?
92 ;; 3) Mark breakpoint locations on scroll-bar of source buffer?
93 ;; 4) With gud-print and gud-pstar, print the variable name in the GUD
94 ;; buffer instead of the value's history number.
95
96 ;;; Code:
97
98 (require 'gud)
99
100 (defvar tool-bar-map)
101 (defvar speedbar-initial-expansion-list-name)
102
103 (defvar gdb-pc-address nil "Initialization for Assembler buffer.
104 Set to \"main\" at start if gdb-show-main is t.")
105 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
106 (defvar gdb-previous-frame-address nil)
107 (defvar gdb-memory-address "main")
108 (defvar gdb-previous-frame nil)
109 (defvar gdb-selected-frame nil)
110 (defvar gdb-frame-number nil)
111 (defvar gdb-current-language nil)
112 (defvar gdb-var-list nil
113 "List of variables in watch window.
114 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS FP)
115 where STATUS is nil (unchanged), `changed' or `out-of-scope', FP the frame
116 address for root variables.")
117 (defvar gdb-force-update t
118 "Non-nil means that view of watch expressions will be updated in the speedbar.")
119 (defvar gdb-main-file nil "Source file from which program execution begins.")
120 (defvar gdb-overlay-arrow-position nil)
121 (defvar gdb-server-prefix nil)
122 (defvar gdb-flush-pending-output nil)
123 (defvar gdb-location-alist nil
124 "Alist of breakpoint numbers and full filenames.")
125 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
126 and #define directives otherwise.")
127 (defvar gdb-error "Non-nil when GDB is reporting an error.")
128 (defvar gdb-macro-info nil
129 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
130 (defvar gdb-buffer-fringe-width nil)
131 (defvar gdb-signalled nil)
132 (defvar gdb-source-window nil)
133 (defvar gdb-inferior-status nil)
134 (defvar gdb-continuation nil)
135
136 (defvar gdb-buffer-type nil
137 "One of the symbols bound in `gdb-buffer-rules'.")
138 (make-variable-buffer-local 'gdb-buffer-type)
139
140 (defvar gdb-input-queue ()
141 "A list of gdb command objects.")
142
143 (defvar gdb-prompting nil
144 "True when gdb is idle with no pending input.")
145
146 (defvar gdb-output-sink 'user
147 "The disposition of the output of the current gdb command.
148 Possible values are these symbols:
149
150 `user' -- gdb output should be copied to the GUD buffer
151 for the user to see.
152
153 `inferior' -- gdb output should be copied to the inferior-io buffer.
154
155 `pre-emacs' -- output should be ignored util the post-prompt
156 annotation is received. Then the output-sink
157 becomes:...
158 `emacs' -- output should be collected in the partial-output-buffer
159 for subsequent processing by a command. This is the
160 disposition of output generated by commands that
161 gdb mode sends to gdb on its own behalf.
162 `post-emacs' -- ignore output until the prompt annotation is
163 received, then go to USER disposition.
164
165 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
166 \(`user' and `emacs').")
167
168 (defvar gdb-current-item nil
169 "The most recent command item sent to gdb.")
170
171 (defvar gdb-pending-triggers '()
172 "A list of trigger functions that have run later than their output
173 handlers.")
174
175 (defvar gdb-first-post-prompt nil)
176 (defvar gdb-version nil)
177 (defvar gdb-locals-font-lock-keywords nil)
178 (defvar gdb-source-file-list nil
179 "List of source files for the current executable")
180 (defconst gdb-error-regexp "\\^error,msg=\"\\(.+\\)\"")
181
182 (defvar gdb-locals-font-lock-keywords-1
183 '(
184 ;; var = (struct struct_tag) value
185 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
186 (1 font-lock-variable-name-face)
187 (3 font-lock-keyword-face)
188 (4 font-lock-type-face))
189 ;; var = (type) value
190 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
191 (1 font-lock-variable-name-face)
192 (3 font-lock-type-face))
193 ;; var = val
194 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
195 (1 font-lock-variable-name-face))
196 )
197 "Font lock keywords used in `gdb-local-mode'.")
198
199 (defvar gdb-locals-font-lock-keywords-2
200 '(
201 ;; var = type value
202 ( "\\(^\\(\\sw\\|[_.]\\)+\\)\t+\\(\\(\\sw\\|[_.]\\)+\\)"
203 (1 font-lock-variable-name-face)
204 (3 font-lock-type-face))
205 )
206 "Font lock keywords used in `gdb-local-mode'.")
207
208 ;; Variables for GDB 6.4+
209 (defvar gdb-register-names nil "List of register names.")
210 (defvar gdb-changed-registers nil
211 "List of changed register numbers (strings).")
212
213 ;;;###autoload
214 (defun gdba (command-line)
215 "Run gdb on program FILE in buffer *gud-FILE*.
216 The directory containing FILE becomes the initial working directory
217 and source-file directory for your debugger.
218
219 If `gdb-many-windows' is nil (the default value) then gdb just
220 pops up the GUD buffer unless `gdb-show-main' is t. In this case
221 it starts with two windows: one displaying the GUD buffer and the
222 other with the source file with the main routine of the inferior.
223
224 If `gdb-many-windows' is t, regardless of the value of
225 `gdb-show-main', the layout below will appear unless
226 `gdb-use-separate-io-buffer' is nil when the source buffer
227 occupies the full width of the frame. Keybindings are shown in
228 some of the buffers.
229
230 Watch expressions appear in the speedbar/slowbar.
231
232 The following commands help control operation :
233
234 `gdb-many-windows' - Toggle the number of windows gdb uses.
235 `gdb-restore-windows' - To restore the window layout.
236
237 See Info node `(emacs)GDB Graphical Interface' for a more
238 detailed description of this mode.
239
240
241 +----------------------------------------------------------------------+
242 | GDB Toolbar |
243 +-----------------------------------+----------------------------------+
244 | GUD buffer (I/O of GDB) | Locals buffer |
245 | | |
246 | | |
247 | | |
248 +-----------------------------------+----------------------------------+
249 | Source buffer | I/O buffer (of debugged program) |
250 | | (comint-mode) |
251 | | |
252 | | |
253 | | |
254 | | |
255 | | |
256 | | |
257 +-----------------------------------+----------------------------------+
258 | Stack buffer | Breakpoints buffer |
259 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
260 | | RET gdb-goto-breakpoint |
261 | | D gdb-delete-breakpoint |
262 +-----------------------------------+----------------------------------+"
263 ;;
264 (interactive (list (gud-query-cmdline 'gdba)))
265 ;;
266 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
267 (gdb command-line)
268 (gdb-init-1))
269
270 (defcustom gdb-debug-ring-max 128
271 "Maximum size of `gdb-debug-ring'."
272 :group 'gud
273 :type 'integer
274 :version "22.1")
275
276 (defvar gdb-debug-ring nil
277 "List of commands, most recent first, sent to and replies received from GDB.
278 This variable is used to debug GDB-UI.")
279
280 ;;;###autoload
281 (defcustom gdb-enable-debug nil
282 "Non-nil means record the process input and output in `gdb-debug-ring'."
283 :type 'boolean
284 :group 'gud
285 :version "22.1")
286
287 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
288 "Shell command for generating a list of defined macros in a source file.
289 This list is used to display the #define directive associated
290 with an identifier as a tooltip. It works in a debug session with
291 GDB, when gud-tooltip-mode is t.
292
293 Set `gdb-cpp-define-alist-flags' for any include paths or
294 predefined macros."
295 :type 'string
296 :group 'gud
297 :version "22.1")
298
299 (defcustom gdb-cpp-define-alist-flags ""
300 "Preprocessor flags for `gdb-cpp-define-alist-program'."
301 :type 'string
302 :group 'gud
303 :version "22.1")
304
305 (defcustom gdb-show-main nil
306 "Non-nil means display source file containing the main routine at startup.
307 Also display the main routine in the disassembly buffer if present."
308 :type 'boolean
309 :group 'gud
310 :version "22.1")
311
312 (defcustom gdb-many-windows nil
313 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
314 In this case it starts with two windows: one displaying the GUD
315 buffer and the other with the source file with the main routine
316 of the inferior. Non-nil means display the layout shown for
317 `gdba'."
318 :type 'boolean
319 :group 'gud
320 :version "22.1")
321
322 (defcustom gdb-use-separate-io-buffer nil
323 "Non-nil means display output from the inferior in a separate buffer."
324 :type 'boolean
325 :group 'gud
326 :version "22.1")
327
328 (defun gdb-force-mode-line-update (status)
329 (let ((buffer gud-comint-buffer))
330 (if (and buffer (buffer-name buffer))
331 (with-current-buffer buffer
332 (setq mode-line-process
333 (format ":%s [%s]"
334 (process-status (get-buffer-process buffer)) status))
335 ;; Force mode line redisplay soon.
336 (force-mode-line-update)))))
337
338 (defun gdb-many-windows (arg)
339 "Toggle the number of windows in the basic arrangement.
340 With arg, display additional buffers iff arg is positive."
341 (interactive "P")
342 (setq gdb-many-windows
343 (if (null arg)
344 (not gdb-many-windows)
345 (> (prefix-numeric-value arg) 0)))
346 (message (format "Display of other windows %sabled"
347 (if gdb-many-windows "en" "dis")))
348 (if (and gud-comint-buffer
349 (buffer-name gud-comint-buffer))
350 (condition-case nil
351 (gdb-restore-windows)
352 (error nil))))
353
354 (defun gdb-use-separate-io-buffer (arg)
355 "Toggle separate IO for inferior.
356 With arg, use separate IO iff arg is positive."
357 (interactive "P")
358 (setq gdb-use-separate-io-buffer
359 (if (null arg)
360 (not gdb-use-separate-io-buffer)
361 (> (prefix-numeric-value arg) 0)))
362 (message (format "Separate inferior IO %sabled"
363 (if gdb-use-separate-io-buffer "en" "dis")))
364 (if (and gud-comint-buffer
365 (buffer-name gud-comint-buffer))
366 (condition-case nil
367 (if gdb-use-separate-io-buffer
368 (if gdb-many-windows (gdb-restore-windows))
369 (kill-buffer (gdb-inferior-io-name)))
370 (error nil))))
371
372 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
373
374 (defun gdb-create-define-alist ()
375 "Create an alist of #define directives for GUD tooltips."
376 (let* ((file (buffer-file-name))
377 (output
378 (with-output-to-string
379 (with-current-buffer standard-output
380 (call-process shell-file-name
381 (if (file-exists-p file) file nil)
382 (list t nil) nil "-c"
383 (concat gdb-cpp-define-alist-program " "
384 gdb-cpp-define-alist-flags)))))
385 (define-list (split-string output "\n" t))
386 (name))
387 (setq gdb-define-alist nil)
388 (dolist (define define-list)
389 (setq name (nth 1 (split-string define "[( ]")))
390 (push (cons name define) gdb-define-alist))))
391
392 (defun gdb-tooltip-print (expr)
393 (tooltip-show
394 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
395 (goto-char (point-min))
396 (let ((string
397 (if (search-forward "=" nil t)
398 (concat expr (buffer-substring (- (point) 2) (point-max)))
399 (buffer-string))))
400 ;; remove newline for gud-tooltip-echo-area
401 (substring string 0 (- (length string) 1))))
402 (or gud-tooltip-echo-area tooltip-use-echo-area)))
403
404 ;; If expr is a macro for a function don't print because of possible dangerous
405 ;; side-effects. Also printing a function within a tooltip generates an
406 ;; unexpected starting annotation (phase error).
407 (defun gdb-tooltip-print-1 (expr)
408 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
409 (goto-char (point-min))
410 (if (search-forward "expands to: " nil t)
411 (unless (looking-at "\\S-+.*(.*).*")
412 (gdb-enqueue-input
413 (list (concat gdb-server-prefix "print " expr "\n")
414 `(lambda () (gdb-tooltip-print ,expr))))))))
415
416 (defconst gdb-source-file-regexp "\\(.+?\\), \\|\\([^, \n].*$\\)")
417
418 (defun gdb-set-gud-minor-mode-existing-buffers ()
419 "Create list of source files for current GDB session."
420 (goto-char (point-min))
421 (when (search-forward "read in on demand:" nil t)
422 (while (re-search-forward gdb-source-file-regexp nil t)
423 (push (file-name-nondirectory (or (match-string 1) (match-string 2)))
424 gdb-source-file-list))
425 (dolist (buffer (buffer-list))
426 (with-current-buffer buffer
427 (when (and buffer-file-name
428 (member (file-name-nondirectory buffer-file-name)
429 gdb-source-file-list))
430 (set (make-local-variable 'gud-minor-mode) 'gdba)
431 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
432 (when gud-tooltip-mode
433 (make-local-variable 'gdb-define-alist)
434 (gdb-create-define-alist)
435 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))))
436
437 (defun gdb-find-watch-expression ()
438 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
439 (varnum (car var)) expr array)
440 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
441 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
442 (component-list (split-string (match-string 2 varnum) "\\." t)))
443 (setq expr (nth 1 var1))
444 (setq varnumlet (car var1))
445 (dolist (component component-list)
446 (setq var2 (assoc varnumlet gdb-var-list))
447 (setq expr (concat expr
448 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
449 (concat "[" component "]")
450 (concat "." component))))
451 (setq varnumlet (concat varnumlet "." component)))
452 expr)))
453
454 (defun gdb-init-1 ()
455 (set (make-local-variable 'gud-minor-mode) 'gdba)
456 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
457 ;;
458 (gud-def gud-break (if (not (string-match "Machine" mode-name))
459 (gud-call "break %f:%l" arg)
460 (save-excursion
461 (beginning-of-line)
462 (forward-char 2)
463 (gud-call "break *%a" arg)))
464 "\C-b" "Set breakpoint at current line or address.")
465 ;;
466 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
467 (gud-call "clear %f:%l" arg)
468 (save-excursion
469 (beginning-of-line)
470 (forward-char 2)
471 (gud-call "clear *%a" arg)))
472 "\C-d" "Remove breakpoint at current line or address.")
473 ;;
474 (gud-def gud-until (if (not (string-match "Machine" mode-name))
475 (gud-call "until %f:%l" arg)
476 (save-excursion
477 (beginning-of-line)
478 (forward-char 2)
479 (gud-call "until *%a" arg)))
480 "\C-u" "Continue to current line or address.")
481 ;;
482 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
483 nil "Start or continue execution.")
484
485 ;; For debugging Emacs only.
486 (gud-def gud-pp
487 (gud-call
488 (concat
489 "pp1 " (if (eq (buffer-local-value
490 'major-mode (window-buffer)) 'speedbar-mode)
491 (gdb-find-watch-expression) "%e")) arg)
492 nil "Print the emacs s-expression.")
493
494 (define-key gud-minor-mode-map [left-margin mouse-1]
495 'gdb-mouse-set-clear-breakpoint)
496 (define-key gud-minor-mode-map [left-fringe mouse-1]
497 'gdb-mouse-set-clear-breakpoint)
498 (define-key gud-minor-mode-map [left-fringe mouse-2]
499 'gdb-mouse-until)
500 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
501 'gdb-mouse-until)
502 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
503 'gdb-mouse-until)
504 (define-key gud-minor-mode-map [left-margin mouse-2]
505 'gdb-mouse-until)
506 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
507 'gdb-mouse-jump)
508 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
509 'gdb-mouse-jump)
510 (define-key gud-minor-mode-map [left-fringe C-mouse-2]
511 'gdb-mouse-jump)
512 (define-key gud-minor-mode-map [left-margin C-mouse-2]
513 'gdb-mouse-jump)
514 (define-key gud-minor-mode-map [left-margin mouse-3]
515 'gdb-mouse-toggle-breakpoint-margin)
516 (define-key gud-minor-mode-map [left-fringe mouse-3]
517 'gdb-mouse-toggle-breakpoint-fringe)
518
519 (setq comint-input-sender 'gdb-send)
520
521 ;; (re-)initialize
522 (setq gdb-pc-address (if gdb-show-main "main" nil))
523 (setq gdb-previous-frame-address nil
524 gdb-memory-address "main"
525 gdb-previous-frame nil
526 gdb-selected-frame nil
527 gdb-current-language nil
528 gdb-frame-number nil
529 gdb-var-list nil
530 gdb-force-update t
531 gdb-main-file nil
532 gdb-first-post-prompt t
533 gdb-prompting nil
534 gdb-input-queue nil
535 gdb-current-item nil
536 gdb-pending-triggers nil
537 gdb-output-sink 'user
538 gdb-server-prefix "server "
539 gdb-flush-pending-output nil
540 gdb-location-alist nil
541 gdb-source-file-list nil
542 gdb-error nil
543 gdb-macro-info nil
544 gdb-buffer-fringe-width (car (window-fringes))
545 gdb-debug-ring nil
546 gdb-signalled nil
547 gdb-source-window nil
548 gdb-inferior-status nil
549 gdb-continuation nil)
550
551 (setq gdb-buffer-type 'gdba)
552
553 (if gdb-use-separate-io-buffer (gdb-clear-inferior-io))
554
555 ;; Hack to see test for GDB 6.4+ (-stack-info-frame was implemented in 6.4)
556 (gdb-enqueue-input (list "server interpreter mi -stack-info-frame\n"
557 'gdb-get-version)))
558
559 (defun gdb-init-2 ()
560 (if (eq window-system 'w32)
561 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
562 (gdb-enqueue-input (list "set height 0\n" 'ignore))
563 (gdb-enqueue-input (list "set width 0\n" 'ignore))
564
565 (if (string-equal gdb-version "pre-6.4")
566 (progn
567 (gdb-enqueue-input (list (concat gdb-server-prefix "info sources\n")
568 'gdb-set-gud-minor-mode-existing-buffers))
569 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-1))
570 (gdb-enqueue-input
571 (list "server interpreter mi -data-list-register-names\n"
572 'gdb-get-register-names))
573 ; Needs GDB 6.2 onwards.
574 (gdb-enqueue-input
575 (list "server interpreter mi \"-file-list-exec-source-files\"\n"
576 'gdb-set-gud-minor-mode-existing-buffers-1))
577 (setq gdb-locals-font-lock-keywords gdb-locals-font-lock-keywords-2))
578
579 ;; Find source file and compilation directory here.
580 ;; Works for C, C++, Fortran and Ada but not Java (GDB 6.4)
581 (gdb-enqueue-input (list "server list\n" 'ignore))
582 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
583
584 (run-hooks 'gdba-mode-hook))
585
586 (defun gdb-get-version ()
587 (goto-char (point-min))
588 (if (re-search-forward "Undefined\\( mi\\)* command:" nil t)
589 (setq gdb-version "pre-6.4")
590 (setq gdb-version "6.4+"))
591 (gdb-init-2))
592
593 (defun gdb-mouse-until (event)
594 "Continue running until a source line past the current line.
595 The destination source line can be selected either by clicking with mouse-2
596 on the fringe/margin or dragging the arrow with mouse-1 (default bindings)."
597 (interactive "e")
598 (if gud-overlay-arrow-position
599 (let ((start (event-start event))
600 (end (event-end event))
601 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
602 (if (not (string-match "Machine" mode-name))
603 (if (equal buffer (window-buffer (posn-window end)))
604 (with-current-buffer buffer
605 (when (or (equal start end)
606 (equal (posn-point start)
607 (marker-position
608 gud-overlay-arrow-position)))
609 (setq line (line-number-at-pos (posn-point end)))
610 (gud-call (concat "until " (number-to-string line))))))
611 (if (equal (marker-buffer gdb-overlay-arrow-position)
612 (window-buffer (posn-window end)))
613 (when (or (equal start end)
614 (equal (posn-point start)
615 (marker-position
616 gdb-overlay-arrow-position)))
617 (save-excursion
618 (goto-line (line-number-at-pos (posn-point end)))
619 (forward-char 2)
620 (gud-call (concat "until *%a")))))))))
621
622 (defun gdb-mouse-jump (event)
623 "Set execution address/line.
624 The destination source line can be selected either by clicking with mouse-2
625 on the fringe/margin or dragging the arrow with mouse-1 (default bindings).
626 Unlike gdb-mouse-until the destination address can be before the current
627 line, and no execution takes place."
628 (interactive "e")
629 (if gud-overlay-arrow-position
630 (let ((start (event-start event))
631 (end (event-end event))
632 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
633 (if (not (string-match "Machine" mode-name))
634 (if (equal buffer (window-buffer (posn-window end)))
635 (with-current-buffer buffer
636 (when (or (equal start end)
637 (equal (posn-point start)
638 (marker-position
639 gud-overlay-arrow-position)))
640 (setq line (line-number-at-pos (posn-point end)))
641 (progn (gud-call (concat "tbreak " (number-to-string line)))
642 (gud-call (concat "jump " (number-to-string line)))))))
643 (if (equal (marker-buffer gdb-overlay-arrow-position)
644 (window-buffer (posn-window end)))
645 (when (or (equal start end)
646 (equal (posn-point start)
647 (marker-position
648 gdb-overlay-arrow-position)))
649 (save-excursion
650 (goto-line (line-number-at-pos (posn-point end)))
651 (forward-char 2)
652 (progn
653 (gud-call (concat "tbreak *%a"))
654 (gud-call (concat "jump *%a"))))))))))
655
656 (defcustom gdb-speedbar-auto-raise nil
657 "If non-nil raise speedbar every time display of watch expressions is\
658 updated."
659 :type 'boolean
660 :group 'gud
661 :version "22.1")
662
663 (defun gdb-speedbar-auto-raise (arg)
664 "Toggle automatic raising of the speedbar for watch expressions.
665 With arg, automatically raise speedbar iff arg is positive."
666 (interactive "P")
667 (setq gdb-speedbar-auto-raise
668 (if (null arg)
669 (not gdb-speedbar-auto-raise)
670 (> (prefix-numeric-value arg) 0)))
671 (message (format "Auto raising %sabled"
672 (if gdb-speedbar-auto-raise "en" "dis"))))
673
674 (defcustom gdb-use-colon-colon-notation nil
675 "If non-nil use FUN::VAR format to display variables in the speedbar."
676 :type 'boolean
677 :group 'gud
678 :version "22.1")
679
680 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
681 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
682
683 (defun gud-watch (&optional arg event)
684 "Watch expression at point.
685 With arg, enter name of variable to be watched in the minibuffer."
686 (interactive (list current-prefix-arg last-input-event))
687 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
688 (if (memq minor-mode '(gdbmi gdba))
689 (progn
690 (if event (posn-set-point (event-end event)))
691 (require 'tooltip)
692 (save-selected-window
693 (let ((expr
694 (if arg
695 (completing-read "Name of variable: "
696 'gud-gdb-complete-command)
697 (if (and transient-mark-mode mark-active)
698 (buffer-substring (region-beginning) (region-end))
699 (tooltip-identifier-from-point (point))))))
700 (catch 'already-watched
701 (dolist (var gdb-var-list)
702 (unless (string-match "\\." (car var))
703 (if (string-equal expr (nth 1 var))
704 (throw 'already-watched nil))))
705 (set-text-properties 0 (length expr) nil expr)
706 (gdb-enqueue-input
707 (list
708 (if (eq minor-mode 'gdba)
709 (concat
710 "server interpreter mi \"-var-create - * " expr "\"\n")
711 (concat"-var-create - * " expr "\n"))
712 `(lambda () (gdb-var-create-handler ,expr))))))))
713 (message "gud-watch is a no-op in this mode."))))
714
715 (defconst gdb-var-create-regexp
716 "name=\"\\(.*?\\)\",.*numchild=\"\\(.*?\\)\",.*type=\"\\(.*?\\)\"")
717
718 (defun gdb-var-create-handler (expr)
719 (goto-char (point-min))
720 (if (re-search-forward gdb-var-create-regexp nil t)
721 (let ((var (list
722 (match-string 1)
723 (if (and (string-equal gdb-current-language "c")
724 gdb-use-colon-colon-notation gdb-selected-frame)
725 (setq expr (concat gdb-selected-frame "::" expr))
726 expr)
727 (match-string 2)
728 (match-string 3)
729 nil nil gdb-frame-address)))
730 (push var gdb-var-list)
731 (speedbar 1)
732 (unless (string-equal
733 speedbar-initial-expansion-list-name "GUD")
734 (speedbar-change-initial-expansion-list "GUD"))
735 (gdb-enqueue-input
736 (list
737 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
738 (concat "server interpreter mi \"-var-evaluate-expression "
739 (car var) "\"\n")
740 (concat "-var-evaluate-expression " (car var) "\n"))
741 `(lambda () (gdb-var-evaluate-expression-handler
742 ,(car var) nil)))))
743 (if (search-forward "Undefined command" nil t)
744 (message-box "Watching expressions requires gdb 6.0 onwards")
745 (message-box "No symbol \"%s\" in current context." expr))))
746
747 (defun gdb-var-evaluate-expression-handler (varnum changed)
748 (goto-char (point-min))
749 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
750 (let ((var (assoc varnum gdb-var-list)))
751 (when var
752 (if changed (setcar (nthcdr 5 var) 'changed))
753 (setcar (nthcdr 4 var) (read (match-string 1))))))
754
755 (defun gdb-var-list-children (varnum)
756 (gdb-enqueue-input
757 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
758 `(lambda () (gdb-var-list-children-handler ,varnum)))))
759
760 (defconst gdb-var-list-children-regexp
761 "child={.*?name=\"\\(.*?\\)\",.*?exp=\"\\(.*?\\)\",.*?\
762 numchild=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\".*?}")
763
764 (defun gdb-var-list-children-handler (varnum)
765 (goto-char (point-min))
766 (let ((var-list nil))
767 (catch 'child-already-watched
768 (dolist (var gdb-var-list)
769 (if (string-equal varnum (car var))
770 (progn
771 (push var var-list)
772 (while (re-search-forward gdb-var-list-children-regexp nil t)
773 (let ((varchild (list (match-string 1)
774 (match-string 2)
775 (match-string 3)
776 (match-string 4)
777 nil nil)))
778 (if (assoc (car varchild) gdb-var-list)
779 (throw 'child-already-watched nil))
780 (push varchild var-list)
781 (gdb-enqueue-input
782 (list
783 (concat
784 "server interpreter mi \"-var-evaluate-expression "
785 (car varchild) "\"\n")
786 `(lambda () (gdb-var-evaluate-expression-handler
787 ,(car varchild) nil)))))))
788 (push var var-list)))
789 (setq gdb-var-list (nreverse var-list)))))
790
791 (defun gdb-var-update ()
792 (when (not (member 'gdb-var-update gdb-pending-triggers))
793 (gdb-enqueue-input
794 (list "server interpreter mi \"-var-update *\"\n"
795 'gdb-var-update-handler))
796 (push 'gdb-var-update gdb-pending-triggers)))
797
798 (defconst gdb-var-update-regexp
799 "{.*?name=\"\\(.*?\\)\",.*?in_scope=\"\\(.*?\\)\",.*?\
800 type_changed=\".*?\".*?}")
801
802 (defun gdb-var-update-handler ()
803 (dolist (var gdb-var-list)
804 (setcar (nthcdr 5 var) nil))
805 (goto-char (point-min))
806 (while (re-search-forward gdb-var-update-regexp nil t)
807 (let ((varnum (match-string 1)))
808 (if (string-equal (match-string 2) "false")
809 (let ((var (assoc varnum gdb-var-list)))
810 (if var (setcar (nthcdr 5 var) 'out-of-scope)))
811 (gdb-enqueue-input
812 (list
813 (concat "server interpreter mi \"-var-evaluate-expression "
814 varnum "\"\n")
815 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t)))))))
816 (setq gdb-pending-triggers
817 (delq 'gdb-var-update gdb-pending-triggers))
818 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
819 ;; Dummy command to update speedbar at right time.
820 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
821 ;; Keep gdb-pending-triggers non-nil till end.
822 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
823
824 (defun gdb-speedbar-refresh ()
825 (setq gdb-pending-triggers
826 (delq 'gdb-speedbar-refresh gdb-pending-triggers))
827 (with-current-buffer gud-comint-buffer
828 (let ((speedbar-verbosity-level 0)
829 (speedbar-shown-directories nil))
830 (save-excursion
831 (speedbar-refresh)))))
832
833 (defun gdb-var-delete ()
834 "Delete watch expression at point from the speedbar."
835 (interactive)
836 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
837 '(gdbmi gdba))
838 (let ((text (speedbar-line-text)))
839 (string-match "\\(\\S-+\\)" 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 (string-match "\\\\$" string)
1107 (setq gdb-continuation (concat gdb-continuation string "\n"))
1108 (let ((item (concat gdb-continuation string "\n")))
1109 (gdb-enqueue-input item)
1110 (setq gdb-continuation nil)))))
1111
1112 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1113 ;; is a query, or other non-top-level prompt.
1114
1115 (defun gdb-enqueue-input (item)
1116 (if (not gud-running)
1117 (if gdb-prompting
1118 (progn
1119 (gdb-send-item item)
1120 (setq gdb-prompting nil))
1121 (push item gdb-input-queue))))
1122
1123 (defun gdb-dequeue-input ()
1124 (let ((queue gdb-input-queue))
1125 (and queue
1126 (let ((last (car (last queue))))
1127 (unless (nbutlast queue) (setq gdb-input-queue '()))
1128 last))))
1129
1130 (defun gdb-send-item (item)
1131 (setq gdb-flush-pending-output nil)
1132 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-ring))
1133 (setq gdb-current-item item)
1134 (let ((process (get-buffer-process gud-comint-buffer)))
1135 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1136 (if (stringp item)
1137 (progn
1138 (setq gdb-output-sink 'user)
1139 (process-send-string process item))
1140 (progn
1141 (gdb-clear-partial-output)
1142 (setq gdb-output-sink 'pre-emacs)
1143 (process-send-string process
1144 (car item))))
1145 ;; case: eq gud-minor-mode 'gdbmi
1146 (gdb-clear-partial-output)
1147 (setq gdb-output-sink 'emacs)
1148 (process-send-string process (car item)))))
1149 \f
1150 ;;
1151 ;; output -- things gdb prints to emacs
1152 ;;
1153 ;; GDB output is a stream interrupted by annotations.
1154 ;; Annotations can be recognized by their beginning
1155 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1156 ;;
1157 ;; The tag is a string obeying symbol syntax.
1158 ;;
1159 ;; The optional part `<opt>' can be either the empty string
1160 ;; or a space followed by more data relating to the annotation.
1161 ;; For example, the SOURCE annotation is followed by a filename,
1162 ;; line number and various useless goo. This data must not include
1163 ;; any newlines.
1164 ;;
1165
1166 (defcustom gud-gdba-command-name "gdb -annotate=3"
1167 "Default command to execute an executable under the GDB-UI debugger."
1168 :type 'string
1169 :group 'gud
1170 :version "22.1")
1171
1172 (defvar gdb-annotation-rules
1173 '(("pre-prompt" gdb-pre-prompt)
1174 ("prompt" gdb-prompt)
1175 ("commands" gdb-subprompt)
1176 ("overload-choice" gdb-subprompt)
1177 ("query" gdb-subprompt)
1178 ;; Need this prompt for GDB 6.1
1179 ("nquery" gdb-subprompt)
1180 ("prompt-for-continue" gdb-subprompt)
1181 ("post-prompt" gdb-post-prompt)
1182 ("source" gdb-source)
1183 ("starting" gdb-starting)
1184 ("exited" gdb-exited)
1185 ("signalled" gdb-signalled)
1186 ("signal" gdb-signal)
1187 ("breakpoint" gdb-stopping)
1188 ("watchpoint" gdb-stopping)
1189 ("frame-begin" gdb-frame-begin)
1190 ("stopped" gdb-stopped)
1191 ("error-begin" gdb-error)
1192 ("error" gdb-error)
1193 ) "An assoc mapping annotation tags to functions which process them.")
1194
1195 (defun gdb-resync()
1196 (setq gdb-flush-pending-output t)
1197 (setq gud-running nil)
1198 (gdb-force-mode-line-update "stopped")
1199 (setq gdb-output-sink 'user)
1200 (setq gdb-input-queue nil)
1201 (setq gdb-pending-triggers nil)
1202 (setq gdb-prompting t))
1203
1204 (defconst gdb-source-spec-regexp
1205 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1206
1207 ;; Do not use this except as an annotation handler.
1208 (defun gdb-source (args)
1209 (string-match gdb-source-spec-regexp args)
1210 ;; Extract the frame position from the marker.
1211 (setq gud-last-frame
1212 (cons
1213 (match-string 1 args)
1214 (string-to-number (match-string 2 args))))
1215 (setq gdb-pc-address (match-string 3 args))
1216 ;; cover for auto-display output which comes *before*
1217 ;; stopped annotation
1218 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1219
1220 (defun gdb-pre-prompt (ignored)
1221 "An annotation handler for `pre-prompt'.
1222 This terminates the collection of output from a previous command if that
1223 happens to be in effect."
1224 (setq gdb-error nil)
1225 (let ((sink gdb-output-sink))
1226 (cond
1227 ((eq sink 'user) t)
1228 ((eq sink 'emacs)
1229 (setq gdb-output-sink 'post-emacs))
1230 (t
1231 (gdb-resync)
1232 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1233
1234 (defun gdb-prompt (ignored)
1235 "An annotation handler for `prompt'.
1236 This sends the next command (if any) to gdb."
1237 (when gdb-first-prompt
1238 (gdb-init-1)
1239 (setq gdb-first-prompt nil))
1240 (let ((sink gdb-output-sink))
1241 (cond
1242 ((eq sink 'user) t)
1243 ((eq sink 'post-emacs)
1244 (setq gdb-output-sink 'user)
1245 (let ((handler
1246 (car (cdr gdb-current-item))))
1247 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1248 (funcall handler))))
1249 (t
1250 (gdb-resync)
1251 (error "Phase error in gdb-prompt (got %s)" sink))))
1252 (let ((input (gdb-dequeue-input)))
1253 (if input
1254 (gdb-send-item input)
1255 (progn
1256 (setq gdb-prompting t)
1257 (gud-display-frame)))))
1258
1259 (defun gdb-subprompt (ignored)
1260 "An annotation handler for non-top-level prompts."
1261 (setq gdb-prompting t))
1262
1263 (defun gdb-starting (ignored)
1264 "An annotation handler for `starting'.
1265 This says that I/O for the subprocess is now the program being debugged,
1266 not GDB."
1267 (setq gdb-active-process t)
1268 (let ((sink gdb-output-sink))
1269 (cond
1270 ((eq sink 'user)
1271 (progn
1272 (setq gud-running t)
1273 (setq gdb-inferior-status "running")
1274 (gdb-force-mode-line-update gdb-inferior-status)
1275 (gdb-remove-text-properties)
1276 (setq gud-overlay-arrow-position nil)
1277 (setq gdb-overlay-arrow-position nil)
1278 (if gdb-use-separate-io-buffer
1279 (setq gdb-output-sink 'inferior))))
1280 (t
1281 (gdb-resync)
1282 (error "Unexpected `starting' annotation")))))
1283
1284 (defun gdb-signal (ignored)
1285 (setq gdb-inferior-status "signal")
1286 (gdb-force-mode-line-update gdb-inferior-status)
1287 (gdb-stopping ignored))
1288
1289 (defun gdb-stopping (ignored)
1290 "An annotation handler for `breakpoint' and other annotations.
1291 They say that I/O for the subprocess is now GDB, not the program
1292 being debugged."
1293 (if gdb-use-separate-io-buffer
1294 (let ((sink gdb-output-sink))
1295 (cond
1296 ((eq sink 'inferior)
1297 (setq gdb-output-sink 'user))
1298 (t
1299 (gdb-resync)
1300 (error "Unexpected stopping annotation"))))))
1301
1302 (defun gdb-exited (ignored)
1303 "An annotation handler for `exited' and `signalled'.
1304 They say that I/O for the subprocess is now GDB, not the program
1305 being debugged and that the program is no longer running. This
1306 function is used to change the focus of GUD tooltips to #define
1307 directives."
1308 (setq gdb-active-process nil)
1309 (setq gud-overlay-arrow-position nil)
1310 (setq gdb-overlay-arrow-position nil)
1311 (setq gdb-inferior-status "exited")
1312 (gdb-force-mode-line-update gdb-inferior-status)
1313 (gdb-stopping ignored))
1314
1315 (defun gdb-signalled (ignored)
1316 (setq gdb-signalled t))
1317
1318 (defun gdb-frame-begin (ignored)
1319 (let ((sink gdb-output-sink))
1320 (cond
1321 ((eq sink 'inferior)
1322 (setq gdb-output-sink 'user))
1323 ((eq sink 'user) t)
1324 ((eq sink 'emacs) t)
1325 (t
1326 (gdb-resync)
1327 (error "Unexpected frame-begin annotation (%S)" sink)))))
1328
1329 (defun gdb-stopped (ignored)
1330 "An annotation handler for `stopped'.
1331 It is just like `gdb-stopping', except that if we already set the output
1332 sink to `user' in `gdb-stopping', that is fine."
1333 (setq gud-running nil)
1334 (unless (or gud-overlay-arrow-position gud-last-frame
1335 (not gud-last-last-frame))
1336 (gud-display-line (car gud-last-last-frame) (cdr gud-last-last-frame)))
1337 (unless (member gdb-inferior-status '("exited" "signal"))
1338 (setq gdb-inferior-status "stopped")
1339 (gdb-force-mode-line-update gdb-inferior-status))
1340 (let ((sink gdb-output-sink))
1341 (cond
1342 ((eq sink 'inferior)
1343 (setq gdb-output-sink 'user))
1344 ((eq sink 'user) t)
1345 (t
1346 (gdb-resync)
1347 (error "Unexpected stopped annotation"))))
1348 (if gdb-signalled (gdb-exited ignored)))
1349
1350 (defun gdb-error (ignored)
1351 (setq gdb-error (not gdb-error)))
1352
1353 (defun gdb-post-prompt (ignored)
1354 "An annotation handler for `post-prompt'.
1355 This begins the collection of output from the current command if that
1356 happens to be appropriate."
1357 ;; Don't add to queue if there outstanding items or gdb-version is not known
1358 ;; yet.
1359 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1360 (gdb-get-selected-frame)
1361 (gdb-invalidate-frames)
1362 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1363 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1364 (gdb-invalidate-breakpoints)
1365 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1366 ;; so gdb-pc-address is updated.
1367 ;; (gdb-invalidate-assembler)
1368
1369 (if (string-equal gdb-version "pre-6.4")
1370 (gdb-invalidate-registers)
1371 (gdb-get-changed-registers)
1372 (gdb-invalidate-registers-1))
1373
1374 (gdb-invalidate-memory)
1375 (if (string-equal gdb-version "pre-6.4")
1376 (gdb-invalidate-locals)
1377 (gdb-invalidate-locals-1))
1378
1379 (gdb-invalidate-threads)
1380 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1381 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1382 ;; Only needed/used with speedbar/watch expressions.
1383 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1384 (setq gdb-force-update t)
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 (unless (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 (when gdb-use-separate-io-buffer
2762 (split-window-horizontally)
2763 (other-window 1)
2764 (gdb-set-window-buffer
2765 (gdb-get-buffer-create 'gdb-inferior-io)))
2766 (other-window 1)
2767 (gdb-set-window-buffer (gdb-stack-buffer-name))
2768 (split-window-horizontally)
2769 (other-window 1)
2770 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2771 (other-window 1))
2772
2773 (defun gdb-restore-windows ()
2774 "Restore the basic arrangement of windows used by gdba.
2775 This arrangement depends on the value of `gdb-many-windows'."
2776 (interactive)
2777 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2778 (delete-other-windows)
2779 (if gdb-many-windows
2780 (gdb-setup-windows)
2781 (when (or gud-last-last-frame gdb-show-main)
2782 (split-window)
2783 (other-window 1)
2784 (switch-to-buffer
2785 (if gud-last-last-frame
2786 (gud-find-file (car gud-last-last-frame))
2787 (gud-find-file gdb-main-file)))
2788 (other-window 1))))
2789
2790 (defun gdb-reset ()
2791 "Exit a debugging session cleanly.
2792 Kills the gdb buffers, and resets variables and the source buffers."
2793 (dolist (buffer (buffer-list))
2794 (unless (eq buffer gud-comint-buffer)
2795 (with-current-buffer buffer
2796 (if (memq gud-minor-mode '(gdbmi gdba))
2797 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2798 (kill-buffer nil)
2799 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2800 (setq gud-minor-mode nil)
2801 (kill-local-variable 'tool-bar-map)
2802 (kill-local-variable 'gdb-define-alist))))))
2803 (when (markerp gdb-overlay-arrow-position)
2804 (move-marker gdb-overlay-arrow-position nil)
2805 (setq gdb-overlay-arrow-position nil))
2806 (setq overlay-arrow-variable-list
2807 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2808 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2809 (if (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
2810 (speedbar-refresh))
2811 (setq gud-running nil)
2812 (setq gdb-active-process nil)
2813 (setq gdb-var-list nil)
2814 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2815
2816 (defun gdb-source-info ()
2817 "Find the source file where the program starts and displays it with related
2818 buffers."
2819 (goto-char (point-min))
2820 (if (and (search-forward "Located in " nil t)
2821 (looking-at "\\S-+"))
2822 (setq gdb-main-file (match-string 0)))
2823 (goto-char (point-min))
2824 (if (search-forward "Includes preprocessor macro info." nil t)
2825 (setq gdb-macro-info t))
2826 (if gdb-many-windows
2827 (gdb-setup-windows)
2828 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2829 (if gdb-show-main
2830 (let ((pop-up-windows t))
2831 (display-buffer (gud-find-file gdb-main-file))))))
2832
2833 (defun gdb-get-location (bptno line flag)
2834 "Find the directory containing the relevant source file.
2835 Put in buffer and place breakpoint icon."
2836 (goto-char (point-min))
2837 (catch 'file-not-found
2838 (if (search-forward "Located in " nil t)
2839 (when (looking-at "\\S-+")
2840 (delete (cons bptno "File not found") gdb-location-alist)
2841 (push (cons bptno (match-string 0)) gdb-location-alist))
2842 (gdb-resync)
2843 (unless (assoc bptno gdb-location-alist)
2844 (push (cons bptno "File not found") gdb-location-alist)
2845 (message-box "Cannot find source file for breakpoint location.\n\
2846 Add directory to search path for source files using the GDB command, dir."))
2847 (throw 'file-not-found nil))
2848 (with-current-buffer
2849 (find-file-noselect (match-string 0))
2850 (save-current-buffer
2851 (set (make-local-variable 'gud-minor-mode) 'gdba)
2852 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2853 ;; only want one breakpoint icon at each location
2854 (save-excursion
2855 (goto-line (string-to-number line))
2856 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2857
2858 (add-hook 'find-file-hook 'gdb-find-file-hook)
2859
2860 (defun gdb-find-file-hook ()
2861 "Set up buffer for debugging if file is part of the source code
2862 of the current session."
2863 (if (and (buffer-name gud-comint-buffer)
2864 ;; in case gud or gdb-ui is just loaded
2865 gud-comint-buffer
2866 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2867 '(gdba gdbmi)))
2868 (if (member buffer-file-name gdb-source-file-list)
2869 (with-current-buffer (find-buffer-visiting buffer-file-name)
2870 (set (make-local-variable 'gud-minor-mode)
2871 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2872 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2873
2874 ;;from put-image
2875 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2876 "Put string PUTSTRING in front of POS in the current buffer.
2877 PUTSTRING is displayed by putting an overlay into the current buffer with a
2878 `before-string' string that has a `display' property whose value is
2879 PUTSTRING."
2880 (let ((string (make-string 1 ?x))
2881 (buffer (current-buffer)))
2882 (setq putstring (copy-sequence putstring))
2883 (let ((overlay (make-overlay pos pos buffer))
2884 (prop (or dprop
2885 (list (list 'margin 'left-margin) putstring))))
2886 (put-text-property 0 1 'display prop string)
2887 (if sprops
2888 (add-text-properties 0 1 sprops string))
2889 (overlay-put overlay 'put-break t)
2890 (overlay-put overlay 'before-string string))))
2891
2892 ;;from remove-images
2893 (defun gdb-remove-strings (start end &optional buffer)
2894 "Remove strings between START and END in BUFFER.
2895 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2896 BUFFER nil or omitted means use the current buffer."
2897 (unless buffer
2898 (setq buffer (current-buffer)))
2899 (dolist (overlay (overlays-in start end))
2900 (when (overlay-get overlay 'put-break)
2901 (delete-overlay overlay))))
2902
2903 (defun gdb-put-breakpoint-icon (enabled bptno)
2904 (let ((start (- (line-beginning-position) 1))
2905 (end (+ (line-end-position) 1))
2906 (putstring (if enabled "B" "b"))
2907 (source-window (get-buffer-window (current-buffer) 0)))
2908 (add-text-properties
2909 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2910 putstring)
2911 (if enabled
2912 (add-text-properties
2913 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2914 (add-text-properties
2915 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2916 (gdb-remove-breakpoint-icons start end)
2917 (if (display-images-p)
2918 (if (>= (or left-fringe-width
2919 (if source-window (car (window-fringes source-window)))
2920 gdb-buffer-fringe-width) 8)
2921 (gdb-put-string
2922 nil (1+ start)
2923 `(left-fringe breakpoint
2924 ,(if enabled
2925 'breakpoint-enabled
2926 'breakpoint-disabled))
2927 'gdb-bptno bptno
2928 'gdb-enabled enabled)
2929 (when (< left-margin-width 2)
2930 (save-current-buffer
2931 (setq left-margin-width 2)
2932 (if source-window
2933 (set-window-margins
2934 source-window
2935 left-margin-width right-margin-width))))
2936 (put-image
2937 (if enabled
2938 (or breakpoint-enabled-icon
2939 (setq breakpoint-enabled-icon
2940 (find-image `((:type xpm :data
2941 ,breakpoint-xpm-data
2942 :ascent 100 :pointer hand)
2943 (:type pbm :data
2944 ,breakpoint-enabled-pbm-data
2945 :ascent 100 :pointer hand)))))
2946 (or breakpoint-disabled-icon
2947 (setq breakpoint-disabled-icon
2948 (find-image `((:type xpm :data
2949 ,breakpoint-xpm-data
2950 :conversion disabled
2951 :ascent 100 :pointer hand)
2952 (:type pbm :data
2953 ,breakpoint-disabled-pbm-data
2954 :ascent 100 :pointer hand))))))
2955 (+ start 1)
2956 putstring
2957 'left-margin))
2958 (when (< left-margin-width 2)
2959 (save-current-buffer
2960 (setq left-margin-width 2)
2961 (let ((window (get-buffer-window (current-buffer) 0)))
2962 (if window
2963 (set-window-margins
2964 window left-margin-width right-margin-width)))))
2965 (gdb-put-string
2966 (propertize putstring
2967 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2968 (1+ start)))))
2969
2970 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2971 (gdb-remove-strings start end)
2972 (if (display-images-p)
2973 (remove-images start end))
2974 (when remove-margin
2975 (setq left-margin-width 0)
2976 (let ((window (get-buffer-window (current-buffer) 0)))
2977 (if window
2978 (set-window-margins
2979 window left-margin-width right-margin-width)))))
2980
2981 \f
2982 ;;
2983 ;; Assembler buffer.
2984 ;;
2985 (gdb-set-buffer-rules 'gdb-assembler-buffer
2986 'gdb-assembler-buffer-name
2987 'gdb-assembler-mode)
2988
2989 ;; We can't use def-gdb-auto-update-handler because we don't want to use
2990 ;; window-start but keep the overlay arrow/current line visible.
2991 (defun gdb-assembler-handler ()
2992 (setq gdb-pending-triggers
2993 (delq 'gdb-invalidate-assembler
2994 gdb-pending-triggers))
2995 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
2996 (and buf
2997 (with-current-buffer buf
2998 (let* ((window (get-buffer-window buf 0))
2999 (p (window-point window))
3000 (buffer-read-only nil))
3001 (erase-buffer)
3002 (insert-buffer-substring (gdb-get-buffer-create
3003 'gdb-partial-output-buffer))
3004 (set-window-point window p)))))
3005 ;; put customisation here
3006 (gdb-assembler-custom))
3007
3008 (defun gdb-assembler-custom ()
3009 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3010 (pos 1) (address) (flag) (bptno))
3011 (with-current-buffer buffer
3012 (save-excursion
3013 (if (not (equal gdb-pc-address "main"))
3014 (progn
3015 (goto-char (point-min))
3016 (if (and gdb-pc-address
3017 (search-forward gdb-pc-address nil t))
3018 (progn
3019 (setq pos (point))
3020 (beginning-of-line)
3021 (setq fringe-indicator-alist
3022 (if (string-equal gdb-frame-number "0")
3023 nil
3024 '((overlay-arrow . hollow-right-triangle))))
3025 (or gdb-overlay-arrow-position
3026 (setq gdb-overlay-arrow-position (make-marker)))
3027 (set-marker gdb-overlay-arrow-position
3028 (point) (current-buffer))))))
3029 ;; remove all breakpoint-icons in assembler buffer before updating.
3030 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3031 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3032 (goto-char (point-min))
3033 (while (< (point) (- (point-max) 1))
3034 (forward-line 1)
3035 (if (looking-at "[^\t].*?breakpoint")
3036 (progn
3037 (looking-at
3038 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3039 (setq bptno (match-string 1))
3040 (setq flag (char-after (match-beginning 2)))
3041 (setq address (match-string 3))
3042 (with-current-buffer buffer
3043 (save-excursion
3044 (goto-char (point-min))
3045 (if (search-forward address nil t)
3046 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3047 (if (not (equal gdb-pc-address "main"))
3048 (with-current-buffer buffer
3049 (set-window-point (get-buffer-window buffer 0) pos)))))
3050
3051 (defvar gdb-assembler-mode-map
3052 (let ((map (make-sparse-keymap)))
3053 (suppress-keymap map)
3054 (define-key map "q" 'kill-this-buffer)
3055 map))
3056
3057 (defvar gdb-assembler-font-lock-keywords
3058 '(;; <__function.name+n>
3059 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3060 (1 font-lock-function-name-face))
3061 ;; 0xNNNNNNNN <__function.name+n>: opcode
3062 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3063 (4 font-lock-keyword-face))
3064 ;; %register(at least i386)
3065 ("%\\sw+" . font-lock-variable-name-face)
3066 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3067 (1 font-lock-comment-face)
3068 (2 font-lock-function-name-face))
3069 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3070 "Font lock keywords used in `gdb-assembler-mode'.")
3071
3072 (defun gdb-assembler-mode ()
3073 "Major mode for viewing code assembler.
3074
3075 \\{gdb-assembler-mode-map}"
3076 (kill-all-local-variables)
3077 (setq major-mode 'gdb-assembler-mode)
3078 (setq mode-name (concat "Machine:" gdb-selected-frame))
3079 (setq gdb-overlay-arrow-position nil)
3080 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3081 (setq fringes-outside-margins t)
3082 (setq buffer-read-only t)
3083 (use-local-map gdb-assembler-mode-map)
3084 (gdb-invalidate-assembler)
3085 (set (make-local-variable 'font-lock-defaults)
3086 '(gdb-assembler-font-lock-keywords))
3087 (run-mode-hooks 'gdb-assembler-mode-hook)
3088 'gdb-invalidate-assembler)
3089
3090 (defun gdb-assembler-buffer-name ()
3091 (with-current-buffer gud-comint-buffer
3092 (concat "*disassembly of " (gdb-get-target-string) "*")))
3093
3094 (defun gdb-display-assembler-buffer ()
3095 "Display disassembly view."
3096 (interactive)
3097 (setq gdb-previous-frame nil)
3098 (gdb-display-buffer
3099 (gdb-get-buffer-create 'gdb-assembler-buffer)))
3100
3101 (defun gdb-frame-assembler-buffer ()
3102 "Display disassembly view in a new frame."
3103 (interactive)
3104 (setq gdb-previous-frame nil)
3105 (let ((special-display-regexps (append special-display-regexps '(".*")))
3106 (special-display-frame-alist gdb-frame-parameters))
3107 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3108
3109 ;; modified because if gdb-pc-address has changed value a new command
3110 ;; must be enqueued to update the buffer with the new output
3111 (defun gdb-invalidate-assembler (&optional ignored)
3112 (if (gdb-get-buffer 'gdb-assembler-buffer)
3113 (progn
3114 (unless (and gdb-selected-frame
3115 (string-equal gdb-selected-frame gdb-previous-frame))
3116 (if (or (not (member 'gdb-invalidate-assembler
3117 gdb-pending-triggers))
3118 (not (string-equal gdb-pc-address
3119 gdb-previous-frame-address)))
3120 (progn
3121 ;; take previous disassemble command, if any, off the queue
3122 (with-current-buffer gud-comint-buffer
3123 (let ((queue gdb-input-queue))
3124 (dolist (item queue)
3125 (if (equal (cdr item) '(gdb-assembler-handler))
3126 (setq gdb-input-queue
3127 (delete item gdb-input-queue))))))
3128 (gdb-enqueue-input
3129 (list
3130 (concat gdb-server-prefix "disassemble "
3131 (if (member gdb-pc-address '(nil "main")) nil "0x")
3132 gdb-pc-address "\n")
3133 'gdb-assembler-handler))
3134 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3135 (setq gdb-previous-frame-address gdb-pc-address)
3136 (setq gdb-previous-frame gdb-selected-frame)))))))
3137
3138 (defun gdb-get-selected-frame ()
3139 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3140 (progn
3141 (gdb-enqueue-input
3142 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3143 (push 'gdb-get-selected-frame
3144 gdb-pending-triggers))))
3145
3146 (defun gdb-frame-handler ()
3147 (setq gdb-pending-triggers
3148 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3149 (goto-char (point-min))
3150 (when (re-search-forward
3151 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3152 (setq gdb-frame-number (match-string 1))
3153 (setq gdb-frame-address (match-string 2)))
3154 (goto-char (point-min))
3155 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-+?\\)\
3156 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*;? "
3157 nil t)
3158 (setq gdb-selected-frame (match-string 2))
3159 (if (gdb-get-buffer 'gdb-locals-buffer)
3160 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3161 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3162 (if (gdb-get-buffer 'gdb-assembler-buffer)
3163 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3164 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3165 (setq gdb-pc-address (match-string 1))
3166 (if (and (match-string 3) gud-overlay-arrow-position)
3167 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3168 (position (marker-position gud-overlay-arrow-position)))
3169 (when (and buffer (string-equal (buffer-name buffer) (match-string 3)))
3170 (with-current-buffer buffer
3171 (setq fringe-indicator-alist
3172 (if (string-equal gdb-frame-number "0")
3173 nil
3174 '((overlay-arrow . hollow-right-triangle))))
3175 (set-marker gud-overlay-arrow-position position))))))
3176 (goto-char (point-min))
3177 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3178 (setq gdb-current-language (match-string 1)))
3179 (gdb-invalidate-assembler))
3180
3181 \f
3182 ;; Code specific to GDB 6.4
3183 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3184
3185 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3186 "Create list of source files for current GDB session.
3187 If buffers already exist for any of these files, gud-minor-mode
3188 is set in them."
3189 (goto-char (point-min))
3190 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3191 (push (match-string 1) gdb-source-file-list))
3192 (dolist (buffer (buffer-list))
3193 (with-current-buffer buffer
3194 (when (member buffer-file-name gdb-source-file-list)
3195 (set (make-local-variable 'gud-minor-mode)
3196 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3197 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3198 (when gud-tooltip-mode
3199 (make-local-variable 'gdb-define-alist)
3200 (gdb-create-define-alist)
3201 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))))))
3202
3203 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3204 (defun gdb-var-list-children-1 (varnum)
3205 (gdb-enqueue-input
3206 (list
3207 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3208 (concat "server interpreter mi \"-var-list-children --all-values "
3209 varnum "\"\n")
3210 (concat "-var-list-children --all-values " varnum "\n"))
3211 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3212
3213 (defconst gdb-var-list-children-regexp-1
3214 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3215 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\),.*?type=\"\\(.+?\\)\".*?}")
3216
3217 (defun gdb-var-list-children-handler-1 (varnum)
3218 (goto-char (point-min))
3219 (let ((var-list nil))
3220 (catch 'child-already-watched
3221 (dolist (var gdb-var-list)
3222 (if (string-equal varnum (car var))
3223 (progn
3224 (push var var-list)
3225 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3226 (let ((varchild (list (match-string 1)
3227 (match-string 2)
3228 (match-string 3)
3229 (match-string 5)
3230 (read (match-string 4))
3231 nil)))
3232 (if (assoc (car varchild) gdb-var-list)
3233 (throw 'child-already-watched nil))
3234 (push varchild var-list))))
3235 (push var var-list)))
3236 (setq gdb-var-list (nreverse var-list)))))
3237
3238 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3239 (defun gdb-var-update-1 ()
3240 (if (not (member 'gdb-var-update gdb-pending-triggers))
3241 (progn
3242 (gdb-enqueue-input
3243 (list
3244 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3245 "server interpreter mi \"-var-update --all-values *\"\n"
3246 "-var-update --all-values *\n")
3247 'gdb-var-update-handler-1))
3248 (push 'gdb-var-update gdb-pending-triggers))))
3249
3250 (defconst gdb-var-update-regexp-1
3251 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3252 in_scope=\"\\(.*?\\)\".*?}")
3253
3254 (defun gdb-var-update-handler-1 ()
3255 (dolist (var gdb-var-list)
3256 (setcar (nthcdr 5 var) nil))
3257 (goto-char (point-min))
3258 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3259 (let* ((varnum (match-string 1))
3260 (var (assoc varnum gdb-var-list)))
3261 (when var
3262 (if (string-equal (match-string 3) "false")
3263 (setcar (nthcdr 5 var) 'out-of-scope)
3264 (setcar (nthcdr 5 var) 'changed)
3265 (setcar (nthcdr 4 var)
3266 (read (match-string 2)))))))
3267 (setq gdb-pending-triggers
3268 (delq 'gdb-var-update gdb-pending-triggers))
3269 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
3270 ;; dummy command to update speedbar at right time
3271 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-refresh))
3272 ;; keep gdb-pending-triggers non-nil till end
3273 (push 'gdb-speedbar-refresh gdb-pending-triggers)))
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