]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-mi.el
* progmodes/gdb-mi.el (gdb-stack-list-frames-handler): Rewritten
[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")) "\n")
491 'ignore)))
492 (if (eq window-system 'w32)
493 (gdb-input (list "-gdb-set new-console off\n" 'ignore)))
494 (gdb-input (list "-gdb-set height 0\n" '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\n" '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\n" 'gdb-get-source-file)))
503 (gdb-input
504 (list "-data-list-register-names\n" 'gdb-get-register-names))
505 (gdb-input
506 (list "-gdb-show prompt\n" '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 "\n")
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 "\n")
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) "\n")
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\n" '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 "\n") 'ignore))
746 (gdb-input
747 (list (concat "-var-list-children --all-values "
748 varnum "\n")
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 "\n") 'ignore))
783 (gdb-var-update)))
784
785 (defun gdb-var-delete-1 (varnum)
786 (gdb-input
787 (list (concat "-var-delete " varnum "\n") '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 "\n") '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 "\n")
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 *\n" '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 (car item)))
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\n"
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 "on" 'face font-lock-warning-face)
1560 (propertize "off" '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 (at (insert (concat " " at)))
1572 (t (insert (gdb-get-field breakpoint 'original-location)))))
1573 (add-text-properties (line-beginning-position)
1574 (line-end-position)
1575 `(gdb-breakpoint ,breakpoint
1576 mouse-face highlight
1577 help-echo "mouse-2, RET: visit breakpoint"))
1578 (newline))
1579 (gdb-place-breakpoints)))
1580
1581 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1582 (defun gdb-place-breakpoints ()
1583 (let ((flag) (bptno))
1584 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1585 (dolist (buffer (buffer-list))
1586 (with-current-buffer buffer
1587 (if (and (eq gud-minor-mode 'gdbmi)
1588 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
1589 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1590 (dolist (breakpoint gdb-breakpoints-list)
1591 (let ((line (gdb-get-field breakpoint 'line)))
1592 (when line
1593 (let ((file (gdb-get-field breakpoint 'file))
1594 (flag (gdb-get-field breakpoint 'enabled))
1595 (bptno (gdb-get-field breakpoint 'number)))
1596 (unless (file-exists-p file)
1597 (setq file (cdr (assoc bptno gdb-location-alist))))
1598 (if (and file
1599 (not (string-equal file "File not found")))
1600 (with-current-buffer
1601 (find-file-noselect file 'nowarn)
1602 (gdb-init-buffer)
1603 ;; Only want one breakpoint icon at each location.
1604 (save-excursion
1605 (goto-line (string-to-number line))
1606 (gdb-put-breakpoint-icon (string-equal flag "y") bptno)))
1607 (gdb-input
1608 (list (concat "list " file ":1\n")
1609 'ignore))
1610 (gdb-input
1611 (list "-file-list-exec-source-file\n"
1612 `(lambda () (gdb-get-location
1613 ,bptno ,line ,flag)))))))))))
1614
1615 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
1616
1617 (defun gdb-get-location (bptno line flag)
1618 "Find the directory containing the relevant source file.
1619 Put in buffer and place breakpoint icon."
1620 (goto-char (point-min))
1621 (catch 'file-not-found
1622 (if (re-search-forward gdb-source-file-regexp nil t)
1623 (delete (cons bptno "File not found") gdb-location-alist)
1624 (push (cons bptno (match-string 1)) gdb-location-alist)
1625 (gdb-resync)
1626 (unless (assoc bptno gdb-location-alist)
1627 (push (cons bptno "File not found") gdb-location-alist)
1628 (message-box "Cannot find source file for breakpoint location.
1629 Add directory to search path for source files using the GDB command, dir."))
1630 (throw 'file-not-found nil))
1631 (with-current-buffer (find-file-noselect (match-string 1))
1632 (gdb-init-buffer)
1633 ;; only want one breakpoint icon at each location
1634 (save-excursion
1635 (goto-line (string-to-number line))
1636 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
1637
1638 (add-hook 'find-file-hook 'gdb-find-file-hook)
1639
1640 (defun gdb-find-file-hook ()
1641 "Set up buffer for debugging if file is part of the source code
1642 of the current session."
1643 (if (and (buffer-name gud-comint-buffer)
1644 ;; in case gud or gdb-ui is just loaded
1645 gud-comint-buffer
1646 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
1647 'gdbmi))
1648 (if (member buffer-file-name gdb-source-file-list)
1649 (with-current-buffer (find-buffer-visiting buffer-file-name)
1650 (gdb-init-buffer)))))
1651
1652 (declare-function gud-remove "gdb-mi" t t) ; gud-def
1653 (declare-function gud-break "gdb-mi" t t) ; gud-def
1654 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
1655
1656 (defun gdb-mouse-set-clear-breakpoint (event)
1657 "Set/clear breakpoint in left fringe/margin at mouse click.
1658 If not in a source or disassembly buffer just set point."
1659 (interactive "e")
1660 (mouse-minibuffer-check event)
1661 (let ((posn (event-end event)))
1662 (with-selected-window (posn-window posn)
1663 (if (or (buffer-file-name) (eq major-mode 'gdb-disassembly-mode))
1664 (if (numberp (posn-point posn))
1665 (save-excursion
1666 (goto-char (posn-point posn))
1667 (if (or (posn-object posn)
1668 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1669 'breakpoint))
1670 (gud-remove nil)
1671 (gud-break nil)))))
1672 (posn-set-point posn))))
1673
1674 (defun gdb-mouse-toggle-breakpoint-margin (event)
1675 "Enable/disable breakpoint in left margin with mouse click."
1676 (interactive "e")
1677 (mouse-minibuffer-check event)
1678 (let ((posn (event-end event)))
1679 (if (numberp (posn-point posn))
1680 (with-selected-window (posn-window posn)
1681 (save-excursion
1682 (goto-char (posn-point posn))
1683 (if (posn-object posn)
1684 (gud-basic-call
1685 (let ((bptno (get-text-property
1686 0 'gdb-bptno (car (posn-string posn)))))
1687 (concat
1688 (if (get-text-property
1689 0 'gdb-enabled (car (posn-string posn)))
1690 "-break-disable "
1691 "-break-enable ")
1692 bptno "\n")))))))))
1693
1694 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1695 "Enable/disable breakpoint in left fringe with mouse click."
1696 (interactive "e")
1697 (mouse-minibuffer-check event)
1698 (let* ((posn (event-end event))
1699 (pos (posn-point posn))
1700 obj)
1701 (when (numberp pos)
1702 (with-selected-window (posn-window posn)
1703 (save-excursion
1704 (set-buffer (window-buffer (selected-window)))
1705 (goto-char pos)
1706 (dolist (overlay (overlays-in pos pos))
1707 (when (overlay-get overlay 'put-break)
1708 (setq obj (overlay-get overlay 'before-string))))
1709 (when (stringp obj)
1710 (gud-basic-call
1711 (concat
1712 (if (get-text-property 0 'gdb-enabled obj)
1713 "-break-disable "
1714 "-break-enable ")
1715 (get-text-property 0 'gdb-bptno obj) "\n"))))))))
1716
1717 (defun gdb-breakpoints-buffer-name ()
1718 (with-current-buffer gud-comint-buffer
1719 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1720
1721 (def-gdb-display-buffer
1722 gdb-display-breakpoints-buffer
1723 'gdb-breakpoints-buffer
1724 "Display status of user-settable breakpoints.")
1725
1726 (def-gdb-frame-for-buffer
1727 gdb-frame-breakpoints-buffer
1728 'gdb-breakpoints-buffer
1729 "Display status of user-settable breakpoints in a new frame.")
1730
1731 (defvar gdb-breakpoints-mode-map
1732 (let ((map (make-sparse-keymap))
1733 (menu (make-sparse-keymap "Breakpoints")))
1734 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1735 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1736 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1737 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1738 (suppress-keymap map)
1739 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1740 (define-key map " " 'gdb-toggle-breakpoint)
1741 (define-key map "D" 'gdb-delete-breakpoint)
1742 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1743 (define-key map "q" 'gdb-delete-frame-or-window)
1744 (define-key map "\r" 'gdb-goto-breakpoint)
1745 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1746 (define-key map [follow-link] 'mouse-face)
1747 map))
1748
1749 (defun gdb-delete-frame-or-window ()
1750 "Delete frame if there is only one window. Otherwise delete the window."
1751 (interactive)
1752 (if (one-window-p) (delete-frame)
1753 (delete-window)))
1754
1755 ;;from make-mode-line-mouse-map
1756 (defun gdb-make-header-line-mouse-map (mouse function) "\
1757 Return a keymap with single entry for mouse key MOUSE on the header line.
1758 MOUSE is defined to run function FUNCTION with no args in the buffer
1759 corresponding to the mode line clicked."
1760 (let ((map (make-sparse-keymap)))
1761 (define-key map (vector 'header-line mouse) function)
1762 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
1763 map))
1764
1765 \f
1766 ;; uses "-thread-info". Needs GDB 7.0 onwards.
1767 ;;; Threads view
1768
1769 (defun gdb-jump-to (file line)
1770 (find-file-other-window file)
1771 (goto-line line))
1772
1773 (define-button-type 'gdb-file-button
1774 'help-echo "Push to jump to source code"
1775 ; 'face 'bold
1776 'action
1777 (lambda (b)
1778 (gdb-jump-to (button-get b 'file)
1779 (button-get b 'line))))
1780
1781 (defun gdb-insert-file-location-button (file line)
1782 "Insert text button which allows jumping to FILE:LINE.
1783
1784 FILE is a full path."
1785 (insert-text-button
1786 (format "%s:%d" (file-name-nondirectory file) line)
1787 :type 'gdb-file-button
1788 'file file
1789 'line line))
1790
1791 (defun gdb-threads-buffer-name ()
1792 (concat "*threads of " (gdb-get-target-string) "*"))
1793
1794 (def-gdb-display-buffer
1795 gdb-display-threads-buffer
1796 'gdb-threads-buffer
1797 "Display GDB threads.")
1798
1799 (def-gdb-frame-for-buffer
1800 gdb-frame-threads-buffer
1801 'gdb-threads-buffer
1802 "Display GDB threads in a new frame.")
1803
1804 (gdb-set-buffer-rules 'gdb-threads-buffer
1805 'gdb-threads-buffer-name
1806 'gdb-threads-mode)
1807
1808 (def-gdb-auto-updated-buffer gdb-threads-buffer
1809 gdb-invalidate-threads "-thread-info\n"
1810 gdb-thread-list-handler gdb-thread-list-handler-custom)
1811
1812
1813 (defvar gdb-threads-font-lock-keywords
1814 '(("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
1815 (" \\(stopped\\) in " (1 font-lock-warning-face))
1816 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
1817 "Font lock keywords used in `gdb-threads-mode'.")
1818
1819 (defvar gdb-threads-mode-map
1820 ;; TODO
1821 (make-sparse-keymap))
1822
1823 (defun gdb-threads-mode ()
1824 "Major mode for GDB threads.
1825
1826 \\{gdb-threads-mode-map}"
1827 (kill-all-local-variables)
1828 (setq major-mode 'gdb-threads-mode)
1829 (setq mode-name "Threads")
1830 (use-local-map gdb-threads-mode-map)
1831 (setq buffer-read-only t)
1832 (buffer-disable-undo)
1833 (setq header-line-format gdb-breakpoints-header)
1834 (set (make-local-variable 'font-lock-defaults)
1835 '(gdb-threads-font-lock-keywords))
1836 (run-mode-hooks 'gdb-threads-mode-hook)
1837 'gdb-invalidate-threads)
1838
1839 (defun gdb-thread-list-handler-custom ()
1840 (let* ((res (json-partial-output))
1841 (threads-list (gdb-get-field res 'threads)))
1842 (dolist (thread threads-list)
1843 (insert (apply 'format `("%s (%s) %s in %s "
1844 ,@(gdb-get-many-fields thread 'id 'target-id 'state)
1845 ,(gdb-get-field thread 'frame 'func))))
1846 ;; Arguments
1847 (insert "(")
1848 (let ((args (gdb-get-field thread 'frame 'args)))
1849 (dolist (arg args)
1850 (insert (apply 'format `("%s=%s" ,@(gdb-get-many-fields arg 'name 'value)))))
1851 (when args (kill-backward-chars 1)))
1852 (insert ")")
1853 (gdb-insert-frame-location (gdb-get-field thread 'frame))
1854 (insert (format " at %s\n" (gdb-get-field thread 'frame 'addr))))))
1855
1856 \f
1857 ;;; Memory view
1858
1859 (defcustom gdb-memory-rows 8
1860 "Number of data rows in memory window."
1861 :type 'integer
1862 :group 'gud
1863 :version "23.2")
1864
1865 (defcustom gdb-memory-columns 4
1866 "Number of data columns in memory window."
1867 :type 'integer
1868 :group 'gud
1869 :version "23.2")
1870
1871 (defcustom gdb-memory-format "x"
1872 "Display format of data items in memory window."
1873 :type '(choice (const :tag "Hexadecimal" "x")
1874 (const :tag "Signed decimal" "d")
1875 (const :tag "Unsigned decimal" "u")
1876 (const :tag "Octal" "o")
1877 (const :tag "Binary" "t"))
1878 :group 'gud
1879 :version "22.1")
1880
1881 (defcustom gdb-memory-unit 4
1882 "Unit size of data items in memory window."
1883 :type '(choice (const :tag "Byte" 1)
1884 (const :tag "Halfword" 2)
1885 (const :tag "Word" 4)
1886 (const :tag "Giant word" 8))
1887 :group 'gud
1888 :version "23.2")
1889
1890 (gdb-set-buffer-rules 'gdb-memory-buffer
1891 'gdb-memory-buffer-name
1892 'gdb-memory-mode)
1893
1894 (def-gdb-auto-updated-buffer gdb-memory-buffer
1895 gdb-invalidate-memory
1896 (format "-data-read-memory %s %s %d %d %d\n"
1897 gdb-memory-address
1898 gdb-memory-format
1899 gdb-memory-unit
1900 gdb-memory-rows
1901 gdb-memory-columns)
1902 gdb-read-memory-handler
1903 gdb-read-memory-custom)
1904
1905 (defun gdb-memory-column-width (size format)
1906 "Return length of string with memory unit of SIZE in FORMAT.
1907
1908 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
1909 in `gdb-memory-format'."
1910 (let ((format-base (cdr (assoc format
1911 '(("x" . 16)
1912 ("d" . 10) ("u" . 10)
1913 ("o" . 8)
1914 ("t" . 2))))))
1915 (if format-base
1916 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
1917 (cond ((string-equal format "x")
1918 (+ 2 res)) ; hexadecimal numbers have 0x in front
1919 ((or (string-equal format "d")
1920 (string-equal format "o"))
1921 (1+ res))
1922 (t res)))
1923 (error "Unknown format"))))
1924
1925 (defun gdb-read-memory-custom ()
1926 (let* ((res (json-partial-output))
1927 (err-msg (gdb-get-field res 'msg)))
1928 (if (not err-msg)
1929 (let ((memory (gdb-get-field res 'memory)))
1930 (setq gdb-memory-address (gdb-get-field res 'addr))
1931 (setq gdb-memory-next-page (gdb-get-field res 'next-page))
1932 (setq gdb-memory-prev-page (gdb-get-field res 'prev-page))
1933 (setq gdb-memory-last-address gdb-memory-address)
1934 (dolist (row memory)
1935 (insert (concat (gdb-get-field row 'addr) ":"))
1936 (dolist (column (gdb-get-field row 'data))
1937 (insert (gdb-pad-string column
1938 (+ 2 (gdb-memory-column-width
1939 gdb-memory-unit
1940 gdb-memory-format)))))
1941 (newline)))
1942 ;; Show last page instead of empty buffer when out of bounds
1943 (progn
1944 (let ((gdb-memory-address gdb-memory-last-address))
1945 (gdb-invalidate-memory)
1946 (error err-msg))))))
1947
1948 (defvar gdb-memory-mode-map
1949 (let ((map (make-sparse-keymap)))
1950 (suppress-keymap map t)
1951 (define-key map "q" 'kill-this-buffer)
1952 (define-key map "n" 'gdb-memory-show-next-page)
1953 (define-key map "p" 'gdb-memory-show-previous-page)
1954 (define-key map "a" 'gdb-memory-set-address)
1955 (define-key map "t" 'gdb-memory-format-binary)
1956 (define-key map "o" 'gdb-memory-format-octal)
1957 (define-key map "u" 'gdb-memory-format-unsigned)
1958 (define-key map "d" 'gdb-memory-format-signed)
1959 (define-key map "x" 'gdb-memory-format-hexadecimal)
1960 (define-key map "b" 'gdb-memory-unit-byte)
1961 (define-key map "h" 'gdb-memory-unit-halfword)
1962 (define-key map "w" 'gdb-memory-unit-word)
1963 (define-key map "g" 'gdb-memory-unit-giant)
1964 (define-key map "R" 'gdb-memory-set-rows)
1965 (define-key map "C" 'gdb-memory-set-columns)
1966 map))
1967
1968 (defun gdb-memory-set-address-event (event)
1969 "Handle a click on address field in memory buffer header."
1970 (interactive "e")
1971 (save-selected-window
1972 (select-window (posn-window (event-start event)))
1973 (gdb-memory-set-address)))
1974
1975 ;; Non-event version for use within keymap
1976 (defun gdb-memory-set-address ()
1977 "Set the start memory address."
1978 (interactive)
1979 (let ((arg (read-from-minibuffer "Memory address: ")))
1980 (setq gdb-memory-address arg))
1981 (gdb-invalidate-memory))
1982
1983 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
1984 "Define a function NAME which reads new VAR value from minibuffer."
1985 `(defun ,name (event)
1986 ,(when doc doc)
1987 (interactive "e")
1988 (save-selected-window
1989 (select-window (posn-window (event-start event)))
1990 (let* ((arg (read-from-minibuffer ,echo-string))
1991 (count (string-to-number arg)))
1992 (if (<= count 0)
1993 (error "Positive number only")
1994 (customize-set-variable ',variable count)
1995 (gdb-invalidate-memory))))))
1996
1997 (def-gdb-set-positive-number
1998 gdb-memory-set-rows
1999 gdb-memory-rows
2000 "Rows: "
2001 "Set the number of data rows in memory window.")
2002
2003 (def-gdb-set-positive-number
2004 gdb-memory-set-columns
2005 gdb-memory-columns
2006 "Columns: "
2007 "Set the number of data columns in memory window.")
2008
2009 (defmacro def-gdb-memory-format (name format doc)
2010 "Define a function NAME to switch memory buffer to use FORMAT.
2011
2012 DOC is an optional documentation string."
2013 `(defun ,name () ,(when doc doc)
2014 (interactive)
2015 (customize-set-variable 'gdb-memory-format ,format)
2016 (gdb-invalidate-memory)))
2017
2018 (def-gdb-memory-format
2019 gdb-memory-format-binary "t"
2020 "Set the display format to binary.")
2021
2022 (def-gdb-memory-format
2023 gdb-memory-format-octal "o"
2024 "Set the display format to octal.")
2025
2026 (def-gdb-memory-format
2027 gdb-memory-format-unsigned "u"
2028 "Set the display format to unsigned decimal.")
2029
2030 (def-gdb-memory-format
2031 gdb-memory-format-signed "d"
2032 "Set the display format to decimal.")
2033
2034 (def-gdb-memory-format
2035 gdb-memory-format-hexadecimal "x"
2036 "Set the display format to hexadecimal.")
2037
2038 (defvar gdb-memory-format-map
2039 (let ((map (make-sparse-keymap)))
2040 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2041 map)
2042 "Keymap to select format in the header line.")
2043
2044 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2045 "Menu of display formats in the header line.")
2046
2047 (define-key gdb-memory-format-menu [binary]
2048 '(menu-item "Binary" gdb-memory-format-binary
2049 :button (:radio . (equal gdb-memory-format "t"))))
2050 (define-key gdb-memory-format-menu [octal]
2051 '(menu-item "Octal" gdb-memory-format-octal
2052 :button (:radio . (equal gdb-memory-format "o"))))
2053 (define-key gdb-memory-format-menu [unsigned]
2054 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2055 :button (:radio . (equal gdb-memory-format "u"))))
2056 (define-key gdb-memory-format-menu [signed]
2057 '(menu-item "Signed Decimal" gdb-memory-format-signed
2058 :button (:radio . (equal gdb-memory-format "d"))))
2059 (define-key gdb-memory-format-menu [hexadecimal]
2060 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2061 :button (:radio . (equal gdb-memory-format "x"))))
2062
2063 (defun gdb-memory-format-menu (event)
2064 (interactive "@e")
2065 (x-popup-menu event gdb-memory-format-menu))
2066
2067 (defun gdb-memory-format-menu-1 (event)
2068 (interactive "e")
2069 (save-selected-window
2070 (select-window (posn-window (event-start event)))
2071 (let* ((selection (gdb-memory-format-menu event))
2072 (binding (and selection (lookup-key gdb-memory-format-menu
2073 (vector (car selection))))))
2074 (if binding (call-interactively binding)))))
2075
2076 (defmacro def-gdb-memory-unit (name unit-size doc)
2077 "Define a function NAME to switch memory unit size to UNIT-SIZE.
2078
2079 DOC is an optional documentation string."
2080 `(defun ,name () ,(when doc doc)
2081 (interactive)
2082 (customize-set-variable 'gdb-memory-unit ,unit-size)
2083 (gdb-invalidate-memory)))
2084
2085 (def-gdb-memory-unit gdb-memory-unit-giant 8
2086 "Set the unit size to giant words (eight bytes).")
2087
2088 (def-gdb-memory-unit gdb-memory-unit-word 4
2089 "Set the unit size to words (four bytes).")
2090
2091 (def-gdb-memory-unit gdb-memory-unit-halfword 2
2092 "Set the unit size to halfwords (two bytes).")
2093
2094 (def-gdb-memory-unit gdb-memory-unit-byte 1
2095 "Set the unit size to bytes.")
2096
2097 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
2098 "Define a function NAME which show new address in memory buffer.
2099
2100 The defined function switches Memory buffer to show address
2101 stored in ADDRESS-VAR variable.
2102
2103 DOC is an optional documentation string."
2104 `(defun ,name
2105 ,(when doc doc)
2106 (interactive)
2107 (let ((gdb-memory-address ,address-var))
2108 (gdb-invalidate-memory))))
2109
2110 (def-gdb-memory-show-page gdb-memory-show-previous-page
2111 gdb-memory-prev-page)
2112
2113 (def-gdb-memory-show-page gdb-memory-show-next-page
2114 gdb-memory-next-page)
2115
2116 (defvar gdb-memory-unit-map
2117 (let ((map (make-sparse-keymap)))
2118 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2119 map)
2120 "Keymap to select units in the header line.")
2121
2122 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2123 "Menu of units in the header line.")
2124
2125 (define-key gdb-memory-unit-menu [giantwords]
2126 '(menu-item "Giant words" gdb-memory-unit-giant
2127 :button (:radio . (equal gdb-memory-unit 8))))
2128 (define-key gdb-memory-unit-menu [words]
2129 '(menu-item "Words" gdb-memory-unit-word
2130 :button (:radio . (equal gdb-memory-unit 4))))
2131 (define-key gdb-memory-unit-menu [halfwords]
2132 '(menu-item "Halfwords" gdb-memory-unit-halfword
2133 :button (:radio . (equal gdb-memory-unit 2))))
2134 (define-key gdb-memory-unit-menu [bytes]
2135 '(menu-item "Bytes" gdb-memory-unit-byte
2136 :button (:radio . (equal gdb-memory-unit 1))))
2137
2138 (defun gdb-memory-unit-menu (event)
2139 (interactive "@e")
2140 (x-popup-menu event gdb-memory-unit-menu))
2141
2142 (defun gdb-memory-unit-menu-1 (event)
2143 (interactive "e")
2144 (save-selected-window
2145 (select-window (posn-window (event-start event)))
2146 (let* ((selection (gdb-memory-unit-menu event))
2147 (binding (and selection (lookup-key gdb-memory-unit-menu
2148 (vector (car selection))))))
2149 (if binding (call-interactively binding)))))
2150
2151 ;;from make-mode-line-mouse-map
2152 (defun gdb-make-header-line-mouse-map (mouse function) "\
2153 Return a keymap with single entry for mouse key MOUSE on the header line.
2154 MOUSE is defined to run function FUNCTION with no args in the buffer
2155 corresponding to the mode line clicked."
2156 (let ((map (make-sparse-keymap)))
2157 (define-key map (vector 'header-line mouse) function)
2158 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2159 map))
2160
2161 (defvar gdb-memory-font-lock-keywords
2162 '(;; <__function.name+n>
2163 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2164 )
2165 "Font lock keywords used in `gdb-memory-mode'.")
2166
2167 (defvar gdb-memory-header
2168 '(:eval
2169 (concat
2170 "Start address["
2171 (propertize "-"
2172 'face font-lock-warning-face
2173 'help-echo "mouse-1: decrement address"
2174 'mouse-face 'mode-line-highlight
2175 'local-map (gdb-make-header-line-mouse-map
2176 'mouse-1
2177 #'gdb-memory-show-previous-page))
2178 "|"
2179 (propertize "+"
2180 'face font-lock-warning-face
2181 'help-echo "mouse-1: increment address"
2182 'mouse-face 'mode-line-highlight
2183 'local-map (gdb-make-header-line-mouse-map
2184 'mouse-1
2185 #'gdb-memory-show-next-page))
2186 "]: "
2187 (propertize gdb-memory-address
2188 'face font-lock-warning-face
2189 'help-echo "mouse-1: set start address"
2190 'mouse-face 'mode-line-highlight
2191 'local-map (gdb-make-header-line-mouse-map
2192 'mouse-1
2193 #'gdb-memory-set-address-event))
2194 " Rows: "
2195 (propertize (number-to-string gdb-memory-rows)
2196 'face font-lock-warning-face
2197 'help-echo "mouse-1: set number of columns"
2198 'mouse-face 'mode-line-highlight
2199 'local-map (gdb-make-header-line-mouse-map
2200 'mouse-1
2201 #'gdb-memory-set-rows))
2202 " Columns: "
2203 (propertize (number-to-string gdb-memory-columns)
2204 'face font-lock-warning-face
2205 'help-echo "mouse-1: set number of columns"
2206 'mouse-face 'mode-line-highlight
2207 'local-map (gdb-make-header-line-mouse-map
2208 'mouse-1
2209 #'gdb-memory-set-columns))
2210 " Display Format: "
2211 (propertize gdb-memory-format
2212 'face font-lock-warning-face
2213 'help-echo "mouse-3: select display format"
2214 'mouse-face 'mode-line-highlight
2215 'local-map gdb-memory-format-map)
2216 " Unit Size: "
2217 (propertize (number-to-string gdb-memory-unit)
2218 'face font-lock-warning-face
2219 'help-echo "mouse-3: select unit size"
2220 'mouse-face 'mode-line-highlight
2221 'local-map gdb-memory-unit-map)))
2222 "Header line used in `gdb-memory-mode'.")
2223
2224 (defun gdb-memory-mode ()
2225 "Major mode for examining memory.
2226
2227 \\{gdb-memory-mode-map}"
2228 (kill-all-local-variables)
2229 (setq major-mode 'gdb-memory-mode)
2230 (setq mode-name "Memory")
2231 (use-local-map gdb-memory-mode-map)
2232 (setq buffer-read-only t)
2233 (setq header-line-format gdb-memory-header)
2234 (set (make-local-variable 'font-lock-defaults)
2235 '(gdb-memory-font-lock-keywords))
2236 (run-mode-hooks 'gdb-memory-mode-hook)
2237 'gdb-invalidate-memory)
2238
2239 (defun gdb-memory-buffer-name ()
2240 (with-current-buffer gud-comint-buffer
2241 (concat "*memory of " (gdb-get-target-string) "*")))
2242
2243 (def-gdb-display-buffer
2244 gdb-display-memory-buffer
2245 'gdb-memory-buffer
2246 "Display memory contents.")
2247
2248 (defun gdb-frame-memory-buffer ()
2249 "Display memory contents in a new frame."
2250 (interactive)
2251 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2252 (special-display-frame-alist
2253 `((left-fringe . 0)
2254 (right-fringe . 0)
2255 (width . 83)
2256 ,@gdb-frame-parameters)))
2257 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2258
2259 \f
2260 ;;; Disassembly view
2261
2262 (defun gdb-disassembly-buffer-name ()
2263 (concat "*disassembly of " (gdb-get-target-string) "*"))
2264
2265 (def-gdb-display-buffer
2266 gdb-display-disassembly-buffer
2267 'gdb-disassembly-buffer
2268 "Display disassembly for current stack frame.")
2269
2270 (def-gdb-frame-for-buffer
2271 gdb-frame-disassembly-buffer
2272 'gdb-disassembly-buffer
2273 "Display disassembly in a new frame.")
2274
2275 (gdb-set-buffer-rules 'gdb-disassembly-buffer
2276 'gdb-disassembly-buffer-name
2277 'gdb-disassembly-mode)
2278
2279 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
2280 (gdb-get-buffer 'gdb-disassembly-buffer)
2281 (let ((file (or gdb-selected-file gdb-main-file))
2282 (line (or gdb-selected-line 1)))
2283 (if (not file) (error "Disassembly invalidated with no file selected.")
2284 (format "-data-disassemble -f %s -l %d -n -1 -- 0\n" file line)))
2285 gdb-disassembly-handler)
2286
2287 (def-gdb-auto-update-handler
2288 gdb-disassembly-handler
2289 gdb-invalidate-disassembly
2290 gdb-disassembly-buffer
2291 gdb-disassembly-handler-custom)
2292
2293 (defvar gdb-disassembly-font-lock-keywords
2294 '(;; <__function.name+n>
2295 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2296 (1 font-lock-function-name-face))
2297 ;; 0xNNNNNNNN <__function.name+n>: opcode
2298 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2299 (4 font-lock-keyword-face))
2300 ;; %register(at least i386)
2301 ("%\\sw+" . font-lock-variable-name-face)
2302 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2303 (1 font-lock-comment-face)
2304 (2 font-lock-function-name-face))
2305 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2306 "Font lock keywords used in `gdb-disassembly-mode'.")
2307
2308 (defvar gdb-disassembly-mode-map
2309 ;; TODO
2310 (make-sparse-keymap))
2311
2312 (defun gdb-disassembly-mode ()
2313 "Major mode for GDB disassembly information.
2314
2315 \\{gdb-disassembly-mode-map}"
2316 (kill-all-local-variables)
2317 (setq major-mode 'gdb-disassembly-mode)
2318 (setq mode-name "Disassembly")
2319 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2320 (setq fringes-outside-margins t)
2321 (setq gdb-overlay-arrow-position (make-marker))
2322 (use-local-map gdb-disassembly-mode-map)
2323 (setq buffer-read-only t)
2324 (buffer-disable-undo)
2325 (set (make-local-variable 'font-lock-defaults)
2326 '(gdb-disassembly-font-lock-keywords))
2327 (run-mode-hooks 'gdb-disassembly-mode-hook)
2328 'gdb-invalidate-disassembly)
2329
2330 (defun gdb-disassembly-handler-custom ()
2331 (let* ((res (json-partial-output))
2332 (instructions (gdb-get-field res 'asm_insns))
2333 (pos 1))
2334 (let* ((last-instr (car (last instructions)))
2335 (column-padding (+ 2 (string-width
2336 (apply 'format
2337 `("<%s+%s>:"
2338 ,@(gdb-get-many-fields last-instr 'func-name 'offset)))))))
2339 (dolist (instr instructions)
2340 ;; Put overlay arrow
2341 (when (string-equal (gdb-get-field instr 'address)
2342 gdb-pc-address)
2343 (progn
2344 (setq pos (point))
2345 (setq fringe-indicator-alist
2346 (if (string-equal gdb-frame-number "0")
2347 nil
2348 '((overlay-arrow . hollow-right-triangle))))
2349 (set-marker gdb-overlay-arrow-position (point))))
2350 (insert
2351 (concat
2352 (gdb-get-field instr 'address)
2353 " "
2354 (gdb-pad-string (apply 'format `("<%s+%s>:" ,@(gdb-get-many-fields instr 'func-name 'offset)))
2355 (- column-padding))
2356 (gdb-get-field instr 'inst)
2357 "\n")))
2358 (gdb-disassembly-place-breakpoints)
2359 (let ((window (get-buffer-window (current-buffer) 0)))
2360 (set-window-point window pos)))))
2361
2362 (defun gdb-disassembly-place-breakpoints ()
2363 (gdb-remove-breakpoint-icons (point-min) (point-max))
2364 (dolist (breakpoint gdb-breakpoints-list)
2365 (let ((bptno (gdb-get-field breakpoint 'number))
2366 (flag (gdb-get-field breakpoint 'enabled))
2367 (address (gdb-get-field breakpoint 'addr)))
2368 (save-excursion
2369 (goto-char (point-min))
2370 (if (re-search-forward (concat "^" address) nil t)
2371 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
2372
2373 \f
2374 ;;; Breakpoints view
2375 (defvar gdb-breakpoints-header
2376 `(,(propertize "Breakpoints"
2377 'help-echo "mouse-1: select"
2378 'mouse-face 'mode-line-highlight
2379 'face 'mode-line
2380 'local-map
2381 (gdb-make-header-line-mouse-map
2382 'mouse-1
2383 (lambda (event) (interactive "e")
2384 (save-selected-window
2385 (select-window (posn-window (event-start event)))
2386 (set-window-dedicated-p (selected-window) nil)
2387 (switch-to-buffer
2388 (gdb-get-buffer-create 'gdb-breakpoints-buffer))
2389 (set-window-dedicated-p (selected-window) t)))))
2390 " "
2391 ,(propertize "Threads"
2392 'help-echo "mouse-1: select"
2393 'mouse-face 'mode-line-highlight
2394 'face 'mode-line
2395 'local-map
2396 (gdb-make-header-line-mouse-map
2397 'mouse-1
2398 ;; TODO: same code few lines above
2399 (lambda (event) (interactive "e")
2400 (save-selected-window
2401 (select-window (posn-window (event-start event)))
2402 (set-window-dedicated-p (selected-window) nil)
2403 (switch-to-buffer
2404 (gdb-get-buffer-create 'gdb-threads-buffer))
2405 (set-window-dedicated-p (selected-window) t)))
2406 ))))
2407
2408 (defun gdb-breakpoints-mode ()
2409 "Major mode for gdb breakpoints.
2410
2411 \\{gdb-breakpoints-mode-map}"
2412 (kill-all-local-variables)
2413 (setq major-mode 'gdb-breakpoints-mode)
2414 (setq mode-name "Breakpoints")
2415 (use-local-map gdb-breakpoints-mode-map)
2416 (setq buffer-read-only t)
2417 (buffer-disable-undo)
2418 (setq header-line-format gdb-breakpoints-header)
2419 (run-mode-hooks 'gdb-breakpoints-mode-hook)
2420 'gdb-invalidate-breakpoints)
2421
2422 (defun gdb-toggle-breakpoint ()
2423 "Enable/disable breakpoint at current line of breakpoints buffer."
2424 (interactive)
2425 (save-excursion
2426 (beginning-of-line)
2427 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
2428 (if breakpoint
2429 (gud-basic-call
2430 (concat (if (string-equal "y" (gdb-get-field breakpoint 'enabled))
2431 "-break-disable "
2432 "-break-enable ")
2433 (gdb-get-field breakpoint 'number)))
2434 (error "Not recognized as break/watchpoint line")))))
2435
2436 (defun gdb-delete-breakpoint ()
2437 "Delete the breakpoint at current line of breakpoints buffer."
2438 (interactive)
2439 (save-excursion
2440 (beginning-of-line)
2441 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
2442 (if breakpoint
2443 (gud-basic-call (concat "-break-delete " (gdb-get-field breakpoint 'number)))
2444 (error "Not recognized as break/watchpoint line")))))
2445
2446 (defun gdb-goto-breakpoint (&optional event)
2447 "Go to the location of breakpoint at current line of
2448 breakpoints buffer."
2449 (interactive (list last-input-event))
2450 (if event (posn-set-point (event-end event)))
2451 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
2452 (let ((window (get-buffer-window gud-comint-buffer)))
2453 (if window (save-selected-window (select-window window))))
2454 (save-excursion
2455 (beginning-of-line)
2456 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
2457 (if breakpoint
2458 (let ((bptno (gdb-get-field breakpoint 'number))
2459 (file (gdb-get-field breakpoint 'file))
2460 (line (gdb-get-field breakpoint 'line)))
2461 (save-selected-window
2462 (let* ((buffer (find-file-noselect
2463 (if (file-exists-p file) file
2464 (cdr (assoc bptno gdb-location-alist)))))
2465 (window (or (gdb-display-source-buffer buffer)
2466 (display-buffer buffer))))
2467 (setq gdb-source-window window)
2468 (with-current-buffer buffer
2469 (goto-line (string-to-number line))
2470 (set-window-point window (point))))))
2471 (error "Not recognized as break/watchpoint line")))))
2472
2473 \f
2474 ;; Frames buffer. This displays a perpetually correct bactrack trace.
2475 ;;
2476 (gdb-set-buffer-rules 'gdb-stack-buffer
2477 'gdb-stack-buffer-name
2478 'gdb-frames-mode)
2479
2480 (def-gdb-auto-updated-buffer gdb-stack-buffer
2481 gdb-invalidate-frames
2482 "-stack-list-frames\n"
2483 gdb-stack-list-frames-handler
2484 gdb-stack-list-frames-custom)
2485
2486 (defun gdb-insert-frame-location (frame)
2487 "Insert \"of file:line\" button or library name for structure FRAME.
2488
2489 FRAME must have either \"file\" and \"line\" members or \"from\"
2490 member."
2491 (let ((file (gdb-get-field frame 'fullname))
2492 (line (gdb-get-field frame 'line))
2493 (from (gdb-get-field frame 'from)))
2494 (cond (file
2495 ;; Filename with line number
2496 (insert " of ")
2497 (gdb-insert-file-location-button
2498 file (string-to-number line)))
2499 ;; Library
2500 (from (insert (format " of %s" from))))))
2501
2502 (defun gdb-stack-list-frames-custom ()
2503 (let* ((res (json-partial-output "frame"))
2504 (stack (gdb-get-field res 'stack)))
2505 (dolist (frame (nreverse stack))
2506 (insert (apply 'format `("%s in %s" ,@(gdb-get-many-fields frame 'level 'func))))
2507 (gdb-insert-frame-location frame)
2508 (newline))
2509 (save-excursion
2510 (goto-char (point-min))
2511 (forward-line 1)
2512 (while (< (point) (point-max))
2513 (add-text-properties (point-at-bol) (1+ (point-at-bol))
2514 '(mouse-face highlight
2515 help-echo "mouse-2, RET: Select frame"))
2516 (beginning-of-line)
2517 (when (and (looking-at "^[0-9]+\\s-+\\S-+\\s-+\\(\\S-+\\)")
2518 (equal (match-string 1) gdb-selected-frame))
2519 (if (> (car (window-fringes)) 0)
2520 (progn
2521 (or gdb-stack-position
2522 (setq gdb-stack-position (make-marker)))
2523 (set-marker gdb-stack-position (point)))
2524 (let ((bl (point-at-bol)))
2525 (put-text-property bl (+ bl 4)
2526 'face '(:inverse-video t)))))
2527 (forward-line 1)))))
2528
2529 (defun gdb-stack-buffer-name ()
2530 (with-current-buffer gud-comint-buffer
2531 (concat "*stack frames of " (gdb-get-target-string) "*")))
2532
2533 (def-gdb-display-buffer
2534 gdb-display-stack-buffer
2535 'gdb-stack-buffer
2536 "Display backtrace of current stack.")
2537
2538 (def-gdb-frame-for-buffer
2539 gdb-frame-stack-buffer
2540 'gdb-stack-buffer
2541 "Display backtrace of current stack in a new frame.")
2542
2543 (defvar gdb-frames-mode-map
2544 (let ((map (make-sparse-keymap)))
2545 (suppress-keymap map)
2546 (define-key map "q" 'kill-this-buffer)
2547 (define-key map "\r" 'gdb-frames-select)
2548 (define-key map [mouse-2] 'gdb-frames-select)
2549 (define-key map [follow-link] 'mouse-face)
2550 map))
2551
2552 (defvar gdb-frames-font-lock-keywords
2553 '(("in \\([^ ]+\\) of " (1 font-lock-function-name-face)))
2554 "Font lock keywords used in `gdb-frames-mode'.")
2555
2556 (defun gdb-frames-mode ()
2557 "Major mode for gdb call stack.
2558
2559 \\{gdb-frames-mode-map}"
2560 (kill-all-local-variables)
2561 (setq major-mode 'gdb-frames-mode)
2562 (setq mode-name "Frames")
2563 (setq gdb-stack-position nil)
2564 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2565 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
2566 (setq buffer-read-only t)
2567 (buffer-disable-undo)
2568 (use-local-map gdb-frames-mode-map)
2569 (set (make-local-variable 'font-lock-defaults)
2570 '(gdb-frames-font-lock-keywords))
2571 (run-mode-hooks 'gdb-frames-mode-hook)
2572 'gdb-invalidate-frames)
2573
2574 (defun gdb-get-frame-number ()
2575 (save-excursion
2576 (end-of-line)
2577 (let* ((pos (re-search-backward "^\\([0-9]+\\)" nil t))
2578 (n (or (and pos (match-string-no-properties 1)) "0")))
2579 n)))
2580
2581 (defun gdb-frames-select (&optional event)
2582 "Select the frame and display the relevant source."
2583 (interactive (list last-input-event))
2584 (if event (posn-set-point (event-end event)))
2585 (gud-basic-call (concat "-stack-select-frame " (gdb-get-frame-number))))
2586
2587 \f
2588 ;; Locals buffer.
2589 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
2590 (gdb-set-buffer-rules 'gdb-locals-buffer
2591 'gdb-locals-buffer-name
2592 'gdb-locals-mode)
2593
2594 (def-gdb-auto-update-trigger gdb-invalidate-locals
2595 (gdb-get-buffer 'gdb-locals-buffer)
2596 "-stack-list-locals --simple-values\n"
2597 gdb-stack-list-locals-handler)
2598
2599 (defconst gdb-stack-list-locals-regexp
2600 (concat "name=\"\\(.*?\\)\",type=\"\\(.*?\\)\""))
2601
2602 (defvar gdb-locals-watch-map
2603 (let ((map (make-sparse-keymap)))
2604 (suppress-keymap map)
2605 (define-key map "\r" 'gud-watch)
2606 (define-key map [mouse-2] 'gud-watch)
2607 map)
2608 "Keymap to create watch expression of a complex data type local variable.")
2609
2610 (defvar gdb-edit-locals-map-1
2611 (let ((map (make-sparse-keymap)))
2612 (suppress-keymap map)
2613 (define-key map "\r" 'gdb-edit-locals-value)
2614 (define-key map [mouse-2] 'gdb-edit-locals-value)
2615 map)
2616 "Keymap to edit value of a simple data type local variable.")
2617
2618 (defun gdb-edit-locals-value (&optional event)
2619 "Assign a value to a variable displayed in the locals buffer."
2620 (interactive (list last-input-event))
2621 (save-excursion
2622 (if event (posn-set-point (event-end event)))
2623 (beginning-of-line)
2624 (let* ((var (current-word))
2625 (value (read-string (format "New value (%s): " var))))
2626 (gud-basic-call
2627 (concat "-gdb-set variable " var " = " value)))))
2628
2629 ;; Dont display values of arrays or structures.
2630 ;; These can be expanded using gud-watch.
2631 (defun gdb-stack-list-locals-handler nil
2632 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2633 gdb-pending-triggers))
2634 (let (local locals-list)
2635 (goto-char (point-min))
2636 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
2637 (let ((local (list (match-string 1)
2638 (match-string 2)
2639 nil)))
2640 (if (looking-at ",value=\\(\".*\"\\)}")
2641 (setcar (nthcdr 2 local) (read (match-string 1))))
2642 (push local locals-list)))
2643 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2644 (and buf (with-current-buffer buf
2645 (let* ((window (get-buffer-window buf 0))
2646 (start (window-start window))
2647 (p (window-point window))
2648 (buffer-read-only nil) (name) (value))
2649 (erase-buffer)
2650 (dolist (local locals-list)
2651 (setq name (car local))
2652 (setq value (nth 2 local))
2653 (if (or (not value)
2654 (string-match "\\0x" value))
2655 (add-text-properties 0 (length name)
2656 `(mouse-face highlight
2657 help-echo "mouse-2: create watch expression"
2658 local-map ,gdb-locals-watch-map)
2659 name)
2660 (add-text-properties 0 (length value)
2661 `(mouse-face highlight
2662 help-echo "mouse-2: edit value"
2663 local-map ,gdb-edit-locals-map-1)
2664 value))
2665 (insert
2666 (concat name "\t" (nth 1 local)
2667 "\t" (nth 2 local) "\n")))
2668 (set-window-start window start)
2669 (set-window-point window p)))))))
2670
2671 (defvar gdb-locals-header
2672 `(,(propertize "Locals"
2673 'help-echo "mouse-1: select"
2674 'mouse-face 'mode-line-highlight
2675 'face 'mode-line
2676 'local-map
2677 (gdb-make-header-line-mouse-map
2678 'mouse-1
2679 (lambda (event) (interactive "e")
2680 (save-selected-window
2681 (select-window (posn-window (event-start event)))
2682 (set-window-dedicated-p (selected-window) nil)
2683 (switch-to-buffer
2684 (gdb-get-buffer-create 'gdb-locals-buffer))
2685 (set-window-dedicated-p (selected-window) t)))))
2686 " "
2687 ,(propertize "Registers"
2688 'help-echo "mouse-1: select"
2689 'mouse-face 'mode-line-highlight
2690 'face 'mode-line
2691 'local-map
2692 (gdb-make-header-line-mouse-map
2693 'mouse-1
2694 (lambda (event) (interactive "e")
2695 (save-selected-window
2696 (select-window (posn-window (event-start event)))
2697 (set-window-dedicated-p (selected-window) nil)
2698 (switch-to-buffer
2699 (gdb-get-buffer-create 'gdb-registers-buffer))
2700 (set-window-dedicated-p (selected-window) t)))))))
2701
2702 (defvar gdb-locals-mode-map
2703 (let ((map (make-sparse-keymap)))
2704 (suppress-keymap map)
2705 (define-key map "q" 'kill-this-buffer)
2706 map))
2707
2708 (defun gdb-locals-mode ()
2709 "Major mode for gdb locals.
2710
2711 \\{gdb-locals-mode-map}"
2712 (kill-all-local-variables)
2713 (setq major-mode 'gdb-locals-mode)
2714 (setq mode-name (concat "Locals:" gdb-selected-frame))
2715 (setq buffer-read-only t)
2716 (buffer-disable-undo)
2717 (setq header-line-format gdb-locals-header)
2718 (use-local-map gdb-locals-mode-map)
2719 (set (make-local-variable 'font-lock-defaults)
2720 '(gdb-locals-font-lock-keywords))
2721 (run-mode-hooks 'gdb-locals-mode-hook)
2722 'gdb-invalidate-locals)
2723
2724 (defun gdb-locals-buffer-name ()
2725 (with-current-buffer gud-comint-buffer
2726 (concat "*locals of " (gdb-get-target-string) "*")))
2727
2728 (def-gdb-display-buffer
2729 gdb-display-locals-buffer
2730 'gdb-locals-buffer
2731 "Display local variables of current stack and their values.")
2732
2733 (def-gdb-frame-for-buffer
2734 gdb-frame-locals-buffer
2735 'gdb-locals-buffer
2736 "Display local variables of current stack and their values in a new frame.")
2737
2738 \f
2739 ;; Registers buffer.
2740 ;;
2741 (gdb-set-buffer-rules 'gdb-registers-buffer
2742 'gdb-registers-buffer-name
2743 'gdb-registers-mode)
2744
2745 (def-gdb-auto-update-trigger gdb-invalidate-registers
2746 (gdb-get-buffer 'gdb-registers-buffer)
2747 "-data-list-register-values x\n"
2748 gdb-data-list-register-values-handler)
2749
2750 (defconst gdb-data-list-register-values-regexp
2751 "number=\"\\(.*?\\)\",value=\"\\(.*?\\)\"")
2752
2753 (defun gdb-data-list-register-values-handler ()
2754 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers
2755 gdb-pending-triggers))
2756 (goto-char (point-min))
2757 (if (re-search-forward gdb-error-regexp nil t)
2758 (progn
2759 (let ((match nil))
2760 (setq match (match-string 1))
2761 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2762 (let ((buffer-read-only nil))
2763 (erase-buffer)
2764 (insert match)
2765 (goto-char (point-min))))))
2766 (let ((register-list (reverse gdb-register-names))
2767 (register nil) (register-string nil) (register-values nil))
2768 (goto-char (point-min))
2769 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
2770 (setq register (pop register-list))
2771 (setq register-string (concat register "\t" (match-string 2) "\n"))
2772 (if (member (match-string 1) gdb-changed-registers)
2773 (put-text-property 0 (length register-string)
2774 'face 'font-lock-warning-face
2775 register-string))
2776 (setq register-values
2777 (concat register-values register-string)))
2778 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
2779 (with-current-buffer buf
2780 (let ((p (window-point (get-buffer-window buf 0)))
2781 (buffer-read-only nil))
2782 (erase-buffer)
2783 (insert register-values)
2784 (set-window-point (get-buffer-window buf 0) p))))))
2785 (gdb-data-list-register-values-custom))
2786
2787 (defun gdb-data-list-register-values-custom ()
2788 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2789 (save-excursion
2790 (let ((buffer-read-only nil)
2791 bl)
2792 (goto-char (point-min))
2793 (while (< (point) (point-max))
2794 (setq bl (line-beginning-position))
2795 (when (looking-at "^[^\t]+")
2796 (put-text-property bl (match-end 0)
2797 'face font-lock-variable-name-face))
2798 (forward-line 1))))))
2799
2800 (defvar gdb-registers-mode-map
2801 (let ((map (make-sparse-keymap)))
2802 (suppress-keymap map)
2803 (define-key map "q" 'kill-this-buffer)
2804 map))
2805
2806 (defun gdb-registers-mode ()
2807 "Major mode for gdb registers.
2808
2809 \\{gdb-registers-mode-map}"
2810 (kill-all-local-variables)
2811 (setq major-mode 'gdb-registers-mode)
2812 (setq mode-name "Registers")
2813 (setq header-line-format gdb-locals-header)
2814 (setq buffer-read-only t)
2815 (buffer-disable-undo)
2816 (use-local-map gdb-registers-mode-map)
2817 (run-mode-hooks 'gdb-registers-mode-hook)
2818 'gdb-invalidate-registers)
2819
2820 (defun gdb-registers-buffer-name ()
2821 (with-current-buffer gud-comint-buffer
2822 (concat "*registers of " (gdb-get-target-string) "*")))
2823
2824 (def-gdb-display-buffer
2825 gdb-display-registers-buffer
2826 'gdb-registers-buffer
2827 "Display integer register contents.")
2828
2829 (def-gdb-frame-for-buffer
2830 gdb-frame-registers-buffer
2831 'gdb-registers-buffer
2832 "Display integer register contents in a new frame.")
2833
2834 ;; Needs GDB 6.4 onwards (used to fail with no stack).
2835 (defun gdb-get-changed-registers ()
2836 (if (and (gdb-get-buffer 'gdb-registers-buffer)
2837 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
2838 (progn
2839 (gdb-input
2840 (list
2841 "-data-list-changed-registers\n"
2842 'gdb-get-changed-registers-handler))
2843 (push 'gdb-get-changed-registers gdb-pending-triggers))))
2844
2845 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
2846
2847 (defun gdb-get-changed-registers-handler ()
2848 (setq gdb-pending-triggers
2849 (delq 'gdb-get-changed-registers gdb-pending-triggers))
2850 (setq gdb-changed-registers nil)
2851 (goto-char (point-min))
2852 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
2853 (push (match-string 1) gdb-changed-registers)))
2854
2855 (defun gdb-get-register-names ()
2856 "Create a list of register names."
2857 (goto-char (point-min))
2858 (setq gdb-register-names nil)
2859 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
2860 (push (match-string 1) gdb-register-names)))
2861 \f
2862
2863 (defun gdb-get-source-file-list ()
2864 "Create list of source files for current GDB session.
2865 If buffers already exist for any of these files, gud-minor-mode
2866 is set in them."
2867 (goto-char (point-min))
2868 (while (re-search-forward gdb-source-file-regexp nil t)
2869 (push (match-string 1) gdb-source-file-list))
2870 (dolist (buffer (buffer-list))
2871 (with-current-buffer buffer
2872 (when (member buffer-file-name gdb-source-file-list)
2873 (gdb-init-buffer))))
2874 (gdb-force-mode-line-update
2875 (propertize "ready" 'face font-lock-variable-name-face)))
2876
2877 (defun gdb-get-selected-frame ()
2878 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
2879 (progn
2880 (gdb-input
2881 (list "-stack-info-frame\n" 'gdb-frame-handler))
2882 (push 'gdb-get-selected-frame
2883 gdb-pending-triggers))))
2884
2885 (defun gdb-frame-handler ()
2886 (setq gdb-pending-triggers
2887 (delq 'gdb-get-selected-frame gdb-pending-triggers))
2888 (let ((frame (gdb-get-field (json-partial-output) 'frame)))
2889 (when frame
2890 (setq gdb-frame-number (gdb-get-field frame 'level))
2891 (setq gdb-pc-address (gdb-get-field frame 'addr))
2892 (setq gdb-selected-frame (gdb-get-field frame 'func))
2893 (setq gdb-selected-file (gdb-get-field frame 'fullname))
2894 (let ((line (gdb-get-field frame 'line)))
2895 (setq gdb-selected-line (or (and line (string-to-number line))
2896 nil)) ; don't fail if line is nil
2897 (when line ; obey the current file only if we have line info
2898 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
2899 (gud-display-frame)))
2900 (if (gdb-get-buffer 'gdb-locals-buffer)
2901 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2902 (setq mode-name (concat "Locals:" gdb-selected-frame))))
2903 (if (gdb-get-buffer 'gdb-disassembly-buffer)
2904 (with-current-buffer (gdb-get-buffer 'gdb-disassembly-buffer)
2905 (setq mode-name (concat "Disassembly:" gdb-selected-frame))))
2906 (if gud-overlay-arrow-position
2907 (let ((buffer (marker-buffer gud-overlay-arrow-position))
2908 (position (marker-position gud-overlay-arrow-position)))
2909 (when buffer
2910 (with-current-buffer buffer
2911 (setq fringe-indicator-alist
2912 (if (string-equal gdb-frame-number "0")
2913 nil
2914 '((overlay-arrow . hollow-right-triangle))))
2915 (setq gud-overlay-arrow-position (make-marker))
2916 (set-marker gud-overlay-arrow-position position)))))
2917 (when gdb-selected-line
2918 (gdb-invalidate-disassembly)))))
2919
2920 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
2921
2922 (defun gdb-get-prompt ()
2923 "Find prompt for GDB session."
2924 (goto-char (point-min))
2925 (setq gdb-prompt-name nil)
2926 (re-search-forward gdb-prompt-name-regexp nil t)
2927 (setq gdb-prompt-name (match-string 1))
2928 ;; Insert first prompt.
2929 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2930
2931 ;;;; Window management
2932 (defun gdb-display-buffer (buf dedicated &optional frame)
2933 (let ((answer (get-buffer-window buf (or frame 0))))
2934 (if answer
2935 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
2936 (let ((window (get-lru-window)))
2937 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
2938 'gdbmi)
2939 (let* ((largest (get-largest-window))
2940 (cur-size (window-height largest)))
2941 (setq answer (split-window largest))
2942 (set-window-buffer answer buf)
2943 (set-window-dedicated-p answer dedicated)
2944 answer)
2945 (set-window-buffer window buf)
2946 window)))))
2947
2948 \f
2949 ;;; Shared keymap initialization:
2950
2951 (let ((menu (make-sparse-keymap "GDB-Windows")))
2952 (define-key gud-menu-map [displays]
2953 `(menu-item "GDB-Windows" ,menu
2954 :visible (eq gud-minor-mode 'gdbmi)))
2955 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2956 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2957 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2958 (define-key menu [disassembly]
2959 '("Disassembly" . gdb-display-disassembly-buffer))
2960 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2961 (define-key menu [inferior]
2962 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2963 :enable gdb-use-separate-io-buffer))
2964 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2965 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
2966 (define-key menu [breakpoints]
2967 '("Breakpoints" . gdb-display-breakpoints-buffer)))
2968
2969 (let ((menu (make-sparse-keymap "GDB-Frames")))
2970 (define-key gud-menu-map [frames]
2971 `(menu-item "GDB-Frames" ,menu
2972 :visible (eq gud-minor-mode 'gdbmi)))
2973 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2974 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2975 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2976 (define-key menu [disassembly] '("Disassembly" . gdb-frame-disassembly-buffer))
2977 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2978 (define-key menu [inferior]
2979 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2980 :enable gdb-use-separate-io-buffer))
2981 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2982 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2983 (define-key menu [breakpoints]
2984 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
2985
2986 (let ((menu (make-sparse-keymap "GDB-MI")))
2987 (define-key gud-menu-map [mi]
2988 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi)))
2989 (define-key menu [gdb-customize]
2990 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
2991 :help "Customize Gdb Graphical Mode options."))
2992 (define-key menu [gdb-use-separate-io]
2993 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2994 :help "Toggle separate IO for debugged program."
2995 :button (:toggle . gdb-use-separate-io-buffer)))
2996 (define-key menu [gdb-many-windows]
2997 '(menu-item "Display Other Windows" gdb-many-windows
2998 :help "Toggle display of locals, stack and breakpoint information"
2999 :button (:toggle . gdb-many-windows)))
3000 (define-key menu [gdb-restore-windows]
3001 '(menu-item "Restore Window Layout" gdb-restore-windows
3002 :help "Restore standard layout for debug session.")))
3003
3004 (defun gdb-frame-gdb-buffer ()
3005 "Display GUD buffer in a new frame."
3006 (interactive)
3007 (let ((special-display-regexps (append special-display-regexps '(".*")))
3008 (special-display-frame-alist
3009 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
3010 gdb-frame-parameters)))
3011 (same-window-regexps nil))
3012 (display-buffer gud-comint-buffer)))
3013
3014 (defun gdb-display-gdb-buffer ()
3015 "Display GUD buffer."
3016 (interactive)
3017 (let ((same-window-regexps nil))
3018 (select-window (display-buffer gud-comint-buffer nil 0))))
3019
3020 (defun gdb-set-window-buffer (name)
3021 (set-window-buffer (selected-window) (get-buffer name))
3022 (set-window-dedicated-p (selected-window) t))
3023
3024 (defun gdb-setup-windows ()
3025 "Layout the window pattern for `gdb-many-windows'."
3026 (gdb-display-locals-buffer)
3027 (gdb-display-stack-buffer)
3028 (delete-other-windows)
3029 (gdb-display-breakpoints-buffer)
3030 (delete-other-windows)
3031 ; Don't dedicate.
3032 (pop-to-buffer gud-comint-buffer)
3033 (split-window nil ( / ( * (window-height) 3) 4))
3034 (split-window nil ( / (window-height) 3))
3035 (split-window-horizontally)
3036 (other-window 1)
3037 (gdb-set-window-buffer (gdb-locals-buffer-name))
3038 (other-window 1)
3039 (switch-to-buffer
3040 (if gud-last-last-frame
3041 (gud-find-file (car gud-last-last-frame))
3042 (if gdb-main-file
3043 (gud-find-file gdb-main-file)
3044 ;; Put buffer list in window if we
3045 ;; can't find a source file.
3046 (list-buffers-noselect))))
3047 (setq gdb-source-window (selected-window))
3048 (when gdb-use-separate-io-buffer
3049 (split-window-horizontally)
3050 (other-window 1)
3051 (gdb-set-window-buffer
3052 (gdb-get-buffer-create 'gdb-inferior-io)))
3053 (other-window 1)
3054 (gdb-set-window-buffer (gdb-stack-buffer-name))
3055 (split-window-horizontally)
3056 (other-window 1)
3057 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
3058 (other-window 1))
3059
3060 (defcustom gdb-many-windows nil
3061 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
3062 In this case it starts with two windows: one displaying the GUD
3063 buffer and the other with the source file with the main routine
3064 of the debugged program. Non-nil means display the layout shown for
3065 `gdb'."
3066 :type 'boolean
3067 :group 'gdb
3068 :version "22.1")
3069
3070 (defun gdb-many-windows (arg)
3071 "Toggle the number of windows in the basic arrangement.
3072 With arg, display additional buffers iff arg is positive."
3073 (interactive "P")
3074 (setq gdb-many-windows
3075 (if (null arg)
3076 (not gdb-many-windows)
3077 (> (prefix-numeric-value arg) 0)))
3078 (message (format "Display of other windows %sabled"
3079 (if gdb-many-windows "en" "dis")))
3080 (if (and gud-comint-buffer
3081 (buffer-name gud-comint-buffer))
3082 (condition-case nil
3083 (gdb-restore-windows)
3084 (error nil))))
3085
3086 (defun gdb-restore-windows ()
3087 "Restore the basic arrangement of windows used by gdb.
3088 This arrangement depends on the value of `gdb-many-windows'."
3089 (interactive)
3090 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
3091 (delete-other-windows)
3092 (if gdb-many-windows
3093 (gdb-setup-windows)
3094 (when (or gud-last-last-frame gdb-show-main)
3095 (split-window)
3096 (other-window 1)
3097 (switch-to-buffer
3098 (if gud-last-last-frame
3099 (gud-find-file (car gud-last-last-frame))
3100 (gud-find-file gdb-main-file)))
3101 (setq gdb-source-window (selected-window))
3102 (other-window 1))))
3103
3104 (defun gdb-reset ()
3105 "Exit a debugging session cleanly.
3106 Kills the gdb buffers, and resets variables and the source buffers."
3107 (dolist (buffer (buffer-list))
3108 (unless (eq buffer gud-comint-buffer)
3109 (with-current-buffer buffer
3110 (if (eq gud-minor-mode 'gdbmi)
3111 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
3112 (kill-buffer nil)
3113 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
3114 (setq gud-minor-mode nil)
3115 (kill-local-variable 'tool-bar-map)
3116 (kill-local-variable 'gdb-define-alist))))))
3117 (setq gdb-overlay-arrow-position nil)
3118 (setq overlay-arrow-variable-list
3119 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
3120 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
3121 (setq gdb-stack-position nil)
3122 (setq overlay-arrow-variable-list
3123 (delq 'gdb-stack-position overlay-arrow-variable-list))
3124 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
3125 (setq gud-running nil)
3126 (setq gdb-active-process nil)
3127 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
3128
3129 (defun gdb-get-source-file ()
3130 "Find the source file where the program starts and display it with related
3131 buffers, if required."
3132 (goto-char (point-min))
3133 (if (re-search-forward gdb-source-file-regexp nil t)
3134 (setq gdb-main-file (match-string 1)))
3135 (if gdb-many-windows
3136 (gdb-setup-windows)
3137 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
3138 (if gdb-show-main
3139 (let ((pop-up-windows t))
3140 (display-buffer (gud-find-file gdb-main-file))))))
3141
3142 ;;from put-image
3143 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3144 "Put string PUTSTRING in front of POS in the current buffer.
3145 PUTSTRING is displayed by putting an overlay into the current buffer with a
3146 `before-string' string that has a `display' property whose value is
3147 PUTSTRING."
3148 (let ((string (make-string 1 ?x))
3149 (buffer (current-buffer)))
3150 (setq putstring (copy-sequence putstring))
3151 (let ((overlay (make-overlay pos pos buffer))
3152 (prop (or dprop
3153 (list (list 'margin 'left-margin) putstring))))
3154 (put-text-property 0 1 'display prop string)
3155 (if sprops
3156 (add-text-properties 0 1 sprops string))
3157 (overlay-put overlay 'put-break t)
3158 (overlay-put overlay 'before-string string))))
3159
3160 ;;from remove-images
3161 (defun gdb-remove-strings (start end &optional buffer)
3162 "Remove strings between START and END in BUFFER.
3163 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3164 BUFFER nil or omitted means use the current buffer."
3165 (unless buffer
3166 (setq buffer (current-buffer)))
3167 (dolist (overlay (overlays-in start end))
3168 (when (overlay-get overlay 'put-break)
3169 (delete-overlay overlay))))
3170
3171 (defun gdb-put-breakpoint-icon (enabled bptno)
3172 (let ((start (- (line-beginning-position) 1))
3173 (end (+ (line-end-position) 1))
3174 (putstring (if enabled "B" "b"))
3175 (source-window (get-buffer-window (current-buffer) 0)))
3176 (add-text-properties
3177 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3178 putstring)
3179 (if enabled
3180 (add-text-properties
3181 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3182 (add-text-properties
3183 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3184 (gdb-remove-breakpoint-icons start end)
3185 (if (display-images-p)
3186 (if (>= (or left-fringe-width
3187 (if source-window (car (window-fringes source-window)))
3188 gdb-buffer-fringe-width) 8)
3189 (gdb-put-string
3190 nil (1+ start)
3191 `(left-fringe breakpoint
3192 ,(if enabled
3193 'breakpoint-enabled
3194 'breakpoint-disabled))
3195 'gdb-bptno bptno
3196 'gdb-enabled enabled)
3197 (when (< left-margin-width 2)
3198 (save-current-buffer
3199 (setq left-margin-width 2)
3200 (if source-window
3201 (set-window-margins
3202 source-window
3203 left-margin-width right-margin-width))))
3204 (put-image
3205 (if enabled
3206 (or breakpoint-enabled-icon
3207 (setq breakpoint-enabled-icon
3208 (find-image `((:type xpm :data
3209 ,breakpoint-xpm-data
3210 :ascent 100 :pointer hand)
3211 (:type pbm :data
3212 ,breakpoint-enabled-pbm-data
3213 :ascent 100 :pointer hand)))))
3214 (or breakpoint-disabled-icon
3215 (setq breakpoint-disabled-icon
3216 (find-image `((:type xpm :data
3217 ,breakpoint-xpm-data
3218 :conversion disabled
3219 :ascent 100 :pointer hand)
3220 (:type pbm :data
3221 ,breakpoint-disabled-pbm-data
3222 :ascent 100 :pointer hand))))))
3223 (+ start 1)
3224 putstring
3225 'left-margin))
3226 (when (< left-margin-width 2)
3227 (save-current-buffer
3228 (setq left-margin-width 2)
3229 (let ((window (get-buffer-window (current-buffer) 0)))
3230 (if window
3231 (set-window-margins
3232 window left-margin-width right-margin-width)))))
3233 (gdb-put-string
3234 (propertize putstring
3235 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3236 (1+ start)))))
3237
3238 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3239 (gdb-remove-strings start end)
3240 (if (display-images-p)
3241 (remove-images start end))
3242 (when remove-margin
3243 (setq left-margin-width 0)
3244 (let ((window (get-buffer-window (current-buffer) 0)))
3245 (if window
3246 (set-window-margins
3247 window left-margin-width right-margin-width)))))
3248
3249 (provide 'gdb-mi)
3250
3251 ;; arch-tag: 1b41ea2b-f364-4cec-8f35-e02e4fe01912
3252 ;;; gdb-mi.el ends here