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