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