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