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