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