]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-ui.el
Merge from emacs--devo--0
[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 (defcustom gdb-delete-out-of-scope t
951 "If non-nil delete watch expressions automatically when they go out of scope."
952 :type 'boolean
953 :group 'gud
954 :version "22.2")
955
956 (defun gdb-speedbar-expand-node (text token indent)
957 "Expand the node the user clicked on.
958 TEXT is the text of the button we clicked on, a + or - item.
959 TOKEN is data related to this node.
960 INDENT is the current indentation depth."
961 (if (and gud-comint-buffer (buffer-name gud-comint-buffer))
962 (progn
963 (cond ((string-match "+" text) ;expand this node
964 (let* ((var (assoc token gdb-var-list))
965 (expr (nth 1 var)) (children (nth 2 var)))
966 (if (or (<= (string-to-number children) gdb-max-children)
967 (y-or-n-p
968 (format
969 "%s has %s children. Continue? " expr children)))
970 (if (and (eq (buffer-local-value
971 'gud-minor-mode gud-comint-buffer) 'gdba)
972 (string-equal gdb-version "pre-6.4"))
973 (gdb-var-list-children token)
974 (gdb-var-list-children-1 token)))))
975 ((string-match "-" text) ;contract this node
976 (dolist (var gdb-var-list)
977 (if (string-match (concat token "\\.") (car var))
978 (setq gdb-var-list (delq var gdb-var-list))))
979 (gdb-var-delete-children token)
980 (speedbar-change-expand-button-char ?+)
981 (speedbar-delete-subblock indent))
982 (t (error "Ooops... not sure what to do")))
983 (speedbar-center-buffer-smartly))
984 (message-box "GUD session has been killed")))
985
986 (defun gdb-get-target-string ()
987 (with-current-buffer gud-comint-buffer
988 gud-target-name))
989 \f
990
991 ;;
992 ;; gdb buffers.
993 ;;
994 ;; Each buffer has a TYPE -- a symbol that identifies the function
995 ;; of that particular buffer.
996 ;;
997 ;; The usual gdb interaction buffer is given the type `gdba' and
998 ;; is constructed specially.
999 ;;
1000 ;; Others are constructed by gdb-get-buffer-create and
1001 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
1002
1003 (defvar gdb-buffer-rules-assoc '())
1004
1005 (defun gdb-get-buffer (key)
1006 "Return the gdb buffer tagged with type KEY.
1007 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1008 (save-excursion
1009 (gdb-look-for-tagged-buffer key (buffer-list))))
1010
1011 (defun gdb-get-buffer-create (key)
1012 "Create a new gdb buffer of the type specified by KEY.
1013 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1014 (or (gdb-get-buffer key)
1015 (let* ((rules (assoc key gdb-buffer-rules-assoc))
1016 (name (funcall (gdb-rules-name-maker rules)))
1017 (new (get-buffer-create name)))
1018 (with-current-buffer new
1019 (let ((trigger))
1020 (if (cdr (cdr rules))
1021 (setq trigger (funcall (car (cdr (cdr rules))))))
1022 (setq gdb-buffer-type key)
1023 (set (make-local-variable 'gud-minor-mode)
1024 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1025 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1026 (if trigger (funcall trigger)))
1027 new))))
1028
1029 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
1030
1031 (defun gdb-look-for-tagged-buffer (key bufs)
1032 (let ((retval nil))
1033 (while (and (not retval) bufs)
1034 (set-buffer (car bufs))
1035 (if (eq gdb-buffer-type key)
1036 (setq retval (car bufs)))
1037 (setq bufs (cdr bufs)))
1038 retval))
1039
1040 ;;
1041 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1042 ;; at least one and possible more functions. The functions have these
1043 ;; roles in defining a buffer type:
1044 ;;
1045 ;; NAME - Return a name for this buffer type.
1046 ;;
1047 ;; The remaining function(s) are optional:
1048 ;;
1049 ;; MODE - called in a new buffer with no arguments, should establish
1050 ;; the proper mode for the buffer.
1051 ;;
1052
1053 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1054 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
1055 (if binding
1056 (setcdr binding rules)
1057 (push (cons buffer-type rules)
1058 gdb-buffer-rules-assoc))))
1059
1060 ;; GUD buffers are an exception to the rules
1061 (gdb-set-buffer-rules 'gdba 'error)
1062
1063 ;; Partial-output buffer : This accumulates output from a command executed on
1064 ;; behalf of emacs (rather than the user).
1065 ;;
1066 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1067 'gdb-partial-output-name)
1068
1069 (defun gdb-partial-output-name ()
1070 (concat " *partial-output-"
1071 (gdb-get-target-string)
1072 "*"))
1073
1074 \f
1075 (gdb-set-buffer-rules 'gdb-inferior-io
1076 'gdb-inferior-io-name
1077 'gdb-inferior-io-mode)
1078
1079 (defun gdb-inferior-io-name ()
1080 (concat "*input/output of "
1081 (gdb-get-target-string)
1082 "*"))
1083
1084 (defun gdb-display-separate-io-buffer ()
1085 "Display IO of debugged program in a separate window."
1086 (interactive)
1087 (if gdb-use-separate-io-buffer
1088 (gdb-display-buffer
1089 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1090
1091 (defconst gdb-frame-parameters
1092 '((height . 14) (width . 80)
1093 (unsplittable . t)
1094 (tool-bar-lines . nil)
1095 (menu-bar-lines . nil)
1096 (minibuffer . nil)))
1097
1098 (defun gdb-frame-separate-io-buffer ()
1099 "Display IO of debugged program in a new frame."
1100 (interactive)
1101 (if gdb-use-separate-io-buffer
1102 (let ((special-display-regexps (append special-display-regexps '(".*")))
1103 (special-display-frame-alist gdb-frame-parameters))
1104 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1105
1106 (defvar gdb-inferior-io-mode-map
1107 (let ((map (make-sparse-keymap)))
1108 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1109 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1110 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1111 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1112 (define-key map "\C-d" 'gdb-separate-io-eof)
1113 map))
1114
1115 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1116 "Major mode for gdb inferior-io."
1117 :syntax-table nil :abbrev-table nil
1118 ;; We want to use comint because it has various nifty and familiar
1119 ;; features. We don't need a process, but comint wants one, so create
1120 ;; a dummy one.
1121 (make-comint-in-buffer
1122 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1123 (current-buffer) "hexl")
1124 (setq comint-input-sender 'gdb-inferior-io-sender))
1125
1126 (defun gdb-inferior-io-sender (proc string)
1127 ;; PROC is the pseudo-process created to satisfy comint.
1128 (with-current-buffer (process-buffer proc)
1129 (setq proc (get-buffer-process gud-comint-buffer))
1130 (process-send-string proc string)
1131 (process-send-string proc "\n")))
1132
1133 (defun gdb-separate-io-interrupt ()
1134 "Interrupt the program being debugged."
1135 (interactive)
1136 (interrupt-process
1137 (get-buffer-process gud-comint-buffer) comint-ptyp))
1138
1139 (defun gdb-separate-io-quit ()
1140 "Send quit signal to the program being debugged."
1141 (interactive)
1142 (quit-process
1143 (get-buffer-process gud-comint-buffer) comint-ptyp))
1144
1145 (defun gdb-separate-io-stop ()
1146 "Stop the program being debugged."
1147 (interactive)
1148 (stop-process
1149 (get-buffer-process gud-comint-buffer) comint-ptyp))
1150
1151 (defun gdb-separate-io-eof ()
1152 "Send end-of-file to the program being debugged."
1153 (interactive)
1154 (process-send-eof
1155 (get-buffer-process gud-comint-buffer)))
1156 \f
1157
1158 ;; gdb communications
1159 ;;
1160
1161 ;; INPUT: things sent to gdb
1162 ;;
1163 ;; The queues are lists. Each element is either a string (indicating user or
1164 ;; user-like input) or a list of the form:
1165 ;;
1166 ;; (INPUT-STRING HANDLER-FN)
1167 ;;
1168 ;; The handler function will be called from the partial-output buffer when the
1169 ;; command completes. This is the way to write commands which invoke gdb
1170 ;; commands autonomously.
1171 ;;
1172 ;; These lists are consumed tail first.
1173 ;;
1174
1175 (defun gdb-send (proc string)
1176 "A comint send filter for gdb.
1177 This filter may simply queue input for a later time."
1178 (when gdb-ready
1179 (with-current-buffer gud-comint-buffer
1180 (let ((inhibit-read-only t))
1181 (remove-text-properties (point-min) (point-max) '(face))))
1182 (if gud-running
1183 (progn
1184 (let ((item (concat string "\n")))
1185 (if gdb-enable-debug (push (cons 'send item) gdb-debug-log))
1186 (process-send-string proc item)))
1187 (if (string-match "\\\\\\'" string)
1188 (setq gdb-continuation (concat gdb-continuation string "\n"))
1189 (let ((item (concat gdb-continuation string
1190 (if (not comint-input-sender-no-newline) "\n"))))
1191 (gdb-enqueue-input item)
1192 (setq gdb-continuation nil))))))
1193
1194 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1195 ;; is a query, or other non-top-level prompt.
1196
1197 (defun gdb-enqueue-input (item)
1198 (if (not gud-running)
1199 (if gdb-prompting
1200 (progn
1201 (gdb-send-item item)
1202 (setq gdb-prompting nil))
1203 (push item gdb-input-queue))))
1204
1205 (defun gdb-dequeue-input ()
1206 (let ((queue gdb-input-queue))
1207 (and queue
1208 (let ((last (car (last queue))))
1209 (unless (nbutlast queue) (setq gdb-input-queue '()))
1210 last))))
1211
1212 (defun gdb-send-item (item)
1213 (setq gdb-flush-pending-output nil)
1214 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1215 (setq gdb-current-item item)
1216 (let ((process (get-buffer-process gud-comint-buffer)))
1217 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1218 (if (stringp item)
1219 (progn
1220 (setq gdb-output-sink 'user)
1221 (process-send-string process item))
1222 (progn
1223 (gdb-clear-partial-output)
1224 (setq gdb-output-sink 'pre-emacs)
1225 (process-send-string process
1226 (car item))))
1227 ;; case: eq gud-minor-mode 'gdbmi
1228 (gdb-clear-partial-output)
1229 (setq gdb-output-sink 'emacs)
1230 (process-send-string process (car item)))))
1231 \f
1232 ;;
1233 ;; output -- things gdb prints to emacs
1234 ;;
1235 ;; GDB output is a stream interrupted by annotations.
1236 ;; Annotations can be recognized by their beginning
1237 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1238 ;;
1239 ;; The tag is a string obeying symbol syntax.
1240 ;;
1241 ;; The optional part `<opt>' can be either the empty string
1242 ;; or a space followed by more data relating to the annotation.
1243 ;; For example, the SOURCE annotation is followed by a filename,
1244 ;; line number and various useless goo. This data must not include
1245 ;; any newlines.
1246 ;;
1247
1248 (defcustom gud-gdb-command-name "gdb --annotate=3"
1249 "Default command to execute an executable under the GDB debugger."
1250 :type 'string
1251 :group 'gud
1252 :version "22.1")
1253
1254 (defvar gdb-annotation-rules
1255 '(("pre-prompt" gdb-pre-prompt)
1256 ("prompt" gdb-prompt)
1257 ("commands" gdb-subprompt)
1258 ("overload-choice" gdb-subprompt)
1259 ("query" gdb-subprompt)
1260 ;; Need this prompt for GDB 6.1
1261 ("nquery" gdb-subprompt)
1262 ("prompt-for-continue" gdb-subprompt)
1263 ("post-prompt" gdb-post-prompt)
1264 ("source" gdb-source)
1265 ("starting" gdb-starting)
1266 ("exited" gdb-exited)
1267 ("signalled" gdb-signalled)
1268 ("signal" gdb-signal)
1269 ("breakpoint" gdb-stopping)
1270 ("watchpoint" gdb-stopping)
1271 ("frame-begin" gdb-frame-begin)
1272 ("stopped" gdb-stopped)
1273 ("error-begin" gdb-error)
1274 ("error" gdb-error)
1275 ) "An assoc mapping annotation tags to functions which process them.")
1276
1277 (defun gdb-resync()
1278 (setq gdb-flush-pending-output t)
1279 (setq gud-running nil)
1280 (gdb-force-mode-line-update
1281 (propertize "stopped"'face font-lock-warning-face))
1282 (setq gdb-output-sink 'user)
1283 (setq gdb-input-queue nil)
1284 (setq gdb-pending-triggers nil)
1285 (setq gdb-prompting t))
1286
1287 (defconst gdb-source-spec-regexp
1288 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1289
1290 ;; Do not use this except as an annotation handler.
1291 (defun gdb-source (args)
1292 (string-match gdb-source-spec-regexp args)
1293 ;; Extract the frame position from the marker.
1294 (setq gud-last-frame
1295 (cons
1296 (match-string 1 args)
1297 (string-to-number (match-string 2 args))))
1298 (setq gdb-pc-address (match-string 3 args))
1299 ;; cover for auto-display output which comes *before*
1300 ;; stopped annotation
1301 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1302
1303 (defun gdb-pre-prompt (ignored)
1304 "An annotation handler for `pre-prompt'.
1305 This terminates the collection of output from a previous command if that
1306 happens to be in effect."
1307 (setq gdb-error nil)
1308 (let ((sink gdb-output-sink))
1309 (cond
1310 ((eq sink 'user) t)
1311 ((eq sink 'emacs)
1312 (setq gdb-output-sink 'post-emacs))
1313 (t
1314 (gdb-resync)
1315 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1316
1317 (defun gdb-prompt (ignored)
1318 "An annotation handler for `prompt'.
1319 This sends the next command (if any) to gdb."
1320 (when gdb-first-prompt
1321 (gdb-force-mode-line-update
1322 (propertize "initializing..." 'face font-lock-variable-name-face))
1323 (gdb-init-1)
1324 (setq gdb-first-prompt nil))
1325 (let ((sink gdb-output-sink))
1326 (cond
1327 ((eq sink 'user) t)
1328 ((eq sink 'post-emacs)
1329 (setq gdb-output-sink 'user)
1330 (let ((handler
1331 (car (cdr gdb-current-item))))
1332 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1333 (funcall handler))))
1334 (t
1335 (gdb-resync)
1336 (error "Phase error in gdb-prompt (got %s)" sink))))
1337 (let ((input (gdb-dequeue-input)))
1338 (if input
1339 (gdb-send-item input)
1340 (progn
1341 (setq gdb-prompting t)
1342 (gud-display-frame)))))
1343
1344 (defun gdb-subprompt (ignored)
1345 "An annotation handler for non-top-level prompts."
1346 (setq gdb-prompting t))
1347
1348 (defun gdb-starting (ignored)
1349 "An annotation handler for `starting'.
1350 This says that I/O for the subprocess is now the program being debugged,
1351 not GDB."
1352 (setq gdb-active-process t)
1353 (setq gdb-printing t)
1354 (let ((sink gdb-output-sink))
1355 (cond
1356 ((eq sink 'user)
1357 (progn
1358 (setq gud-running t)
1359 (setq gdb-inferior-status "running")
1360 (setq gdb-signalled nil)
1361 (gdb-force-mode-line-update
1362 (propertize gdb-inferior-status 'face font-lock-type-face))
1363 (gdb-remove-text-properties)
1364 (setq gud-old-arrow gud-overlay-arrow-position)
1365 (setq gud-overlay-arrow-position nil)
1366 (setq gdb-overlay-arrow-position nil)
1367 (setq gdb-stack-position nil)
1368 (if gdb-use-separate-io-buffer
1369 (setq gdb-output-sink 'inferior))))
1370 (t
1371 (gdb-resync)
1372 (error "Unexpected `starting' annotation")))))
1373
1374 (defun gdb-signal (ignored)
1375 (setq gdb-inferior-status "signal")
1376 (gdb-force-mode-line-update
1377 (propertize gdb-inferior-status 'face font-lock-warning-face))
1378 (gdb-stopping ignored))
1379
1380 (defun gdb-stopping (ignored)
1381 "An annotation handler for `breakpoint' and other annotations.
1382 They say that I/O for the subprocess is now GDB, not the program
1383 being debugged."
1384 (if gdb-use-separate-io-buffer
1385 (let ((sink gdb-output-sink))
1386 (cond
1387 ((eq sink 'inferior)
1388 (setq gdb-output-sink 'user))
1389 (t
1390 (gdb-resync)
1391 (error "Unexpected stopping annotation"))))))
1392
1393 (defun gdb-exited (ignored)
1394 "An annotation handler for `exited' and `signalled'.
1395 They say that I/O for the subprocess is now GDB, not the program
1396 being debugged and that the program is no longer running. This
1397 function is used to change the focus of GUD tooltips to #define
1398 directives."
1399 (setq gdb-active-process nil)
1400 (setq gud-overlay-arrow-position nil)
1401 (setq gdb-overlay-arrow-position nil)
1402 (setq gdb-stack-position nil)
1403 (setq gud-old-arrow nil)
1404 (setq gdb-inferior-status "exited")
1405 (gdb-force-mode-line-update
1406 (propertize gdb-inferior-status 'face font-lock-warning-face))
1407 (gdb-stopping ignored))
1408
1409 (defun gdb-signalled (ignored)
1410 (setq gdb-signalled t))
1411
1412 (defun gdb-frame-begin (ignored)
1413 (setq gdb-frame-begin t)
1414 (setq gdb-printing nil)
1415 (let ((sink gdb-output-sink))
1416 (cond
1417 ((eq sink 'inferior)
1418 (setq gdb-output-sink 'user))
1419 ((eq sink 'user) t)
1420 ((eq sink 'emacs) t)
1421 (t
1422 (gdb-resync)
1423 (error "Unexpected frame-begin annotation (%S)" sink)))))
1424
1425 (defcustom gdb-same-frame focus-follows-mouse
1426 "Non-nil means pop up GUD buffer in same frame."
1427 :group 'gud
1428 :type 'boolean
1429 :version "22.1")
1430
1431 (defcustom gdb-find-source-frame nil
1432 "Non-nil means try to find a source frame further up stack e.g after signal."
1433 :group 'gud
1434 :type 'boolean
1435 :version "22.1")
1436
1437 (defun gdb-find-source-frame (arg)
1438 "Toggle trying to find a source frame further up stack.
1439 With prefix argument ARG, look for a source frame further up
1440 stack if ARG is positive, otherwise don't look further up."
1441 (interactive "P")
1442 (setq gdb-find-source-frame
1443 (if (null arg)
1444 (not gdb-find-source-frame)
1445 (> (prefix-numeric-value arg) 0)))
1446 (message (format "Looking for source frame %sabled"
1447 (if gdb-find-source-frame "en" "dis"))))
1448
1449 (defun gdb-stopped (ignored)
1450 "An annotation handler for `stopped'.
1451 It is just like `gdb-stopping', except that if we already set the output
1452 sink to `user' in `gdb-stopping', that is fine."
1453 (setq gud-running nil)
1454 (unless (or gud-overlay-arrow-position gud-last-frame)
1455 (if (and gdb-frame-begin gdb-printing)
1456 (setq gud-overlay-arrow-position gud-old-arrow)
1457 ;;Pop up GUD buffer to display current frame when it doesn't have source
1458 ;;information i.e if not compiled with -g as with libc routines generally.
1459 (if gdb-same-frame
1460 (gdb-display-gdb-buffer)
1461 (gdb-frame-gdb-buffer))
1462 (if gdb-find-source-frame
1463 ;;Try to find source further up stack e.g after signal.
1464 (setq gdb-look-up-stack
1465 (if (gdb-get-buffer 'gdb-stack-buffer)
1466 'keep
1467 (progn
1468 (gdb-get-buffer-create 'gdb-stack-buffer)
1469 (gdb-invalidate-frames)
1470 'delete))))))
1471 (unless (member gdb-inferior-status '("exited" "signal"))
1472 (setq gdb-active-process t) ;Just for attaching case.
1473 (setq gdb-inferior-status "stopped")
1474 (gdb-force-mode-line-update
1475 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1476 (let ((sink gdb-output-sink))
1477 (cond
1478 ((eq sink 'inferior)
1479 (setq gdb-output-sink 'user))
1480 ((eq sink 'user) t)
1481 (t
1482 (gdb-resync)
1483 (error "Unexpected stopped annotation"))))
1484 (if gdb-signalled (gdb-exited ignored)))
1485
1486 (defun gdb-error (ignored)
1487 (setq gdb-error (not gdb-error)))
1488
1489 (defun gdb-post-prompt (ignored)
1490 "An annotation handler for `post-prompt'.
1491 This begins the collection of output from the current command if that
1492 happens to be appropriate."
1493 ;; Don't add to queue if there outstanding items or gdb-version is not known
1494 ;; yet.
1495 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1496 (gdb-get-selected-frame)
1497 (gdb-invalidate-frames)
1498 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1499 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1500 (gdb-invalidate-breakpoints)
1501 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1502 ;; so gdb-pc-address is updated.
1503 ;; (gdb-invalidate-assembler)
1504
1505 (if (string-equal gdb-version "pre-6.4")
1506 (gdb-invalidate-registers)
1507 (gdb-get-changed-registers)
1508 (gdb-invalidate-registers-1))
1509
1510 (gdb-invalidate-memory)
1511 (if (string-equal gdb-version "pre-6.4")
1512 (gdb-invalidate-locals)
1513 (gdb-invalidate-locals-1))
1514
1515 (gdb-invalidate-threads)
1516 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1517 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1518 ;; Only needed/used with speedbar/watch expressions.
1519 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1520 (if (string-equal gdb-version "pre-6.4")
1521 (gdb-var-update)
1522 (gdb-var-update-1)))))
1523 (setq gdb-first-post-prompt nil)
1524 (let ((sink gdb-output-sink))
1525 (cond
1526 ((eq sink 'user) t)
1527 ((eq sink 'pre-emacs)
1528 (setq gdb-output-sink 'emacs))
1529 (t
1530 (gdb-resync)
1531 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1532
1533 (defconst gdb-buffer-list
1534 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1535
1536 (defun gdb-remove-text-properties ()
1537 (dolist (buffertype gdb-buffer-list)
1538 (let ((buffer (gdb-get-buffer buffertype)))
1539 (if buffer
1540 (with-current-buffer buffer
1541 (let ((inhibit-read-only t))
1542 (remove-text-properties
1543 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1544
1545 ;; GUD displays the selected GDB frame. This might might not be the current
1546 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1547 ;; visited breakpoint is, use that window.
1548 (defun gdb-display-source-buffer (buffer)
1549 (let* ((last-window (if gud-last-last-frame
1550 (get-buffer-window
1551 (gud-find-file (car gud-last-last-frame)))))
1552 (source-window (or last-window
1553 (if (and gdb-source-window
1554 (window-live-p gdb-source-window))
1555 gdb-source-window))))
1556 (when source-window
1557 (setq gdb-source-window source-window)
1558 (set-window-buffer source-window buffer))
1559 source-window))
1560
1561 ;; Derived from gud-gdb-marker-regexp
1562 (defvar gdb-fullname-regexp
1563 (concat "\\(.:?[^" ":" "\n]*\\)" ":" "\\([0-9]*\\)" ":" ".*"))
1564
1565 (defun gud-gdba-marker-filter (string)
1566 "A gud marker filter for gdb. Handle a burst of output from GDB."
1567 (if gdb-flush-pending-output
1568 nil
1569 (when gdb-enable-debug
1570 (push (cons 'recv string) gdb-debug-log)
1571 (if (and gdb-debug-log-max
1572 (> (length gdb-debug-log) gdb-debug-log-max))
1573 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1574 ;; Recall the left over gud-marker-acc from last time.
1575 (setq gud-marker-acc (concat gud-marker-acc string))
1576 ;; Start accumulating output for the GUD buffer.
1577 (let ((output ""))
1578 ;;
1579 ;; Process all the complete markers in this chunk.
1580 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1581 (let ((annotation (match-string 1 gud-marker-acc))
1582 (before (substring gud-marker-acc 0 (match-beginning 0)))
1583 (after (substring gud-marker-acc (match-end 0))))
1584 ;;
1585 ;; Parse the tag from the annotation, and maybe its arguments.
1586 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1587 (let* ((annotation-type (match-string 1 annotation))
1588 (annotation-arguments (match-string 2 annotation))
1589 (annotation-rule (assoc annotation-type
1590 gdb-annotation-rules))
1591 (fullname (string-match gdb-fullname-regexp annotation-type)))
1592
1593 ;; Stuff prior to the match is just ordinary output.
1594 ;; It is either concatenated to OUTPUT or directed
1595 ;; elsewhere.
1596 (setq output
1597 (gdb-concat-output output
1598 (concat before (if fullname "\n"))))
1599
1600 ;; Take that stuff off the gud-marker-acc.
1601 (setq gud-marker-acc after)
1602
1603 ;; Call the handler for this annotation.
1604 (if annotation-rule
1605 (funcall (car (cdr annotation-rule))
1606 annotation-arguments)
1607
1608 ;; Switch to gud-gdb-marker-filter if appropriate.
1609 (when fullname
1610
1611 ;; Extract the frame position from the marker.
1612 (setq gud-last-frame (cons (match-string 1 annotation)
1613 (string-to-number
1614 (match-string 2 annotation))))
1615
1616 (set (make-local-variable 'gud-minor-mode) 'gdb)
1617 (set (make-local-variable 'gud-marker-filter)
1618 'gud-gdb-marker-filter)))
1619
1620 ;; Else the annotation is not recognized. Ignore it silently,
1621 ;; so that GDB can add new annotations without causing
1622 ;; us to blow up.
1623 )))
1624
1625 ;; Does the remaining text end in a partial line?
1626 ;; If it does, then keep part of the gud-marker-acc until we get more.
1627 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1628 gud-marker-acc)
1629 (progn
1630 ;; Everything before the potential marker start can be output.
1631 (setq output
1632 (gdb-concat-output output
1633 (substring gud-marker-acc 0
1634 (match-beginning 0))))
1635 ;;
1636 ;; Everything after, we save, to combine with later input.
1637 (setq gud-marker-acc (substring gud-marker-acc
1638 (match-beginning 0))))
1639 ;;
1640 ;; In case we know the gud-marker-acc contains no partial annotations:
1641 (progn
1642 (setq output (gdb-concat-output output gud-marker-acc))
1643 (setq gud-marker-acc "")))
1644 output)))
1645
1646 (defun gdb-concat-output (so-far new)
1647 (if gdb-error
1648 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1649 (let ((sink gdb-output-sink))
1650 (cond
1651 ((eq sink 'user) (concat so-far new))
1652 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1653 ((eq sink 'emacs)
1654 (gdb-append-to-partial-output new)
1655 so-far)
1656 ((eq sink 'inferior)
1657 (gdb-append-to-inferior-io new)
1658 so-far)
1659 (t
1660 (gdb-resync)
1661 (error "Bogon output sink %S" sink)))))
1662
1663 (defun gdb-append-to-partial-output (string)
1664 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1665 (goto-char (point-max))
1666 (insert string)))
1667
1668 (defun gdb-clear-partial-output ()
1669 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1670 (erase-buffer)))
1671
1672 (defun gdb-append-to-inferior-io (string)
1673 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1674 (goto-char (point-max))
1675 (insert-before-markers string))
1676 (if (not (string-equal string ""))
1677 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1678
1679 (defun gdb-clear-inferior-io ()
1680 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1681 (erase-buffer)))
1682 \f
1683
1684 ;; One trick is to have a command who's output is always available in a buffer
1685 ;; of it's own, and is always up to date. We build several buffers of this
1686 ;; type.
1687 ;;
1688 ;; There are two aspects to this: gdb has to tell us when the output for that
1689 ;; command might have changed, and we have to be able to run the command
1690 ;; behind the user's back.
1691 ;;
1692 ;; The output phasing associated with the variable gdb-output-sink
1693 ;; help us to run commands behind the user's back.
1694 ;;
1695 ;; Below is the code for specificly managing buffers of output from one
1696 ;; command.
1697 ;;
1698
1699 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1700 ;; It adds an input for the command we are tracking. It should be the
1701 ;; annotation rule binding of whatever gdb sends to tell us this command
1702 ;; might have changed it's output.
1703 ;;
1704 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1705 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1706 ;; input in the input queue (see comment about ``gdb communications'' above).
1707
1708 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1709 output-handler)
1710 `(defun ,name (&optional ignored)
1711 (if (and ,demand-predicate
1712 (not (member ',name
1713 gdb-pending-triggers)))
1714 (progn
1715 (gdb-enqueue-input
1716 (list ,gdb-command ',output-handler))
1717 (push ',name gdb-pending-triggers)))))
1718
1719 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1720 `(defun ,name ()
1721 (setq gdb-pending-triggers
1722 (delq ',trigger
1723 gdb-pending-triggers))
1724 (let ((buf (gdb-get-buffer ',buf-key)))
1725 (and buf
1726 (with-current-buffer buf
1727 (let* ((window (get-buffer-window buf 0))
1728 (start (window-start window))
1729 (p (window-point window))
1730 (buffer-read-only nil))
1731 (erase-buffer)
1732 (insert-buffer-substring (gdb-get-buffer-create
1733 'gdb-partial-output-buffer))
1734 (set-window-start window start)
1735 (set-window-point window p)))))
1736 ;; put customisation here
1737 (,custom-defun)))
1738
1739 (defmacro def-gdb-auto-updated-buffer (buffer-key
1740 trigger-name gdb-command
1741 output-handler-name custom-defun)
1742 `(progn
1743 (def-gdb-auto-update-trigger ,trigger-name
1744 ;; The demand predicate:
1745 (gdb-get-buffer ',buffer-key)
1746 ,gdb-command
1747 ,output-handler-name)
1748 (def-gdb-auto-update-handler ,output-handler-name
1749 ,trigger-name ,buffer-key ,custom-defun)))
1750
1751 \f
1752 ;;
1753 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1754 ;;
1755 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1756 'gdb-breakpoints-buffer-name
1757 'gdb-breakpoints-mode)
1758
1759 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1760 ;; This defines the auto update rule for buffers of type
1761 ;; `gdb-breakpoints-buffer'.
1762 ;;
1763 ;; It defines a function to serve as the annotation handler that
1764 ;; handles the `foo-invalidated' message. That function is called:
1765 gdb-invalidate-breakpoints
1766 ;;
1767 ;; To update the buffer, this command is sent to gdb.
1768 "server info breakpoints\n"
1769 ;;
1770 ;; This also defines a function to be the handler for the output
1771 ;; from the command above. That function will copy the output into
1772 ;; the appropriately typed buffer. That function will be called:
1773 gdb-info-breakpoints-handler
1774 ;; buffer specific functions
1775 gdb-info-breakpoints-custom)
1776
1777 (defconst breakpoint-xpm-data
1778 "/* XPM */
1779 static char *magick[] = {
1780 /* columns rows colors chars-per-pixel */
1781 \"10 10 2 1\",
1782 \" c red\",
1783 \"+ c None\",
1784 /* pixels */
1785 \"+++ +++\",
1786 \"++ ++\",
1787 \"+ +\",
1788 \" \",
1789 \" \",
1790 \" \",
1791 \" \",
1792 \"+ +\",
1793 \"++ ++\",
1794 \"+++ +++\",
1795 };"
1796 "XPM data used for breakpoint icon.")
1797
1798 (defconst breakpoint-enabled-pbm-data
1799 "P1
1800 10 10\",
1801 0 0 0 0 1 1 1 1 0 0 0 0
1802 0 0 0 1 1 1 1 1 1 0 0 0
1803 0 0 1 1 1 1 1 1 1 1 0 0
1804 0 1 1 1 1 1 1 1 1 1 1 0
1805 0 1 1 1 1 1 1 1 1 1 1 0
1806 0 1 1 1 1 1 1 1 1 1 1 0
1807 0 1 1 1 1 1 1 1 1 1 1 0
1808 0 0 1 1 1 1 1 1 1 1 0 0
1809 0 0 0 1 1 1 1 1 1 0 0 0
1810 0 0 0 0 1 1 1 1 0 0 0 0"
1811 "PBM data used for enabled breakpoint icon.")
1812
1813 (defconst breakpoint-disabled-pbm-data
1814 "P1
1815 10 10\",
1816 0 0 1 0 1 0 1 0 0 0
1817 0 1 0 1 0 1 0 1 0 0
1818 1 0 1 0 1 0 1 0 1 0
1819 0 1 0 1 0 1 0 1 0 1
1820 1 0 1 0 1 0 1 0 1 0
1821 0 1 0 1 0 1 0 1 0 1
1822 1 0 1 0 1 0 1 0 1 0
1823 0 1 0 1 0 1 0 1 0 1
1824 0 0 1 0 1 0 1 0 1 0
1825 0 0 0 1 0 1 0 1 0 0"
1826 "PBM data used for disabled breakpoint icon.")
1827
1828 (defvar breakpoint-enabled-icon nil
1829 "Icon for enabled breakpoint in display margin.")
1830
1831 (defvar breakpoint-disabled-icon nil
1832 "Icon for disabled breakpoint in display margin.")
1833
1834 (and (display-images-p)
1835 ;; Bitmap for breakpoint in fringe
1836 (define-fringe-bitmap 'breakpoint
1837 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1838 ;; Bitmap for gud-overlay-arrow in fringe
1839 (define-fringe-bitmap 'hollow-right-triangle
1840 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1841
1842 (defface breakpoint-enabled
1843 '((t
1844 :foreground "red1"
1845 :weight bold))
1846 "Face for enabled breakpoint icon in fringe."
1847 :group 'gud)
1848
1849 (defface breakpoint-disabled
1850 '((((class color) (min-colors 88)) :foreground "grey70")
1851 ;; Ensure that on low-color displays that we end up something visible.
1852 (((class color) (min-colors 8) (background light))
1853 :foreground "black")
1854 (((class color) (min-colors 8) (background dark))
1855 :foreground "white")
1856 (((type tty) (class mono))
1857 :inverse-video t)
1858 (t :background "gray"))
1859 "Face for disabled breakpoint icon in fringe."
1860 :group 'gud)
1861
1862 (defconst gdb-breakpoint-regexp
1863 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1864
1865 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1866 (defun gdb-info-breakpoints-custom ()
1867 (let ((flag) (bptno))
1868 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1869 (dolist (buffer (buffer-list))
1870 (with-current-buffer buffer
1871 (if (and (memq gud-minor-mode '(gdba gdbmi))
1872 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
1873 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1874 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1875 (save-excursion
1876 (let ((buffer-read-only nil))
1877 (goto-char (point-min))
1878 (while (< (point) (- (point-max) 1))
1879 (forward-line 1)
1880 (if (looking-at gdb-breakpoint-regexp)
1881 (progn
1882 (setq bptno (match-string 1))
1883 (setq flag (char-after (match-beginning 2)))
1884 (add-text-properties
1885 (match-beginning 2) (match-end 2)
1886 (if (eq flag ?y)
1887 '(face font-lock-warning-face)
1888 '(face font-lock-type-face)))
1889 (let ((bl (point))
1890 (el (line-end-position)))
1891 (if (re-search-forward " in \\(.*\\) at\\s-+" el t)
1892 (progn
1893 (add-text-properties
1894 (match-beginning 1) (match-end 1)
1895 '(face font-lock-function-name-face))
1896 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1897 (let ((line (match-string 2))
1898 (file (match-string 1)))
1899 (add-text-properties bl el
1900 '(mouse-face highlight
1901 help-echo "mouse-2, RET: visit breakpoint"))
1902 (unless (file-exists-p file)
1903 (setq file (cdr (assoc bptno gdb-location-alist))))
1904 (if (and file
1905 (not (string-equal file "File not found")))
1906 (with-current-buffer
1907 (find-file-noselect file 'nowarn)
1908 (set (make-local-variable 'gud-minor-mode)
1909 'gdba)
1910 (set (make-local-variable 'tool-bar-map)
1911 gud-tool-bar-map)
1912 ;; Only want one breakpoint icon at each
1913 ;; location.
1914 (save-excursion
1915 (goto-line (string-to-number line))
1916 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1917 (gdb-enqueue-input
1918 (list
1919 (concat gdb-server-prefix "list "
1920 (match-string-no-properties 1) ":1\n")
1921 'ignore))
1922 (gdb-enqueue-input
1923 (list (concat gdb-server-prefix "info source\n")
1924 `(lambda () (gdb-get-location
1925 ,bptno ,line ,flag)))))))
1926 (if (re-search-forward
1927 "<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1928 el t)
1929 (add-text-properties
1930 (match-beginning 1) (match-end 1)
1931 '(face font-lock-function-name-face))
1932 (end-of-line)
1933 (re-search-backward "\\s-\\(\\S-*\\)"
1934 bl t)
1935 (add-text-properties
1936 (match-beginning 1) (match-end 1)
1937 '(face font-lock-variable-name-face)))))))
1938 (end-of-line))))))
1939 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1940
1941 (defun gdb-mouse-set-clear-breakpoint (event)
1942 "Set/clear breakpoint in left fringe/margin with mouse click."
1943 (interactive "e")
1944 (mouse-minibuffer-check event)
1945 (let ((posn (event-end event)))
1946 (if (numberp (posn-point posn))
1947 (with-selected-window (posn-window posn)
1948 (save-excursion
1949 (goto-char (posn-point posn))
1950 (if (or (posn-object posn)
1951 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1952 'breakpoint))
1953 (gud-remove nil)
1954 (gud-break nil)))))))
1955
1956 (defun gdb-mouse-toggle-breakpoint-margin (event)
1957 "Enable/disable breakpoint in left margin with mouse click."
1958 (interactive "e")
1959 (mouse-minibuffer-check event)
1960 (let ((posn (event-end event)))
1961 (if (numberp (posn-point posn))
1962 (with-selected-window (posn-window posn)
1963 (save-excursion
1964 (goto-char (posn-point posn))
1965 (if (posn-object posn)
1966 (gdb-enqueue-input
1967 (list
1968 (let ((bptno (get-text-property
1969 0 'gdb-bptno (car (posn-string posn)))))
1970 (concat gdb-server-prefix
1971 (if (get-text-property
1972 0 'gdb-enabled (car (posn-string posn)))
1973 "disable "
1974 "enable ")
1975 bptno "\n"))
1976 'ignore))))))))
1977
1978 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1979 "Enable/disable breakpoint in left fringe with mouse click."
1980 (interactive "e")
1981 (mouse-minibuffer-check event)
1982 (let* ((posn (event-end event))
1983 (pos (posn-point posn))
1984 obj)
1985 (when (numberp pos)
1986 (with-selected-window (posn-window posn)
1987 (save-excursion
1988 (set-buffer (window-buffer (selected-window)))
1989 (goto-char pos)
1990 (dolist (overlay (overlays-in pos pos))
1991 (when (overlay-get overlay 'put-break)
1992 (setq obj (overlay-get overlay 'before-string))))
1993 (when (stringp obj)
1994 (gdb-enqueue-input
1995 (list
1996 (concat gdb-server-prefix
1997 (if (get-text-property 0 'gdb-enabled obj)
1998 "disable "
1999 "enable ")
2000 (get-text-property 0 'gdb-bptno obj) "\n")
2001 'ignore))))))))
2002
2003 (defun gdb-breakpoints-buffer-name ()
2004 (with-current-buffer gud-comint-buffer
2005 (concat "*breakpoints of " (gdb-get-target-string) "*")))
2006
2007 (defun gdb-display-breakpoints-buffer ()
2008 "Display status of user-settable breakpoints."
2009 (interactive)
2010 (gdb-display-buffer
2011 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
2012
2013 (defun gdb-frame-breakpoints-buffer ()
2014 "Display status of user-settable breakpoints in a new frame."
2015 (interactive)
2016 (let ((special-display-regexps (append special-display-regexps '(".*")))
2017 (special-display-frame-alist gdb-frame-parameters))
2018 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
2019
2020 (defvar gdb-breakpoints-mode-map
2021 (let ((map (make-sparse-keymap))
2022 (menu (make-sparse-keymap "Breakpoints")))
2023 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2024 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2025 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2026 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2027 (suppress-keymap map)
2028 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2029 (define-key map " " 'gdb-toggle-breakpoint)
2030 (define-key map "D" 'gdb-delete-breakpoint)
2031 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2032 (define-key map "q" 'gdb-delete-frame-or-window)
2033 (define-key map "\r" 'gdb-goto-breakpoint)
2034 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2035 (define-key map [follow-link] 'mouse-face)
2036 map))
2037
2038 (defun gdb-delete-frame-or-window ()
2039 "Delete frame if there is only one window. Otherwise delete the window."
2040 (interactive)
2041 (if (one-window-p) (delete-frame)
2042 (delete-window)))
2043
2044 (defun gdb-breakpoints-mode ()
2045 "Major mode for gdb breakpoints.
2046
2047 \\{gdb-breakpoints-mode-map}"
2048 (kill-all-local-variables)
2049 (setq major-mode 'gdb-breakpoints-mode)
2050 (setq mode-name "Breakpoints")
2051 (use-local-map gdb-breakpoints-mode-map)
2052 (setq buffer-read-only t)
2053 (run-mode-hooks 'gdb-breakpoints-mode-hook)
2054 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2055 'gdb-invalidate-breakpoints
2056 'gdbmi-invalidate-breakpoints))
2057
2058 (defun gdb-toggle-breakpoint ()
2059 "Enable/disable breakpoint at current line."
2060 (interactive)
2061 (save-excursion
2062 (beginning-of-line 1)
2063 (if (looking-at gdb-breakpoint-regexp)
2064 (gdb-enqueue-input
2065 (list
2066 (concat gdb-server-prefix
2067 (if (eq ?y (char-after (match-beginning 2)))
2068 "disable "
2069 "enable ")
2070 (match-string 1) "\n") 'ignore))
2071 (error "Not recognized as break/watchpoint line"))))
2072
2073 (defun gdb-delete-breakpoint ()
2074 "Delete the breakpoint at current line."
2075 (interactive)
2076 (beginning-of-line 1)
2077 (if (looking-at gdb-breakpoint-regexp)
2078 (gdb-enqueue-input
2079 (list
2080 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
2081 (error "Not recognized as break/watchpoint line")))
2082
2083 (defun gdb-goto-breakpoint (&optional event)
2084 "Display the breakpoint location specified at current line."
2085 (interactive (list last-input-event))
2086 (if event (posn-set-point (event-end event)))
2087 (save-excursion
2088 (beginning-of-line 1)
2089 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
2090 (let ((bptno (match-string 1))
2091 (file (match-string 2))
2092 (line (match-string 3)))
2093 (save-selected-window
2094 (let* ((buffer (find-file-noselect
2095 (if (file-exists-p file) file
2096 (cdr (assoc bptno gdb-location-alist)))))
2097 (window (or (gdb-display-source-buffer buffer)
2098 (display-buffer buffer))))
2099 (setq gdb-source-window window)
2100 (with-current-buffer buffer
2101 (goto-line (string-to-number line))
2102 (set-window-point window (point))))))
2103 (error "No location specified."))))
2104 \f
2105
2106 ;; Frames buffer. This displays a perpetually correct bactracktrace
2107 ;; (from the command `where').
2108 ;;
2109 ;; Alas, if your stack is deep, it is costly.
2110 ;;
2111 (defcustom gdb-max-frames 40
2112 "Maximum number of frames displayed in call stack."
2113 :type 'integer
2114 :group 'gud
2115 :version "22.1")
2116
2117 (gdb-set-buffer-rules 'gdb-stack-buffer
2118 'gdb-stack-buffer-name
2119 'gdb-frames-mode)
2120
2121 (def-gdb-auto-updated-buffer gdb-stack-buffer
2122 gdb-invalidate-frames
2123 (concat "server info stack " (number-to-string gdb-max-frames) "\n")
2124 gdb-info-stack-handler
2125 gdb-info-stack-custom)
2126
2127 (defun gdb-info-stack-custom ()
2128 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2129 (let (move-to)
2130 (save-excursion
2131 (unless (eq gdb-look-up-stack 'delete)
2132 (let ((buffer-read-only nil)
2133 bl el)
2134 (goto-char (point-min))
2135 (while (< (point) (point-max))
2136 (setq bl (line-beginning-position)
2137 el (line-end-position))
2138 (when (looking-at "#")
2139 (add-text-properties bl el
2140 '(mouse-face highlight
2141 help-echo "mouse-2, RET: Select frame")))
2142 (goto-char bl)
2143 (when (looking-at "^#\\([0-9]+\\)")
2144 (when (string-equal (match-string 1) gdb-frame-number)
2145 (if (> (car (window-fringes)) 0)
2146 (progn
2147 (or gdb-stack-position
2148 (setq gdb-stack-position (make-marker)))
2149 (set-marker gdb-stack-position (point))
2150 (setq move-to gdb-stack-position))
2151 (put-text-property bl (+ bl 4)
2152 'face '(:inverse-video t))
2153 (setq move-to bl)))
2154 (when (re-search-forward
2155 (concat
2156 (if (string-equal (match-string 1) "0") "" " in ")
2157 "\\([^ ]+\\) (") el t)
2158 (put-text-property (match-beginning 1) (match-end 1)
2159 'face font-lock-function-name-face)
2160 (setq bl (match-end 0))
2161 (while (re-search-forward "<\\([^>]+\\)>" el t)
2162 (put-text-property (match-beginning 1) (match-end 1)
2163 'face font-lock-function-name-face))
2164 (goto-char bl)
2165 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2166 (put-text-property (match-beginning 1) (match-end 1)
2167 'face font-lock-variable-name-face))))
2168 (forward-line 1))
2169 (forward-line -1)
2170 (when (looking-at "(More stack frames follow...)")
2171 (add-text-properties (match-beginning 0) (match-end 0)
2172 '(mouse-face highlight
2173 gdb-max-frames t
2174 help-echo
2175 "mouse-2, RET: customize gdb-max-frames to see more frames")))))
2176 (when gdb-look-up-stack
2177 (goto-char (point-min))
2178 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2179 (let ((start (line-beginning-position))
2180 (file (match-string 1))
2181 (line (match-string 2)))
2182 (re-search-backward "^#*\\([0-9]+\\)" start t)
2183 (gdb-enqueue-input
2184 (list (concat gdb-server-prefix "frame "
2185 (match-string 1) "\n") 'gdb-set-hollow))
2186 (gdb-enqueue-input
2187 (list (concat gdb-server-prefix "frame 0\n") 'ignore))))))
2188 (when move-to
2189 (let ((window (get-buffer-window (current-buffer) 0)))
2190 (when window
2191 (with-selected-window window
2192 (goto-char move-to)
2193 (unless (pos-visible-in-window-p)
2194 (recenter '(center)))))))))
2195 (if (eq gdb-look-up-stack 'delete)
2196 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2197 (setq gdb-look-up-stack nil))
2198
2199 (defun gdb-set-hollow ()
2200 (if gud-last-last-frame
2201 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2202 (setq fringe-indicator-alist
2203 '((overlay-arrow . hollow-right-triangle))))))
2204
2205 (defun gdb-stack-buffer-name ()
2206 (with-current-buffer gud-comint-buffer
2207 (concat "*stack frames of " (gdb-get-target-string) "*")))
2208
2209 (defun gdb-display-stack-buffer ()
2210 "Display backtrace of current stack."
2211 (interactive)
2212 (gdb-display-buffer
2213 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2214
2215 (defun gdb-frame-stack-buffer ()
2216 "Display backtrace of current stack in a new frame."
2217 (interactive)
2218 (let ((special-display-regexps (append special-display-regexps '(".*")))
2219 (special-display-frame-alist gdb-frame-parameters))
2220 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2221
2222 (defvar gdb-frames-mode-map
2223 (let ((map (make-sparse-keymap)))
2224 (suppress-keymap map)
2225 (define-key map "q" 'kill-this-buffer)
2226 (define-key map "\r" 'gdb-frames-select)
2227 (define-key map [mouse-2] 'gdb-frames-select)
2228 (define-key map [follow-link] 'mouse-face)
2229 map))
2230
2231 (defun gdb-frames-mode ()
2232 "Major mode for gdb call stack.
2233
2234 \\{gdb-frames-mode-map}"
2235 (kill-all-local-variables)
2236 (setq major-mode 'gdb-frames-mode)
2237 (setq mode-name "Frames")
2238 (setq gdb-stack-position nil)
2239 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2240 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
2241 (setq buffer-read-only t)
2242 (use-local-map gdb-frames-mode-map)
2243 (run-mode-hooks 'gdb-frames-mode-hook)
2244 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2245 'gdb-invalidate-frames
2246 'gdbmi-invalidate-frames))
2247
2248 (defun gdb-get-frame-number ()
2249 (save-excursion
2250 (end-of-line)
2251 (let* ((start (line-beginning-position))
2252 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2253 (n (or (and pos (match-string 1)) "0")))
2254 n)))
2255
2256 (defun gdb-frames-select (&optional event)
2257 "Select the frame and display the relevant source."
2258 (interactive (list last-input-event))
2259 (if event (posn-set-point (event-end event)))
2260 (if (get-text-property (point) 'gdb-max-frames)
2261 (progn
2262 (message-box "After setting gdb-max-frames, you need to enter\n\
2263 another GDB command e.g pwd, to see new frames")
2264 (customize-variable-other-window 'gdb-max-frames))
2265 (gdb-enqueue-input
2266 (list (concat gdb-server-prefix "frame "
2267 (gdb-get-frame-number) "\n") 'ignore))))
2268 \f
2269
2270 ;; Threads buffer. This displays a selectable thread list.
2271 ;;
2272 (gdb-set-buffer-rules 'gdb-threads-buffer
2273 'gdb-threads-buffer-name
2274 'gdb-threads-mode)
2275
2276 (def-gdb-auto-updated-buffer gdb-threads-buffer
2277 gdb-invalidate-threads
2278 (concat gdb-server-prefix "info threads\n")
2279 gdb-info-threads-handler
2280 gdb-info-threads-custom)
2281
2282 (defun gdb-info-threads-custom ()
2283 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2284 (let ((buffer-read-only nil))
2285 (save-excursion
2286 (goto-char (point-min))
2287 (while (< (point) (point-max))
2288 (unless (looking-at "No ")
2289 (add-text-properties (line-beginning-position) (line-end-position)
2290 '(mouse-face highlight
2291 help-echo "mouse-2, RET: select thread")))
2292 (forward-line 1))))))
2293
2294 (defun gdb-threads-buffer-name ()
2295 (with-current-buffer gud-comint-buffer
2296 (concat "*threads of " (gdb-get-target-string) "*")))
2297
2298 (defun gdb-display-threads-buffer ()
2299 "Display IDs of currently known threads."
2300 (interactive)
2301 (gdb-display-buffer
2302 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2303
2304 (defun gdb-frame-threads-buffer ()
2305 "Display IDs of currently known threads in a new frame."
2306 (interactive)
2307 (let ((special-display-regexps (append special-display-regexps '(".*")))
2308 (special-display-frame-alist gdb-frame-parameters))
2309 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2310
2311 (defvar gdb-threads-mode-map
2312 (let ((map (make-sparse-keymap)))
2313 (suppress-keymap map)
2314 (define-key map "q" 'kill-this-buffer)
2315 (define-key map "\r" 'gdb-threads-select)
2316 (define-key map [mouse-2] 'gdb-threads-select)
2317 (define-key map [follow-link] 'mouse-face)
2318 map))
2319
2320 (defvar gdb-threads-font-lock-keywords
2321 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2322 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2323 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2324 "Font lock keywords used in `gdb-threads-mode'.")
2325
2326 (defun gdb-threads-mode ()
2327 "Major mode for gdb threads.
2328
2329 \\{gdb-threads-mode-map}"
2330 (kill-all-local-variables)
2331 (setq major-mode 'gdb-threads-mode)
2332 (setq mode-name "Threads")
2333 (setq buffer-read-only t)
2334 (use-local-map gdb-threads-mode-map)
2335 (set (make-local-variable 'font-lock-defaults)
2336 '(gdb-threads-font-lock-keywords))
2337 (run-mode-hooks 'gdb-threads-mode-hook)
2338 'gdb-invalidate-threads)
2339
2340 (defun gdb-get-thread-number ()
2341 (save-excursion
2342 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2343 (match-string-no-properties 1)))
2344
2345 (defun gdb-threads-select (&optional event)
2346 "Select the thread and display the relevant source."
2347 (interactive (list last-input-event))
2348 (if event (posn-set-point (event-end event)))
2349 (gdb-enqueue-input
2350 (list (concat gdb-server-prefix "thread "
2351 (gdb-get-thread-number) "\n") 'ignore))
2352 (gud-display-frame))
2353 \f
2354
2355 ;; Registers buffer.
2356 ;;
2357 (defcustom gdb-all-registers nil
2358 "Non-nil means include floating-point registers."
2359 :type 'boolean
2360 :group 'gud
2361 :version "22.1")
2362
2363 (gdb-set-buffer-rules 'gdb-registers-buffer
2364 'gdb-registers-buffer-name
2365 'gdb-registers-mode)
2366
2367 (def-gdb-auto-updated-buffer gdb-registers-buffer
2368 gdb-invalidate-registers
2369 (concat
2370 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2371 gdb-info-registers-handler
2372 gdb-info-registers-custom)
2373
2374 (defun gdb-info-registers-custom ()
2375 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2376 (save-excursion
2377 (let ((buffer-read-only nil)
2378 start end)
2379 (goto-char (point-min))
2380 (while (< (point) (point-max))
2381 (setq start (line-beginning-position))
2382 (setq end (line-end-position))
2383 (when (looking-at "^[^ ]+")
2384 (unless (string-equal (match-string 0) "The")
2385 (put-text-property start (match-end 0)
2386 'face font-lock-variable-name-face)
2387 (add-text-properties start end
2388 '(help-echo "mouse-2: edit value"
2389 mouse-face highlight))))
2390 (forward-line 1))))))
2391
2392 (defun gdb-edit-register-value (&optional event)
2393 (interactive (list last-input-event))
2394 (save-excursion
2395 (if event (posn-set-point (event-end event)))
2396 (beginning-of-line)
2397 (let* ((register (current-word))
2398 (value (read-string (format "New value (%s): " register))))
2399 (gdb-enqueue-input
2400 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2401 'ignore)))))
2402
2403 (defvar gdb-registers-mode-map
2404 (let ((map (make-sparse-keymap)))
2405 (suppress-keymap map)
2406 (define-key map "\r" 'gdb-edit-register-value)
2407 (define-key map [mouse-2] 'gdb-edit-register-value)
2408 (define-key map " " 'gdb-all-registers)
2409 (define-key map "q" 'kill-this-buffer)
2410 map))
2411
2412 (defun gdb-registers-mode ()
2413 "Major mode for gdb registers.
2414
2415 \\{gdb-registers-mode-map}"
2416 (kill-all-local-variables)
2417 (setq major-mode 'gdb-registers-mode)
2418 (setq mode-name "Registers")
2419 (setq buffer-read-only t)
2420 (use-local-map gdb-registers-mode-map)
2421 (run-mode-hooks 'gdb-registers-mode-hook)
2422 (if (string-equal gdb-version "pre-6.4")
2423 (progn
2424 (if gdb-all-registers (setq mode-name "Registers:All"))
2425 'gdb-invalidate-registers)
2426 'gdb-invalidate-registers-1))
2427
2428 (defun gdb-registers-buffer-name ()
2429 (with-current-buffer gud-comint-buffer
2430 (concat "*registers of " (gdb-get-target-string) "*")))
2431
2432 (defun gdb-display-registers-buffer ()
2433 "Display integer register contents."
2434 (interactive)
2435 (gdb-display-buffer
2436 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2437
2438 (defun gdb-frame-registers-buffer ()
2439 "Display integer register contents in a new frame."
2440 (interactive)
2441 (let ((special-display-regexps (append special-display-regexps '(".*")))
2442 (special-display-frame-alist gdb-frame-parameters))
2443 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2444
2445 (defun gdb-all-registers ()
2446 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2447 (interactive)
2448 (when (string-equal gdb-version "pre-6.4")
2449 (if gdb-all-registers
2450 (progn
2451 (setq gdb-all-registers nil)
2452 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2453 (setq mode-name "Registers")))
2454 (setq gdb-all-registers t)
2455 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2456 (setq mode-name "Registers:All")))
2457 (message (format "Display of floating-point registers %sabled"
2458 (if gdb-all-registers "en" "dis")))
2459 (gdb-invalidate-registers)))
2460 \f
2461
2462 ;; Memory buffer.
2463 ;;
2464 (defcustom gdb-memory-repeat-count 32
2465 "Number of data items in memory window."
2466 :type 'integer
2467 :group 'gud
2468 :version "22.1")
2469
2470 (defcustom gdb-memory-format "x"
2471 "Display format of data items in memory window."
2472 :type '(choice (const :tag "Hexadecimal" "x")
2473 (const :tag "Signed decimal" "d")
2474 (const :tag "Unsigned decimal" "u")
2475 (const :tag "Octal" "o")
2476 (const :tag "Binary" "t"))
2477 :group 'gud
2478 :version "22.1")
2479
2480 (defcustom gdb-memory-unit "w"
2481 "Unit size of data items in memory window."
2482 :type '(choice (const :tag "Byte" "b")
2483 (const :tag "Halfword" "h")
2484 (const :tag "Word" "w")
2485 (const :tag "Giant word" "g"))
2486 :group 'gud
2487 :version "22.1")
2488
2489 (gdb-set-buffer-rules 'gdb-memory-buffer
2490 'gdb-memory-buffer-name
2491 'gdb-memory-mode)
2492
2493 (def-gdb-auto-updated-buffer gdb-memory-buffer
2494 gdb-invalidate-memory
2495 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2496 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2497 gdb-read-memory-handler
2498 gdb-read-memory-custom)
2499
2500 (defun gdb-read-memory-custom ()
2501 (save-excursion
2502 (goto-char (point-min))
2503 (if (looking-at "0x[[:xdigit:]]+")
2504 (setq gdb-memory-address (match-string 0)))))
2505
2506 (defvar gdb-memory-mode-map
2507 (let ((map (make-sparse-keymap)))
2508 (suppress-keymap map)
2509 (define-key map "q" 'kill-this-buffer)
2510 map))
2511
2512 (defun gdb-memory-set-address (event)
2513 "Set the start memory address."
2514 (interactive "e")
2515 (save-selected-window
2516 (select-window (posn-window (event-start event)))
2517 (let ((arg (read-from-minibuffer "Memory address: ")))
2518 (setq gdb-memory-address arg))
2519 (gdb-invalidate-memory)))
2520
2521 (defun gdb-memory-set-repeat-count (event)
2522 "Set the number of data items in memory window."
2523 (interactive "e")
2524 (save-selected-window
2525 (select-window (posn-window (event-start event)))
2526 (let* ((arg (read-from-minibuffer "Repeat count: "))
2527 (count (string-to-number arg)))
2528 (if (<= count 0)
2529 (error "Positive numbers only")
2530 (customize-set-variable 'gdb-memory-repeat-count count)
2531 (gdb-invalidate-memory)))))
2532
2533 (defun gdb-memory-format-binary ()
2534 "Set the display format to binary."
2535 (interactive)
2536 (customize-set-variable 'gdb-memory-format "t")
2537 (gdb-invalidate-memory))
2538
2539 (defun gdb-memory-format-octal ()
2540 "Set the display format to octal."
2541 (interactive)
2542 (customize-set-variable 'gdb-memory-format "o")
2543 (gdb-invalidate-memory))
2544
2545 (defun gdb-memory-format-unsigned ()
2546 "Set the display format to unsigned decimal."
2547 (interactive)
2548 (customize-set-variable 'gdb-memory-format "u")
2549 (gdb-invalidate-memory))
2550
2551 (defun gdb-memory-format-signed ()
2552 "Set the display format to decimal."
2553 (interactive)
2554 (customize-set-variable 'gdb-memory-format "d")
2555 (gdb-invalidate-memory))
2556
2557 (defun gdb-memory-format-hexadecimal ()
2558 "Set the display format to hexadecimal."
2559 (interactive)
2560 (customize-set-variable 'gdb-memory-format "x")
2561 (gdb-invalidate-memory))
2562
2563 (defvar gdb-memory-format-map
2564 (let ((map (make-sparse-keymap)))
2565 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2566 map)
2567 "Keymap to select format in the header line.")
2568
2569 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2570 "Menu of display formats in the header line.")
2571
2572 (define-key gdb-memory-format-menu [binary]
2573 '(menu-item "Binary" gdb-memory-format-binary
2574 :button (:radio . (equal gdb-memory-format "t"))))
2575 (define-key gdb-memory-format-menu [octal]
2576 '(menu-item "Octal" gdb-memory-format-octal
2577 :button (:radio . (equal gdb-memory-format "o"))))
2578 (define-key gdb-memory-format-menu [unsigned]
2579 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2580 :button (:radio . (equal gdb-memory-format "u"))))
2581 (define-key gdb-memory-format-menu [signed]
2582 '(menu-item "Signed Decimal" gdb-memory-format-signed
2583 :button (:radio . (equal gdb-memory-format "d"))))
2584 (define-key gdb-memory-format-menu [hexadecimal]
2585 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2586 :button (:radio . (equal gdb-memory-format "x"))))
2587
2588 (defun gdb-memory-format-menu (event)
2589 (interactive "@e")
2590 (x-popup-menu event gdb-memory-format-menu))
2591
2592 (defun gdb-memory-format-menu-1 (event)
2593 (interactive "e")
2594 (save-selected-window
2595 (select-window (posn-window (event-start event)))
2596 (let* ((selection (gdb-memory-format-menu event))
2597 (binding (and selection (lookup-key gdb-memory-format-menu
2598 (vector (car selection))))))
2599 (if binding (call-interactively binding)))))
2600
2601 (defun gdb-memory-unit-giant ()
2602 "Set the unit size to giant words (eight bytes)."
2603 (interactive)
2604 (customize-set-variable 'gdb-memory-unit "g")
2605 (gdb-invalidate-memory))
2606
2607 (defun gdb-memory-unit-word ()
2608 "Set the unit size to words (four bytes)."
2609 (interactive)
2610 (customize-set-variable 'gdb-memory-unit "w")
2611 (gdb-invalidate-memory))
2612
2613 (defun gdb-memory-unit-halfword ()
2614 "Set the unit size to halfwords (two bytes)."
2615 (interactive)
2616 (customize-set-variable 'gdb-memory-unit "h")
2617 (gdb-invalidate-memory))
2618
2619 (defun gdb-memory-unit-byte ()
2620 "Set the unit size to bytes."
2621 (interactive)
2622 (customize-set-variable 'gdb-memory-unit "b")
2623 (gdb-invalidate-memory))
2624
2625 (defvar gdb-memory-unit-map
2626 (let ((map (make-sparse-keymap)))
2627 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2628 map)
2629 "Keymap to select units in the header line.")
2630
2631 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2632 "Menu of units in the header line.")
2633
2634 (define-key gdb-memory-unit-menu [giantwords]
2635 '(menu-item "Giant words" gdb-memory-unit-giant
2636 :button (:radio . (equal gdb-memory-unit "g"))))
2637 (define-key gdb-memory-unit-menu [words]
2638 '(menu-item "Words" gdb-memory-unit-word
2639 :button (:radio . (equal gdb-memory-unit "w"))))
2640 (define-key gdb-memory-unit-menu [halfwords]
2641 '(menu-item "Halfwords" gdb-memory-unit-halfword
2642 :button (:radio . (equal gdb-memory-unit "h"))))
2643 (define-key gdb-memory-unit-menu [bytes]
2644 '(menu-item "Bytes" gdb-memory-unit-byte
2645 :button (:radio . (equal gdb-memory-unit "b"))))
2646
2647 (defun gdb-memory-unit-menu (event)
2648 (interactive "@e")
2649 (x-popup-menu event gdb-memory-unit-menu))
2650
2651 (defun gdb-memory-unit-menu-1 (event)
2652 (interactive "e")
2653 (save-selected-window
2654 (select-window (posn-window (event-start event)))
2655 (let* ((selection (gdb-memory-unit-menu event))
2656 (binding (and selection (lookup-key gdb-memory-unit-menu
2657 (vector (car selection))))))
2658 (if binding (call-interactively binding)))))
2659
2660 ;;from make-mode-line-mouse-map
2661 (defun gdb-make-header-line-mouse-map (mouse function) "\
2662 Return a keymap with single entry for mouse key MOUSE on the header line.
2663 MOUSE is defined to run function FUNCTION with no args in the buffer
2664 corresponding to the mode line clicked."
2665 (let ((map (make-sparse-keymap)))
2666 (define-key map (vector 'header-line mouse) function)
2667 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2668 map))
2669
2670 (defvar gdb-memory-font-lock-keywords
2671 '(;; <__function.name+n>
2672 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2673 )
2674 "Font lock keywords used in `gdb-memory-mode'.")
2675
2676 (defun gdb-memory-mode ()
2677 "Major mode for examining memory.
2678
2679 \\{gdb-memory-mode-map}"
2680 (kill-all-local-variables)
2681 (setq major-mode 'gdb-memory-mode)
2682 (setq mode-name "Memory")
2683 (setq buffer-read-only t)
2684 (use-local-map gdb-memory-mode-map)
2685 (setq header-line-format
2686 '(:eval
2687 (concat
2688 "Read address["
2689 (propertize
2690 "-"
2691 'face font-lock-warning-face
2692 'help-echo "mouse-1: decrement address"
2693 'mouse-face 'mode-line-highlight
2694 'local-map
2695 (gdb-make-header-line-mouse-map
2696 'mouse-1
2697 (lambda () (interactive)
2698 (let ((gdb-memory-address
2699 ;; Let GDB do the arithmetic.
2700 (concat
2701 gdb-memory-address " - "
2702 (number-to-string
2703 (* gdb-memory-repeat-count
2704 (cond ((string= gdb-memory-unit "b") 1)
2705 ((string= gdb-memory-unit "h") 2)
2706 ((string= gdb-memory-unit "w") 4)
2707 ((string= gdb-memory-unit "g") 8)))))))
2708 (gdb-invalidate-memory)))))
2709 "|"
2710 (propertize "+"
2711 'face font-lock-warning-face
2712 'help-echo "mouse-1: increment address"
2713 'mouse-face 'mode-line-highlight
2714 'local-map (gdb-make-header-line-mouse-map
2715 'mouse-1
2716 (lambda () (interactive)
2717 (let ((gdb-memory-address nil))
2718 (gdb-invalidate-memory)))))
2719 "]: "
2720 (propertize gdb-memory-address
2721 'face font-lock-warning-face
2722 'help-echo "mouse-1: set memory address"
2723 'mouse-face 'mode-line-highlight
2724 'local-map (gdb-make-header-line-mouse-map
2725 'mouse-1
2726 #'gdb-memory-set-address))
2727 " Repeat Count: "
2728 (propertize (number-to-string gdb-memory-repeat-count)
2729 'face font-lock-warning-face
2730 'help-echo "mouse-1: set repeat count"
2731 'mouse-face 'mode-line-highlight
2732 'local-map (gdb-make-header-line-mouse-map
2733 'mouse-1
2734 #'gdb-memory-set-repeat-count))
2735 " Display Format: "
2736 (propertize gdb-memory-format
2737 'face font-lock-warning-face
2738 'help-echo "mouse-3: select display format"
2739 'mouse-face 'mode-line-highlight
2740 'local-map gdb-memory-format-map)
2741 " Unit Size: "
2742 (propertize gdb-memory-unit
2743 'face font-lock-warning-face
2744 'help-echo "mouse-3: select unit size"
2745 'mouse-face 'mode-line-highlight
2746 'local-map gdb-memory-unit-map))))
2747 (set (make-local-variable 'font-lock-defaults)
2748 '(gdb-memory-font-lock-keywords))
2749 (run-mode-hooks 'gdb-memory-mode-hook)
2750 'gdb-invalidate-memory)
2751
2752 (defun gdb-memory-buffer-name ()
2753 (with-current-buffer gud-comint-buffer
2754 (concat "*memory of " (gdb-get-target-string) "*")))
2755
2756 (defun gdb-display-memory-buffer ()
2757 "Display memory contents."
2758 (interactive)
2759 (gdb-display-buffer
2760 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2761
2762 (defun gdb-frame-memory-buffer ()
2763 "Display memory contents in a new frame."
2764 (interactive)
2765 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2766 (special-display-frame-alist
2767 (cons '(left-fringe . 0)
2768 (cons '(right-fringe . 0)
2769 (cons '(width . 83) gdb-frame-parameters)))))
2770 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2771 \f
2772
2773 ;; Locals buffer.
2774 ;;
2775 (gdb-set-buffer-rules 'gdb-locals-buffer
2776 'gdb-locals-buffer-name
2777 'gdb-locals-mode)
2778
2779 (def-gdb-auto-update-trigger gdb-invalidate-locals
2780 (gdb-get-buffer 'gdb-locals-buffer)
2781 "server info locals\n"
2782 gdb-info-locals-handler)
2783
2784 (defvar gdb-locals-watch-map
2785 (let ((map (make-sparse-keymap)))
2786 (suppress-keymap map)
2787 (define-key map "\r" (lambda () (interactive)
2788 (beginning-of-line)
2789 (gud-watch)))
2790 (define-key map [mouse-2] (lambda (event) (interactive "e")
2791 (mouse-set-point event)
2792 (beginning-of-line)
2793 (gud-watch)))
2794 map)
2795 "Keymap to create watch expression of a complex data type local variable.")
2796
2797 (defconst gdb-struct-string
2798 (concat (propertize "[struct/union]"
2799 'mouse-face 'highlight
2800 'help-echo "mouse-2: create watch expression"
2801 'local-map gdb-locals-watch-map) "\n"))
2802
2803 (defconst gdb-array-string
2804 (concat " " (propertize "[array]"
2805 'mouse-face 'highlight
2806 'help-echo "mouse-2: create watch expression"
2807 'local-map gdb-locals-watch-map) "\n"))
2808
2809 ;; Abbreviate for arrays and structures.
2810 ;; These can be expanded using gud-display.
2811 (defun gdb-info-locals-handler ()
2812 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2813 gdb-pending-triggers))
2814 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2815 (with-current-buffer buf
2816 (goto-char (point-min))
2817 (while (re-search-forward "^[ }].*\n" nil t)
2818 (replace-match "" nil nil))
2819 (goto-char (point-min))
2820 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2821 (replace-match gdb-struct-string nil nil))
2822 (goto-char (point-min))
2823 (while (re-search-forward "\\s-*{.*\n" nil t)
2824 (replace-match gdb-array-string nil nil))))
2825 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2826 (and buf
2827 (with-current-buffer buf
2828 (let* ((window (get-buffer-window buf 0))
2829 (start (window-start window))
2830 (p (window-point window))
2831 (buffer-read-only nil))
2832 (erase-buffer)
2833 (insert-buffer-substring (gdb-get-buffer-create
2834 'gdb-partial-output-buffer))
2835 (set-window-start window start)
2836 (set-window-point window p))
2837 )))
2838 (run-hooks 'gdb-info-locals-hook))
2839
2840 (defvar gdb-locals-mode-map
2841 (let ((map (make-sparse-keymap)))
2842 (suppress-keymap map)
2843 (define-key map "q" 'kill-this-buffer)
2844 map))
2845
2846 (defun gdb-locals-mode ()
2847 "Major mode for gdb locals.
2848
2849 \\{gdb-locals-mode-map}"
2850 (kill-all-local-variables)
2851 (setq major-mode 'gdb-locals-mode)
2852 (setq mode-name (concat "Locals:" gdb-selected-frame))
2853 (setq buffer-read-only t)
2854 (use-local-map gdb-locals-mode-map)
2855 (set (make-local-variable 'font-lock-defaults)
2856 '(gdb-locals-font-lock-keywords))
2857 (run-mode-hooks 'gdb-locals-mode-hook)
2858 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2859 (string-equal gdb-version "pre-6.4"))
2860 'gdb-invalidate-locals
2861 'gdb-invalidate-locals-1))
2862
2863 (defun gdb-locals-buffer-name ()
2864 (with-current-buffer gud-comint-buffer
2865 (concat "*locals of " (gdb-get-target-string) "*")))
2866
2867 (defun gdb-display-locals-buffer ()
2868 "Display local variables of current stack and their values."
2869 (interactive)
2870 (gdb-display-buffer
2871 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2872
2873 (defun gdb-frame-locals-buffer ()
2874 "Display local variables of current stack and their values in a new frame."
2875 (interactive)
2876 (let ((special-display-regexps (append special-display-regexps '(".*")))
2877 (special-display-frame-alist gdb-frame-parameters))
2878 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2879 \f
2880
2881 ;;;; Window management
2882 (defun gdb-display-buffer (buf dedicated &optional size)
2883 (let ((answer (get-buffer-window buf 0))
2884 (must-split nil))
2885 (if answer
2886 (display-buffer buf nil 0) ;Deiconify the frame if necessary.
2887 ;; The buffer is not yet displayed.
2888 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2889 (let ((window (get-lru-window)))
2890 (if (and window
2891 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2892 ,gdb-source-window))))
2893 (progn
2894 (set-window-buffer window buf)
2895 (setq answer window))
2896 (setq must-split t)))
2897 (if must-split
2898 (let* ((largest (get-largest-window))
2899 (cur-size (window-height largest))
2900 (new-size (and size (< size cur-size) (- cur-size size))))
2901 (setq answer (split-window largest new-size))
2902 (set-window-buffer answer buf)
2903 (set-window-dedicated-p answer dedicated)))
2904 answer)))
2905
2906 \f
2907 ;;; Shared keymap initialization:
2908
2909 (let ((menu (make-sparse-keymap "GDB-Windows")))
2910 (define-key gud-menu-map [displays]
2911 `(menu-item "GDB-Windows" ,menu
2912 :visible (memq gud-minor-mode '(gdbmi gdba))))
2913 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2914 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2915 (define-key menu [inferior]
2916 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2917 :enable gdb-use-separate-io-buffer))
2918 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2919 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2920 (define-key menu [disassembly]
2921 '("Disassembly" . gdb-display-assembler-buffer))
2922 (define-key menu [breakpoints]
2923 '("Breakpoints" . gdb-display-breakpoints-buffer))
2924 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2925 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2926
2927 (let ((menu (make-sparse-keymap "GDB-Frames")))
2928 (define-key gud-menu-map [frames]
2929 `(menu-item "GDB-Frames" ,menu
2930 :visible (memq gud-minor-mode '(gdbmi gdba))))
2931 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2932 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2933 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2934 (define-key menu [inferior]
2935 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2936 :enable gdb-use-separate-io-buffer))
2937 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2938 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2939 (define-key menu [breakpoints]
2940 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2941 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2942 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2943
2944 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2945 (define-key gud-menu-map [ui]
2946 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2947 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2948 (define-key menu [gdb-find-source-frame]
2949 '(menu-item "Look For Source Frame" gdb-find-source-frame
2950 :visible (eq gud-minor-mode 'gdba)
2951 :help "Toggle look for source frame."
2952 :button (:toggle . gdb-find-source-frame)))
2953 (define-key menu [gdb-use-separate-io]
2954 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2955 :visible (eq gud-minor-mode 'gdba)
2956 :help "Toggle separate IO for debugged program."
2957 :button (:toggle . gdb-use-separate-io-buffer)))
2958 (define-key menu [gdb-many-windows]
2959 '(menu-item "Display Other Windows" gdb-many-windows
2960 :help "Toggle display of locals, stack and breakpoint information"
2961 :button (:toggle . gdb-many-windows)))
2962 (define-key menu [gdb-restore-windows]
2963 '(menu-item "Restore Window Layout" gdb-restore-windows
2964 :help "Restore standard layout for debug session.")))
2965
2966 (defun gdb-frame-gdb-buffer ()
2967 "Display GUD buffer in a new frame."
2968 (interactive)
2969 (let ((special-display-regexps (append special-display-regexps '(".*")))
2970 (special-display-frame-alist
2971 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2972 gdb-frame-parameters)))
2973 (same-window-regexps nil))
2974 (display-buffer gud-comint-buffer)))
2975
2976 (defun gdb-display-gdb-buffer ()
2977 "Display GUD buffer."
2978 (interactive)
2979 (let ((same-window-regexps nil))
2980 (pop-to-buffer gud-comint-buffer)))
2981
2982 (defun gdb-set-window-buffer (name)
2983 (set-window-buffer (selected-window) (get-buffer name))
2984 (set-window-dedicated-p (selected-window) t))
2985
2986 (defun gdb-setup-windows ()
2987 "Layout the window pattern for `gdb-many-windows'."
2988 (gdb-display-locals-buffer)
2989 (gdb-display-stack-buffer)
2990 (delete-other-windows)
2991 (gdb-display-breakpoints-buffer)
2992 (delete-other-windows)
2993 ; Don't dedicate.
2994 (pop-to-buffer gud-comint-buffer)
2995 (split-window nil ( / ( * (window-height) 3) 4))
2996 (split-window nil ( / (window-height) 3))
2997 (split-window-horizontally)
2998 (other-window 1)
2999 (gdb-set-window-buffer (gdb-locals-buffer-name))
3000 (other-window 1)
3001 (switch-to-buffer
3002 (if gud-last-last-frame
3003 (gud-find-file (car gud-last-last-frame))
3004 (if gdb-main-file
3005 (gud-find-file gdb-main-file)
3006 ;; Put buffer list in window if we
3007 ;; can't find a source file.
3008 (list-buffers-noselect))))
3009 (setq gdb-source-window (selected-window))
3010 (when gdb-use-separate-io-buffer
3011 (split-window-horizontally)
3012 (other-window 1)
3013 (gdb-set-window-buffer
3014 (gdb-get-buffer-create 'gdb-inferior-io)))
3015 (other-window 1)
3016 (gdb-set-window-buffer (gdb-stack-buffer-name))
3017 (split-window-horizontally)
3018 (other-window 1)
3019 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
3020 (other-window 1))
3021
3022 (defun gdb-restore-windows ()
3023 "Restore the basic arrangement of windows used by gdba.
3024 This arrangement depends on the value of `gdb-many-windows'."
3025 (interactive)
3026 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
3027 (delete-other-windows)
3028 (if gdb-many-windows
3029 (gdb-setup-windows)
3030 (when (or gud-last-last-frame gdb-show-main)
3031 (split-window)
3032 (other-window 1)
3033 (switch-to-buffer
3034 (if gud-last-last-frame
3035 (gud-find-file (car gud-last-last-frame))
3036 (gud-find-file gdb-main-file)))
3037 (setq gdb-source-window (selected-window))
3038 (other-window 1))))
3039
3040 (defun gdb-reset ()
3041 "Exit a debugging session cleanly.
3042 Kills the gdb buffers, and resets variables and the source buffers."
3043 (dolist (buffer (buffer-list))
3044 (unless (eq buffer gud-comint-buffer)
3045 (with-current-buffer buffer
3046 (if (memq gud-minor-mode '(gdbmi gdba))
3047 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
3048 (kill-buffer nil)
3049 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
3050 (setq gud-minor-mode nil)
3051 (kill-local-variable 'tool-bar-map)
3052 (kill-local-variable 'gdb-define-alist))))))
3053 (setq gdb-overlay-arrow-position nil)
3054 (setq overlay-arrow-variable-list
3055 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
3056 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
3057 (setq gdb-stack-position nil)
3058 (setq overlay-arrow-variable-list
3059 (delq 'gdb-stack-position overlay-arrow-variable-list))
3060 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
3061 (setq gud-running nil)
3062 (setq gdb-active-process nil)
3063 (setq gdb-var-list nil)
3064 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
3065
3066 (defun gdb-source-info ()
3067 "Find the source file where the program starts and displays it with related
3068 buffers."
3069 (goto-char (point-min))
3070 (if (and (search-forward "Located in " nil t)
3071 (looking-at "\\S-+"))
3072 (setq gdb-main-file (match-string 0)))
3073 (goto-char (point-min))
3074 (if (search-forward "Includes preprocessor macro info." nil t)
3075 (setq gdb-macro-info t))
3076 (if gdb-many-windows
3077 (gdb-setup-windows)
3078 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
3079 (if gdb-show-main
3080 (let ((pop-up-windows t))
3081 (display-buffer (gud-find-file gdb-main-file)))))
3082 (setq gdb-ready t))
3083
3084 (defun gdb-get-location (bptno line flag)
3085 "Find the directory containing the relevant source file.
3086 Put in buffer and place breakpoint icon."
3087 (goto-char (point-min))
3088 (catch 'file-not-found
3089 (if (search-forward "Located in " nil t)
3090 (when (looking-at "\\S-+")
3091 (delete (cons bptno "File not found") gdb-location-alist)
3092 (push (cons bptno (match-string 0)) gdb-location-alist))
3093 (gdb-resync)
3094 (unless (assoc bptno gdb-location-alist)
3095 (push (cons bptno "File not found") gdb-location-alist)
3096 (message-box "Cannot find source file for breakpoint location.\n\
3097 Add directory to search path for source files using the GDB command, dir."))
3098 (throw 'file-not-found nil))
3099 (with-current-buffer
3100 (find-file-noselect (match-string 0))
3101 (save-current-buffer
3102 (set (make-local-variable 'gud-minor-mode) 'gdba)
3103 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
3104 ;; only want one breakpoint icon at each location
3105 (save-excursion
3106 (goto-line (string-to-number line))
3107 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
3108
3109 (add-hook 'find-file-hook 'gdb-find-file-hook)
3110
3111 (defun gdb-find-file-hook ()
3112 "Set up buffer for debugging if file is part of the source code
3113 of the current session."
3114 (if (and (buffer-name gud-comint-buffer)
3115 ;; in case gud or gdb-ui is just loaded
3116 gud-comint-buffer
3117 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3118 '(gdba gdbmi)))
3119 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
3120 (if (member (if (string-equal gdb-version "pre-6.4")
3121 (file-name-nondirectory buffer-file-name)
3122 buffer-file-name)
3123 gdb-source-file-list)
3124 (with-current-buffer (find-buffer-visiting buffer-file-name)
3125 (set (make-local-variable 'gud-minor-mode)
3126 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3127 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
3128
3129 ;;from put-image
3130 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3131 "Put string PUTSTRING in front of POS in the current buffer.
3132 PUTSTRING is displayed by putting an overlay into the current buffer with a
3133 `before-string' string that has a `display' property whose value is
3134 PUTSTRING."
3135 (let ((string (make-string 1 ?x))
3136 (buffer (current-buffer)))
3137 (setq putstring (copy-sequence putstring))
3138 (let ((overlay (make-overlay pos pos buffer))
3139 (prop (or dprop
3140 (list (list 'margin 'left-margin) putstring))))
3141 (put-text-property 0 1 'display prop string)
3142 (if sprops
3143 (add-text-properties 0 1 sprops string))
3144 (overlay-put overlay 'put-break t)
3145 (overlay-put overlay 'before-string string))))
3146
3147 ;;from remove-images
3148 (defun gdb-remove-strings (start end &optional buffer)
3149 "Remove strings between START and END in BUFFER.
3150 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3151 BUFFER nil or omitted means use the current buffer."
3152 (unless buffer
3153 (setq buffer (current-buffer)))
3154 (dolist (overlay (overlays-in start end))
3155 (when (overlay-get overlay 'put-break)
3156 (delete-overlay overlay))))
3157
3158 (defun gdb-put-breakpoint-icon (enabled bptno)
3159 (let ((start (- (line-beginning-position) 1))
3160 (end (+ (line-end-position) 1))
3161 (putstring (if enabled "B" "b"))
3162 (source-window (get-buffer-window (current-buffer) 0)))
3163 (add-text-properties
3164 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3165 putstring)
3166 (if enabled
3167 (add-text-properties
3168 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3169 (add-text-properties
3170 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3171 (gdb-remove-breakpoint-icons start end)
3172 (if (display-images-p)
3173 (if (>= (or left-fringe-width
3174 (if source-window (car (window-fringes source-window)))
3175 gdb-buffer-fringe-width) 8)
3176 (gdb-put-string
3177 nil (1+ start)
3178 `(left-fringe breakpoint
3179 ,(if enabled
3180 'breakpoint-enabled
3181 'breakpoint-disabled))
3182 'gdb-bptno bptno
3183 'gdb-enabled enabled)
3184 (when (< left-margin-width 2)
3185 (save-current-buffer
3186 (setq left-margin-width 2)
3187 (if source-window
3188 (set-window-margins
3189 source-window
3190 left-margin-width right-margin-width))))
3191 (put-image
3192 (if enabled
3193 (or breakpoint-enabled-icon
3194 (setq breakpoint-enabled-icon
3195 (find-image `((:type xpm :data
3196 ,breakpoint-xpm-data
3197 :ascent 100 :pointer hand)
3198 (:type pbm :data
3199 ,breakpoint-enabled-pbm-data
3200 :ascent 100 :pointer hand)))))
3201 (or breakpoint-disabled-icon
3202 (setq breakpoint-disabled-icon
3203 (find-image `((:type xpm :data
3204 ,breakpoint-xpm-data
3205 :conversion disabled
3206 :ascent 100 :pointer hand)
3207 (:type pbm :data
3208 ,breakpoint-disabled-pbm-data
3209 :ascent 100 :pointer hand))))))
3210 (+ start 1)
3211 putstring
3212 'left-margin))
3213 (when (< left-margin-width 2)
3214 (save-current-buffer
3215 (setq left-margin-width 2)
3216 (let ((window (get-buffer-window (current-buffer) 0)))
3217 (if window
3218 (set-window-margins
3219 window left-margin-width right-margin-width)))))
3220 (gdb-put-string
3221 (propertize putstring
3222 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3223 (1+ start)))))
3224
3225 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3226 (gdb-remove-strings start end)
3227 (if (display-images-p)
3228 (remove-images start end))
3229 (when remove-margin
3230 (setq left-margin-width 0)
3231 (let ((window (get-buffer-window (current-buffer) 0)))
3232 (if window
3233 (set-window-margins
3234 window left-margin-width right-margin-width)))))
3235
3236 \f
3237 ;;
3238 ;; Assembler buffer.
3239 ;;
3240 (gdb-set-buffer-rules 'gdb-assembler-buffer
3241 'gdb-assembler-buffer-name
3242 'gdb-assembler-mode)
3243
3244 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3245 ;; window-start but keep the overlay arrow/current line visible.
3246 (defun gdb-assembler-handler ()
3247 (setq gdb-pending-triggers
3248 (delq 'gdb-invalidate-assembler
3249 gdb-pending-triggers))
3250 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3251 (and buf
3252 (with-current-buffer buf
3253 (let* ((window (get-buffer-window buf 0))
3254 (p (window-point window))
3255 (buffer-read-only nil))
3256 (erase-buffer)
3257 (insert-buffer-substring (gdb-get-buffer-create
3258 'gdb-partial-output-buffer))
3259 (set-window-point window p)))))
3260 ;; put customisation here
3261 (gdb-assembler-custom))
3262
3263 (defun gdb-assembler-custom ()
3264 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3265 (pos 1) (address) (flag) (bptno))
3266 (with-current-buffer buffer
3267 (save-excursion
3268 (if (not (equal gdb-pc-address "main"))
3269 (progn
3270 (goto-char (point-min))
3271 (if (and gdb-pc-address
3272 (search-forward gdb-pc-address nil t))
3273 (progn
3274 (setq pos (point))
3275 (beginning-of-line)
3276 (setq fringe-indicator-alist
3277 (if (string-equal gdb-frame-number "0")
3278 nil
3279 '((overlay-arrow . hollow-right-triangle))))
3280 (or gdb-overlay-arrow-position
3281 (setq gdb-overlay-arrow-position (make-marker)))
3282 (set-marker gdb-overlay-arrow-position (point))))))
3283 ;; remove all breakpoint-icons in assembler buffer before updating.
3284 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3285 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3286 (goto-char (point-min))
3287 (while (< (point) (- (point-max) 1))
3288 (forward-line 1)
3289 (if (looking-at "[^\t].*?breakpoint")
3290 (progn
3291 (looking-at
3292 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3293 (setq bptno (match-string 1))
3294 (setq flag (char-after (match-beginning 2)))
3295 (setq address (match-string 3))
3296 (with-current-buffer buffer
3297 (save-excursion
3298 (goto-char (point-min))
3299 (if (search-forward address nil t)
3300 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3301 (if (not (equal gdb-pc-address "main"))
3302 (with-current-buffer buffer
3303 (set-window-point (get-buffer-window buffer 0) pos)))))
3304
3305 (defvar gdb-assembler-mode-map
3306 (let ((map (make-sparse-keymap)))
3307 (suppress-keymap map)
3308 (define-key map "q" 'kill-this-buffer)
3309 map))
3310
3311 (defvar gdb-assembler-font-lock-keywords
3312 '(;; <__function.name+n>
3313 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3314 (1 font-lock-function-name-face))
3315 ;; 0xNNNNNNNN <__function.name+n>: opcode
3316 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3317 (4 font-lock-keyword-face))
3318 ;; %register(at least i386)
3319 ("%\\sw+" . font-lock-variable-name-face)
3320 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3321 (1 font-lock-comment-face)
3322 (2 font-lock-function-name-face))
3323 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3324 "Font lock keywords used in `gdb-assembler-mode'.")
3325
3326 (defun gdb-assembler-mode ()
3327 "Major mode for viewing code assembler.
3328
3329 \\{gdb-assembler-mode-map}"
3330 (kill-all-local-variables)
3331 (setq major-mode 'gdb-assembler-mode)
3332 (setq mode-name (concat "Machine:" gdb-selected-frame))
3333 (setq gdb-overlay-arrow-position nil)
3334 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3335 (setq fringes-outside-margins t)
3336 (setq buffer-read-only t)
3337 (use-local-map gdb-assembler-mode-map)
3338 (gdb-invalidate-assembler)
3339 (set (make-local-variable 'font-lock-defaults)
3340 '(gdb-assembler-font-lock-keywords))
3341 (run-mode-hooks 'gdb-assembler-mode-hook)
3342 'gdb-invalidate-assembler)
3343
3344 (defun gdb-assembler-buffer-name ()
3345 (with-current-buffer gud-comint-buffer
3346 (concat "*disassembly of " (gdb-get-target-string) "*")))
3347
3348 (defun gdb-display-assembler-buffer ()
3349 "Display disassembly view."
3350 (interactive)
3351 (setq gdb-previous-frame nil)
3352 (gdb-display-buffer
3353 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3354
3355 (defun gdb-frame-assembler-buffer ()
3356 "Display disassembly view in a new frame."
3357 (interactive)
3358 (setq gdb-previous-frame nil)
3359 (let ((special-display-regexps (append special-display-regexps '(".*")))
3360 (special-display-frame-alist gdb-frame-parameters))
3361 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3362
3363 ;; modified because if gdb-pc-address has changed value a new command
3364 ;; must be enqueued to update the buffer with the new output
3365 (defun gdb-invalidate-assembler (&optional ignored)
3366 (if (gdb-get-buffer 'gdb-assembler-buffer)
3367 (progn
3368 (unless (and gdb-selected-frame
3369 (string-equal gdb-selected-frame gdb-previous-frame))
3370 (if (or (not (member 'gdb-invalidate-assembler
3371 gdb-pending-triggers))
3372 (not (string-equal gdb-pc-address
3373 gdb-previous-frame-address)))
3374 (progn
3375 ;; take previous disassemble command, if any, off the queue
3376 (with-current-buffer gud-comint-buffer
3377 (let ((queue gdb-input-queue))
3378 (dolist (item queue)
3379 (if (equal (cdr item) '(gdb-assembler-handler))
3380 (setq gdb-input-queue
3381 (delete item gdb-input-queue))))))
3382 (gdb-enqueue-input
3383 (list
3384 (concat gdb-server-prefix "disassemble "
3385 (if (member gdb-pc-address '(nil "main")) nil "0x")
3386 gdb-pc-address "\n")
3387 'gdb-assembler-handler))
3388 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3389 (setq gdb-previous-frame-address gdb-pc-address)
3390 (setq gdb-previous-frame gdb-selected-frame)))))))
3391
3392 (defun gdb-get-selected-frame ()
3393 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3394 (progn
3395 (gdb-enqueue-input
3396 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3397 (push 'gdb-get-selected-frame
3398 gdb-pending-triggers))))
3399
3400 (defun gdb-frame-handler ()
3401 (setq gdb-pending-triggers
3402 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3403 (goto-char (point-min))
3404 (when (re-search-forward
3405 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3406 (setq gdb-frame-number (match-string 1))
3407 (setq gdb-frame-address (match-string 2)))
3408 (goto-char (point-min))
3409 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3410 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3411 nil t)
3412 (setq gdb-selected-frame (match-string 2))
3413 (if (gdb-get-buffer 'gdb-locals-buffer)
3414 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3415 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3416 (if (gdb-get-buffer 'gdb-assembler-buffer)
3417 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3418 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3419 (setq gdb-pc-address (match-string 1))
3420 (if (and (match-string 3) gud-overlay-arrow-position)
3421 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3422 (position (marker-position gud-overlay-arrow-position)))
3423 (when (and buffer
3424 (string-equal (buffer-name buffer)
3425 (file-name-nondirectory (match-string 3))))
3426 (with-current-buffer buffer
3427 (setq fringe-indicator-alist
3428 (if (string-equal gdb-frame-number "0")
3429 nil
3430 '((overlay-arrow . hollow-right-triangle))))
3431 (set-marker gud-overlay-arrow-position position))))))
3432 (goto-char (point-min))
3433 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3434 (setq gdb-current-language (match-string 1)))
3435 (gdb-invalidate-assembler))
3436
3437 \f
3438 ;; Code specific to GDB 6.4
3439 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3440
3441 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3442 "Create list of source files for current GDB session.
3443 If buffers already exist for any of these files, gud-minor-mode
3444 is set in them."
3445 (goto-char (point-min))
3446 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3447 (push (match-string 1) gdb-source-file-list))
3448 (dolist (buffer (buffer-list))
3449 (with-current-buffer buffer
3450 (when (member buffer-file-name gdb-source-file-list)
3451 (set (make-local-variable 'gud-minor-mode)
3452 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3453 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3454 (when gud-tooltip-mode
3455 (make-local-variable 'gdb-define-alist)
3456 (gdb-create-define-alist)
3457 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3458 (gdb-force-mode-line-update
3459 (propertize "ready" 'face font-lock-variable-name-face)))
3460
3461 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3462 (defun gdb-var-list-children-1 (varnum)
3463 (gdb-enqueue-input
3464 (list
3465 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3466 (concat "server interpreter mi \"-var-list-children --all-values "
3467 varnum "\"\n")
3468 (concat "-var-list-children --all-values " varnum "\n"))
3469 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3470
3471 (defconst gdb-var-list-children-regexp-1
3472 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3473 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\)\
3474 \\(}\\|,.*?\\(type=\"\\(.+?\\)\"\\)?.*?}\\)")
3475
3476 (defun gdb-var-list-children-handler-1 (varnum)
3477 (goto-char (point-min))
3478 (let ((var-list nil))
3479 (catch 'child-already-watched
3480 (dolist (var gdb-var-list)
3481 (if (string-equal varnum (car var))
3482 (progn
3483 (push var var-list)
3484 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3485 (let ((varchild (list (match-string 1)
3486 (match-string 2)
3487 (match-string 3)
3488 (match-string 7)
3489 (read (match-string 4))
3490 nil)))
3491 (if (assoc (car varchild) gdb-var-list)
3492 (throw 'child-already-watched nil))
3493 (push varchild var-list))))
3494 (push var var-list)))
3495 (setq gdb-var-list (nreverse var-list))))
3496 (gdb-speedbar-update))
3497
3498 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3499 (defun gdb-var-update-1 ()
3500 (if (not (member 'gdb-var-update gdb-pending-triggers))
3501 (progn
3502 (gdb-enqueue-input
3503 (list
3504 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3505 "server interpreter mi \"-var-update --all-values *\"\n"
3506 "-var-update --all-values *\n")
3507 'gdb-var-update-handler-1))
3508 (push 'gdb-var-update gdb-pending-triggers))))
3509
3510 (defconst gdb-var-update-regexp-1
3511 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3512 in_scope=\"\\(.*?\\)\".*?}")
3513
3514 (defun gdb-var-update-handler-1 ()
3515 (dolist (var gdb-var-list)
3516 (setcar (nthcdr 5 var) nil))
3517 (goto-char (point-min))
3518 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3519 (let* ((varnum (match-string 1))
3520 (var (assoc varnum gdb-var-list)))
3521 (when var
3522 (let ((match (match-string 3)))
3523 (cond ((string-equal match "false")
3524 (if gdb-delete-out-of-scope
3525 (gdb-var-delete-1 varnum)
3526 (setcar (nthcdr 5 var) 'out-of-scope)))
3527 ((string-equal match "true")
3528 (setcar (nthcdr 5 var) 'changed)
3529 (setcar (nthcdr 4 var)
3530 (read (match-string 2))))
3531 ((string-equal match "invalid")
3532 (gdb-var-delete-1 varnum)))))))
3533 (setq gdb-pending-triggers
3534 (delq 'gdb-var-update gdb-pending-triggers))
3535 (gdb-speedbar-update))
3536
3537 ;; Registers buffer.
3538 ;;
3539 (gdb-set-buffer-rules 'gdb-registers-buffer
3540 'gdb-registers-buffer-name
3541 'gdb-registers-mode)
3542
3543 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3544 (gdb-get-buffer 'gdb-registers-buffer)
3545 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3546 "server interpreter mi \"-data-list-register-values x\"\n"
3547 "-data-list-register-values x\n")
3548 gdb-data-list-register-values-handler)
3549
3550 (defconst gdb-data-list-register-values-regexp
3551 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3552
3553 (defun gdb-data-list-register-values-handler ()
3554 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3555 gdb-pending-triggers))
3556 (goto-char (point-min))
3557 (if (re-search-forward gdb-error-regexp nil t)
3558 (let ((err (match-string 1)))
3559 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3560 (let ((buffer-read-only nil))
3561 (erase-buffer)
3562 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3563 (insert err)
3564 (goto-char (point-min)))))
3565 (let ((register-list (reverse gdb-register-names))
3566 (register nil) (register-string nil) (register-values nil))
3567 (goto-char (point-min))
3568 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3569 (setq register (pop register-list))
3570 (setq register-string (concat register "\t" (match-string 2) "\n"))
3571 (if (member (match-string 1) gdb-changed-registers)
3572 (put-text-property 0 (length register-string)
3573 'face 'font-lock-warning-face
3574 register-string))
3575 (setq register-values
3576 (concat register-values register-string)))
3577 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3578 (with-current-buffer buf
3579 (let* ((window (get-buffer-window buf 0))
3580 (start (window-start window))
3581 (p (window-point window))
3582 (buffer-read-only nil))
3583 (erase-buffer)
3584 (insert register-values)
3585 (set-window-start window start)
3586 (set-window-point window p))))))
3587 (gdb-data-list-register-values-custom))
3588
3589 (defun gdb-data-list-register-values-custom ()
3590 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3591 (save-excursion
3592 (let ((buffer-read-only nil)
3593 start end)
3594 (goto-char (point-min))
3595 (while (< (point) (point-max))
3596 (setq start (line-beginning-position))
3597 (setq end (line-end-position))
3598 (when (looking-at "^[^\t]+")
3599 (unless (string-equal (match-string 0) "No registers.")
3600 (put-text-property start (match-end 0)
3601 'face font-lock-variable-name-face)
3602 (add-text-properties start end
3603 '(help-echo "mouse-2: edit value"
3604 mouse-face highlight))))
3605 (forward-line 1))))))
3606
3607 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3608 (defun gdb-get-changed-registers ()
3609 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3610 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3611 (progn
3612 (gdb-enqueue-input
3613 (list
3614 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3615 "server interpreter mi -data-list-changed-registers\n"
3616 "-data-list-changed-registers\n")
3617 'gdb-get-changed-registers-handler))
3618 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3619
3620 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3621
3622 (defun gdb-get-changed-registers-handler ()
3623 (setq gdb-pending-triggers
3624 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3625 (setq gdb-changed-registers nil)
3626 (goto-char (point-min))
3627 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3628 (push (match-string 1) gdb-changed-registers)))
3629 \f
3630
3631 ;; Locals buffer.
3632 ;;
3633 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3634 (gdb-set-buffer-rules 'gdb-locals-buffer
3635 'gdb-locals-buffer-name
3636 'gdb-locals-mode)
3637
3638 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3639 (gdb-get-buffer 'gdb-locals-buffer)
3640 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3641 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3642 "-stack-list-locals --simple-values\n")
3643 gdb-stack-list-locals-handler)
3644
3645 (defconst gdb-stack-list-locals-regexp
3646 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3647
3648 (defvar gdb-locals-watch-map-1
3649 (let ((map (make-sparse-keymap)))
3650 (suppress-keymap map)
3651 (define-key map "\r" 'gud-watch)
3652 (define-key map [mouse-2] 'gud-watch)
3653 map)
3654 "Keymap to create watch expression of a complex data type local variable.")
3655
3656 (defvar gdb-edit-locals-map-1
3657 (let ((map (make-sparse-keymap)))
3658 (suppress-keymap map)
3659 (define-key map "\r" 'gdb-edit-locals-value)
3660 (define-key map [mouse-2] 'gdb-edit-locals-value)
3661 map)
3662 "Keymap to edit value of a simple data type local variable.")
3663
3664 (defun gdb-edit-locals-value (&optional event)
3665 "Assign a value to a variable displayed in the locals buffer."
3666 (interactive (list last-input-event))
3667 (save-excursion
3668 (if event (posn-set-point (event-end event)))
3669 (beginning-of-line)
3670 (let* ((var (current-word))
3671 (value (read-string (format "New value (%s): " var))))
3672 (gdb-enqueue-input
3673 (list (concat gdb-server-prefix"set variable " var " = " value "\n")
3674 'ignore)))))
3675
3676 ;; Dont display values of arrays or structures.
3677 ;; These can be expanded using gud-watch.
3678 (defun gdb-stack-list-locals-handler ()
3679 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3680 gdb-pending-triggers))
3681 (goto-char (point-min))
3682 (if (re-search-forward gdb-error-regexp nil t)
3683 (let ((err (match-string 1)))
3684 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3685 (let ((buffer-read-only nil))
3686 (erase-buffer)
3687 (insert err)
3688 (goto-char (point-min)))))
3689 (let (local locals-list)
3690 (goto-char (point-min))
3691 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3692 (let ((local (list (match-string 1)
3693 (match-string 2)
3694 nil)))
3695 (if (looking-at ",value=\\(\".*\"\\).*?}")
3696 (setcar (nthcdr 2 local) (read (match-string 1))))
3697 (push local locals-list)))
3698 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3699 (and buf (with-current-buffer buf
3700 (let* ((window (get-buffer-window buf 0))
3701 (start (window-start window))
3702 (p (window-point window))
3703 (buffer-read-only nil) (name) (value))
3704 (erase-buffer)
3705 (dolist (local locals-list)
3706 (setq name (car local))
3707 (setq value (nth 2 local))
3708 (if (or (not value)
3709 (string-match "^\\0x" value))
3710 (add-text-properties 0 (length name)
3711 `(mouse-face highlight
3712 help-echo "mouse-2: create watch expression"
3713 local-map ,gdb-locals-watch-map-1)
3714 name)
3715 (add-text-properties 0 (length value)
3716 `(mouse-face highlight
3717 help-echo "mouse-2: edit value"
3718 local-map ,gdb-edit-locals-map-1)
3719 value))
3720 (insert
3721 (concat name "\t" (nth 1 local)
3722 "\t" value "\n")))
3723 (set-window-start window start)
3724 (set-window-point window p))))))))
3725
3726 (defun gdb-get-register-names ()
3727 "Create a list of register names."
3728 (goto-char (point-min))
3729 (setq gdb-register-names nil)
3730 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3731 (push (match-string 1) gdb-register-names)))
3732
3733 (provide 'gdb-ui)
3734
3735 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3736 ;;; gdb-ui.el ends here