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