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