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