Replace "Maintainer: FSF" with the emacs-devel mailing address
[bpt/emacs.git] / lisp / progmodes / gdb-mi.el
1 ;;; gdb-mi.el --- User Interface for running GDB -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2007-2014 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 (defun gdb-inferior-filter (proc string)
1633 (unless (string-equal string "")
1634 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))
1635 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1636 (comint-output-filter proc string)))
1637
1638 (defun gdb-io-interrupt ()
1639 "Interrupt the program being debugged."
1640 (interactive)
1641 (interrupt-process
1642 (get-buffer-process gud-comint-buffer) comint-ptyp))
1643
1644 (defun gdb-io-quit ()
1645 "Send quit signal to the program being debugged."
1646 (interactive)
1647 (quit-process
1648 (get-buffer-process gud-comint-buffer) comint-ptyp))
1649
1650 (defun gdb-io-stop ()
1651 "Stop the program being debugged."
1652 (interactive)
1653 (stop-process
1654 (get-buffer-process gud-comint-buffer) comint-ptyp))
1655
1656 (defun gdb-io-eof ()
1657 "Send end-of-file to the program being debugged."
1658 (interactive)
1659 (process-send-eof
1660 (get-buffer-process gud-comint-buffer)))
1661
1662 (defun gdb-clear-inferior-io ()
1663 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1664 (erase-buffer)))
1665 \f
1666
1667 (defconst breakpoint-xpm-data
1668 "/* XPM */
1669 static char *magick[] = {
1670 /* columns rows colors chars-per-pixel */
1671 \"10 10 2 1\",
1672 \" c red\",
1673 \"+ c None\",
1674 /* pixels */
1675 \"+++ +++\",
1676 \"++ ++\",
1677 \"+ +\",
1678 \" \",
1679 \" \",
1680 \" \",
1681 \" \",
1682 \"+ +\",
1683 \"++ ++\",
1684 \"+++ +++\",
1685 };"
1686 "XPM data used for breakpoint icon.")
1687
1688 (defconst breakpoint-enabled-pbm-data
1689 "P1
1690 10 10\",
1691 0 0 0 0 1 1 1 1 0 0 0 0
1692 0 0 0 1 1 1 1 1 1 0 0 0
1693 0 0 1 1 1 1 1 1 1 1 0 0
1694 0 1 1 1 1 1 1 1 1 1 1 0
1695 0 1 1 1 1 1 1 1 1 1 1 0
1696 0 1 1 1 1 1 1 1 1 1 1 0
1697 0 1 1 1 1 1 1 1 1 1 1 0
1698 0 0 1 1 1 1 1 1 1 1 0 0
1699 0 0 0 1 1 1 1 1 1 0 0 0
1700 0 0 0 0 1 1 1 1 0 0 0 0"
1701 "PBM data used for enabled breakpoint icon.")
1702
1703 (defconst breakpoint-disabled-pbm-data
1704 "P1
1705 10 10\",
1706 0 0 1 0 1 0 1 0 0 0
1707 0 1 0 1 0 1 0 1 0 0
1708 1 0 1 0 1 0 1 0 1 0
1709 0 1 0 1 0 1 0 1 0 1
1710 1 0 1 0 1 0 1 0 1 0
1711 0 1 0 1 0 1 0 1 0 1
1712 1 0 1 0 1 0 1 0 1 0
1713 0 1 0 1 0 1 0 1 0 1
1714 0 0 1 0 1 0 1 0 1 0
1715 0 0 0 1 0 1 0 1 0 0"
1716 "PBM data used for disabled breakpoint icon.")
1717
1718 (defvar breakpoint-enabled-icon nil
1719 "Icon for enabled breakpoint in display margin.")
1720
1721 (defvar breakpoint-disabled-icon nil
1722 "Icon for disabled breakpoint in display margin.")
1723
1724 (declare-function define-fringe-bitmap "fringe.c"
1725 (bitmap bits &optional height width align))
1726
1727 (and (display-images-p)
1728 ;; Bitmap for breakpoint in fringe
1729 (define-fringe-bitmap 'breakpoint
1730 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1731 ;; Bitmap for gud-overlay-arrow in fringe
1732 (define-fringe-bitmap 'hollow-right-triangle
1733 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1734
1735 (defface breakpoint-enabled
1736 '((t
1737 :foreground "red1"
1738 :weight bold))
1739 "Face for enabled breakpoint icon in fringe."
1740 :group 'gdb)
1741
1742 (defface breakpoint-disabled
1743 '((((class color) (min-colors 88)) :foreground "grey70")
1744 ;; Ensure that on low-color displays that we end up something visible.
1745 (((class color) (min-colors 8) (background light))
1746 :foreground "black")
1747 (((class color) (min-colors 8) (background dark))
1748 :foreground "white")
1749 (((type tty) (class mono))
1750 :inverse-video t)
1751 (t :background "gray"))
1752 "Face for disabled breakpoint icon in fringe."
1753 :group 'gdb)
1754
1755 \f
1756 (defvar gdb-control-commands-regexp
1757 (concat
1758 "^\\("
1759 "commands\\|if\\|while\\|define\\|document\\|python\\|"
1760 "while-stepping\\|stepping\\|ws\\|actions"
1761 "\\)\\([[:blank:]]+.*\\)?$")
1762 "Regexp matching GDB commands that enter a recursive reading loop.
1763 As long as GDB is in the recursive reading loop, it does not expect
1764 commands to be prefixed by \"-interpreter-exec console\".")
1765
1766 (defun gdb-strip-string-backslash (string)
1767 (replace-regexp-in-string "\\\\$" "" string))
1768
1769 (defun gdb-send (proc string)
1770 "A comint send filter for gdb."
1771 (with-current-buffer gud-comint-buffer
1772 (let ((inhibit-read-only t))
1773 (remove-text-properties (point-min) (point-max) '(face))))
1774 ;; mimic <RET> key to repeat previous command in GDB
1775 (if (not (string= "" string))
1776 (if gdb-continuation
1777 (setq gdb-last-command (concat gdb-continuation
1778 (gdb-strip-string-backslash string)
1779 " "))
1780 (setq gdb-last-command (gdb-strip-string-backslash string)))
1781 (if gdb-last-command (setq string gdb-last-command))
1782 (setq gdb-continuation nil))
1783 (if (and (not gdb-continuation) (or (string-match "^-" string)
1784 (> gdb-control-level 0)))
1785 ;; Either MI command or we are feeding GDB's recursive reading loop.
1786 (progn
1787 (setq gdb-first-done-or-error t)
1788 (process-send-string proc (concat string "\n"))
1789 (if (and (string-match "^end$" string)
1790 (> gdb-control-level 0))
1791 (setq gdb-control-level (1- gdb-control-level))))
1792 ;; CLI command
1793 (if (string-match "\\\\$" string)
1794 (setq gdb-continuation
1795 (concat gdb-continuation (gdb-strip-string-backslash
1796 string)
1797 " "))
1798 (setq gdb-first-done-or-error t)
1799 (let ((to-send (concat "-interpreter-exec console "
1800 (gdb-mi-quote (concat gdb-continuation string " "))
1801 "\n")))
1802 (if gdb-enable-debug
1803 (push (cons 'mi-send to-send) gdb-debug-log))
1804 (process-send-string proc to-send))
1805 (if (and (string-match "^end$" string)
1806 (> gdb-control-level 0))
1807 (setq gdb-control-level (1- gdb-control-level)))
1808 (setq gdb-continuation nil)))
1809 (if (string-match gdb-control-commands-regexp string)
1810 (setq gdb-control-level (1+ gdb-control-level))))
1811
1812 (defun gdb-mi-quote (string)
1813 "Return STRING quoted properly as an MI argument.
1814 The string is enclosed in double quotes.
1815 All embedded quotes, newlines, and backslashes are preceded with a backslash."
1816 (setq string (replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\&" string))
1817 (setq string (replace-regexp-in-string "\n" "\\n" string t t))
1818 (concat "\"" string "\""))
1819
1820 (defun gdb-input (command handler-function &optional trigger-name)
1821 "Send COMMAND to GDB via the MI interface.
1822 Run the function HANDLER-FUNCTION, with no arguments, once the command is
1823 complete. Do not send COMMAND to GDB if TRIGGER-NAME is non-nil and
1824 Emacs is still waiting for a reply from another command previously
1825 sent with the same TRIGGER-NAME."
1826 (when (or (not trigger-name)
1827 (not (gdb-pending-handler-p trigger-name)))
1828 (setq gdb-token-number (1+ gdb-token-number))
1829 (setq command (concat (number-to-string gdb-token-number) command))
1830
1831 (if gdb-enable-debug (push (list 'send-item command handler-function)
1832 gdb-debug-log))
1833
1834 (gdb-add-handler gdb-token-number handler-function trigger-name)
1835
1836 (if gdbmi-debug-mode (message "gdb-input: %s" command))
1837 (process-send-string (get-buffer-process gud-comint-buffer)
1838 (concat command "\n"))))
1839
1840 ;; NOFRAME is used for gud execution control commands
1841 (defun gdb-current-context-command (command)
1842 "Add --thread to gdb COMMAND when needed."
1843 (if (and gdb-thread-number
1844 gdb-supports-non-stop)
1845 (concat command " --thread " gdb-thread-number)
1846 command))
1847
1848 (defun gdb-current-context-buffer-name (name)
1849 "Add thread information and asterisks to string NAME.
1850
1851 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1852 (concat "*" name
1853 (if (local-variable-p 'gdb-thread-number)
1854 (format " (bound to thread %s)" gdb-thread-number)
1855 "")
1856 "*"))
1857
1858 (defun gdb-current-context-mode-name (mode)
1859 "Add thread information to MODE which is to be used as `mode-name'."
1860 (concat mode
1861 (if gdb-thread-number
1862 (format " [thread %s]" gdb-thread-number)
1863 "")))
1864 \f
1865
1866 (defcustom gud-gdb-command-name "gdb -i=mi"
1867 "Default command to execute an executable under the GDB debugger."
1868 :type 'string
1869 :group 'gdb)
1870
1871 (defun gdb-resync()
1872 (setq gud-running nil)
1873 (setq gdb-output-sink 'user)
1874 (gdb-remove-all-pending-triggers))
1875
1876 (defun gdb-update (&optional no-proc)
1877 "Update buffers showing status of debug session.
1878 If NO-PROC is non-nil, do not try to contact the GDB process."
1879 (when gdb-first-prompt
1880 (gdb-force-mode-line-update
1881 (propertize "initializing..." 'face font-lock-variable-name-face))
1882 (gdb-init-1)
1883 (setq gdb-first-prompt nil))
1884
1885 (unless no-proc
1886 (gdb-get-main-selected-frame))
1887
1888 ;; We may need to update gdb-threads-list so we can use
1889 (gdb-get-buffer-create 'gdb-threads-buffer)
1890 ;; gdb-break-list is maintained in breakpoints handler
1891 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1892
1893 (unless no-proc
1894 (gdb-emit-signal gdb-buf-publisher 'update))
1895
1896 (gdb-get-changed-registers)
1897 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1898 (dolist (var gdb-var-list)
1899 (setcar (nthcdr 5 var) nil))
1900 (gdb-var-update)))
1901
1902 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1903 ;; because we may need to update current gud-running value without
1904 ;; changing current thread (see gdb-running)
1905 (defun gdb-setq-thread-number (number)
1906 "Set `gdb-thread-number' to NUMBER.
1907 Only this function must be used to change `gdb-thread-number'
1908 value to NUMBER, because `gud-running' and `gdb-frame-number'
1909 need to be updated appropriately when current thread changes."
1910 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1911 (unless (string-equal number "0") (setq gdb-thread-number number))
1912 (setq gdb-frame-number "0")
1913 (gdb-update-gud-running))
1914
1915 (defun gdb-update-gud-running ()
1916 "Set `gud-running' according to the state of current thread.
1917
1918 `gdb-frame-number' is set to 0 if current thread is now stopped.
1919
1920 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1921 cannot be reliably used to determine whether or not execution
1922 control buttons should be shown in menu or toolbar. Use
1923 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1924 instead.
1925
1926 For all-stop mode, thread information is unavailable while target
1927 is running."
1928 (let ((old-value gud-running))
1929 (setq gud-running
1930 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1931 "running"))
1932 ;; Set frame number to "0" when _current_ threads stops.
1933 (when (and (gdb-current-buffer-thread)
1934 (not (eq gud-running old-value)))
1935 (setq gdb-frame-number "0"))))
1936
1937 (defun gdb-show-run-p ()
1938 "Return t if \"Run/continue\" should be shown on the toolbar."
1939 (or (not gdb-active-process)
1940 (and (or
1941 (not gdb-gud-control-all-threads)
1942 (not gdb-non-stop))
1943 (not gud-running))
1944 (and gdb-gud-control-all-threads
1945 (> gdb-stopped-threads-count 0))))
1946
1947 (defun gdb-show-stop-p ()
1948 "Return t if \"Stop\" should be shown on the toolbar."
1949 (or (and (or
1950 (not gdb-gud-control-all-threads)
1951 (not gdb-non-stop))
1952 gud-running)
1953 (and gdb-gud-control-all-threads
1954 (> gdb-running-threads-count 0))))
1955
1956 ;; GUD displays the selected GDB frame. This might might not be the current
1957 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1958 ;; visited breakpoint is, use that window.
1959 (defun gdb-display-source-buffer (buffer)
1960 (let* ((last-window (if gud-last-last-frame
1961 (get-buffer-window
1962 (gud-find-file (car gud-last-last-frame)))))
1963 (source-window (or last-window
1964 (if (and gdb-source-window
1965 (window-live-p gdb-source-window))
1966 gdb-source-window))))
1967 (when source-window
1968 (setq gdb-source-window source-window)
1969 (set-window-buffer source-window buffer))
1970 source-window))
1971
1972
1973 (defun gdbmi-start-with (str offset match)
1974 "Return non-nil if string STR starts with MATCH, else returns nil.
1975 OFFSET is the position in STR at which the comparison takes place."
1976 (let ((match-length (length match))
1977 (str-length (- (length str) offset)))
1978 (when (>= str-length match-length)
1979 (string-equal match (substring str offset (+ offset match-length))))))
1980
1981 (defun gdbmi-same-start (str offset match)
1982 "Return non-nil iff STR and MATCH are equal up to the end of either strings.
1983 OFFSET is the position in STR at which the comparison takes place."
1984 (let* ((str-length (- (length str) offset))
1985 (match-length (length match))
1986 (compare-length (min str-length match-length)))
1987 (when (> compare-length 0)
1988 (string-equal (substring str offset (+ offset compare-length))
1989 (substring match 0 compare-length)))))
1990
1991 (defun gdbmi-is-number (character)
1992 "Return non-nil iff CHARACTER is a numerical character between 0 and 9."
1993 (and (>= character ?0)
1994 (<= character ?9)))
1995
1996
1997 (defvar-local gdbmi-bnf-state 'gdbmi-bnf-output
1998 "Current GDB/MI output parser state.
1999 The parser is placed in a different state when an incomplete data steam is
2000 received from GDB.
2001 This variable will preserve the state required to resume the parsing
2002 when more data arrives.")
2003
2004 (defvar-local gdbmi-bnf-offset 0
2005 "Offset in `gud-marker-acc' at which the parser is reading.
2006 This offset is used to be able to parse the GDB/MI message
2007 in-place, without the need of copying the string in a temporary buffer
2008 or discarding parsed tokens by substringing the message.")
2009
2010 (defun gdbmi-bnf-init ()
2011 "Initialize the GDB/MI message parser."
2012 (setq gdbmi-bnf-state 'gdbmi-bnf-output)
2013 (setq gdbmi-bnf-offset 0)
2014 (setq gud-marker-acc ""))
2015
2016
2017 (defun gdbmi-bnf-output ()
2018 "Implementation of the following GDB/MI output grammar rule:
2019
2020 output ==>
2021 ( out-of-band-record )* [ result-record ] gdb-prompt"
2022
2023 (gdbmi-bnf-skip-unrecognized)
2024 (while (gdbmi-bnf-out-of-band-record))
2025 (gdbmi-bnf-result-record)
2026 (gdbmi-bnf-gdb-prompt))
2027
2028
2029 (defun gdbmi-bnf-skip-unrecognized ()
2030 "Skip characters until is encounters the beginning of a valid record.
2031 Used as a protection mechanism in case something goes wrong when parsing
2032 a GDB/MI reply message."
2033 (let ((acc-length (length gud-marker-acc))
2034 (prefix-offset gdbmi-bnf-offset)
2035 (prompt "(gdb) \n"))
2036
2037 (while (and (< prefix-offset acc-length)
2038 (gdbmi-is-number (aref gud-marker-acc prefix-offset)))
2039 (setq prefix-offset (1+ prefix-offset)))
2040
2041 (if (and (< prefix-offset acc-length)
2042 (not (memq (aref gud-marker-acc prefix-offset)
2043 '(?^ ?* ?+ ?= ?~ ?@ ?&)))
2044 (not (gdbmi-same-start gud-marker-acc gdbmi-bnf-offset prompt))
2045 (string-match "\\([^^*+=~@&]+\\)" gud-marker-acc
2046 gdbmi-bnf-offset))
2047 (let ((unrecognized-str (match-string 0 gud-marker-acc)))
2048 (setq gdbmi-bnf-offset (match-end 0))
2049 (if gdbmi-debug-mode
2050 (message "gdbmi-bnf-skip-unrecognized: %s" unrecognized-str))
2051 (gdb-shell unrecognized-str)
2052 t))))
2053
2054
2055 (defun gdbmi-bnf-gdb-prompt ()
2056 "Implementation of the following GDB/MI output grammar rule:
2057 gdb-prompt ==>
2058 '(gdb)' nl
2059
2060 nl ==>
2061 CR | CR-LF"
2062
2063 (let ((prompt "(gdb) \n"))
2064 (when (gdbmi-start-with gud-marker-acc gdbmi-bnf-offset prompt)
2065 (if gdbmi-debug-mode (message "gdbmi-bnf-gdb-prompt: %s" prompt))
2066 (gdb-gdb prompt)
2067 (setq gdbmi-bnf-offset (+ gdbmi-bnf-offset (length prompt)))
2068
2069 ;; Returns non-nil to tell gud-gdbmi-marker-filter we've reached
2070 ;; the end of a GDB reply message.
2071 t)))
2072
2073
2074 (defun gdbmi-bnf-result-record ()
2075 "Implementation of the following GDB/MI output grammar rule:
2076
2077 result-record ==>
2078 [ token ] '^' result-class ( ',' result )* nl
2079
2080 token ==>
2081 any sequence of digits."
2082
2083 (gdbmi-bnf-result-and-async-record-impl))
2084
2085
2086 (defun gdbmi-bnf-out-of-band-record ()
2087 "Implementation of the following GDB/MI output grammar rule:
2088
2089 out-of-band-record ==>
2090 async-record | stream-record"
2091
2092 (or (gdbmi-bnf-async-record)
2093 (gdbmi-bnf-stream-record)))
2094
2095
2096 (defun gdbmi-bnf-async-record ()
2097 "Implementation of the following GDB/MI output grammar rules:
2098
2099 async-record ==>
2100 exec-async-output | status-async-output | notify-async-output
2101
2102 exec-async-output ==>
2103 [ token ] '*' async-output
2104
2105 status-async-output ==>
2106 [ token ] '+' async-output
2107
2108 notify-async-output ==>
2109 [ token ] '=' async-output
2110
2111 async-output ==>
2112 async-class ( ',' result )* nl"
2113
2114 (gdbmi-bnf-result-and-async-record-impl))
2115
2116
2117 (defun gdbmi-bnf-stream-record ()
2118 "Implement the following GDB/MI output grammar rule:
2119 stream-record ==>
2120 console-stream-output | target-stream-output | log-stream-output
2121
2122 console-stream-output ==>
2123 '~' c-string
2124
2125 target-stream-output ==>
2126 '@' c-string
2127
2128 log-stream-output ==>
2129 '&' c-string"
2130 (when (< gdbmi-bnf-offset (length gud-marker-acc))
2131 (if (and (member (aref gud-marker-acc gdbmi-bnf-offset) '(?~ ?@ ?&))
2132 (string-match (concat "\\([~@&]\\)\\(" gdb--string-regexp "\\)\n")
2133 gud-marker-acc
2134 gdbmi-bnf-offset))
2135 (let ((prefix (match-string 1 gud-marker-acc))
2136 (c-string (match-string 2 gud-marker-acc)))
2137
2138 (setq gdbmi-bnf-offset (match-end 0))
2139 (if gdbmi-debug-mode (message "gdbmi-bnf-stream-record: %s"
2140 (match-string 0 gud-marker-acc)))
2141
2142 (cond ((string-equal prefix "~")
2143 (gdbmi-bnf-console-stream-output c-string))
2144 ((string-equal prefix "@")
2145 (gdbmi-bnf-target-stream-output c-string))
2146 ((string-equal prefix "&")
2147 (gdbmi-bnf-log-stream-output c-string)))
2148 t))))
2149
2150 (defun gdbmi-bnf-console-stream-output (c-string)
2151 "Handler for the console-stream-output GDB/MI output grammar rule."
2152 (gdb-console c-string))
2153
2154 (defun gdbmi-bnf-target-stream-output (_c-string)
2155 "Handler for the target-stream-output GDB/MI output grammar rule."
2156 ;; Not currently used.
2157 )
2158
2159 (defun gdbmi-bnf-log-stream-output (c-string)
2160 "Handler for the log-stream-output GDB/MI output grammar rule."
2161 ;; Suppress "No registers." GDB 6.8 and earlier
2162 ;; duplicates MI error message on internal stream.
2163 ;; Don't print to GUD buffer.
2164 (if (not (string-equal (read c-string) "No registers.\n"))
2165 (gdb-internals c-string)))
2166
2167
2168 (defconst gdbmi-bnf-result-state-configs
2169 '(("^" . (("done" . (gdb-done . progressive))
2170 ("error" . (gdb-error . progressive))
2171 ("running" . (gdb-starting . atomic))))
2172 ("*" . (("stopped" . (gdb-stopped . atomic))
2173 ("running" . (gdb-running . atomic))))
2174 ("+" . ())
2175 ("=" . (("thread-created" . (gdb-thread-created . atomic))
2176 ("thread-selected" . (gdb-thread-selected . atomic))
2177 ("thread-existed" . (gdb-ignored-notification . atomic))
2178 ('default . (gdb-ignored-notification . atomic)))))
2179 "Alist of alists, mapping the type and class of message to a handler function.
2180 Handler functions are all flagged as either `progressive' or `atomic'.
2181 `progressive' handlers are capable of parsing incomplete messages.
2182 They can be called several time with new data chunk as they arrive from GDB.
2183 `progressive' handlers must have an extra argument that is set to a non-nil
2184 value when the message is complete.
2185
2186 Implement the following GDB/MI output grammar rule:
2187 result-class ==>
2188 'done' | 'running' | 'connected' | 'error' | 'exit'
2189
2190 async-class ==>
2191 'stopped' | others (where others will be added depending on the needs
2192 --this is still in development).")
2193
2194 (defun gdbmi-bnf-result-and-async-record-impl ()
2195 "Common implementation of the result-record and async-record rule.
2196 Both rules share the same syntax. Those records may be very large in size.
2197 For that reason, the \"result\" part of the record is parsed by
2198 `gdbmi-bnf-incomplete-record-result', which will keep
2199 receiving characters as they arrive from GDB until the record is complete."
2200 (let ((acc-length (length gud-marker-acc))
2201 (prefix-offset gdbmi-bnf-offset))
2202
2203 (while (and (< prefix-offset acc-length)
2204 (gdbmi-is-number (aref gud-marker-acc prefix-offset)))
2205 (setq prefix-offset (1+ prefix-offset)))
2206
2207 (if (and (< prefix-offset acc-length)
2208 (member (aref gud-marker-acc prefix-offset) '(?* ?+ ?= ?^))
2209 (string-match "\\([0-9]*\\)\\([*+=^]\\)\\(.+?\\)\\([,\n]\\)"
2210 gud-marker-acc gdbmi-bnf-offset))
2211
2212 (let ((token (match-string 1 gud-marker-acc))
2213 (prefix (match-string 2 gud-marker-acc))
2214 (class (match-string 3 gud-marker-acc))
2215 (complete (string-equal (match-string 4 gud-marker-acc) "\n"))
2216 class-alist
2217 class-command)
2218
2219 (setq gdbmi-bnf-offset (match-end 0))
2220 (if gdbmi-debug-mode (message "gdbmi-bnf-result-record: %s"
2221 (match-string 0 gud-marker-acc)))
2222
2223 (setq class-alist
2224 (cdr (assoc prefix gdbmi-bnf-result-state-configs)))
2225 (setq class-command (cdr (assoc class class-alist)))
2226 (if (null class-command)
2227 (setq class-command (cdr (assoc 'default class-alist))))
2228
2229 (if complete
2230 (if class-command
2231 (if (equal (cdr class-command) 'progressive)
2232 (funcall (car class-command) token "" complete)
2233 (funcall (car class-command) token "")))
2234 (setq gdbmi-bnf-state
2235 (lambda ()
2236 (gdbmi-bnf-incomplete-record-result token class-command)))
2237 (funcall gdbmi-bnf-state))
2238 t))))
2239
2240 (defun gdbmi-bnf-incomplete-record-result (token class-command)
2241 "State of the parser used to progressively parse a result-record or async-record
2242 rule from an incomplete data stream. The parser will stay in this state until
2243 the end of the current result or async record is reached."
2244 (when (< gdbmi-bnf-offset (length gud-marker-acc))
2245 ;; Search the data stream for the end of the current record:
2246 (let* ((newline-pos (string-match "\n" gud-marker-acc gdbmi-bnf-offset))
2247 (is-progressive (equal (cdr class-command) 'progressive))
2248 (is-complete (not (null newline-pos)))
2249 result-str)
2250
2251 (when gdbmi-debug-mode
2252 (message "gdbmi-bnf-incomplete-record-result: %s"
2253 (substring gud-marker-acc gdbmi-bnf-offset newline-pos)))
2254
2255 ;; Update the gdbmi-bnf-offset only if the current chunk of data can
2256 ;; be processed by the class-command handler:
2257 (when (or is-complete is-progressive)
2258 (setq result-str
2259 (substring gud-marker-acc gdbmi-bnf-offset newline-pos))
2260
2261 ;; Move gdbmi-bnf-offset past the end of the chunk.
2262 (setq gdbmi-bnf-offset (+ gdbmi-bnf-offset (length result-str)))
2263 (when newline-pos
2264 (setq gdbmi-bnf-offset (1+ gdbmi-bnf-offset))))
2265
2266 ;; Update the parsing state before invoking the handler in class-command
2267 ;; to make sure it's not left in an invalid state if the handler was
2268 ;; to generate an error.
2269 (if is-complete
2270 (setq gdbmi-bnf-state 'gdbmi-bnf-output))
2271
2272 (if class-command
2273 (if is-progressive
2274 (funcall (car class-command) token result-str is-complete)
2275 (if is-complete
2276 (funcall (car class-command) token result-str))))
2277
2278 (unless is-complete
2279 ;; Incomplete gdb response: abort parsing until we receive more data.
2280 (if gdbmi-debug-mode (message "gdbmi-bnf-incomplete-record-result, aborting: incomplete stream"))
2281 (throw 'gdbmi-incomplete-stream nil))
2282
2283 is-complete)))
2284
2285
2286 ; The following grammar rules are not yet implemented by this GDBMI-BNF parser.
2287 ; The handling of those rules is currently done by the handlers registered
2288 ; in gdbmi-bnf-result-state-configs
2289 ;
2290 ; result ==>
2291 ; variable "=" value
2292 ;
2293 ; variable ==>
2294 ; string
2295 ;
2296 ; value ==>
2297 ; const | tuple | list
2298 ;
2299 ; const ==>
2300 ; c-string
2301 ;
2302 ; tuple ==>
2303 ; "{}" | "{" result ( "," result )* "}"
2304 ;
2305 ; list ==>
2306 ; "[]" | "[" value ( "," value )* "]" | "[" result ( "," result )* "]"
2307
2308
2309 (defun gud-gdbmi-marker-filter (string)
2310 "Filter GDB/MI output."
2311
2312 ;; Record transactions if logging is enabled.
2313 (when gdb-enable-debug
2314 (push (cons 'recv string) gdb-debug-log)
2315 (if (and gdb-debug-log-max
2316 (> (length gdb-debug-log) gdb-debug-log-max))
2317 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
2318
2319 ;; Recall the left over gud-marker-acc from last time.
2320 (setq gud-marker-acc (concat gud-marker-acc string))
2321
2322 ;; Start accumulating output for the GUD buffer.
2323 (setq gdb-filter-output "")
2324
2325 (let ((acc-length (length gud-marker-acc)))
2326 (catch 'gdbmi-incomplete-stream
2327 (while (and (< gdbmi-bnf-offset acc-length)
2328 (funcall gdbmi-bnf-state)))))
2329
2330 (when (/= gdbmi-bnf-offset 0)
2331 (setq gud-marker-acc (substring gud-marker-acc gdbmi-bnf-offset))
2332 (setq gdbmi-bnf-offset 0))
2333
2334 (when (and gdbmi-debug-mode (> (length gud-marker-acc) 0))
2335 (message "gud-gdbmi-marker-filter, unparsed string: %s" gud-marker-acc))
2336
2337 gdb-filter-output)
2338
2339 (defun gdb-gdb (_output-field))
2340
2341 (defun gdb-shell (output-field)
2342 (setq gdb-filter-output
2343 (concat output-field gdb-filter-output)))
2344
2345 (defun gdb-ignored-notification (_token _output-field))
2346
2347 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
2348 (defun gdb-thread-created (_token _output-field))
2349 (defun gdb-thread-exited (_token output-field)
2350 "Handle =thread-exited async record.
2351 Unset `gdb-thread-number' if current thread exited and update threads list."
2352 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
2353 (if (string= gdb-thread-number thread-id)
2354 (gdb-setq-thread-number nil))
2355 ;; When we continue current thread and it quickly exits,
2356 ;; the pending triggers in gdb-handler-list left after gdb-running
2357 ;; disallow us to properly call -thread-info without --thread option.
2358 ;; Thus we need to use gdb-wait-for-pending.
2359 (gdb-wait-for-pending
2360 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
2361
2362 (defun gdb-thread-selected (_token output-field)
2363 "Handler for =thread-selected MI output record.
2364
2365 Sets `gdb-thread-number' to new id."
2366 (let* ((result (gdb-json-string output-field))
2367 (thread-id (bindat-get-field result 'id)))
2368 (gdb-setq-thread-number thread-id)
2369 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
2370 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
2371 ;; as usually. Things happen to fast and second call (from
2372 ;; gdb-thread-selected handler) gets cut off by our beloved
2373 ;; pending triggers.
2374 ;; Solution is `gdb-wait-for-pending' macro: it guarantees that its
2375 ;; body will get executed when `gdb-handler-list' if free of
2376 ;; pending triggers.
2377 (gdb-wait-for-pending
2378 (gdb-update))))
2379
2380 (defun gdb-running (_token output-field)
2381 (let* ((thread-id
2382 (bindat-get-field (gdb-json-string output-field) 'thread-id)))
2383 ;; We reset gdb-frame-number to nil if current thread has gone
2384 ;; running. This can't be done in gdb-thread-list-handler-custom
2385 ;; because we need correct gdb-frame-number by the time
2386 ;; -thread-info command is sent.
2387 (when (or (string-equal thread-id "all")
2388 (string-equal thread-id gdb-thread-number))
2389 (setq gdb-frame-number nil)))
2390 (setq gdb-inferior-status "running")
2391 (gdb-force-mode-line-update
2392 (propertize gdb-inferior-status 'face font-lock-type-face))
2393 (when (not gdb-non-stop)
2394 (setq gud-running t))
2395 (setq gdb-active-process t))
2396
2397 (defun gdb-starting (_output-field _result)
2398 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
2399 (setq gdb-inferior-status "running")
2400 (gdb-force-mode-line-update
2401 (propertize gdb-inferior-status 'face font-lock-type-face))
2402 (setq gdb-active-process t)
2403 (setq gud-running t))
2404
2405 ;; -break-insert -t didn't give a reason before gdb 6.9
2406
2407 (defun gdb-stopped (_token output-field)
2408 "Given the contents of *stopped MI async record, select new
2409 current thread and update GDB buffers."
2410 ;; Reason is available with target-async only
2411 (let* ((result (gdb-json-string output-field))
2412 (reason (bindat-get-field result 'reason))
2413 (thread-id (bindat-get-field result 'thread-id)))
2414
2415 ;; -data-list-register-names needs to be issued for any stopped
2416 ;; thread
2417 (when (not gdb-register-names)
2418 (gdb-input (concat "-data-list-register-names"
2419 (if gdb-supports-non-stop
2420 (concat " --thread " thread-id)))
2421 'gdb-register-names-handler))
2422
2423 ;; Don't set gud-last-frame here as it's currently done in
2424 ;; gdb-frame-handler because synchronous GDB doesn't give these fields
2425 ;; with CLI.
2426 ;;(when file
2427 ;; (setq
2428 ;; ;; Extract the frame position from the marker.
2429 ;; gud-last-frame (cons file
2430 ;; (string-to-number
2431 ;; (match-string 6 gud-marker-acc)))))
2432
2433 (setq gdb-inferior-status (or reason "unknown"))
2434 (gdb-force-mode-line-update
2435 (propertize gdb-inferior-status 'face font-lock-warning-face))
2436 (if (string-equal reason "exited-normally")
2437 (setq gdb-active-process nil))
2438
2439 ;; Select new current thread.
2440
2441 ;; Don't switch if we have no reasons selected
2442 (when gdb-switch-reasons
2443 ;; Switch from another stopped thread only if we have
2444 ;; gdb-switch-when-another-stopped:
2445 (when (or gdb-switch-when-another-stopped
2446 (not (string= "stopped"
2447 (bindat-get-field (gdb-current-buffer-thread) 'state))))
2448 ;; Switch if current reason has been selected or we have no
2449 ;; reasons
2450 (if (or (eq gdb-switch-reasons t)
2451 (member reason gdb-switch-reasons))
2452 (when (not (string-equal gdb-thread-number thread-id))
2453 (message "Switched to thread %s" thread-id)
2454 (gdb-setq-thread-number thread-id))
2455 (message "Thread %s stopped" thread-id))))
2456
2457 ;; Print "(gdb)" to GUD console
2458 (when gdb-first-done-or-error
2459 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2460
2461 ;; In non-stop, we update information as soon as another thread gets
2462 ;; stopped
2463 (when (or gdb-first-done-or-error
2464 gdb-non-stop)
2465 ;; In all-stop this updates gud-running properly as well.
2466 (gdb-update)
2467 (setq gdb-first-done-or-error nil))
2468 (run-hook-with-args 'gdb-stopped-functions result)))
2469
2470 ;; Remove the trimmings from log stream containing debugging messages
2471 ;; being produced by GDB's internals, use warning face and send to GUD
2472 ;; buffer.
2473 (defun gdb-internals (output-field)
2474 (setq gdb-filter-output
2475 (gdb-concat-output
2476 gdb-filter-output
2477 (if (string= output-field "\"\\n\"")
2478 ""
2479 (let ((error-message
2480 (read output-field)))
2481 (put-text-property
2482 0 (length error-message)
2483 'face font-lock-warning-face
2484 error-message)
2485 error-message)))))
2486
2487 ;; Remove the trimmings from the console stream and send to GUD buffer
2488 ;; (frontend MI commands should not print to this stream)
2489 (defun gdb-console (output-field)
2490 (setq gdb-filter-output
2491 (gdb-concat-output gdb-filter-output (read output-field))))
2492
2493 (defun gdb-done (token-number output-field is-complete)
2494 (gdb-done-or-error token-number 'done output-field is-complete))
2495
2496 (defun gdb-error (token-number output-field is-complete)
2497 (gdb-done-or-error token-number 'error output-field is-complete))
2498
2499 (defun gdb-done-or-error (token-number type output-field is-complete)
2500 (if (string-equal token-number "")
2501 ;; Output from command entered by user
2502 (progn
2503 (setq gdb-output-sink 'user)
2504 (setq token-number nil)
2505 ;; MI error - send to minibuffer
2506 (when (eq type 'error)
2507 ;; Skip "msg=" from `output-field'
2508 (message "%s" (read (substring output-field 4)))
2509 ;; Don't send to the console twice. (If it is a console error
2510 ;; it is also in the console stream.)
2511 (setq output-field nil)))
2512 ;; Output from command from frontend.
2513 (setq gdb-output-sink 'emacs))
2514
2515 ;; The process may already be dead (e.g. C-d at the gdb prompt).
2516 (let* ((proc (get-buffer-process gud-comint-buffer))
2517 (no-proc (or (null proc)
2518 (memq (process-status proc) '(exit signal)))))
2519
2520 (when (and is-complete gdb-first-done-or-error)
2521 (unless (or token-number gud-running no-proc)
2522 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2523 (gdb-update no-proc)
2524 (setq gdb-first-done-or-error nil))
2525
2526 (setq gdb-filter-output
2527 (gdb-concat-output gdb-filter-output output-field))
2528
2529 ;; We are done concatenating to the output sink. Restore it to user sink:
2530 (setq gdb-output-sink 'user)
2531
2532 (when (and token-number is-complete)
2533 (with-current-buffer
2534 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2535 (gdb-handle-reply (string-to-number token-number))))
2536
2537 (when is-complete
2538 (gdb-clear-partial-output))))
2539
2540 (defun gdb-concat-output (so-far new)
2541 (cond
2542 ((eq gdb-output-sink 'user) (concat so-far new))
2543 ((eq gdb-output-sink 'emacs)
2544 (gdb-append-to-partial-output new)
2545 so-far)))
2546
2547 (defun gdb-append-to-partial-output (string)
2548 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2549 (goto-char (point-max))
2550 (insert string)))
2551
2552 (defun gdb-clear-partial-output ()
2553 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2554 (erase-buffer)))
2555
2556 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2557 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2558
2559 Field names are wrapped in double quotes and equal signs are
2560 replaced with semicolons.
2561
2562 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2563 partial output. This is used to get rid of useless keys in lists
2564 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2565 -break-info are examples of MI commands which issue such
2566 responses.
2567
2568 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2569 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2570 -break-info output when it contains breakpoint script field
2571 incompatible with GDB/MI output syntax."
2572 (save-excursion
2573 (goto-char (point-min))
2574 (when fix-key
2575 (save-excursion
2576 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2577 (replace-match "" nil nil nil 1))))
2578 (when fix-list
2579 (save-excursion
2580 ;; Find positions of braces which enclose broken list
2581 (while (re-search-forward (concat fix-list "={\"") nil t)
2582 (let ((p1 (goto-char (- (point) 2)))
2583 (p2 (progn (forward-sexp)
2584 (1- (point)))))
2585 ;; Replace braces with brackets
2586 (save-excursion
2587 (goto-char p1)
2588 (delete-char 1)
2589 (insert "[")
2590 (goto-char p2)
2591 (delete-char 1)
2592 (insert "]"))))))
2593 (goto-char (point-min))
2594 (insert "{")
2595 (let ((re (concat "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|"
2596 gdb--string-regexp "\\)")))
2597 (while (re-search-forward re nil t)
2598 (replace-match "\"\\1\":\\2" nil nil)))
2599 (goto-char (point-max))
2600 (insert "}")))
2601
2602 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2603 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2604
2605 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2606 (gdb-jsonify-buffer fix-key fix-list)
2607 (save-excursion
2608 (goto-char (point-min))
2609 (let ((json-array-type 'list))
2610 (json-read))))
2611
2612 (defun gdb-json-string (string &optional fix-key fix-list)
2613 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2614
2615 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2616 (with-temp-buffer
2617 (insert string)
2618 (gdb-json-read-buffer fix-key fix-list)))
2619
2620 (defun gdb-json-partial-output (&optional fix-key fix-list)
2621 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2622
2623 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2624 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2625 (gdb-json-read-buffer fix-key fix-list)))
2626
2627 (defun gdb-line-posns (line)
2628 "Return a pair of LINE beginning and end positions."
2629 (let ((offset (1+ (- line (line-number-at-pos)))))
2630 (cons
2631 (line-beginning-position offset)
2632 (line-end-position offset))))
2633
2634 (defmacro gdb-mark-line (line variable)
2635 "Set VARIABLE marker to point at beginning of LINE.
2636
2637 If current window has no fringes, inverse colors on LINE.
2638
2639 Return position where LINE begins."
2640 `(save-excursion
2641 (let* ((posns (gdb-line-posns ,line))
2642 (start-posn (car posns))
2643 (end-posn (cdr posns)))
2644 (set-marker ,variable (copy-marker start-posn))
2645 (when (not (> (car (window-fringes)) 0))
2646 (put-text-property start-posn end-posn
2647 'font-lock-face '(:inverse-video t)))
2648 start-posn)))
2649
2650 (defun gdb-pad-string (string padding)
2651 (format (concat "%" (number-to-string padding) "s") string))
2652
2653 ;; gdb-table struct is a way to programmatically construct simple
2654 ;; tables. It help to reliably align columns of data in GDB buffers
2655 ;; and provides
2656 (cl-defstruct gdb-table
2657 (column-sizes nil)
2658 (rows nil)
2659 (row-properties nil)
2660 (right-align nil))
2661
2662 (defun gdb-table-add-row (table row &optional properties)
2663 "Add ROW of string to TABLE and recalculate column sizes.
2664
2665 When non-nil, PROPERTIES will be added to the whole row when
2666 calling `gdb-table-string'."
2667 (let ((rows (gdb-table-rows table))
2668 (row-properties (gdb-table-row-properties table))
2669 (column-sizes (gdb-table-column-sizes table))
2670 (right-align (gdb-table-right-align table)))
2671 (when (not column-sizes)
2672 (setf (gdb-table-column-sizes table)
2673 (make-list (length row) 0)))
2674 (setf (gdb-table-rows table)
2675 (append rows (list row)))
2676 (setf (gdb-table-row-properties table)
2677 (append row-properties (list properties)))
2678 (setf (gdb-table-column-sizes table)
2679 (cl-mapcar (lambda (x s)
2680 (let ((new-x
2681 (max (abs x) (string-width (or s "")))))
2682 (if right-align new-x (- new-x))))
2683 (gdb-table-column-sizes table)
2684 row))
2685 ;; Avoid trailing whitespace at eol
2686 (if (not (gdb-table-right-align table))
2687 (setcar (last (gdb-table-column-sizes table)) 0))))
2688
2689 (defun gdb-table-string (table &optional sep)
2690 "Return TABLE as a string with columns separated with SEP."
2691 (let ((column-sizes (gdb-table-column-sizes table)))
2692 (mapconcat
2693 'identity
2694 (cl-mapcar
2695 (lambda (row properties)
2696 (apply 'propertize
2697 (mapconcat 'identity
2698 (cl-mapcar (lambda (s x) (gdb-pad-string s x))
2699 row column-sizes)
2700 sep)
2701 properties))
2702 (gdb-table-rows table)
2703 (gdb-table-row-properties table))
2704 "\n")))
2705
2706 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2707 (defun gdb-get-many-fields (struct &rest fields)
2708 "Return a list of FIELDS values from STRUCT."
2709 (let ((values))
2710 (dolist (field fields)
2711 (push (bindat-get-field struct field) values))
2712 (nreverse values)))
2713
2714 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2715 handler-name
2716 &optional signal-list)
2717 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2718 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2719 buffer with `gdb-bind-function-to-buffer'.
2720
2721 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2722 defined trigger is called with an argument from SIGNAL-LIST. It's
2723 not recommended to define triggers with empty SIGNAL-LIST.
2724 Normally triggers should respond at least to 'update signal.
2725
2726 Normally the trigger defined by this command must be called from
2727 the buffer where HANDLER-NAME must work. This should be done so
2728 that buffer-local thread number may be used in GDB-COMMAND (by
2729 calling `gdb-current-context-command').
2730 `gdb-bind-function-to-buffer' is used to achieve this, see
2731 `gdb-get-buffer-create'.
2732
2733 Triggers defined by this command are meant to be used as a
2734 trigger argument when describing buffer types with
2735 `gdb-set-buffer-rules'."
2736 `(defun ,trigger-name (&optional signal)
2737 (when
2738 (or (not ,signal-list)
2739 (memq signal ,signal-list))
2740 (gdb-input ,gdb-command
2741 (gdb-bind-function-to-buffer ',handler-name (current-buffer))
2742 (cons (current-buffer) ',trigger-name)))))
2743
2744 ;; Used by disassembly buffer only, the rest use
2745 ;; def-gdb-trigger-and-handler
2746 (defmacro def-gdb-auto-update-handler (handler-name custom-defun
2747 &optional nopreserve)
2748 "Define a handler HANDLER-NAME calling CUSTOM-DEFUN.
2749
2750 Handlers are normally called from the buffers they put output in.
2751
2752 Erase current buffer and evaluate CUSTOM-DEFUN.
2753 Then call `gdb-update-buffer-name'.
2754
2755 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2756 `(defun ,handler-name ()
2757 (let* ((inhibit-read-only t)
2758 ,@(unless nopreserve
2759 '((window (get-buffer-window (current-buffer) 0))
2760 (start (window-start window))
2761 (p (window-point window)))))
2762 (erase-buffer)
2763 (,custom-defun)
2764 (gdb-update-buffer-name)
2765 ,@(when (not nopreserve)
2766 '((set-window-start window start)
2767 (set-window-point window p))))))
2768
2769 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2770 handler-name custom-defun
2771 &optional signal-list)
2772 "Define trigger and handler.
2773
2774 TRIGGER-NAME trigger is defined to send GDB-COMMAND.
2775 See `def-gdb-auto-update-trigger'.
2776
2777 HANDLER-NAME handler uses customization of CUSTOM-DEFUN.
2778 See `def-gdb-auto-update-handler'."
2779 `(progn
2780 (def-gdb-auto-update-trigger ,trigger-name
2781 ,gdb-command
2782 ,handler-name ,signal-list)
2783 (def-gdb-auto-update-handler ,handler-name
2784 ,custom-defun)))
2785
2786 \f
2787
2788 ;; Breakpoint buffer : This displays the output of `-break-list'.
2789 (def-gdb-trigger-and-handler
2790 gdb-invalidate-breakpoints "-break-list"
2791 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2792 '(start update))
2793
2794 (gdb-set-buffer-rules
2795 'gdb-breakpoints-buffer
2796 'gdb-breakpoints-buffer-name
2797 'gdb-breakpoints-mode
2798 'gdb-invalidate-breakpoints)
2799
2800 (defun gdb-breakpoints-list-handler-custom ()
2801 (let ((breakpoints-list (bindat-get-field
2802 (gdb-json-partial-output "bkpt" "script")
2803 'BreakpointTable 'body))
2804 (table (make-gdb-table)))
2805 (setq gdb-breakpoints-list nil)
2806 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2807 (dolist (breakpoint breakpoints-list)
2808 (add-to-list 'gdb-breakpoints-list
2809 (cons (bindat-get-field breakpoint 'number)
2810 breakpoint))
2811 (let ((at (bindat-get-field breakpoint 'at))
2812 (pending (bindat-get-field breakpoint 'pending))
2813 (func (bindat-get-field breakpoint 'func))
2814 (type (bindat-get-field breakpoint 'type)))
2815 (gdb-table-add-row table
2816 (list
2817 (bindat-get-field breakpoint 'number)
2818 (or type "")
2819 (or (bindat-get-field breakpoint 'disp) "")
2820 (let ((flag (bindat-get-field breakpoint 'enabled)))
2821 (if (string-equal flag "y")
2822 (eval-when-compile
2823 (propertize "y" 'font-lock-face
2824 font-lock-warning-face))
2825 (eval-when-compile
2826 (propertize "n" 'font-lock-face
2827 font-lock-comment-face))))
2828 (bindat-get-field breakpoint 'addr)
2829 (or (bindat-get-field breakpoint 'times) "")
2830 (if (and type (string-match ".*watchpoint" type))
2831 (bindat-get-field breakpoint 'what)
2832 (or pending at
2833 (concat "in "
2834 (propertize (or func "unknown")
2835 'font-lock-face font-lock-function-name-face)
2836 (gdb-frame-location breakpoint)))))
2837 ;; Add clickable properties only for breakpoints with file:line
2838 ;; information
2839 (append (list 'gdb-breakpoint breakpoint)
2840 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2841 mouse-face highlight))))))
2842 (insert (gdb-table-string table " "))
2843 (gdb-place-breakpoints)))
2844
2845 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2846 (defun gdb-place-breakpoints ()
2847 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2848 (dolist (buffer (buffer-list))
2849 (with-current-buffer buffer
2850 (if (and (eq gud-minor-mode 'gdbmi)
2851 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2852 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2853 (dolist (breakpoint gdb-breakpoints-list)
2854 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2855 ; an associative list
2856 (line (bindat-get-field breakpoint 'line)))
2857 (when line
2858 (let ((file (bindat-get-field breakpoint 'fullname))
2859 (flag (bindat-get-field breakpoint 'enabled))
2860 (bptno (bindat-get-field breakpoint 'number)))
2861 (unless (and file (file-exists-p file))
2862 (setq file (cdr (assoc bptno gdb-location-alist))))
2863 (if (or (null file)
2864 (string-equal file "File not found"))
2865 ;; If the full filename is not recorded in the
2866 ;; breakpoint structure or in `gdb-location-alist', use
2867 ;; -file-list-exec-source-file to extract it.
2868 (when (setq file (bindat-get-field breakpoint 'file))
2869 (gdb-input (concat "list " file ":1") 'ignore)
2870 (gdb-input "-file-list-exec-source-file"
2871 `(lambda () (gdb-get-location
2872 ,bptno ,line ,flag))))
2873 (with-current-buffer (find-file-noselect file 'nowarn)
2874 (gdb-init-buffer)
2875 ;; Only want one breakpoint icon at each location.
2876 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2877 (string-to-number line)))))))))
2878
2879 (defconst gdb-source-file-regexp
2880 (concat "fullname=\\(" gdb--string-regexp "\\)"))
2881
2882 (defun gdb-get-location (bptno line flag)
2883 "Find the directory containing the relevant source file.
2884 Put in buffer and place breakpoint icon."
2885 (goto-char (point-min))
2886 (catch 'file-not-found
2887 (if (re-search-forward gdb-source-file-regexp nil t)
2888 (delete (cons bptno "File not found") gdb-location-alist)
2889 ;; FIXME: Why/how do we use (match-string 1) when the search failed?
2890 (push (cons bptno (match-string 1)) gdb-location-alist)
2891 (gdb-resync)
2892 (unless (assoc bptno gdb-location-alist)
2893 (push (cons bptno "File not found") gdb-location-alist)
2894 (message-box "Cannot find source file for breakpoint location.
2895 Add directory to search path for source files using the GDB command, dir."))
2896 (throw 'file-not-found nil))
2897 (with-current-buffer (find-file-noselect (match-string 1))
2898 (gdb-init-buffer)
2899 ;; only want one breakpoint icon at each location
2900 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2901
2902 (add-hook 'find-file-hook 'gdb-find-file-hook)
2903
2904 (defun gdb-find-file-hook ()
2905 "Set up buffer for debugging if file is part of the source code
2906 of the current session."
2907 (if (and (buffer-name gud-comint-buffer)
2908 ;; in case gud or gdb-ui is just loaded
2909 gud-comint-buffer
2910 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2911 'gdbmi))
2912 (if (member buffer-file-name gdb-source-file-list)
2913 (with-current-buffer (find-buffer-visiting buffer-file-name)
2914 (gdb-init-buffer)))))
2915
2916 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2917 (declare-function gud-break "gdb-mi" t t) ; gud-def
2918 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2919
2920 (defun gdb-mouse-set-clear-breakpoint (event)
2921 "Set/clear breakpoint in left fringe/margin at mouse click.
2922 If not in a source or disassembly buffer just set point."
2923 (interactive "e")
2924 (mouse-minibuffer-check event)
2925 (let ((posn (event-end event)))
2926 (with-selected-window (posn-window posn)
2927 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2928 (if (numberp (posn-point posn))
2929 (save-excursion
2930 (goto-char (posn-point posn))
2931 (if (or (posn-object posn)
2932 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2933 'breakpoint))
2934 (gud-remove nil)
2935 (gud-break nil)))))
2936 (posn-set-point posn))))
2937
2938 (defun gdb-mouse-toggle-breakpoint-margin (event)
2939 "Enable/disable breakpoint in left margin with mouse click."
2940 (interactive "e")
2941 (mouse-minibuffer-check event)
2942 (let ((posn (event-end event)))
2943 (if (numberp (posn-point posn))
2944 (with-selected-window (posn-window posn)
2945 (save-excursion
2946 (goto-char (posn-point posn))
2947 (if (posn-object posn)
2948 (gud-basic-call
2949 (let ((bptno (get-text-property
2950 0 'gdb-bptno (car (posn-string posn)))))
2951 (concat
2952 (if (get-text-property
2953 0 'gdb-enabled (car (posn-string posn)))
2954 "-break-disable "
2955 "-break-enable ")
2956 bptno)))))))))
2957
2958 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2959 "Enable/disable breakpoint in left fringe with mouse click."
2960 (interactive "e")
2961 (mouse-minibuffer-check event)
2962 (let* ((posn (event-end event))
2963 (pos (posn-point posn))
2964 obj)
2965 (when (numberp pos)
2966 (with-selected-window (posn-window posn)
2967 (with-current-buffer (window-buffer)
2968 (goto-char pos)
2969 (dolist (overlay (overlays-in pos pos))
2970 (when (overlay-get overlay 'put-break)
2971 (setq obj (overlay-get overlay 'before-string))))
2972 (when (stringp obj)
2973 (gud-basic-call
2974 (concat
2975 (if (get-text-property 0 'gdb-enabled obj)
2976 "-break-disable "
2977 "-break-enable ")
2978 (get-text-property 0 'gdb-bptno obj)))))))))
2979
2980 (defun gdb-breakpoints-buffer-name ()
2981 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2982
2983 (defun gdb-display-breakpoints-buffer (&optional thread)
2984 "Display GDB breakpoints."
2985 (interactive)
2986 (gdb-display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)))
2987
2988 (defun gdb-frame-breakpoints-buffer (&optional thread)
2989 "Display GDB breakpoints in another frame."
2990 (interactive)
2991 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer thread)
2992 gdb-display-buffer-other-frame-action))
2993
2994 (defvar gdb-breakpoints-mode-map
2995 (let ((map (make-sparse-keymap))
2996 (menu (make-sparse-keymap "Breakpoints")))
2997 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2998 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2999 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
3000 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
3001 (suppress-keymap map)
3002 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
3003 (define-key map " " 'gdb-toggle-breakpoint)
3004 (define-key map "D" 'gdb-delete-breakpoint)
3005 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
3006 (define-key map "q" 'gdb-delete-frame-or-window)
3007 (define-key map "\r" 'gdb-goto-breakpoint)
3008 (define-key map "\t" (lambda ()
3009 (interactive)
3010 (gdb-set-window-buffer
3011 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
3012 (define-key map [mouse-2] 'gdb-goto-breakpoint)
3013 (define-key map [follow-link] 'mouse-face)
3014 map))
3015
3016 (defun gdb-delete-frame-or-window ()
3017 "Delete frame if there is only one window. Otherwise delete the window."
3018 (interactive)
3019 (if (one-window-p) (delete-frame)
3020 (delete-window)))
3021
3022 ;;from make-mode-line-mouse-map
3023 (defun gdb-make-header-line-mouse-map (mouse function) "\
3024 Return a keymap with single entry for mouse key MOUSE on the header line.
3025 MOUSE is defined to run function FUNCTION with no args in the buffer
3026 corresponding to the mode line clicked."
3027 (let ((map (make-sparse-keymap)))
3028 (define-key map (vector 'header-line mouse) function)
3029 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
3030 map))
3031
3032 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
3033 `(propertize ,name
3034 'help-echo ,help-echo
3035 'mouse-face ',mouse-face
3036 'face ',face
3037 'local-map
3038 (gdb-make-header-line-mouse-map
3039 'mouse-1
3040 (lambda (event) (interactive "e")
3041 (save-selected-window
3042 (select-window (posn-window (event-start event)))
3043 (gdb-set-window-buffer
3044 (gdb-get-buffer-create ',buffer) t) )))))
3045
3046 \f
3047 ;; uses "-thread-info". Needs GDB 7.0 onwards.
3048 ;;; Threads view
3049
3050 (defun gdb-threads-buffer-name ()
3051 (concat "*threads of " (gdb-get-target-string) "*"))
3052
3053 (defun gdb-display-threads-buffer (&optional thread)
3054 "Display GDB threads."
3055 (interactive)
3056 (gdb-display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)))
3057
3058 (defun gdb-frame-threads-buffer (&optional thread)
3059 "Display GDB threads in another frame."
3060 (interactive)
3061 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer thread)
3062 gdb-display-buffer-other-frame-action))
3063
3064 (def-gdb-trigger-and-handler
3065 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
3066 gdb-thread-list-handler gdb-thread-list-handler-custom
3067 '(start update update-threads))
3068
3069 (gdb-set-buffer-rules
3070 'gdb-threads-buffer
3071 'gdb-threads-buffer-name
3072 'gdb-threads-mode
3073 'gdb-invalidate-threads)
3074
3075 (defvar gdb-threads-font-lock-keywords
3076 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
3077 (" \\(stopped\\)" (1 font-lock-warning-face))
3078 (" \\(running\\)" (1 font-lock-string-face))
3079 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
3080 "Font lock keywords used in `gdb-threads-mode'.")
3081
3082 (defvar gdb-threads-mode-map
3083 (let ((map (make-sparse-keymap)))
3084 (define-key map "\r" 'gdb-select-thread)
3085 (define-key map "f" 'gdb-display-stack-for-thread)
3086 (define-key map "F" 'gdb-frame-stack-for-thread)
3087 (define-key map "l" 'gdb-display-locals-for-thread)
3088 (define-key map "L" 'gdb-frame-locals-for-thread)
3089 (define-key map "r" 'gdb-display-registers-for-thread)
3090 (define-key map "R" 'gdb-frame-registers-for-thread)
3091 (define-key map "d" 'gdb-display-disassembly-for-thread)
3092 (define-key map "D" 'gdb-frame-disassembly-for-thread)
3093 (define-key map "i" 'gdb-interrupt-thread)
3094 (define-key map "c" 'gdb-continue-thread)
3095 (define-key map "s" 'gdb-step-thread)
3096 (define-key map "\t"
3097 (lambda ()
3098 (interactive)
3099 (gdb-set-window-buffer
3100 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
3101 (define-key map [mouse-2] 'gdb-select-thread)
3102 (define-key map [follow-link] 'mouse-face)
3103 map))
3104
3105 (defvar gdb-threads-header
3106 (list
3107 (gdb-propertize-header
3108 "Breakpoints" gdb-breakpoints-buffer
3109 "mouse-1: select" mode-line-highlight mode-line-inactive)
3110 " "
3111 (gdb-propertize-header "Threads" gdb-threads-buffer
3112 nil nil mode-line)))
3113
3114 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
3115 "Major mode for GDB threads."
3116 (setq gdb-thread-position (make-marker))
3117 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
3118 (setq header-line-format gdb-threads-header)
3119 (set (make-local-variable 'font-lock-defaults)
3120 '(gdb-threads-font-lock-keywords))
3121 'gdb-invalidate-threads)
3122
3123 (defun gdb-thread-list-handler-custom ()
3124 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
3125 (table (make-gdb-table))
3126 (marked-line nil))
3127 (setq gdb-threads-list nil)
3128 (setq gdb-running-threads-count 0)
3129 (setq gdb-stopped-threads-count 0)
3130 (set-marker gdb-thread-position nil)
3131
3132 (dolist (thread (reverse threads-list))
3133 (let ((running (equal (bindat-get-field thread 'state) "running")))
3134 (add-to-list 'gdb-threads-list
3135 (cons (bindat-get-field thread 'id)
3136 thread))
3137 (cl-incf (if running
3138 gdb-running-threads-count
3139 gdb-stopped-threads-count))
3140
3141 (gdb-table-add-row
3142 table
3143 (list
3144 (bindat-get-field thread 'id)
3145 (concat
3146 (if gdb-thread-buffer-verbose-names
3147 (concat (bindat-get-field thread 'target-id) " ") "")
3148 (bindat-get-field thread 'state)
3149 ;; Include frame information for stopped threads
3150 (if (not running)
3151 (concat
3152 " in " (bindat-get-field thread 'frame 'func)
3153 (if gdb-thread-buffer-arguments
3154 (concat
3155 " ("
3156 (let ((args (bindat-get-field thread 'frame 'args)))
3157 (mapconcat
3158 (lambda (arg)
3159 (apply #'format "%s=%s"
3160 (gdb-get-many-fields arg 'name 'value)))
3161 args ","))
3162 ")")
3163 "")
3164 (if gdb-thread-buffer-locations
3165 (gdb-frame-location (bindat-get-field thread 'frame)) "")
3166 (if gdb-thread-buffer-addresses
3167 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
3168 "")))
3169 (list
3170 'gdb-thread thread
3171 'mouse-face 'highlight
3172 'help-echo "mouse-2, RET: select thread")))
3173 (when (string-equal gdb-thread-number
3174 (bindat-get-field thread 'id))
3175 (setq marked-line (length gdb-threads-list))))
3176 (insert (gdb-table-string table " "))
3177 (when marked-line
3178 (gdb-mark-line marked-line gdb-thread-position)))
3179 ;; We update gud-running here because we need to make sure that
3180 ;; gdb-threads-list is up-to-date
3181 (gdb-update-gud-running)
3182 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
3183
3184 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
3185 "Define a NAME command which will act upon thread on the current line.
3186
3187 CUSTOM-DEFUN may use locally bound `thread' variable, which will
3188 be the value of 'gdb-thread property of the current line.
3189 If `gdb-thread' is nil, error is signaled."
3190 `(defun ,name (&optional event)
3191 ,(when doc doc)
3192 (interactive (list last-input-event))
3193 (if event (posn-set-point (event-end event)))
3194 (save-excursion
3195 (beginning-of-line)
3196 (let ((thread (get-text-property (point) 'gdb-thread)))
3197 (if thread
3198 ,custom-defun
3199 (error "Not recognized as thread line"))))))
3200
3201 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command
3202 &optional doc)
3203 "Define a NAME which will call BUFFER-COMMAND with id of thread
3204 on the current line."
3205 `(def-gdb-thread-buffer-command ,name
3206 (,buffer-command (bindat-get-field thread 'id))
3207 ,doc))
3208
3209 (def-gdb-thread-buffer-command gdb-select-thread
3210 (let ((new-id (bindat-get-field thread 'id)))
3211 (gdb-setq-thread-number new-id)
3212 (gdb-input (concat "-thread-select " new-id) 'ignore)
3213 (gdb-update))
3214 "Select the thread at current line of threads buffer.")
3215
3216 (def-gdb-thread-buffer-simple-command
3217 gdb-display-stack-for-thread
3218 gdb-preemptively-display-stack-buffer
3219 "Display stack buffer for the thread at current line.")
3220
3221 (def-gdb-thread-buffer-simple-command
3222 gdb-display-locals-for-thread
3223 gdb-preemptively-display-locals-buffer
3224 "Display locals buffer for the thread at current line.")
3225
3226 (def-gdb-thread-buffer-simple-command
3227 gdb-display-registers-for-thread
3228 gdb-preemptively-display-registers-buffer
3229 "Display registers buffer for the thread at current line.")
3230
3231 (def-gdb-thread-buffer-simple-command
3232 gdb-display-disassembly-for-thread
3233 gdb-preemptively-display-disassembly-buffer
3234 "Display disassembly buffer for the thread at current line.")
3235
3236 (def-gdb-thread-buffer-simple-command
3237 gdb-frame-stack-for-thread
3238 gdb-frame-stack-buffer
3239 "Display another frame with stack buffer for thread at current line.")
3240
3241 (def-gdb-thread-buffer-simple-command
3242 gdb-frame-locals-for-thread
3243 gdb-frame-locals-buffer
3244 "Display another frame with locals buffer for thread at current line.")
3245
3246 (def-gdb-thread-buffer-simple-command
3247 gdb-frame-registers-for-thread
3248 gdb-frame-registers-buffer
3249 "Display another frame with registers buffer for the thread at current line.")
3250
3251 (def-gdb-thread-buffer-simple-command
3252 gdb-frame-disassembly-for-thread
3253 gdb-frame-disassembly-buffer
3254 "Display another frame with disassembly buffer for the thread at current line.")
3255
3256 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
3257 "Define a NAME which will execute GUD-COMMAND with
3258 `gdb-thread-number' locally bound to id of thread on the current
3259 line."
3260 `(def-gdb-thread-buffer-command ,name
3261 (if gdb-non-stop
3262 (let ((gdb-thread-number (bindat-get-field thread 'id))
3263 (gdb-gud-control-all-threads nil))
3264 (call-interactively #',gud-command))
3265 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
3266 ,doc))
3267
3268 (def-gdb-thread-buffer-gud-command
3269 gdb-interrupt-thread
3270 gud-stop-subjob
3271 "Interrupt thread at current line.")
3272
3273 ;; Defined opaquely in M-x gdb via gud-def.
3274 (declare-function gud-cont "gdb-mi" (arg) t)
3275
3276 (def-gdb-thread-buffer-gud-command
3277 gdb-continue-thread
3278 gud-cont
3279 "Continue thread at current line.")
3280
3281 (declare-function gud-step "gdb-mi" (arg) t)
3282
3283 (def-gdb-thread-buffer-gud-command
3284 gdb-step-thread
3285 gud-step
3286 "Step thread at current line.")
3287
3288 \f
3289 ;;; Memory view
3290
3291 (defcustom gdb-memory-rows 8
3292 "Number of data rows in memory window."
3293 :type 'integer
3294 :group 'gud
3295 :version "23.2")
3296
3297 (defcustom gdb-memory-columns 4
3298 "Number of data columns in memory window."
3299 :type 'integer
3300 :group 'gud
3301 :version "23.2")
3302
3303 (defcustom gdb-memory-format "x"
3304 "Display format of data items in memory window."
3305 :type '(choice (const :tag "Hexadecimal" "x")
3306 (const :tag "Signed decimal" "d")
3307 (const :tag "Unsigned decimal" "u")
3308 (const :tag "Octal" "o")
3309 (const :tag "Binary" "t"))
3310 :group 'gud
3311 :version "22.1")
3312
3313 (defcustom gdb-memory-unit 4
3314 "Unit size of data items in memory window."
3315 :type '(choice (const :tag "Byte" 1)
3316 (const :tag "Halfword" 2)
3317 (const :tag "Word" 4)
3318 (const :tag "Giant word" 8))
3319 :group 'gud
3320 :version "23.2")
3321
3322 (def-gdb-trigger-and-handler
3323 gdb-invalidate-memory
3324 (format "-data-read-memory %s %s %d %d %d"
3325 gdb-memory-address
3326 gdb-memory-format
3327 gdb-memory-unit
3328 gdb-memory-rows
3329 gdb-memory-columns)
3330 gdb-read-memory-handler
3331 gdb-read-memory-custom
3332 '(start update))
3333
3334 (gdb-set-buffer-rules
3335 'gdb-memory-buffer
3336 'gdb-memory-buffer-name
3337 'gdb-memory-mode
3338 'gdb-invalidate-memory)
3339
3340 (defun gdb-memory-column-width (size format)
3341 "Return length of string with memory unit of SIZE in FORMAT.
3342
3343 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
3344 in `gdb-memory-format'."
3345 (let ((format-base (cdr (assoc format
3346 '(("x" . 16)
3347 ("d" . 10) ("u" . 10)
3348 ("o" . 8)
3349 ("t" . 2))))))
3350 (if format-base
3351 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
3352 (cond ((string-equal format "x")
3353 (+ 2 res)) ; hexadecimal numbers have 0x in front
3354 ((or (string-equal format "d")
3355 (string-equal format "o"))
3356 (1+ res))
3357 (t res)))
3358 (error "Unknown format"))))
3359
3360 (defun gdb-read-memory-custom ()
3361 (let* ((res (gdb-json-partial-output))
3362 (err-msg (bindat-get-field res 'msg)))
3363 (if (not err-msg)
3364 (let ((memory (bindat-get-field res 'memory)))
3365 (setq gdb-memory-address (bindat-get-field res 'addr))
3366 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
3367 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
3368 (setq gdb-memory-last-address gdb-memory-address)
3369 (dolist (row memory)
3370 (insert (concat (bindat-get-field row 'addr) ":"))
3371 (dolist (column (bindat-get-field row 'data))
3372 (insert (gdb-pad-string column
3373 (+ 2 (gdb-memory-column-width
3374 gdb-memory-unit
3375 gdb-memory-format)))))
3376 (newline)))
3377 ;; Show last page instead of empty buffer when out of bounds
3378 (progn
3379 (let ((gdb-memory-address gdb-memory-last-address))
3380 (gdb-invalidate-memory 'update)
3381 (error err-msg))))))
3382
3383 (defvar gdb-memory-mode-map
3384 (let ((map (make-sparse-keymap)))
3385 (suppress-keymap map t)
3386 (define-key map "q" 'kill-this-buffer)
3387 (define-key map "n" 'gdb-memory-show-next-page)
3388 (define-key map "p" 'gdb-memory-show-previous-page)
3389 (define-key map "a" 'gdb-memory-set-address)
3390 (define-key map "t" 'gdb-memory-format-binary)
3391 (define-key map "o" 'gdb-memory-format-octal)
3392 (define-key map "u" 'gdb-memory-format-unsigned)
3393 (define-key map "d" 'gdb-memory-format-signed)
3394 (define-key map "x" 'gdb-memory-format-hexadecimal)
3395 (define-key map "b" 'gdb-memory-unit-byte)
3396 (define-key map "h" 'gdb-memory-unit-halfword)
3397 (define-key map "w" 'gdb-memory-unit-word)
3398 (define-key map "g" 'gdb-memory-unit-giant)
3399 (define-key map "R" 'gdb-memory-set-rows)
3400 (define-key map "C" 'gdb-memory-set-columns)
3401 map))
3402
3403 (defun gdb-memory-set-address-event (event)
3404 "Handle a click on address field in memory buffer header."
3405 (interactive "e")
3406 (save-selected-window
3407 (select-window (posn-window (event-start event)))
3408 (gdb-memory-set-address)))
3409
3410 ;; Non-event version for use within keymap
3411 (defun gdb-memory-set-address ()
3412 "Set the start memory address."
3413 (interactive)
3414 (let ((arg (read-from-minibuffer "Memory address: ")))
3415 (setq gdb-memory-address arg))
3416 (gdb-invalidate-memory 'update))
3417
3418 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
3419 "Define a function NAME which reads new VAR value from minibuffer."
3420 `(defun ,name (event)
3421 ,(when doc doc)
3422 (interactive "e")
3423 (save-selected-window
3424 (select-window (posn-window (event-start event)))
3425 (let* ((arg (read-from-minibuffer ,echo-string))
3426 (count (string-to-number arg)))
3427 (if (<= count 0)
3428 (error "Positive number only")
3429 (customize-set-variable ',variable count)
3430 (gdb-invalidate-memory 'update))))))
3431
3432 (def-gdb-set-positive-number
3433 gdb-memory-set-rows
3434 gdb-memory-rows
3435 "Rows: "
3436 "Set the number of data rows in memory window.")
3437
3438 (def-gdb-set-positive-number
3439 gdb-memory-set-columns
3440 gdb-memory-columns
3441 "Columns: "
3442 "Set the number of data columns in memory window.")
3443
3444 (defmacro def-gdb-memory-format (name format doc)
3445 "Define a function NAME to switch memory buffer to use FORMAT.
3446
3447 DOC is an optional documentation string."
3448 `(defun ,name () ,(when doc doc)
3449 (interactive)
3450 (customize-set-variable 'gdb-memory-format ,format)
3451 (gdb-invalidate-memory 'update)))
3452
3453 (def-gdb-memory-format
3454 gdb-memory-format-binary "t"
3455 "Set the display format to binary.")
3456
3457 (def-gdb-memory-format
3458 gdb-memory-format-octal "o"
3459 "Set the display format to octal.")
3460
3461 (def-gdb-memory-format
3462 gdb-memory-format-unsigned "u"
3463 "Set the display format to unsigned decimal.")
3464
3465 (def-gdb-memory-format
3466 gdb-memory-format-signed "d"
3467 "Set the display format to decimal.")
3468
3469 (def-gdb-memory-format
3470 gdb-memory-format-hexadecimal "x"
3471 "Set the display format to hexadecimal.")
3472
3473 (defvar gdb-memory-format-map
3474 (let ((map (make-sparse-keymap)))
3475 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
3476 map)
3477 "Keymap to select format in the header line.")
3478
3479 (defvar gdb-memory-format-menu
3480 (let ((map (make-sparse-keymap "Format")))
3481
3482 (define-key map [binary]
3483 '(menu-item "Binary" gdb-memory-format-binary
3484 :button (:radio . (equal gdb-memory-format "t"))))
3485 (define-key map [octal]
3486 '(menu-item "Octal" gdb-memory-format-octal
3487 :button (:radio . (equal gdb-memory-format "o"))))
3488 (define-key map [unsigned]
3489 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
3490 :button (:radio . (equal gdb-memory-format "u"))))
3491 (define-key map [signed]
3492 '(menu-item "Signed Decimal" gdb-memory-format-signed
3493 :button (:radio . (equal gdb-memory-format "d"))))
3494 (define-key map [hexadecimal]
3495 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3496 :button (:radio . (equal gdb-memory-format "x"))))
3497 map)
3498 "Menu of display formats in the header line.")
3499
3500 (defun gdb-memory-format-menu (event)
3501 (interactive "@e")
3502 (x-popup-menu event gdb-memory-format-menu))
3503
3504 (defun gdb-memory-format-menu-1 (event)
3505 (interactive "e")
3506 (save-selected-window
3507 (select-window (posn-window (event-start event)))
3508 (let* ((selection (gdb-memory-format-menu event))
3509 (binding (and selection (lookup-key gdb-memory-format-menu
3510 (vector (car selection))))))
3511 (if binding (call-interactively binding)))))
3512
3513 (defmacro def-gdb-memory-unit (name unit-size doc)
3514 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3515
3516 DOC is an optional documentation string."
3517 `(defun ,name () ,(when doc doc)
3518 (interactive)
3519 (customize-set-variable 'gdb-memory-unit ,unit-size)
3520 (gdb-invalidate-memory 'update)))
3521
3522 (def-gdb-memory-unit gdb-memory-unit-giant 8
3523 "Set the unit size to giant words (eight bytes).")
3524
3525 (def-gdb-memory-unit gdb-memory-unit-word 4
3526 "Set the unit size to words (four bytes).")
3527
3528 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3529 "Set the unit size to halfwords (two bytes).")
3530
3531 (def-gdb-memory-unit gdb-memory-unit-byte 1
3532 "Set the unit size to bytes.")
3533
3534 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3535 "Define a function NAME which show new address in memory buffer.
3536
3537 The defined function switches Memory buffer to show address
3538 stored in ADDRESS-VAR variable.
3539
3540 DOC is an optional documentation string."
3541 `(defun ,name
3542 ,(when doc doc)
3543 (interactive)
3544 (let ((gdb-memory-address ,address-var))
3545 (gdb-invalidate-memory))))
3546
3547 (def-gdb-memory-show-page gdb-memory-show-previous-page
3548 gdb-memory-prev-page)
3549
3550 (def-gdb-memory-show-page gdb-memory-show-next-page
3551 gdb-memory-next-page)
3552
3553 (defvar gdb-memory-unit-map
3554 (let ((map (make-sparse-keymap)))
3555 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3556 map)
3557 "Keymap to select units in the header line.")
3558
3559 (defvar gdb-memory-unit-menu
3560 (let ((map (make-sparse-keymap "Unit")))
3561 (define-key map [giantwords]
3562 '(menu-item "Giant words" gdb-memory-unit-giant
3563 :button (:radio . (equal gdb-memory-unit 8))))
3564 (define-key map [words]
3565 '(menu-item "Words" gdb-memory-unit-word
3566 :button (:radio . (equal gdb-memory-unit 4))))
3567 (define-key map [halfwords]
3568 '(menu-item "Halfwords" gdb-memory-unit-halfword
3569 :button (:radio . (equal gdb-memory-unit 2))))
3570 (define-key map [bytes]
3571 '(menu-item "Bytes" gdb-memory-unit-byte
3572 :button (:radio . (equal gdb-memory-unit 1))))
3573 map)
3574 "Menu of units in the header line.")
3575
3576 (defun gdb-memory-unit-menu (event)
3577 (interactive "@e")
3578 (x-popup-menu event gdb-memory-unit-menu))
3579
3580 (defun gdb-memory-unit-menu-1 (event)
3581 (interactive "e")
3582 (save-selected-window
3583 (select-window (posn-window (event-start event)))
3584 (let* ((selection (gdb-memory-unit-menu event))
3585 (binding (and selection (lookup-key gdb-memory-unit-menu
3586 (vector (car selection))))))
3587 (if binding (call-interactively binding)))))
3588
3589 (defvar gdb-memory-font-lock-keywords
3590 '(;; <__function.name+n>
3591 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3592 (1 font-lock-function-name-face)))
3593 "Font lock keywords used in `gdb-memory-mode'.")
3594
3595 (defvar gdb-memory-header
3596 '(:eval
3597 (concat
3598 "Start address["
3599 (propertize "-"
3600 'face font-lock-warning-face
3601 'help-echo "mouse-1: decrement address"
3602 'mouse-face 'mode-line-highlight
3603 'local-map (gdb-make-header-line-mouse-map
3604 'mouse-1
3605 #'gdb-memory-show-previous-page))
3606 "|"
3607 (propertize "+"
3608 'face font-lock-warning-face
3609 'help-echo "mouse-1: increment address"
3610 'mouse-face 'mode-line-highlight
3611 'local-map (gdb-make-header-line-mouse-map
3612 'mouse-1
3613 #'gdb-memory-show-next-page))
3614 "]: "
3615 (propertize gdb-memory-address
3616 'face font-lock-warning-face
3617 'help-echo "mouse-1: set start address"
3618 'mouse-face 'mode-line-highlight
3619 'local-map (gdb-make-header-line-mouse-map
3620 'mouse-1
3621 #'gdb-memory-set-address-event))
3622 " Rows: "
3623 (propertize (number-to-string gdb-memory-rows)
3624 'face font-lock-warning-face
3625 'help-echo "mouse-1: set number of columns"
3626 'mouse-face 'mode-line-highlight
3627 'local-map (gdb-make-header-line-mouse-map
3628 'mouse-1
3629 #'gdb-memory-set-rows))
3630 " Columns: "
3631 (propertize (number-to-string gdb-memory-columns)
3632 'face font-lock-warning-face
3633 'help-echo "mouse-1: set number of columns"
3634 'mouse-face 'mode-line-highlight
3635 'local-map (gdb-make-header-line-mouse-map
3636 'mouse-1
3637 #'gdb-memory-set-columns))
3638 " Display Format: "
3639 (propertize gdb-memory-format
3640 'face font-lock-warning-face
3641 'help-echo "mouse-3: select display format"
3642 'mouse-face 'mode-line-highlight
3643 'local-map gdb-memory-format-map)
3644 " Unit Size: "
3645 (propertize (number-to-string gdb-memory-unit)
3646 'face font-lock-warning-face
3647 'help-echo "mouse-3: select unit size"
3648 'mouse-face 'mode-line-highlight
3649 'local-map gdb-memory-unit-map)))
3650 "Header line used in `gdb-memory-mode'.")
3651
3652 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3653 "Major mode for examining memory."
3654 (setq header-line-format gdb-memory-header)
3655 (set (make-local-variable 'font-lock-defaults)
3656 '(gdb-memory-font-lock-keywords))
3657 'gdb-invalidate-memory)
3658
3659 (defun gdb-memory-buffer-name ()
3660 (concat "*memory of " (gdb-get-target-string) "*"))
3661
3662 (defun gdb-display-memory-buffer (&optional thread)
3663 "Display GDB memory contents."
3664 (interactive)
3665 (gdb-display-buffer (gdb-get-buffer-create 'gdb-memory-buffer thread)))
3666
3667 (defun gdb-frame-memory-buffer ()
3668 "Display memory contents in another frame."
3669 (interactive)
3670 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer)
3671 gdb-display-buffer-other-frame-action))
3672
3673 \f
3674 ;;; Disassembly view
3675
3676 (defun gdb-disassembly-buffer-name ()
3677 (gdb-current-context-buffer-name
3678 (concat "disassembly of " (gdb-get-target-string))))
3679
3680 (defun gdb-display-disassembly-buffer (&optional thread)
3681 "Display GDB disassembly information."
3682 (interactive)
3683 (gdb-display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)))
3684
3685 (def-gdb-preempt-display-buffer
3686 gdb-preemptively-display-disassembly-buffer
3687 'gdb-disassembly-buffer)
3688
3689 (defun gdb-frame-disassembly-buffer (&optional thread)
3690 "Display GDB disassembly information in another frame."
3691 (interactive)
3692 (display-buffer (gdb-get-buffer-create 'gdb-disassembly-buffer thread)
3693 gdb-display-buffer-other-frame-action))
3694
3695 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3696 (let* ((frame (gdb-current-buffer-frame))
3697 (file (bindat-get-field frame 'fullname))
3698 (line (bindat-get-field frame 'line)))
3699 (if file
3700 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)
3701 ;; If we're unable to get a file name / line for $PC, simply
3702 ;; follow $PC, disassembling the next 10 (x ~15 (on IA) ==
3703 ;; 150 bytes) instructions.
3704 "-data-disassemble -s $pc -e \"$pc + 150\" -- 0"))
3705 gdb-disassembly-handler
3706 ;; We update disassembly only after we have actual frame information
3707 ;; about all threads, so no there's `update' signal in this list
3708 '(start update-disassembly))
3709
3710 (def-gdb-auto-update-handler
3711 gdb-disassembly-handler
3712 gdb-disassembly-handler-custom
3713 t)
3714
3715 (gdb-set-buffer-rules
3716 'gdb-disassembly-buffer
3717 'gdb-disassembly-buffer-name
3718 'gdb-disassembly-mode
3719 'gdb-invalidate-disassembly)
3720
3721 (defvar gdb-disassembly-font-lock-keywords
3722 '(;; <__function.name+n>
3723 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3724 (1 font-lock-function-name-face))
3725 ;; 0xNNNNNNNN <__function.name+n>: opcode
3726 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3727 (4 font-lock-keyword-face))
3728 ;; %register(at least i386)
3729 ("%\\sw+" . font-lock-variable-name-face)
3730 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3731 (1 font-lock-comment-face)
3732 (2 font-lock-function-name-face))
3733 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3734 "Font lock keywords used in `gdb-disassembly-mode'.")
3735
3736 (defvar gdb-disassembly-mode-map
3737 ;; TODO
3738 (let ((map (make-sparse-keymap)))
3739 (suppress-keymap map)
3740 (define-key map "q" 'kill-this-buffer)
3741 map))
3742
3743 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3744 "Major mode for GDB disassembly information."
3745 ;; TODO Rename overlay variable for disassembly mode
3746 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3747 (setq fringes-outside-margins t)
3748 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3749 (set (make-local-variable 'font-lock-defaults)
3750 '(gdb-disassembly-font-lock-keywords))
3751 'gdb-invalidate-disassembly)
3752
3753 (defun gdb-disassembly-handler-custom ()
3754 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3755 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3756 (table (make-gdb-table))
3757 (marked-line nil))
3758 (dolist (instr instructions)
3759 (gdb-table-add-row table
3760 (list
3761 (bindat-get-field instr 'address)
3762 (let
3763 ((func-name (bindat-get-field instr 'func-name))
3764 (offset (bindat-get-field instr 'offset)))
3765 (if func-name
3766 (format "<%s+%s>:" func-name offset)
3767 ""))
3768 (bindat-get-field instr 'inst)))
3769 (when (string-equal (bindat-get-field instr 'address)
3770 address)
3771 (progn
3772 (setq marked-line (length (gdb-table-rows table)))
3773 (setq fringe-indicator-alist
3774 (if (string-equal gdb-frame-number "0")
3775 nil
3776 '((overlay-arrow . hollow-right-triangle)))))))
3777 (insert (gdb-table-string table " "))
3778 (gdb-disassembly-place-breakpoints)
3779 ;; Mark current position with overlay arrow and scroll window to
3780 ;; that point
3781 (when marked-line
3782 (let ((window (get-buffer-window (current-buffer) 0)))
3783 (set-window-point window (gdb-mark-line marked-line
3784 gdb-disassembly-position))))
3785 (setq mode-name
3786 (gdb-current-context-mode-name
3787 (concat "Disassembly: "
3788 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3789
3790 (defun gdb-disassembly-place-breakpoints ()
3791 (gdb-remove-breakpoint-icons (point-min) (point-max))
3792 (dolist (breakpoint gdb-breakpoints-list)
3793 (let* ((breakpoint (cdr breakpoint))
3794 (bptno (bindat-get-field breakpoint 'number))
3795 (flag (bindat-get-field breakpoint 'enabled))
3796 (address (bindat-get-field breakpoint 'addr)))
3797 (save-excursion
3798 (goto-char (point-min))
3799 (if (re-search-forward (concat "^" address) nil t)
3800 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3801
3802 \f
3803 (defvar gdb-breakpoints-header
3804 (list
3805 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3806 nil nil mode-line)
3807 " "
3808 (gdb-propertize-header "Threads" gdb-threads-buffer
3809 "mouse-1: select" mode-line-highlight
3810 mode-line-inactive)))
3811
3812 ;;; Breakpoints view
3813 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3814 "Major mode for gdb breakpoints."
3815 (setq header-line-format gdb-breakpoints-header)
3816 'gdb-invalidate-breakpoints)
3817
3818 (defun gdb-toggle-breakpoint ()
3819 "Enable/disable breakpoint at current line of breakpoints buffer."
3820 (interactive)
3821 (save-excursion
3822 (beginning-of-line)
3823 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3824 (if breakpoint
3825 (gud-basic-call
3826 (concat (if (equal "y" (bindat-get-field breakpoint 'enabled))
3827 "-break-disable "
3828 "-break-enable ")
3829 (bindat-get-field breakpoint 'number)))
3830 (error "Not recognized as break/watchpoint line")))))
3831
3832 (defun gdb-delete-breakpoint ()
3833 "Delete the breakpoint at current line of breakpoints buffer."
3834 (interactive)
3835 (save-excursion
3836 (beginning-of-line)
3837 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3838 (if breakpoint
3839 (gud-basic-call (concat "-break-delete "
3840 (bindat-get-field breakpoint 'number)))
3841 (error "Not recognized as break/watchpoint line")))))
3842
3843 (defun gdb-goto-breakpoint (&optional event)
3844 "Go to the location of breakpoint at current line of breakpoints buffer."
3845 (interactive (list last-input-event))
3846 (if event (posn-set-point (event-end event)))
3847 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3848 (let ((window (get-buffer-window gud-comint-buffer)))
3849 (if window (save-selected-window (select-window window))))
3850 (save-excursion
3851 (beginning-of-line)
3852 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3853 (if breakpoint
3854 (let ((bptno (bindat-get-field breakpoint 'number))
3855 (file (bindat-get-field breakpoint 'fullname))
3856 (line (bindat-get-field breakpoint 'line)))
3857 (save-selected-window
3858 (let* ((buffer (find-file-noselect
3859 (if (file-exists-p file) file
3860 (cdr (assoc bptno gdb-location-alist)))))
3861 (window (or (gdb-display-source-buffer buffer)
3862 (display-buffer buffer))))
3863 (setq gdb-source-window window)
3864 (with-current-buffer buffer
3865 (goto-char (point-min))
3866 (forward-line (1- (string-to-number line)))
3867 (set-window-point window (point))))))
3868 (error "Not recognized as break/watchpoint line")))))
3869
3870 \f
3871 ;; Frames buffer. This displays a perpetually correct backtrack trace.
3872 ;;
3873 (def-gdb-trigger-and-handler
3874 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3875 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3876 '(start update))
3877
3878 (gdb-set-buffer-rules
3879 'gdb-stack-buffer
3880 'gdb-stack-buffer-name
3881 'gdb-frames-mode
3882 'gdb-invalidate-frames)
3883
3884 (defun gdb-frame-location (frame)
3885 "Return \" of file:line\" or \" of library\" for structure FRAME.
3886
3887 FRAME must have either \"file\" and \"line\" members or \"from\"
3888 member."
3889 (let ((file (bindat-get-field frame 'file))
3890 (line (bindat-get-field frame 'line))
3891 (from (bindat-get-field frame 'from)))
3892 (let ((res (or (and file line (concat file ":" line))
3893 from)))
3894 (if res (concat " of " res) ""))))
3895
3896 (defun gdb-stack-list-frames-custom ()
3897 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3898 (table (make-gdb-table)))
3899 (set-marker gdb-stack-position nil)
3900 (dolist (frame stack)
3901 (gdb-table-add-row table
3902 (list
3903 (bindat-get-field frame 'level)
3904 "in"
3905 (concat
3906 (bindat-get-field frame 'func)
3907 (if gdb-stack-buffer-locations
3908 (gdb-frame-location frame) "")
3909 (if gdb-stack-buffer-addresses
3910 (concat " at " (bindat-get-field frame 'addr)) "")))
3911 `(mouse-face highlight
3912 help-echo "mouse-2, RET: Select frame"
3913 gdb-frame ,frame)))
3914 (insert (gdb-table-string table " ")))
3915 (when (and gdb-frame-number
3916 (gdb-buffer-shows-main-thread-p))
3917 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3918 gdb-stack-position))
3919 (setq mode-name
3920 (gdb-current-context-mode-name "Frames")))
3921
3922 (defun gdb-stack-buffer-name ()
3923 (gdb-current-context-buffer-name
3924 (concat "stack frames of " (gdb-get-target-string))))
3925
3926 (defun gdb-display-stack-buffer (&optional thread)
3927 "Display GDB backtrace for current stack."
3928 (interactive)
3929 (gdb-display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)))
3930
3931 (def-gdb-preempt-display-buffer
3932 gdb-preemptively-display-stack-buffer
3933 'gdb-stack-buffer nil t)
3934
3935 (defun gdb-frame-stack-buffer (&optional thread)
3936 "Display GDB backtrace for current stack in another frame."
3937 (interactive)
3938 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer thread)
3939 gdb-display-buffer-other-frame-action))
3940
3941 (defvar gdb-frames-mode-map
3942 (let ((map (make-sparse-keymap)))
3943 (suppress-keymap map)
3944 (define-key map "q" 'kill-this-buffer)
3945 (define-key map "\r" 'gdb-select-frame)
3946 (define-key map [mouse-2] 'gdb-select-frame)
3947 (define-key map [follow-link] 'mouse-face)
3948 map))
3949
3950 (defvar gdb-frames-font-lock-keywords
3951 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3952 "Font lock keywords used in `gdb-frames-mode'.")
3953
3954 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3955 "Major mode for gdb call stack."
3956 (setq gdb-stack-position (make-marker))
3957 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3958 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3959 (set (make-local-variable 'font-lock-defaults)
3960 '(gdb-frames-font-lock-keywords))
3961 'gdb-invalidate-frames)
3962
3963 (defun gdb-select-frame (&optional event)
3964 "Select the frame and display the relevant source."
3965 (interactive (list last-input-event))
3966 (if event (posn-set-point (event-end event)))
3967 (let ((frame (get-text-property (point) 'gdb-frame)))
3968 (if frame
3969 (if (gdb-buffer-shows-main-thread-p)
3970 (let ((new-level (bindat-get-field frame 'level)))
3971 (setq gdb-frame-number new-level)
3972 (gdb-input (concat "-stack-select-frame " new-level)
3973 'ignore)
3974 (gdb-update))
3975 (error "Could not select frame for non-current thread"))
3976 (error "Not recognized as frame line"))))
3977
3978 \f
3979 ;; Locals buffer.
3980 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3981 (def-gdb-trigger-and-handler
3982 gdb-invalidate-locals
3983 (concat (gdb-current-context-command "-stack-list-locals")
3984 " --simple-values")
3985 gdb-locals-handler gdb-locals-handler-custom
3986 '(start update))
3987
3988 (gdb-set-buffer-rules
3989 'gdb-locals-buffer
3990 'gdb-locals-buffer-name
3991 'gdb-locals-mode
3992 'gdb-invalidate-locals)
3993
3994 (defvar gdb-locals-watch-map
3995 (let ((map (make-sparse-keymap)))
3996 (suppress-keymap map)
3997 (define-key map "\r" 'gud-watch)
3998 (define-key map [mouse-2] 'gud-watch)
3999 map)
4000 "Keymap to create watch expression of a complex data type local variable.")
4001
4002 (defvar gdb-edit-locals-map-1
4003 (let ((map (make-sparse-keymap)))
4004 (suppress-keymap map)
4005 (define-key map "\r" 'gdb-edit-locals-value)
4006 (define-key map [mouse-2] 'gdb-edit-locals-value)
4007 map)
4008 "Keymap to edit value of a simple data type local variable.")
4009
4010 (defun gdb-edit-locals-value (&optional event)
4011 "Assign a value to a variable displayed in the locals buffer."
4012 (interactive (list last-input-event))
4013 (save-excursion
4014 (if event (posn-set-point (event-end event)))
4015 (beginning-of-line)
4016 (let* ((var (bindat-get-field
4017 (get-text-property (point) 'gdb-local-variable) 'name))
4018 (value (read-string (format "New value (%s): " var))))
4019 (gud-basic-call
4020 (concat "-gdb-set variable " var " = " value)))))
4021
4022 ;; Don't display values of arrays or structures.
4023 ;; These can be expanded using gud-watch.
4024 (defun gdb-locals-handler-custom ()
4025 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
4026 (table (make-gdb-table)))
4027 (dolist (local locals-list)
4028 (let ((name (bindat-get-field local 'name))
4029 (value (bindat-get-field local 'value))
4030 (type (bindat-get-field local 'type)))
4031 (if (or (not value)
4032 (string-match "\\0x" value))
4033 (add-text-properties 0 (length name)
4034 `(mouse-face highlight
4035 help-echo "mouse-2: create watch expression"
4036 local-map ,gdb-locals-watch-map)
4037 name)
4038 (add-text-properties 0 (length value)
4039 `(mouse-face highlight
4040 help-echo "mouse-2: edit value"
4041 local-map ,gdb-edit-locals-map-1)
4042 value))
4043 (gdb-table-add-row
4044 table
4045 (list
4046 (propertize type 'font-lock-face font-lock-type-face)
4047 (propertize name 'font-lock-face font-lock-variable-name-face)
4048 value)
4049 `(gdb-local-variable ,local))))
4050 (insert (gdb-table-string table " "))
4051 (setq mode-name
4052 (gdb-current-context-mode-name
4053 (concat "Locals: "
4054 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
4055
4056 (defvar gdb-locals-header
4057 (list
4058 (gdb-propertize-header "Locals" gdb-locals-buffer
4059 nil nil mode-line)
4060 " "
4061 (gdb-propertize-header "Registers" gdb-registers-buffer
4062 "mouse-1: select" mode-line-highlight
4063 mode-line-inactive)))
4064
4065 (defvar gdb-locals-mode-map
4066 (let ((map (make-sparse-keymap)))
4067 (suppress-keymap map)
4068 (define-key map "q" 'kill-this-buffer)
4069 (define-key map "\t" (lambda ()
4070 (interactive)
4071 (gdb-set-window-buffer
4072 (gdb-get-buffer-create
4073 'gdb-registers-buffer
4074 gdb-thread-number) t)))
4075 map))
4076
4077 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
4078 "Major mode for gdb locals."
4079 (setq header-line-format gdb-locals-header)
4080 'gdb-invalidate-locals)
4081
4082 (defun gdb-locals-buffer-name ()
4083 (gdb-current-context-buffer-name
4084 (concat "locals of " (gdb-get-target-string))))
4085
4086 (defun gdb-display-locals-buffer (&optional thread)
4087 "Display the local variables of current GDB stack."
4088 (interactive)
4089 (gdb-display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)))
4090
4091 (def-gdb-preempt-display-buffer
4092 gdb-preemptively-display-locals-buffer
4093 'gdb-locals-buffer nil t)
4094
4095 (defun gdb-frame-locals-buffer (&optional thread)
4096 "Display the local variables of the current GDB stack in another frame."
4097 (interactive)
4098 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer thread)
4099 gdb-display-buffer-other-frame-action))
4100
4101 \f
4102 ;; Registers buffer.
4103
4104 (def-gdb-trigger-and-handler
4105 gdb-invalidate-registers
4106 (concat (gdb-current-context-command "-data-list-register-values") " x")
4107 gdb-registers-handler
4108 gdb-registers-handler-custom
4109 '(start update))
4110
4111 (gdb-set-buffer-rules
4112 'gdb-registers-buffer
4113 'gdb-registers-buffer-name
4114 'gdb-registers-mode
4115 'gdb-invalidate-registers)
4116
4117 (defun gdb-registers-handler-custom ()
4118 (when gdb-register-names
4119 (let ((register-values
4120 (bindat-get-field (gdb-json-partial-output) 'register-values))
4121 (table (make-gdb-table)))
4122 (dolist (register register-values)
4123 (let* ((register-number (bindat-get-field register 'number))
4124 (value (bindat-get-field register 'value))
4125 (register-name (nth (string-to-number register-number)
4126 gdb-register-names)))
4127 (gdb-table-add-row
4128 table
4129 (list
4130 (propertize register-name
4131 'font-lock-face font-lock-variable-name-face)
4132 (if (member register-number gdb-changed-registers)
4133 (propertize value 'font-lock-face font-lock-warning-face)
4134 value))
4135 `(mouse-face highlight
4136 help-echo "mouse-2: edit value"
4137 gdb-register-name ,register-name))))
4138 (insert (gdb-table-string table " ")))
4139 (setq mode-name
4140 (gdb-current-context-mode-name "Registers"))))
4141
4142 (defun gdb-edit-register-value (&optional event)
4143 "Assign a value to a register displayed in the registers buffer."
4144 (interactive (list last-input-event))
4145 (save-excursion
4146 (if event (posn-set-point (event-end event)))
4147 (beginning-of-line)
4148 (let* ((var (bindat-get-field
4149 (get-text-property (point) 'gdb-register-name)))
4150 (value (read-string (format "New value (%s): " var))))
4151 (gud-basic-call
4152 (concat "-gdb-set variable $" var " = " value)))))
4153
4154 (defvar gdb-registers-mode-map
4155 (let ((map (make-sparse-keymap)))
4156 (suppress-keymap map)
4157 (define-key map "\r" 'gdb-edit-register-value)
4158 (define-key map [mouse-2] 'gdb-edit-register-value)
4159 (define-key map "q" 'kill-this-buffer)
4160 (define-key map "\t" (lambda ()
4161 (interactive)
4162 (gdb-set-window-buffer
4163 (gdb-get-buffer-create
4164 'gdb-locals-buffer
4165 gdb-thread-number) t)))
4166 map))
4167
4168 (defvar gdb-registers-header
4169 (list
4170 (gdb-propertize-header "Locals" gdb-locals-buffer
4171 "mouse-1: select" mode-line-highlight
4172 mode-line-inactive)
4173 " "
4174 (gdb-propertize-header "Registers" gdb-registers-buffer
4175 nil nil mode-line)))
4176
4177 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
4178 "Major mode for gdb registers."
4179 (setq header-line-format gdb-registers-header)
4180 'gdb-invalidate-registers)
4181
4182 (defun gdb-registers-buffer-name ()
4183 (gdb-current-context-buffer-name
4184 (concat "registers of " (gdb-get-target-string))))
4185
4186 (defun gdb-display-registers-buffer (&optional thread)
4187 "Display GDB register contents."
4188 (interactive)
4189 (gdb-display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)))
4190
4191 (def-gdb-preempt-display-buffer
4192 gdb-preemptively-display-registers-buffer
4193 'gdb-registers-buffer nil t)
4194
4195 (defun gdb-frame-registers-buffer (&optional thread)
4196 "Display GDB register contents in another frame."
4197 (interactive)
4198 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer thread)
4199 gdb-display-buffer-other-frame-action))
4200
4201 ;; Needs GDB 6.4 onwards (used to fail with no stack).
4202 (defun gdb-get-changed-registers ()
4203 (when (gdb-get-buffer 'gdb-registers-buffer)
4204 (gdb-input "-data-list-changed-registers"
4205 'gdb-changed-registers-handler
4206 'gdb-get-changed-registers)))
4207
4208 (defun gdb-changed-registers-handler ()
4209 (setq gdb-changed-registers nil)
4210 (dolist (register-number
4211 (bindat-get-field (gdb-json-partial-output) 'changed-registers))
4212 (push register-number gdb-changed-registers)))
4213
4214 (defun gdb-register-names-handler ()
4215 ;; Don't use pending triggers because this handler is called
4216 ;; only once (in gdb-init-1)
4217 (setq gdb-register-names nil)
4218 (dolist (register-name
4219 (bindat-get-field (gdb-json-partial-output) 'register-names))
4220 (push register-name gdb-register-names))
4221 (setq gdb-register-names (reverse gdb-register-names)))
4222 \f
4223
4224 (defun gdb-get-source-file-list ()
4225 "Create list of source files for current GDB session.
4226 If buffers already exist for any of these files, `gud-minor-mode'
4227 is set in them."
4228 (goto-char (point-min))
4229 (while (re-search-forward gdb-source-file-regexp nil t)
4230 (push (read (match-string 1)) gdb-source-file-list))
4231 (dolist (buffer (buffer-list))
4232 (with-current-buffer buffer
4233 (when (member buffer-file-name gdb-source-file-list)
4234 (gdb-init-buffer)))))
4235
4236 (defun gdb-get-main-selected-frame ()
4237 "Trigger for `gdb-frame-handler' which uses main current thread.
4238 Called from `gdb-update'."
4239 (gdb-input (gdb-current-context-command "-stack-info-frame")
4240 'gdb-frame-handler
4241 'gdb-get-main-selected-frame))
4242
4243 (defun gdb-frame-handler ()
4244 "Set `gdb-selected-frame' and `gdb-selected-file' to show
4245 overlay arrow in source buffer."
4246 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
4247 (when frame
4248 (setq gdb-selected-frame (bindat-get-field frame 'func))
4249 (setq gdb-selected-file (bindat-get-field frame 'fullname))
4250 (setq gdb-frame-number (bindat-get-field frame 'level))
4251 (setq gdb-frame-address (bindat-get-field frame 'addr))
4252 (let ((line (bindat-get-field frame 'line)))
4253 (setq gdb-selected-line (and line (string-to-number line)))
4254 (when (and gdb-selected-file gdb-selected-line)
4255 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
4256 (gud-display-frame)))
4257 (if gud-overlay-arrow-position
4258 (let ((buffer (marker-buffer gud-overlay-arrow-position))
4259 (position (marker-position gud-overlay-arrow-position)))
4260 (when buffer
4261 (with-current-buffer buffer
4262 (setq fringe-indicator-alist
4263 (if (string-equal gdb-frame-number "0")
4264 nil
4265 '((overlay-arrow . hollow-right-triangle))))
4266 (setq gud-overlay-arrow-position (make-marker))
4267 (set-marker gud-overlay-arrow-position position))))))))
4268
4269 (defconst gdb-prompt-name-regexp
4270 (concat "value=\\(" gdb--string-regexp "\\)"))
4271
4272 (defun gdb-get-prompt ()
4273 "Find prompt for GDB session."
4274 (goto-char (point-min))
4275 (setq gdb-prompt-name nil)
4276 (re-search-forward gdb-prompt-name-regexp nil t)
4277 (setq gdb-prompt-name (read (match-string 1)))
4278 ;; Insert first prompt.
4279 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
4280
4281 ;;;; Window management
4282 (defun gdb-display-buffer (buf)
4283 "Show buffer BUF, and make that window dedicated."
4284 (let ((window (display-buffer buf)))
4285 (set-window-dedicated-p window t)
4286 window))
4287
4288 ;; (let ((answer (get-buffer-window buf 0)))
4289 ;; (if answer
4290 ;; (display-buffer buf nil 0) ;Deiconify frame if necessary.
4291 ;; (let ((window (get-lru-window)))
4292 ;; (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
4293 ;; 'gdbmi)
4294 ;; (let ((largest (get-largest-window)))
4295 ;; (setq answer (split-window largest))
4296 ;; (set-window-buffer answer buf)
4297 ;; (set-window-dedicated-p answer t)
4298 ;; answer)
4299 ;; (set-window-buffer window buf)
4300 ;; window)))))
4301
4302
4303 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
4304 "Find window displaying a buffer with the same
4305 `gdb-buffer-type' as BUF and show BUF there. If no such window
4306 exists, just call `gdb-display-buffer' for BUF. If the window
4307 found is already dedicated, split window according to
4308 SPLIT-HORIZONTAL and show BUF in the new window."
4309 (if buf
4310 (when (not (get-buffer-window buf))
4311 (let* ((buf-type (gdb-buffer-type buf))
4312 (existing-window
4313 (get-window-with-predicate
4314 #'(lambda (w)
4315 (and (eq buf-type
4316 (gdb-buffer-type (window-buffer w)))
4317 (not (window-dedicated-p w)))))))
4318 (if existing-window
4319 (set-window-buffer existing-window buf)
4320 (let ((dedicated-window
4321 (get-window-with-predicate
4322 #'(lambda (w)
4323 (eq buf-type
4324 (gdb-buffer-type (window-buffer w)))))))
4325 (if dedicated-window
4326 (set-window-buffer
4327 (split-window dedicated-window nil split-horizontal) buf)
4328 (gdb-display-buffer buf))))))
4329 (error "Null buffer")))
4330 \f
4331 ;;; Shared keymap initialization:
4332
4333 (let ((menu (make-sparse-keymap "GDB-Windows")))
4334 (define-key gud-menu-map [displays]
4335 `(menu-item "GDB-Windows" ,menu
4336 :visible (eq gud-minor-mode 'gdbmi)))
4337 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
4338 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
4339 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
4340 (define-key menu [disassembly]
4341 '("Disassembly" . gdb-display-disassembly-buffer))
4342 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
4343 (define-key menu [inferior]
4344 '("IO" . gdb-display-io-buffer))
4345 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
4346 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
4347 (define-key menu [breakpoints]
4348 '("Breakpoints" . gdb-display-breakpoints-buffer)))
4349
4350 (let ((menu (make-sparse-keymap "GDB-Frames")))
4351 (define-key gud-menu-map [frames]
4352 `(menu-item "GDB-Frames" ,menu
4353 :visible (eq gud-minor-mode 'gdbmi)))
4354 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
4355 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
4356 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
4357 (define-key menu [disassembly]
4358 '("Disassembly" . gdb-frame-disassembly-buffer))
4359 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
4360 (define-key menu [inferior]
4361 '("IO" . gdb-frame-io-buffer))
4362 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
4363 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
4364 (define-key menu [breakpoints]
4365 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
4366
4367 (let ((menu (make-sparse-keymap "GDB-MI")))
4368 (define-key menu [gdb-customize]
4369 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
4370 :help "Customize Gdb Graphical Mode options."))
4371 (define-key menu [gdb-many-windows]
4372 '(menu-item "Display Other Windows" gdb-many-windows
4373 :help "Toggle display of locals, stack and breakpoint information"
4374 :button (:toggle . gdb-many-windows)))
4375 (define-key menu [gdb-restore-windows]
4376 '(menu-item "Restore Window Layout" gdb-restore-windows
4377 :help "Restore standard layout for debug session."))
4378 (define-key menu [sep1]
4379 '(menu-item "--"))
4380 (define-key menu [all-threads]
4381 '(menu-item "GUD controls all threads"
4382 (lambda ()
4383 (interactive)
4384 (setq gdb-gud-control-all-threads t))
4385 :help "GUD start/stop commands apply to all threads"
4386 :button (:radio . gdb-gud-control-all-threads)))
4387 (define-key menu [current-thread]
4388 '(menu-item "GUD controls current thread"
4389 (lambda ()
4390 (interactive)
4391 (setq gdb-gud-control-all-threads nil))
4392 :help "GUD start/stop commands apply to current thread only"
4393 :button (:radio . (not gdb-gud-control-all-threads))))
4394 (define-key menu [sep2]
4395 '(menu-item "--"))
4396 (define-key menu [gdb-customize-reasons]
4397 '(menu-item "Customize switching..."
4398 (lambda ()
4399 (interactive)
4400 (customize-option 'gdb-switch-reasons))))
4401 (define-key menu [gdb-switch-when-another-stopped]
4402 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped
4403 gdb-switch-when-another-stopped
4404 "Automatically switch to stopped thread"
4405 "GDB thread switching %s"
4406 "Switch to stopped thread"))
4407 (define-key gud-menu-map [mi]
4408 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
4409
4410 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
4411 ;; GDB-MI menu will need to be moved to gud.el. We can't use
4412 ;; tool-bar-local-item-from-menu here because it appends new buttons
4413 ;; to toolbar from right to left while we want our A/T throttle to
4414 ;; show up right before Run button.
4415 (define-key-after gud-tool-bar-map [all-threads]
4416 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
4417 :image (find-image '((:type xpm :file "gud/thread.xpm")))
4418 :visible (and (eq gud-minor-mode 'gdbmi)
4419 gdb-non-stop
4420 (not gdb-gud-control-all-threads)))
4421 'run)
4422
4423 (define-key-after gud-tool-bar-map [current-thread]
4424 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
4425 :image (find-image '((:type xpm :file "gud/all.xpm")))
4426 :visible (and (eq gud-minor-mode 'gdbmi)
4427 gdb-non-stop
4428 gdb-gud-control-all-threads))
4429 'all-threads)
4430
4431 (defun gdb-frame-gdb-buffer ()
4432 "Display GUD buffer in another frame."
4433 (interactive)
4434 (display-buffer-other-frame gud-comint-buffer))
4435
4436 (defun gdb-display-gdb-buffer ()
4437 "Display GUD buffer."
4438 (interactive)
4439 (pop-to-buffer gud-comint-buffer nil 0))
4440
4441 (defun gdb-set-window-buffer (name &optional ignore-dedicated window)
4442 "Set buffer of selected window to NAME and dedicate window.
4443
4444 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
4445 window is dedicated."
4446 (unless window (setq window (selected-window)))
4447 (when ignore-dedicated
4448 (set-window-dedicated-p window nil))
4449 (set-window-buffer window (get-buffer name))
4450 (set-window-dedicated-p window t))
4451
4452 (defun gdb-setup-windows ()
4453 "Layout the window pattern for option `gdb-many-windows'."
4454 (gdb-get-buffer-create 'gdb-locals-buffer)
4455 (gdb-get-buffer-create 'gdb-stack-buffer)
4456 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4457 (set-window-dedicated-p (selected-window) nil)
4458 (switch-to-buffer gud-comint-buffer)
4459 (delete-other-windows)
4460 (let ((win0 (selected-window))
4461 (win1 (split-window nil ( / ( * (window-height) 3) 4)))
4462 (win2 (split-window nil ( / (window-height) 3)))
4463 (win3 (split-window-right)))
4464 (gdb-set-window-buffer (gdb-locals-buffer-name) nil win3)
4465 (select-window win2)
4466 (set-window-buffer
4467 win2
4468 (if gud-last-last-frame
4469 (gud-find-file (car gud-last-last-frame))
4470 (if gdb-main-file
4471 (gud-find-file gdb-main-file)
4472 ;; Put buffer list in window if we
4473 ;; can't find a source file.
4474 (list-buffers-noselect))))
4475 (setq gdb-source-window (selected-window))
4476 (let ((win4 (split-window-right)))
4477 (gdb-set-window-buffer
4478 (gdb-get-buffer-create 'gdb-inferior-io) nil win4))
4479 (select-window win1)
4480 (gdb-set-window-buffer (gdb-stack-buffer-name))
4481 (let ((win5 (split-window-right)))
4482 (gdb-set-window-buffer (if gdb-show-threads-by-default
4483 (gdb-threads-buffer-name)
4484 (gdb-breakpoints-buffer-name))
4485 nil win5))
4486 (select-window win0)))
4487
4488 (define-minor-mode gdb-many-windows
4489 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
4490 In this case it starts with two windows: one displaying the GUD
4491 buffer and the other with the source file with the main routine
4492 of the debugged program. Non-nil means display the layout shown for
4493 `gdb'."
4494 :global t
4495 :group 'gdb
4496 :version "22.1"
4497 (if (and gud-comint-buffer
4498 (buffer-name gud-comint-buffer))
4499 (ignore-errors
4500 (gdb-restore-windows))))
4501
4502 (defun gdb-restore-windows ()
4503 "Restore the basic arrangement of windows used by gdb.
4504 This arrangement depends on the value of option `gdb-many-windows'."
4505 (interactive)
4506 (switch-to-buffer gud-comint-buffer) ;Select the right window and frame.
4507 (delete-other-windows)
4508 (if gdb-many-windows
4509 (gdb-setup-windows)
4510 (when (or gud-last-last-frame gdb-show-main)
4511 (let ((win (split-window)))
4512 (set-window-buffer
4513 win
4514 (if gud-last-last-frame
4515 (gud-find-file (car gud-last-last-frame))
4516 (gud-find-file gdb-main-file)))
4517 (setq gdb-source-window win)))))
4518
4519 ;; Called from `gud-sentinel' in gud.el:
4520 (defun gdb-reset ()
4521 "Exit a debugging session cleanly.
4522 Kills the gdb buffers, and resets variables and the source buffers."
4523 ;; The gdb-inferior buffer has a pty hooked up to the main gdb
4524 ;; process. This pty must be deleted explicitly.
4525 (let ((pty (get-process "gdb-inferior")))
4526 (if pty (delete-process pty)))
4527 ;; Find gdb-mi buffers and kill them.
4528 (dolist (buffer (buffer-list))
4529 (unless (eq buffer gud-comint-buffer)
4530 (with-current-buffer buffer
4531 (if (eq gud-minor-mode 'gdbmi)
4532 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4533 (kill-buffer nil)
4534 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4535 (setq gud-minor-mode nil)
4536 (kill-local-variable 'tool-bar-map)
4537 (kill-local-variable 'gdb-define-alist))))))
4538 (setq gdb-disassembly-position nil)
4539 (setq overlay-arrow-variable-list
4540 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4541 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4542 (setq gdb-stack-position nil)
4543 (setq overlay-arrow-variable-list
4544 (delq 'gdb-stack-position overlay-arrow-variable-list))
4545 (setq gdb-thread-position nil)
4546 (setq overlay-arrow-variable-list
4547 (delq 'gdb-thread-position overlay-arrow-variable-list))
4548 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4549 (setq gud-running nil)
4550 (setq gdb-active-process nil)
4551 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4552
4553 (defun gdb-get-source-file ()
4554 "Find the source file where the program starts and display it with related
4555 buffers, if required."
4556 (goto-char (point-min))
4557 (if (re-search-forward gdb-source-file-regexp nil t)
4558 (setq gdb-main-file (read (match-string 1))))
4559 (if gdb-many-windows
4560 (gdb-setup-windows)
4561 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4562 (and gdb-show-main
4563 gdb-main-file
4564 (display-buffer (gud-find-file gdb-main-file))))
4565 (gdb-force-mode-line-update
4566 (propertize "ready" 'face font-lock-variable-name-face)))
4567
4568 ;;from put-image
4569 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4570 "Put string PUTSTRING in front of POS in the current buffer.
4571 PUTSTRING is displayed by putting an overlay into the current buffer with a
4572 `before-string' string that has a `display' property whose value is
4573 PUTSTRING."
4574 (let ((string (make-string 1 ?x))
4575 (buffer (current-buffer)))
4576 (setq putstring (copy-sequence putstring))
4577 (let ((overlay (make-overlay pos pos buffer))
4578 (prop (or dprop
4579 (list (list 'margin 'left-margin) putstring))))
4580 (put-text-property 0 1 'display prop string)
4581 (if sprops
4582 (add-text-properties 0 1 sprops string))
4583 (overlay-put overlay 'put-break t)
4584 (overlay-put overlay 'before-string string))))
4585
4586 ;;from remove-images
4587 (defun gdb-remove-strings (start end &optional buffer)
4588 "Remove strings between START and END in BUFFER.
4589 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4590 BUFFER nil or omitted means use the current buffer."
4591 (unless buffer
4592 (setq buffer (current-buffer)))
4593 (dolist (overlay (overlays-in start end))
4594 (when (overlay-get overlay 'put-break)
4595 (delete-overlay overlay))))
4596
4597 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4598 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4599 (start (- (car posns) 1))
4600 (end (+ (cdr posns) 1))
4601 (putstring (if enabled "B" "b"))
4602 (source-window (get-buffer-window (current-buffer) 0)))
4603 (add-text-properties
4604 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4605 putstring)
4606 (if enabled
4607 (add-text-properties
4608 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4609 (add-text-properties
4610 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4611 (gdb-remove-breakpoint-icons start end)
4612 (if (display-images-p)
4613 (if (>= (or left-fringe-width
4614 (if source-window (car (window-fringes source-window)))
4615 gdb-buffer-fringe-width) 8)
4616 (gdb-put-string
4617 nil (1+ start)
4618 `(left-fringe breakpoint
4619 ,(if enabled
4620 'breakpoint-enabled
4621 'breakpoint-disabled))
4622 'gdb-bptno bptno
4623 'gdb-enabled enabled)
4624 (when (< left-margin-width 2)
4625 (save-current-buffer
4626 (setq left-margin-width 2)
4627 (if source-window
4628 (set-window-margins
4629 source-window
4630 left-margin-width right-margin-width))))
4631 (put-image
4632 (if enabled
4633 (or breakpoint-enabled-icon
4634 (setq breakpoint-enabled-icon
4635 (find-image `((:type xpm :data
4636 ,breakpoint-xpm-data
4637 :ascent 100 :pointer hand)
4638 (:type pbm :data
4639 ,breakpoint-enabled-pbm-data
4640 :ascent 100 :pointer hand)))))
4641 (or breakpoint-disabled-icon
4642 (setq breakpoint-disabled-icon
4643 (find-image `((:type xpm :data
4644 ,breakpoint-xpm-data
4645 :conversion disabled
4646 :ascent 100 :pointer hand)
4647 (:type pbm :data
4648 ,breakpoint-disabled-pbm-data
4649 :ascent 100 :pointer hand))))))
4650 (+ start 1)
4651 putstring
4652 'left-margin))
4653 (when (< left-margin-width 2)
4654 (save-current-buffer
4655 (setq left-margin-width 2)
4656 (let ((window (get-buffer-window (current-buffer) 0)))
4657 (if window
4658 (set-window-margins
4659 window left-margin-width right-margin-width)))))
4660 (gdb-put-string
4661 (propertize putstring
4662 'face (if enabled
4663 'breakpoint-enabled 'breakpoint-disabled))
4664 (1+ start)))))
4665
4666 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4667 (gdb-remove-strings start end)
4668 (if (display-images-p)
4669 (remove-images start end))
4670 (when remove-margin
4671 (setq left-margin-width 0)
4672 (let ((window (get-buffer-window (current-buffer) 0)))
4673 (if window
4674 (set-window-margins
4675 window left-margin-width right-margin-width)))))
4676
4677 \f
4678 ;;; Functions for inline completion.
4679
4680 (defvar gud-gdb-fetch-lines-in-progress)
4681 (defvar gud-gdb-fetch-lines-string)
4682 (defvar gud-gdb-fetch-lines-break)
4683 (defvar gud-gdb-fetched-lines)
4684
4685 (defun gud-gdbmi-completions (context command)
4686 "Completion table for GDB/MI commands.
4687 COMMAND is the prefix for which we seek completion.
4688 CONTEXT is the text before COMMAND on the line."
4689 (let ((gud-gdb-fetch-lines-in-progress t)
4690 (gud-gdb-fetch-lines-string nil)
4691 (gud-gdb-fetch-lines-break (length context))
4692 (gud-gdb-fetched-lines nil)
4693 ;; This filter dumps output lines to `gud-gdb-fetched-lines'.
4694 (gud-marker-filter #'gud-gdbmi-fetch-lines-filter))
4695 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
4696 (gdb-input (concat "complete " context command)
4697 (lambda () (setq gud-gdb-fetch-lines-in-progress nil)))
4698 (while gud-gdb-fetch-lines-in-progress
4699 (accept-process-output (get-buffer-process gud-comint-buffer))))
4700 (gud-gdb-completions-1 gud-gdb-fetched-lines)))
4701
4702 (defun gud-gdbmi-fetch-lines-filter (string)
4703 "Custom filter function for `gud-gdbmi-completions'."
4704 (setq string (concat gud-gdb-fetch-lines-string
4705 (gud-gdbmi-marker-filter string)))
4706 (while (string-match "\n" string)
4707 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
4708 gud-gdb-fetched-lines)
4709 (setq string (substring string (match-end 0))))
4710 "")
4711
4712 (provide 'gdb-mi)
4713
4714 ;;; gdb-mi.el ends here