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