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