Merge from emacs-24; up to 2012-12-27T08:21:08Z!rgm@gnu.org
[bpt/emacs.git] / lisp / progmodes / gdb-mi.el
1 ;;; gdb-mi.el --- User Interface for running GDB -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2007-2013 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 Nick Roberts following the general design
29 ;; used in gdb-ui.el for Emacs 22.1 - 23.1. It was further 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 runs gdb with
47 ;; GDB/MI (-interp=mi) and access CLI using "-interpreter-exec console
48 ;; cli-command". This code replaces gdb-ui.el and uses MI tokens instead
49 ;; of queues. Eventually MI should be asynchronous.
50
51 ;; Windows Platforms:
52
53 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
54 ;; explicitly in your program if you want timely display of I/O in Emacs.
55 ;; Alternatively you can make the output stream unbuffered, for example, by
56 ;; using a macro:
57
58 ;; #ifdef UNBUFFERED
59 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
60 ;; #endif
61
62 ;; and compiling with -DUNBUFFERED while debugging.
63
64 ;; If you are using Cygwin GDB and find that the source is not being displayed
65 ;; in Emacs when you step through it, possible solutions are to:
66
67 ;; 1) Use Cygwin X Windows and Cygwin Emacs.
68 ;; (Since 22.1 Emacs builds under Cygwin.)
69 ;; 2) Use MinGW GDB instead.
70 ;; 3) Use cygwin-mount.el
71
72 ;;; Mac OSX:
73
74 ;; GDB in Emacs on Mac OSX works best with FSF GDB as Apple have made
75 ;; some changes to the version that they include as part of Mac OSX.
76 ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
77 ;; as earlier versions do not compile on Mac OSX.
78
79 ;;; Known Bugs:
80
81 ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
82 ;; line information, e.g., a routine in libc (just a TODO item).
83
84 ;; TODO:
85 ;; 2) Watch windows to work with threads.
86 ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
87 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
88
89 ;;; Code:
90
91 (require 'gud)
92 (require 'json)
93 (require 'bindat)
94 (eval-when-compile (require 'cl-lib))
95
96 (declare-function speedbar-change-initial-expansion-list
97 "speedbar" (new-default))
98 (declare-function speedbar-timer-fn "speedbar" ())
99 (declare-function speedbar-line-text "speedbar" (&optional p))
100 (declare-function speedbar-change-expand-button-char "speedbar" (char))
101 (declare-function speedbar-delete-subblock "speedbar" (indent))
102 (declare-function speedbar-center-buffer-smartly "speedbar" ())
103
104 (defvar tool-bar-map)
105 (defvar speedbar-initial-expansion-list-name)
106 (defvar speedbar-frame)
107
108 (defvar gdb-memory-address "main")
109 (defvar gdb-memory-last-address nil
110 "Last successfully accessed memory address.")
111 (defvar gdb-memory-next-page nil
112 "Address of next memory page for program memory buffer.")
113 (defvar gdb-memory-prev-page nil
114 "Address of previous memory page for program memory buffer.")
115
116 (defvar gdb-thread-number nil
117 "Main current thread.
118
119 Invalidation triggers use this variable to query GDB for
120 information on the specified thread by wrapping GDB/MI commands
121 in `gdb-current-context-command'.
122
123 This variable may be updated implicitly by GDB via `gdb-stopped'
124 or explicitly by `gdb-select-thread'.
125
126 Only `gdb-setq-thread-number' should be used to change this
127 value.")
128
129 (defvar gdb-frame-number nil
130 "Selected frame level for main current thread.
131
132 Updated according to the following rules:
133
134 When a thread is selected or current thread stops, set to \"0\".
135
136 When current thread goes running (and possibly exits eventually),
137 set to nil.
138
139 May be manually changed by user with `gdb-select-frame'.")
140
141 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
142
143 ;; Used to show overlay arrow in source buffer. All set in
144 ;; gdb-get-main-selected-frame. Disassembly buffer should not use
145 ;; these but rely on buffer-local thread information instead.
146 (defvar gdb-selected-frame nil
147 "Name of selected function for main current thread.")
148 (defvar gdb-selected-file nil
149 "Name of selected file for main current thread.")
150 (defvar gdb-selected-line nil
151 "Number of selected line for main current thread.")
152
153 (defvar gdb-threads-list nil
154 "Associative list of threads provided by \"-thread-info\" MI command.
155
156 Keys are thread numbers (in strings) and values are structures as
157 returned from -thread-info by `gdb-json-partial-output'. Updated in
158 `gdb-thread-list-handler-custom'.")
159
160 (defvar gdb-running-threads-count nil
161 "Number of currently running threads.
162
163 If nil, no information is available.
164
165 Updated in `gdb-thread-list-handler-custom'.")
166
167 (defvar gdb-stopped-threads-count nil
168 "Number of currently stopped threads.
169
170 See also `gdb-running-threads-count'.")
171
172 (defvar gdb-breakpoints-list nil
173 "Associative list of breakpoints provided by \"-break-list\" MI command.
174
175 Keys are breakpoint numbers (in string) and values are structures
176 as returned from \"-break-list\" by `gdb-json-partial-output'
177 \(\"body\" field is used). Updated in
178 `gdb-breakpoints-list-handler-custom'.")
179
180 (defvar gdb-current-language nil)
181 (defvar gdb-var-list nil
182 "List of variables in watch window.
183 Each element has the form
184 (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS HAS_MORE FP)
185 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
186 address for root variables.")
187 (defvar gdb-main-file nil "Source file from which program execution begins.")
188
189 ;; Overlay arrow markers
190 (defvar gdb-stack-position nil)
191 (defvar gdb-thread-position nil)
192 (defvar gdb-disassembly-position nil)
193
194 (defvar gdb-location-alist nil
195 "Alist of breakpoint numbers and full filenames.
196 Only used for files that Emacs can't find.")
197 (defvar gdb-active-process nil
198 "GUD tooltips display variable values when t, and macro definitions otherwise.")
199 (defvar gdb-error "Non-nil when GDB is reporting an error.")
200 (defvar gdb-macro-info nil
201 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
202 (defvar gdb-register-names nil "List of register names.")
203 (defvar gdb-changed-registers nil
204 "List of changed register numbers (strings).")
205 (defvar gdb-buffer-fringe-width nil)
206 (defvar gdb-last-command nil)
207 (defvar gdb-prompt-name nil)
208 (defvar gdb-token-number 0)
209 (defvar gdb-handler-alist '())
210 (defvar gdb-handler-number nil)
211 (defvar gdb-source-file-list nil
212 "List of source files for the current executable.")
213 (defvar gdb-first-done-or-error t)
214 (defvar gdb-source-window nil)
215 (defvar gdb-inferior-status nil)
216 (defvar gdb-continuation nil)
217 (defvar gdb-supports-non-stop nil)
218 (defvar gdb-filter-output nil
219 "Message to be shown in GUD console.
220
221 This variable is updated in `gdb-done-or-error' and returned by
222 `gud-gdbmi-marker-filter'.")
223
224 (defvar gdb-non-stop nil
225 "Indicates whether current GDB session is using non-stop mode.
226
227 It is initialized to `gdb-non-stop-setting' at the beginning of
228 every GDB session.")
229
230 (defvar-local gdb-buffer-type nil
231 "One of the symbols bound in `gdb-buffer-rules'.")
232
233 (defvar gdb-output-sink 'nil
234 "The disposition of the output of the current gdb command.
235 Possible values are these symbols:
236
237 `user' -- gdb output should be copied to the GUD buffer
238 for the user to see.
239
240 `emacs' -- output should be collected in the partial-output-buffer
241 for subsequent processing by a command. This is the
242 disposition of output generated by commands that
243 gdb mode sends to gdb on its own behalf.")
244
245 ;; Pending triggers prevent congestion: Emacs won't send two similar
246 ;; consecutive requests.
247
248 (defvar gdb-pending-triggers '()
249 "A list of trigger functions which have not yet been handled.
250
251 Elements are either function names or pairs (buffer . function)")
252
253 (defmacro gdb-add-pending (item)
254 `(push ,item gdb-pending-triggers))
255 (defmacro gdb-pending-p (item)
256 `(member ,item gdb-pending-triggers))
257 (defmacro gdb-delete-pending (item)
258 `(setq gdb-pending-triggers
259 (delete ,item gdb-pending-triggers)))
260
261 (defmacro gdb-wait-for-pending (&rest body)
262 "Wait until `gdb-pending-triggers' is empty and evaluate FORM.
263
264 This function checks `gdb-pending-triggers' value every
265 `gdb-wait-for-pending' seconds."
266 (run-with-timer
267 0.5 nil
268 `(lambda ()
269 (if (not gdb-pending-triggers)
270 (progn ,@body)
271 (gdb-wait-for-pending ,@body)))))
272
273 ;; Publish-subscribe
274
275 (defmacro gdb-add-subscriber (publisher subscriber)
276 "Register new PUBLISHER's SUBSCRIBER.
277
278 SUBSCRIBER must be a pair, where cdr is a function of one
279 argument (see `gdb-emit-signal')."
280 `(add-to-list ',publisher ,subscriber t))
281
282 (defmacro gdb-delete-subscriber (publisher subscriber)
283 "Unregister SUBSCRIBER from PUBLISHER."
284 `(setq ,publisher (delete ,subscriber
285 ,publisher)))
286
287 (defun gdb-get-subscribers (publisher)
288 publisher)
289
290 (defun gdb-emit-signal (publisher &optional signal)
291 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
292 (dolist (subscriber (gdb-get-subscribers publisher))
293 (funcall (cdr subscriber) signal)))
294
295 (defvar gdb-buf-publisher '()
296 "Used to invalidate GDB buffers by emitting a signal in `gdb-update'.
297 Must be a list of pairs with cars being buffers and cdr's being
298 valid signal handlers.")
299
300 (defgroup gdb nil
301 "GDB graphical interface"
302 :group 'tools
303 :link '(info-link "(emacs)GDB Graphical Interface")
304 :version "23.2")
305
306 (defgroup gdb-non-stop nil
307 "GDB non-stop debugging settings"
308 :group 'gdb
309 :version "23.2")
310
311 (defgroup gdb-buffers nil
312 "GDB buffers"
313 :group 'gdb
314 :version "23.2")
315
316 (defcustom gdb-debug-log-max 128
317 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
318 :group 'gdb
319 :type '(choice (integer :tag "Number of elements")
320 (const :tag "Unlimited" nil))
321 :version "22.1")
322
323 (defcustom gdb-non-stop-setting t
324 "When in non-stop mode, stopped threads can be examined while
325 other threads continue to execute.
326
327 GDB session needs to be restarted for this setting to take effect."
328 :type 'boolean
329 :group 'gdb-non-stop
330 :version "23.2")
331
332 ;; TODO Some commands can't be called with --all (give a notice about
333 ;; it in setting doc)
334 (defcustom gdb-gud-control-all-threads t
335 "When non-nil, GUD execution commands affect all threads when
336 in non-stop mode. Otherwise, only current thread is affected."
337 :type 'boolean
338 :group 'gdb-non-stop
339 :version "23.2")
340
341 (defcustom gdb-switch-reasons t
342 "List of stop reasons for which Emacs should switch thread.
343 When t, switch to stopped thread no matter what the reason was.
344 When nil, never switch to stopped thread automatically.
345
346 This setting is used in non-stop mode only. In all-stop mode,
347 Emacs always switches to the thread which caused the stop."
348 ;; exited, exited-normally and exited-signaled are not
349 ;; thread-specific stop reasons and therefore are not included in
350 ;; this list
351 :type '(choice
352 (const :tag "All reasons" t)
353 (set :tag "Selection of reasons..."
354 (const :tag "A breakpoint was reached." "breakpoint-hit")
355 (const :tag "A watchpoint was triggered." "watchpoint-trigger")
356 (const :tag "A read watchpoint was triggered."
357 "read-watchpoint-trigger")
358 (const :tag "An access watchpoint was triggered."
359 "access-watchpoint-trigger")
360 (const :tag "Function finished execution." "function-finished")
361 (const :tag "Location reached." "location-reached")
362 (const :tag "Watchpoint has gone out of scope"
363 "watchpoint-scope")
364 (const :tag "End of stepping range reached."
365 "end-stepping-range")
366 (const :tag "Signal received (like interruption)."
367 "signal-received"))
368 (const :tag "None" nil))
369 :group 'gdb-non-stop
370 :version "23.2"
371 :link '(info-link "(gdb)GDB/MI Async Records"))
372
373 (defcustom gdb-stopped-functions nil
374 "List of functions called whenever GDB stops.
375
376 Each function takes one argument, a parsed MI response, which
377 contains fields of corresponding MI *stopped async record:
378
379 ((stopped-threads . \"all\")
380 (thread-id . \"1\")
381 (frame (line . \"38\")
382 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
383 (file . \"server.c\")
384 (args ((value . \"0x804b038\")
385 (name . \"arg\")))
386 (func . \"hello\")
387 (addr . \"0x0804869e\"))
388 (reason . \"end-stepping-range\"))
389
390 Note that \"reason\" is only present in non-stop debugging mode.
391
392 `bindat-get-field' may be used to access the fields of response.
393
394 Each function is called after the new current thread was selected
395 and GDB buffers were updated in `gdb-stopped'."
396 :type '(repeat function)
397 :group 'gdb
398 :version "23.2"
399 :link '(info-link "(gdb)GDB/MI Async Records"))
400
401 (defcustom gdb-switch-when-another-stopped t
402 "When nil, don't switch to stopped thread if some other
403 stopped thread is already selected."
404 :type 'boolean
405 :group 'gdb-non-stop
406 :version "23.2")
407
408 (defcustom gdb-stack-buffer-locations t
409 "Show file information or library names in stack buffers."
410 :type 'boolean
411 :group 'gdb-buffers
412 :version "23.2")
413
414 (defcustom gdb-stack-buffer-addresses nil
415 "Show frame addresses in stack buffers."
416 :type 'boolean
417 :group 'gdb-buffers
418 :version "23.2")
419
420 (defcustom gdb-thread-buffer-verbose-names t
421 "Show long thread names in threads buffer."
422 :type 'boolean
423 :group 'gdb-buffers
424 :version "23.2")
425
426 (defcustom gdb-thread-buffer-arguments t
427 "Show function arguments in threads buffer."
428 :type 'boolean
429 :group 'gdb-buffers
430 :version "23.2")
431
432 (defcustom gdb-thread-buffer-locations t
433 "Show file information or library names in threads buffer."
434 :type 'boolean
435 :group 'gdb-buffers
436 :version "23.2")
437
438 (defcustom gdb-thread-buffer-addresses nil
439 "Show addresses for thread frames in threads buffer."
440 :type 'boolean
441 :group 'gdb-buffers
442 :version "23.2")
443
444 (defcustom gdb-show-threads-by-default nil
445 "Show threads list buffer instead of breakpoints list by default."
446 :type 'boolean
447 :group 'gdb-buffers
448 :version "23.2")
449
450 (defvar gdb-debug-log nil
451 "List of commands sent to and replies received from GDB.
452 Most recent commands are listed first. This list stores only the last
453 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
454
455 ;;;###autoload
456 (define-minor-mode gdb-enable-debug
457 "Toggle logging of transaction between Emacs and Gdb.
458 The log is stored in `gdb-debug-log' as an alist with elements
459 whose cons is send, send-item or recv and whose cdr is the string
460 being transferred. This list may grow up to a size of
461 `gdb-debug-log-max' after which the oldest element (at the end of
462 the list) is deleted every time a new one is added (at the front)."
463 :global t
464 :group 'gdb
465 :version "22.1")
466
467 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
468 "Shell command for generating a list of defined macros in a source file.
469 This list is used to display the #define directive associated
470 with an identifier as a tooltip. It works in a debug session with
471 GDB, when `gud-tooltip-mode' is t.
472
473 Set `gdb-cpp-define-alist-flags' for any include paths or
474 predefined macros."
475 :type 'string
476 :group 'gdb
477 :version "22.1")
478
479 (defcustom gdb-cpp-define-alist-flags ""
480 "Preprocessor flags for `gdb-cpp-define-alist-program'."
481 :type 'string
482 :group 'gdb
483 :version "22.1")
484
485 (defcustom gdb-create-source-file-list t
486 "Non-nil means create a list of files from which the executable was built.
487 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
488 line for a long time when starting, possibly because your executable was
489 built from a large number of files. This allows quicker initialization
490 but means that these files are not automatically enabled for debugging,
491 e.g., you won't be able to click in the fringe to set a breakpoint until
492 execution has already stopped there."
493 :type 'boolean
494 :group 'gdb
495 :version "23.1")
496
497 (defcustom gdb-show-main nil
498 "Non-nil means display source file containing the main routine at startup.
499 Also display the main routine in the disassembly buffer if present."
500 :type 'boolean
501 :group 'gdb
502 :version "22.1")
503
504 (defvar gdbmi-debug-mode nil
505 "When non-nil, print the messages sent/received from GDB/MI in *Messages*.")
506
507 (defun gdb-force-mode-line-update (status)
508 (let ((buffer gud-comint-buffer))
509 (if (and buffer (buffer-name buffer))
510 (with-current-buffer buffer
511 (setq mode-line-process
512 (format ":%s [%s]"
513 (process-status (get-buffer-process buffer)) status))
514 ;; Force mode line redisplay soon.
515 (force-mode-line-update)))))
516
517 ;; These two are used for menu and toolbar
518 (defun gdb-control-all-threads ()
519 "Switch to non-stop/A mode."
520 (interactive)
521 (setq gdb-gud-control-all-threads t)
522 ;; Actually forcing the tool-bar to update.
523 (force-mode-line-update)
524 (message "Now in non-stop/A mode."))
525
526 (defun gdb-control-current-thread ()
527 "Switch to non-stop/T mode."
528 (interactive)
529 (setq gdb-gud-control-all-threads nil)
530 ;; Actually forcing the tool-bar to update.
531 (force-mode-line-update)
532 (message "Now in non-stop/T mode."))
533
534 (defun gdb-find-watch-expression ()
535 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
536 (varnum (car var)) expr)
537 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
538 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
539 (component-list (split-string (match-string 2 varnum) "\\." t)))
540 (setq expr (nth 1 var1))
541 (setq varnumlet (car var1))
542 (dolist (component component-list)
543 (setq var2 (assoc varnumlet gdb-var-list))
544 (setq expr (concat expr
545 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
546 (concat "[" component "]")
547 (concat "." component))))
548 (setq varnumlet (concat varnumlet "." component)))
549 expr)))
550
551 ;; noall is used for commands which don't take --all, but only
552 ;; --thread.
553 (defun gdb-gud-context-command (command &optional noall)
554 "When `gdb-non-stop' is t, add --thread option to COMMAND if
555 `gdb-gud-control-all-threads' is nil and --all option otherwise.
556 If NOALL is t, always add --thread option no matter what
557 `gdb-gud-control-all-threads' value is.
558
559 When `gdb-non-stop' is nil, return COMMAND unchanged."
560 (if gdb-non-stop
561 (if (and gdb-gud-control-all-threads
562 (not noall)
563 gdb-supports-non-stop)
564 (concat command " --all ")
565 (gdb-current-context-command command))
566 command))
567
568 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
569 "`gud-call' wrapper which adds --thread/--all options between
570 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
571
572 NOARG must be t when this macro is used outside `gud-def'"
573 `(gud-call
574 (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
575 ,(when (not noarg) 'arg)))
576
577 (defun gdb--check-interpreter (filter proc string)
578 (unless (zerop (length string))
579 (remove-function (process-filter proc) #'gdb--check-interpreter)
580 (unless (memq (aref string 0) '(?^ ?~ ?@ ?& ?* ?=))
581 ;; Apparently we're not running with -i=mi.
582 (let ((msg "Error: you did not specify -i=mi on GDB's command line!"))
583 (message msg)
584 (setq string (concat (propertize msg 'font-lock-face 'error)
585 "\n" string)))
586 ;; Use the old gud-gbd filter, not because it works, but because it
587 ;; will properly display GDB's answers rather than hanging waiting for
588 ;; answers that aren't coming.
589 (set (make-local-variable 'gud-marker-filter) #'gud-gdb-marker-filter))
590 (funcall filter proc string)))
591
592 (defvar gdb-control-level 0)
593
594 ;;;###autoload
595 (defun gdb (command-line)
596 "Run gdb on program FILE in buffer *gud-FILE*.
597 The directory containing FILE becomes the initial working directory
598 and source-file directory for your debugger.
599
600 COMMAND-LINE is the shell command for starting the gdb session.
601 It should be a string consisting of the name of the gdb
602 executable followed by command line options. The command line
603 options should include \"-i=mi\" to use gdb's MI text interface.
604 Note that the old \"--annotate\" option is no longer supported.
605
606 If option `gdb-many-windows' is nil (the default value) then gdb just
607 pops up the GUD buffer unless `gdb-show-main' is t. In this case
608 it starts with two windows: one displaying the GUD buffer and the
609 other with the source file with the main routine of the inferior.
610
611 If option `gdb-many-windows' is t, regardless of the value of
612 `gdb-show-main', the layout below will appear. Keybindings are
613 shown in some of the buffers.
614
615 Watch expressions appear in the speedbar/slowbar.
616
617 The following commands help control operation :
618
619 `gdb-many-windows' - Toggle the number of windows gdb uses.
620 `gdb-restore-windows' - To restore the window layout.
621
622 See Info node `(emacs)GDB Graphical Interface' for a more
623 detailed description of this mode.
624
625
626 +----------------------------------------------------------------------+
627 | GDB Toolbar |
628 +-----------------------------------+----------------------------------+
629 | GUD buffer (I/O of GDB) | Locals buffer |
630 | | |
631 | | |
632 | | |
633 +-----------------------------------+----------------------------------+
634 | Source buffer | I/O buffer (of debugged program) |
635 | | (comint-mode) |
636 | | |
637 | | |
638 | | |
639 | | |
640 | | |
641 | | |
642 +-----------------------------------+----------------------------------+
643 | Stack buffer | Breakpoints buffer |
644 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
645 | | RET gdb-goto-breakpoint |
646 | | D gdb-delete-breakpoint |
647 +-----------------------------------+----------------------------------+"
648 ;;
649 (interactive (list (gud-query-cmdline 'gdb)))
650
651 (when (and gud-comint-buffer
652 (buffer-name gud-comint-buffer)
653 (get-buffer-process gud-comint-buffer)
654 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
655 (gdb-restore-windows)
656 (error
657 "Multiple debugging requires restarting in text command mode"))
658 ;;
659 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
660
661 ;; Setup a temporary process filter to warn when GDB was not started
662 ;; with -i=mi.
663 (let ((proc (get-buffer-process gud-comint-buffer)))
664 (add-function :around (process-filter proc) #'gdb--check-interpreter))
665
666 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
667 (set (make-local-variable 'gdb-control-level) 0)
668 (setq comint-input-sender 'gdb-send)
669 (when (ring-empty-p comint-input-ring) ; cf shell-mode
670 (let ((hfile (expand-file-name (or (getenv "GDBHISTFILE")
671 (if (eq system-type 'ms-dos)
672 "_gdb_history"
673 ".gdb_history"))))
674 ;; gdb defaults to 256, but we'll default to comint-input-ring-size.
675 (hsize (getenv "HISTSIZE")))
676 (dolist (file (append '("~/.gdbinit")
677 (unless (string-equal (expand-file-name ".")
678 (expand-file-name "~"))
679 '(".gdbinit"))))
680 (if (file-readable-p (setq file (expand-file-name file)))
681 (with-temp-buffer
682 (insert-file-contents file)
683 ;; TODO? check for "set history save\\( *on\\)?" and do
684 ;; not use history otherwise?
685 (while (re-search-forward
686 "^ *set history \\(filename\\|size\\) *\\(.*\\)" nil t)
687 (cond ((string-equal (match-string 1) "filename")
688 (setq hfile (expand-file-name
689 (match-string 2)
690 (file-name-directory file))))
691 ((string-equal (match-string 1) "size")
692 (setq hsize (match-string 2))))))))
693 (and (stringp hsize)
694 (integerp (setq hsize (string-to-number hsize)))
695 (> hsize 0)
696 (set (make-local-variable 'comint-input-ring-size) hsize))
697 (if (stringp hfile)
698 (set (make-local-variable 'comint-input-ring-file-name) hfile))
699 (comint-read-input-ring t)))
700 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
701 "Set temporary breakpoint at current line.")
702 (gud-def gud-jump
703 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
704 "\C-j" "Set execution address to current line.")
705
706 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
707 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
708 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
709 (gud-def gud-pstar "print* %e" nil
710 "Evaluate C dereferenced pointer expression at point.")
711
712 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
713 "\C-s"
714 "Step one source line with display.")
715 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
716 "\C-i"
717 "Step one instruction with display.")
718 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
719 "\C-n"
720 "Step one line (skip functions).")
721 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
722 nil
723 "Step one instruction (skip functions).")
724 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
725 "\C-r"
726 "Continue with display.")
727 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
728 "\C-f"
729 "Finish executing current function.")
730 (gud-def gud-run "-exec-run"
731 nil
732 "Run the program.")
733
734 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
735 (gud-call "break %f:%l" arg)
736 (save-excursion
737 (beginning-of-line)
738 (forward-char 2)
739 (gud-call "break *%a" arg)))
740 "\C-b" "Set breakpoint at current line or address.")
741
742 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
743 (gud-call "clear %f:%l" arg)
744 (save-excursion
745 (beginning-of-line)
746 (forward-char 2)
747 (gud-call "clear *%a" arg)))
748 "\C-d" "Remove breakpoint at current line or address.")
749
750 ;; -exec-until doesn't support --all yet
751 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
752 (gud-call "-exec-until %f:%l" arg)
753 (save-excursion
754 (beginning-of-line)
755 (forward-char 2)
756 (gud-call "-exec-until *%a" arg)))
757 "\C-u" "Continue to current line or address.")
758 ;; TODO Why arg here?
759 (gud-def
760 gud-go (gud-call (if gdb-active-process
761 (gdb-gud-context-command "-exec-continue")
762 "-exec-run") arg)
763 nil "Start or continue execution.")
764
765 ;; For debugging Emacs only.
766 (gud-def gud-pp
767 (gud-call
768 (concat
769 "pp " (if (eq (buffer-local-value
770 'major-mode (window-buffer)) 'speedbar-mode)
771 (gdb-find-watch-expression) "%e")) arg)
772 nil "Print the Emacs s-expression.")
773
774 (define-key gud-minor-mode-map [left-margin mouse-1]
775 'gdb-mouse-set-clear-breakpoint)
776 (define-key gud-minor-mode-map [left-fringe mouse-1]
777 'gdb-mouse-set-clear-breakpoint)
778 (define-key gud-minor-mode-map [left-margin C-mouse-1]
779 'gdb-mouse-toggle-breakpoint-margin)
780 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
781 'gdb-mouse-toggle-breakpoint-fringe)
782
783 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
784 'gdb-mouse-until)
785 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
786 'gdb-mouse-until)
787 (define-key gud-minor-mode-map [left-margin mouse-3]
788 'gdb-mouse-until)
789 (define-key gud-minor-mode-map [left-fringe mouse-3]
790 'gdb-mouse-until)
791
792 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
793 'gdb-mouse-jump)
794 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
795 'gdb-mouse-jump)
796 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
797 'gdb-mouse-jump)
798 (define-key gud-minor-mode-map [left-margin C-mouse-3]
799 'gdb-mouse-jump)
800
801 (set (make-local-variable 'gud-gdb-completion-function)
802 'gud-gdbmi-completions)
803
804 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
805 nil 'local)
806 (local-set-key "\C-i" 'completion-at-point)
807
808 (local-set-key [remap comint-delchar-or-maybe-eof] 'gdb-delchar-or-quit)
809
810 (setq gdb-first-prompt t)
811 (setq gud-running nil)
812
813 (gdb-update)
814
815 (run-hooks 'gdb-mode-hook))
816
817 (defun gdb-init-1 ()
818 ;; (Re-)initialize.
819 (setq gdb-selected-frame nil
820 gdb-frame-number nil
821 gdb-thread-number nil
822 gdb-var-list nil
823 gdb-pending-triggers nil
824 gdb-output-sink 'user
825 gdb-location-alist nil
826 gdb-source-file-list nil
827 gdb-last-command nil
828 gdb-token-number 0
829 gdb-handler-alist '()
830 gdb-handler-number nil
831 gdb-prompt-name nil
832 gdb-first-done-or-error t
833 gdb-buffer-fringe-width (car (window-fringes))
834 gdb-debug-log nil
835 gdb-source-window nil
836 gdb-inferior-status nil
837 gdb-continuation nil
838 gdb-buf-publisher '()
839 gdb-threads-list '()
840 gdb-breakpoints-list '()
841 gdb-register-names '()
842 gdb-non-stop gdb-non-stop-setting)
843 ;;
844 (gdbmi-bnf-init)
845 ;;
846 (setq gdb-buffer-type 'gdbmi)
847 ;;
848 (gdb-force-mode-line-update
849 (propertize "initializing..." 'face font-lock-variable-name-face))
850
851 (gdb-get-buffer-create 'gdb-inferior-io)
852 (gdb-clear-inferior-io)
853 (gdb-inferior-io--init-proc (get-process "gdb-inferior"))
854
855 (when (eq system-type 'windows-nt)
856 ;; Don't create a separate console window for the debuggee.
857 (gdb-input "-gdb-set new-console off" 'ignore)
858 ;; Force GDB to behave as if its input and output stream were
859 ;; connected to a TTY device (since on Windows we use pipes for
860 ;; communicating with GDB).
861 (gdb-input "-gdb-set interactive-mode on" 'ignore))
862 (gdb-input "-gdb-set height 0" 'ignore)
863
864 (when gdb-non-stop
865 (gdb-input "-gdb-set non-stop 1" 'gdb-non-stop-handler))
866
867 (gdb-input "-enable-pretty-printing" 'ignore)
868
869 ;; Find source file and compilation directory here.
870 (if gdb-create-source-file-list
871 ;; Needs GDB 6.2 onwards.
872 (gdb-input "-file-list-exec-source-files" 'gdb-get-source-file-list))
873 ;; Needs GDB 6.0 onwards.
874 (gdb-input "-file-list-exec-source-file" 'gdb-get-source-file)
875 (gdb-input "-gdb-show prompt" 'gdb-get-prompt))
876
877 (defun gdb-non-stop-handler ()
878 (goto-char (point-min))
879 (if (re-search-forward "No symbol" nil t)
880 (progn
881 (message
882 "This version of GDB doesn't support non-stop mode. Turning it off.")
883 (setq gdb-non-stop nil)
884 (setq gdb-supports-non-stop nil))
885 (setq gdb-supports-non-stop t)
886 (gdb-input "-gdb-set target-async 1" 'ignore)
887 (gdb-input "-list-target-features" 'gdb-check-target-async)))
888
889 (defun gdb-check-target-async ()
890 (goto-char (point-min))
891 (unless (re-search-forward "async" nil t)
892 (message
893 "Target doesn't support non-stop mode. Turning it off.")
894 (setq gdb-non-stop nil)
895 (gdb-input "-gdb-set non-stop 0" 'ignore)))
896
897 (defun gdb-delchar-or-quit (arg)
898 "Delete ARG characters or send a quit command to GDB.
899 Send a quit only if point is at the end of the buffer, there is
900 no input, and GDB is waiting for input."
901 (interactive "p")
902 (unless (and (eq (current-buffer) gud-comint-buffer)
903 (eq gud-minor-mode 'gdbmi))
904 (error "Not in a GDB-MI buffer"))
905 (let ((proc (get-buffer-process gud-comint-buffer)))
906 (if (and (eobp) proc (process-live-p proc)
907 (not gud-running)
908 (= (point) (marker-position (process-mark proc))))
909 ;; Sending an EOF does not work with GDB-MI; submit an
910 ;; explicit quit command.
911 (progn
912 (insert "quit")
913 (comint-send-input t t))
914 (delete-char arg))))
915
916 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
917
918 (defun gdb-create-define-alist ()
919 "Create an alist of #define directives for GUD tooltips."
920 (let* ((file (buffer-file-name))
921 (output
922 (with-output-to-string
923 (with-current-buffer standard-output
924 (and file
925 (file-exists-p file)
926 ;; call-process doesn't work with remote file names.
927 (not (file-remote-p default-directory))
928 (call-process shell-file-name file
929 (list t nil) nil "-c"
930 (concat gdb-cpp-define-alist-program " "
931 gdb-cpp-define-alist-flags))))))
932 (define-list (split-string output "\n" t))
933 (name))
934 (setq gdb-define-alist nil)
935 (dolist (define define-list)
936 (setq name (nth 1 (split-string define "[( ]")))
937 (push (cons name define) gdb-define-alist))))
938
939 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
940
941 (defun gdb-tooltip-print (expr)
942 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
943 (goto-char (point-min))
944 (cond
945 ((re-search-forward ".*value=\\(\".*\"\\)" nil t)
946 (tooltip-show
947 (concat expr " = " (read (match-string 1)))
948 (or gud-tooltip-echo-area
949 (not (display-graphic-p)))))
950 ((re-search-forward "msg=\\(\".+\"\\)$" nil t)
951 (tooltip-show (read (match-string 1))
952 (or gud-tooltip-echo-area
953 (not (display-graphic-p))))))))
954
955 ;; If expr is a macro for a function don't print because of possible dangerous
956 ;; side-effects. Also printing a function within a tooltip generates an
957 ;; unexpected starting annotation (phase error).
958 (defun gdb-tooltip-print-1 (expr)
959 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
960 (goto-char (point-min))
961 (if (search-forward "expands to: " nil t)
962 (unless (looking-at "\\S-+.*(.*).*")
963 (gdb-input (concat "-data-evaluate-expression \"" expr "\"")
964 `(lambda () (gdb-tooltip-print ,expr)))))))
965
966 (defun gdb-init-buffer ()
967 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
968 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
969 (when gud-tooltip-mode
970 (make-local-variable 'gdb-define-alist)
971 (gdb-create-define-alist)
972 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
973
974 (defmacro gdb--if-arrow (arrow-position start-posn end-posn &rest body)
975 (declare (indent 3))
976 (let ((buffer (make-symbol "buffer")))
977 `(if ,arrow-position
978 (let ((,buffer (marker-buffer ,arrow-position)))
979 (if (equal ,buffer (window-buffer (posn-window ,end-posn)))
980 (with-current-buffer ,buffer
981 (when (or (equal ,start-posn ,end-posn)
982 (equal (posn-point ,start-posn)
983 (marker-position ,arrow-position)))
984 ,@body)))))))
985
986 (defun gdb-mouse-until (event)
987 "Continue running until a source line past the current line.
988 The destination source line can be selected either by clicking
989 with mouse-3 on the fringe/margin or dragging the arrow
990 with mouse-1 (default bindings)."
991 (interactive "e")
992 (let ((start (event-start event))
993 (end (event-end event)))
994 (gdb--if-arrow gud-overlay-arrow-position start end
995 (let ((line (line-number-at-pos (posn-point end))))
996 (gud-call (concat "until " (number-to-string line)))))
997 (gdb--if-arrow gdb-disassembly-position start end
998 (save-excursion
999 (goto-char (point-min))
1000 (forward-line (1- (line-number-at-pos (posn-point end))))
1001 (forward-char 2)
1002 (gud-call (concat "until *%a"))))))
1003
1004 (defun gdb-mouse-jump (event)
1005 "Set execution address/line.
1006 The destination source line can be selected either by clicking with C-mouse-3
1007 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
1008 Unlike `gdb-mouse-until' the destination address can be before the current
1009 line, and no execution takes place."
1010 (interactive "e")
1011 (let ((start (event-start event))
1012 (end (event-end event)))
1013 (gdb--if-arrow gud-overlay-arrow-position start end
1014 (let ((line (line-number-at-pos (posn-point end))))
1015 (gud-call (concat "tbreak " (number-to-string line)))
1016 (gud-call (concat "jump " (number-to-string line)))))
1017 (gdb--if-arrow gdb-disassembly-position start end
1018 (save-excursion
1019 (goto-char (point-min))
1020 (forward-line (1- (line-number-at-pos (posn-point end))))
1021 (forward-char 2)
1022 (gud-call (concat "tbreak *%a"))
1023 (gud-call (concat "jump *%a"))))))
1024
1025 (defcustom gdb-show-changed-values t
1026 "If non-nil change the face of out of scope variables and changed values.
1027 Out of scope variables are suppressed with `shadow' face.
1028 Changed values are highlighted with the face `font-lock-warning-face'."
1029 :type 'boolean
1030 :group 'gdb
1031 :version "22.1")
1032
1033 (defcustom gdb-max-children 40
1034 "Maximum number of children before expansion requires confirmation."
1035 :type 'integer
1036 :group 'gdb
1037 :version "22.1")
1038
1039 (defcustom gdb-delete-out-of-scope t
1040 "If non-nil delete watch expressions automatically when they go out of scope."
1041 :type 'boolean
1042 :group 'gdb
1043 :version "22.2")
1044
1045 (define-minor-mode gdb-speedbar-auto-raise
1046 "Minor mode to automatically raise the speedbar for watch expressions.
1047 With prefix argument ARG, automatically raise speedbar if ARG is
1048 positive, otherwise don't automatically raise it."
1049 :global t
1050 :group 'gdb
1051 :version "22.1")
1052
1053 (defcustom gdb-use-colon-colon-notation nil
1054 "If non-nil use FUN::VAR format to display variables in the speedbar."
1055 :type 'boolean
1056 :group 'gdb
1057 :version "22.1")
1058
1059 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
1060 (define-key global-map (vconcat gud-key-prefix "\C-w") 'gud-watch)
1061
1062 (declare-function tooltip-identifier-from-point "tooltip" (point))
1063
1064 (defun gud-watch (&optional arg event)
1065 "Watch expression at point.
1066 With arg, enter name of variable to be watched in the minibuffer."
1067 (interactive (list current-prefix-arg last-input-event))
1068 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
1069 (if (eq minor-mode 'gdbmi)
1070 (progn
1071 (if event (posn-set-point (event-end event)))
1072 (require 'tooltip)
1073 (save-selected-window
1074 (let ((expr
1075 (if arg
1076 (completing-read "Name of variable: "
1077 'gud-gdb-complete-command)
1078 (if (and transient-mark-mode mark-active)
1079 (buffer-substring (region-beginning) (region-end))
1080 (concat (if (derived-mode-p 'gdb-registers-mode) "$")
1081 (tooltip-identifier-from-point (point)))))))
1082 (set-text-properties 0 (length expr) nil expr)
1083 (gdb-input (concat "-var-create - * " expr "")
1084 `(lambda () (gdb-var-create-handler ,expr))))))
1085 (message "gud-watch is a no-op in this mode."))))
1086
1087 (defun gdb-var-create-handler (expr)
1088 (let* ((result (gdb-json-partial-output)))
1089 (if (not (bindat-get-field result 'msg))
1090 (let ((var
1091 (list (bindat-get-field result 'name)
1092 (if (and (string-equal gdb-current-language "c")
1093 gdb-use-colon-colon-notation gdb-selected-frame)
1094 (setq expr (concat gdb-selected-frame "::" expr))
1095 expr)
1096 (bindat-get-field result 'numchild)
1097 (bindat-get-field result 'type)
1098 (bindat-get-field result 'value)
1099 nil
1100 (bindat-get-field result 'has_more)
1101 gdb-frame-address)))
1102 (push var gdb-var-list)
1103 (speedbar 1)
1104 (unless (string-equal
1105 speedbar-initial-expansion-list-name "GUD")
1106 (speedbar-change-initial-expansion-list "GUD")))
1107 (message-box "No symbol \"%s\" in current context." expr))))
1108
1109 (defun gdb-speedbar-update ()
1110 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1111 (not (gdb-pending-p 'gdb-speedbar-timer)))
1112 ;; Dummy command to update speedbar even when idle.
1113 (gdb-input "-environment-pwd" 'gdb-speedbar-timer-fn)
1114 ;; Keep gdb-pending-triggers non-nil till end.
1115 (gdb-add-pending 'gdb-speedbar-timer)))
1116
1117 (defun gdb-speedbar-timer-fn ()
1118 (if gdb-speedbar-auto-raise
1119 (raise-frame speedbar-frame))
1120 (gdb-delete-pending 'gdb-speedbar-timer)
1121 (speedbar-timer-fn))
1122
1123 (defun gdb-var-evaluate-expression-handler (varnum changed)
1124 (goto-char (point-min))
1125 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1126 (let ((var (assoc varnum gdb-var-list)))
1127 (when var
1128 (if changed (setcar (nthcdr 5 var) 'changed))
1129 (setcar (nthcdr 4 var) (read (match-string 1)))))
1130 (gdb-speedbar-update))
1131
1132 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1133 (defun gdb-var-list-children (varnum)
1134 (gdb-input (concat "-var-update " varnum) 'ignore)
1135 (gdb-input (concat "-var-list-children --all-values " varnum)
1136 `(lambda () (gdb-var-list-children-handler ,varnum))))
1137
1138 (defun gdb-var-list-children-handler (varnum)
1139 (let* ((var-list nil)
1140 (output (bindat-get-field (gdb-json-partial-output "child")))
1141 (children (bindat-get-field output 'children)))
1142 (catch 'child-already-watched
1143 (dolist (var gdb-var-list)
1144 (if (string-equal varnum (car var))
1145 (progn
1146 ;; With dynamic varobjs numchild may have increased.
1147 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1148 (push var var-list)
1149 (dolist (child children)
1150 (let ((varchild (list (bindat-get-field child 'name)
1151 (bindat-get-field child 'exp)
1152 (bindat-get-field child 'numchild)
1153 (bindat-get-field child 'type)
1154 (bindat-get-field child 'value)
1155 nil
1156 (bindat-get-field child 'has_more))))
1157 (if (assoc (car varchild) gdb-var-list)
1158 (throw 'child-already-watched nil))
1159 (push varchild var-list))))
1160 (push var var-list)))
1161 (setq gdb-var-list (nreverse var-list))))
1162 (gdb-speedbar-update))
1163
1164 (defun gdb-var-set-format (format)
1165 "Set the output format for a variable displayed in the speedbar."
1166 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1167 (varnum (car var)))
1168 (gdb-input (concat "-var-set-format " varnum " " format) 'ignore)
1169 (gdb-var-update)))
1170
1171 (defun gdb-var-delete-1 (var varnum)
1172 (gdb-input (concat "-var-delete " varnum) 'ignore)
1173 (setq gdb-var-list (delq var gdb-var-list))
1174 (dolist (varchild gdb-var-list)
1175 (if (string-match (concat (car var) "\\.") (car varchild))
1176 (setq gdb-var-list (delq varchild gdb-var-list)))))
1177
1178 (defun gdb-var-delete ()
1179 "Delete watch expression at point from the speedbar."
1180 (interactive)
1181 (let ((text (speedbar-line-text)))
1182 (string-match "\\(\\S-+\\)" text)
1183 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1184 (varnum (car var)))
1185 (if (string-match "\\." (car var))
1186 (message-box "Can only delete a root expression")
1187 (gdb-var-delete-1 var varnum)))))
1188
1189 (defun gdb-var-delete-children (varnum)
1190 "Delete children of variable object at point from the speedbar."
1191 (gdb-input (concat "-var-delete -c " varnum) 'ignore))
1192
1193 (defun gdb-edit-value (_text _token _indent)
1194 "Assign a value to a variable displayed in the speedbar."
1195 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1196 (varnum (car var))
1197 (value (read-string "New value: ")))
1198 (gdb-input (concat "-var-assign " varnum " " value)
1199 `(lambda () (gdb-edit-value-handler ,value)))))
1200
1201 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1202
1203 (defun gdb-edit-value-handler (value)
1204 (goto-char (point-min))
1205 (if (re-search-forward gdb-error-regexp nil t)
1206 (message-box "Invalid number or expression (%s)" value)))
1207
1208 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1209 (defun gdb-var-update ()
1210 (if (not (gdb-pending-p 'gdb-var-update))
1211 (gdb-input "-var-update --all-values *" 'gdb-var-update-handler))
1212 (gdb-add-pending 'gdb-var-update))
1213
1214 (defun gdb-var-update-handler ()
1215 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1216 (dolist (var gdb-var-list)
1217 (setcar (nthcdr 5 var) nil))
1218 (let ((temp-var-list gdb-var-list))
1219 (dolist (change changelist)
1220 (let* ((varnum (bindat-get-field change 'name))
1221 (var (assoc varnum gdb-var-list))
1222 (new-num (bindat-get-field change 'new_num_children)))
1223 (when var
1224 (let ((scope (bindat-get-field change 'in_scope))
1225 (has-more (bindat-get-field change 'has_more)))
1226 (cond ((string-equal scope "false")
1227 (if gdb-delete-out-of-scope
1228 (gdb-var-delete-1 var varnum)
1229 (setcar (nthcdr 5 var) 'out-of-scope)))
1230 ((string-equal scope "true")
1231 (setcar (nthcdr 6 var) has-more)
1232 (when (and (or (not has-more)
1233 (string-equal has-more "0"))
1234 (not new-num)
1235 (string-equal (nth 2 var) "0"))
1236 (setcar (nthcdr 4 var)
1237 (bindat-get-field change 'value))
1238 (setcar (nthcdr 5 var) 'changed)))
1239 ((string-equal scope "invalid")
1240 (gdb-var-delete-1 var varnum)))))
1241 (let ((var-list nil) var1
1242 (children (bindat-get-field change 'new_children)))
1243 (when new-num
1244 (setq var1 (pop temp-var-list))
1245 (while var1
1246 (if (string-equal varnum (car var1))
1247 (let ((new (string-to-number new-num))
1248 (previous (string-to-number (nth 2 var1))))
1249 (setcar (nthcdr 2 var1) new-num)
1250 (push var1 var-list)
1251 (cond
1252 ((> new previous)
1253 ;; Add new children to list.
1254 (dotimes (_ previous)
1255 (push (pop temp-var-list) var-list))
1256 (dolist (child children)
1257 (let ((varchild
1258 (list (bindat-get-field child 'name)
1259 (bindat-get-field child 'exp)
1260 (bindat-get-field child 'numchild)
1261 (bindat-get-field child 'type)
1262 (bindat-get-field child 'value)
1263 'changed
1264 (bindat-get-field child 'has_more))))
1265 (push varchild var-list))))
1266 ;; Remove deleted children from list.
1267 ((< new previous)
1268 (dotimes (_ new)
1269 (push (pop temp-var-list) var-list))
1270 (dotimes (_ (- previous new))
1271 (pop temp-var-list)))))
1272 (push var1 var-list))
1273 (setq var1 (pop temp-var-list)))
1274 (setq gdb-var-list (nreverse var-list))))))))
1275 (setq gdb-pending-triggers
1276 (delq 'gdb-var-update gdb-pending-triggers))
1277 (gdb-speedbar-update))
1278
1279 (defun gdb-speedbar-expand-node (text token indent)
1280 "Expand the node the user clicked on.
1281 TEXT is the text of the button we clicked on, a + or - item.
1282 TOKEN is data related to this node.
1283 INDENT is the current indentation depth."
1284 (cond ((string-match "+" text) ;expand this node
1285 (let* ((var (assoc token gdb-var-list))
1286 (expr (nth 1 var)) (children (nth 2 var)))
1287 (if (or (<= (string-to-number children) gdb-max-children)
1288 (y-or-n-p
1289 (format "%s has %s children. Continue? " expr children)))
1290 (gdb-var-list-children token))))
1291 ((string-match "-" text) ;contract this node
1292 (dolist (var gdb-var-list)
1293 (if (string-match (concat token "\\.") (car var))
1294 (setq gdb-var-list (delq var gdb-var-list))))
1295 (gdb-var-delete-children token)
1296 (speedbar-change-expand-button-char ?+)
1297 (speedbar-delete-subblock indent))
1298 (t (error "Ooops... not sure what to do")))
1299 (speedbar-center-buffer-smartly))
1300
1301 (defun gdb-get-target-string ()
1302 (with-current-buffer gud-comint-buffer
1303 gud-target-name))
1304 \f
1305
1306 ;;
1307 ;; gdb buffers.
1308 ;;
1309 ;; Each buffer has a TYPE -- a symbol that identifies the function
1310 ;; of that particular buffer.
1311 ;;
1312 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1313 ;; is constructed specially.
1314 ;;
1315 ;; Others are constructed by gdb-get-buffer-create and
1316 ;; named according to the rules set forth in the gdb-buffer-rules
1317
1318 (defvar gdb-buffer-rules '())
1319
1320 (defun gdb-rules-name-maker (rules-entry)
1321 (cadr rules-entry))
1322 (defun gdb-rules-buffer-mode (rules-entry)
1323 (nth 2 rules-entry))
1324 (defun gdb-rules-update-trigger (rules-entry)
1325 (nth 3 rules-entry))
1326
1327 (defun gdb-update-buffer-name ()
1328 "Rename current buffer according to name-maker associated with
1329 it in `gdb-buffer-rules'."
1330 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1331 gdb-buffer-rules))))
1332 (when f (rename-buffer (funcall f)))))
1333
1334 (defun gdb-current-buffer-rules ()
1335 "Get `gdb-buffer-rules' entry for current buffer type."
1336 (assoc gdb-buffer-type gdb-buffer-rules))
1337
1338 (defun gdb-current-buffer-thread ()
1339 "Get thread object of current buffer from `gdb-threads-list'.
1340
1341 When current buffer is not bound to any thread, return main
1342 thread."
1343 (cdr (assoc gdb-thread-number gdb-threads-list)))
1344
1345 (defun gdb-current-buffer-frame ()
1346 "Get current stack frame object for thread of current buffer."
1347 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1348
1349 (defun gdb-buffer-type (buffer)
1350 "Get value of `gdb-buffer-type' for BUFFER."
1351 (with-current-buffer buffer
1352 gdb-buffer-type))
1353
1354 (defun gdb-buffer-shows-main-thread-p ()
1355 "Return t if current GDB buffer shows main selected thread and
1356 is not bound to it."
1357 (current-buffer)
1358 (not (local-variable-p 'gdb-thread-number)))
1359
1360 (defun gdb-get-buffer (buffer-type &optional thread)
1361 "Get a specific GDB buffer.
1362
1363 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1364 and `gdb-thread-number' (if provided) must be equal to THREAD."
1365 (catch 'found
1366 (dolist (buffer (buffer-list) nil)
1367 (with-current-buffer buffer
1368 (when (and (eq gdb-buffer-type buffer-type)
1369 (or (not thread)
1370 (equal gdb-thread-number thread)))
1371 (throw 'found buffer))))))
1372
1373 (defun gdb-get-buffer-create (buffer-type &optional thread)
1374 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1375 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1376
1377 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1378 buffer-local variable of the new buffer.
1379
1380 Buffer mode and name are selected according to buffer type.
1381
1382 If buffer has trigger associated with it in `gdb-buffer-rules',
1383 this trigger is subscribed to `gdb-buf-publisher' and called with
1384 'update argument."
1385 (or (gdb-get-buffer buffer-type thread)
1386 (let ((rules (assoc buffer-type gdb-buffer-rules))
1387 (new (generate-new-buffer "limbo")))
1388 (with-current-buffer new
1389 (let ((mode (gdb-rules-buffer-mode rules))
1390 (trigger (gdb-rules-update-trigger rules)))
1391 (when mode (funcall mode))
1392 (setq gdb-buffer-type buffer-type)
1393 (when thread
1394 (set (make-local-variable 'gdb-thread-number) thread))
1395 (set (make-local-variable 'gud-minor-mode)
1396 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1397 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1398 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1399 (when trigger
1400 (gdb-add-subscriber gdb-buf-publisher
1401 (cons (current-buffer)
1402 (gdb-bind-function-to-buffer
1403 trigger (current-buffer))))
1404 (funcall trigger 'start))
1405 (current-buffer))))))
1406
1407 (defun gdb-bind-function-to-buffer (expr buffer)
1408 "Return a function which will evaluate EXPR in BUFFER."
1409 `(lambda (&rest args)
1410 (with-current-buffer ,buffer
1411 (apply ',expr args))))
1412
1413 ;; Used to display windows with thread-bound buffers
1414 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1415 split-horizontal)
1416 `(defun ,name (&optional thread)
1417 ,(when doc doc)
1418 (message thread)
1419 (gdb-preempt-existing-or-display-buffer
1420 (gdb-get-buffer-create ,buffer thread)
1421 ,split-horizontal)))
1422
1423 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1424 ;; at least one and possible more functions. The functions have these
1425 ;; roles in defining a buffer type:
1426 ;;
1427 ;; NAME - Return a name for this buffer type.
1428 ;;
1429 ;; The remaining function(s) are optional:
1430 ;;
1431 ;; MODE - called in a new buffer with no arguments, should establish
1432 ;; the proper mode for the buffer.
1433 ;;
1434
1435 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1436 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1437 (if binding
1438 (setcdr binding rules)
1439 (push (cons buffer-type rules)
1440 gdb-buffer-rules))))
1441
1442 (defun gdb-parent-mode ()
1443 "Generic mode to derive all other GDB buffer modes from."
1444 (kill-all-local-variables)
1445 (setq buffer-read-only t)
1446 (buffer-disable-undo)
1447 ;; Delete buffer from gdb-buf-publisher when it's killed
1448 ;; (if it has an associated update trigger)
1449 (add-hook
1450 'kill-buffer-hook
1451 (function
1452 (lambda ()
1453 (let ((trigger (gdb-rules-update-trigger
1454 (gdb-current-buffer-rules))))
1455 (when trigger
1456 (gdb-delete-subscriber
1457 gdb-buf-publisher
1458 ;; This should match gdb-add-subscriber done in
1459 ;; gdb-get-buffer-create
1460 (cons (current-buffer)
1461 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1462 nil t))
1463
1464 ;; Partial-output buffer : This accumulates output from a command executed on
1465 ;; behalf of emacs (rather than the user).
1466 ;;
1467 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1468 'gdb-partial-output-name)
1469
1470 (defun gdb-partial-output-name ()
1471 (concat " *partial-output-"
1472 (gdb-get-target-string)
1473 "*"))
1474
1475 \f
1476 (gdb-set-buffer-rules 'gdb-inferior-io
1477 'gdb-inferior-io-name
1478 'gdb-inferior-io-mode)
1479
1480 (defun gdb-inferior-io-name ()
1481 (concat "*input/output of "
1482 (gdb-get-target-string)
1483 "*"))
1484
1485 (defun gdb-display-io-buffer ()
1486 "Display IO of debugged program in a separate window."
1487 (interactive)
1488 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))
1489
1490 (defun gdb-inferior-io--init-proc (proc)
1491 ;; Set up inferior I/O. Needs GDB 6.4 onwards.
1492 (set-process-filter proc 'gdb-inferior-filter)
1493 (set-process-sentinel proc 'gdb-inferior-io-sentinel)
1494 ;; The process can run on a remote host.
1495 (let ((tty (or (process-get proc 'remote-tty)
1496 (process-tty-name proc))))
1497 (unless (or (null tty)
1498 (string= tty ""))
1499 (gdb-input
1500 (concat "-inferior-tty-set " tty) 'ignore))))
1501
1502 (defun gdb-inferior-io-sentinel (proc _str)
1503 (when (eq (process-status proc) 'failed)
1504 ;; When the debugged process exits, Emacs gets an EIO error on
1505 ;; read from the pty, and stops listening to it. If the gdb
1506 ;; process is still running, remove the pty, make a new one, and
1507 ;; pass it to gdb.
1508 (let ((gdb-proc (get-buffer-process gud-comint-buffer))
1509 (io-buffer (process-buffer proc)))
1510 (when (and gdb-proc (process-live-p gdb-proc)
1511 (buffer-live-p io-buffer))
1512 ;; `comint-exec' deletes the original process as a side effect.
1513 (comint-exec io-buffer "gdb-inferior" nil nil nil)
1514 (gdb-inferior-io--init-proc (get-buffer-process io-buffer))))))
1515
1516 (defcustom gdb-display-buffer-other-frame-action
1517 '((display-buffer-reuse-window display-buffer-pop-up-frame)
1518 (reusable-frames . visible)
1519 (inhibit-same-window . t)
1520 (pop-up-frame-parameters (height . 14)
1521 (width . 80)
1522 (unsplittable . t)
1523 (tool-bar-lines . nil)
1524 (menu-bar-lines . nil)
1525 (minibuffer . nil)))
1526 "`display-buffer' action for displaying GDB utility frames."
1527 :group 'gdb
1528 :type display-buffer--action-custom-type
1529 :risky t
1530 :version "24.3")
1531
1532 (defun gdb-frame-io-buffer ()
1533 "Display IO of debugged program in another frame."
1534 (interactive)
1535 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1536 gdb-display-buffer-other-frame-action))
1537
1538 (defvar gdb-inferior-io-mode-map
1539 (let ((map (make-sparse-keymap)))
1540 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1541 (define-key map "\C-c\C-z" 'gdb-io-stop)
1542 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1543 (define-key map "\C-c\C-d" 'gdb-io-eof)
1544 (define-key map "\C-d" 'gdb-io-eof)
1545 map))
1546
1547 ;; We want to use comint because it has various nifty and familiar features.
1548 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1549 "Major mode for gdb inferior-io."
1550 :syntax-table nil :abbrev-table nil
1551 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1552
1553 (defun gdb-inferior-filter (proc string)
1554 (unless (string-equal string "")
1555 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))
1556 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1557 (comint-output-filter proc string)))
1558
1559 (defun gdb-io-interrupt ()
1560 "Interrupt the program being debugged."
1561 (interactive)
1562 (interrupt-process
1563 (get-buffer-process gud-comint-buffer) comint-ptyp))
1564
1565 (defun gdb-io-quit ()
1566 "Send quit signal to the program being debugged."
1567 (interactive)
1568 (quit-process
1569 (get-buffer-process gud-comint-buffer) comint-ptyp))
1570
1571 (defun gdb-io-stop ()
1572 "Stop the program being debugged."
1573 (interactive)
1574 (stop-process
1575 (get-buffer-process gud-comint-buffer) comint-ptyp))
1576
1577 (defun gdb-io-eof ()
1578 "Send end-of-file to the program being debugged."
1579 (interactive)
1580 (process-send-eof
1581 (get-buffer-process gud-comint-buffer)))
1582
1583 (defun gdb-clear-inferior-io ()
1584 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1585 (erase-buffer)))
1586 \f
1587
1588 (defconst breakpoint-xpm-data
1589 "/* XPM */
1590 static char *magick[] = {
1591 /* columns rows colors chars-per-pixel */
1592 \"10 10 2 1\",
1593 \" c red\",
1594 \"+ c None\",
1595 /* pixels */
1596 \"+++ +++\",
1597 \"++ ++\",
1598 \"+ +\",
1599 \" \",
1600 \" \",
1601 \" \",
1602 \" \",
1603 \"+ +\",
1604 \"++ ++\",
1605 \"+++ +++\",
1606 };"
1607 "XPM data used for breakpoint icon.")
1608
1609 (defconst breakpoint-enabled-pbm-data
1610 "P1
1611 10 10\",
1612 0 0 0 0 1 1 1 1 0 0 0 0
1613 0 0 0 1 1 1 1 1 1 0 0 0
1614 0 0 1 1 1 1 1 1 1 1 0 0
1615 0 1 1 1 1 1 1 1 1 1 1 0
1616 0 1 1 1 1 1 1 1 1 1 1 0
1617 0 1 1 1 1 1 1 1 1 1 1 0
1618 0 1 1 1 1 1 1 1 1 1 1 0
1619 0 0 1 1 1 1 1 1 1 1 0 0
1620 0 0 0 1 1 1 1 1 1 0 0 0
1621 0 0 0 0 1 1 1 1 0 0 0 0"
1622 "PBM data used for enabled breakpoint icon.")
1623
1624 (defconst breakpoint-disabled-pbm-data
1625 "P1
1626 10 10\",
1627 0 0 1 0 1 0 1 0 0 0
1628 0 1 0 1 0 1 0 1 0 0
1629 1 0 1 0 1 0 1 0 1 0
1630 0 1 0 1 0 1 0 1 0 1
1631 1 0 1 0 1 0 1 0 1 0
1632 0 1 0 1 0 1 0 1 0 1
1633 1 0 1 0 1 0 1 0 1 0
1634 0 1 0 1 0 1 0 1 0 1
1635 0 0 1 0 1 0 1 0 1 0
1636 0 0 0 1 0 1 0 1 0 0"
1637 "PBM data used for disabled breakpoint icon.")
1638
1639 (defvar breakpoint-enabled-icon nil
1640 "Icon for enabled breakpoint in display margin.")
1641
1642 (defvar breakpoint-disabled-icon nil
1643 "Icon for disabled breakpoint in display margin.")
1644
1645 (declare-function define-fringe-bitmap "fringe.c"
1646 (bitmap bits &optional height width align))
1647
1648 (and (display-images-p)
1649 ;; Bitmap for breakpoint in fringe
1650 (define-fringe-bitmap 'breakpoint
1651 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1652 ;; Bitmap for gud-overlay-arrow in fringe
1653 (define-fringe-bitmap 'hollow-right-triangle
1654 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1655
1656 (defface breakpoint-enabled
1657 '((t
1658 :foreground "red1"
1659 :weight bold))
1660 "Face for enabled breakpoint icon in fringe."
1661 :group 'gdb)
1662
1663 (defface breakpoint-disabled
1664 '((((class color) (min-colors 88)) :foreground "grey70")
1665 ;; Ensure that on low-color displays that we end up something visible.
1666 (((class color) (min-colors 8) (background light))
1667 :foreground "black")
1668 (((class color) (min-colors 8) (background dark))
1669 :foreground "white")
1670 (((type tty) (class mono))
1671 :inverse-video t)
1672 (t :background "gray"))
1673 "Face for disabled breakpoint icon in fringe."
1674 :group 'gdb)
1675
1676 \f
1677 (defvar gdb-control-commands-regexp
1678 (concat
1679 "^\\("
1680 "commands\\|if\\|while\\|define\\|document\\|python\\|"
1681 "while-stepping\\|stepping\\|ws\\|actions"
1682 "\\)\\([[:blank:]]+.*\\)?$")
1683 "Regexp matching GDB commands that enter a recursive reading loop.
1684 As long as GDB is in the recursive reading loop, it does not expect
1685 commands to be prefixed by \"-interpreter-exec console\".")
1686
1687 (defun gdb-send (proc string)
1688 "A comint send filter for gdb."
1689 (with-current-buffer gud-comint-buffer
1690 (let ((inhibit-read-only t))
1691 (remove-text-properties (point-min) (point-max) '(face))))
1692 ;; mimic <RET> key to repeat previous command in GDB
1693 (if (not (string= "" string))
1694 (setq gdb-last-command string)
1695 (if gdb-last-command (setq string gdb-last-command)))
1696 (if (or (string-match "^-" string)
1697 (> gdb-control-level 0))
1698 ;; Either MI command or we are feeding GDB's recursive reading loop.
1699 (progn
1700 (setq gdb-first-done-or-error t)
1701 (process-send-string proc (concat string "\n"))
1702 (if (and (string-match "^end$" string)
1703 (> gdb-control-level 0))
1704 (setq gdb-control-level (1- gdb-control-level))))
1705 ;; CLI command
1706 (if (string-match "\\\\$" string)
1707 (setq gdb-continuation (concat gdb-continuation string "\n"))
1708 (setq gdb-first-done-or-error t)
1709 (let ((to-send (concat "-interpreter-exec console "
1710 (gdb-mi-quote string)
1711 "\n")))
1712 (if gdb-enable-debug
1713 (push (cons 'mi-send to-send) gdb-debug-log))
1714 (process-send-string proc to-send))
1715 (if (and (string-match "^end$" string)
1716 (> gdb-control-level 0))
1717 (setq gdb-control-level (1- gdb-control-level)))
1718 (setq gdb-continuation nil)))
1719 (if (string-match gdb-control-commands-regexp string)
1720 (setq gdb-control-level (1+ gdb-control-level))))
1721
1722 (defun gdb-mi-quote (string)
1723 "Return STRING quoted properly as an MI argument.
1724 The string is enclosed in double quotes.
1725 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1726 (setq string (replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string))
1727 (setq string (replace-regexp-in-string "\n" "\\n" string t t))
1728 (concat "\"" string "\""))
1729
1730 (defun gdb-input (command handler-function)
1731 "Send COMMAND to GDB via the MI interface.
1732 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1733 complete."
1734 (if gdb-enable-debug (push (list 'send-item command handler-function)
1735 gdb-debug-log))
1736 (setq gdb-token-number (1+ gdb-token-number))
1737 (setq command (concat (number-to-string gdb-token-number) command))
1738 (push (cons gdb-token-number handler-function) gdb-handler-alist)
1739 (if gdbmi-debug-mode (message "gdb-input: %s" command))
1740 (process-send-string (get-buffer-process gud-comint-buffer)
1741 (concat command "\n")))
1742
1743 ;; NOFRAME is used for gud execution control commands
1744 (defun gdb-current-context-command (command)
1745 "Add --thread to gdb COMMAND when needed."
1746 (if (and gdb-thread-number
1747 gdb-supports-non-stop)
1748 (concat command " --thread " gdb-thread-number)
1749 command))
1750
1751 (defun gdb-current-context-buffer-name (name)
1752 "Add thread information and asterisks to string NAME.
1753
1754 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1755 (concat "*" name
1756 (if (local-variable-p 'gdb-thread-number)
1757 (format " (bound to thread %s)" gdb-thread-number)
1758 "")
1759 "*"))
1760
1761 (defun gdb-current-context-mode-name (mode)
1762 "Add thread information to MODE which is to be used as `mode-name'."
1763 (concat mode
1764 (if gdb-thread-number
1765 (format " [thread %s]" gdb-thread-number)
1766 "")))
1767 \f
1768
1769 (defcustom gud-gdb-command-name "gdb -i=mi"
1770 "Default command to execute an executable under the GDB debugger."
1771 :type 'string
1772 :group 'gdb)
1773
1774 (defun gdb-resync()
1775 (setq gud-running nil)
1776 (setq gdb-output-sink 'user)
1777 (setq gdb-pending-triggers nil))
1778
1779 (defun gdb-update (&optional no-proc)
1780 "Update buffers showing status of debug session.
1781 If NO-PROC is non-nil, do not try to contact the GDB process."
1782 (when gdb-first-prompt
1783 (gdb-force-mode-line-update
1784 (propertize "initializing..." 'face font-lock-variable-name-face))
1785 (gdb-init-1)
1786 (setq gdb-first-prompt nil))
1787
1788 (unless no-proc
1789 (gdb-get-main-selected-frame))
1790
1791 ;; We may need to update gdb-threads-list so we can use
1792 (gdb-get-buffer-create 'gdb-threads-buffer)
1793 ;; gdb-break-list is maintained in breakpoints handler
1794 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1795
1796 (unless no-proc
1797 (gdb-emit-signal gdb-buf-publisher 'update))
1798
1799 (gdb-get-changed-registers)
1800 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1801 (dolist (var gdb-var-list)
1802 (setcar (nthcdr 5 var) nil))
1803 (gdb-var-update)))
1804
1805 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1806 ;; because we may need to update current gud-running value without
1807 ;; changing current thread (see gdb-running)
1808 (defun gdb-setq-thread-number (number)
1809 "Set `gdb-thread-number' to NUMBER.
1810 Only this function must be used to change `gdb-thread-number'
1811 value to NUMBER, because `gud-running' and `gdb-frame-number'
1812 need to be updated appropriately when current thread changes."
1813 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1814 (unless (string-equal number "0") (setq gdb-thread-number number))
1815 (setq gdb-frame-number "0")
1816 (gdb-update-gud-running))
1817
1818 (defun gdb-update-gud-running ()
1819 "Set `gud-running' according to the state of current thread.
1820
1821 `gdb-frame-number' is set to 0 if current thread is now stopped.
1822
1823 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1824 cannot be reliably used to determine whether or not execution
1825 control buttons should be shown in menu or toolbar. Use
1826 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1827 instead.
1828
1829 For all-stop mode, thread information is unavailable while target
1830 is running."
1831 (let ((old-value gud-running))
1832 (setq gud-running
1833 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1834 "running"))
1835 ;; Set frame number to "0" when _current_ threads stops.
1836 (when (and (gdb-current-buffer-thread)
1837 (not (eq gud-running old-value)))
1838 (setq gdb-frame-number "0"))))
1839
1840 (defun gdb-show-run-p ()
1841 "Return t if \"Run/continue\" should be shown on the toolbar."
1842 (or (not gdb-active-process)
1843 (and (or
1844 (not gdb-gud-control-all-threads)
1845 (not gdb-non-stop))
1846 (not gud-running))
1847 (and gdb-gud-control-all-threads
1848 (> gdb-stopped-threads-count 0))))
1849
1850 (defun gdb-show-stop-p ()
1851 "Return t if \"Stop\" should be shown on the toolbar."
1852 (or (and (or
1853 (not gdb-gud-control-all-threads)
1854 (not gdb-non-stop))
1855 gud-running)
1856 (and gdb-gud-control-all-threads
1857 (> gdb-running-threads-count 0))))
1858
1859 ;; GUD displays the selected GDB frame. This might might not be the current
1860 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1861 ;; visited breakpoint is, use that window.
1862 (defun gdb-display-source-buffer (buffer)
1863 (let* ((last-window (if gud-last-last-frame
1864 (get-buffer-window
1865 (gud-find-file (car gud-last-last-frame)))))
1866 (source-window (or last-window
1867 (if (and gdb-source-window
1868 (window-live-p gdb-source-window))
1869 gdb-source-window))))
1870 (when source-window
1871 (setq gdb-source-window source-window)
1872 (set-window-buffer source-window buffer))
1873 source-window))
1874
1875
1876 (defun gdbmi-start-with (str offset match)
1877 "Return non-nil if string STR starts with MATCH, else returns nil.
1878 OFFSET is the position in STR at which the comparison takes place."
1879 (let ((match-length (length match))
1880 (str-length (- (length str) offset)))
1881 (when (>= str-length match-length)
1882 (string-equal match (substring str offset (+ offset match-length))))))
1883
1884 (defun gdbmi-same-start (str offset match)
1885 "Return non-nil iff STR and MATCH are equal up to the end of either strings.
1886 OFFSET is the position in STR at which the comparison takes place."
1887 (let* ((str-length (- (length str) offset))
1888 (match-length (length match))
1889 (compare-length (min str-length match-length)))
1890 (when (> compare-length 0)
1891 (string-equal (substring str offset (+ offset compare-length))
1892 (substring match 0 compare-length)))))
1893
1894 (defun gdbmi-is-number (character)
1895 "Return non-nil iff CHARACTER is a numerical character between 0 and 9."
1896 (and (>= character ?0)
1897 (<= character ?9)))
1898
1899
1900 (defvar-local gdbmi-bnf-state 'gdbmi-bnf-output
1901 "Current GDB/MI output parser state.
1902 The parser is placed in a different state when an incomplete data steam is
1903 received from GDB.
1904 This variable will preserve the state required to resume the parsing
1905 when more data arrives.")
1906
1907 (defvar-local gdbmi-bnf-offset 0
1908 "Offset in `gud-marker-acc' at which the parser is reading.
1909 This offset is used to be able to parse the GDB/MI message
1910 in-place, without the need of copying the string in a temporary buffer
1911 or discarding parsed tokens by substringing the message.")
1912
1913 (defun gdbmi-bnf-init ()
1914 "Initialize the GDB/MI message parser."
1915 (setq gdbmi-bnf-state 'gdbmi-bnf-output)
1916 (setq gdbmi-bnf-offset 0)
1917 (setq gud-marker-acc ""))
1918
1919
1920 (defun gdbmi-bnf-output ()
1921 "Implementation of the following GDB/MI output grammar rule:
1922
1923 output ==>
1924 ( out-of-band-record )* [ result-record ] gdb-prompt"
1925
1926 (gdbmi-bnf-skip-unrecognized)
1927 (while (gdbmi-bnf-out-of-band-record))
1928 (gdbmi-bnf-result-record)
1929 (gdbmi-bnf-gdb-prompt))
1930
1931
1932 (defun gdbmi-bnf-skip-unrecognized ()
1933 "Skip characters until is encounters the beginning of a valid record.
1934 Used as a protection mechanism in case something goes wrong when parsing
1935 a GDB/MI reply message."
1936 (let ((acc-length (length gud-marker-acc))
1937 (prefix-offset gdbmi-bnf-offset)
1938 (prompt "(gdb) \n"))
1939
1940 (while (and (< prefix-offset acc-length)
1941 (gdbmi-is-number (aref gud-marker-acc prefix-offset)))
1942 (setq prefix-offset (1+ prefix-offset)))
1943
1944 (if (and (< prefix-offset acc-length)
1945 (not (memq (aref gud-marker-acc prefix-offset)
1946 '(?^ ?* ?+ ?= ?~ ?@ ?&)))
1947 (not (gdbmi-same-start gud-marker-acc gdbmi-bnf-offset prompt))
1948 (string-match "\\([^^*+=~@&]+\\)" gud-marker-acc
1949 gdbmi-bnf-offset))
1950 (let ((unrecognized-str (match-string 0 gud-marker-acc)))
1951 (setq gdbmi-bnf-offset (match-end 0))
1952 (if gdbmi-debug-mode
1953 (message "gdbmi-bnf-skip-unrecognized: %s" unrecognized-str))
1954 (gdb-shell unrecognized-str)
1955 t))))
1956
1957
1958 (defun gdbmi-bnf-gdb-prompt ()
1959 "Implementation of the following GDB/MI output grammar rule:
1960 gdb-prompt ==>
1961 '(gdb)' nl
1962
1963 nl ==>
1964 CR | CR-LF"
1965
1966 (let ((prompt "(gdb) \n"))
1967 (when (gdbmi-start-with gud-marker-acc gdbmi-bnf-offset prompt)
1968 (if gdbmi-debug-mode (message "gdbmi-bnf-gdb-prompt: %s" prompt))
1969 (gdb-gdb prompt)
1970 (setq gdbmi-bnf-offset (+ gdbmi-bnf-offset (length prompt)))
1971
1972 ;; Returns non-nil to tell gud-gdbmi-marker-filter we've reached
1973 ;; the end of a GDB reply message.
1974 t)))
1975
1976
1977 (defun gdbmi-bnf-result-record ()
1978 "Implementation of the following GDB/MI output grammar rule:
1979
1980 result-record ==>
1981 [ token ] '^' result-class ( ',' result )* nl
1982
1983 token ==>
1984 any sequence of digits."
1985
1986 (gdbmi-bnf-result-and-async-record-impl))
1987
1988
1989 (defun gdbmi-bnf-out-of-band-record ()
1990 "Implementation of the following GDB/MI output grammar rule:
1991
1992 out-of-band-record ==>
1993 async-record | stream-record"
1994
1995 (or (gdbmi-bnf-async-record)
1996 (gdbmi-bnf-stream-record)))
1997
1998
1999 (defun gdbmi-bnf-async-record ()
2000 "Implementation of the following GDB/MI output grammar rules:
2001
2002 async-record ==>
2003 exec-async-output | status-async-output | notify-async-output
2004
2005 exec-async-output ==>
2006 [ token ] '*' async-output
2007
2008 status-async-output ==>
2009 [ token ] '+' async-output
2010
2011 notify-async-output ==>
2012 [ token ] '=' async-output
2013
2014 async-output ==>
2015 async-class ( ',' result )* nl"
2016
2017 (gdbmi-bnf-result-and-async-record-impl))
2018
2019
2020 (defun gdbmi-bnf-stream-record ()
2021 "Implement the following GDB/MI output grammar rule:
2022 stream-record ==>
2023 console-stream-output | target-stream-output | log-stream-output
2024
2025 console-stream-output ==>
2026 '~' c-string
2027
2028 target-stream-output ==>
2029 '@' c-string
2030
2031 log-stream-output ==>
2032 '&' c-string"
2033 (when (< gdbmi-bnf-offset (length gud-marker-acc))
2034 (if (and (member (aref gud-marker-acc gdbmi-bnf-offset) '(?~ ?@ ?&))
2035 (string-match "\\([~@&]\\)\\(\".*?\"\\)\n" gud-marker-acc
2036 gdbmi-bnf-offset))
2037 (let ((prefix (match-string 1 gud-marker-acc))
2038 (c-string (match-string 2 gud-marker-acc)))
2039
2040 (setq gdbmi-bnf-offset (match-end 0))
2041 (if gdbmi-debug-mode (message "gdbmi-bnf-stream-record: %s"
2042 (match-string 0 gud-marker-acc)))
2043
2044 (cond ((string-equal prefix "~")
2045 (gdbmi-bnf-console-stream-output c-string))
2046 ((string-equal prefix "@")
2047 (gdbmi-bnf-target-stream-output c-string))
2048 ((string-equal prefix "&")
2049 (gdbmi-bnf-log-stream-output c-string)))
2050 t))))
2051
2052 (defun gdbmi-bnf-console-stream-output (c-string)
2053 "Handler for the console-stream-output GDB/MI output grammar rule."
2054 (gdb-console c-string))
2055
2056 (defun gdbmi-bnf-target-stream-output (_c-string)
2057 "Handler for the target-stream-output GDB/MI output grammar rule."
2058 ;; Not currently used.
2059 )
2060
2061 (defun gdbmi-bnf-log-stream-output (c-string)
2062 "Handler for the log-stream-output GDB/MI output grammar rule."
2063 ;; Suppress "No registers." GDB 6.8 and earlier
2064 ;; duplicates MI error message on internal stream.
2065 ;; Don't print to GUD buffer.
2066 (if (not (string-equal (read c-string) "No registers.\n"))
2067 (gdb-internals c-string)))
2068
2069
2070 (defconst gdbmi-bnf-result-state-configs
2071 '(("^" . (("done" . (gdb-done . progressive))
2072 ("error" . (gdb-error . progressive))
2073 ("running" . (gdb-starting . atomic))))
2074 ("*" . (("stopped" . (gdb-stopped . atomic))
2075 ("running" . (gdb-running . atomic))))
2076 ("+" . ())
2077 ("=" . (("thread-created" . (gdb-thread-created . atomic))
2078 ("thread-selected" . (gdb-thread-selected . atomic))
2079 ("thread-existed" . (gdb-ignored-notification . atomic))
2080 ('default . (gdb-ignored-notification . atomic)))))
2081 "Alist of alists, mapping the type and class of message to a handler function.
2082 Handler functions are all flagged as either `progressive' or `atomic'.
2083 `progressive' handlers are capable of parsing incomplete messages.
2084 They can be called several time with new data chunk as they arrive from GDB.
2085 `progressive' handlers must have an extra argument that is set to a non-nil
2086 value when the message is complete.
2087
2088 Implement the following GDB/MI output grammar rule:
2089 result-class ==>
2090 'done' | 'running' | 'connected' | 'error' | 'exit'
2091
2092 async-class ==>
2093 'stopped' | others (where others will be added depending on the needs
2094 --this is still in development).")
2095
2096 (defun gdbmi-bnf-result-and-async-record-impl ()
2097 "Common implementation of the result-record and async-record rule.
2098 Both rules share the same syntax. Those records may be very large in size.
2099 For that reason, the \"result\" part of the record is parsed by
2100 `gdbmi-bnf-incomplete-record-result', which will keep
2101 receiving characters as they arrive from GDB until the record is complete."
2102 (let ((acc-length (length gud-marker-acc))
2103 (prefix-offset gdbmi-bnf-offset))
2104
2105 (while (and (< prefix-offset acc-length)
2106 (gdbmi-is-number (aref gud-marker-acc prefix-offset)))
2107 (setq prefix-offset (1+ prefix-offset)))
2108
2109 (if (and (< prefix-offset acc-length)
2110 (member (aref gud-marker-acc prefix-offset) '(?* ?+ ?= ?^))
2111 (string-match "\\([0-9]*\\)\\([*+=^]\\)\\(.+?\\)\\([,\n]\\)"
2112 gud-marker-acc gdbmi-bnf-offset))
2113
2114 (let ((token (match-string 1 gud-marker-acc))
2115 (prefix (match-string 2 gud-marker-acc))
2116 (class (match-string 3 gud-marker-acc))
2117 (complete (string-equal (match-string 4 gud-marker-acc) "\n"))
2118 class-alist
2119 class-command)
2120
2121 (setq gdbmi-bnf-offset (match-end 0))
2122 (if gdbmi-debug-mode (message "gdbmi-bnf-result-record: %s"
2123 (match-string 0 gud-marker-acc)))
2124
2125 (setq class-alist
2126 (cdr (assoc prefix gdbmi-bnf-result-state-configs)))
2127 (setq class-command (cdr (assoc class class-alist)))
2128 (if (null class-command)
2129 (setq class-command (cdr (assoc 'default class-alist))))
2130
2131 (if complete
2132 (if class-command
2133 (if (equal (cdr class-command) 'progressive)
2134 (funcall (car class-command) token "" complete)
2135 (funcall (car class-command) token "")))
2136 (setq gdbmi-bnf-state
2137 (lambda ()
2138 (gdbmi-bnf-incomplete-record-result token class-command)))
2139 (funcall gdbmi-bnf-state))
2140 t))))
2141
2142 (defun gdbmi-bnf-incomplete-record-result (token class-command)
2143 "State of the parser used to progressively parse a result-record or async-record
2144 rule from an incomplete data stream. The parser will stay in this state until
2145 the end of the current result or async record is reached."
2146 (when (< gdbmi-bnf-offset (length gud-marker-acc))
2147 ;; Search the data stream for the end of the current record:
2148 (let* ((newline-pos (string-match "\n" gud-marker-acc gdbmi-bnf-offset))
2149 (is-progressive (equal (cdr class-command) 'progressive))
2150 (is-complete (not (null newline-pos)))
2151 result-str)
2152
2153 (when gdbmi-debug-mode
2154 (message "gdbmi-bnf-incomplete-record-result: %s"
2155 (substring gud-marker-acc gdbmi-bnf-offset newline-pos)))
2156
2157 ;; Update the gdbmi-bnf-offset only if the current chunk of data can
2158 ;; be processed by the class-command handler:
2159 (when (or is-complete is-progressive)
2160 (setq result-str
2161 (substring gud-marker-acc gdbmi-bnf-offset newline-pos))
2162
2163 ;; Move gdbmi-bnf-offset past the end of the chunk.
2164 (setq gdbmi-bnf-offset (+ gdbmi-bnf-offset (length result-str)))
2165 (when newline-pos
2166 (setq gdbmi-bnf-offset (1+ gdbmi-bnf-offset))))
2167
2168 ;; Update the parsing state before invoking the handler in class-command
2169 ;; to make sure it's not left in an invalid state if the handler was
2170 ;; to generate an error.
2171 (if is-complete
2172 (setq gdbmi-bnf-state 'gdbmi-bnf-output))
2173
2174 (if class-command
2175 (if is-progressive
2176 (funcall (car class-command) token result-str is-complete)
2177 (if is-complete
2178 (funcall (car class-command) token result-str))))
2179
2180 (unless is-complete
2181 ;; Incomplete gdb response: abort parsing until we receive more data.
2182 (if gdbmi-debug-mode (message "gdbmi-bnf-incomplete-record-result, aborting: incomplete stream"))
2183 (throw 'gdbmi-incomplete-stream nil))
2184
2185 is-complete)))
2186
2187
2188 ; The following grammar rules are not yet implemented by this GDBMI-BNF parser.
2189 ; The handling of those rules is currently done by the handlers registered
2190 ; in gdbmi-bnf-result-state-configs
2191 ;
2192 ; result ==>
2193 ; variable "=" value
2194 ;
2195 ; variable ==>
2196 ; string
2197 ;
2198 ; value ==>
2199 ; const | tuple | list
2200 ;
2201 ; const ==>
2202 ; c-string
2203 ;
2204 ; tuple ==>
2205 ; "{}" | "{" result ( "," result )* "}"
2206 ;
2207 ; list ==>
2208 ; "[]" | "[" value ( "," value )* "]" | "[" result ( "," result )* "]"
2209
2210
2211 (defun gud-gdbmi-marker-filter (string)
2212 "Filter GDB/MI output."
2213
2214 ;; Record transactions if logging is enabled.
2215 (when gdb-enable-debug
2216 (push (cons 'recv string) gdb-debug-log)
2217 (if (and gdb-debug-log-max
2218 (> (length gdb-debug-log) gdb-debug-log-max))
2219 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
2220
2221 ;; Recall the left over gud-marker-acc from last time.
2222 (setq gud-marker-acc (concat gud-marker-acc string))
2223
2224 ;; Start accumulating output for the GUD buffer.
2225 (setq gdb-filter-output "")
2226
2227 (let ((acc-length (length gud-marker-acc)))
2228 (catch 'gdbmi-incomplete-stream
2229 (while (and (< gdbmi-bnf-offset acc-length)
2230 (funcall gdbmi-bnf-state)))))
2231
2232 (when (/= gdbmi-bnf-offset 0)
2233 (setq gud-marker-acc (substring gud-marker-acc gdbmi-bnf-offset))
2234 (setq gdbmi-bnf-offset 0))
2235
2236 (when (and gdbmi-debug-mode (> (length gud-marker-acc) 0))
2237 (message "gud-gdbmi-marker-filter, unparsed string: %s" gud-marker-acc))
2238
2239 gdb-filter-output)
2240
2241 (defun gdb-gdb (_output-field))
2242
2243 (defun gdb-shell (output-field)
2244 (setq gdb-filter-output
2245 (concat output-field gdb-filter-output)))
2246
2247 (defun gdb-ignored-notification (_token _output-field))
2248
2249 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
2250 (defun gdb-thread-created (_token _output-field))
2251 (defun gdb-thread-exited (_token output-field)
2252 "Handle =thread-exited async record.
2253 Unset `gdb-thread-number' if current thread exited and update threads list."
2254 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
2255 (if (string= gdb-thread-number thread-id)
2256 (gdb-setq-thread-number nil))
2257 ;; When we continue current thread and it quickly exits,
2258 ;; gdb-pending-triggers left after gdb-running disallow us to
2259 ;; properly call -thread-info without --thread option. Thus we
2260 ;; need to use gdb-wait-for-pending.
2261 (gdb-wait-for-pending
2262 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
2263
2264 (defun gdb-thread-selected (_token output-field)
2265 "Handler for =thread-selected MI output record.
2266
2267 Sets `gdb-thread-number' to new id."
2268 (let* ((result (gdb-json-string output-field))
2269 (thread-id (bindat-get-field result 'id)))
2270 (gdb-setq-thread-number thread-id)
2271 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
2272 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
2273 ;; as usually. Things happen to fast and second call (from
2274 ;; gdb-thread-selected handler) gets cut off by our beloved
2275 ;; gdb-pending-triggers.
2276 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
2277 ;; body will get executed when `gdb-pending-triggers` is empty.
2278 (gdb-wait-for-pending
2279 (gdb-update))))
2280
2281 (defun gdb-running (_token output-field)
2282 (let* ((thread-id
2283 (bindat-get-field (gdb-json-string output-field) 'thread-id)))
2284 ;; We reset gdb-frame-number to nil if current thread has gone
2285 ;; running. This can't be done in gdb-thread-list-handler-custom
2286 ;; because we need correct gdb-frame-number by the time
2287 ;; -thread-info command is sent.
2288 (when (or (string-equal thread-id "all")
2289 (string-equal thread-id gdb-thread-number))
2290 (setq gdb-frame-number nil)))
2291 (setq gdb-inferior-status "running")
2292 (gdb-force-mode-line-update
2293 (propertize gdb-inferior-status 'face font-lock-type-face))
2294 (when (not gdb-non-stop)
2295 (setq gud-running t))
2296 (setq gdb-active-process t)
2297 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2298
2299 (defun gdb-starting (_output-field _result)
2300 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2301 (setq gdb-inferior-status "running")
2302 (gdb-force-mode-line-update
2303 (propertize gdb-inferior-status 'face font-lock-type-face))
2304 (setq gdb-active-process t)
2305 (setq gud-running t)
2306 ;; GDB doesn't seem to respond to -thread-info before first stop or
2307 ;; thread exit (even in non-stop mode), so this is useless.
2308 ;; Behavior may change in the future.
2309 (gdb-emit-signal gdb-buf-publisher 'update-threads))
2310
2311 ;; -break-insert -t didn't give a reason before gdb 6.9
2312
2313 (defun gdb-stopped (_token output-field)
2314 "Given the contents of *stopped MI async record, select new
2315 current thread and update GDB buffers."
2316 ;; Reason is available with target-async only
2317 (let* ((result (gdb-json-string output-field))
2318 (reason (bindat-get-field result 'reason))
2319 (thread-id (bindat-get-field result 'thread-id)))
2320
2321 ;; -data-list-register-names needs to be issued for any stopped
2322 ;; thread
2323 (when (not gdb-register-names)
2324 (gdb-input (concat "-data-list-register-names"
2325 (if gdb-supports-non-stop
2326 (concat " --thread " thread-id)))
2327 'gdb-register-names-handler))
2328
2329 ;; Don't set gud-last-frame here as it's currently done in
2330 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2331 ;; with CLI.
2332 ;;(when file
2333 ;; (setq
2334 ;; ;; Extract the frame position from the marker.
2335 ;; gud-last-frame (cons file
2336 ;; (string-to-number
2337 ;; (match-string 6 gud-marker-acc)))))
2338
2339 (setq gdb-inferior-status (or reason "unknown"))
2340 (gdb-force-mode-line-update
2341 (propertize gdb-inferior-status 'face font-lock-warning-face))
2342 (if (string-equal reason "exited-normally")
2343 (setq gdb-active-process nil))
2344
2345 ;; Select new current thread.
2346
2347 ;; Don't switch if we have no reasons selected
2348 (when gdb-switch-reasons
2349 ;; Switch from another stopped thread only if we have
2350 ;; gdb-switch-when-another-stopped:
2351 (when (or gdb-switch-when-another-stopped
2352 (not (string= "stopped"
2353 (bindat-get-field (gdb-current-buffer-thread) 'state))))
2354 ;; Switch if current reason has been selected or we have no
2355 ;; reasons
2356 (if (or (eq gdb-switch-reasons t)
2357 (member reason gdb-switch-reasons))
2358 (when (not (string-equal gdb-thread-number thread-id))
2359 (message (concat "Switched to thread " thread-id))
2360 (gdb-setq-thread-number thread-id))
2361 (message (format "Thread %s stopped" thread-id)))))
2362
2363 ;; Print "(gdb)" to GUD console
2364 (when gdb-first-done-or-error
2365 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2366
2367 ;; In non-stop, we update information as soon as another thread gets
2368 ;; stopped
2369 (when (or gdb-first-done-or-error
2370 gdb-non-stop)
2371 ;; In all-stop this updates gud-running properly as well.
2372 (gdb-update)
2373 (setq gdb-first-done-or-error nil))
2374 (run-hook-with-args 'gdb-stopped-functions result)))
2375
2376 ;; Remove the trimmings from log stream containing debugging messages
2377 ;; being produced by GDB's internals, use warning face and send to GUD
2378 ;; buffer.
2379 (defun gdb-internals (output-field)
2380 (setq gdb-filter-output
2381 (gdb-concat-output
2382 gdb-filter-output
2383 (if (string= output-field "\"\\n\"")
2384 ""
2385 (let ((error-message
2386 (read output-field)))
2387 (put-text-property
2388 0 (length error-message)
2389 'face font-lock-warning-face
2390 error-message)
2391 error-message)))))
2392
2393 ;; Remove the trimmings from the console stream and send to GUD buffer
2394 ;; (frontend MI commands should not print to this stream)
2395 (defun gdb-console (output-field)
2396 (setq gdb-filter-output
2397 (gdb-concat-output gdb-filter-output (read output-field))))
2398
2399 (defun gdb-done (token-number output-field is-complete)
2400 (gdb-done-or-error token-number 'done output-field is-complete))
2401
2402 (defun gdb-error (token-number output-field is-complete)
2403 (gdb-done-or-error token-number 'error output-field is-complete))
2404
2405 (defun gdb-done-or-error (token-number type output-field is-complete)
2406 (if (string-equal token-number "")
2407 ;; Output from command entered by user
2408 (progn
2409 (setq gdb-output-sink 'user)
2410 (setq token-number nil)
2411 ;; MI error - send to minibuffer
2412 (when (eq type 'error)
2413 ;; Skip "msg=" from `output-field'
2414 (message (read (substring output-field 4)))
2415 ;; Don't send to the console twice. (If it is a console error
2416 ;; it is also in the console stream.)
2417 (setq output-field nil)))
2418 ;; Output from command from frontend.
2419 (setq gdb-output-sink 'emacs))
2420
2421 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2422 (let* ((proc (get-buffer-process gud-comint-buffer))
2423 (no-proc (or (null proc)
2424 (memq (process-status proc) '(exit signal)))))
2425
2426 (when (and is-complete gdb-first-done-or-error)
2427 (unless (or token-number gud-running no-proc)
2428 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2429 (gdb-update no-proc)
2430 (setq gdb-first-done-or-error nil))
2431
2432 (setq gdb-filter-output
2433 (gdb-concat-output gdb-filter-output output-field))
2434
2435 ;; We are done concatenating to the output sink. Restore it to user sink:
2436 (setq gdb-output-sink 'user)
2437
2438 (when (and token-number is-complete)
2439 (with-current-buffer
2440 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2441 (funcall
2442 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
2443 (setq gdb-handler-alist
2444 (assq-delete-all token-number gdb-handler-alist)))
2445
2446 (when is-complete
2447 (gdb-clear-partial-output))))
2448
2449 (defun gdb-concat-output (so-far new)
2450 (cond
2451 ((eq gdb-output-sink 'user) (concat so-far new))
2452 ((eq gdb-output-sink 'emacs)
2453 (gdb-append-to-partial-output new)
2454 so-far)))
2455
2456 (defun gdb-append-to-partial-output (string)
2457 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2458 (goto-char (point-max))
2459 (insert string)))
2460
2461 (defun gdb-clear-partial-output ()
2462 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2463 (erase-buffer)))
2464
2465 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2466 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2467
2468 Field names are wrapped in double quotes and equal signs are
2469 replaced with semicolons.
2470
2471 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2472 partial output. This is used to get rid of useless keys in lists
2473 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2474 -break-info are examples of MI commands which issue such
2475 responses.
2476
2477 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2478 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2479 -break-info output when it contains breakpoint script field
2480 incompatible with GDB/MI output syntax."
2481 (save-excursion
2482 (goto-char (point-min))
2483 (when fix-key
2484 (save-excursion
2485 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2486 (replace-match "" nil nil nil 1))))
2487 (when fix-list
2488 (save-excursion
2489 ;; Find positions of braces which enclose broken list
2490 (while (re-search-forward (concat fix-list "={\"") nil t)
2491 (let ((p1 (goto-char (- (point) 2)))
2492 (p2 (progn (forward-sexp)
2493 (1- (point)))))
2494 ;; Replace braces with brackets
2495 (save-excursion
2496 (goto-char p1)
2497 (delete-char 1)
2498 (insert "[")
2499 (goto-char p2)
2500 (delete-char 1)
2501 (insert "]"))))))
2502 (goto-char (point-min))
2503 (insert "{")
2504 (while (re-search-forward
2505 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t)
2506 (replace-match "\"\\1\":\\2" nil nil))
2507 (goto-char (point-max))
2508 (insert "}")))
2509
2510 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2511 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2512
2513 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2514 (gdb-jsonify-buffer fix-key fix-list)
2515 (save-excursion
2516 (goto-char (point-min))
2517 (let ((json-array-type 'list))
2518 (json-read))))
2519
2520 (defun gdb-json-string (string &optional fix-key fix-list)
2521 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2522
2523 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2524 (with-temp-buffer
2525 (insert string)
2526 (gdb-json-read-buffer fix-key fix-list)))
2527
2528 (defun gdb-json-partial-output (&optional fix-key fix-list)
2529 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2530
2531 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2532 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2533 (gdb-json-read-buffer fix-key fix-list)))
2534
2535 (defun gdb-line-posns (line)
2536 "Return a pair of LINE beginning and end positions."
2537 (let ((offset (1+ (- line (line-number-at-pos)))))
2538 (cons
2539 (line-beginning-position offset)
2540 (line-end-position offset))))
2541
2542 (defmacro gdb-mark-line (line variable)
2543 "Set VARIABLE marker to point at beginning of LINE.
2544
2545 If current window has no fringes, inverse colors on LINE.
2546
2547 Return position where LINE begins."
2548 `(save-excursion
2549 (let* ((posns (gdb-line-posns ,line))
2550 (start-posn (car posns))
2551 (end-posn (cdr posns)))
2552 (set-marker ,variable (copy-marker start-posn))
2553 (when (not (> (car (window-fringes)) 0))
2554 (put-text-property start-posn end-posn
2555 'font-lock-face '(:inverse-video t)))
2556 start-posn)))
2557
2558 (defun gdb-pad-string (string padding)
2559 (format (concat "%" (number-to-string padding) "s") string))
2560
2561 ;; gdb-table struct is a way to programmatically construct simple
2562 ;; tables. It help to reliably align columns of data in GDB buffers
2563 ;; and provides
2564 (cl-defstruct gdb-table
2565 (column-sizes nil)
2566 (rows nil)
2567 (row-properties nil)
2568 (right-align nil))
2569
2570 (defun gdb-mapcar* (function &rest seqs)
2571 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2572 If there are several SEQS, FUNCTION is called with that many
2573 arguments, and mapping stops as soon as the shortest list runs
2574 out."
2575 (let ((shortest (apply #'min (mapcar #'length seqs))))
2576 (mapcar (lambda (i)
2577 (apply function
2578 (mapcar
2579 (lambda (seq)
2580 (nth i seq))
2581 seqs)))
2582 (number-sequence 0 (1- shortest)))))
2583
2584 (defun gdb-table-add-row (table row &optional properties)
2585 "Add ROW of string to TABLE and recalculate column sizes.
2586
2587 When non-nil, PROPERTIES will be added to the whole row when
2588 calling `gdb-table-string'."
2589 (let ((rows (gdb-table-rows table))
2590 (row-properties (gdb-table-row-properties table))
2591 (column-sizes (gdb-table-column-sizes table))
2592 (right-align (gdb-table-right-align table)))
2593 (when (not column-sizes)
2594 (setf (gdb-table-column-sizes table)
2595 (make-list (length row) 0)))
2596 (setf (gdb-table-rows table)
2597 (append rows (list row)))
2598 (setf (gdb-table-row-properties table)
2599 (append row-properties (list properties)))
2600 (setf (gdb-table-column-sizes table)
2601 (gdb-mapcar* (lambda (x s)
2602 (let ((new-x
2603 (max (abs x) (string-width (or s "")))))
2604 (if right-align new-x (- new-x))))
2605 (gdb-table-column-sizes table)
2606 row))
2607 ;; Avoid trailing whitespace at eol
2608 (if (not (gdb-table-right-align table))
2609 (setcar (last (gdb-table-column-sizes table)) 0))))
2610
2611 (defun gdb-table-string (table &optional sep)
2612 "Return TABLE as a string with columns separated with SEP."
2613 (let ((column-sizes (gdb-table-column-sizes table)))
2614 (mapconcat
2615 'identity
2616 (gdb-mapcar*
2617 (lambda (row properties)
2618 (apply 'propertize
2619 (mapconcat 'identity
2620 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2621 row column-sizes)
2622 sep)
2623 properties))
2624 (gdb-table-rows table)
2625 (gdb-table-row-properties table))
2626 "\n")))
2627
2628 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2629 (defun gdb-get-many-fields (struct &rest fields)
2630 "Return a list of FIELDS values from STRUCT."
2631 (let ((values))
2632 (dolist (field fields)
2633 (push (bindat-get-field struct field) values))
2634 (nreverse values)))
2635
2636 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2637 handler-name
2638 &optional signal-list)
2639 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2640 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2641 buffer with `gdb-bind-function-to-buffer'.
2642
2643 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2644 defined trigger is called with an argument from SIGNAL-LIST. It's
2645 not recommended to define triggers with empty SIGNAL-LIST.
2646 Normally triggers should respond at least to 'update signal.
2647
2648 Normally the trigger defined by this command must be called from
2649 the buffer where HANDLER-NAME must work. This should be done so
2650 that buffer-local thread number may be used in GDB-COMMAND (by
2651 calling `gdb-current-context-command').
2652 `gdb-bind-function-to-buffer' is used to achieve this, see
2653 `gdb-get-buffer-create'.
2654
2655 Triggers defined by this command are meant to be used as a
2656 trigger argument when describing buffer types with
2657 `gdb-set-buffer-rules'."
2658 `(defun ,trigger-name (&optional signal)
2659 (when
2660 (or (not ,signal-list)
2661 (memq signal ,signal-list))
2662 (when (not (gdb-pending-p
2663 (cons (current-buffer) ',trigger-name)))
2664 (gdb-input ,gdb-command
2665 (gdb-bind-function-to-buffer ',handler-name (current-buffer)))
2666 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2667
2668 ;; Used by disassembly buffer only, the rest use
2669 ;; def-gdb-trigger-and-handler
2670 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2671 &optional nopreserve)
2672 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2673
2674 Handlers are normally called from the buffers they put output in.
2675
2676 Delete ((current-buffer) . TRIGGER-NAME) from
2677 `gdb-pending-triggers', erase current buffer and evaluate
2678 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2679
2680 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2681 `(defun ,handler-name ()
2682 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2683 (let* ((inhibit-read-only t)
2684 ,@(unless nopreserve
2685 '((window (get-buffer-window (current-buffer) 0))
2686 (start (window-start window))
2687 (p (window-point window)))))
2688 (erase-buffer)
2689 (,custom-defun)
2690 (gdb-update-buffer-name)
2691 ,@(when (not nopreserve)
2692 '((set-window-start window start)
2693 (set-window-point window p))))))
2694
2695 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2696 handler-name custom-defun
2697 &optional signal-list)
2698 "Define trigger and handler.
2699
2700 TRIGGER-NAME trigger is defined to send GDB-COMMAND.
2701 See `def-gdb-auto-update-trigger'.
2702
2703 HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
2704 See `def-gdb-auto-update-handler'."
2705 `(progn
2706 (def-gdb-auto-update-trigger ,trigger-name
2707 ,gdb-command
2708 ,handler-name ,signal-list)
2709 (def-gdb-auto-update-handler ,handler-name
2710 ,trigger-name ,custom-defun)))
2711
2712 \f
2713
2714 ;; Breakpoint buffer : This displays the output of `-break-list'.
2715 (def-gdb-trigger-and-handler
2716 gdb-invalidate-breakpoints "-break-list"
2717 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2718 '(start update))
2719
2720 (gdb-set-buffer-rules
2721 'gdb-breakpoints-buffer
2722 'gdb-breakpoints-buffer-name
2723 'gdb-breakpoints-mode
2724 'gdb-invalidate-breakpoints)
2725
2726 (defun gdb-breakpoints-list-handler-custom ()
2727 (let ((breakpoints-list (bindat-get-field
2728 (gdb-json-partial-output "bkpt" "script")
2729 'BreakpointTable 'body))
2730 (table (make-gdb-table)))
2731 (setq gdb-breakpoints-list nil)
2732 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2733 (dolist (breakpoint breakpoints-list)
2734 (add-to-list 'gdb-breakpoints-list
2735 (cons (bindat-get-field breakpoint 'number)
2736 breakpoint))
2737 (let ((at (bindat-get-field breakpoint 'at))
2738 (pending (bindat-get-field breakpoint 'pending))
2739 (func (bindat-get-field breakpoint 'func))
2740 (type (bindat-get-field breakpoint 'type)))
2741 (gdb-table-add-row table
2742 (list
2743 (bindat-get-field breakpoint 'number)
2744 (or type "")
2745 (or (bindat-get-field breakpoint 'disp) "")
2746 (let ((flag (bindat-get-field breakpoint 'enabled)))
2747 (if (string-equal flag "y")
2748 (propertize "y" 'font-lock-face font-lock-warning-face)
2749 (propertize "n" 'font-lock-face font-lock-comment-face)))
2750 (bindat-get-field breakpoint 'addr)
2751 (or (bindat-get-field breakpoint 'times) "")
2752 (if (and type (string-match ".*watchpoint" type))
2753 (bindat-get-field breakpoint 'what)
2754 (or pending at
2755 (concat "in "
2756 (propertize (or func "unknown")
2757 'font-lock-face font-lock-function-name-face)
2758 (gdb-frame-location breakpoint)))))
2759 ;; Add clickable properties only for breakpoints with file:line
2760 ;; information
2761 (append (list 'gdb-breakpoint breakpoint)
2762 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2763 mouse-face highlight))))))
2764 (insert (gdb-table-string table " "))
2765 (gdb-place-breakpoints)))
2766
2767 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2768 (defun gdb-place-breakpoints ()
2769 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2770 (dolist (buffer (buffer-list))
2771 (with-current-buffer buffer
2772 (if (and (eq gud-minor-mode 'gdbmi)
2773 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2774 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2775 (dolist (breakpoint gdb-breakpoints-list)
2776 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2777 ; an associative list
2778 (line (bindat-get-field breakpoint 'line)))
2779 (when line
2780 (let ((file (bindat-get-field breakpoint 'fullname))
2781 (flag (bindat-get-field breakpoint 'enabled))
2782 (bptno (bindat-get-field breakpoint 'number)))
2783 (unless (and file (file-exists-p file))
2784 (setq file (cdr (assoc bptno gdb-location-alist))))
2785 (if (or (null file)
2786 (string-equal file "File not found"))
2787 ;; If the full filename is not recorded in the
2788 ;; breakpoint structure or in `gdb-location-alist', use
2789 ;; -file-list-exec-source-file to extract it.
2790 (when (setq file (bindat-get-field breakpoint 'file))
2791 (gdb-input (concat "list " file ":1") 'ignore)
2792 (gdb-input "-file-list-exec-source-file"
2793 `(lambda () (gdb-get-location
2794 ,bptno ,line ,flag))))
2795 (with-current-buffer (find-file-noselect file 'nowarn)
2796 (gdb-init-buffer)
2797 ;; Only want one breakpoint icon at each location.
2798 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2799 (string-to-number line)))))))))
2800
2801 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2802
2803 (defun gdb-get-location (bptno line flag)
2804 "Find the directory containing the relevant source file.
2805 Put in buffer and place breakpoint icon."
2806 (goto-char (point-min))
2807 (catch 'file-not-found
2808 (if (re-search-forward gdb-source-file-regexp nil t)
2809 (delete (cons bptno "File not found") gdb-location-alist)
2810 (push (cons bptno (match-string 1)) gdb-location-alist)
2811 (gdb-resync)
2812 (unless (assoc bptno gdb-location-alist)
2813 (push (cons bptno "File not found") gdb-location-alist)
2814 (message-box "Cannot find source file for breakpoint location.
2815 Add directory to search path for source files using the GDB command, dir."))
2816 (throw 'file-not-found nil))
2817 (with-current-buffer (find-file-noselect (match-string 1))
2818 (gdb-init-buffer)
2819 ;; only want one breakpoint icon at each location
2820 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2821
2822 (add-hook 'find-file-hook 'gdb-find-file-hook)
2823
2824 (defun gdb-find-file-hook ()
2825 "Set up buffer for debugging if file is part of the source code
2826 of the current session."
2827 (if (and (buffer-name gud-comint-buffer)
2828 ;; in case gud or gdb-ui is just loaded
2829 gud-comint-buffer
2830 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2831 'gdbmi))
2832 (if (member buffer-file-name gdb-source-file-list)
2833 (with-current-buffer (find-buffer-visiting buffer-file-name)
2834 (gdb-init-buffer)))))
2835
2836 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2837 (declare-function gud-break "gdb-mi" t t) ; gud-def
2838 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2839
2840 (defun gdb-mouse-set-clear-breakpoint (event)
2841 "Set/clear breakpoint in left fringe/margin at mouse click.
2842 If not in a source or disassembly buffer just set point."
2843 (interactive "e")
2844 (mouse-minibuffer-check event)
2845 (let ((posn (event-end event)))
2846 (with-selected-window (posn-window posn)
2847 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2848 (if (numberp (posn-point posn))
2849 (save-excursion
2850 (goto-char (posn-point posn))
2851 (if (or (posn-object posn)
2852 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2853 'breakpoint))
2854 (gud-remove nil)
2855 (gud-break nil)))))
2856 (posn-set-point posn))))
2857
2858 (defun gdb-mouse-toggle-breakpoint-margin (event)
2859 "Enable/disable breakpoint in left margin with mouse click."
2860 (interactive "e")
2861 (mouse-minibuffer-check event)
2862 (let ((posn (event-end event)))
2863 (if (numberp (posn-point posn))
2864 (with-selected-window (posn-window posn)
2865 (save-excursion
2866 (goto-char (posn-point posn))
2867 (if (posn-object posn)
2868 (gud-basic-call
2869 (let ((bptno (get-text-property
2870 0 'gdb-bptno (car (posn-string posn)))))
2871 (concat
2872 (if (get-text-property
2873 0 'gdb-enabled (car (posn-string posn)))
2874 "-break-disable "
2875 "-break-enable ")
2876 bptno)))))))))
2877
2878 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2879 "Enable/disable breakpoint in left fringe with mouse click."
2880 (interactive "e")
2881 (mouse-minibuffer-check event)
2882 (let* ((posn (event-end event))
2883 (pos (posn-point posn))
2884 obj)
2885 (when (numberp pos)
2886 (with-selected-window (posn-window posn)
2887 (with-current-buffer (window-buffer (selected-window))
2888 (goto-char pos)
2889 (dolist (overlay (overlays-in pos pos))
2890 (when (overlay-get overlay 'put-break)
2891 (setq obj (overlay-get overlay 'before-string))))
2892 (when (stringp obj)
2893 (gud-basic-call
2894 (concat
2895 (if (get-text-property 0 'gdb-enabled obj)
2896 "-break-disable "
2897 "-break-enable ")
2898 (get-text-property 0 'gdb-bptno obj)))))))))
2899
2900 (defun gdb-breakpoints-buffer-name ()
2901 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2902
2903 (defun gdb-display-breakpoints-buffer (&optional thread)
2904 "Display GDB breakpoints."
2905 (interactive)
2906 (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)))
2907
2908 (defun gdb-frame-breakpoints-buffer (&optional thread)
2909 "Display GDB breakpoints in another frame."
2910 (interactive)
2911 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)
2912 gdb-display-buffer-other-frame-action))
2913
2914 (defvar gdb-breakpoints-mode-map
2915 (let ((map (make-sparse-keymap))
2916 (menu (make-sparse-keymap "Breakpoints")))
2917 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2918 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2919 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2920 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2921 (suppress-keymap map)
2922 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2923 (define-key map " " 'gdb-toggle-breakpoint)
2924 (define-key map "D" 'gdb-delete-breakpoint)
2925 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2926 (define-key map "q" 'gdb-delete-frame-or-window)
2927 (define-key map "\r" 'gdb-goto-breakpoint)
2928 (define-key map "\t" (lambda ()
2929 (interactive)
2930 (gdb-set-window-buffer
2931 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2932 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2933 (define-key map [follow-link] 'mouse-face)
2934 map))
2935
2936 (defun gdb-delete-frame-or-window ()
2937 "Delete frame if there is only one window. Otherwise delete the window."
2938 (interactive)
2939 (if (one-window-p) (delete-frame)
2940 (delete-window)))
2941
2942 ;;from make-mode-line-mouse-map
2943 (defun gdb-make-header-line-mouse-map (mouse function) "\
2944 Return a keymap with single entry for mouse key MOUSE on the header line.
2945 MOUSE is defined to run function FUNCTION with no args in the buffer
2946 corresponding to the mode line clicked."
2947 (let ((map (make-sparse-keymap)))
2948 (define-key map (vector 'header-line mouse) function)
2949 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2950 map))
2951
2952 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2953 `(propertize ,name
2954 'help-echo ,help-echo
2955 'mouse-face ',mouse-face
2956 'face ',face
2957 'local-map
2958 (gdb-make-header-line-mouse-map
2959 'mouse-1
2960 (lambda (event) (interactive "e")
2961 (save-selected-window
2962 (select-window (posn-window (event-start event)))
2963 (gdb-set-window-buffer
2964 (gdb-get-buffer-create ',buffer) t) )))))
2965
2966 \f
2967 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2968 ;;; Threads view
2969
2970 (defun gdb-threads-buffer-name ()
2971 (concat "*threads of " (gdb-get-target-string) "*"))
2972
2973 (defun gdb-display-threads-buffer (&optional thread)
2974 "Display GDB threads."
2975 (interactive)
2976 (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
2977
2978 (defun gdb-frame-threads-buffer (&optional thread)
2979 "Display GDB threads in another frame."
2980 (interactive)
2981 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
2982 gdb-display-buffer-other-frame-action))
2983
2984 (def-gdb-trigger-and-handler
2985 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2986 gdb-thread-list-handler gdb-thread-list-handler-custom
2987 '(start update update-threads))
2988
2989 (gdb-set-buffer-rules
2990 'gdb-threads-buffer
2991 'gdb-threads-buffer-name
2992 'gdb-threads-mode
2993 'gdb-invalidate-threads)
2994
2995 (defvar gdb-threads-font-lock-keywords
2996 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2997 (" \\(stopped\\)" (1 font-lock-warning-face))
2998 (" \\(running\\)" (1 font-lock-string-face))
2999 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
3000 "Font lock keywords used in `gdb-threads-mode'.")
3001
3002 (defvar gdb-threads-mode-map
3003 (let ((map (make-sparse-keymap)))
3004 (define-key map "\r" 'gdb-select-thread)
3005 (define-key map "f" 'gdb-display-stack-for-thread)
3006 (define-key map "F" 'gdb-frame-stack-for-thread)
3007 (define-key map "l" 'gdb-display-locals-for-thread)
3008 (define-key map "L" 'gdb-frame-locals-for-thread)
3009 (define-key map "r" 'gdb-display-registers-for-thread)
3010 (define-key map "R" 'gdb-frame-registers-for-thread)
3011 (define-key map "d" 'gdb-display-disassembly-for-thread)
3012 (define-key map "D" 'gdb-frame-disassembly-for-thread)
3013 (define-key map "i" 'gdb-interrupt-thread)
3014 (define-key map "c" 'gdb-continue-thread)
3015 (define-key map "s" 'gdb-step-thread)
3016 (define-key map "\t"
3017 (lambda ()
3018 (interactive)
3019 (gdb-set-window-buffer
3020 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
3021 (define-key map [mouse-2] 'gdb-select-thread)
3022 (define-key map [follow-link] 'mouse-face)
3023 map))
3024
3025 (defvar gdb-threads-header
3026 (list
3027 (gdb-propertize-header
3028 "Breakpoints" gdb-breakpoints-buffer
3029 "mouse-1: select" mode-line-highlight mode-line-inactive)
3030 " "
3031 (gdb-propertize-header "Threads" gdb-threads-buffer
3032 nil nil mode-line)))
3033
3034 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
3035 "Major mode for GDB threads."
3036 (setq gdb-thread-position (make-marker))
3037 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
3038 (setq header-line-format gdb-threads-header)
3039 (set (make-local-variable 'font-lock-defaults)
3040 '(gdb-threads-font-lock-keywords))
3041 'gdb-invalidate-threads)
3042
3043 (defun gdb-thread-list-handler-custom ()
3044 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
3045 (table (make-gdb-table))
3046 (marked-line nil))
3047 (setq gdb-threads-list nil)
3048 (setq gdb-running-threads-count 0)
3049 (setq gdb-stopped-threads-count 0)
3050 (set-marker gdb-thread-position nil)
3051
3052 (dolist (thread (reverse threads-list))
3053 (let ((running (equal (bindat-get-field thread 'state) "running")))
3054 (add-to-list 'gdb-threads-list
3055 (cons (bindat-get-field thread 'id)
3056 thread))
3057 (cl-incf (if running
3058 gdb-running-threads-count
3059 gdb-stopped-threads-count))
3060
3061 (gdb-table-add-row
3062 table
3063 (list
3064 (bindat-get-field thread 'id)
3065 (concat
3066 (if gdb-thread-buffer-verbose-names
3067 (concat (bindat-get-field thread 'target-id) " ") "")
3068 (bindat-get-field thread 'state)
3069 ;; Include frame information for stopped threads
3070 (if (not running)
3071 (concat
3072 " in " (bindat-get-field thread 'frame 'func)
3073 (if gdb-thread-buffer-arguments
3074 (concat
3075 " ("
3076 (let ((args (bindat-get-field thread 'frame 'args)))
3077 (mapconcat
3078 (lambda (arg)
3079 (apply #'format "%s=%s"
3080 (gdb-get-many-fields arg 'name 'value)))
3081 args ","))
3082 ")")
3083 "")
3084 (if gdb-thread-buffer-locations
3085 (gdb-frame-location (bindat-get-field thread 'frame)) "")
3086 (if gdb-thread-buffer-addresses
3087 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
3088 "")))
3089 (list
3090 'gdb-thread thread
3091 'mouse-face 'highlight
3092 'help-echo "mouse-2, RET: select thread")))
3093 (when (string-equal gdb-thread-number
3094 (bindat-get-field thread 'id))
3095 (setq marked-line (length gdb-threads-list))))
3096 (insert (gdb-table-string table " "))
3097 (when marked-line
3098 (gdb-mark-line marked-line gdb-thread-position)))
3099 ;; We update gud-running here because we need to make sure that
3100 ;; gdb-threads-list is up-to-date
3101 (gdb-update-gud-running)
3102 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
3103
3104 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
3105 "Define a NAME command which will act upon thread on the current line.
3106
3107 CUSTOM-DEFUN may use locally bound `thread' variable, which will
3108 be the value of 'gdb-thread property of the current line.
3109 If `gdb-thread' is nil, error is signaled."
3110 `(defun ,name (&optional event)
3111 ,(when doc doc)
3112 (interactive (list last-input-event))
3113 (if event (posn-set-point (event-end event)))
3114 (save-excursion
3115 (beginning-of-line)
3116 (let ((thread (get-text-property (point) 'gdb-thread)))
3117 (if thread
3118 ,custom-defun
3119 (error "Not recognized as thread line"))))))
3120
3121 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
3122 &optional doc)
3123 "Define a NAME which will call BUFFER-COMMAND with id of thread
3124 on the current line."
3125 `(def-gdb-thread-buffer-command ,name
3126 (,buffer-command (bindat-get-field thread 'id))
3127 ,doc))
3128
3129 (def-gdb-thread-buffer-command gdb-select-thread
3130 (let ((new-id (bindat-get-field thread 'id)))
3131 (gdb-setq-thread-number new-id)
3132 (gdb-input (concat "-thread-select " new-id) 'ignore)
3133 (gdb-update))
3134 "Select the thread at current line of threads buffer.")
3135
3136 (def-gdb-thread-buffer-simple-command
3137 gdb-display-stack-for-thread
3138 gdb-preemptively-display-stack-buffer
3139 "Display stack buffer for the thread at current line.")
3140
3141 (def-gdb-thread-buffer-simple-command
3142 gdb-display-locals-for-thread
3143 gdb-preemptively-display-locals-buffer
3144 "Display locals buffer for the thread at current line.")
3145
3146 (def-gdb-thread-buffer-simple-command
3147 gdb-display-registers-for-thread
3148 gdb-preemptively-display-registers-buffer
3149 "Display registers buffer for the thread at current line.")
3150
3151 (def-gdb-thread-buffer-simple-command
3152 gdb-display-disassembly-for-thread
3153 gdb-preemptively-display-disassembly-buffer
3154 "Display disassembly buffer for the thread at current line.")
3155
3156 (def-gdb-thread-buffer-simple-command
3157 gdb-frame-stack-for-thread
3158 gdb-frame-stack-buffer
3159 "Display another frame with stack buffer for thread at current line.")
3160
3161 (def-gdb-thread-buffer-simple-command
3162 gdb-frame-locals-for-thread
3163 gdb-frame-locals-buffer
3164 "Display another frame with locals buffer for thread at current line.")
3165
3166 (def-gdb-thread-buffer-simple-command
3167 gdb-frame-registers-for-thread
3168 gdb-frame-registers-buffer
3169 "Display another frame with registers buffer for the thread at current line.")
3170
3171 (def-gdb-thread-buffer-simple-command
3172 gdb-frame-disassembly-for-thread
3173 gdb-frame-disassembly-buffer
3174 "Display another frame with disassembly buffer for the thread at current line.")
3175
3176 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
3177 "Define a NAME which will execute GUD-COMMAND with
3178 `gdb-thread-number' locally bound to id of thread on the current
3179 line."
3180 `(def-gdb-thread-buffer-command ,name
3181 (if gdb-non-stop
3182 (let ((gdb-thread-number (bindat-get-field thread 'id))
3183 (gdb-gud-control-all-threads nil))
3184 (call-interactively #',gud-command))
3185 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
3186 ,doc))
3187
3188 (def-gdb-thread-buffer-gud-command
3189 gdb-interrupt-thread
3190 gud-stop-subjob
3191 "Interrupt thread at current line.")
3192
3193 (def-gdb-thread-buffer-gud-command
3194 gdb-continue-thread
3195 gud-cont
3196 "Continue thread at current line.")
3197
3198 (def-gdb-thread-buffer-gud-command
3199 gdb-step-thread
3200 gud-step
3201 "Step thread at current line.")
3202
3203 \f
3204 ;;; Memory view
3205
3206 (defcustom gdb-memory-rows 8
3207 "Number of data rows in memory window."
3208 :type 'integer
3209 :group 'gud
3210 :version "23.2")
3211
3212 (defcustom gdb-memory-columns 4
3213 "Number of data columns in memory window."
3214 :type 'integer
3215 :group 'gud
3216 :version "23.2")
3217
3218 (defcustom gdb-memory-format "x"
3219 "Display format of data items in memory window."
3220 :type '(choice (const :tag "Hexadecimal" "x")
3221 (const :tag "Signed decimal" "d")
3222 (const :tag "Unsigned decimal" "u")
3223 (const :tag "Octal" "o")
3224 (const :tag "Binary" "t"))
3225 :group 'gud
3226 :version "22.1")
3227
3228 (defcustom gdb-memory-unit 4
3229 "Unit size of data items in memory window."
3230 :type '(choice (const :tag "Byte" 1)
3231 (const :tag "Halfword" 2)
3232 (const :tag "Word" 4)
3233 (const :tag "Giant word" 8))
3234 :group 'gud
3235 :version "23.2")
3236
3237 (def-gdb-trigger-and-handler
3238 gdb-invalidate-memory
3239 (format "-data-read-memory %s %s %d %d %d"
3240 gdb-memory-address
3241 gdb-memory-format
3242 gdb-memory-unit
3243 gdb-memory-rows
3244 gdb-memory-columns)
3245 gdb-read-memory-handler
3246 gdb-read-memory-custom
3247 '(start update))
3248
3249 (gdb-set-buffer-rules
3250 'gdb-memory-buffer
3251 'gdb-memory-buffer-name
3252 'gdb-memory-mode
3253 'gdb-invalidate-memory)
3254
3255 (defun gdb-memory-column-width (size format)
3256 "Return length of string with memory unit of SIZE in FORMAT.
3257
3258 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
3259 in `gdb-memory-format'."
3260 (let ((format-base (cdr (assoc format
3261 '(("x" . 16)
3262 ("d" . 10) ("u" . 10)
3263 ("o" . 8)
3264 ("t" . 2))))))
3265 (if format-base
3266 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
3267 (cond ((string-equal format "x")
3268 (+ 2 res)) ; hexadecimal numbers have 0x in front
3269 ((or (string-equal format "d")
3270 (string-equal format "o"))
3271 (1+ res))
3272 (t res)))
3273 (error "Unknown format"))))
3274
3275 (defun gdb-read-memory-custom ()
3276 (let* ((res (gdb-json-partial-output))
3277 (err-msg (bindat-get-field res 'msg)))
3278 (if (not err-msg)
3279 (let ((memory (bindat-get-field res 'memory)))
3280 (setq gdb-memory-address (bindat-get-field res 'addr))
3281 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3282 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3283 (setq gdb-memory-last-address gdb-memory-address)
3284 (dolist (row memory)
3285 (insert (concat (bindat-get-field row 'addr) ":"))
3286 (dolist (column (bindat-get-field row 'data))
3287 (insert (gdb-pad-string column
3288 (+ 2 (gdb-memory-column-width
3289 gdb-memory-unit
3290 gdb-memory-format)))))
3291 (newline)))
3292 ;; Show last page instead of empty buffer when out of bounds
3293 (progn
3294 (let ((gdb-memory-address gdb-memory-last-address))
3295 (gdb-invalidate-memory 'update)
3296 (error err-msg))))))
3297
3298 (defvar gdb-memory-mode-map
3299 (let ((map (make-sparse-keymap)))
3300 (suppress-keymap map t)
3301 (define-key map "q" 'kill-this-buffer)
3302 (define-key map "n" 'gdb-memory-show-next-page)
3303 (define-key map "p" 'gdb-memory-show-previous-page)
3304 (define-key map "a" 'gdb-memory-set-address)
3305 (define-key map "t" 'gdb-memory-format-binary)
3306 (define-key map "o" 'gdb-memory-format-octal)
3307 (define-key map "u" 'gdb-memory-format-unsigned)
3308 (define-key map "d" 'gdb-memory-format-signed)
3309 (define-key map "x" 'gdb-memory-format-hexadecimal)
3310 (define-key map "b" 'gdb-memory-unit-byte)
3311 (define-key map "h" 'gdb-memory-unit-halfword)
3312 (define-key map "w" 'gdb-memory-unit-word)
3313 (define-key map "g" 'gdb-memory-unit-giant)
3314 (define-key map "R" 'gdb-memory-set-rows)
3315 (define-key map "C" 'gdb-memory-set-columns)
3316 map))
3317
3318 (defun gdb-memory-set-address-event (event)
3319 "Handle a click on address field in memory buffer header."
3320 (interactive "e")
3321 (save-selected-window
3322 (select-window (posn-window (event-start event)))
3323 (gdb-memory-set-address)))
3324
3325 ;; Non-event version for use within keymap
3326 (defun gdb-memory-set-address ()
3327 "Set the start memory address."
3328 (interactive)
3329 (let ((arg (read-from-minibuffer "Memory address: ")))
3330 (setq gdb-memory-address arg))
3331 (gdb-invalidate-memory 'update))
3332
3333 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3334 "Define a function NAME which reads new VAR value from minibuffer."
3335 `(defun ,name (event)
3336 ,(when doc doc)
3337 (interactive "e")
3338 (save-selected-window
3339 (select-window (posn-window (event-start event)))
3340 (let* ((arg (read-from-minibuffer ,echo-string))
3341 (count (string-to-number arg)))
3342 (if (<= count 0)
3343 (error "Positive number only")
3344 (customize-set-variable ',variable count)
3345 (gdb-invalidate-memory 'update))))))
3346
3347 (def-gdb-set-positive-number
3348 gdb-memory-set-rows
3349 gdb-memory-rows
3350 "Rows: "
3351 "Set the number of data rows in memory window.")
3352
3353 (def-gdb-set-positive-number
3354 gdb-memory-set-columns
3355 gdb-memory-columns
3356 "Columns: "
3357 "Set the number of data columns in memory window.")
3358
3359 (defmacro def-gdb-memory-format (name format doc)
3360 "Define a function NAME to switch memory buffer to use FORMAT.
3361
3362 DOC is an optional documentation string."
3363 `(defun ,name () ,(when doc doc)
3364 (interactive)
3365 (customize-set-variable 'gdb-memory-format ,format)
3366 (gdb-invalidate-memory 'update)))
3367
3368 (def-gdb-memory-format
3369 gdb-memory-format-binary "t"
3370 "Set the display format to binary.")
3371
3372 (def-gdb-memory-format
3373 gdb-memory-format-octal "o"
3374 "Set the display format to octal.")
3375
3376 (def-gdb-memory-format
3377 gdb-memory-format-unsigned "u"
3378 "Set the display format to unsigned decimal.")
3379
3380 (def-gdb-memory-format
3381 gdb-memory-format-signed "d"
3382 "Set the display format to decimal.")
3383
3384 (def-gdb-memory-format
3385 gdb-memory-format-hexadecimal "x"
3386 "Set the display format to hexadecimal.")
3387
3388 (defvar gdb-memory-format-map
3389 (let ((map (make-sparse-keymap)))
3390 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3391 map)
3392 "Keymap to select format in the header line.")
3393
3394 (defvar gdb-memory-format-menu
3395 (let ((map (make-sparse-keymap "Format")))
3396
3397 (define-key map [binary]
3398 '(menu-item "Binary" gdb-memory-format-binary
3399 :button (:radio . (equal gdb-memory-format "t"))))
3400 (define-key map [octal]
3401 '(menu-item "Octal" gdb-memory-format-octal
3402 :button (:radio . (equal gdb-memory-format "o"))))
3403 (define-key map [unsigned]
3404 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3405 :button (:radio . (equal gdb-memory-format "u"))))
3406 (define-key map [signed]
3407 '(menu-item "Signed Decimal" gdb-memory-format-signed
3408 :button (:radio . (equal gdb-memory-format "d"))))
3409 (define-key map [hexadecimal]
3410 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3411 :button (:radio . (equal gdb-memory-format "x"))))
3412 map)
3413 "Menu of display formats in the header line.")
3414
3415 (defun gdb-memory-format-menu (event)
3416 (interactive "@e")
3417 (x-popup-menu event gdb-memory-format-menu))
3418
3419 (defun gdb-memory-format-menu-1 (event)
3420 (interactive "e")
3421 (save-selected-window
3422 (select-window (posn-window (event-start event)))
3423 (let* ((selection (gdb-memory-format-menu event))
3424 (binding (and selection (lookup-key gdb-memory-format-menu
3425 (vector (car selection))))))
3426 (if binding (call-interactively binding)))))
3427
3428 (defmacro def-gdb-memory-unit (name unit-size doc)
3429 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3430
3431 DOC is an optional documentation string."
3432 `(defun ,name () ,(when doc doc)
3433 (interactive)
3434 (customize-set-variable 'gdb-memory-unit ,unit-size)
3435 (gdb-invalidate-memory 'update)))
3436
3437 (def-gdb-memory-unit gdb-memory-unit-giant 8
3438 "Set the unit size to giant words (eight bytes).")
3439
3440 (def-gdb-memory-unit gdb-memory-unit-word 4
3441 "Set the unit size to words (four bytes).")
3442
3443 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3444 "Set the unit size to halfwords (two bytes).")
3445
3446 (def-gdb-memory-unit gdb-memory-unit-byte 1
3447 "Set the unit size to bytes.")
3448
3449 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3450 "Define a function NAME which show new address in memory buffer.
3451
3452 The defined function switches Memory buffer to show address
3453 stored in ADDRESS-VAR variable.
3454
3455 DOC is an optional documentation string."
3456 `(defun ,name
3457 ,(when doc doc)
3458 (interactive)
3459 (let ((gdb-memory-address ,address-var))
3460 (gdb-invalidate-memory))))
3461
3462 (def-gdb-memory-show-page gdb-memory-show-previous-page
3463 gdb-memory-prev-page)
3464
3465 (def-gdb-memory-show-page gdb-memory-show-next-page
3466 gdb-memory-next-page)
3467
3468 (defvar gdb-memory-unit-map
3469 (let ((map (make-sparse-keymap)))
3470 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3471 map)
3472 "Keymap to select units in the header line.")
3473
3474 (defvar gdb-memory-unit-menu
3475 (let ((map (make-sparse-keymap "Unit")))
3476 (define-key map [giantwords]
3477 '(menu-item "Giant words" gdb-memory-unit-giant
3478 :button (:radio . (equal gdb-memory-unit 8))))
3479 (define-key map [words]
3480 '(menu-item "Words" gdb-memory-unit-word
3481 :button (:radio . (equal gdb-memory-unit 4))))
3482 (define-key map [halfwords]
3483 '(menu-item "Halfwords" gdb-memory-unit-halfword
3484 :button (:radio . (equal gdb-memory-unit 2))))
3485 (define-key map [bytes]
3486 '(menu-item "Bytes" gdb-memory-unit-byte
3487 :button (:radio . (equal gdb-memory-unit 1))))
3488 map)
3489 "Menu of units in the header line.")
3490
3491 (defun gdb-memory-unit-menu (event)
3492 (interactive "@e")
3493 (x-popup-menu event gdb-memory-unit-menu))
3494
3495 (defun gdb-memory-unit-menu-1 (event)
3496 (interactive "e")
3497 (save-selected-window
3498 (select-window (posn-window (event-start event)))
3499 (let* ((selection (gdb-memory-unit-menu event))
3500 (binding (and selection (lookup-key gdb-memory-unit-menu
3501 (vector (car selection))))))
3502 (if binding (call-interactively binding)))))
3503
3504 (defvar gdb-memory-font-lock-keywords
3505 '(;; <__function.name+n>
3506 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3507 (1 font-lock-function-name-face)))
3508 "Font lock keywords used in `gdb-memory-mode'.")
3509
3510 (defvar gdb-memory-header
3511 '(:eval
3512 (concat
3513 "Start address["
3514 (propertize "-"
3515 'face font-lock-warning-face
3516 'help-echo "mouse-1: decrement address"
3517 'mouse-face 'mode-line-highlight
3518 'local-map (gdb-make-header-line-mouse-map
3519 'mouse-1
3520 #'gdb-memory-show-previous-page))
3521 "|"
3522 (propertize "+"
3523 'face font-lock-warning-face
3524 'help-echo "mouse-1: increment address"
3525 'mouse-face 'mode-line-highlight
3526 'local-map (gdb-make-header-line-mouse-map
3527 'mouse-1
3528 #'gdb-memory-show-next-page))
3529 "]: "
3530 (propertize gdb-memory-address
3531 'face font-lock-warning-face
3532 'help-echo "mouse-1: set start address"
3533 'mouse-face 'mode-line-highlight
3534 'local-map (gdb-make-header-line-mouse-map
3535 'mouse-1
3536 #'gdb-memory-set-address-event))
3537 " Rows: "
3538 (propertize (number-to-string gdb-memory-rows)
3539 'face font-lock-warning-face
3540 'help-echo "mouse-1: set number of columns"
3541 'mouse-face 'mode-line-highlight
3542 'local-map (gdb-make-header-line-mouse-map
3543 'mouse-1
3544 #'gdb-memory-set-rows))
3545 " Columns: "
3546 (propertize (number-to-string gdb-memory-columns)
3547 'face font-lock-warning-face
3548 'help-echo "mouse-1: set number of columns"
3549 'mouse-face 'mode-line-highlight
3550 'local-map (gdb-make-header-line-mouse-map
3551 'mouse-1
3552 #'gdb-memory-set-columns))
3553 " Display Format: "
3554 (propertize gdb-memory-format
3555 'face font-lock-warning-face
3556 'help-echo "mouse-3: select display format"
3557 'mouse-face 'mode-line-highlight
3558 'local-map gdb-memory-format-map)
3559 " Unit Size: "
3560 (propertize (number-to-string gdb-memory-unit)
3561 'face font-lock-warning-face
3562 'help-echo "mouse-3: select unit size"
3563 'mouse-face 'mode-line-highlight
3564 'local-map gdb-memory-unit-map)))
3565 "Header line used in `gdb-memory-mode'.")
3566
3567 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3568 "Major mode for examining memory."
3569 (setq header-line-format gdb-memory-header)
3570 (set (make-local-variable 'font-lock-defaults)
3571 '(gdb-memory-font-lock-keywords))
3572 'gdb-invalidate-memory)
3573
3574 (defun gdb-memory-buffer-name ()
3575 (concat "*memory of " (gdb-get-target-string) "*"))
3576
3577 (defun gdb-display-memory-buffer (&optional thread)
3578 "Display GDB memory contents."
3579 (interactive)
3580 (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
3581
3582 (defun gdb-frame-memory-buffer ()
3583 "Display memory contents in another frame."
3584 (interactive)
3585 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
3586 gdb-display-buffer-other-frame-action))
3587
3588 \f
3589 ;;; Disassembly view
3590
3591 (defun gdb-disassembly-buffer-name ()
3592 (gdb-current-context-buffer-name
3593 (concat "disassembly of " (gdb-get-target-string))))
3594
3595 (defun gdb-display-disassembly-buffer (&optional thread)
3596 "Display GDB disassembly information."
3597 (interactive)
3598 (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
3599
3600 (def-gdb-preempt-display-buffer
3601 gdb-preemptively-display-disassembly-buffer
3602 'gdb-disassembly-buffer)
3603
3604 (defun gdb-frame-disassembly-buffer (&optional thread)
3605 "Display GDB disassembly information in another frame."
3606 (interactive)
3607 (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
3608 gdb-display-buffer-other-frame-action))
3609
3610 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3611 (let* ((frame (gdb-current-buffer-frame))
3612 (file (bindat-get-field frame 'fullname))
3613 (line (bindat-get-field frame 'line)))
3614 (if file
3615 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3616 ;; If we're unable to get a file name / line for $PC, simply
3617 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3618 ;; 150 bytes) instructions.
3619 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3620 gdb-disassembly-handler
3621 ;; We update disassembly only after we have actual frame information
3622 ;; about all threads, so no there's `update' signal in this list
3623 '(start update-disassembly))
3624
3625 (def-gdb-auto-update-handler
3626 gdb-disassembly-handler
3627 gdb-invalidate-disassembly
3628 gdb-disassembly-handler-custom
3629 t)
3630
3631 (gdb-set-buffer-rules
3632 'gdb-disassembly-buffer
3633 'gdb-disassembly-buffer-name
3634 'gdb-disassembly-mode
3635 'gdb-invalidate-disassembly)
3636
3637 (defvar gdb-disassembly-font-lock-keywords
3638 '(;; <__function.name+n>
3639 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3640 (1 font-lock-function-name-face))
3641 ;; 0xNNNNNNNN <__function.name+n>: opcode
3642 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3643 (4 font-lock-keyword-face))
3644 ;; %register(at least i386)
3645 ("%\\sw+" . font-lock-variable-name-face)
3646 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3647 (1 font-lock-comment-face)
3648 (2 font-lock-function-name-face))
3649 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3650 "Font lock keywords used in `gdb-disassembly-mode'.")
3651
3652 (defvar gdb-disassembly-mode-map
3653 ;; TODO
3654 (let ((map (make-sparse-keymap)))
3655 (suppress-keymap map)
3656 (define-key map "q" 'kill-this-buffer)
3657 map))
3658
3659 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3660 "Major mode for GDB disassembly information."
3661 ;; TODO Rename overlay variable for disassembly mode
3662 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3663 (setq fringes-outside-margins t)
3664 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3665 (set (make-local-variable 'font-lock-defaults)
3666 '(gdb-disassembly-font-lock-keywords))
3667 'gdb-invalidate-disassembly)
3668
3669 (defun gdb-disassembly-handler-custom ()
3670 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3671 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3672 (table (make-gdb-table))
3673 (marked-line nil))
3674 (dolist (instr instructions)
3675 (gdb-table-add-row table
3676 (list
3677 (bindat-get-field instr 'address)
3678 (let
3679 ((func-name (bindat-get-field instr 'func-name))
3680 (offset (bindat-get-field instr 'offset)))
3681 (if func-name
3682 (format "<%s+%s>:" func-name offset)
3683 ""))
3684 (bindat-get-field instr 'inst)))
3685 (when (string-equal (bindat-get-field instr 'address)
3686 address)
3687 (progn
3688 (setq marked-line (length (gdb-table-rows table)))
3689 (setq fringe-indicator-alist
3690 (if (string-equal gdb-frame-number "0")
3691 nil
3692 '((overlay-arrow . hollow-right-triangle)))))))
3693 (insert (gdb-table-string table " "))
3694 (gdb-disassembly-place-breakpoints)
3695 ;; Mark current position with overlay arrow and scroll window to
3696 ;; that point
3697 (when marked-line
3698 (let ((window (get-buffer-window (current-buffer) 0)))
3699 (set-window-point window (gdb-mark-line marked-line
3700 gdb-disassembly-position))))
3701 (setq mode-name
3702 (gdb-current-context-mode-name
3703 (concat "Disassembly: "
3704 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3705
3706 (defun gdb-disassembly-place-breakpoints ()
3707 (gdb-remove-breakpoint-icons (point-min) (point-max))
3708 (dolist (breakpoint gdb-breakpoints-list)
3709 (let* ((breakpoint (cdr breakpoint))
3710 (bptno (bindat-get-field breakpoint 'number))
3711 (flag (bindat-get-field breakpoint 'enabled))
3712 (address (bindat-get-field breakpoint 'addr)))
3713 (save-excursion
3714 (goto-char (point-min))
3715 (if (re-search-forward (concat "^" address) nil t)
3716 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3717
3718 \f
3719 (defvar gdb-breakpoints-header
3720 (list
3721 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3722 nil nil mode-line)
3723 " "
3724 (gdb-propertize-header "Threads" gdb-threads-buffer
3725 "mouse-1: select" mode-line-highlight
3726 mode-line-inactive)))
3727
3728 ;;; Breakpoints view
3729 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3730 "Major mode for gdb breakpoints."
3731 (setq header-line-format gdb-breakpoints-header)
3732 'gdb-invalidate-breakpoints)
3733
3734 (defun gdb-toggle-breakpoint ()
3735 "Enable/disable breakpoint at current line of breakpoints buffer."
3736 (interactive)
3737 (save-excursion
3738 (beginning-of-line)
3739 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3740 (if breakpoint
3741 (gud-basic-call
3742 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3743 "-break-disable "
3744 "-break-enable ")
3745 (bindat-get-field breakpoint 'number)))
3746 (error "Not recognized as break/watchpoint line")))))
3747
3748 (defun gdb-delete-breakpoint ()
3749 "Delete the breakpoint at current line of breakpoints buffer."
3750 (interactive)
3751 (save-excursion
3752 (beginning-of-line)
3753 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3754 (if breakpoint
3755 (gud-basic-call (concat "-break-delete "
3756 (bindat-get-field breakpoint 'number)))
3757 (error "Not recognized as break/watchpoint line")))))
3758
3759 (defun gdb-goto-breakpoint (&optional event)
3760 "Go to the location of breakpoint at current line of breakpoints buffer."
3761 (interactive (list last-input-event))
3762 (if event (posn-set-point (event-end event)))
3763 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3764 (let ((window (get-buffer-window gud-comint-buffer)))
3765 (if window (save-selected-window (select-window window))))
3766 (save-excursion
3767 (beginning-of-line)
3768 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3769 (if breakpoint
3770 (let ((bptno (bindat-get-field breakpoint 'number))
3771 (file (bindat-get-field breakpoint 'fullname))
3772 (line (bindat-get-field breakpoint 'line)))
3773 (save-selected-window
3774 (let* ((buffer (find-file-noselect
3775 (if (file-exists-p file) file
3776 (cdr (assoc bptno gdb-location-alist)))))
3777 (window (or (gdb-display-source-buffer buffer)
3778 (display-buffer buffer))))
3779 (setq gdb-source-window window)
3780 (with-current-buffer buffer
3781 (goto-char (point-min))
3782 (forward-line (1- (string-to-number line)))
3783 (set-window-point window (point))))))
3784 (error "Not recognized as break/watchpoint line")))))
3785
3786 \f
3787 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3788 ;;
3789 (def-gdb-trigger-and-handler
3790 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3791 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3792 '(start update))
3793
3794 (gdb-set-buffer-rules
3795 'gdb-stack-buffer
3796 'gdb-stack-buffer-name
3797 'gdb-frames-mode
3798 'gdb-invalidate-frames)
3799
3800 (defun gdb-frame-location (frame)
3801 "Return \" of file:line\" or \" of library\" for structure FRAME.
3802
3803 FRAME must have either \"file\" and \"line\" members or \"from\"
3804 member."
3805 (let ((file (bindat-get-field frame 'file))
3806 (line (bindat-get-field frame 'line))
3807 (from (bindat-get-field frame 'from)))
3808 (let ((res (or (and file line (concat file ":" line))
3809 from)))
3810 (if res (concat " of " res) ""))))
3811
3812 (defun gdb-stack-list-frames-custom ()
3813 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3814 (table (make-gdb-table)))
3815 (set-marker gdb-stack-position nil)
3816 (dolist (frame stack)
3817 (gdb-table-add-row table
3818 (list
3819 (bindat-get-field frame 'level)
3820 "in"
3821 (concat
3822 (bindat-get-field frame 'func)
3823 (if gdb-stack-buffer-locations
3824 (gdb-frame-location frame) "")
3825 (if gdb-stack-buffer-addresses
3826 (concat " at " (bindat-get-field frame 'addr)) "")))
3827 `(mouse-face highlight
3828 help-echo "mouse-2, RET: Select frame"
3829 gdb-frame ,frame)))
3830 (insert (gdb-table-string table " ")))
3831 (when (and gdb-frame-number
3832 (gdb-buffer-shows-main-thread-p))
3833 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3834 gdb-stack-position))
3835 (setq mode-name
3836 (gdb-current-context-mode-name "Frames")))
3837
3838 (defun gdb-stack-buffer-name ()
3839 (gdb-current-context-buffer-name
3840 (concat "stack frames of " (gdb-get-target-string))))
3841
3842 (defun gdb-display-stack-buffer (&optional thread)
3843 "Display GDB backtrace for current stack."
3844 (interactive)
3845 (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
3846
3847 (def-gdb-preempt-display-buffer
3848 gdb-preemptively-display-stack-buffer
3849 'gdb-stack-buffer nil t)
3850
3851 (defun gdb-frame-stack-buffer (&optional thread)
3852 "Display GDB backtrace for current stack in another frame."
3853 (interactive)
3854 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
3855 gdb-display-buffer-other-frame-action))
3856
3857 (defvar gdb-frames-mode-map
3858 (let ((map (make-sparse-keymap)))
3859 (suppress-keymap map)
3860 (define-key map "q" 'kill-this-buffer)
3861 (define-key map "\r" 'gdb-select-frame)
3862 (define-key map [mouse-2] 'gdb-select-frame)
3863 (define-key map [follow-link] 'mouse-face)
3864 map))
3865
3866 (defvar gdb-frames-font-lock-keywords
3867 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3868 "Font lock keywords used in `gdb-frames-mode'.")
3869
3870 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3871 "Major mode for gdb call stack."
3872 (setq gdb-stack-position (make-marker))
3873 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3874 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3875 (set (make-local-variable 'font-lock-defaults)
3876 '(gdb-frames-font-lock-keywords))
3877 'gdb-invalidate-frames)
3878
3879 (defun gdb-select-frame (&optional event)
3880 "Select the frame and display the relevant source."
3881 (interactive (list last-input-event))
3882 (if event (posn-set-point (event-end event)))
3883 (let ((frame (get-text-property (point) 'gdb-frame)))
3884 (if frame
3885 (if (gdb-buffer-shows-main-thread-p)
3886 (let ((new-level (bindat-get-field frame 'level)))
3887 (setq gdb-frame-number new-level)
3888 (gdb-input (concat "-stack-select-frame " new-level)
3889 'ignore)
3890 (gdb-update))
3891 (error "Could not select frame for non-current thread"))
3892 (error "Not recognized as frame line"))))
3893
3894 \f
3895 ;; Locals buffer.
3896 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3897 (def-gdb-trigger-and-handler
3898 gdb-invalidate-locals
3899 (concat (gdb-current-context-command "-stack-list-locals")
3900 " --simple-values")
3901 gdb-locals-handler gdb-locals-handler-custom
3902 '(start update))
3903
3904 (gdb-set-buffer-rules
3905 'gdb-locals-buffer
3906 'gdb-locals-buffer-name
3907 'gdb-locals-mode
3908 'gdb-invalidate-locals)
3909
3910 (defvar gdb-locals-watch-map
3911 (let ((map (make-sparse-keymap)))
3912 (suppress-keymap map)
3913 (define-key map "\r" 'gud-watch)
3914 (define-key map [mouse-2] 'gud-watch)
3915 map)
3916 "Keymap to create watch expression of a complex data type local variable.")
3917
3918 (defvar gdb-edit-locals-map-1
3919 (let ((map (make-sparse-keymap)))
3920 (suppress-keymap map)
3921 (define-key map "\r" 'gdb-edit-locals-value)
3922 (define-key map [mouse-2] 'gdb-edit-locals-value)
3923 map)
3924 "Keymap to edit value of a simple data type local variable.")
3925
3926 (defun gdb-edit-locals-value (&optional event)
3927 "Assign a value to a variable displayed in the locals buffer."
3928 (interactive (list last-input-event))
3929 (save-excursion
3930 (if event (posn-set-point (event-end event)))
3931 (beginning-of-line)
3932 (let* ((var (bindat-get-field
3933 (get-text-property (point) 'gdb-local-variable) 'name))
3934 (value (read-string (format "New value (%s): " var))))
3935 (gud-basic-call
3936 (concat "-gdb-set variable " var " = " value)))))
3937
3938 ;; Don't display values of arrays or structures.
3939 ;; These can be expanded using gud-watch.
3940 (defun gdb-locals-handler-custom ()
3941 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3942 (table (make-gdb-table)))
3943 (dolist (local locals-list)
3944 (let ((name (bindat-get-field local 'name))
3945 (value (bindat-get-field local 'value))
3946 (type (bindat-get-field local 'type)))
3947 (if (or (not value)
3948 (string-match "\\0x" value))
3949 (add-text-properties 0 (length name)
3950 `(mouse-face highlight
3951 help-echo "mouse-2: create watch expression"
3952 local-map ,gdb-locals-watch-map)
3953 name)
3954 (add-text-properties 0 (length value)
3955 `(mouse-face highlight
3956 help-echo "mouse-2: edit value"
3957 local-map ,gdb-edit-locals-map-1)
3958 value))
3959 (gdb-table-add-row
3960 table
3961 (list
3962 (propertize type 'font-lock-face font-lock-type-face)
3963 (propertize name 'font-lock-face font-lock-variable-name-face)
3964 value)
3965 `(gdb-local-variable ,local))))
3966 (insert (gdb-table-string table " "))
3967 (setq mode-name
3968 (gdb-current-context-mode-name
3969 (concat "Locals: "
3970 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3971
3972 (defvar gdb-locals-header
3973 (list
3974 (gdb-propertize-header "Locals" gdb-locals-buffer
3975 nil nil mode-line)
3976 " "
3977 (gdb-propertize-header "Registers" gdb-registers-buffer
3978 "mouse-1: select" mode-line-highlight
3979 mode-line-inactive)))
3980
3981 (defvar gdb-locals-mode-map
3982 (let ((map (make-sparse-keymap)))
3983 (suppress-keymap map)
3984 (define-key map "q" 'kill-this-buffer)
3985 (define-key map "\t" (lambda ()
3986 (interactive)
3987 (gdb-set-window-buffer
3988 (gdb-get-buffer-create
3989 'gdb-registers-buffer
3990 gdb-thread-number) t)))
3991 map))
3992
3993 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3994 "Major mode for gdb locals."
3995 (setq header-line-format gdb-locals-header)
3996 'gdb-invalidate-locals)
3997
3998 (defun gdb-locals-buffer-name ()
3999 (gdb-current-context-buffer-name
4000 (concat "locals of " (gdb-get-target-string))))
4001
4002 (defun gdb-display-locals-buffer (&optional thread)
4003 "Display the local variables of current GDB stack."
4004 (interactive)
4005 (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
4006
4007 (def-gdb-preempt-display-buffer
4008 gdb-preemptively-display-locals-buffer
4009 'gdb-locals-buffer nil t)
4010
4011 (defun gdb-frame-locals-buffer (&optional thread)
4012 "Display the local variables of the current GDB stack in another frame."
4013 (interactive)
4014 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
4015 gdb-display-buffer-other-frame-action))
4016
4017 \f
4018 ;; Registers buffer.
4019
4020 (def-gdb-trigger-and-handler
4021 gdb-invalidate-registers
4022 (concat (gdb-current-context-command "-data-list-register-values") " x")
4023 gdb-registers-handler
4024 gdb-registers-handler-custom
4025 '(start update))
4026
4027 (gdb-set-buffer-rules
4028 'gdb-registers-buffer
4029 'gdb-registers-buffer-name
4030 'gdb-registers-mode
4031 'gdb-invalidate-registers)
4032
4033 (defun gdb-registers-handler-custom ()
4034 (when gdb-register-names
4035 (let ((register-values
4036 (bindat-get-field (gdb-json-partial-output) 'register-values))
4037 (table (make-gdb-table)))
4038 (dolist (register register-values)
4039 (let* ((register-number (bindat-get-field register 'number))
4040 (value (bindat-get-field register 'value))
4041 (register-name (nth (string-to-number register-number)
4042 gdb-register-names)))
4043 (gdb-table-add-row
4044 table
4045 (list
4046 (propertize register-name
4047 'font-lock-face font-lock-variable-name-face)
4048 (if (member register-number gdb-changed-registers)
4049 (propertize value 'font-lock-face font-lock-warning-face)
4050 value))
4051 `(mouse-face highlight
4052 help-echo "mouse-2: edit value"
4053 gdb-register-name ,register-name))))
4054 (insert (gdb-table-string table " ")))
4055 (setq mode-name
4056 (gdb-current-context-mode-name "Registers"))))
4057
4058 (defun gdb-edit-register-value (&optional event)
4059 "Assign a value to a register displayed in the registers buffer."
4060 (interactive (list last-input-event))
4061 (save-excursion
4062 (if event (posn-set-point (event-end event)))
4063 (beginning-of-line)
4064 (let* ((var (bindat-get-field
4065 (get-text-property (point) 'gdb-register-name)))
4066 (value (read-string (format "New value (%s): " var))))
4067 (gud-basic-call
4068 (concat "-gdb-set variable $" var " = " value)))))
4069
4070 (defvar gdb-registers-mode-map
4071 (let ((map (make-sparse-keymap)))
4072 (suppress-keymap map)
4073 (define-key map "\r" 'gdb-edit-register-value)
4074 (define-key map [mouse-2] 'gdb-edit-register-value)
4075 (define-key map "q" 'kill-this-buffer)
4076 (define-key map "\t" (lambda ()
4077 (interactive)
4078 (gdb-set-window-buffer
4079 (gdb-get-buffer-create
4080 'gdb-locals-buffer
4081 gdb-thread-number) t)))
4082 map))
4083
4084 (defvar gdb-registers-header
4085 (list
4086 (gdb-propertize-header "Locals" gdb-locals-buffer
4087 "mouse-1: select" mode-line-highlight
4088 mode-line-inactive)
4089 " "
4090 (gdb-propertize-header "Registers" gdb-registers-buffer
4091 nil nil mode-line)))
4092
4093 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
4094 "Major mode for gdb registers."
4095 (setq header-line-format gdb-registers-header)
4096 'gdb-invalidate-registers)
4097
4098 (defun gdb-registers-buffer-name ()
4099 (gdb-current-context-buffer-name
4100 (concat "registers of " (gdb-get-target-string))))
4101
4102 (defun gdb-display-registers-buffer (&optional thread)
4103 "Display GDB register contents."
4104 (interactive)
4105 (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
4106
4107 (def-gdb-preempt-display-buffer
4108 gdb-preemptively-display-registers-buffer
4109 'gdb-registers-buffer nil t)
4110
4111 (defun gdb-frame-registers-buffer (&optional thread)
4112 "Display GDB register contents in another frame."
4113 (interactive)
4114 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
4115 gdb-display-buffer-other-frame-action))
4116
4117 ;; Needs GDB 6.4 onwards (used to fail with no stack).
4118 (defun gdb-get-changed-registers ()
4119 (when (and (gdb-get-buffer 'gdb-registers-buffer)
4120 (not (gdb-pending-p 'gdb-get-changed-registers)))
4121 (gdb-input "-data-list-changed-registers"
4122 'gdb-changed-registers-handler)
4123 (gdb-add-pending 'gdb-get-changed-registers)))
4124
4125 (defun gdb-changed-registers-handler ()
4126 (gdb-delete-pending 'gdb-get-changed-registers)
4127 (setq gdb-changed-registers nil)
4128 (dolist (register-number
4129 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
4130 (push register-number gdb-changed-registers)))
4131
4132 (defun gdb-register-names-handler ()
4133 ;; Don't use gdb-pending-triggers because this handler is called
4134 ;; only once (in gdb-init-1)
4135 (setq gdb-register-names nil)
4136 (dolist (register-name
4137 (bindat-get-field (gdb-json-partial-output) 'register-names))
4138 (push register-name gdb-register-names))
4139 (setq gdb-register-names (reverse gdb-register-names)))
4140 \f
4141
4142 (defun gdb-get-source-file-list ()
4143 "Create list of source files for current GDB session.
4144 If buffers already exist for any of these files, `gud-minor-mode'
4145 is set in them."
4146 (goto-char (point-min))
4147 (while (re-search-forward gdb-source-file-regexp nil t)
4148 (push (match-string 1) gdb-source-file-list))
4149 (dolist (buffer (buffer-list))
4150 (with-current-buffer buffer
4151 (when (member buffer-file-name gdb-source-file-list)
4152 (gdb-init-buffer)))))
4153
4154 (defun gdb-get-main-selected-frame ()
4155 "Trigger for `gdb-frame-handler' which uses main current thread.
4156 Called from `gdb-update'."
4157 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
4158 (progn
4159 (gdb-input (gdb-current-context-command "-stack-info-frame")
4160 'gdb-frame-handler)
4161 (gdb-add-pending 'gdb-get-main-selected-frame))))
4162
4163 (defun gdb-frame-handler ()
4164 "Set `gdb-selected-frame' and `gdb-selected-file' to show
4165 overlay arrow in source buffer."
4166 (gdb-delete-pending 'gdb-get-main-selected-frame)
4167 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
4168 (when frame
4169 (setq gdb-selected-frame (bindat-get-field frame 'func))
4170 (setq gdb-selected-file (bindat-get-field frame 'fullname))
4171 (setq gdb-frame-number (bindat-get-field frame 'level))
4172 (setq gdb-frame-address (bindat-get-field frame 'addr))
4173 (let ((line (bindat-get-field frame 'line)))
4174 (setq gdb-selected-line (and line (string-to-number line)))
4175 (when (and gdb-selected-file gdb-selected-line)
4176 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
4177 (gud-display-frame)))
4178 (if gud-overlay-arrow-position
4179 (let ((buffer (marker-buffer gud-overlay-arrow-position))
4180 (position (marker-position gud-overlay-arrow-position)))
4181 (when buffer
4182 (with-current-buffer buffer
4183 (setq fringe-indicator-alist
4184 (if (string-equal gdb-frame-number "0")
4185 nil
4186 '((overlay-arrow . hollow-right-triangle))))
4187 (setq gud-overlay-arrow-position (make-marker))
4188 (set-marker gud-overlay-arrow-position position))))))))
4189
4190 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
4191
4192 (defun gdb-get-prompt ()
4193 "Find prompt for GDB session."
4194 (goto-char (point-min))
4195 (setq gdb-prompt-name nil)
4196 (re-search-forward gdb-prompt-name-regexp nil t)
4197 (setq gdb-prompt-name (match-string 1))
4198 ;; Insert first prompt.
4199 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
4200
4201 ;;;; Window management
4202 (defun gdb-display-buffer (buf)
4203 "Show buffer BUF, and make that window dedicated."
4204 (let ((window (display-buffer buf)))
4205 (set-window-dedicated-p window t)
4206 window))
4207
4208 ;; (let ((answer (get-buffer-window buf 0)))
4209 ;; (if answer
4210 ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
4211 ;; (let ((window (get-lru-window)))
4212 ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
4213 ;; 'gdbmi)
4214 ;; (let ((largest (get-largest-window)))
4215 ;; (setq answer (split-window largest))
4216 ;; (set-window-buffer answer buf)
4217 ;; (set-window-dedicated-p answer t)
4218 ;; answer)
4219 ;; (set-window-buffer window buf)
4220 ;; window)))))
4221
4222
4223 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
4224 "Find window displaying a buffer with the same
4225 `gdb-buffer-type' as BUF and show BUF there. If no such window
4226 exists, just call `gdb-display-buffer' for BUF. If the window
4227 found is already dedicated, split window according to
4228 SPLIT-HORIZONTAL and show BUF in the new window."
4229 (if buf
4230 (when (not (get-buffer-window buf))
4231 (let* ((buf-type (gdb-buffer-type buf))
4232 (existing-window
4233 (get-window-with-predicate
4234 #'(lambda (w)
4235 (and (eq buf-type
4236 (gdb-buffer-type (window-buffer w)))
4237 (not (window-dedicated-p w)))))))
4238 (if existing-window
4239 (set-window-buffer existing-window buf)
4240 (let ((dedicated-window
4241 (get-window-with-predicate
4242 #'(lambda (w)
4243 (eq buf-type
4244 (gdb-buffer-type (window-buffer w)))))))
4245 (if dedicated-window
4246 (set-window-buffer
4247 (split-window dedicated-window nil split-horizontal) buf)
4248 (gdb-display-buffer buf))))))
4249 (error "Null buffer")))
4250 \f
4251 ;;; Shared keymap initialization:
4252
4253 (let ((menu (make-sparse-keymap "GDB-Windows")))
4254 (define-key gud-menu-map [displays]
4255 `(menu-item "GDB-Windows" ,menu
4256 :visible (eq gud-minor-mode 'gdbmi)))
4257 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
4258 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
4259 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
4260 (define-key menu [disassembly]
4261 '("Disassembly" . gdb-display-disassembly-buffer))
4262 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
4263 (define-key menu [inferior]
4264 '("IO" . gdb-display-io-buffer))
4265 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
4266 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
4267 (define-key menu [breakpoints]
4268 '("Breakpoints" . gdb-display-breakpoints-buffer)))
4269
4270 (let ((menu (make-sparse-keymap "GDB-Frames")))
4271 (define-key gud-menu-map [frames]
4272 `(menu-item "GDB-Frames" ,menu
4273 :visible (eq gud-minor-mode 'gdbmi)))
4274 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
4275 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
4276 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4277 (define-key menu [disassembly]
4278 '("Disassembly" . gdb-frame-disassembly-buffer))
4279 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4280 (define-key menu [inferior]
4281 '("IO" . gdb-frame-io-buffer))
4282 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4283 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4284 (define-key menu [breakpoints]
4285 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4286
4287 (let ((menu (make-sparse-keymap "GDB-MI")))
4288 (define-key menu [gdb-customize]
4289 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4290 :help "Customize Gdb Graphical Mode options."))
4291 (define-key menu [gdb-many-windows]
4292 '(menu-item "Display Other Windows" gdb-many-windows
4293 :help "Toggle display of locals, stack and breakpoint information"
4294 :button (:toggle . gdb-many-windows)))
4295 (define-key menu [gdb-restore-windows]
4296 '(menu-item "Restore Window Layout" gdb-restore-windows
4297 :help "Restore standard layout for debug session."))
4298 (define-key menu [sep1]
4299 '(menu-item "--"))
4300 (define-key menu [all-threads]
4301 '(menu-item "GUD controls all threads"
4302 (lambda ()
4303 (interactive)
4304 (setq gdb-gud-control-all-threads t))
4305 :help "GUD start/stop commands apply to all threads"
4306 :button (:radio . gdb-gud-control-all-threads)))
4307 (define-key menu [current-thread]
4308 '(menu-item "GUD controls current thread"
4309 (lambda ()
4310 (interactive)
4311 (setq gdb-gud-control-all-threads nil))
4312 :help "GUD start/stop commands apply to current thread only"
4313 :button (:radio . (not gdb-gud-control-all-threads))))
4314 (define-key menu [sep2]
4315 '(menu-item "--"))
4316 (define-key menu [gdb-customize-reasons]
4317 '(menu-item "Customize switching..."
4318 (lambda ()
4319 (interactive)
4320 (customize-option 'gdb-switch-reasons))))
4321 (define-key menu [gdb-switch-when-another-stopped]
4322 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4323 gdb-switch-when-another-stopped
4324 "Automatically switch to stopped thread"
4325 "GDB thread switching %s"
4326 "Switch to stopped thread"))
4327 (define-key gud-menu-map [mi]
4328 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4329
4330 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4331 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4332 ;; tool-bar-local-item-from-menu here because it appends new buttons
4333 ;; to toolbar from right to left while we want our A/T throttle to
4334 ;; show up right before Run button.
4335 (define-key-after gud-tool-bar-map [all-threads]
4336 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4337 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4338 :visible (and (eq gud-minor-mode 'gdbmi)
4339 gdb-non-stop
4340 (not gdb-gud-control-all-threads)))
4341 'run)
4342
4343 (define-key-after gud-tool-bar-map [current-thread]
4344 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4345 :image (find-image '((:type xpm :file "gud/all.xpm")))
4346 :visible (and (eq gud-minor-mode 'gdbmi)
4347 gdb-non-stop
4348 gdb-gud-control-all-threads))
4349 'all-threads)
4350
4351 (defun gdb-frame-gdb-buffer ()
4352 "Display GUD buffer in another frame."
4353 (interactive)
4354 (display-buffer-other-frame gud-comint-buffer))
4355
4356 (defun gdb-display-gdb-buffer ()
4357 "Display GUD buffer."
4358 (interactive)
4359 (pop-to-buffer gud-comint-buffer nil 0))
4360
4361 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4362 "Set buffer of selected window to NAME and dedicate window.
4363
4364 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4365 window is dedicated."
4366 (unless window (setq window (selected-window)))
4367 (when ignore-dedicated
4368 (set-window-dedicated-p window nil))
4369 (set-window-buffer window (get-buffer name))
4370 (set-window-dedicated-p window t))
4371
4372 (defun gdb-setup-windows ()
4373 "Layout the window pattern for option `gdb-many-windows'."
4374 (gdb-get-buffer-create 'gdb-locals-buffer)
4375 (gdb-get-buffer-create 'gdb-stack-buffer)
4376 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4377 (set-window-dedicated-p (selected-window) nil)
4378 (switch-to-buffer gud-comint-buffer)
4379 (delete-other-windows)
4380 (let ((win0 (selected-window))
4381 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4382 (win2 (split-window nil ( / (window-height) 3)))
4383 (win3 (split-window-right)))
4384 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4385 (select-window win2)
4386 (set-window-buffer
4387 win2
4388 (if gud-last-last-frame
4389 (gud-find-file (car gud-last-last-frame))
4390 (if gdb-main-file
4391 (gud-find-file gdb-main-file)
4392 ;; Put buffer list in window if we
4393 ;; can't find a source file.
4394 (list-buffers-noselect))))
4395 (setq gdb-source-window (selected-window))
4396 (let ((win4 (split-window-right)))
4397 (gdb-set-window-buffer
4398 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4399 (select-window win1)
4400 (gdb-set-window-buffer (gdb-stack-buffer-name))
4401 (let ((win5 (split-window-right)))
4402 (gdb-set-window-buffer (if gdb-show-threads-by-default
4403 (gdb-threads-buffer-name)
4404 (gdb-breakpoints-buffer-name))
4405 nil win5))
4406 (select-window win0)))
4407
4408 (define-minor-mode gdb-many-windows
4409 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4410 In this case it starts with two windows: one displaying the GUD
4411 buffer and the other with the source file with the main routine
4412 of the debugged program. Non-nil means display the layout shown for
4413 `gdb'."
4414 :global t
4415 :group 'gdb
4416 :version "22.1"
4417 (if (and gud-comint-buffer
4418 (buffer-name gud-comint-buffer))
4419 (ignore-errors
4420 (gdb-restore-windows))))
4421
4422 (defun gdb-restore-windows ()
4423 "Restore the basic arrangement of windows used by gdb.
4424 This arrangement depends on the value of option `gdb-many-windows'."
4425 (interactive)
4426 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4427 (delete-other-windows)
4428 (if gdb-many-windows
4429 (gdb-setup-windows)
4430 (when (or gud-last-last-frame gdb-show-main)
4431 (let ((win (split-window)))
4432 (set-window-buffer
4433 win
4434 (if gud-last-last-frame
4435 (gud-find-file (car gud-last-last-frame))
4436 (gud-find-file gdb-main-file)))
4437 (setq gdb-source-window win)))))
4438
4439 ;; Called from `gud-sentinel' in gud.el:
4440 (defun gdb-reset ()
4441 "Exit a debugging session cleanly.
4442 Kills the gdb buffers, and resets variables and the source buffers."
4443 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4444 ;; process. This pty must be deleted explicitly.
4445 (let ((pty (get-process "gdb-inferior")))
4446 (if pty (delete-process pty)))
4447 ;; Find gdb-mi buffers and kill them.
4448 (dolist (buffer (buffer-list))
4449 (unless (eq buffer gud-comint-buffer)
4450 (with-current-buffer buffer
4451 (if (eq gud-minor-mode 'gdbmi)
4452 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4453 (kill-buffer nil)
4454 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4455 (setq gud-minor-mode nil)
4456 (kill-local-variable 'tool-bar-map)
4457 (kill-local-variable 'gdb-define-alist))))))
4458 (setq gdb-disassembly-position nil)
4459 (setq overlay-arrow-variable-list
4460 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4461 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4462 (setq gdb-stack-position nil)
4463 (setq overlay-arrow-variable-list
4464 (delq 'gdb-stack-position overlay-arrow-variable-list))
4465 (setq gdb-thread-position nil)
4466 (setq overlay-arrow-variable-list
4467 (delq 'gdb-thread-position overlay-arrow-variable-list))
4468 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4469 (setq gud-running nil)
4470 (setq gdb-active-process nil)
4471 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4472
4473 (defun gdb-get-source-file ()
4474 "Find the source file where the program starts and display it with related
4475 buffers, if required."
4476 (goto-char (point-min))
4477 (if (re-search-forward gdb-source-file-regexp nil t)
4478 (setq gdb-main-file (match-string 1)))
4479 (if gdb-many-windows
4480 (gdb-setup-windows)
4481 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4482 (and gdb-show-main
4483 gdb-main-file
4484 (display-buffer (gud-find-file gdb-main-file))))
4485 (gdb-force-mode-line-update
4486 (propertize "ready" 'face font-lock-variable-name-face)))
4487
4488 ;;from put-image
4489 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4490 "Put string PUTSTRING in front of POS in the current buffer.
4491 PUTSTRING is displayed by putting an overlay into the current buffer with a
4492 `before-string' string that has a `display' property whose value is
4493 PUTSTRING."
4494 (let ((string (make-string 1 ?x))
4495 (buffer (current-buffer)))
4496 (setq putstring (copy-sequence putstring))
4497 (let ((overlay (make-overlay pos pos buffer))
4498 (prop (or dprop
4499 (list (list 'margin 'left-margin) putstring))))
4500 (put-text-property 0 1 'display prop string)
4501 (if sprops
4502 (add-text-properties 0 1 sprops string))
4503 (overlay-put overlay 'put-break t)
4504 (overlay-put overlay 'before-string string))))
4505
4506 ;;from remove-images
4507 (defun gdb-remove-strings (start end &optional buffer)
4508 "Remove strings between START and END in BUFFER.
4509 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4510 BUFFER nil or omitted means use the current buffer."
4511 (unless buffer
4512 (setq buffer (current-buffer)))
4513 (dolist (overlay (overlays-in start end))
4514 (when (overlay-get overlay 'put-break)
4515 (delete-overlay overlay))))
4516
4517 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4518 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4519 (start (- (car posns) 1))
4520 (end (+ (cdr posns) 1))
4521 (putstring (if enabled "B" "b"))
4522 (source-window (get-buffer-window (current-buffer) 0)))
4523 (add-text-properties
4524 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4525 putstring)
4526 (if enabled
4527 (add-text-properties
4528 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4529 (add-text-properties
4530 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4531 (gdb-remove-breakpoint-icons start end)
4532 (if (display-images-p)
4533 (if (>= (or left-fringe-width
4534 (if source-window (car (window-fringes source-window)))
4535 gdb-buffer-fringe-width) 8)
4536 (gdb-put-string
4537 nil (1+ start)
4538 `(left-fringe breakpoint
4539 ,(if enabled
4540 'breakpoint-enabled
4541 'breakpoint-disabled))
4542 'gdb-bptno bptno
4543 'gdb-enabled enabled)
4544 (when (< left-margin-width 2)
4545 (save-current-buffer
4546 (setq left-margin-width 2)
4547 (if source-window
4548 (set-window-margins
4549 source-window
4550 left-margin-width right-margin-width))))
4551 (put-image
4552 (if enabled
4553 (or breakpoint-enabled-icon
4554 (setq breakpoint-enabled-icon
4555 (find-image `((:type xpm :data
4556 ,breakpoint-xpm-data
4557 :ascent 100 :pointer hand)
4558 (:type pbm :data
4559 ,breakpoint-enabled-pbm-data
4560 :ascent 100 :pointer hand)))))
4561 (or breakpoint-disabled-icon
4562 (setq breakpoint-disabled-icon
4563 (find-image `((:type xpm :data
4564 ,breakpoint-xpm-data
4565 :conversion disabled
4566 :ascent 100 :pointer hand)
4567 (:type pbm :data
4568 ,breakpoint-disabled-pbm-data
4569 :ascent 100 :pointer hand))))))
4570 (+ start 1)
4571 putstring
4572 'left-margin))
4573 (when (< left-margin-width 2)
4574 (save-current-buffer
4575 (setq left-margin-width 2)
4576 (let ((window (get-buffer-window (current-buffer) 0)))
4577 (if window
4578 (set-window-margins
4579 window left-margin-width right-margin-width)))))
4580 (gdb-put-string
4581 (propertize putstring
4582 'face (if enabled
4583 'breakpoint-enabled 'breakpoint-disabled))
4584 (1+ start)))))
4585
4586 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4587 (gdb-remove-strings start end)
4588 (if (display-images-p)
4589 (remove-images start end))
4590 (when remove-margin
4591 (setq left-margin-width 0)
4592 (let ((window (get-buffer-window (current-buffer) 0)))
4593 (if window
4594 (set-window-margins
4595 window left-margin-width right-margin-width)))))
4596
4597 \f
4598 ;;; Functions for inline completion.
4599
4600 (defvar gud-gdb-fetch-lines-in-progress)
4601 (defvar gud-gdb-fetch-lines-string)
4602 (defvar gud-gdb-fetch-lines-break)
4603 (defvar gud-gdb-fetched-lines)
4604
4605 (defun gud-gdbmi-completions (context command)
4606 "Completion table for GDB/MI commands.
4607 COMMAND is the prefix for which we seek completion.
4608 CONTEXT is the text before COMMAND on the line."
4609 (let ((gud-gdb-fetch-lines-in-progress t)
4610 (gud-gdb-fetch-lines-string nil)
4611 (gud-gdb-fetch-lines-break (length context))
4612 (gud-gdb-fetched-lines nil)
4613 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4614 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
4615 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4616 (gdb-input (concat "complete " context command)
4617 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4618 (while gud-gdb-fetch-lines-in-progress
4619 (accept-process-output (get-buffer-process gud-comint-buffer))))
4620 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4621
4622 (defun gud-gdbmi-fetch-lines-filter (string)
4623 "Custom filter function for `gud-gdbmi-completions'."
4624 (setq string (concat gud-gdb-fetch-lines-string
4625 (gud-gdbmi-marker-filter string)))
4626 (while (string-match "\n" string)
4627 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4628 gud-gdb-fetched-lines)
4629 (setq string (substring string (match-end 0))))
4630 "")
4631
4632 (provide 'gdb-mi)
4633
4634 ;;; gdb-mi.el ends here