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