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