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