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